Skip to content

Instantly share code, notes, and snippets.

@jarvis657
Created November 25, 2017 15:00
Show Gist options
  • Save jarvis657/653097ab3b79e9906e83757c2c07d285 to your computer and use it in GitHub Desktop.
Save jarvis657/653097ab3b79e9906e83757c2c07d285 to your computer and use it in GitHub Desktop.
查找用户java占用最高的线程
typeset top=${1:-10}
typeset pid=${2:-$(pgrep -u $USER java)}
typeset tmp_file=/tmp/java_${pid}_$$.trace
# fix for alibaba-inc.com
export JAVA_HOME=/opt/taobao/java
$JAVA_HOME/bin/jstack $pid > $tmp_file
ps H -eo user,pid,ppid,tid,time,%cpu --sort=%cpu --no-headers\
| tail -$top\
| awk -v "pid=$pid" '$2==pid{print $4"\t"$6}'\
| while read line;
do
typeset nid=$(echo "$line"|awk '{printf("0x%x",$1)}')
typeset cpu=$(echo "$line"|awk '{print $2}')
awk -v "cpu=$cpu" '/nid='"$nid"'/,/^$/{print $0"\t"(isF++?"":"cpu="cpu"%");}' $tmp_file
done
rm -f $tmp_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment