Skip to content

Instantly share code, notes, and snippets.

@markito
Forked from mrbuk/pseudo_java_profiler.sh
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save markito/438f2e35bfcf23c6da9b to your computer and use it in GitHub Desktop.

Select an option

Save markito/438f2e35bfcf23c6da9b to your computer and use it in GitHub Desktop.
#!/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