|
| 1 | +thread |
| 2 | +====== |
| 3 | + |
| 4 | +Check the basic profile and stack trace of the threads. |
| 5 | + |
| 6 | +### Parameters |
| 7 | + |
| 8 | +|Name|Specification| |
| 9 | +|---:|:---| |
| 10 | +|*id*|thread id in JVM| |
| 11 | +|[n:]|the top n busiest with stack traces| |
| 12 | +|[b]|locate the threads blocking others| |
| 13 | +|[i `<value>`]|specify the interval to collect data to compute CPU ratios (ms)| |
| 14 | + |
| 15 | +How to get the CPU ratios? |
| 16 | + |
| 17 | +> Within an *specified* interval, the time cost by the thread compared to the total CPU time. |
| 18 | +> Take a sample (using `java.lang.management.ThreadMXBean#getThreadCpuTime`) to get the CPU time cost for all the threads and after a *specified* interval (default *100 ms*, which can be specified by `-i`), take another sample and we have the CPU time cost and the ratios naturally. |
| 19 | +
|
| 20 | +> Attention: this kind of operation will take time, to decrease the extra cost, you'd better expand the interval to like `5000 ms` for less performance overhead. |
| 21 | +
|
| 22 | +F.Y.I |
| 23 | + |
| 24 | +If you'd like to check the CPU ratios from the very start of the Java process, [show-busy-java-threads](https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#-show-busy-java-threads) can be a help. |
| 25 | + |
| 26 | +### Usage |
| 27 | + |
| 28 | +#### List the top n busiest with detailed stack trace |
| 29 | + |
| 30 | +```shell |
| 31 | +$ thread -n 3 |
| 32 | +"as-command-execute-daemon" Id=29 cpuUsage=75% RUNNABLE |
| 33 | + at sun.management.ThreadImpl.dumpThreads0(Native Method) |
| 34 | + at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:440) |
| 35 | + at com.taobao.arthas.core.command.monitor200.ThreadCommand$1.action(ThreadCommand.java:58) |
| 36 | + at com.taobao.arthas.core.command.handler.AbstractCommandHandler.execute(AbstractCommandHandler.java:238) |
| 37 | + at com.taobao.arthas.core.command.handler.DefaultCommandHandler.handleCommand(DefaultCommandHandler.java:67) |
| 38 | + at com.taobao.arthas.core.server.ArthasServer$4.run(ArthasServer.java:276) |
| 39 | + at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) |
| 40 | + at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) |
| 41 | + at java.lang.Thread.run(Thread.java:745) |
| 42 | + |
| 43 | + Number of locked synchronizers = 1 |
| 44 | + - java.util.concurrent.ThreadPoolExecutor$Worker@6cd0b6f8 |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +"as-session-expire-daemon" Id=25 cpuUsage=24% TIMED_WAITING |
| 49 | + at java.lang.Thread.sleep(Native Method) |
| 50 | + at com.taobao.arthas.core.server.DefaultSessionManager$2.run(DefaultSessionManager.java:85) |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +"Reference Handler" Id=2 cpuUsage=0% WAITING on java.lang.ref.Reference$Lock@69ba0f27 |
| 55 | + at java.lang.Object.wait(Native Method) |
| 56 | + - waiting on java.lang.ref.Reference$Lock@69ba0f27 |
| 57 | + at java.lang.Object.wait(Object.java:503) |
| 58 | + at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) |
| 59 | +``` |
| 60 | + |
| 61 | +#### List all info when no options provided |
| 62 | + |
| 63 | +```shell |
| 64 | +$ thread |
| 65 | +Threads Total: 16, NEW: 0, RUNNABLE: 7, BLOCKED: 0, WAITING: 5, TIMED_WAITING: 4, TERMINATED: 0 |
| 66 | +ID NAME GROUP PRIORITY STATE %CPU TIME INTERRUPTE DAEMON |
| 67 | +30 as-command-execute-daemon system 9 RUNNABLE 72 0:0 false true |
| 68 | +23 as-session-expire-daemon system 9 TIMED_WAIT 27 0:0 false true |
| 69 | +22 Attach Listener system 9 RUNNABLE 0 0:0 false true |
| 70 | +11 pool-2-thread-1 main 5 TIMED_WAIT 0 0:0 false false |
| 71 | +12 Thread-2 main 5 RUNNABLE 0 0:0 false true |
| 72 | +13 pool-3-thread-1 main 5 TIMED_WAIT 0 0:0 false false |
| 73 | +25 as-selector-daemon system 9 RUNNABLE 0 0:0 false true |
| 74 | +14 Thread-3 main 5 TIMED_WAIT 0 0:0 false false |
| 75 | +26 pool-5-thread-1 system 5 WAITING 0 0:0 false false |
| 76 | +15 Thread-4 main 5 RUNNABLE 0 0:0 false false |
| 77 | +1 main main 5 WAITING 0 0:2 false false |
| 78 | +2 Reference Handler system 10 WAITING 0 0:0 false true |
| 79 | +3 Finalizer system 8 WAITING 0 0:0 false true |
| 80 | +4 Signal Dispatcher system 9 RUNNABLE 0 0:0 false true |
| 81 | +20 NonBlockingInputStreamThread main 5 WAITING 0 0:0 false true |
| 82 | +21 Thread-8 main 5 RUNNABLE 0 0:0 false true |
| 83 | +``` |
| 84 | + |
| 85 | +#### thread <thread_id> present the specified thread profile |
| 86 | + |
| 87 | +```shell |
| 88 | +$ thread 1 |
| 89 | +"main" Id=1 WAITING on java.util.concurrent.CountDownLatch$Sync@29fafb28 |
| 90 | + at sun.misc.Unsafe.park(Native Method) |
| 91 | + - waiting on java.util.concurrent.CountDownLatch$Sync@29fafb28 |
| 92 | + at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) |
| 93 | + at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836) |
| 94 | + at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997) |
| 95 | + at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304) |
| 96 | + at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231) |
| 97 | +``` |
| 98 | + |
| 99 | +#### thread -b locate the blocking threads |
| 100 | + |
| 101 | +Using `-b`, we can effectively locate the threads holding locks blocking other threads resulting in a frozen system. |
| 102 | + |
| 103 | +```sh |
| 104 | +$ thread -b |
| 105 | +"http-bio-8080-exec-4" Id=27 TIMED_WAITING |
| 106 | + at java.lang.Thread.sleep(Native Method) |
| 107 | + at test.arthas.TestThreadBlocking.doGet(TestThreadBlocking.java:22) |
| 108 | + - locked java.lang.Object@725be470 <---- but blocks 4 other threads! |
| 109 | + at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) |
| 110 | + at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) |
| 111 | + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) |
| 112 | + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) |
| 113 | + at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) |
| 114 | + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) |
| 115 | + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) |
| 116 | + at test.filter.TestDurexFilter.doFilter(TestDurexFilter.java:46) |
| 117 | + at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) |
| 118 | + at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) |
| 119 | + at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) |
| 120 | + at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) |
| 121 | + at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) |
| 122 | + at com.taobao.tomcat.valves.ContextLoadFilterValve$FilterChainAdapter.doFilter(ContextLoadFilterValve.java:191) |
| 123 | + at com.taobao.eagleeye.EagleEyeFilter.doFilter(EagleEyeFilter.java:81) |
| 124 | + at com.taobao.tomcat.valves.ContextLoadFilterValve.invoke(ContextLoadFilterValve.java:150) |
| 125 | + at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) |
| 126 | + at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) |
| 127 | + at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) |
| 128 | + at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:429) |
| 129 | + at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) |
| 130 | + at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) |
| 131 | + at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) |
| 132 | + - locked org.apache.tomcat.util.net.SocketWrapper@7127ee12 |
| 133 | + at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) |
| 134 | + at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) |
| 135 | + at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) |
| 136 | + at java.lang.Thread.run(Thread.java:745) |
| 137 | + |
| 138 | + Number of locked synchronizers = 1 |
| 139 | + - java.util.concurrent.ThreadPoolExecutor$Worker@31a6493e |
| 140 | +``` |
| 141 | + |
| 142 | +> Attention: only `synchronized` blocked threads can be located for now, `JUL` not supported yet. |
| 143 | +
|
| 144 | + |
| 145 | +#### thread -i specify the collecting interval |
| 146 | + |
| 147 | +```sh |
| 148 | +$ thread -n 3 -i 1000 |
| 149 | +"as-command-execute-daemon" Id=4759 cpuUsage=23% RUNNABLE |
| 150 | + at sun.management.ThreadImpl.dumpThreads0(Native Method) |
| 151 | + at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:440) |
| 152 | + at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:133) |
| 153 | + at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:79) |
| 154 | + at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:96) |
| 155 | + at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:27) |
| 156 | + at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:125) |
| 157 | + at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:122) |
| 158 | + at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:332) |
| 159 | + at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) |
| 160 | + at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) |
| 161 | + at java.lang.Thread.run(Thread.java:756) |
| 162 | + |
| 163 | + Number of locked synchronizers = 1 |
| 164 | + - java.util.concurrent.ThreadPoolExecutor$Worker@546aeec1 |
| 165 | +... |
| 166 | +``` |
0 commit comments