66# $ ./show-busy-java-threads.sh
77#
88# @author Jerry Lee
9+ # @author superhj1987
910
1011readonly PROG=` basename $0 `
1112readonly -a COMMAND_LINE=(" $0 " " $@ " )
@@ -15,26 +16,31 @@ usage() {
1516 [ -n " $1 " -a " $1 " != 0 ] && out=/dev/stderr || out=/dev/stdout
1617
1718 > $out cat << EOF
18- Usage: ${PROG} [OPTION]...
19+ Usage: ${PROG} [OPTION]... [delay [count]]
1920Find out the highest cpu consumed threads of java, and print the stack of these threads.
20- Example: ${PROG} -c 10
21+
22+ Example:
23+ ${PROG} # show busy java threads info
24+ ${PROG} 1 # update every 1 seconds, (stop by eg: CTRL+C)
25+ ${PROG} 3 10 # update every 3 seconds, update 10 times
2126
2227Options:
2328 -p, --pid <java pid> find out the highest cpu consumed threads from the specifed java process,
2429 default from all java process.
2530 -c, --count <num> set the thread count to show, default is 5
2631 -a, --append-file <file> specify the file to append output as log
27- -t, --repeat-times <num> specify the show times, default just show 1 time
28- -i, --interval <secs> seconds to wait between updates, default 3 seconds
2932 -s, --jstack-path <path> specify the path of jstack command
3033 -F, --force set jstack to force a thread dump(use jstack -F option)
3134 -h, --help display this help and exit
35+ delay the delay between updates in seconds.
36+ count the number of updates.
37+ the usage of delay/count imitates vmstat
3238EOF
3339
3440 exit $1
3541}
3642
37- readonly ARGS=` getopt -n " $PROG " -a -o p:c:a:t:i: s:Fh -l count:,pid:,append-file:,repeat-times:,interval :,jstack-path:,force,help -- " $@ " `
43+ readonly ARGS=` getopt -n " $PROG " -a -o p:c:a:s:Fh -l count:,pid:,append-file:,jstack-path:,force,help -- " $@ " `
3844[ $? -ne 0 ] && usage 1
3945eval set -- " ${ARGS} "
4046
@@ -52,14 +58,6 @@ while true; do
5258 append_file=" $2 "
5359 shift 2
5460 ;;
55- -t|--repeat-times)
56- repeat_times=" $2 "
57- shift 2
58- ;;
59- -i|--interval)
60- interval=" $2 "
61- shift 2
62- ;;
6361 -s|--jstack-path)
6462 jstack_path=" $2 "
6563 shift 2
@@ -78,8 +76,10 @@ while true; do
7876 esac
7977done
8078count=${count:- 5}
81- repeat_times=${repeat_times:- 1}
82- interval=${interval:- 3}
79+
80+ update_delay=${1:- 0}
81+ [ -z " $1 " ] && update_count=1 || update_count=${2:- 0}
82+ [ $update_count -lt 0 ] && update_count=0
8383
8484colorPrint () {
8585 local color=$1
@@ -120,11 +120,13 @@ normalPrint() {
120120
121121if [ -n " $jstack_path " ]; then
122122 ! [ -x " $jstack_path " ] && {
123- redPrint " Error: $jstack_path is NOT executalbe!" 1>&2
123+ redPrint " Error: $jstack_path is NOT found/ executalbe!" 1>&2
124124 exit 1
125125 }
126- # Check the existence of jstack command!
127- elif ! which jstack & > /dev/null; then
126+ elif which jstack & > /dev/null; then
127+ # Check the existence of jstack command!
128+ jstack_path=" ` which jstack` "
129+ else
128130 [ -z " $JAVA_HOME " ] && {
129131 redPrint " Error: jstack not found on PATH!" 1>&2
130132 exit 1
@@ -185,19 +187,19 @@ printStackOfThreads() {
185187 done
186188}
187189
188-
189190head_info () {
190191 echo ================================================================================
191- echo " $( date " +%Y-%m-%d %H:%M:%S.%N" ) : ${COMMAND_LINE[@]} "
192+ echo " $( date " +%Y-%m-%d %H:%M:%S.%N" ) [ $(( i + 1 )) / $update_count ] : ${COMMAND_LINE[@]} "
192193 echo ================================================================================
193194 echo
194195}
195196
196- for (( i = 0 ; i < repeat_times; ++ i)) ; do
197- [ " $i " -gt 0 ] && sleep " $interval "
197+ # if update_count <= 0, infinite loop till user interupted (eg: CTRL+C)
198+ for (( i = 0 ; update_count <= 0 || i < update_count; ++ i)) ; do
199+ [ " $i " -gt 0 ] && sleep " $update_delay "
198200
199201 [ -n " $append_file " ] && head_info >> " $append_file "
200- [ " $repeat_times " -gt 1 ] && head_info
202+ [ " $update_count " -ne 1 ] && head_info
201203
202204 ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
203205 [ -z " ${pid} " ] &&
0 commit comments