Skip to content

Commit b155a46

Browse files
committed
refactor: use one awk command instead two successive awk
1 parent 0e74f68 commit b155a46

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

show-busy-java-threads

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,20 @@ __top_threadId_cpu() {
316316
# HOME="$tmp_store_dir" top -H -b -n 1 -o '%CPU'
317317
# 2. change HOME env var when run top,
318318
# so as to prevent top command output format being change by .toprc user config file unexpectedly
319-
# 3. use option `-d 0.1`(interval 0.1 second) and `-n 2`(show 2 times), and use second time update data
320-
# to get cpu percentage of thread in 0.1 second interval
319+
# 3. use option `-d 0.5`(interval 0.5 second) and `-n 2`(show 2 times), and use second time update data
320+
# to get cpu percentage of thread in 0.5 second interval
321321
HOME="$tmp_store_dir" top -H -b -d $top_delay -n 2 |
322322
awk '{
323-
if (idx == 3 && $0 != "")
324-
# only print 4th text block
325-
# aka. process info of second top update
326-
print
323+
if (idx == 3 && $NF == "java") # $NF is command
324+
# only print 4th text block(idx == 3)
325+
# aka. process info of second top update
326+
print $1 " " $9 # $1 is thread id, $9 is %cpu
327327
if($0 == "")
328328
idx++
329-
}' |
330-
awk '$NF=="java"{print $1 " " $9}' | sort -k2,2nr
329+
}' | sort -k2,2nr
331330
}
332331

333-
# output is same as function findBusyJavaThreadsByPs
332+
# output format is same as function findBusyJavaThreadsByPs
334333
findBusyJavaThreadsByTop() {
335334
if [ -n "${pid}" ]; then
336335
local -r ps_options="-p $pid"

0 commit comments

Comments
 (0)