Skip to content

top threads

M. Isuru Tharanga Chrishantha Perera edited this page Feb 23, 2018 · 3 revisions

top-threads

Show top CPU consuming threads.

Usage

./top-threads -h

Usage: 
top-threads -p <pid> [-n <number_of_threads>] [-x] [-o <output_directory>] [-t] [-l <number_of_lines>] [-h]

-p: The Java Process ID
-n: Number of top threads to display. Default: 10.
-x: Do not save thread dump and PS output
-o: Output directory to save results. Works with -s option. A temp directory is used by default.
-t: Print stack traces.
-l: Number of lines to print in stack traces. Works with -t option. Default: 3.
-h: Display this help.

Example: Monitoring highcpu sample program

./top-threads -p `pgrep -f highcpu`
Name                                 %CPU  %MEM Thread Status   State  Nice        CPU Time    LWP    NID
"Thread 0: Math"                     53.6   9.0 RUNNABLE        R         0        00:00:32  30164 0x75d4
"Thread 1: Math"                     53.2   9.0 RUNNABLE        R         0        00:00:32  30165 0x75d5
"Thread 3: Hashing"                  38.3   9.0 RUNNABLE        R         0        00:00:23  30157 0x75cd
"Thread 2: Hashing"                  37.9   9.0 RUNNABLE        S         0        00:00:23  30156 0x75cc
"Thread 5: Hashing"                  37.7   9.0 BLOCKED         S         0        00:00:23  30159 0x75cf
"Thread 1: Hashing"                  37.7   9.0 RUNNABLE        S         0        00:00:23  30155 0x75cb
"Thread 0: Hashing"                  37.7   9.0 BLOCKED         S         0        00:00:23  30154 0x75ca
"Thread 4: Hashing"                  37.2   9.0 RUNNABLE        S         0        00:00:22  30158 0x75ce
"VM Thread"                           2.5   9.0                 S         0        00:00:01  30144 0x75c0
"C2 CompilerThread1"                  0.9   9.0 RUNNABLE        S         0        00:00:00  30149 0x75c5
PS output saved:   /tmp/top-threads.3oT2Mb/1519363418363-ps.txt      
Thread dump saved: /tmp/top-threads.3oT2Mb/1519363418363-thread-dump.txt
Top threads saved: /tmp/top-threads.3oT2Mb/1519363418363-top-threads.txt

Example: Show thread stack trace

./top-threads -p `pgrep -f highcpu` -t
Name                                 %CPU  %MEM Thread Status   State  Nice        CPU Time    LWP    NID
"Thread 0: Math"                     51.1   9.1 RUNNABLE        R         0        00:00:49  30164 0x75d4
"Thread 0: Math" #20 prio=5 os_prio=0 tid=0x00007f091826e000 nid=0x75d4 runnable [0x00007f08f9c00000]
   java.lang.Thread.State: RUNNABLE
	at java.lang.StrictMath.atan(Native Method)
	at java.lang.Math.atan(Math.java:222)
	at com.github.chrishantha.sample.highcpu.MathWorker.run(MathWorker.java:32)

"Thread 1: Math"                     50.6   9.1 RUNNABLE        R         0        00:00:48  30165 0x75d5
"Thread 1: Math" #21 prio=5 os_prio=0 tid=0x00007f0918270000 nid=0x75d5 runnable [0x00007f08f9aff000]
   java.lang.Thread.State: RUNNABLE
	at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.access$100(FloatingDecimal.java:259)
	at sun.misc.FloatingDecimal.getBinaryToASCIIConverter(FloatingDecimal.java:1785)
	at sun.misc.FloatingDecimal.getBinaryToASCIIConverter(FloatingDecimal.java:1738)

"Thread 5: Hashing"                  37.2   9.1 RUNNABLE        S         0        00:00:35  30159 0x75cf
"Thread 5: Hashing" #15 prio=5 os_prio=0 tid=0x00007f0918265000 nid=0x75cf runnable [0x00007f08fa105000]
   java.lang.Thread.State: RUNNABLE
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:207)
	at java.lang.StringBuilder.toString(StringBuilder.java:407)

"Thread 3: Hashing"                  37.2   9.1 RUNNABLE        S         0        00:00:35  30157 0x75cd
"Thread 3: Hashing" #13 prio=5 os_prio=0 tid=0x00007f0918262000 nid=0x75cd runnable [0x00007f08fa307000]
   java.lang.Thread.State: RUNNABLE
	at sun.security.provider.NativePRNG$RandomIO.implNextBytes(NativePRNG.java:534)
	at sun.security.provider.NativePRNG$RandomIO.access$400(NativePRNG.java:331)
	at sun.security.provider.NativePRNG.engineNextBytes(NativePRNG.java:220)

"Thread 4: Hashing"                  37.1   9.1 RUNNABLE        R         0        00:00:35  30158 0x75ce
"Thread 4: Hashing" #14 prio=5 os_prio=0 tid=0x00007f0918263800 nid=0x75ce runnable [0x00007f08fa206000]
   java.lang.Thread.State: RUNNABLE
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:207)
	at java.lang.StringBuilder.toString(StringBuilder.java:407)

"Thread 1: Hashing"                  37.0   9.1 RUNNABLE        R         0        00:00:35  30155 0x75cb
"Thread 1: Hashing" #11 prio=5 os_prio=0 tid=0x00007f091825e800 nid=0x75cb runnable [0x00007f08fa509000]
   java.lang.Thread.State: RUNNABLE
	at java.security.MessageDigest$Delegate.engineUpdate(MessageDigest.java:584)
	at java.security.MessageDigest.update(MessageDigest.java:335)
	at sun.security.provider.SecureRandom.engineNextBytes(SecureRandom.java:243)

"Thread 2: Hashing"                  36.6   9.1 RUNNABLE        R         0        00:00:35  30156 0x75cc
"Thread 2: Hashing" #12 prio=5 os_prio=0 tid=0x00007f0918260000 nid=0x75cc runnable [0x00007f08fa408000]
   java.lang.Thread.State: RUNNABLE
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:207)
	at java.lang.StringBuilder.toString(StringBuilder.java:407)

"Thread 0: Hashing"                  36.4   9.1 RUNNABLE        S         0        00:00:34  30154 0x75ca
"Thread 0: Hashing" #10 prio=5 os_prio=0 tid=0x00007f091825a800 nid=0x75ca runnable [0x00007f08fa60a000]
   java.lang.Thread.State: RUNNABLE
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:207)
	at java.lang.StringBuilder.toString(StringBuilder.java:407)

"VM Thread"                           2.5   9.1                 S         0        00:00:02  30144 0x75c0
"VM Thread" os_prio=0 tid=0x00007f0918140000 nid=0x75c0 runnable 

"GC task thread#3 (ParallelGC)"       0.8   9.1                 S         0        00:00:00  30143 0x75bf
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f0918026000 nid=0x75bf runnable 
PS output saved:   /tmp/top-threads.vK8gFa/1519363452730-ps.txt      
Thread dump saved: /tmp/top-threads.vK8gFa/1519363452730-thread-dump.txt
Top threads saved: /tmp/top-threads.vK8gFa/1519363452730-top-threads.txt

Example: Continuous monitoring using watch command

watch -n 1 ./top-threads -p `pgrep -f highcpu` -o /tmp/highcpu-threads

It's highly recommended to use -o option with a directory when using watch command.

The output directory can be later used with analyze-jstack-samples command.