1 参数说明

参数名称 参数说明
id 线程id
[n:] 指定最忙的前N个线程并打印堆栈
[b] 找出当前阻塞其他线程的线程
[i ] 指定cpu使用率统计的采样间隔,单位为毫秒,默认值为200
[—all] 显示所有匹配的线程

这里的cpu使用率与linux 命令top-H-p的线程%CPU类似,一段采样间隔时间内,当前JVM里各个线程的增量cpu时间与采样间隔时间的比例。

工作原理说明:

首先第一次采样,获取所有线程的CPU时间(调用的是 java.lang.management.ThreadMXBean#getThreadCpuTime()及sun.management.HotspotThreadMBean.getInternalThreadCpuTimes()接口)

  • 然后睡眠等待一个间隔时间(默认为200ms,可以通过-i 指定间隔时间)
  • 再次第二次采样,获取所有线程的CPU时间,对比两次采样数据,计算出每个线程的增量CPU时间
  • 线程CPU使用率 = 线程增量CPU时间 / 采样间隔时间 * 100%

注意: 这个统计也会产生一定的开销(JDK这个接口本身开销比较大),因此会看到as的线程占用一定的百分比,为了降低统计自身的开销带来的影响,可以把采样间隔拉长一些,比如5000毫秒。

2 命令演示

2.1 thread -n 3 : 展示当前最忙的前N个线程并打印堆栈

  1. "sentinel-time-tick-thread" Id=70 cpuUsage=2.84% deltaTime=5ms time=61388ms TIMED_WAITING
  2. at java.lang.Thread.sleep(Native Method)
  3. at java.lang.Thread.sleep(Thread.java:340)
  4. at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
  5. at com.alibaba.csp.sentinel.util.TimeUtil$1.run(TimeUtil.java:37)
  6. at java.lang.Thread.run(Thread.java:748)
  7. "C1 CompilerThread1" [Internal] cpuUsage=1.75% deltaTime=3ms time=11201ms
  8. "arthas-command-execute" Id=88 cpuUsage=0.78% deltaTime=1ms time=115ms RUNNABLE
  9. at sun.management.ThreadImpl.dumpThreads0(Native Method)
  10. at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:448)
  11. at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:199)
  12. at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:122)
  13. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82)
  14. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18)
  15. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111)
  16. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108)
  17. at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:385)
  18. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  19. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  20. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
  21. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
  22. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  23. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  24. at java.lang.Thread.run(Thread.java:748)
  • 没有线程ID,包含[Internal]表示为JVM内部线程。
  • cpuUsage为采样间隔时间内线程的CPU使用率。
  • deltaTime为采样间隔时间内线程的增量CPU时间,小于1ms时被取整显示为0ms。
  • time线程运行总CPU时间。

    2.2 thread : 当没有参数时,显示第一页线程的信息

    ```shell Threads Total: 76, NEW: 0, RUNNABLE: 16, BLOCKED: 0, WAITING: 22, TIMED_WAITING: 31, TERMINATED: 0, Internal threads: 7
    ID NAME GROUP PRIORITY STATE %CPU DELTA_TIME TIME INTERRUPTED DAEMON
    70 sentinel-time-tick-thread main 5 TIMED_WAITI 3.38 0.006 1:7.807 false true
    -1 C1 CompilerThread1 - -1 - 2.28 0.004 0:11.618 false true
    69 sentinel-system-metric-record-task-th main 5 TIMED_WAITI 0.61 0.001 0:13.486 false true
    88 arthas-command-execute system 5 RUNNABLE 0.52 0.001 0:0.124 false true
    80 SimplePauseDetectorThread_0 main 5 TIMED_WAITI 0.44 0.000 0:5.943 false true
    81 SimplePauseDetectorThread_1 main 5 TIMED_WAITI 0.43 0.000 0:5.915 false true
    82 SimplePauseDetectorThread_2 main 5 TIMED_WAITI 0.34 0.000 0:6.190 false true
    75 sentinel-cluster-token-batch-request- main 5 TIMED_WAITI 0.13 0.000 0:2.662 false true
    -1 VM Periodic Task Thread - -1 - 0.08 0.000 0:1.851 false true
    -1 C2 CompilerThread0 - -1 - 0.01 0.000 0:36.102 false true
    2 Reference Handler system 10 WAITING 0.0 0.000 0:0.027 false true
    3 Finalizer system 8 WAITING 0.0 0.000 0:0.028 false true
    4 Signal Dispatcher system 9 RUNNABLE 0.0 0.000 0:0.000 false true
    54 Attach Listener system 9 RUNNABLE 0.0 0.000 0:0.018 false true
    56 arthas-timer system 9 WAITING 0.0 0.000 0:0.000 false true
    59 arthas-NettyHttpTelnetBootstrap-3-1 system 5 RUNNABLE 0.0 0.000 0:0.061 false true
    60 arthas-NettyWebsocketTtyBootstrap-4-1 system 5 RUNNABLE 0.0 0.000 0:0.003 false true
    61 arthas-NettyWebsocketTtyBootstrap-4-2 system 5 RUNNABLE 0.0 0.000 0:0.004 false true
    62 arthas-shell-server system 9 TIMED_WAITI 0.0 0.000 0:0.007 false true
    63 arthas-session-manager system 9 TIMED_WAITI 0.0 0.000 0:0.004 false true
    64 arthas-UserStat system 9 WAITING 0.0 0.000 0:0.000 false true
<a name="dKYAW"></a>
#### 2.3 thread –all: 显示所有匹配的线程
<a name="f16FY"></a>
#### 2.4 thread id: 显示指定线程的运行堆栈
<a name="n4sfs"></a>
#### 2.5 thread -b: 找出当前阻塞其他线程的线程
注意, 目前只支持找出synchronized关键字阻塞住的线程, 如果是java.util.concurrent.Lock, 目前还不支持。
<a name="Y1BF8"></a>
#### 2.6 thread -i:指定采样时间间隔

- thread -i 1000: 统计最近1000ms内的线程CPU时间。
- thread -n 3 -i 1000: 列出1000ms内最忙的3个线程栈
<a name="jDq1X"></a>
#### 2.7 thread --state ,查看指定状态的线程

```shell
#thread --state WAITING
Threads Total: 67, NEW: 0, RUNNABLE: 16, BLOCKED: 0, WAITING: 22, TIMED_WAITING: 29, TERMINATED: 0                                                     
ID    NAME                                  GROUP              PRIORITY     STATE       %CPU         DELTA_TIME  TIME         INTERRUPTED DAEMON       
2     Reference Handler                     system             10           WAITING     0.0          0.000       0:0.027      false       true         
3     Finalizer                             system             8            WAITING     0.0          0.000       0:0.028      false       true         
56    arthas-timer                          system             9            WAITING     0.0          0.000       0:0.000      false       true         
64    arthas-UserStat                       system             9            WAITING     0.0          0.000       0:0.000      false       true         
19    AsyncAppender-Worker-info_fileAsync   main               5            WAITING     0.0          0.000       0:0.010      false       true         
20    AsyncAppender-Worker-warn_fileAsync   main               5            WAITING     0.0          0.000       0:0.001      false       true         
21    AsyncAppender-Worker-error_fileAsync  main               5            WAITING     0.0          0.000       0:0.001      false       true         
42    logback-4                             main               5            WAITING     0.0          0.000       0:0.002      false       true         
44    sentinel-logger-print-scheduler-threa main               5            WAITING     0.0          0.000       0:0.170      false       true         
52    logback-5                             main               5            WAITING     0.0          0.000       0:0.001      false       true         
67    logback-6                             main               5            WAITING     0.0          0.000       0:0.001      false       true         
78    lettuce-eventExecutorLoop-1-2         main               5            WAITING     0.0          0.000       0:0.019      false       true         
79    Thread-24                             main               5            WAITING     0.0          0.000       0:0.006      false       true         
85    sentinel-stat-status-code-record-task main               5            WAITING     0.0          0.000       0:0.025      false       true         
86    sentinel-metric-log-writer-task-threa main               5            WAITING     0.0          0.000       0:0.103      false       true         
87    sentinel-heartbeat-send-task-thread-2 main               5            WAITING     0.0          0.000       0:0.020      false       true         
89    logback-7                             main               5            WAITING     0.0          0.000       0:0.000      false       true         
91    logback-8                             main               5            WAITING     0.0          0.000       0:0.000      false       true         
92    lettuce-eventExecutorLoop-1-3         main               5            WAITING     0.0          0.000       0:0.019      false       true         
93    sentinel-stat-status-code-write-task- main               5            WAITING     0.0          0.000       0:0.001      false       true         
94    logback-9                             main               5            WAITING     0.0          0.000       0:0.000      false       true