Check CPU Overload

Practice

Code

Problem Code:

1
2
3
4
5
println("PID: ${ManagementFactory.getRuntimeMXBean().name}")
val random = Random(System.currentTimeMillis())
while (true) {
random.nextInt() * random.nextInt()
}

Top CPU

  • Mac
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    top -o cpu
    ``

    - Linux
    ``` bash
    top -c
    #显示进程运行信息列表。按下P,进程按照cpu使用率排序

    top -Hp pid
    #显示一个进程的线程运行信息列表。按下P,进程按照cpu使用率排序

    cat pid.stack | grep 'ThreadId' -C 8

Dump Process Stack

1
jstack -l PID > pid.stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):

"Attach Listener" #11 daemon prio=9 os_prio=31 tid=0x00007ff4cf882800 nid=0x5903 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"Service Thread" #10 daemon prio=9 os_prio=31 tid=0x00007ff4d2838000 nid=0x5703 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"C1 CompilerThread3" #9 daemon prio=9 os_prio=31 tid=0x00007ff4d2047800 nid=0x5503 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"C2 CompilerThread2" #8 daemon prio=9 os_prio=31 tid=0x00007ff4d2047000 nid=0x4003 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"C2 CompilerThread1" #7 daemon prio=9 os_prio=31 tid=0x00007ff4d2046000 nid=0x3e03 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=31 tid=0x00007ff4cf842800 nid=0x3c03 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"Monitor Ctrl-Break" #5 daemon prio=5 os_prio=31 tid=0x00007ff4cf839000 nid=0x3a03 runnable [0x0000700005020000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x000000071584d860> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x000000071584d860> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)

Locked ownable synchronizers:
- None

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007ff4d2816800 nid=0x4303 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007ff4cf818000 nid=0x4b03 in Object.wait() [0x0000700004e1a000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000715588ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x0000000715588ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

Locked ownable synchronizers:
- None

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007ff4d0807000 nid=0x3503 in Object.wait() [0x0000700004d17000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000715586bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000715586bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

Locked ownable synchronizers:
- None

"main" #1 prio=5 os_prio=31 tid=0x00007ff4d0006800 nid=0x2703 runnable [0x00007000040f3000]
java.lang.Thread.State: RUNNABLE
at com.demo.death.CPUOverloadKt.main(CPUOverload.kt:9)
at com.demo.death.CPUOverloadKt.main(CPUOverload.kt)

Locked ownable synchronizers:
- None

"VM Thread" os_prio=31 tid=0x00007ff4cf007000 nid=0x4c03 runnable

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007ff4cf006000 nid=0x1f07 runnable

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007ff4d000f800 nid=0x2a03 runnable

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007ff4cf803000 nid=0x2c03 runnable

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007ff4cf804000 nid=0x5303 runnable

"GC task thread#4 (ParallelGC)" os_prio=31 tid=0x00007ff4cf804800 nid=0x2d03 runnable

"GC task thread#5 (ParallelGC)" os_prio=31 tid=0x00007ff4cf805000 nid=0x5003 runnable

"GC task thread#6 (ParallelGC)" os_prio=31 tid=0x00007ff4cf805800 nid=0x4f03 runnable

"GC task thread#7 (ParallelGC)" os_prio=31 tid=0x00007ff4d0802800 nid=0x4e03 runnable

"GC task thread#8 (ParallelGC)" os_prio=31 tid=0x00007ff4cf806800 nid=0x3103 runnable

"GC task thread#9 (ParallelGC)" os_prio=31 tid=0x00007ff4cf807000 nid=0x4d03 runnable

"VM Periodic Task Thread" os_prio=31 tid=0x00007ff4cf845800 nid=0xa803 waiting on condition

JNI global references: 15

抽出关键部分

1
2
3
4
5
6
7
"main" #1 prio=5 os_prio=31 tid=0x00007ff4d0006800 nid=0x2703 runnable [0x00007000040f3000]
java.lang.Thread.State: RUNNABLE
at com.demo.death.CPUOverloadKt.main(CPUOverload.kt:9)
at com.demo.death.CPUOverloadKt.main(CPUOverload.kt)

Locked ownable synchronizers:
- None

其中 nid=0x2703 是线程ID号,换算成十进制是9987


Reference

https://wsgzao.github.io/post/htop/