程序笔记   发布时间:2022-07-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了show-busy-java-threads脚本内容及使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

show-busy-java-threads脚本内容及使用

@R_673_11266@CPU占用高的排查

CPU占用较高,top -Hp <pid>查看发现占用CPU高的线程一直在变化,并非某个线程一直占用;

使用jstack无法抓到准确的信息,故使用

show-busy-java-threads脚本

先附脚本内容

#!/bin/bash# @Function# Find out the highest cpu consumed threads of java processes, and print the stack of these threads.## @Usage# $ ./show-busy-java-threads## @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#-show-busy-java-threads# @author jerry Lee (oldratlee at gmail dot com)# @author superhj1987 (superhj1987 at 126 dot com)

readonly PROG="`basename $0`"readonly -a COMMAND_LINE=("$0" "$@")# Get current user name via whoami command# See https://www.lifewire.com/current-linux-user-whoami-command-3867579# Because if run command by `sudo -u`, env var $USER is not rewritten/correct, just inherited from outside!readonly USER="`whoami`"

################################################################################# util functions################################################################################

# NOTE: $'foo' is the escape sequence syntax of bashreadonly ec=$'33' # escape charreadonly eend=$'33[0m' # escape end

colorEcho() { local color=$1 shift

# if stdout is console, turn on color output. [ -t 1 ] && echo "$ec[1;${Color}m$@$eend" || echo "$@"}

colorPrint() { local color=$1 shift

colorEcho "$color" "$@" [ -n "$append_file" -a -w "$append_file" ] && echo "$@" >> "$append_file" [ -n "$store_dir" -a -w "$store_dir" ] && echo "$@" >> "${store_file_prefix}$PROG"}

normalPrint() { echo "$@" [ -n "$append_file" -a -w "$append_file" ] && echo "$@" >> "$append_file" [ -n "$store_dir" -a -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}

usage() { local -r exit_code="$1" shift [ -n "$exit_code" -a "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout

(( $# > 0 )) && { echo "$@"; echo; } > $out

> $out cat <<EOFUsage: ${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 timesOutput control: -p, --pid <java pid> find out the highest cpu consumed threads from the specified java process. default from all java process. -c, --count <num> set the thread count to show, default is 5. -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: -d, --top-delay specifies the delay between top samples. default is 0.5 (second). get thread cpu percentage during this delay interval. more info see top -d option. eg: -d 1 (1 second). -P, --use-ps use ps command to find busy thread(cpu usagE) instead of top command. default use top command, because cpu usage of ps command is expressed as the percentage of time spent running during the *entire lifetime* of a process, this is not ideal In general.Miscellaneous: -h, --Help display this Help and exit.EOF

exit $exit_codE}

################################################################################# check os support################################################################################

uname | grep '^Linux' -q || die "$PROG only support Linux, not support `uname` yet!"

################################################################################# parse options################################################################################

# NOTE: ARGS can not be declared as readonly!!# readonly declaration make exit code of assignment to be always 0, aka. the exit code of `getopt` in subsHell is discarded.# tested on bash 4.2.46ARGS=`getopt -n "$PROG" -a -o p:c:a:s:S:Pd:Fmlh -l count:,pid:,append-file:,jstack-path:,store-dir:,use-ps,top-delay:,force,mix-native-frames,lock-info,Help -- "$@"`[ $? -ne 0 ] && { echo; usage 1; }eval set -- "${ARGS}"

while true; do case "$1" in -c|--count) count="$2" shift 2 ;; -p|--pid) pid="$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) use_ps=true shift ;; -d|--top-delay) top_delay="$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 ;; --) shift break ;; esacdone

count=${Count:-5}

update_delay=${1:-0}[ -z "$1" ] && update_count=1 || update_count=${2:-0}(( update_count < 0 )) && update_count=0

top_delay=${top_delay:-0.5}use_ps=${use_ps:-falsE}

# check the directory of append-file(-a) mode, create if not exsit.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 fifi

# check store directory(-S) mode, create directory if not exsit.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)!" fifi

################################################################################# check the existence of jstack command################################################################################

if [ -n "$jstack_path" ]; then [ -f "$jstack_path" ] || die "$jstack_path is NOT found!" [ -x "$jstack_path" ] || die "$jstack_path is NOT executalbe!"elif which jstack &> /dev/null; then jstack_path="`which jstack`"else [ -n "$JAVA_HOME" ] || die "jstack not found on PATH and No JAVA_HOME setTing! Use -s option set jstack path manually." [ -f "$JAVA_HOME/bin/jstack" ] || die "jstack not found on PATH and $JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) file does NOT exists! Use -s option set jstack path manually." [ -x "$JAVA_HOME/bin/jstack" ] || die "jstack not found on PATH and $JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executalbe! Use -s option set jstack path manually." jstack_path="$JAVA_HOME/bin/jstack"fi

################################################################################# biz logic################################################################################

readonly 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}_"fimkdir -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") [$(( i + 1 ))/$update_count]: ${COMMAND_LINE[@]}" colorEcho "0;34;42" ================================================================================ echo}

if [ -n "${piD}" ]; then readonly ps_process_@R_@R_874_11248@_10288@ct_options="-p $pid"else readonly ps_process_@R_@R_874_11248@_10288@ct_options="-C java -C jsvc"fi

# output field: pid, thread id(lwp), pcpu, user# order by pcpu(percentage of cpu usagE)findBusyJavaThreadsByPs() { # 1. sort by %cpu by ps option `--sort -pcpu` # 2. use wide output(unlimited width) by ps option `-ww` # avoid trunk user column to username_fo+ or $uid alike local -a ps_cmd_line=(ps $ps_process_@R_@R_874_11248@_10288@ct_options -wwLo pid,lwp,pcpu,user --sort -pcpu --no-headers) local -r ps_out="$("${ps_cmd_line[@]}")" if [ -n "$store_dir" ]; then echo "$ps_out" | logAndCat "${ps_cmd_line[@]}" > "${store_file_prefix}$(( i + 1 ))_ps" fi

echo "$ps_out" | head -n "${Count}"}

