~percona-toolkit-dev/percona-toolkit/fix-empty-table-bug-987393

« back to all changes in this revision

Viewing changes to lib/bash/summary_common.sh

  • Committer: Daniel Nichter
  • Date: 2012-04-03 16:14:55 UTC
  • mfrom: (217.6.22 2.0.3)
  • Revision ID: daniel@percona.com-20120403161455-ntv33vju9o6njtqv
Merge summary-tools-2.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This program is copyright 2011-2012 Percona Inc.
 
2
# Feedback and improvements are welcome.
 
3
#
 
4
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 
5
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 
6
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify it under
 
9
# the terms of the GNU General Public License as published by the Free Software
 
10
# Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
 
11
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
 
12
# licenses.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along with
 
15
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
16
# Place, Suite 330, Boston, MA  02111-1307  USA.
 
17
# ###########################################################################
 
18
# summary_common package
 
19
# ###########################################################################
 
20
 
 
21
# Package: summary_common
 
22
# Common functions between the summary packages.
 
23
 
 
24
set -u
 
25
 
 
26
CMD_FILE="$( _which file 2>/dev/null )"
 
27
CMD_NM="$( _which nm 2>/dev/null )"
 
28
CMD_OBJDUMP="$( _which objdump 2>/dev/null )"
 
29
 
 
30
# Tries to find the niceness of the passed in PID. First with ps, and
 
31
# failing that, with a bit of C, using getpriority().
 
32
# Returns the nice for the pid, or "?" if it can't find any.
 
33
get_nice_of_pid () {
 
34
   local pid="$1"
 
35
   local niceness="$(ps -p $pid -o nice | awk '$1 !~ /[^0-9]/ {print $1; exit}')"
 
36
 
 
37
   if [ -n "${niceness}" ]; then
 
38
      echo $niceness
 
39
   else
 
40
      local tmpfile="$TMPDIR/nice_through_c.tmp.c"
 
41
      _d "Getting the niceness from ps failed, somehow. We are about to try this:"
 
42
      cat <<EOC > "$tmpfile"
 
43
#include <sys/time.h>
 
44
#include <sys/resource.h>
 
45
#include <errno.h>
 
46
#include <stdio.h>
 
47
 
 
48
int main(void) {
 
49
   int priority = getpriority(PRIO_PROCESS, $pid);
 
50
   if ( priority == -1 && errno == ESRCH ) {
 
51
      return 1;
 
52
   }
 
53
   else {
 
54
      printf("%d\\n", priority);
 
55
      return 0;
 
56
   }
 
57
}
 
58
 
 
59
EOC
 
60
      local c_comp=$(_which gcc)
 
61
      if [ -z "${c_comp}" ]; then
 
62
         c_comp=$(_which cc)
 
63
      fi
 
64
      _d "$tmpfile: $( cat "$tmpfile" )"
 
65
      _d "$c_comp -xc \"$tmpfile\" -o \"$tmpfile\" && eval \"$tmpfile\""
 
66
      $c_comp -xc "$tmpfile" -o "$tmpfile" 2>/dev/null && eval "$tmpfile" 2>/dev/null
 
67
      if [ $? -ne 0 ]; then
 
68
         echo "?"
 
69
         _d "Failed to get a niceness value for $pid"
 
70
      fi
 
71
   fi
 
72
}
 
73
 
 
74
# Fetches the oom value for a given pid.
 
75
# To avoi deprecation warnings, tries /proc/PID/oom_score_adj first.
 
76
# Will only work if /proc/cpuinfo is available.
 
77
get_oom_of_pid () {
 
78
   local pid="$1"
 
79
   local oom_adj=""
 
80
 
 
81
   if [ -n "${pid}" -a -e /proc/cpuinfo ]; then
 
82
      if [ -s "/proc/$pid/oom_score_adj" ]; then
 
83
         oom_adj=$(cat "/proc/$pid/oom_score_adj" 2>/dev/null)
 
84
         _d "For $pid, the oom value is $oom_adj, retreived from oom_score_adj"
 
85
      else
 
86
         oom_adj=$(cat "/proc/$pid/oom_adj" 2>/dev/null)
 
87
         _d "For $pid, the oom value is $oom_adj, retreived from oom_adj"
 
88
      fi
 
89
   fi
 
90
 
 
91
   if [ -n "${oom_adj}" ]; then
 
92
      echo "${oom_adj}"
 
93
   else
 
94
      echo "?"
 
95
      _d "Can't find the oom value for $pid"
 
96
   fi
 
97
}
 
98
 
 
99
has_symbols () {
 
100
   local executable="$(_which "$1")"
 
101
   local has_symbols=""
 
102
 
 
103
   if    [ "${CMD_FILE}" ] \
 
104
      && [ "$($CMD_FILE "${executable}" | grep 'not stripped' )" ]; then
 
105
      has_symbols=1
 
106
   elif    [ "${CMD_NM}" ] \
 
107
        || [ "${CMD_OBJDMP}" ]; then
 
108
      if    [ "${CMD_NM}" ] \
 
109
         && [ !"$("${CMD_NM}" -- "${executable}" 2>&1 | grep 'File format not recognized' )" ]; then
 
110
         if [ -z "$( $CMD_NM -- "${executable}" 2>&1 | grep ': no symbols' )" ]; then
 
111
            has_symbols=1
 
112
         fi
 
113
      elif [ -z "$("${CMD_OBJDUMP}" -t -- "${executable}" | grep '^no symbols$' )" ]; then
 
114
         has_symbols=1
 
115
      fi
 
116
   fi
 
117
 
 
118
   if [ "${has_symbols}" ]; then
 
119
      echo "Yes"
 
120
   else
 
121
      echo "No"
 
122
   fi
 
123
}
 
124
 
 
125
setup_data_dir () {
 
126
   local existing_dir="$1"
 
127
   local data_dir=""
 
128
   if [ -z "$existing_dir" ]; then
 
129
      # User didn't specify a --save-data dir, so use a sub-dir in our tmpdir.
 
130
      mkdir "$TMPDIR/data" || die "Cannot mkdir $TMPDIR/data"
 
131
      data_dir="$TMPDIR/data"
 
132
   else
 
133
      # Check the user's --save-data dir.
 
134
      if [ ! -d "$existing_dir" ]; then
 
135
         mkdir "$existing_dir" || die "Cannot mkdir $existing_dir"
 
136
      elif [ "$( ls -A "$existing_dir" )" ]; then
 
137
         die "--save-samples directory isn't empty, halting."
 
138
      fi
 
139
      touch "$existing_dir/test" || die "Cannot write to $existing_dir"
 
140
      rm "$existing_dir/test"    || die "Cannot rm $existing_dir/test"
 
141
      data_dir="$existing_dir"
 
142
   fi
 
143
   echo "$data_dir"
 
144
}
 
145
 
 
146
# gets a value from the passed in file.
 
147
get_var () {
 
148
   local varname="$1"
 
149
   local file="$2"
 
150
   awk -v pattern="${varname}" '$1 == pattern { if (length($2)) { len = length($1); print substr($0, len+index(substr($0, len+1), $2)) } }' "${file}"
 
151
}
 
152
 
 
153
# ###########################################################################
 
154
# End summary_common package
 
155
# ###########################################################################