Skip to content

Instantly share code, notes, and snippets.

@olamy
Forked from gregw/threadwatch.sh
Created August 17, 2020 12:29
Show Gist options
  • Save olamy/62413f8a265371dc5fb84f39edbfdb01 to your computer and use it in GitHub Desktop.
Save olamy/62413f8a265371dc5fb84f39edbfdb01 to your computer and use it in GitHub Desktop.
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