cpu飚高分析
cpu飚高分析
#!/bin/bash
# utf-8
# description: 分析java进程cpu飚高,打出具体的线程栈
# get javaHome
function GetJavaHome(){
javaHome=$(ps -ef | grep java | grep -v jcollector | grep -v grep | head -1 | awk "{print \$8}" 2>/dev/null)
javaHome=$(echo $javaHome | sed 's#/java##g')
if [[ -z $javaHome ]];then
javaHome=$(echo $JAVA_BIN)
fi
}
# command
function InitCommands(){
jstackCommand="${javaHome}/jstack"
jmapCommand="${javaHome}/jmap"
jstatCommand="${javaHome}/jstat"
}
# top
function PrintTopInfo(){
echo "当前top信息如下"
/usr/bin/top -b -n 1
echo "##############################################"
}
# pids
function GetJavaPids(){
pids=$(ps -ef | grep java | grep -v jcollector | grep -v grep | awk "{print \$2}")
echo $pids
}
function GetJavaBaseInfo(){
local pid=$1
appName=$(ps -ef | grep $pid | grep -v grep | awk "{print \$(NF-1)}")
echo "应用名:${appName}"
echo "pid:$pid"
echo "##############################################"
}
function GetThreadInfo(){
local pid=$1
threadNum=$(ps p ${pid} -L -o pcpu,pid,tid,time,tname,stat,psr | /bin/sort -n -k1 -r | wc -l 2>/dev/null)
echo "线程数:$threadNum"
echo "线程情况如下,按照占用cpu由大到小排列"
ps p ${pid} -L -o pcpu,pid,tid,time,tname,stat,psr | /bin/sort -n -k1 -r 2>/dev/null
echo "占用最高的cpu线程数为:"
cpuHighThreadInfo=$(ps p ${pid} -L -o pcpu,pid,tid,time,tname,stat,psr | /bin/sort -n -k1 -r 2>/dev/null)
cpuHighThreadId=$(echo $cpuHighThreadInfo | head -1 | awk '{print $3}')
echo "$cpuHighThreadId"
echo "转换为16进制如下:"
cpuHighThreadId16=$(printf "%x\n" ${cpuHighThreadId})
echo $cpuHighThreadId16
echo "所有线程信息:"
${jstackCommand} -F $pid | grep -A 50 ${cpuHighThreadId16}
}
function GetInfo(){
pids=$(GetJavaPids)
for pid in $pids
do
GetJavaBaseInfo ${pid}
GetThreadInfo ${pid}
done
}
function main(){
GetJavaHome
InitCommands
echo "使用分析工具版本:${javaHome}"
GetInfo
}
main
原创:做时间的朋友