Created
May 11, 2017 07:17
-
-
Save vamdt/efff12e9c51651c7a55d2484a311d388 to your computer and use it in GitHub Desktop.
Print high cpu thread jstack message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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