1.查找jvm进程ID: jps -lv 或者 ps aux | grep java
2.根据pid,查找占用cpu较高的线程:ps -mp pid -o THREAD,tid,time 如图所示:找到占用cpu最高的tid (可以使用sort命令排序:sort -k 3 -r -n)
3.将tid转换为16进制的数字:printf "%x\n" tid
4.使用jstack命令,查询线程信息,从而定位到具体线程和代码:jstack pid | grep 7ccd -A 30 (注意7ccd就是tid转换成16进制之后的)
===========================以下是实操========================================
root@ecs-c6s-xlarge-2-linux-20200723164331:~# jps -lv
14001 sun.tools.jps.Jps -Denv.class.path=.:/root/znylPro/jdk/jdk1.8.0_131/lib/dt.jar:/root/znylPro/jdk/jdk1.8.0_131/lib/tools.jar -Dapplication.home=/root/znylPro/jdk/jdk1.8.0_131 -Xms8m
27800 eureka.jar -Xmx1024m -Xms256m
13883 manageserver.jar -Xmx1024m -Xms256m
12876 dbserver.jar -Xmx1024m -Xms256m
12927 clientserver.jar -Xmx1024m -Xms256m
root@ecs-c6s-xlarge-2-linux-20200723164331:~# ps -mp pid -oc^CHREAD,tid,time
root@ecs-c6s-xlarge-2-linux-20200723164331:~# top
top - 13:42:00 up 35 days, 20:17, 2 users, load average: 0.92, 0.25, 0.09
Tasks: 131 total, 1 running, 79 sleeping, 0 stopped, 0 zombie
%Cpu(s): 22.7 us, 7.6 sy, 0.0 ni, 69.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8167200 total, 3346476 free, 2589900 used, 2230824 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 5289348 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
13883 root 20 0 3768292 557212 17472 S 135.7 6.8 1:10.09 java
12876 root 20 0 4609588 642780 17976 S 0.3 7.9 0:32.90 java
12927 root 20 0 4608732 636976 18456 S 0.3 7.8 0:35.28 java
14020 root 20 0 41780 3704 3176 R 0.3 0.0 0:00.01 top
1 root 20 0 225500 9192 6672 S 0.0 0.1 0:07.40 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.16 kthreadd
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:+
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_+
7 root 20 0 0 0 0 S 0.0 0.0 0:00.12 ksoftirqd/0
8 root 20 0 0 0 0 I 0.0 0.0 1:27.20 rcu_sched
9 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_bh
10 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
11 root rt 0 0 0 0 S 0.0 0.0 0:03.18 watchdog/0
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/0
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/1
root@ecs-c6s-xlarge-2-linux-20200723164331:~# ps -mp 13883 -o THREAD,tid,time
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
root 155 - - - - - - 00:01:42
root 0.0 19 - futex_ - - 13883 00:00:00
root 32.8 19 - - - - 13884 00:00:21
root 0.2 19 - futex_ - - 13885 00:00:00
root 0.2 19 - futex_ - - 13886 00:00:00
root 0.2 19 - futex_ - - 13887 00:00:00
root 0.2 19 - futex_ - - 13888 00:00:00
root 0.1 19 - futex_ - - 13889 00:00:00
root 0.0 19 - futex_ - - 13890 00:00:00
root 0.0 19 - futex_ - - 13891 00:00:00
root 0.0 19 - futex_ - - 13892 00:00:00
root 11.4 19 - - - - 13893 00:00:07
root 10.0 19 - futex_ - - 13894 00:00:06
root 2.6 19 - futex_ - - 13895 00:00:01
root 0.0 19 - futex_ - - 13896 00:00:00
root 0.0 19 - futex_ - - 13897 00:00:00
root 91.4 19 - - - - 13898 00:01:00
root 0.0 19 - futex_ - - 14018 00:00:00
root 0.0 19 - futex_ - - 14019 00:00:00
root@ecs-c6s-xlarge-2-linux-20200723164331:~# printf "%x\n" 13898
364a
root@ecs-c6s-xlarge-2-linux-20200723164331:~# jstack 13883 | grep 364a -A 30
"Thread-0" #9 prio=5 os_prio=0 tid=0x00007f13082b4000 nid=0x364a runnable [0x00007f12e820d000]
java.lang.Thread.State: RUNNABLE
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126)
- locked <0x00000000c00162c8> (a java.io.BufferedOutputStream)
at java.io.PrintStream.write(PrintStream.java:480)
- locked <0x00000000c00162f8> (a java.io.PrintStream)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
- locked <0x00000000c00162e0> (a java.io.OutputStreamWriter)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
at java.io.PrintStream.write(PrintStream.java:527)
- eliminated <0x00000000c00162f8> (a java.io.PrintStream)
at java.io.PrintStream.print(PrintStream.java:597)
at java.io.PrintStream.println(PrintStream.java:736)
- locked <0x00000000c00162f8> (a java.io.PrintStream)
at com.hxl.ApplicationManageServer$1.run(ApplicationManageServer.java:21)
"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007f130818f800 nid=0x3648 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007f1308184800 nid=0x3647 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f1308182800 nid=0x3646 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f130817f800 nid=0x3645 waiting on condition [0x0000000000000000]
root@ecs-c6s-xlarge-2-linux-20200723164331:~# ^C
root@ecs-c6s-xlarge-2-linux-20200723164331:~#