Skip to content

Instantly share code, notes, and snippets.

@legatoo
Created November 23, 2018 07:04
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 legatoo/34cef9b8b0432c17d6500390cab68cff to your computer and use it in GitHub Desktop.
Save legatoo/34cef9b8b0432c17d6500390cab68cff to your computer and use it in GitHub Desktop.
谁在消耗cpu
#!/bin/sh
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment