improve file lock handling for errors

This commit is contained in:
Brendan Gregg
2014-07-18 00:53:45 -07:00
parent b81197724a
commit 69025cf803
2 changed files with 19 additions and 14 deletions

14
iosnoop
View File

@@ -83,10 +83,14 @@ function warn {
function die {
echo >&2 "$@"
[[ -e $flock ]] && rm $flock
exit 1
}
function ldie {
[[ -e $flock ]] && rm $flock
die "$@"
}
### process options
while getopts d:hn:p:st opt
do
@@ -120,17 +124,17 @@ echo $$ > $flock || die "ERROR: unable to write $flock."
[[ -x /usr/bin/$use ]] && awk=$use || awk=awk
### setup and begin tracing
cd $tracing || die "ERROR: accessing tracing. Root user? Kernel has FTRACE?"
cd $tracing || ldie "ERROR: accessing tracing. Root user? Kernel has FTRACE?"
echo nop > current_tracer
warn "echo $bufsize_kb > buffer_size_kb"
if (( opt_pid )); then
if ! echo "common_pid==$pid" > events/block/block_rq_issue/filter; then
die "ERROR: setting -p $pid. Exiting."
if ! echo "commonpid==$pid" > events/block/block_rq_issue/filter; then
ldie "ERROR: setting -p $pid. Exiting."
fi
fi
if ! echo 1 > events/block/block_rq_issue/enable || \
! echo 1 > events/block/block_rq_complete/enable; then
die "ERROR: enabling block I/O tracepoints. Exiting."
ldie "ERROR: enabling block I/O tracepoints. Exiting."
fi
(( opt_start )) && printf "%-14s " "STARTs"
(( opt_end )) && printf "%-14s " "ENDs"

View File

@@ -89,6 +89,11 @@ function die {
exit 1
}
function ldie {
[[ -e $flock ]] && rm $flock
die "$@"
}
### process options
while getopts d:hp: opt
do
@@ -116,25 +121,21 @@ echo $$ > $flock || die "ERROR: unable to write $flock."
### setup and commence tracing
if ! cd $tracing; then
echo >&2 "ERROR: accessing tracing. Root user? Kernel has FTRACE?"
exit 1
ldie "ERROR: accessing tracing. Root user? Kernel has FTRACE?"
fi
sysctl -q kernel.ftrace_enabled=1 # doesn't set exit status
read mode < current_tracer
[[ "$mode" != "nop" ]] && die "ERROR: ftrace active (current_tracer=$mode)"
[[ "$mode" != "nop" ]] && ldie "ERROR: ftrace active (current_tracer=$mode)"
if (( opt_pid )); then
if ! echo $pid > set_ftrace_pid; then
echo >&2 "ERROR: setting -p $pid (PID exist?). Exiting."
exit 1
ldie "ERROR: setting -p $pid (PID exist?). Exiting."
fi
fi
if ! echo "$funcs" > set_ftrace_filter; then
echo >&2 "ERROR: enabling \"$funcs\". Exiting."
exit 1
ldie "ERROR: enabling \"$funcs\". Exiting."
fi
if ! echo function > current_tracer; then
echo >&2 "ERROR: setting current_tracer to \"function\". Exiting."
exit 1
ldie "ERROR: setting current_tracer to \"function\". Exiting."
fi
### print trace buffer