Created
September 17, 2014 17:49
-
-
Save hongjiang/72abde56478f893f8e82 to your computer and use it in GitHub Desktop.
检查最耗cpu的java线程,给出cpu的使用率和堆栈信息,只能用于linux系统
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 | |
if [ $# -eq 0 ];then | |
echo "please enter java pid" | |
exit -1 | |
fi | |
pid=$1 | |
jstack_cmd="" | |
if [[ $JAVA_HOME != "" ]]; then | |
jstack_cmd="$JAVA_HOME/bin/jstack" | |
else | |
r=`which jstack 2>/dev/null` | |
if [[ $r != "" ]]; then | |
jstack_cmd=$r | |
else | |
echo "can not find jstack" | |
exit -2 | |
fi | |
fi | |
line=`top -H -o %CPU -b -n 1 -p $pid | sed '1,/^$/d' | grep -v $pid | awk 'NR==2'` | |
echo "$line" | awk '{print "tid: "$1," cpu: %"$9}' | |
tid_0x=`printf "%0x" $(echo "$line" | awk '{print $1}')` | |
$jstack_cmd $pid | grep $tid_0x -A20 | sed -n '1,/^$/p' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment