~percona-toolkit-dev/percona-toolkit/pt-table-sync-binlog-format-only-if-not-statement

« back to all changes in this revision

Viewing changes to bin/pt-ioprofile

MergeĀ lp:~percona-toolkit-dev/percona-toolkit/fix-945079-tmpdir-should-use-TEMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
153
153
   OPT_ERRS=0
154
154
   OPT_VERSION=""
155
155
   OPT_HELP=""
156
 
   PO_DIR="$TMPDIR/po"
 
156
   PO_DIR="$PT_TMPDIR/po"
157
157
 
158
158
   if [ ! -d "$PO_DIR" ]; then
159
159
      mkdir "$PO_DIR"
357
357
            opt="$(echo $opt | awk -F= '{print $1}')"
358
358
         fi
359
359
 
360
 
         if [ -f "$TMPDIR/po/$opt" ]; then
361
 
            spec="$TMPDIR/po/$opt"
 
360
         if [ -f "$PT_TMPDIR/po/$opt" ]; then
 
361
            spec="$PT_TMPDIR/po/$opt"
362
362
         else
363
 
            spec=$(grep "^short form:-$opt\$" "$TMPDIR"/po/* | cut -d ':' -f 1)
 
363
            spec=$(grep "^short form:-$opt\$" "$PT_TMPDIR"/po/* | cut -d ':' -f 1)
364
364
            if [ -z "$spec"  ]; then
365
365
               option_error "Unknown option: $real_opt"
366
366
               continue
430
430
 
431
431
set -u
432
432
 
433
 
TMPDIR=""
 
433
PT_TMPDIR=""
434
434
 
435
435
mk_tmpdir() {
436
436
   local dir="${1:-""}"
439
439
      if [ ! -d "$dir" ]; then
440
440
         mkdir "$dir" || die "Cannot make tmpdir $dir"
441
441
      fi
442
 
      TMPDIR="$dir"
 
442
      PT_TMPDIR="$dir"
443
443
   else
444
444
      local tool="${0##*/}"
445
445
      local pid="$$"
446
 
      TMPDIR=`mktemp -d /tmp/${tool}.${pid}.XXXXXX` \
 
446
      PT_TMPDIR=`mktemp -d -t "${tool}.${pid}.XXXXXX"` \
447
447
         || die "Cannot make secure tmpdir"
448
448
   fi
449
449
}
450
450
 
451
451
rm_tmpdir() {
452
 
   if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
453
 
      rm -rf "$TMPDIR"
 
452
   if [ -n "$PT_TMPDIR" ] && [ -d "$PT_TMPDIR" ]; then
 
453
      rm -rf "$PT_TMPDIR"
454
454
   fi
455
 
   TMPDIR=""
 
455
   PT_TMPDIR=""
456
456
}
457
457
 
458
458
# ###########################################################################
519
519
# pid function fd_no size timing filename
520
520
# The arguments are the files to summarize.
521
521
tabulate_strace() {
522
 
   cat > $TMPDIR/tabulate_strace.awk <<EOF
 
522
   cat > $PT_TMPDIR/tabulate_strace.awk <<EOF
523
523
   BEGIN {
524
524
      # These are function names, or partial function names, that we care about.
525
525
      # Later we will ignore any function whose name doesn't look like these.
616
616
      }
617
617
   }
618
618
EOF
619
 
   awk -f $TMPDIR/tabulate_strace.awk "$@"
 
619
   awk -f $PT_TMPDIR/tabulate_strace.awk "$@"
620
620
}
621
621
 
622
622
# Takes as input the output from tabulate_strace.  Arguments are just a subset
628
628
   local group_by="$3"
629
629
   local file="$4"
630
630
 
631
 
   cat > "$TMPDIR/summarize_strace.awk" <<EOF
 
631
   cat > "$PT_TMPDIR/summarize_strace.awk" <<EOF
632
632
   BEGIN {
633
633
      # These are function names, or partial function names, that we care about.
634
634
      # Later we will ignore any function whose name doesn't look like these.
739
739
   }
740
740
EOF
741
741
 
742
 
   awk -f $TMPDIR/summarize_strace.awk "$file" > $TMPDIR/summarized_samples
 
742
   awk -f $PT_TMPDIR/summarize_strace.awk "$file" > $PT_TMPDIR/summarized_samples
743
743
   if [ "$group_by" != "all" ]; then
744
 
      head -n1 $TMPDIR/summarized_samples
745
 
      tail -n +2 $TMPDIR/summarized_samples | sort -rn -k1
 
744
      head -n1 $PT_TMPDIR/summarized_samples
 
745
      tail -n +2 $PT_TMPDIR/summarized_samples | sort -rn -k1
746
746
   else
747
 
      grep TOTAL $TMPDIR/summarized_samples
748
 
      grep -v TOTAL $TMPDIR/summarized_samples | sort -rn -k1
 
747
      grep TOTAL $PT_TMPDIR/summarized_samples
 
748
      grep -v TOTAL $PT_TMPDIR/summarized_samples | sort -rn -k1
749
749
   fi
750
750
}
751
751
 
752
752
main() {
753
753
   if [ $# -gt 0 ]; then
754
754
      # Summarize the files the user passed in.
755
 
      tabulate_strace "$@" > $TMPDIR/tabulated_samples
 
755
      tabulate_strace "$@" > $PT_TMPDIR/tabulated_samples
756
756
   else
757
757
      # There's no file to analyze, so we'll make one.
758
758
      if which strace > /dev/null 2>&1; then
759
759
 
760
 
         local samples=${OPT_SAVE_SAMPLES:-"$TMPDIR/samples"}
 
760
         local samples=${OPT_SAVE_SAMPLES:-"$PT_TMPDIR/samples"}
761
761
 
762
762
         # Get the PID of the process to profile, unless the user
763
763
         # gave us it explicitly with --profile-pid.
807
807
            kill -s 18 $proc_pid
808
808
 
809
809
            # Summarize the output we just generated.
810
 
            tabulate_strace "$samples" > $TMPDIR/tabulated_samples
 
810
            tabulate_strace "$samples" > $PT_TMPDIR/tabulated_samples
811
811
         else
812
812
            echo "Cannot determine PID of $OPT_PROFILE_PROCESS process" >&2
813
813
            exit 1
822
822
      $OPT_AGGREGATE \
823
823
      $OPT_CELL      \
824
824
      $OPT_GROUP_BY  \
825
 
      "$TMPDIR/tabulated_samples"
 
825
      "$PT_TMPDIR/tabulated_samples"
826
826
}
827
827
 
828
828
# Execute the program if it was not included from another file.