Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to match /proc/PID data with jstack
#!/bin/bash
PID=$1
CPUFILE=/tmp/cpu-$PID-0
LASTFILE=/tmp/cpu-$PID-1
>$LASTFILE
function cleanup { rm $CPUFILE $LASTFILE ; }
trap cleanup EXIT
cd /proc/${PID}/task/
while :
do
echo
for TID in *
do
CPU=$(grep se.sum_exec_runtime $TID/sched ) 2>/dev/null
[ "$CPU" = "" ] || echo $TID $CPU
done | sed 's/[a-z_\.]* : //' | sort -g > $CPUFILE
LINE=$(join $LASTFILE $CPUFILE | while read TID LAST CPU
do
TICK=$(echo $CPU - $LAST | bc -q)
[ "$TICK" = "0" ] || echo $TICK $TID
done | sed 's/^\./0./' | sort -gr | head -20 | sed 's/^\(.*\) \(.*\)$/\2=\1/' )
cp $CPUFILE $LASTFILE
echo $(date +'%Y-%m-%d %H:%M:%S'):$PID: $LINE
TOP=$(printf '%x' $(echo $LINE | cut -d= -f1 ))
jstack $PID | awk "BEGIN { found=0 } /nid=0x$TOP/ { found=1; } /^$/ { found=0;} { if (found==1) print \$0 ; } "
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment