每次提起性能测试,都是开始于脚本、压力,然后各种监控,最后的最后就是分析。每次到了最后一个环境,所有大牛都说是一个很复杂的过程一两句话说不清楚。让人陷入了无尽的遐想~~~~~。
今天,测试就告诉你如何找到Java消耗CPU的进程是怎么被找到的。
传统的手段
当遇见CPU性能飙升到接近100的时候,首先需要进入对应的服务器,然后通过如下一连串的动作找到最耗资源的罪魁祸首。
top发现最好是的进程
在shell界面中,输入top,找到最消耗CPU的那个Java进程。记录下PID号码。
然后通过:
-
top -H -p pid
命令查看对应PID进程下的线程,找到最耗资源的线程的PID。将其通过shell转成十六进制:
-
printf ”%x\n“ pid -
-
%x,表示 十六进制,\n是换行
应用jstack针对上面耗时的线程进行分析
-
jstack PID(十六进制) 1> xxx.tmp
然后打开xxx.tmp文件,搜索PID(十六进制)号码,下面的堆栈就是这个线程打出来的。排查问题就可以从这些堆栈信息开整找到问题。
一个一劳永逸的工具
show-busy-java-threads用于快速排查Java的CPU性能问题(top us值过高),自动查出运行的Java进程中消耗CPU多的线程,并打印出其线程栈,从而确定导致性能问题的方法调用。
GitHub地址:https://github.com/oldratlee/useful-scripts
在服务器端,克隆项目: git clone git://github.com/oldratlee/useful-scripts.git
-
cd useful-scripts
然后就可以看到了show-busy-jaa-threads工具了。其中,该Github仓库有一个release发布分支,里面包含的都是功能稳定的脚本,建议使用的时候切换到该分支。方法如下:
-
# 使用Release分支的内容 -
git checkout release -
-
# 更新脚本 -
git pull
进入userful-scripts目录后,输入 ./show-busy-java-threads就可以使用这个工具进行上述消耗CPU的优化分析了。其他show-busy-java-threads工具的用法如下:
-
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 -h -
Usage: 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. -
-
Miscellaneous: -
-h, --help display this help and exit.
其实优化是性能测试的根本目标,找到问题后分析问题才是关键,通过各种手段找到更详细的问题,找到更多的数据和RD一起分析。
——————————————————————————————————————————————————
给大家推荐一个学习资料分享群(574253227),里面大牛已经为我们整理好了许多的学习资料,有自动化,接口,性能等等的学习资料!
9285

被折叠的 条评论
为什么被折叠?



