查看tomcat项目中,具体占用cpu高的线程。

时间:2021-05-11 10:34:12

1、查看主进程占用cpu高:

此处主进程:27823
[root@storage- ~]# top
top - :0: up days, :, 3 users, load average: 13.12, 13.31, 13.23
Tasks: total, running, sleeping, stopped, zombie
%Cpu(s):100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : total, free, used, buff/cache
KiB Swap: total, free, used. avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
root .527g S 783.3 20.0 : java
root R 16.7 0.0 :00.01 top
root S 0.0 0.1 :29.91 systemd
root S 0.0 0.0 :03.18 kthreadd
root S 0.0 0.0 :31.90 ksoftirqd/

2、按照线程占用cpu由高到低进行排查:

通过主进程查看所有的线程,此处只列出了一部分线程TID为线程号,THREAD为线程的各项内容

[root@storage- ~]# ps -mp  -o THREAD,tid,time | sort -rn |head -6
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
storage+ 38.3 - - - - -::
storage+ 38.3 - - - - -::
storage+ 38.3 - - - - -::
storage+ - - - - - - -::
storage+ 12.1 - futex_ - - ::

找到线程占用高的线程id (tid) 例如:28296 28273 28241 各占用30%多

将线程号转化为16进制:

[root@zz-p-storagesdk-api01 ~]# printf "%x\n"
6e88
[root@storage- service]# printf "%x\n"
6e71
[root@storage- service]# printf "%x\n"
6e51

通过主进程好搜索所有的线程执行的内容,

注意:必须使用项目运行的用户,以及使用的jdk版本下的jstack去查看

[root@zz-p-storagesdk-api01 ~]# sudo -u root /usr/local/jdk1.8/bin/jstack 27823 |grep 6e88 -A 30

[root@zz-p-storagesdk-api01 ~]# sudo -u root /usr/local/jdk1.8/bin/jstack 27823 |grep 6e71 -A 30

[root@zz-p-storagesdk-api01 ~]# sudo -u root /usr/local/jdk1.8/bin/jstack 27823 |grep 6e51 -A 30