Skip to content

Instantly share code, notes, and snippets.

@bsenduran
Last active December 20, 2017 04:41
Show Gist options
  • Save bsenduran/a65cd7c0abc4e95ef6222acb19beedc9 to your computer and use it in GitHub Desktop.
Save bsenduran/a65cd7c0abc4e95ef6222acb19beedc9 to your computer and use it in GitHub Desktop.
Analyze the thread usage of a java application and prints the top CPU consuming threads
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "usage: sh top-consumers.sh <pid> <number-of-consumers>"
exit
fi
lowerBound=$(expr "$2" + 1)
jstack -l $1 > top-consumers_thread_dump.txt &
ps --pid $1 -Lo pid,tid,%cpu,time,nlwp,c > top-consumers_thread_usage.txt
wait
cat top-consumers_thread_usage.txt | sort -k 3 | tail -$lowerBound | head -$2 | awk '{print $2}' | xargs -I {} echo "obase=16; {}" | bc | xargs -I pattern grep -i pattern top-consumers_thread_dump.txt
rm top-consumers_thread_usage.txt top-consumers_thread_dump.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment