查看java项目中,具体占用cpu高的线程。
写的也不错:https://www.cnblogs.com/fengzheng/p/13935066.html
1、查看主进程占用cpu高:
此处主进程:27823
[root@storage-02 ~]# top top - 1:02:33 up 2 days, 1:54, 3 users, load average: 13.12, 13.31, 13.23 Tasks: 180 total, 3 running, 177 sleeping, 0 stopped, 0 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 : 8002908 total, 582432 free, 3442856 used, 3977620 buff/cache KiB Swap: 8388604 total, 8388604 free, 0 used. 3802688 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 27823 root 20 0 7041832 1.527g 13328 S 783.3 20.0 14494:52 java 21735 root 20 0 157720 2272 1552 R 16.7 0.0 0:00.01 top 1 root 20 0 190988 4012 2416 S 0.0 0.1 48:29.91 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:03.18 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:31.90 ksoftirqd/0
2、按照线程占用cpu由高到低进行排查:
通过主进程查看所有的线程,此处只列出了一部分线程TID为线程号,THREAD为线程的各项内容
[root@storage-02 ~]# ps -mp 27823 -o THREAD,tid,time | sort -rn |head -6 USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME storage+ 38.3 19 - - - - 28296 2-22:05:22 storage+ 38.3 19 - - - - 28273 2-22:07:21 storage+ 38.3 19 - - - - 28241 2-22:10:49 storage+ 132 - - - - - - 10-01:55:19 storage+ 12.1 19 - futex_ - - 28212 22:15:30
找到线程占用高的线程id (tid) 例如:28296 28273 28241 各占用30%多
将线程号转化为16进制:
[root@zz-p-storagesdk-api01 ~]# printf "%x\n" 28296 6e88 [root@storage-02 service]# printf "%x\n" 28273 6e71 [root@storage-02 service]# printf "%x\n" 28241 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
python3