用于快速排查Java的CPU性能问题
2017-08-17 20:37
525 查看
show-busy-java-threads.sh
用于快速排查Java的
CPU性能问题(
top us值过高),自动查出运行的
Java进程中消耗
CPU多的线程,并打印出其线程栈,从而确定导致性能问题的方法调用。
PS,如何操作可以参见@bluedavy的《分布式Java应用》的【5.1.1 cpu消耗分析】一节,说得很详细:
top命令找出有问题
Java进程及线程
id:
开启线程显示模式
按
CPU使用率排序
记下
Java进程
id及其
CPU高的线程
id
用进程
id作为参数,
jstack有问题的
Java进程
手动转换线程
id成十六进制(可以用
printf %x 1234)
查找十六进制的线程
id(可以用
grep)
查看对应的线程栈
查问题时,会要多次这样操作以确定问题,上面过程太繁琐太慢了。
用法
show-busy-java-threads.sh # 从 所有的 Java进程中找出最消耗CPU的线程(缺省5个),打印出其线程栈。 show-busy-java-threads.sh -c <要显示的线程栈数> show-busy-java-threads.sh -c <要显示的线程栈数> -p <指定的Java Process> ############################## # 注意: ############################## # 如果Java进程的用户 与 执行脚本的当前用户 不同,则jstack不了这个Java进程。 # 为了能切换到Java进程的用户,需要加sudo来执行,即可以解决: sudo show-busy-java-threads.sh
示例
$ show-busy-java-threads.sh [1] Busy(57.0%) thread(23355/0x5b3b) stack of java process(23269) under user(admin): "pool-1-thread-1" prio=10 tid=0x000000005b5c5000 nid=0x5b3b runnable [0x000000004062c000] java.lang.Thread.State: RUNNABLE at java.text.DateFormat.format(DateFormat.java:316) at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41) at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:127) at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) [2] Busy(26.1%) thread(24018/0x5dd2) stack of java process(23269) under user(admin): "pool-1-thread-2" prio=10 tid=0x000000005a968800 nid=0x5dd2 runnable [0x00000000420e9000] java.lang.Thread.State: RUNNABLE at java.util.Arrays.copyOf(Arrays.java:2882) at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:572) at java.lang.StringBuffer.append(StringBuffer.java:320) - locked <0x00000007908d0030> (a java.lang.StringBuffer) at java.text.SimpleDateFormat.format(SimpleDateFormat.java:890) at java.text.SimpleDateFormat.format(SimpleDateFormat.java:869) at java.text.DateFormat.format(DateFormat.java:316) at com.xxx.foo.services.common.DateFormatUtil.format(DateFormatUtil.java:41) at com.xxx.foo.shared.monitor.schedule.AppMonitorDataAvgScheduler.run(AppMonitorDataAvgScheduler.java:126) at com.xxx.foo.services.common.utils.AliTimer$2.run(AliTimer.java:128) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) ...
上面的线程栈可以看出,
CPU消耗最高的2个线程都在执行
java.text.DateFormat.format,业务代码对应的方法是
shared.monitor.schedule.AppMonitorDataAvgScheduler.run。可以基本确定:
AppMonitorDataAvgScheduler.run调用
DateFormat.format次数比较频繁。
DateFormat.format比较慢。(这个可以由
DateFormat.format的实现确定。)
多个执行几次
show-busy-java-threads.sh,如果上面情况高概率出现,则可以确定上面的判定。
# 因为调用越少代码执行越快,则出现在线程栈的概率就越低。
分析
shared.monitor.schedule.AppMonitorDataAvgScheduler.run实现逻辑和调用方式,以优化实现解决问题。
贡献者
silentforce改进此脚本,增加对环境变量JAVA_HOME的判断。
#15
liuyangc3
优化性能,通过
read -a简化反复的
awk操作
#51
发现并解决
jstack非当前用户
Java进程的问题
#50
show-busy-java-threads.sh
#!/bin/bash# @Function
# Find out the highest cpu consumed threads of java, and print the stack of these threads.
#
# @Usage
# $ ./show-busy-java-threads.sh
#
# @author Jerry Lee
readonly PROG=`basename $0`
readonly -a COMMAND_LINE=("$0" "$@")
usage() {
cat <<EOF
Usage: ${PROG} [OPTION]...
Find out the highest cpu consumed threads of java, and print the stack of these threads.
Example: ${PROG} -c 10
Options:
-p, --pid find out the highest cpu consumed threads from the specifed java process,
default from all java process.
-c, --count set the thread count to show, default is 5
-h, --help display this help and exit
EOF
exit $1
}
readonly ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
[ $? -ne 0 ] && usage 1
eval set -- "${ARGS}"
while true; do
case "$1" in
-c|--count)
count="$2"
shift 2
;;
-p|--pid)
pid="$2"
shift 2
;;
-h|--help)
usage
;;
--)
shift
break
;;
esac
done
count=${count:-5}
redEcho() {
[ -c /dev/stdout ] && {
# if stdout is console, turn on color output.
echo -ne "\033[1;31m"
echo -n "$@"
echo -e "\033[0m"
} || echo "$@"
}
yellowEcho() {
[ -c /dev/stdout ] && {
# if stdout is console, turn on color output.
echo -ne "\033[1;33m"
echo -n "$@"
echo -e "\033[0m"
} || echo "$@"
}
blueEcho() {
[ -c /dev/stdout ] && {
# if stdout is console, turn on color output.
echo -ne "\033[1;36m"
echo -n "$@"
echo -e "\033[0m"
} || echo "$@"
}
# Check the existence of jstack command!
if ! which jstack &> /dev/null; then
[ -z "$JAVA_HOME" ] && {
redEcho "Error: jstack not found on PATH!"
exit 1
}
! [ -f "$JAVA_HOME/bin/jstack" ] && {
redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack file does NOT exists!"
exit 1
}
! [ -x "$JAVA_HOME/bin/jstack" ] && {
redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack is NOT executalbe!"
exit 1
}
export PATH="$JAVA_HOME/bin:$PATH"
fi
readonly uuid=`date +%s`_${RANDOM}_$$
cleanupWhenExit() {
rm /tmp/${uuid}_* &> /dev/null
}
trap "cleanupWhenExit" EXIT
printStackOfThreads() {
local line
local count=1
while IFS=" " read -a line ; do
local pid=${line[0]}
local threadId=${line[1]}
local threadId0x="0x`printf %x ${threadId}`"
local user=${line[2]}
local pcpu=${line[4]}
local jstackFile=/tmp/${uuid}_${pid}
[ ! -f "${jstackFile}" ] && {
{
if [ "${user}" == "${USER}" ]; then
jstack ${pid} > ${jstackFile}
else
if [ $UID == 0 ]; then
sudo -u ${user} jstack ${pid} > ${jstackFile}
else
redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
redEcho "User of java process($user) is not current user($USER), need sudo to run again:"
yellowEcho " sudo ${COMMAND_LINE[@]}"
echo
continue
fi
fi
} || {
redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user})."
echo
rm ${jstackFile}
continue
}
}
blueEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/${threadId0x}) stack of java process(${pid}) under user(${user}):"
sed "/nid=${threadId0x} /,/^$/p" -n ${jstackFile}
done
}
ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
[ -z "${pid}" ] &&
awk '$4=="java"{print $0}' ||
awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}'
} | sort -k5 -r -n | head --lines "${count}" | printStackOfThreads
参考地址:https://github.com/superhj1987/awesome-scripts
相关文章推荐
- java cpu 过高问题排查
- 记一次java程序CPU占用过高问题排查
- (一)JAVA CPU占用过高问题排查(linux)
- 1、简单排查java应用CPU飙高的线程问题
- JAVA CPU占用过高问题排查(linux)
- java进程CPU占用高的问题排查
- java 性能问题排查
- java实战CPU占用过高问题的排查及解决
- java 性能问题 排查
- java 一次CPU占用过高问题的排查及解决
- java应用线上CPU过高问题排查
- java cpu高达100%问题 排查
- java问题排查工具之一板斧jstack——使用 jstack 定位 java进程CPU过高的问题
- java高cpu占用和高内存占用问题排查 (转)
- java 高cpu问题排查
- 线上java程序CPU占用过高问题排查
- Java服务CPU飙到99%问题排查
- java进程CPU过高问题如何排查?
- 如何用jconsole来跟踪Java系统性能问题
- 标题: 优化 Java 垃圾收集的性能——如何利用 IBM Java 虚拟机检测和解决垃圾收集问题