Skip to content

Commit 1cb12d2

Browse files
committed
update show-busy-java-threads.sh
1 parent 89c9707 commit 1cb12d2

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

docs/java.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ show-busy-java-threads.sh -c <要显示的线程栈数> -p <指定的Java Proces
2323

2424
```bash
2525
$ show-busy-java-threads.sh
26-
The stack of busy(57.0%) thread(23355/0x5b3b) of java process(23269) of user(admin):
26+
[1] Busy(57.0%) thread(23355/0x5b3b) stack of java process(23269) under user(admin):
2727
"pool-1-thread-1" prio=10 tid=0x000000005b5c5000 nid=0x5b3b runnable [0x000000004062c000]
2828
java.lang.Thread.State: RUNNABLE
2929
at java.text.DateFormat.format(DateFormat.java:316)
@@ -34,7 +34,7 @@ The stack of busy(57.0%) thread(23355/0x5b3b) of java process(23269) of user(adm
3434
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
3535
at java.lang.Thread.run(Thread.java:662)
3636

37-
The stack of busy(26.1%) thread(24018/0x5dd2) of java process(23269) of user(admin):
37+
[2] Busy(26.1%) thread(24018/0x5dd2) stack of java process(23269) under user(admin):
3838
"pool-1-thread-2" prio=10 tid=0x000000005a968800 nid=0x5dd2 runnable [0x00000000420e9000]
3939
java.lang.Thread.State: RUNNABLE
4040
at java.util.Arrays.copyOf(Arrays.java:2882)

show-busy-java-threads.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#
88
# @author Jerry Lee
99

10-
PROG=`basename $0`
10+
readonly PROG=`basename $0`
1111

1212
usage() {
1313
cat <<EOF
@@ -24,7 +24,7 @@ EOF
2424
exit $1
2525
}
2626

27-
ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
27+
readonly ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
2828
[ $? -ne 0 ] && usage 1
2929
eval set -- "${ARGS}"
3030

@@ -75,23 +75,25 @@ if ! which jstack &> /dev/null; then
7575
export PATH="$JAVA_HOME/bin:$PATH"
7676
fi
7777

78-
uuid=`date +%s`_${RANDOM}_$$
78+
readonly uuid=`date +%s`_${RANDOM}_$$
7979

8080
cleanupWhenExit() {
8181
rm /tmp/${uuid}_* &> /dev/null
8282
}
8383
trap "cleanupWhenExit" EXIT
8484

8585
printStackOfThread() {
86+
local threadLine
87+
local count=1
8688
while read threadLine ; do
87-
pid=`echo ${threadLine} | awk '{print $1}'`
88-
threadId=`echo ${threadLine} | awk '{print $2}'`
89-
threadId0x=`printf %x ${threadId}`
90-
user=`echo ${threadLine} | awk '{print $3}'`
91-
pcpu=`echo ${threadLine} | awk '{print $5}'`
92-
93-
jstackFile=/tmp/${uuid}_${pid}
94-
89+
local pid=`echo ${threadLine} | awk '{print $1}'`
90+
local threadId=`echo ${threadLine} | awk '{print $2}'`
91+
local threadId0x=`printf %x ${threadId}`
92+
local user=`echo ${threadLine} | awk '{print $3}'`
93+
local pcpu=`echo ${threadLine} | awk '{print $5}'`
94+
95+
local jstackFile=/tmp/${uuid}_${pid}
96+
9597
[ ! -f "${jstackFile}" ] && {
9698
jstack ${pid} > ${jstackFile} || {
9799
redEcho "Fail to jstack java process ${pid}!"
@@ -100,7 +102,7 @@ printStackOfThread() {
100102
}
101103
}
102104

103-
redEcho "Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
105+
redEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
104106
sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
105107
done
106108
}
@@ -110,4 +112,3 @@ ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
110112
awk '$4=="java"{print $0}' ||
111113
awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}'
112114
} | sort -k5 -r -n | head --lines "${count}" | printStackOfThread
113-

0 commit comments

Comments
 (0)