@@ -78,6 +78,10 @@ Options:
78
78
-c, --count <num> set the thread count to show, default is 5
79
79
-a, --append-file <file> specify the file to append output as log
80
80
-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.
81
85
-F, --force set jstack to force a thread dump
82
86
use when jstack <pid> does not respond (process is hung)
83
87
-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
101
105
# parse options
102
106
# ###############################################################################
103
107
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 -- " $@ " `
105
109
[ $? -ne 0 ] && usage 1
106
110
eval set -- " ${ARGS} "
107
111
@@ -123,17 +127,21 @@ while true; do
123
127
jstack_path=" $2 "
124
128
shift 2
125
129
;;
130
+ -P|--use-ps)
131
+ use_ps=true
132
+ shift
133
+ ;;
126
134
-F|--force)
127
135
force=-F
128
- shift 1
136
+ shift
129
137
;;
130
138
-m|--mix-native-frames)
131
139
mix_native_frames=-m
132
- shift 1
140
+ shift
133
141
;;
134
142
-l|--lock-info)
135
143
more_lock_info=-l
136
- shift 1
144
+ shift
137
145
;;
138
146
-h|--help)
139
147
usage
@@ -145,10 +153,11 @@ while true; do
145
153
esac
146
154
done
147
155
count=${count:- 5}
156
+ use_ps=${use_ps:- false}
148
157
149
158
update_delay=${1:- 0}
150
159
[ -z " $1 " ] && update_count=1 || update_count=${2:- 0}
151
- [ $ update_count -lt 0 ] && update_count=0
160
+ (( update_count < 0 )) && update_count=0
152
161
153
162
# ###############################################################################
154
163
# check the existence of jstack command
@@ -237,7 +246,21 @@ headInfo() {
237
246
echo
238
247
}
239
248
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 () {
241
264
if [ -n " ${pid} " ]; then
242
265
local ps_options=" -p $pid "
243
266
else
@@ -273,6 +296,9 @@ for ((i = 0; update_count <= 0 || i < update_count; ++i)); do
273
296
[ -n " $append_file " ] && headInfo >> " $append_file "
274
297
(( update_count != 1 )) && headInfo
275
298
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
278
304
done
0 commit comments