11#! /bin/bash
2+ # -*- mode: shell-script; indent-tabs-mode: t; sh-basic-offset: 8; sh-indentation: 8; sh-indent-for-case-alt: + -*-
23
34script_path=` dirname $0 `
45script_name=` basename $0 `
@@ -235,6 +236,63 @@ case "$tool" in
235236 tool_cmd=" $script_path /datalog/$tool -datalog $interval $username $password $tool_stdout_file "
236237 ;;
237238esac
239+
240+ function safe_kill() {
241+ # safer kill: check for strange situations and deal with them.
242+ # If there is a pid, try to kill it. If the kill succeeds,
243+ # return success, but if it fails, try to see if the pid is
244+ # still running: if so, try to kill it with a KILL and return
245+ # failure. Eat any error message.
246+ local tool=$1
247+ local pid=$2
248+ local signal=${3:- TERM}
249+ local pidlist p pid_to_kill len rc
250+
251+ if [ -z " $pid " ] ; then
252+ # should not happen
253+ return 1
254+ fi
255+
256+ # check that the pid corresponds to the tool
257+ pidlist=$( pidof -x $tool )
258+ typeset -i len=0
259+ for p in $pidlist ; do
260+ len=$len +1
261+ if [[ $p == $pid ]] ; then
262+ pid_to_kill=$pid
263+ fi
264+ done
265+ # hack: individual tools will have a different number of pids running.
266+ # we might just delete the warning if it gets too cumbersome.
267+ if [ $len -ge 4 -o $len -ge 3 -a $tool != " turbostat" ] ; then
268+ warn_log " Too many pids for $tool : $pidlist -- maybe old tools running? Use pbench-kill-tools."
269+ fi
270+
271+ rc=0
272+ if [ ! -z " $pid_to_kill " ] ; then
273+ kill -s $signal $pid_to_kill 2> /dev/null
274+ rc=$?
275+ fi
276+
277+ [[ $rc == 0 ]] && return 0
278+
279+ # check if the process is still running.
280+ pidlist=$( pidof -x $tool )
281+ for p in $pidlist ; do
282+ if [[ $p == $pid ]] ; then
283+ # why is the pid still there?
284+ kill -s KILL $pid 2> /dev/null
285+ return 2
286+ # else
287+ # why are there other tool processes running?
288+ # should we kill them?
289+ fi
290+ done
291+ return 0
292+ }
293+
294+ # return code
295+ rc=0
238296case " $mode " in
239297 install)
240298 case " $tool " in
@@ -294,29 +352,35 @@ case "$mode" in
294352 debug_log " $script_name : running $tool_cmd "
295353 " $tool_cmd_file " 2> " $tool_stderr_file " & echo $! > " $tool_pid_file "
296354 wait
355+ rc=0
297356 popd > /dev/null
298357 ;;
299358 stop)
300359 debug_log " stopping $script_name "
301360 pid=` cat $tool_pid_file `
302- if [ " $tool " == " kvmtrace" ]; then
303- if [ -z " $sleep_cmd " ]; then
304- # kill the perf cmd
305- kill -s SIGINT $pid
306- else
307- # kill the sleep process if it still exists
308- sleep_pid=` ps --ppid $pid | pgrep sleep`
309- if [ ! -z " $sleep_pid " ]; then
310- kill $sleep_pid
361+ if [ ! -z " $pid " ] ; then
362+ if [ " $tool " == " kvmtrace" ]; then
363+ if [ -z " $sleep_cmd " ]; then
364+ # kill the perf cmd
365+ safe_kill $tool $pid INT
366+ else
367+ # kill the sleep process if it still exists
368+ sleep_pid=` ps --ppid $pid | pgrep sleep`
369+ if [ ! -z " $sleep_pid " ]; then
370+ safe_kill $tool $sleep_pid
371+ fi
311372 fi
373+ # wait for the trace-cmd pid to complete
374+ while [ -d /proc/$pid ]; do
375+ debug_log " waiting for PID $pid to die"
376+ sleep 0.5
377+ done
378+ rc=0
379+ else
380+ safe_kill $tool $pid
381+ rc=$?
382+ /bin/rm -f " $tool_pid_file "
312383 fi
313- # wait for the trace-cmd pid to complete
314- while [ -d /proc/$pid ]; do
315- debug_log " waiting for PID $pid to die"
316- sleep 0.5
317- done
318- else
319- kill $pid && /bin/rm -f " $tool_pid_file "
320384 fi
321385 ;;
322386 postprocess)
@@ -328,6 +392,7 @@ case "$mode" in
328392 done
329393 pushd $tool_output_dir > /dev/null
330394 blkparse $devs > blkparse-stdout.txt 2> blkparse-stderr.txt
395+ rc=$?
331396 bzip2 blkparse-stdout.txt &
332397 for dev in $devs ; do
333398 bzip2 $dev .blktrace.* &
@@ -344,6 +409,7 @@ case "$mode" in
344409 fi
345410 ssh $vm cat /proc/kallsyms > kallsyms-guest.txt
346411 $script_path /postprocess/kvmtrace-postprocess .
412+ rc=$?
347413 bzip2 --fast kvmtrace-report-stdout.txt &
348414 bzip2 --fast trace.dat &
349415 bzip2 --fast kallsyms-guest.txt &
@@ -352,8 +418,11 @@ case "$mode" in
352418 * )
353419 debug_log " postprocessing $script_name "
354420 $script_path /postprocess/$script_name -postprocess " $tool_output_dir "
421+ rc=$?
355422 ;;
356423 esac
357424 popd > /dev/null
358425 ;;
359426esac
427+
428+ exit $rc
0 commit comments