# top with output field: thread id, %cpu__top_threadId_cpu() { # 1. sort by %cpu by top option `-o %CPU` # unfortunately, top version 3.2 does not support -o option(supports from top version 3.3+), # use # HOME="$tmp_store_dir" top -H -b -n 1 # combined # sort # instead of # HOME="$tmp_store_dir" top -H -b -n 1 -o '%CPU' # 2. change HOME env var when run top, # so as to prevent top command output format being change by .toprc user config file unexpectedly # 3. use option `-d 0.5`(updatE interval 0.5 second) and `-n 2`(update 2 times), # and use second time update data to get cpu percentage of thread in 0.5 second interval # 4. top v3.3, there is 1 black line between 2 update; # but top v3.2, there is 2 blank lines between 2 update! local -a top_cmd_line=(top -H -b -d $top_delay -n 2) local -r top_out=$(HOME="$tmp_store_dir" "${top_cmd_line[@]}") if [ -n "$store_dir" ]; then echo "$top_out" | logAndCat "${top_cmd_line[@]}" > "${store_file_prefix}$(( i + 1 ))_top" fi

echo "$top_out" | awk 'BEGIN { blockIndex = 0; currentLineHasText = 0; prevLineHasText = 0; } { currentLineHasText = ($0 != "") if (prevLineHasText && !currentLineHasText) blockIndex++ # from text line to empty line, increase block index if (blockIndex == 3 && ($NF == "java" || $NF == "jsvc")) # $NF(last field) is command field # only print 4th text block(blockIndex == 3), aka. process info of second top update print $1 " " $9 # $1 is thread id field, $9 is %cpu field prevLineHasText = currentLineHasText # update prevLineHasText }' | sort -k2,2nr}

__complete_pid_user_by_ps() { # ps output field: pid, thread id(lwp), user local -a ps_cmd_line=(ps $ps_process_@R_@R_874_11248@_10288@ct_options -wwLo pid,lwp,user --no-headers) local -r ps_out="$("${ps_cmd_line[@]}")" if [ -n "$store_dir" ]; then echo "$ps_out" | logAndCat "${ps_cmd_line[@]}" > "${store_file_prefix}$(( i + 1 ))_ps" fi

local idx=0 local -a line while IFS=" " read -a line ; do (( idx < count )) || break

local threadId="${line[0]}" local pcpu="${line[1]}"

# output field: pid, threadId, pcpu, user local output_fields="$( echo "$ps_out" | awk -v "threadId=$threadId" -v "pcpu=$pcpu" '$2==threadId { printf "%s %s %s %sn", $1, threadId, pcpu, $3; exit }' )" if [ -n "$output_fields" ]; then (( idx++ )) echo "$output_fields" fi donE}

# output format is same as function findBusyJavaThreadsByPsfindBusyJavaThreadsByTop() { __top_threadId_cpu | __complete_pid_user_by_ps}

printStackOfThreads() { local -a line local idx=0 while IFS=" " read -a line ; do local pid="${line[0]}" local threadId="${line[1]}" local threadId0x="0x`printf %x ${threadID}`" local pcpu="${line[2]}" local user="${line[3]}"

(( idx++ )) local jstackFile="${store_file_prefix}$(( i + 1 ))_jstack_${piD}" [ -f "${jstackFilE}" ] || { local -a jstack_cmd_line=( "$jstack_path" ${forcE} $mix_native_frames $more_lock_info ${piD} ) if [ "${user}" == "${USER}" ]; then # run without sudo, when java process user is current user logAndRun "${jstack_cmd_line[@]}" > ${jstackFilE} elif [ $UID == 0 ]; then # if java process user is not current user, must run jstack with sudo logAndRun sudo -u "${user}" "${jstack_cmd_line[@]}" > ${jstackFilE} else # current user is not root user, so can not run with sudo; print error message and rerun suggestion 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 ${COMMAND_LINE[@]}" 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################################################################################

@H_52_2@main() { local i # if update_count <= 0, infinite loop till user interrupted (eg: CTRL+C) for (( i = 0; update_count <= 0 || i < update_count; ++i )); do (( i > 0 )) && sleep "$update_delay"

[ -n "$append_file" -o -n "$store_dir" ] && headInfo | tee ${append_file:+-a "$append_file"} ${store_dir:+-a "${store_file_prefix}$PROG"} > /dev/null (( update_count != 1 )) && headInfo

if $use_ps; then findBusyJavaThreadsByPs else findBusyJavaThreadsByTop fi | printStackOfThreads donE}

@H_52_2@main

使用方法:

show-busy-java-threads# 从所有运行的Java进程中找出最消耗CPU的线程(缺省5个),打印出其线程栈 # 缺省会自动从所有的Java进程中找出最消耗CPU的线程,这样用更方便# 当然你可以手动指定要分析的Java进程Id,以保证只会显示出那个你关心的那个Java进程的信息show-busy-java-threads -p <指定的Java进程Id> show-busy-java-threads -c <要显示的线程栈数> show-busy-java-threads <重复执行的间隔秒数> [<重复执行的次数>]# 多次执行;这2个参数的使用方式类似vmstat命令 show-busy-java-threads -a <运行输出的记录到的文件># 记录到文件以方便回溯查看 show-duplicate-java-classes -S <存储jstack输出文件的目录># 指定jstack输出文件的存储目录,方便记录以后续分析 ############################### 注意:############################### 如果Java进程的用户 与 执行脚本的当前用户 不同,则jstack不了这个Java进程# 为了能切换到Java进程的用户,需要加sudo来执行,即可以解决:sudo show-busy-java-threads show-busy-java-threads -s <指定jstack命令的全路径># 对于sudo方式的运行,JAVA_HOME环境变量不能传递给root,# 而root用户往往没有配置JAVA_HOME且不方便配置,# 显式指定jstack命令的路径就反而显得更方便了 # -m选项:执行jstack命令时加上-m选项,显示上Native的栈帧,一般应用排查不需要使用show-busy-java-threads -m# -F选项:执行jstack命令时加上 -F 选项(如果直接jstack无响应时,用于强制jstack),一般情况不需要使用show-busy-java-threads -F# -l选项:执行jstack命令时加上 -l 选项,显示上更多相关锁的信息,一般情况不需要使用# 注意:和 -m -F 选项一起使用时,可能会大大增加jstack操作的耗时show-busy-java-threads -l # 帮助信息$ show-busy-java-threads -hUsage: show-busy-java-threads [OPTION]... [delay [count]]Find out the highest cpu consumed threads of java, and print the stack of these threads. Example: show-busy-java-threads # show busy java threads info show-busy-java-threads 1 # update every 1 second, (stop by eg: CTRL+C) show-busy-java-threads 3 10 # update every 3 seconds, update 10 times Output control: -p, --pid <java pid> find out the highest cpu consumed threads from the specified java process, default from all java process. -c, --count <num> set the thread count to show, default is 5. -a, --append-file <file> specifies the file to append output as log. -S, --store-dir <dir> specifies the directory for storing 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 <pid> 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: -d, --top-delay specifies the delay between top samples, default is 0.5 (second). get thread cpu percentage during this delay interval. more info see top -d option. eg: -d 1 (1 second). -P, --use-ps use ps command to find busy thread(cpu usagE) instead of top command, default use top command, because cpu usage of ps command is expressed as the percentage of time spent running during the *entire lifetime* of a process, this is not ideal In general. Miscellaneous: -h, --Help display this Help and exit.

大佬总结

以上是大佬教程为你收集整理的show-busy-java-threads脚本内容及使用全部内容,希望文章能够帮你解决show-busy-java-threads脚本内容及使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。