show-busy-java-threads
show-busy-java-threads -p <指定的Java进程Id>
show-busy-java-threads -p 42
show-busy-java-threads -p 42,47
show-busy-java-threads -c <要展示示的线程栈个数>
show-busy-java-threads <重复执行的间隔秒数> [<重复执行的次数>]
show-busy-java-threads -a <运行输出的记录到的文件>
show-busy-java-threads -S <存储jstack输出文件的目录>
sudo show-busy-java-threads
show-busy-java-threads -s <指定jstack命令的全路径>
show-busy-java-threads -m
show-busy-java-threads -F
show-busy-java-threads -l
$ show-busy-java-threads -h
Usage: show-busy-java-threads [OPTION]... [delay [count]]
Find out the highest cpu consumed threads of java processes,
and print the stack of these threads.
Example:
show-busy-java-threads
show-busy-java-threads 1
show-busy-java-threads 3 10
Output control:
-p, --pid <java pid(s)> find out the highest cpu consumed threads from
the specified java process.
support pid list(eg: 42,47).
default from all java process.
-c, --count <num> set the thread count to show, default is 5.
set count 0 to show all threads.
-a, --append-file <file> specifies the file to append output as log.
-S, --store-dir <dir> specifies the directory for storing
the intermediate files, and keep files.
default store intermediate files at tmp dir,
and auto remove after run. use this option to keep
files so as to review jstack/top/ps output later.
delay the delay between updates in seconds.
count the number of updates.
delay/count arguments imitates the style of
vmstat command.
jstack control:
-s, --jstack-path <path> specifies the path of jstack command.
-F, --force set jstack to force a thread dump.
use when jstack does not respond (process is hung).
-m, --mix-native-frames set jstack to print both java and
native frames (mixed mode).
-l, --lock-info set jstack with long listing.
prints additional information about locks.
CPU usage calculation control:
-i, --cpu-sample-interval specifies the delay between cpu samples to get
thread cpu usage percentage during this interval.
default is 0.5 (second).
set interval 0 to get the percentage of time spent
running during the *entire lifetime* of a process.
Miscellaneous:
-h, --help display this help and exit.
-V, --version display version information and exit.
#!/usr/bin/env bash
readonly PROG=${0##*/}
readonly PROG_VERSION='3.x-dev'
readonly -a COMMAND_LINE=("${BASH_SOURCE[0]}" "$@")
WHOAMI=$(whoami)
UNAME=$(uname)
readonly WHOAMI UNAME
readonly NL=$'\n'
colorPrint() {
local color=$1
shift
if [ -t 1 ]; then
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
else
printf '%s\n' "$*"
fi
}
__appendToFile() {
[[ -n "$append_file" && -w "$append_file" ]] && printf '%s\n' "$*" >>"$append_file"
[[ -n "$store_dir" && -w "$store_dir" ]] && printf '%s\n' "$*" >>"$store_file_prefix$PROG"
}
colorOutput() {
local color=$1
shift
colorPrint "$color" "$*"
__appendToFile "$*"
}
normalOutput() {
printf '%s\n' "$*"
__appendToFile "$*"
}
redOutput() {
colorOutput 31 "$*"
}
greenOutput() {
colorOutput 32 "$*"
}
yellowOutput() {
colorOutput 33 "$*"
}
blueOutput() {
colorOutput 36 "$*"
}
die() {
local prompt_help=false exit_status=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-s)
exit_status=$2
shift 2
;;
*)
break
;;
esac
done
(($# > 0)) && colorPrint "1;31" "$PROG: $*"
$prompt_help && echo "Try '$PROG --help' for more information."
exit "$exit_status"
} >&2
logAndRun() {
printf '%s\n' "$*"
echo
"$@"
}
logAndCat() {
printf '%s\n' "$*"
echo
cat
}
isNonNegativeFloatNumber() {
[[ "$1" =~ ^[+]?[0-9]+\.?[0-9]*$ ]]
}
isNaturalNumber() {
[[ "$1" =~ ^[+]?[0-9]+$ ]]
}
isNaturalNumberList() {
[[ "$1" =~ ^([0-9]+)(,[0-9]+)*$ ]]
}
printCallingCommandLine() {
local arg isFirst=true
for arg in "${COMMAND_LINE[@]}"; do
if $isFirst; then
isFirst=false
else
printf ' '
fi
printf '%q' "$arg"
done
echo
}
usage() {
cat <<EOF
Usage: $PROG [OPTION]... [delay [count]]
Find out the highest cpu consumed threads of java processes,
and print the stack of these threads.
Example:
$PROG # show busy java threads info
$PROG 1 # update every 1 second, (stop by eg: CTRL+C)
$PROG 3 10 # update every 3 seconds, update 10 times
Output control:
-p, --pid <java pid(s)> find out the highest cpu consumed threads from
the specified java process.
support pid list(eg: 42,47).
default from all java process.
-c, --count <num> set the thread count to show, default is 5.
set count 0 to show all threads.
-a, --append-file <file> specifies the file to append output as log.
-S, --store-dir <dir> specifies the directory for storing
the intermediate files, and keep files.
default store intermediate files at tmp dir,
and auto remove after run. use this option to keep
files so as to review jstack/top/ps output later.
delay the delay between updates in seconds.
count the number of updates.
delay/count arguments imitates the style of
vmstat command.
jstack control:
-s, --jstack-path <path> specifies the path of jstack command.
-F, --force set jstack to force a thread dump.
use when jstack does not respond (process is hung).
-m, --mix-native-frames set jstack to print both java and
native frames (mixed mode).
-l, --lock-info set jstack with long listing.
prints additional information about locks.
CPU usage calculation control:
-i, --cpu-sample-interval specifies the delay between cpu samples to get
thread cpu usage percentage during this interval.
default is 0.5 (second).
set interval 0 to get the percentage of time spent
running during the *entire lifetime* of a process.
Miscellaneous:
-h, --help display this help and exit.
-V, --version display version information and exit.
EOF
exit
}
progVersion() {
printf '%s\n' "$PROG $PROG_VERSION"
exit
}
[[ $UNAME = Linux* ]] || die "only support Linux, not support $UNAME yet!"
ARGS=$(
getopt -n "$PROG" -a -o c:p:a:s:S:i:Pd:FmlhV \
-l count:,pid:,append-file:,jstack-path:,store-dir:,cpu-sample-interval:,use-ps,top-delay:,force,mix-native-frames,lock-info,help,version \
-- "$@"
) || die -h
eval set -- "$ARGS"
unset ARGS
count=5
cpu_sample_interval=0.5
while true; do
case "$1" in
-c | --count)
count=$2
shift 2
;;
-p | --pid)
pid_list=$2
shift 2
;;
-a | --append-file)
append_file=$2
shift 2
;;
-s | --jstack-path)
jstack_path=$2
shift 2
;;
-S | --store-dir)
store_dir=$2
shift 2
;;
-P | --use-ps)
cpu_sample_interval=0
shift
;;
-i | --cpu-sample-interval | -d | --top-delay)
cpu_sample_interval=$2
shift 2
;;
-F | --force)
force=-F
shift
;;
-m | --mix-native-frames)
mix_native_frames=-m
shift
;;
-l | --lock-info)
lock_info=-l
shift
;;
-h | --help)
usage
;;
-V | --version)
progVersion
;;
--)
shift
break
;;
esac
done
readonly count cpu_sample_interval force mix_native_frames lock_info
readonly update_delay=${1:-0}
isNonNegativeFloatNumber "$update_delay" || die "update delay($update_delay) is not a non-negative float number!"
[ -z "$1" ] && update_count=1 || update_count=${2:-0}
isNaturalNumber "$update_count" || die "update count($update_count) is not a natural number!"
readonly update_count
if [ -n "$pid_list" ]; then
pid_list=${pid_list//[[:space:]]/}
isNaturalNumberList "$pid_list" || die "pid(s)($pid_list) is illegal! example: 42 or 42,99,67"
fi
readonly pid_list
if [ -n "$append_file" ]; then
if [ -e "$append_file" ]; then
[ -f "$append_file" ] || die "$append_file(specified by option -a, for storing run output files) exists but is not a file!"
[ -w "$append_file" ] || die "file $append_file(specified by option -a, for storing run output files) exists but is not writable!"
else
append_file_dir=$(dirname -- "$append_file")
if [ -e "$append_file_dir" ]; then
[ -d "$append_file_dir" ] || die "directory $append_file_dir(specified by option -a, for storing run output files) exists but is not a directory!"
[ -w "$append_file_dir" ] || die "directory $append_file_dir(specified by option -a, for storing run output files) exists but is not writable!"
else
mkdir -p "$append_file_dir" || die "fail to create directory $append_file_dir(specified by option -a, for storing run output files)!"
fi
fi
fi
readonly append_file
if [ -n "$store_dir" ]; then
if [ -e "$store_dir" ]; then
[ -d "$store_dir" ] || die "$store_dir(specified by option -S, for storing output files) exists but is not a directory!"
[ -w "$store_dir" ] || die "directory $store_dir(specified by option -S, for storing output files) exists but is not writable!"
else
mkdir -p "$store_dir" || die "fail to create directory $store_dir(specified by option -S, for storing output files)!"
fi
fi
readonly store_dir
isNonNegativeFloatNumber "$cpu_sample_interval" || die "cpu sample interval($cpu_sample_interval) is not a non-negative float number!"
if [ -n "$jstack_path" ]; then
[ -f "$jstack_path" ] || die "$jstack_path (set by -s option) is NOT found!"
[ -x "$jstack_path" ] || die "$jstack_path (set by -s option) is NOT executable!"
elif [ -n "$JAVA_HOME" ]; then
if [ -f "$JAVA_HOME/bin/jstack" ]; then
[ -x "$JAVA_HOME/bin/jstack" ] || die -h "found \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually."
jstack_path="$JAVA_HOME/bin/jstack"
elif [ -f "$JAVA_HOME/../bin/jstack" ]; then
[ -x "$JAVA_HOME/../bin/jstack" ] || die -h "found \$JAVA_HOME/../bin/jstack($JAVA_HOME/../bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually."
jstack_path="$JAVA_HOME/../bin/jstack"
fi
elif type -P jstack &>/dev/null; then
jstack_path=$(type -P jstack)
[ -x "$jstack_path" ] || die -h "found $jstack_path from PATH is NOT executable!${NL}Use -s option set jstack path manually."
else
die -h "jstack NOT found by JAVA_HOME(${JAVA_HOME:-not set}) setting and PATH!${NL}Use -s option set jstack path manually."
fi
readonly jstack_path
run_timestamp=$(date "+%Y-%m-%d_%H:%M:%S.%N")
readonly run_timestamp
readonly uuid="${PROG}_${run_timestamp}_${$}_${RANDOM}"
readonly tmp_store_dir="/tmp/$uuid"
if [ -n "$store_dir" ]; then
readonly store_file_prefix="$store_dir/${run_timestamp}_"
else
readonly store_file_prefix="$tmp_store_dir/${run_timestamp}_"
fi
mkdir -p "$tmp_store_dir"
cleanupWhenExit() {
rm -rf "$tmp_store_dir" &>/dev/null
}
trap cleanupWhenExit EXIT
headInfo() {
local timestamp=$1
colorPrint "0;34;42" ================================================================================
printf '%s\n' "$timestamp [$((update_round_num + 1))/$update_count]: $(printCallingCommandLine)"
colorPrint "0;34;42" ================================================================================
echo
}
if [ -n "$pid_list" ]; then
readonly ps_process_select_options="-p $pid_list"
else
readonly ps_process_select_options="-C java -C jsvc"
fi
__die_when_no_java_process_found() {
if [ -n "$pid_list" ]; then
die "process($pid_list) is not running, or not java process!"
else
die 'No java process found!'
fi
}
findBusyJavaThreadsByPs() {
local -a ps_cmd_line=(ps $ps_process_select_options -wwLo 'pid,lwp,pcpu,user' --no-headers)
local ps_out
ps_out=$("${ps_cmd_line[@]}" | sort -k3,3nr)
[ -n "$ps_out" ] || __die_when_no_java_process_found
if [ -n "$store_dir" ]; then
printf '%s\n' "$ps_out" | logAndCat "${ps_cmd_line[*]} | sort -k3,3nr" >"$store_file_prefix$((update_round_num + 1))_ps"
fi
if ((count > 0)); then
printf '%s\n' "$ps_out" | head -n "$count"
else
printf '%s\n' "$ps_out"
fi
}
__top_threadId_cpu() {
local java_pid_list
java_pid_list=$(ps $ps_process_select_options -o pid --no-headers)
[ -n "$java_pid_list" ] || __die_when_no_java_process_found
java_pid_list=$(echo $java_pid_list | tr ' ' ,)
local -a top_cmd_line=(top -H -b -d "$cpu_sample_interval" -n 2 -p "$java_pid_list")
local top_out
top_out=$(HOME=$tmp_store_dir "${top_cmd_line[@]}")
if [ -n "$store_dir" ]; then
printf '%s\n' "$top_out" | logAndCat "${top_cmd_line[@]}" >"$store_file_prefix$((update_round_num + 1))_top"
fi
local result_threads_top_info
result_threads_top_info=$(printf '%s\n' "$top_out" | awk '{
# from text line to empty line, increase block index
if (previousLine && !$0) blockIndex++
# only print 4th text block(blockIndex == 3), aka. process info of second top update
if (blockIndex == 3 && $1 ~ /^[0-9]+$/)
print $1, $9 # $1 is thread id field, $9 is %cpu field
previousLine = $0
}')
[ -n "$result_threads_top_info" ] || __die_when_no_java_process_found
printf '%s\n' "$result_threads_top_info" | sort -k2,2nr
}
__complete_pid_user_by_ps() {
local -a ps_cmd_line=(ps $ps_process_select_options -wwLo 'pid,lwp,user' --no-headers)
local ps_out
ps_out=$("${ps_cmd_line[@]}")
if [ -n "$store_dir" ]; then
printf '%s\n' "$ps_out" | logAndCat "${ps_cmd_line[@]}" >"$store_file_prefix$((update_round_num + 1))_ps"
fi
local idx=0 threadId pcpu output_fields
while read -r threadId pcpu; do
((count <= 0 || idx < count)) || break
output_fields=$(printf '%s\n' "$ps_out" | awk -v "threadId=$threadId" -v "pcpu=$pcpu" '$2==threadId {
print $1, threadId, pcpu, $3; exit
}')
if [ -n "$output_fields" ]; then
((idx++))
printf '%s\n' "$output_fields"
fi
done
}
findBusyJavaThreadsByTop() {
__top_threadId_cpu | __complete_pid_user_by_ps
}
printStackOfThreads() {
local idx=0 pid threadId pcpu user threadId0x
while read -r pid threadId pcpu user; do
printf -v threadId0x '%#x' "$threadId"
((idx++ > 0)) && normalOutput
local jstackFile="$store_file_prefix$((update_round_num + 1))_jstack_$pid"
[ -f "$jstackFile" ] || {
local -a jstack_cmd_line=("$jstack_path" $force $mix_native_frames $lock_info $pid)
if [ "$user" = "$WHOAMI" ]; then
logAndRun "${jstack_cmd_line[@]}" >"$jstackFile"
elif ((UID == 0)); then
logAndRun sudo -u "$user" "${jstack_cmd_line[@]}" >"$jstackFile"
else
redOutput "[$idx] Fail to jstack busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user)."
redOutput "User of java process($user) is not current user($WHOAMI), need sudo to rerun:"
yellowOutput " sudo $(printCallingCommandLine)"
continue
fi || {
redOutput "[$idx] Fail to jstack busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user)."
rm "$jstackFile" &>/dev/null
continue
}
}
blueOutput "[$idx] Busy($pcpu%) thread($threadId/$threadId0x) stack of java process($pid) under user($user):"
if [ -n "$mix_native_frames" ]; then
local sed_script="/--------------- $threadId ---------------/,/^---------------/ {
/--------------- $threadId ---------------/b # skip first separator line
/^---------------/d # delete second separator line
p
}"
elif [ -n "$force" ]; then
local sed_script="/^Thread $threadId:/,/^$/ {
/^$/d; p # delete end separator line
}"
else
local sed_script="/ nid=($threadId0x|$threadId) /,/^$/ {
/^$/d; p # delete end separator line
}"
fi
sed "$sed_script" -n -r "$jstackFile" | tee ${append_file:+-a "$append_file"} ${store_dir:+-a "$store_file_prefix$PROG"}
done
}
main() {
local update_round_num timestamp
for ((update_round_num = 0; update_count <= 0 || update_round_num < update_count; ++update_round_num)); do
((update_round_num > 0)) && {
sleep "$update_delay"
normalOutput
}
timestamp=$(date "+%Y-%m-%d %H:%M:%S.%N")
[[ -n "$append_file" || -n "$store_dir" ]] && headInfo "$timestamp" |
tee ${append_file:+-a "$append_file"} ${store_dir:+-a "$store_file_prefix$PROG"} >/dev/null
((update_count != 1)) && headInfo "$timestamp"
if [ "$cpu_sample_interval" = 0 ]; then
findBusyJavaThreadsByPs
else
findBusyJavaThreadsByTop
fi | printStackOfThreads
done
}
main