Skip to content

Commit 2d37450

Browse files
committed
improve comments
1 parent 6538777 commit 2d37450

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

docs/java.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
用于快速排查`Java``CPU`性能问题(`top us`值过高),自动查出运行的`Java`进程中消耗`CPU`多的线程,并打印出其线程栈,从而确定导致性能问题的方法调用。
3232
目前只支持`Linux`。原因是`Mac``Windows``ps`命令不支持列出线程线程,更多信息参见[#33](https://github.com/oldratlee/useful-scripts/issues/33),欢迎提供解法。
3333

34-
PS,如何操作可以参见[@bluedavy](http://weibo.com/bluedavy)的《分布式Java应用》的【5.1.1 cpu消耗分析】一节,说得很详细:
34+
PS,如何操作可以参见[@bluedavy](http://weibo.com/bluedavy)[《分布式Java应用》](https://book.douban.com/subject/4848587/)的【5.1.1 cpu消耗分析】一节,说得很详细:
3535

3636
1. `top`命令找出有问题`Java`进程及线程`id`
3737
1. 开启线程显示模式(`top -H`,或是打开`top`后按`H`
@@ -42,7 +42,7 @@ PS,如何操作可以参见[@bluedavy](http://weibo.com/bluedavy)的《分布
4242
1. 查找十六进制的线程`id`(可以用`vim`的查找功能`/0x1234`,或是`grep 0x1234 -A 20`
4343
1. 查看对应的线程栈,以分析问题
4444

45-
查问题时,会要多次这样操作以确定问题,上面过程**太繁琐太慢了**
45+
查问题时,会要多次上面的操作以分析确定问题,这个过程**太繁琐太慢了**
4646

4747
### 用法
4848

@@ -139,7 +139,9 @@ $ show-busy-java-threads
139139
at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128)
140140
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
141141
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
142-
...
142+
at java.lang.Thread.run(Thread.java:662)
143+
144+
......
143145
```
144146
145147
上面的线程栈可以看出,`CPU`消耗最高的2个线程都在执行`java.text.DateFormat.format`,业务代码对应的方法是`shared.monitor.schedule.AppMonitorDataAvgScheduler.run`。可以基本确定:

find-in-jars

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ else
139139
fi
140140

141141
################################################################################
142-
# find logic
142+
# utils
143143
################################################################################
144144

145145
[ -t 1 ] && readonly is_console=true || readonly is_console=false
@@ -174,6 +174,9 @@ clearResponsiveMessage() {
174174
echo -n "$ec[2K$cr"
175175
}
176176

177+
################################################################################
178+
# find logic
179+
################################################################################
177180

178181
readonly jar_files="$(find "${dirs[@]}" -iname '*.jar')"
179182
readonly total_count="$(echo "$jar_files" | wc -l)"

parseOpts.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# @author Jerry Lee (oldratlee at gmail dot com)
2020

2121
#####################################################################
22-
# Utils Methods
22+
# Utils Funtions
2323
#####################################################################
2424

2525
# NOTE: $'foo' is the escape sequence syntax of bash
@@ -50,7 +50,7 @@ _opts_convertToVarName() {
5050
}
5151

5252
#####################################################################
53-
# Parse Methods
53+
# Parse Functions
5454
#
5555
# Use Globle Variable:
5656
# * _OPT_INFO_LIST_INDEX : Option info, data structure.
@@ -315,7 +315,7 @@ parseOpts() {
315315
}
316316

317317
#####################################################################
318-
# Show parsed Option Info Methods
318+
# Show parsed Option Info Functions
319319
#####################################################################
320320

321321
_opts_showOptDescInfoList() {

show-busy-java-threads

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ uname | grep '^Linux' -q || {
1919
}
2020

2121
# Get corrent current user name via whoami command
22-
# See get https://www.lifewire.com/current-linux-user-whoami-command-3867579
23-
# Because if use `sudo -u` to run command, env var $USER is not rewrited/correct, just inherited from outside!
22+
# See https://www.lifewire.com/current-linux-user-whoami-command-3867579
23+
# Because if run command by `sudo -u`, env var $USER is not rewrited/correct, just inherited from outside!
2424
readonly USER="`whoami`"
2525

2626
usage() {
@@ -54,6 +54,10 @@ EOF
5454
exit $1
5555
}
5656

57+
################################################################################
58+
# parse options
59+
################################################################################
60+
5761
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 -- "$@"`
5862
[ $? -ne 0 ] && usage 1
5963
eval set -- "${ARGS}"
@@ -103,19 +107,20 @@ update_delay=${1:-0}
103107
[ -z "$1" ] && update_count=1 || update_count=${2:-0}
104108
[ $update_count -lt 0 ] && update_count=0
105109

110+
################################################################################
111+
# utils funtions
112+
################################################################################
113+
106114
# NOTE: $'foo' is the escape sequence syntax of bash
107115
readonly ec=$'\033' # escape char
108116
readonly eend=$'\033[0m' # escape end
109117

110118
colorPrint() {
111119
local color=$1
112120
shift
113-
if [ -t 1 ] ; then
114-
# if stdout is console, turn on color output.
115-
echo "$ec[1;${color}m$@$eend"
116-
else
117-
echo "$@"
118-
fi
121+
122+
# if stdout is console, turn on color output.
123+
[ -t 1 ] && echo "$ec[1;${color}m$@$eend" || echo "$@"
119124

120125
[ -n "$append_file" ] && echo "$@" >> "$append_file"
121126
}
@@ -142,13 +147,16 @@ normalPrint() {
142147
[ -n "$append_file" ] && echo "$@" >> "$append_file"
143148
}
144149

150+
################################################################################
151+
# check the existence of jstack command!
152+
################################################################################
153+
145154
if [ -n "$jstack_path" ]; then
146155
[ ! -x "$jstack_path" ] && {
147156
redPrint "Error: $jstack_path is NOT found/executalbe!" 1>&2
148157
exit 1
149158
}
150159
elif which jstack &> /dev/null; then
151-
# Check the existence of jstack command!
152160
jstack_path="`which jstack`"
153161
else
154162
[ -z "$JAVA_HOME" ] && {
@@ -166,6 +174,10 @@ else
166174
jstack_path="$JAVA_HOME/bin/jstack"
167175
fi
168176

177+
################################################################################
178+
# biz logic
179+
################################################################################
180+
169181
readonly uuid=`date +%s`_${RANDOM}_$$
170182

171183
cleanupWhenExit() {
@@ -192,14 +204,14 @@ printStackOfThreads() {
192204
elif [ $UID == 0 ]; then
193205
sudo -u "${user}" "$jstack_path" ${force} $mix_native_frames $more_lock_info ${pid} > ${jstackFile}
194206
else
195-
redPrint "[$counter] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
207+
redPrint "[$counter] Fail to jstack busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
196208
redPrint "User of java process($user) is not current user($USER), need sudo to run again:"
197209
yellowPrint " sudo ${COMMAND_LINE[@]}"
198210
normalPrint
199211
continue
200212
fi
201213
} || {
202-
redPrint "[$counter] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
214+
redPrint "[$counter] Fail to jstack busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
203215
normalPrint
204216
rm ${jstackFile}
205217
continue

test-cases/parseOpts-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ BASE=`dirname $0`
66

77

88
#################################################
9-
# Utils Methods
9+
# Utils Functions
1010
#################################################
1111

1212
# NOTE: $'foo' is the escape sequence syntax of bash

0 commit comments

Comments
 (0)