Skip to content

Commit 66f0856

Browse files
committed
use argument not option to set update delay/count
imitate vmstate style
1 parent b2c1920 commit 66f0856

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
有自己用的好的脚本 或是 平时常用但没有写成脚本的功能,欢迎提供([提交Issue](https://github.com/oldratlee/useful-scripts/issues))和分享([Fork后提交代码](https://github.com/oldratlee/useful-scripts/fork))! :sparkling_heart:
1313

14+
PS:
15+
16+
本仓库的脚本(如`Java`相关脚本)在阿里等公司(如随身云,见[`awesome-scripts`仓库](https://github.com/Suishenyun/awesome-scripts)说明)的线上生产环境部署使用。
17+
如果你的公司有部署使用,欢迎使用通过[提交Issue](https://github.com/oldratlee/useful-scripts/issues)告知,方便互相交流反馈~ :cupid:
18+
1419
:beginner: 快速下载&使用
1520
----------------------
1621

docs/java.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ show-busy-java-threads.sh -s <指定jstack命令的全路径>
6161
# 显式指定jstack命令的路径就反而显得更方便了
6262

6363
show-busy-java-threads.sh -a <输出记录到的文件>
64+
# 记录到文件以方便回溯查看
6465

65-
show-busy-java-threads.sh -t <重复执行的次数> -i <重复执行的间隔秒数>
66-
# 缺省执行一次;执行间隔缺省是3秒
66+
show-busy-java-threads.sh <重复执行的间隔秒数> [<重复执行的次数>]
67+
# 多次执行,类似vmstat行为
6768

6869
##############################
6970
# 注意:
@@ -74,20 +75,25 @@ sudo show-busy-java-threads.sh
7475

7576
# 帮助信息
7677
$ show-busy-java-threads.sh -h
77-
Usage: show-busy-java-threads.sh [OPTION]...
78+
Usage: show-busy-java-threads.sh [OPTION]... [delay [count]]
7879
Find out the highest cpu consumed threads of java, and print the stack of these threads.
79-
Example: show-busy-java-threads.sh -c 10
80+
81+
Example:
82+
show-busy-java-threads.sh # show busy java threads info
83+
show-busy-java-threads.sh 1 # update every 1 seconds, (stop by eg: CTRL+C)
84+
show-busy-java-threads.sh 3 10 # update every 3 seconds, update 10 times
8085

8186
Options:
8287
-p, --pid <java pid> find out the highest cpu consumed threads from the specifed java process,
8388
default from all java process.
8489
-c, --count <num> set the thread count to show, default is 5
8590
-a, --append-file <file> specify the file to append output as log
86-
-t, --repeat-times <num> specify the show times, default just show 1 time
87-
-i, --interval <secs> seconds to wait between updates, default 3 seconds
8891
-s, --jstack-path <path> specify the path of jstack command
8992
-F, --force set jstack to force a thread dump(use jstack -F option)
9093
-h, --help display this help and exit
94+
delay the delay between updates in seconds.
95+
count the number of updates.
96+
the usage of delay/count imitates vmstat
9197
```
9298
9399
### 示例
@@ -131,7 +137,7 @@ $ show-busy-java-threads.sh
131137
132138
多执行几次`show-busy-java-threads.sh`,如果上面情况高概率出现,则可以确定上面的判定。
133139
\# 因为调用越少代码执行越快,则出现在线程栈的概率就越低。
134-
\# 脚本有自动多次执行的功能,`-t`选项,详见使用文档
140+
\# 脚本有自动多次执行的功能,指定 重复执行的间隔秒数/重复执行的次数 参数
135141
136142
分析`shared.monitor.schedule.AppMonitorDataAvgScheduler.run`实现逻辑和调用方式,以优化实现解决问题。
137143
@@ -141,6 +147,8 @@ $ show-busy-java-threads.sh
141147
- [liuyangc3](https://github.com/liuyangc3)
142148
- 发现并解决`jstack`非当前用户`Java`进程的问题。 [#50](https://github.com/oldratlee/useful-scripts/pull/50)
143149
- 优化性能,通过`read -a`简化反复的`awk`操作。 [#51](https://github.com/oldratlee/useful-scripts/pull/51)
150+
- [superhj1987](https://github.com/superhj1987) / [lirenzuo](https://github.com/lirenzuo)
151+
- 提供/实现多次执行的功能 [superhj1987/awesome-scripts#1](https://github.com/superhj1987/awesome-scripts/issues/1)
144152
145153
:beer: [show-duplicate-java-classes](../show-duplicate-java-classes)
146154
----------------------

show-busy-java-threads.sh

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# $ ./show-busy-java-threads.sh
77
#
88
# @author Jerry Lee
9+
# @author superhj1987
910

1011
readonly PROG=`basename $0`
1112
readonly -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]]
1920
Find 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
2227
Options:
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
3238
EOF
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
3945
eval 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
7977
done
8078
count=${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

8484
colorPrint() {
8585
local color=$1
@@ -120,11 +120,13 @@ normalPrint() {
120120

121121
if [ -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-
189190
head_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

Comments
 (0)