1
# This program is copyright 2011-2012 Percona Inc.
2
# Feedback and improvements are welcome.
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.
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
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
# ###########################################################################
21
# Package: summary_common
22
# Common functions between the summary packages.
26
CMD_FILE="$( _which file 2>/dev/null )"
27
CMD_NM="$( _which nm 2>/dev/null )"
28
CMD_OBJDUMP="$( _which objdump 2>/dev/null )"
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.
35
local niceness="$(ps -p $pid -o nice | awk '$1 !~ /[^0-9]/ {print $1; exit}')"
37
if [ -n "${niceness}" ]; then
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"
44
#include <sys/resource.h>
49
int priority = getpriority(PRIO_PROCESS, $pid);
50
if ( priority == -1 && errno == ESRCH ) {
54
printf("%d\\n", priority);
60
local c_comp=$(_which gcc)
61
if [ -z "${c_comp}" ]; then
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
69
_d "Failed to get a niceness value for $pid"
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.
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"
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"
91
if [ -n "${oom_adj}" ]; then
95
_d "Can't find the oom value for $pid"
100
local executable="$(_which "$1")"
103
if [ "${CMD_FILE}" ] \
104
&& [ "$($CMD_FILE "${executable}" | grep 'not stripped' )" ]; then
106
elif [ "${CMD_NM}" ] \
107
|| [ "${CMD_OBJDMP}" ]; then
109
&& [ !"$("${CMD_NM}" -- "${executable}" 2>&1 | grep 'File format not recognized' )" ]; then
110
if [ -z "$( $CMD_NM -- "${executable}" 2>&1 | grep ': no symbols' )" ]; then
113
elif [ -z "$("${CMD_OBJDUMP}" -t -- "${executable}" | grep '^no symbols$' )" ]; then
118
if [ "${has_symbols}" ]; then
126
local existing_dir="$1"
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"
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."
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"
146
# gets a value from the passed in file.
150
awk -v pattern="${varname}" '$1 == pattern { if (length($2)) { len = length($1); print substr($0, len+index(substr($0, len+1), $2)) } }' "${file}"
153
# ###########################################################################
154
# End summary_common package
155
# ###########################################################################