定位进程id
使用top命令找出占cpu高的java进程
[bigdataservice@bjxg-bd-slave12 bigdataservice]$ top
top - 17:54:51 up 325 days, 2:49, 2 users, load average: 3.70, 3.91, 3.88
Tasks: 290 total, 1 running, 289 sleeping, 0 stopped, 0 zombie
%Cpu(s): 19.6 us, 1.2 sy, 0.0 ni, 79.0 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem : 13182059+total, 13213764 free, 34492852 used, 84113984 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 91661168 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
34511 bigdata+ 20 0 24.2g 2.6g 12748 S 61.8 2.1 159075:54 java
60030 yarn 20 0 6642956 3.5g 28584 S 6.3 2.8 17337:00 java
57978 yarn 20 0 3017184 643696 55708 S 2.3 0.5 79:04.07 java
59449 bigdata+ 20 0 10.0g 1.9g 6860 S 2.0 1.5 10205:35 java
19690 yarn 20 0 4300596 2.2g 51728 S 1.0 1.8 168:49.23 java
24707 root 20 0 1258824 50764 1632 S 1.0 0.0 5493:35 python2
很明显占cpu最高的java进程id为 34511
列出目标进程的线程cpu情况
执行top -p 34511 -H (-H参数是为了显示具体线程id列表)
[bigdataservice@bjxg-bd-slave12 bigdataservice]$ top -p 34511 -H
top - 17:59:51 up 325 days, 2:54, 2 users, load average: 3.16, 3.58, 3.77
Threads: 93 total, 0 running, 93 sleeping, 0 stopped, 0 zombie
%Cpu(s): 19.8 us, 1.6 sy, 0.0 ni, 78.5 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 13182059+total, 13107176 free, 34512124 used, 84201296 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 91639376 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
34896 bigdata+ 20 0 24.2g 2.6g 12748 S 8.0 2.1 16060:15 java
34885 bigdata+ 20 0 24.2g 2.6g 12748 S 1.7 2.1 5358:18 java
61227 bigdata+ 20 0 24.2g 2.6g 12748 S 1.7 2.1 4738:06 java
35481 bigdata+ 20 0 24.2g 2.6g 12748 S 1.7 2.1 4734:51 java
19044 bigdata+ 20 0 24.2g 2.6g 12748 S 1.7 2.1 1278:44 java
19053 bigdata+ 20 0 24.2g 2.6g 12748 S 1.7 2.1 1278:35 java
29379 bigdata+ 20 0 24.2g 2.6g 12748 S 1.7 2.1 1156:39 java
34881 bigdata+ 20 0 24.2g 2.6g 12748 S 1.3 2.1 5346:11 java
由显示结果可知,该进程下,最占cpu的线程id 是34896
根据线程id定位代码
将该线程id转为16进制形式
如图,转换结果是0x8850
或者
直接在linux机器上执行 printf '%x\n' 34896
结果也是一样,这种方式更快捷!
接下来利用jstack直接定位java代码
执行命令
jstack 34511 | grep -A 20 0x8850 (grep -A 20表示匹配到目标行,再往后多显示20行)
[bigdataservice@bjxg-bd-slave12 bigdataservice]$ jstack 34511 | grep -A 20 0x8850
"kafka-producer-network-thread | producer-1" #44 daemon prio=5 os_prio=0 tid=0x00007f155c16d800 nid=0x8850 runnable [0x00007f162c394000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000003c0deda38> (a sun.nio.ch.Util$2)
- locked <0x00000003c0deda28> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000003c0dd5648> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at org.apache.kafka.common.network.Selector.select(Selector.java:691)
at org.apache.kafka.common.network.Selector.poll(Selector.java:411)
at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:510)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:239)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:163)
at java.lang.Thread.run(Thread.java:745)
"DestroyJavaVM" #43 prio=5 os_prio=0 tid=0x00007f1650008800 nid=0x86d0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"registrationTask1" #42 prio=5 os_prio=0 tid=0x00007f1651da4000 nid=0x884c waiting on condition [0x00007f162c695000]
成功将目标java代码定位出来了