Skip to content

Commit 4d1d331

Browse files
committed
fix bug: should re-jstack when next update
1 parent e47f67d commit 4d1d331

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

show-busy-java-threads

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ fi
188188
readonly uuid="${PROG}_`date +%s`_${RANDOM}_$$"
189189
readonly store_dir="/tmp/${uuid}"
190190
mkdir -p "$store_dir"
191+
readonly jstack_file_path_prefix="$store_dir/jstack_"
191192

192193
cleanupWhenExit() {
193194
rm -rf "$store_dir" &> /dev/null
@@ -204,8 +205,8 @@ printStackOfThreads() {
204205
local pcpu="${line[2]}"
205206
local user="${line[3]}"
206207

207-
((idx++))
208-
local jstackFile="$store_dir/${pid}"
208+
(( idx++ ))
209+
local jstackFile="$jstack_file_path_prefix${pid}"
209210
[ -f "${jstackFile}" ] || {
210211
if [ "${user}" == "${USER}" ]; then
211212
# run without sudo, when java process user is current user
@@ -243,11 +244,14 @@ printStackOfThreads() {
243244
fi
244245
sed "$sed_script" -n ${jstackFile} | tee ${append_file:+-a "$append_file"}
245246
done
247+
248+
# rm stack files, so as to re-jstack at next update
249+
rm "$jstack_file_path_prefix"* &> /dev/null
246250
}
247251

248252
headInfo() {
249253
colorEcho "0;34;42" ================================================================================
250-
echo "$(date "+%Y-%m-%d %H:%M:%S.%N") [$((i+1))/$update_count]: ${COMMAND_LINE[@]}"
254+
echo "$(date "+%Y-%m-%d %H:%M:%S.%N") [$(( i + 1 ))/$update_count]: ${COMMAND_LINE[@]}"
251255
colorEcho "0;34;42" ================================================================================
252256
echo
253257
}
@@ -290,19 +294,19 @@ findBusyJavaThreadsByTop() {
290294
print $1 " " threadId " " pcpu " " $3; exit
291295
}' )"
292296
if [ -n "$output_fields" ]; then
293-
((idx++))
297+
(( idx++ ))
294298
echo "$output_fields"
295299
fi
296300
done < <(HOME="$store_dir" top -H -b -n 1 | awk '$NF=="java"{print $1 " " $9}' | sort -k2,2nr)
297301
# top output field: thread id, %cpu
298302
#
299303
# 1. sort by %cpu by top option `-o %CPU`
300-
# unfortunately, top version 3.2 not supports -o option(supports from top version 3.3+),
304+
# unfortunately, top version 3.2 does not support -o option(supports from top version 3.3+),
301305
# use
302306
# HOME="$store_dir" top -H -b -n 1 | awk '$NF=="java"{print $1 " " $9}' | sort -k2,2nr
303307
# instead of
304308
# HOME="$store_dir" top -H -b -n 1 -o '%CPU' | awk '$NF=="java"{print $1 " " $9}'
305-
# 2. change HOME env var for running top,
309+
# 2. change HOME env var when run top,
306310
# so as to prevent top command output format being change by .toprc user config file unexpectedly
307311
}
308312

0 commit comments

Comments
 (0)