Skip to content

Commit 41ea658

Browse files
committed
add option use-ps
1 parent e0e44f8 commit 41ea658

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

show-busy-java-threads

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ Options:
7878
-c, --count <num> set the thread count to show, default is 5
7979
-a, --append-file <file> specify the file to append output as log
8080
-s, --jstack-path <path> specify the path of jstack command
81+
-P, --use-ps use ps command to find busy thead(cpu usage) instead of top command,
82+
default use top command, because cpu usage of ps command is expressed as
83+
the percentage of time spent running during the entire lifetime of a process,
84+
this is not ideal.
8185
-F, --force set jstack to force a thread dump
8286
use when jstack <pid> does not respond (process is hung)
8387
-m, --mix-native-frames set jstack to print both java and native frames (mixed mode)
@@ -101,7 +105,7 @@ uname | grep '^Linux' -q || fatal "Error: $PROG only support Linux, not support
101105
# parse options
102106
################################################################################
103107

104-
readonly ARGS=`getopt -n "$PROG" -a -o p:c:a:s:Fmlh -l count:,pid:,append-file:,jstack-path:,force,mix-native-frames,lock-info,help -- "$@"`
108+
readonly ARGS=`getopt -n "$PROG" -a -o p:c:a:s:PFmlh -l count:,pid:,append-file:,jstack-path:,use-ps,force,mix-native-frames,lock-info,help -- "$@"`
105109
[ $? -ne 0 ] && usage 1
106110
eval set -- "${ARGS}"
107111

@@ -123,17 +127,21 @@ while true; do
123127
jstack_path="$2"
124128
shift 2
125129
;;
130+
-P|--use-ps)
131+
use_ps=true
132+
shift
133+
;;
126134
-F|--force)
127135
force=-F
128-
shift 1
136+
shift
129137
;;
130138
-m|--mix-native-frames)
131139
mix_native_frames=-m
132-
shift 1
140+
shift
133141
;;
134142
-l|--lock-info)
135143
more_lock_info=-l
136-
shift 1
144+
shift
137145
;;
138146
-h|--help)
139147
usage
@@ -145,10 +153,11 @@ while true; do
145153
esac
146154
done
147155
count=${count:-5}
156+
use_ps=${use_ps:-false}
148157

149158
update_delay=${1:-0}
150159
[ -z "$1" ] && update_count=1 || update_count=${2:-0}
151-
[ $update_count -lt 0 ] && update_count=0
160+
(( update_count < 0 )) && update_count=0
152161

153162
################################################################################
154163
# check the existence of jstack command
@@ -237,7 +246,21 @@ headInfo() {
237246
echo
238247
}
239248

240-
find_top_java_thread() {
249+
# output field: pid, thread id(lwp), pcpu, user
250+
# order by pcpu(percentage of cpu usage)
251+
findBusyJavaThreadByPs() {
252+
if [ -n "${pid}" ]; then
253+
local ps_options="-p $pid"
254+
else
255+
local ps_options="-C java"
256+
fi
257+
# sort by %cpu(by option `--sort -pcpu`)
258+
# use wide output(unlimited width), avoid trunk user column to username_fo+ or $uid alike
259+
ps $ps_options -wwLo pid,lwp,pcpu,user --sort -pcpu --no-headers | head -n "${count}"
260+
}
261+
262+
# output is same as function findBusyJavaThreadByPs
263+
findBusyJavaThreadByTop() {
241264
if [ -n "${pid}" ]; then
242265
local ps_options="-p $pid"
243266
else
@@ -273,6 +296,9 @@ for ((i = 0; update_count <= 0 || i < update_count; ++i)); do
273296
[ -n "$append_file" ] && headInfo >> "$append_file"
274297
(( update_count != 1 )) && headInfo
275298

276-
# use wide output(unlimited width), avoid trunk user column to username_fo+ or $uid alike
277-
find_top_java_thread | printStackOfThreads
299+
if $use_ps; then
300+
findBusyJavaThreadByPs
301+
else
302+
findBusyJavaThreadByTop
303+
fi | printStackOfThreads
278304
done

0 commit comments

Comments
 (0)