Skip to content

Instantly share code, notes, and snippets.

@vamdt
Created May 11, 2017 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vamdt/efff12e9c51651c7a55d2484a311d388 to your computer and use it in GitHub Desktop.
Save vamdt/efff12e9c51651c7a55d2484a311d388 to your computer and use it in GitHub Desktop.
Print high cpu thread jstack message
#!/bin/bash
usage() {
echo "Usage: $0 -p <pid> [-n <thread number>]"
}
# args check
while getopts ":p:n:" arg; do
case "$arg" in
p)
PID=${OPTARG}
;;
n)
NUM=${OPTARG}
;;
*)
usage
;;
esac
done
# check process
if [[ -z $PID ]]; then
usage
exit
fi
# check process exists
if [[ -z "$(jps -l | grep $PID)" ]]; then
echo "process of $PID is not exists"
exit
fi
echo $NUM
if [[ -z $NUM ]]; then
NUM=100
fi
JSTACKOUT=$(jstack $PID)
THREADS=$(ps -Tp $PID -o %cpu,tid,time | tail -n +2 | sort -rn | awk '{ if($1!="0.0") print $2}' | xargs printf "%x\n" | head -n $NUM )
echo $THREADS
for tid in $THREADS; do
echo "******************************************************************* ThreadId=$tid **************************************************************************"
echo "$JSTACKOUT" | grep $tid -A 15
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment