1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
| #!/bin/bash
PROG="$(basename "$0")" readonly PROG_VERSION='2.5.0-dev'
readonly -a COMMAND_LINE=("${BASH_SOURCE[0]}" "$@")
USER="$(whoami)"
readonly ec=$'\033' readonly eend=$'\033[0m' readonly nl=$'\n'
colorEcho() { local color=$1 shift
[ -t 1 ] && echo "${ec}[1;${color}m$*$eend" || echo "$@" }
colorPrint() { local color=$1 shift
colorEcho "$color" "$@" [[ -n "$append_file" && -w "$append_file" ]] && echo "$@" >>"$append_file" [[ -n "$store_dir" && -w "$store_dir" ]] && echo "$@" >>"${store_file_prefix}$PROG" }
normalPrint() { echo "$@" [[ -n "$append_file" && -w "$append_file" ]] && echo "$@" >>"$append_file" [[ -n "$store_dir" && -w "$store_dir" ]] && echo "$@" >>"${store_file_prefix}$PROG" }
redPrint() { colorPrint 31 "$@" }
greenPrint() { colorPrint 32 "$@" }
yellowPrint() { colorPrint 33 "$@" }
bluePrint() { colorPrint 36 "$@" }
die() { redPrint "Error: $*" 1>&2 exit 1 }
logAndRun() { echo "$@" echo "$@" }
logAndCat() { echo "$@" 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() { local -r exit_code="${1:-0}" (($# > 0)) && shift [ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
(($# > 0)) && colorEcho 31 "$*$nl" >$out
cat >$out <<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} ${PROG} 1 ${PROG} 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. EOF
exit "$exit_code" }
progVersion() { echo "$PROG $PROG_VERSION" exit }
uname | grep '^Linux' -q || die "$PROG 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 \ -- "$@" ) || { echo usage 1 } eval set -- "${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) more_lock_info=-l shift ;; -h | --help) usage ;; -V | --version) progVersion ;; --) shift break ;; esac done
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!"
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
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
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
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 "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 "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 command -v jstack &>/dev/null; then jstack_path="$(command -v jstack)" [ -x "$jstack_path" ] || die "found $jstack_path from PATH is NOT executable!${nl}Use -s option set jstack path manually." else die "jstack NOT found by JAVA_HOME(${JAVA_HOME:-not set}) setting and PATH!${nl}Use -s option set jstack path manually." fi
run_timestamp="$(date "+%Y-%m-%d_%H:%M:%S.%N")" 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() { colorEcho "0;34;42" ================================================================================ echo "$(date "+%Y-%m-%d %H:%M:%S.%N") [$((update_round_num + 1))/$update_count]: $(printCallingCommandLine)" colorEcho "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 echo "$ps_out" | logAndCat "${ps_cmd_line[*]} | sort -k3,3nr" >"${store_file_prefix}$((update_round_num + 1))_ps" fi
if ((count > 0)); then echo "$ps_out" | head -n "${count}" else echo "$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 echo "$top_out" | logAndCat "${top_cmd_line[@]}" >"${store_file_prefix}$((update_round_num + 1))_top" fi
local result_threads_top_info result_threads_top_info=$( echo "$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
echo "$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 echo "$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="$(echo "$ps_out" | awk -v "threadId=$threadId" -v "pcpu=$pcpu" '$2==threadId { print $1, threadId, pcpu, $3; exit }')" if [ -n "$output_fields" ]; then ((idx++)) echo "$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 threadId0x="0x$(printf %x "${threadId}")"
((idx++)) local jstackFile="${store_file_prefix}$((update_round_num + 1))_jstack_${pid}" [ -f "${jstackFile}" ] || { local -a jstack_cmd_line=("$jstack_path" ${force} $mix_native_frames $more_lock_info ${pid}) if [ "${user}" == "${USER}" ]; then logAndRun "${jstack_cmd_line[@]}" >"${jstackFile}" elif [ $UID == 0 ]; then logAndRun sudo -u "${user}" "${jstack_cmd_line[@]}" >"${jstackFile}" else redPrint "[$idx] Fail to jstack busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})." redPrint "User of java process($user) is not current user($USER), need sudo to rerun:" yellowPrint " sudo $(printCallingCommandLine)" normalPrint continue fi || { redPrint "[$idx] Fail to jstack busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})." normalPrint rm "${jstackFile}" &>/dev/null continue } }
bluePrint "[$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} /,/^$/ { /^$/d; p # delete end separator line }" fi { sed "$sed_script" -n "${jstackFile}" echo } | tee ${append_file:+-a "$append_file"} ${store_dir:+-a "${store_file_prefix}$PROG"} done }
################################################################################ # Main ################################################################################
main() { local update_round_num # if update_count <= 0, infinite loop till user interrupted (eg: CTRL+C) for ((update_round_num = 0; update_count <= 0 || update_round_num < update_count; ++update_round_num)); do ((update_round_num > 0)) && sleep "$update_delay"
[[ -n "$append_file" || -n "$store_dir" ]] && headInfo | tee ${append_file:+-a "$append_file"} ${store_dir:+-a "${store_file_prefix}$PROG"} >/dev/null ((update_count != 1)) && headInfo
if [ "$cpu_sample_interval" == 0 ]; then findBusyJavaThreadsByPs else findBusyJavaThreadsByTop fi | printStackOfThreads done }
main
|