47
95
# End tmpdir package
48
96
# ###########################################################################
98
# ###########################################################################
99
# parse_options package
100
# This package is a copy without comments from the original. The original
101
# with comments and its test file can be found in the Bazaar repository at,
102
# lib/bash/parse_options.sh
103
# t/lib/bash/parse_options.sh
104
# See https://launchpad.net/percona-toolkit for more information.
105
# ###########################################################################
113
ARGV="" # Non-option args (probably input files)
114
EXT_ARGV="" # Everything after -- (args for an external command)
115
HAVE_EXT_ARGV="" # Got --, everything else is put into EXT_ARGV
116
OPT_ERRS=0 # How many command line option errors
117
OPT_VERSION="" # If --version was specified
118
OPT_HELP="" # If --help was specified
119
PO_DIR="" # Directory with program option spec files
124
local usage="$(grep '^Usage: ' "$file")"
127
echo "For more information, 'man $TOOL' or 'perldoc $file'."
133
if [ "$OPT_VERSION" ]; then
134
local version=$(grep '^pt-[^ ]\+ [0-9]' "$file")
139
if [ "$OPT_HELP" ]; then
142
echo "Command line options:"
146
use warnings FATAL => qw(all);
147
my $lcol = 20; # Allow this much space for option names.
148
my $rcol = 80 - $lcol; # The terminal is assumed to be 80 chars wide.
153
if ( $line =~ s/^long:/ --/ ) {
156
elsif ( $line =~ s/^desc:// ) {
158
my @lines = grep { $_ }
159
$line =~ m/(.{0,$rcol})(?:\s+|\Z)/g;
160
if ( length($name) >= $lcol ) {
161
print $name, "\n", (q{ } x $lcol);
164
printf "%-${lcol}s", $name;
166
print join("\n" . (q{ } x $lcol), @lines);
172
echo "Options and values after processing arguments:"
177
local varname="OPT_$(echo "$opt" | tr a-z- A-Z_)"
178
eval local varvalue=\$$varname
179
if ! grep -q "type:" "$PO_DIR/$opt" >/dev/null; then
180
if [ "$varvalue" -a "$varvalue" = "yes" ];
186
printf -- " --%-30s %s" "$opt" "${varvalue:-(No value)}"
193
if [ $OPT_ERRS -gt 0 ]; then
204
OPT_ERRS=$(($OPT_ERRS + 1))
218
PO_DIR="$PT_TMPDIR/po"
220
if [ ! -d "$PO_DIR" ]; then
222
if [ $? -ne 0 ]; then
223
echo "Cannot mkdir $PO_DIR" >&2
229
if [ $? -ne 0 ]; then
230
echo "Cannot rm -rf $PO_DIR/*" >&2
234
_parse_pod "$file" # Parse POD into program option (po) spec files
235
_eval_po # Eval po into existence with default values
237
if [ $# -ge 2 ] && [ "$1" = "--config" ]; then
239
local user_config_files="$1"
242
for user_config_file in $user_config_files; do
243
_parse_config_files "$user_config_file"
246
_parse_config_files "/etc/percona-toolkit/percona-toolkit.conf" "/etc/percona-toolkit/$TOOL.conf" "$HOME/.percona-toolkit.conf" "$HOME/.$TOOL.conf"
249
_parse_command_line "${@:-""}"
255
cat "$file" | PO_DIR="$PO_DIR" perl -ne '
257
next unless $_ =~ m/^=head1 OPTIONS/;
258
while ( defined(my $para = <>) ) {
259
last if $para =~ m/^=head1/;
261
if ( $para =~ m/^=item --(\S+)/ ) {
263
my $file = "$ENV{PO_DIR}/$opt";
264
open my $opt_fh, ">", $file or die "Cannot open $file: $!";
265
print $opt_fh "long:$opt\n";
268
if ( $para =~ m/^[a-z ]+:/ ) {
271
my ($attrib, $val) = split(/: /, $_);
272
print $opt_fh "$attrib:$val\n";
273
} split(/; /, $para);
277
my ($desc) = $para =~ m/^([^?.]+)/;
278
print $opt_fh "desc:$desc.\n";
288
for opt_spec in "$PO_DIR"/*; do
293
while read key val; do
296
opt=$(echo $val | sed 's/-/_/g' | tr '[:lower:]' '[:upper:]')
304
[ "$val" = "size" ] && size=1
309
if [ "$val" = "yes" ]; then
314
echo "Invalid attribute in $opt_spec: $line" >&2
319
if [ -z "$opt" ]; then
320
echo "No long attribute in option spec $opt_spec" >&2
324
if [ $neg -eq 1 ]; then
325
if [ -z "$default_val" ] || [ "$default_val" != "yes" ]; then
326
echo "Option $opt_spec is negatable but not default: yes" >&2
331
if [ $size -eq 1 -a -n "$default_val" ]; then
332
default_val=$(size_to_bytes $default_val)
335
eval "OPT_${opt}"="$default_val"
339
_parse_config_files() {
341
for config_file in "${@:-""}"; do
342
test -f "$config_file" || continue
344
while read config_opt; do
346
echo "$config_opt" | grep '^[ ]*[^#]' >/dev/null 2>&1 || continue
348
config_opt="$(echo "$config_opt" | sed -e 's/^ *//g' -e 's/ *$//g' -e 's/[ ]*=[ ]*/=/' -e 's/[ ]*#.*$//')"
350
[ "$config_opt" = "" ] && continue
352
if ! [ "$HAVE_EXT_ARGV" ]; then
353
config_opt="--$config_opt"
356
_parse_command_line "$config_opt"
358
done < "$config_file"
360
HAVE_EXT_ARGV="" # reset for each file
365
_parse_command_line() {
368
local next_opt_is_val=""
370
local opt_is_negated=""
372
local required_arg=""
375
for opt in "${@:-""}"; do
376
if [ "$opt" = "--" -o "$opt" = "----" ]; then
380
if [ "$HAVE_EXT_ARGV" ]; then
381
if [ "$EXT_ARGV" ]; then
382
EXT_ARGV="$EXT_ARGV $opt"
389
if [ "$next_opt_is_val" ]; then
391
if [ $# -eq 0 ] || [ $(expr "$opt" : "\-") -eq 1 ]; then
392
option_error "$real_opt requires a $required_arg argument"
398
if [ $(expr "$opt" : "\-") -eq 0 ]; then
399
if [ -z "$ARGV" ]; then
409
if $(echo $opt | grep '^--no[^-]' >/dev/null); then
410
local base_opt=$(echo $opt | sed 's/^--no//')
411
if [ -f "$PT_TMPDIR/po/$base_opt" ]; then
416
opt=$(echo $opt | sed 's/^-*//')
419
if $(echo $opt | grep '^--no-' >/dev/null); then
421
opt=$(echo $opt | sed 's/^--no-//')
424
opt=$(echo $opt | sed 's/^-*//')
428
if $(echo $opt | grep '^[a-z-][a-z-]*=' >/dev/null 2>&1); then
429
val="$(echo $opt | awk -F= '{print $2}')"
430
opt="$(echo $opt | awk -F= '{print $1}')"
433
if [ -f "$PT_TMPDIR/po/$opt" ]; then
434
spec="$PT_TMPDIR/po/$opt"
436
spec=$(grep "^short form:-$opt\$" "$PT_TMPDIR"/po/* | cut -d ':' -f 1)
437
if [ -z "$spec" ]; then
438
option_error "Unknown option: $real_opt"
443
required_arg=$(cat "$spec" | awk -F: '/^type:/{print $2}')
444
if [ "$required_arg" ]; then
452
option_error "Option $real_opt does not take a value"
455
if [ "$opt_is_negated" ]; then
464
if [ "$opt_is_ok" ]; then
465
opt=$(cat "$spec" | grep '^long:' | cut -d':' -f2 | sed 's/-/_/g' | tr '[:lower:]' '[:upper:]')
467
if grep "^type:size" "$spec" >/dev/null; then
468
val=$(size_to_bytes $val)
471
eval "OPT_$opt"="'$val'"
487
echo $size | perl -ne '%f=(B=>1, K=>1_024, M=>1_048_576, G=>1_073_741_824, T=>1_099_511_627_776); m/^(\d+)([kMGT])?/i; print $1 * $f{uc($2 || "B")};'
490
# ###########################################################################
491
# End parse_options package
492
# ###########################################################################
494
# ###########################################################################
496
# This package is a copy without comments from the original. The original
497
# with comments and its test file can be found in the Bazaar repository at,
498
# lib/bash/alt_cmds.sh
499
# t/lib/bash/alt_cmds.sh
500
# See https://launchpad.net/percona-toolkit for more information.
501
# ###########################################################################
508
awk "BEGIN { for(i=1; i<=$i; i++) print i; }"
513
if ! pidof "$cmd" 2>/dev/null; then
514
ps -eo pid,ucomm | awk -v comm="$cmd" '$2 == comm { print $1 }'
520
if ! lsof -p $pid 2>/dev/null; then
521
/bin/ls -l /proc/$pid/fd 2>/dev/null
528
if [ -x /usr/bin/which ]; then
529
/usr/bin/which "$1" 2>/dev/null | awk '{print $1}'
530
elif which which 1>/dev/null 2>&1; then
531
which "$1" 2>/dev/null | awk '{print $1}'
537
# ###########################################################################
538
# End alt_cmds package
539
# ###########################################################################
53
if [ "${OPT_ERR}" ]; then
56
echo "Usage: pt-pmp [OPTIONS] [FILES]" >&2
57
echo "For more information, 'man pt-pmp' or 'perldoc $0'" >&2
61
543
# Actually does the aggregation. The arguments are the max number of functions
62
544
# to aggregate, and the files to read. If maxlen=0, it means infinity. We have
63
545
# to pass the maxlen argument into this function to make maxlen testable.
154
636
# The main program to run.
157
# Get command-line options
167
shift; OPT_b="${1}"; shift;
170
shift; OPT_i="${1}"; shift;
173
shift; OPT_k="${1}"; shift;
176
shift; OPT_l="${1}"; shift;
179
shift; OPT_p="${1}"; shift;
182
shift; OPT_s="${1}"; shift;
185
OPT_ERR="Unknown option ${o}."
190
export OPT_i="${OPT_i:-1}";
191
export OPT_k="${OPT_k:-}";
192
export OPT_l="${OPT_l:-0}";
193
export OPT_b="${OPT_b:-mysqld}";
194
export OPT_p="${OPT_p:-}";
195
export OPT_s="${OPT_s:-0}";
197
if [ -z "${1}" ]; then
198
# There's no file to analyze, so we'll make one.
199
if [ -z "${OPT_p}" ]; then
200
OPT_p=$(pidof -s "${OPT_b}" 2>/dev/null);
201
if [ -z "${OPT_p}" ]; then
202
OPT_p=$(pgrep -o -x "${OPT_b}" 2>/dev/null)
638
local output_file="${OPT_SAVE_SAMPLES:-"$PT_TMPDIR/percona-toolkit"}"
640
if [ -z "$ARGV" ]; then
641
# There are no files to analyze, so we'll make one.
642
if [ -z "$OPT_PID" ]; then
643
OPT_PID=$(pidof -s "$OPT_BINARY" 2>/dev/null);
644
if [ -z "$OPT_PID" ]; then
645
OPT_PID=$(pgrep -o -x "$OPT_BINARY" 2>/dev/null)
204
if [ -z "${OPT_p}" ]; then
205
OPT_p=$(ps -eaf | grep "${OPT_b}" | grep -v grep | awk '{print $2}' | head -n1);
647
if [ -z "$OPT_PID" ]; then
648
OPT_PID=$(ps -eaf | grep "$OPT_BINARY" | grep -v grep | awk '{print $2}' | head -n1);
209
for x in $(seq 1 $OPT_i); do
210
gdb -ex "set pagination 0" -ex "thread apply all bt" -batch -p $OPT_p >> "${OPT_k:-$PT_TMPDIR/percona-toolkit}"
211
date +'TS %N.%s %F %T' >> "${OPT_k:-$PT_TMPDIR/percona-toolkit}"
652
for x in $(_seq $OPT_ITERATIONS); do
653
gdb -ex "set pagination 0" \
654
-ex "thread apply all bt" \
658
date +'TS %N.%s %F %T' >> "$output_file"
216
if [ $# -eq 0 ]; then
217
aggregate_stacktrace "${OPT_l}" "${OPT_k:-$PT_TMPDIR/percona-toolkit}"
663
if [ -z "$ARGV" ]; then
664
aggregate_stacktrace "$OPT_LINES" "$output_file"
219
aggregate_stacktrace "${OPT_l}" "$@"
666
aggregate_stacktrace "$OPT_LINES" $ARGV