Skip to content

Commit

Permalink
! show-busy-java-threads: fix top extract error by process name
Browse files Browse the repository at this point in the history
COMMAND column of top -H output can be thread name not process name

top output sample and env info:

```
$ uname -a
Linux 33e449b39f66 4.19.121-linuxkit #1 SMP Thu Jan 21 15:36:34 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$ java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04, mixed mode, sharing)
$ top -v
  procps-ng 3.3.12
Usage:
  top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
$ top -H
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 7012 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 java
 7014 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.62 GC Thread#0
 7015 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 G1 Main Marker
 7017 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 G1 Refine#0
 7018 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.06 G1 Young RemSet
 7019 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.67 VM Thread
 7020 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 Reference Handl
 7021 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 Finalizer
 7022 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 Signal Dispatch
 7023 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.34 C2 CompilerThre
 7024 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.03 C1 CompilerThre
 7025 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 Sweeper thread
 7026 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.00 Service Thread
 7027 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.26 VM Periodic Tas
 7032 jerry     20   0 3492604  79492  27096 S  0.0  3.9   0:00.52 GC Thread#1
 ...
```
  • Loading branch information
oldratlee committed Mar 31, 2021
1 parent d8df80f commit 1efb992
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions bin/show-busy-java-threads
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ findBusyJavaThreadsByPs() {

# top with output field: thread id, %cpu
__top_threadId_cpu() {
local java_pid_list
# DO NOT combine var java_pid_list declaration and assignment, because its value is supplied by subshell.
# shellcheck disable=SC2086
java_pid_list="$(ps $ps_process_select_options -o pid --no-headers)"
# shellcheck disable=SC2086
java_pid_list="$(echo $java_pid_list | tr ' ' ,)" # join with ,

# 1. sort by %cpu by top option `-o %CPU`
# unfortunately, top version 3.2 does not support -o option(supports from top version 3.3+),
# use
Expand All @@ -357,7 +364,7 @@ __top_threadId_cpu() {
# and use second time update data to get cpu percentage of thread in 0.5 second interval
# 4. top v3.3, there is 1 black line between 2 update;
# but top v3.2, there is 2 blank lines between 2 update!
local -a top_cmd_line=(top -H -b -d "$top_delay" -n 2)
local -a top_cmd_line=(top -H -b -d "$top_delay" -n 2 -p "$java_pid_list")
# DO NOT combine var ps_out declaration and assignment, because its value is supplied by subshell.
local top_out
top_out=$(HOME="$tmp_store_dir" "${top_cmd_line[@]}")
Expand All @@ -370,7 +377,7 @@ __top_threadId_cpu() {
# from text line to empty line, increase block index
if (previousLine && !$0) blockIndex++
# only print 4th text block(blockIndex == 3), aka. process info of second top update
if (blockIndex == 3 && ($NF == "java" || $NF == "jsvc")) # $NF(last field) is command field
if (blockIndex == 3 && $1 ~ /^[0-9]+$/) # $NF(last field) is command field
print $1, $9 # $1 is thread id field, $9 is %cpu field
previousLine = $0
}' | sort -k2,2nr
Expand Down

0 comments on commit 1efb992

Please sign in to comment.