|`[n:]`|the top n busiest threads with stack traces printed|
|`[b]`|locate the thread blocking the others|
|[i `<value>`]|specify the interval to collect data to compute CPU ratios (ms)|
|[--all]|Show all matching threads|
> How the CPU ratios are calculated? <br/><br/>
> CPU ratio for a given thread is the CPU time it takes divided by the total CPU time within a specified interval period. It is calculated in the following way: sample CPU times for all the thread by calling `java.lang.management.ThreadMXBean#getThreadCpuTime` first, then sleep for a period (the default value is 100ms, which can be specified by `-i`), then sample CPU times again. By this, we can get the time cost for this period for each thread, then come up with the ratio. <br/><br/>
> Note: this operation consumes CPU time too (`getThreadCpuTime` is time-consuming), therefore it is possible to observe Arthas's thread appears in the list. To avoid this, try to increase sample interval, for example: 5000 ms.<br/><br/>
> If you'd like to check the CPU ratios from the very beginning of the Java process, [show-busy-java-threads](https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#-show-busy-java-threads) can come to help.
### How the CPU ratios are calculated?
The cpu ratios here is similar to the thread `%CPU` of the linux command `top -H -p <pid>`. During a sampling interval,
the ratio of the incremental cpu time of each thread in the current JVM to the sampling interval time.
> Working principle description:
* Do the first sampling, get the CPU time of all threads ( by calling `java.lang.management.ThreadMXBean#getThreadCpuTime()` and
* Sleep and wait for an interval (the default is 200ms, the interval can be specified by `-i`)
* Do the second sampling, get the CPU time of all threads, compare the two sampling data, and calculate the incremental CPU time of each thread
* `Thread CPU usage ratio` = `Thread increment CPU time` / `Sampling interval time` * 100%
> Note: this operation consumes CPU time too (`getThreadCpuTime` is time-consuming), therefore it is possible to observe Arthas's thread appears in the list. To avoid this, try to increase sample interval, for example: 5000 ms.<br/>
> Another way to view the thread cpu usage of the Java process, [show-busy-java-threads](https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#-show-busy-java-threads) can come to help.
<li><p>PRIORITY: thread priority, ranged from 1 to 10. The greater number, the higher priority</p></li>
<li><p>STATE: thread state</p></li>
<li><p>CPU%: the ratio of CPU usage for the thread, sampled every 100ms</p></li>
<li><p>TIME: total running time in <codeclass="docutils literal notranslate"><spanclass="pre">minute:second</span></code> format</p></li>
<li><p>CPU%: the ratio of CPU usage for the thread. For example, the sampling interval is 1000ms, and the incremental cpu time
of a thread is 100ms, then the cpu usage rate=100/1000=10%</p></li>
<li><p>DELTA_TIME: incremental CPU time of thread running after the last sampling in <codeclass="docutils literal notranslate"><spanclass="pre">second</span></code> format</p></li>
<li><p>TIME: total CPU time of the thread in <codeclass="docutils literal notranslate"><spanclass="pre">minute:second</span></code> format</p></li>
<li><p>INTERRUPTED: the thread interruption state</p></li>
<li><p>DAEMON: daemon thread or not</p></li>
</ul>
<divclass="section"id="jvm-internal-threads">
<h3>JVM internal threads<aclass="headerlink"href="#jvm-internal-threads"title="Permalink to this headline">¶</a></h3>
<p>After Java 8, it is supported to obtain the CPU time of JVM internal threads. These threads only have the name and CPU time,
without ID and status information (display ID is -1).</p>
<p>JVM activities can be observed through internal threads, such as GC, JIT compilation, etc., to perceive the overall status of JVM.</p>
<ulclass="simple">
<li><p>When the JVM heap/metaspace space is insufficient or OOM, it can be seen that the CPU usage of the GC threads is
significantly higher than other threads.</p></li>
<li><p>After executing commands such as <codeclass="docutils literal notranslate"><spanclass="pre">trace/watch/tt/redefine</span></code>, you can see that JIT threads activities become more frequent.
Because the JIT compilation data related to this class is cleared when the JVM hot update the class bytecode, it needs to be recompiled.</p></li>
</ul>
<p>JVM internal threads include the following:</p>
<ulclass="simple">
<li><p>JIT compilation thread: such as <codeclass="docutils literal notranslate"><spanclass="pre">C1</span><spanclass="pre">CompilerThread0</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">C2</span><spanclass="pre">CompilerThread0</span></code></p></li>
<li><p>GC thread: such as <codeclass="docutils literal notranslate"><spanclass="pre">GC</span><spanclass="pre">Thread0</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">G1</span><spanclass="pre">Young</span><spanclass="pre">RemSet</span><spanclass="pre">Sampling</span></code></p></li>
<liclass="toctree-l4"><aclass="reference internal"href="#list-the-top-n-busiest-threads-with-detailed-stack-trace">List the top n busiest threads with detailed stack trace</a></li>
<liclass="toctree-l4"><aclass="reference internal"href="#list-all-threads-info-when-no-options-provided">List all threads’ info when no options provided</a></li>
<liclass="toctree-l4"><aclass="reference internal"href="#list-first-page-threads-info-when-no-options-provided">List first page threads’ info when no options provided</a></li>
<liclass="toctree-l4"><aclass="reference internal"href="#thread-all-show-all-matching-threads">thread –all, show all matching threads</a></li>
<liclass="toctree-l4"><aclass="reference internal"href="#thread-id-show-the-running-stack-for-the-target-thread">thread id, show the running stack for the target thread</a></li>
<liclass="toctree-l4"><aclass="reference internal"href="#thread-b-locate-the-thread-bocking-the-others">thread -b, locate the thread bocking the others</a></li>
<liclass="toctree-l4"><aclass="reference internal"href="#thread-i-specify-the-sampling-interval">thread -i, specify the sampling interval</a></li>
<tdalign="left">specify the interval to collect data to compute CPU ratios (ms)</td>
</tr>
<tr>
<tdalign="right">[--all]</td>
<tdalign="left">Show all matching threads</td>
</tr>
</tbody>
</table><blockquote>
<div><p>How the CPU ratios are calculated? <br/><br/>
CPU ratio for a given thread is the CPU time it takes divided by the total CPU time within a specified interval period. It is calculated in the following way: sample CPU times for all the thread by calling <codeclass="docutils literal notranslate"><spanclass="pre">java.lang.management.ThreadMXBean#getThreadCpuTime</span></code> first, then sleep for a period (the default value is 100ms, which can be specified by <codeclass="docutils literal notranslate"><spanclass="pre">-i</span></code>), then sample CPU times again. By this, we can get the time cost for this period for each thread, then come up with the ratio. <br/><br/>
Note: this operation consumes CPU time too (<codeclass="docutils literal notranslate"><spanclass="pre">getThreadCpuTime</span></code> is time-consuming), therefore it is possible to observe Arthas’s thread appears in the list. To avoid this, try to increase sample interval, for example: 5000 ms.<br/><br/>
If you’d like to check the CPU ratios from the very beginning of the Java process, <aclass="reference external"href="https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#-show-busy-java-threads">show-busy-java-threads</a> can come to help.</p>
<h2>How the CPU ratios are calculated?<aclass="headerlink"href="#how-the-cpu-ratios-are-calculated"title="Permalink to this headline">¶</a></h2>
<p>The cpu ratios here is similar to the thread <codeclass="docutils literal notranslate"><spanclass="pre">%CPU</span></code> of the linux command <codeclass="docutils literal notranslate"><spanclass="pre">top</span><spanclass="pre">-H</span><spanclass="pre">-p</span><spanclass="pre"><pid></span></code>. During a sampling interval,
the ratio of the incremental cpu time of each thread in the current JVM to the sampling interval time.</p>
<blockquote>
<div><p>Working principle description:</p>
</div></blockquote>
<ulclass="simple">
<li><p>Do the first sampling, get the CPU time of all threads ( by calling <codeclass="docutils literal notranslate"><spanclass="pre">java.lang.management.ThreadMXBean#getThreadCpuTime()</span></code> and
<li><p>Sleep and wait for an interval (the default is 200ms, the interval can be specified by <codeclass="docutils literal notranslate"><spanclass="pre">-i</span></code>)</p></li>
<li><p>Do the second sampling, get the CPU time of all threads, compare the two sampling data, and calculate the incremental CPU time of each thread</p></li>
<div><p>Note: this operation consumes CPU time too (<codeclass="docutils literal notranslate"><spanclass="pre">getThreadCpuTime</span></code> is time-consuming), therefore it is possible to observe Arthas’s thread appears in the list. To avoid this, try to increase sample interval, for example: 5000 ms.<br/></p>
</div></blockquote>
<blockquote>
<div><p>Another way to view the thread cpu usage of the Java process, <aclass="reference external"href="https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#-show-busy-java-threads">show-busy-java-threads</a> can come to help.</p>
</div></blockquote>
</div>
<divclass="section"id="usage">
@ -275,54 +296,66 @@ If you’d like to check the CPU ratios from the very beginning of the Java proc
<h3>List the top n busiest threads with detailed stack trace<aclass="headerlink"href="#list-the-top-n-busiest-threads-with-detailed-stack-trace"title="Permalink to this headline">¶</a></h3>
<li><p>Without thread ID, including <codeclass="docutils literal notranslate"><spanclass="pre">[Internal]</span></code> means JVM internal thread, refer to the introduction of <aclass="reference internal"href="dashboard.html"><spanclass="doc">dashboard</span></a> command.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">cpuUsage</span></code> is the CPU usage of the thread during the sampling interval, consistent with the data of the <aclass="reference internal"href="dashboard.html"><spanclass="doc">dashboard</span></a> command.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">deltaTime</span></code> is the incremental CPU time of the thread during the sampling interval. If it is less than 1ms, it will be rounded and displayed as 0ms.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">time</span></code> The total CPU time of thread.</p></li>
</ul>
<p><strong>Note:</strong> The thread stack is acquired at the end of the second sampling, which does not indicate that the thread is
processing the same task during the sampling interval. It is recommended that the interval time should not be too long.
The larger the interval time, the more inaccurate.</p>
<p>You can try to specify different intervals according to the specific situation and observe the output results.</p>
<h3>List all threads’ info when no options provided<aclass="headerlink"href="#list-all-threads-info-when-no-options-provided"title="Permalink to this headline">¶</a></h3>
<h3>List first page threads’ info when no options provided<aclass="headerlink"href="#list-first-page-threads-info-when-no-options-provided"title="Permalink to this headline">¶</a></h3>
<p>By default, they are arranged in descending order of CPU increment time, and only the first page of data is displayed.</p>
<h3>thread –all, show all matching threads<aclass="headerlink"href="#thread-all-show-all-matching-threads"title="Permalink to this headline">¶</a></h3>
<p>Display all matching threads. Sometimes it is necessary to obtain all the thread data of the JVM for analysis.</p>
<h3>thread id, show the running stack for the target thread<aclass="headerlink"href="#thread-id-show-the-running-stack-for-the-target-thread"title="Permalink to this headline">¶</a></h3>
<h3>thread -i, specify the sampling interval<aclass="headerlink"href="#thread-i-specify-the-sampling-interval"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">thread</span><spanclass="pre">-i</span><spanclass="pre">1000</span></code>: Count the thread cpu time of the last 1000ms.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">thread</span><spanclass="pre">-n</span><spanclass="pre">3</span><spanclass="pre">-i</span><spanclass="pre">1000</span></code>: List the 3 busiest thread stacks in 1000ms</p></li>
<h3>thread –state , view the special state theads<aclass="headerlink"href="#thread-state-view-the-special-state-theads"title="Permalink to this headline">¶</a></h3>