Skip to content

Instantly share code, notes, and snippets.

@mrbuk
Last active August 29, 2015 14:15
Show Gist options
  • Save mrbuk/414a19f1467aa86b30b2 to your computer and use it in GitHub Desktop.
Save mrbuk/414a19f1467aa86b30b2 to your computer and use it in GitHub Desktop.
A little "pseudo java profiler" for the *NIX command line. Checks the top 10 cpu consuming java threads and prints for each of these the current thread stack. If threads are really really shortlived it can happen that we won't catch them with jstack.
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 jps-filter-grep-expr"
exit 1
fi
jps_filter=$1
# extract the PID of the java process
java_app_pid=$(jps -lvm | grep ${jps_filter} | awk '{print $1}')
# get 10 top consuming threads
top_consuming_threads=$(top -n1 -H -p ${java_app_pid} | \
egrep java | egrep -o '^[^0-9]*[0-9]+ ' | \
sed -r 's/[^0-9]+//g' | head)
temp_file=$(mktemp)
# create jstack_output
jstack ${java_app_pid} > ${temp_file}
# print for every thread the current stack trace
for thread in ${top_consuming_threads}; do
echo "Stack trace for Thread '${thread}':"
printf -v thread_hex "%x" $thread
grep -A10 "nid=0x${thread_hex}" ${temp_file}
echo
done;
rm ${temp_file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment