-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavaThreadOrderByCPU.sh
35 lines (26 loc) · 1.08 KB
/
javaThreadOrderByCPU.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh
# use it in linux
ts=$(date +"%s")
jvmPid=$1
defaultLines=100
defaultTop=20
threadStackLines=${2:-$defaultLines}
topThreads=${3:-$defaultTop}
jvmCapture=$(top -b -n1 | grep java )
threadsTopCapture=$(top -b -n1 -H | grep java )
jstackOutput=$(echo "$(jstack $jvmPid )" )
topOutput=$(echo "$(echo "$threadsTopCapture" | head -n $topThreads | perl -pe 's/\e\[?.*?[\@-~] ?//g' | awk '{gsub(/^ +/,"");print}' | awk '{gsub(/ +|[+-]/," ");print}' | cut -d " " -f 1,9 )\n ")
echo "*************************************************************************************************************"
uptime
echo "Analyzing top $topThreads threads"
echo "*************************************************************************************************************"
printf %s "$topOutput" | while IFS= read line
do
pid=$(echo $line | cut -d " " -f 1)
hexapid=$(printf "%x" $pid)
cpu=$(echo $line | cut -d " " -f 2)
echo -n $cpu"% [$pid] "
echo "$jstackOutput" | grep "tid.*0x$hexapid " -A $threadStackLines | sed -n -e '/0x'$hexapid'/,/tid/ p' | head -n -1
echo "\n"
done
echo "\n"