Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sdslnmd
Created July 23, 2013 08:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sdslnmd/6060738 to your computer and use it in GitHub Desktop.
Save sdslnmd/6060738 to your computer and use it in GitHub Desktop.
top java thread performance
#!/bin/bash
redEcho()
{
echo -e "\033[1;31m$@\033[0m"
}
pid=`pgrep java`
uuid=`date +%s`_${RANDOM}_$$
jstackFile=/tmp/${uuid}_${pid}
sudo -u tomcat jstack ${pid} > ${jstackFile}
top -b -n1 -d0.01 -H -p ${pid} | grep tomcat | sort -k9 -r -n | head -5 | while read threadLine ; do
threadId=`echo ${threadLine} | awk '{print $1}'`
threadId0x=`printf %x ${threadId}`
pcpu=`echo ${threadLine} | awk '{print $9}'`
user=`echo ${threadLine} | awk '{print $2}'`
[ ! -f "${jstackFile}" ] &&
{
{ redEcho "Fail to jstack java process ${pid}"; rm ${jstackFile} ; continue; }
}
redEcho "The stack of busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) of java process(${pid} of user(${user}):"
sed "/nid=0x${threadId0x}/,/^$/p" -n ${jstackFile}
done
rm /tmp/${uuid}_*
@oldratlee
Copy link

Got it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment