From 74e6cb4a388d00c95c146c47e89ea833b3a01eba Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Wed, 31 Mar 2021 11:56:09 +0800 Subject: [PATCH] ! show-busy-java-threads: fix top extract error by process name 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 ... ``` --- bin/show-busy-java-threads | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bin/show-busy-java-threads b/bin/show-busy-java-threads index 8489de8c..928d6745 100755 --- a/bin/show-busy-java-threads +++ b/bin/show-busy-java-threads @@ -343,6 +343,14 @@ findBusyJavaThreadsByPs() { # top with output field: thread id, %cpu __top_threadId_cpu() { + local java_pid_list="${pid:-}" + if [ -z "$java_pid_list" ]; then + # 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 , + fi + # 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 @@ -357,7 +365,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[@]}") @@ -370,7 +378,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]+$/) print $1, $9 # $1 is thread id field, $9 is %cpu field previousLine = $0 }' | sort -k2,2nr