1.查看cpu占用率高的进程
1.1> top
1.2> 用ps -eo pid,pcpu | sort -n -k 2
2.确定高cpu消耗的线程
2.1>top -h
2.2>ps h -eo pid,tid,pcpu | sort -n -k 3
3.查看进程的线程
3.1>pstree -p pid
3.2>查看线程的相信信息:cat /proc/进程号/task/线程号/status
4.实时线程进程的资源占用信息
4.1>top -h -p pid
5.查看该进程下的所有线程
5.1>ps -efl | grep pid
5.2>根据pid,查找占用cpu较高的线程
ps -mp pid -o thread,tid,time
6.使用jstack定位线程堆栈信息,精确找到异常代码
6.1>查看某进程下占用cpu较高的线程:ps p 替换进程号 -l -o pcpu,pid,tid,time,tname,cmd
%cpu pid tid time tty cmd
0.0 1538 1538 00:00:00 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
97.1 1538 1539 04:39:56 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.1 1538 1540 00:00:25 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.0 1538 1541 00:00:00 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.0 1538 1542 00:00:00 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.0 1538 1543 00:00:00 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.1 1538 1544 00:00:33 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.0 1538 1545 00:00:12 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.0 1538 1546 00:00:00 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.5 1538 1547 00:01:43 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
0.0 1538 7170 00:00:00 ? java -classpath .:/usr/java/jdk1.8.0_131/lib/dt.jar:/usr/java/jdk1.8.0_131/lib/tools.jar:lib/adsl.jar:lib/commons-beanutils-1.8
6.2>将获取的线程号(十进制数)转换成十六进制
printf "%x\n" 1539
结果:603
6.3>结合进程号和线程号,利用jstack查到异常代码所在行
jstack -l 1538| grep 0x603 -a 10
结果:
[root@cloud ~]# jstack -l 1538| grep 0x603 -a 10
"main" #1 prio=5 os_prio=0 tid=0x00007f0088008800 nid=0x603 runnable [0x00007f008ca04000]
java.lang.thread.state: runnable
at com.xxxx.xxxx.tester.pppoe.main(unknown source)
locked ownable synchronizers:
- none
"vm thread" os_prio=0 tid=0x00007f008806d000 nid=0x604 runnable "vm periodic task thread" os_prio=0 tid=0x00007f00880c0000 nid=0x60b waiting on condition
此时,就可以看到占用cpu的程序了。
6.4>将信息输出到文件
jstack -l 1538 > 1538.pid
使用vi查看。
6.5>注意:需要在多个时间段提出多个 thread dump信息,然后综合进行对比分析,单独分析一个文件是没有意义的
脚本(cpu飙高时):
#!/bin/bash
#
# 当jvm占用cpu特别高时,查看cpu正在做什么
# 可输入两个参数:1、pid java进程id,必须参数 2、打印线程id上下文行数,可选参数,默认打印10行
#
pid=$1
if test -z $pid
then
echo "pid can not be null!"
exit
else
echo "checking pid($pid)"
fi
if test -z "$(jps -l | cut -d '' -f 1 | grep $pid)"
then
echo "process of $pid is not exists"
exit
fi
linenum=$2
if test -z $linenum
then
$linenum=10
fi
jstack $pid >> "$pid".bak
ps -mp $pid -o thread,tid,time | sort -k2r | awk '{if ($1 !="user" && $2 != "0.0" && $8 !="-") print $8;}' | xargs printf "%x\n" >> "$pid".tmp
tidarray="$( cat $pid.tmp)"
for tid in $tidarray
do
echo "******************************************************************* threadid=$tid **************************************************************************"
cat "$pid".bak | grep $tid -a $linenum
done
rm -rf $pid.bak
rm -rf $pid.tmp