Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 181 lines (174 sloc) 4.61 KB
#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: t; sh-basic-offset: 8; sh-indentation: 8; sh-indent-for-case-alt: + -*-
script_path=`dirname $0`
script_name=`basename $0`
pbench_bin="`cd ${script_path}/..; /bin/pwd`"
# source the base script
. "$pbench_bin"/base
# Perftool scripts must provide the following functions
# 1) Install the tool
# 2) Start data collection
# 3) Stop data collection
# 4) post-process the data
# Defaults
tool=perf
tool_bin=/usr/bin/$tool
group=default
dir="/tmp"
mode=""
iteration="1"
options="none"
record_opts="record -a --freq=100"
report_opts="report --show-nr-samples -I"
# Process options and arguments
opts=$(getopt -o d --longoptions "dir:,group:,iteration:,callgraph,record-opts:,report-opts:,start,stop,install,postprocess" -n "getopt.sh" -- "$@");
if [ $? -ne 0 ]; then
printf "\n"
printf "$script_name: you specified an invalid option\n\n"
printf "The following options are available: \n\n"
printf -- "\t--install, install this perf tool\n"
printf "\n"
printf -- "\t--start|stop|postprocess start/stop/post-process the data collection\n"
printf -- "\t--iteration=int the iteration (required)\n"
printf -- "\t--group=str the perftool group (required)\n"
printf -- "\t--dir=str directory to store data collection (required)\n"
printf -- "\t--callgraph generate a call graph (--record-opts and --report-opts should not be used with this option)\n"
printf -- "\t--record-opts=str options one would use to record perf data, for example: 'record -a'\n"
printf -- "\t--report-opts=str options one would use to report perf data, for example: 'report'\n"
exit 1
fi
eval set -- "$opts";
while true; do
case "$1" in
--install)
mode="install"
shift;
;;
--start)
mode="start"
shift;
;;
--stop)
mode="stop"
shift;
;;
--postprocess)
mode="postprocess"
shift;
;;
-d|--dir)
shift;
if [ -n "$1" ]; then
dir="$1"
shift
fi
;;
-g|--group)
shift;
if [ -n "$1" ]; then
group="$1"
shift
fi
;;
-i|--iteration)
shift;
if [ -n "$1" ]; then
iteration="$1"
shift
fi
;;
--callgraph)
shift;
if [ -n "$1" ]; then
record_opts="record -a --freq=100 -g"
report_opts="report -I -g"
fi
;;
-r|--record-opts)
shift;
if [ -n "$1" ]; then
record_opts="$1"
shift
fi
;;
-p|--report-opts)
shift;
if [ -n "$1" ]; then
report_opts="$1"
shift
fi
;;
--)
shift;
break;
;;
esac
done
tool_cmd="$tool_bin $record_opts"
tool_dir="$dir/tools-$group"
tool_output_dir=$tool_dir/$tool # all tools keep data in their tool specific dir
tool_cmd_file="$tool_output_dir/$tool.cmd"
tool_pid_file=$pbench_tmp/$group.$iteration.$tool.pid
tool_output_file=$tool_output_dir/$tool.txt
case "$mode" in
install)
;;
start)
if ! cat /proc/mounts | grep -q debugfs; then
mount -t debugfs none /sys/kernel/debug
fi
mkdir $tool_output_dir
pushd $tool_output_dir >/dev/null
echo "$tool_cmd" >$tool_cmd_file
# call the tool and background it...
$tool_cmd > "$tool_output_dir/perf-stdout.txt" 2> "$tool_output_dir/perf-stderr.txt" & echo $! >"$tool_pid_file"
wait
popd >/dev/null
;;
stop)
if [[ -s "$tool_pid_file" ]]; then
pushd $tool_output_dir >/dev/null
pid=`cat "$tool_pid_file"`
if [ ! -z "$pid" ]; then
kill -SIGINT $pid
# wait for perf to finish recording.
# if you do not wait, 'perf report' will not be correct.
# perf is not a child process, so we cannot use "wait".
# this is an alternative.
pidcmd=`ps -p $pid | tail -1 | awk '{print $4}'`
while [ -d /proc/$pid ]; do
debug_log "waiting for PID $pid ($pidcmd) to finish"
sleep 0.5
done
fi
/bin/rm "$tool_pid_file"
popd >/dev/null
else
warn_log "[$script_name]: tool is not running, nothing to kill"
fi
;;
postprocess)
pushd $tool_output_dir >/dev/null
$tool_bin $report_opts -i ./perf.data --stdio >perf-report.txt 2>&1
# report per-cpu
mkdir -p perf-percpu
for cpu_id in `cat /proc/cpuinfo | grep processor | awk -F: '{print $2}'`; do
$tool_bin $report_opts -i ./perf.data --stdio --cpu $cpu_id >./perf-percpu/perf-report-cpu_$cpu_id.txt 2>/dev/null
xz --threads=0 perf-percpu/perf-report-cpu_$cpu_id.txt
chmod go+r perf-percpu/perf-report-cpu_$cpu_id.txt.xz
done
if [ -f perf.data ]; then
xz --threads=0 perf.data
chmod go+r perf.data.xz
fi
# only postprocess the perf report if the default options are used
if echo $record_opts | grep -E -q -- -g\|--callgraph; then
debug_log "Not going to run perf postprocess script since this report contains a callgraph"
else
$script_path/postprocess/$script_name-postprocess .
fi
xz --threads=0 perf-report.txt
chmod go+r perf-report.txt.xz
popd >/dev/null
;;
esac