線上Java應(yīng)用,在業(yè)務(wù)高峰期的時(shí)候經(jīng)常出現(xiàn)CPU跑高,需要查看實(shí)時(shí)的線程占用cpu情況,下面是一個(gè)很好用的腳本,可以快速導(dǎo)出每個(gè)線程的占用CPU情況,結(jié)合jstack日志,排查到具體的線程類名。
#!/bin/sh
export LANG="zh_CN.UTF-8";
export LC_ALL="zh_CN.UTF-8";
LOG_FILE="/tmp/jkiller.log";
JSTACK_FILE="/tmp/jstack.log";
PID="$1";
shift;
i=0;
j="$1";
if [ -z "${j}" ]; then
j=5;
fi
ps -mp ${PID} -o THREAD,tid,time | sort -rn > ${LOG_FILE};
jstack ${PID} > ${JSTACK_FILE};
for LINE in `cat ${LOG_FILE}|gawk -F '-' '{print $4}'|gawk -F ' ' '{print $1}'`
do
i=$(($i+1));
if (($i>$j)); then
break;
fi;
XPID=`printf "%x\n" ${LINE}`;
echo -ne "\033[32m";
echo ${XPID};
echo -e "\033[34m";
grep -A 10 "0x${XPID}" ${JSTACK_FILE};
echo -e "\e[0m";
done;