背景

        有处理过生产问题的同学基本都能遇到系统忽然缓慢,CPU突然飙升,甚至整个应用请求不可用。当出现这种情况下,在不影响数据准确性的前提下,我们应该尽快导出jstack和内存信息,然后重启系统,尽快回复系统的可用性,避免用户体验过差。本文针对CPU飙升问题,提供该问题的排查思路,从而能够快速定位到某线程甚至某快代码导致CPU飙升,从而提供处理该问题的思路。

排查过程

  1. 通过top命令查看cpu飙升的java进程pid
  2. 通过ps -mp [pid] -o THREAD,tid,time查看该进程下所拥有的线程及各个线程占用cpu的使用率,并且记录CPU使用率过高的线程ID号
  3. 将线程ID号转换为16进程的数值记为tid_hex
  4. 使用jdk自带jstack监控命令
  5. 使用命令jstack [pid] | grep tid_hex -A100命令输出该线程的堆栈信息
  6. 根据堆栈信息分析代码。

通过以上步骤可以查找出导致cpu飙升的相关代码位置,然后对代码进行code review即可。

工具封装

  1. 以上步骤已经封装为脚本文件,通过以下脚本文件只需要指定进程ID即pid即可导出默认前5条导致CPU率过高的堆栈信息。
  2. 已上传github : 点我进入
./java-thread-top.sh -p pid
#!/bin/bash
# @Function
# Find out the highest cpu consumed threads of java processes, and print the stack of these threads.
# @github https://github.com/cjunn/script_tool/
# @author cjunn
# @date Sun Jan 12 2020 21:08:58 GMT+0800
# pid='';
count=5;
mode=0;
function usage(){
readonly PROG="`basename $0`"
cat <<EOF
Usage: ${PROG} [OPTION]
Find out the highest cpu consumed threads of java processes,
and print the stack of these threads.
Example:
${PROG} -p <pid> -c 5 # show top 5 busy java threads info
Output control:
-p, --pid <java pid> find out the highest cpu consumed threads from
the specified java process.
default from all java process.
-t, --Top Using top command to get CPU utilization
-c, --count <num> set the thread count to show, default is 5.
Miscellaneous:
-h, --help display this help and exit.
EOF
} #1.Collect script parameters
#2.Check whether PID exists
if [ $# -gt 0 ];
then
while true; do
case "$1" in
-c|--count)
count="$2"
shift 2
;;
-p|--pid)
pid="$2"
shift 2
;;
-t|--top)
mode=1
shift 1
;;
-h|--help)
usage
exit 0;
;;
--)
shift 1;
break
;;
*)
shift 1;
if [ -z "$1" ] ; then
break
fi
;;
esac
done
fi
if [ ! -n "$pid" ] ;then
echo "error: -p is empty"
exit 1;
fi if [ `jps |grep $pid |wc -l` -ne 1 ];then
echo "error: -p is wrong"
exit 1;
fi function workerByJstack(){
local tid_hex=$(printf "%x" $tid);
echo "====================== tid:${tid} tid_hex:${tid_hex} cpu:${cpu} time:${time} ======================";
jstack $pid | awk 'BEGIN {RS = "\n\n+";ORS = "\n\n"} /'${tid_hex}'/ {print $0}';
echo "";
} function workerByTop(){
top -Hp $pid -n1 | sed '1,7d'| sed '$d' |sed '$d' | sort -k 10 -n -r | sed $[$count+1]',$d' | awk '{print $10,$2,$12}' | while read cpu tid time
do
workerByJstack $pid $tid $cpu $time
done
} function workerByPs(){
#1.Query all threads according to PID.
#2.Delete header and first line information.
#3.According to the second column of CPU to sort, reverse display.
#4.Delete the count + 1 to last column based on the count value.
#5.Get CPU utilization, TID value, thread used time, and assign them to CPU, TID, time respectively.
#6.Perform hex conversion on TID.
#7.Use JDK to monitor all threads of jstack output PID.
#8.Use awk to regularly query the thread information of tid_hex required.
#9.Display the stack information of count before thread busy.
ps -mp $pid -o THREAD,tid,time | sed '1,2d' | sort -k 2 -n -r |sed $[$count+1]',$d' | awk '{print $2,$8,$9}' | while read cpu tid time
do
workerByJstack $pid $tid $cpu $time
done
} function worker(){
echo "start-mode:$mode"
if [ $mode -eq 0 ];then
workerByPs
elif [ $mode -eq 1 ];then
workerByTop
fi
}
worker

java性能分析 - CPU飙高分析工具的更多相关文章

  1. JVM进程cpu飙高分析

    在项目快速迭代中版本发布频繁  近期上线报错一个JVM导致服务器cpu飙高 但内存充足的原因现象.  对于耗内存的JVM程序来而言,  基本可以断定是线程僵死(死锁.死循环等)问题. 这里是纪录一下排 ...

  2. 几个常用的内存、CPU飙高 分析工具

    Process Hacker.Windbg.vs2017 调试托管内存.Microsoft.Samples.Debugging.ants memory profiler.ants performanc ...

  3. Windows服务器java.exe占用CPU过高问题分析及解决

    最近在测试一个用java语言实现的数据采集接口时发现,接口一旦运行起来,CPU利用率瞬间飙升到85%-95%,一旦停止就恢复到40%以下,这让我不得不面对以前从未关注过的程序性能问题. 在硬着头皮查找 ...

  4. java进程CPU飙高

    因为这段时间一直在弄监控,但是工作还是在进行中 因为机器不多,所以今天早上巡检了一下,看到一台生产机器上的CPU飙高 top

  5. STORM在线业务实践-集群空闲CPU飙高问题排查

    源:http://daiwa.ninja/index.php/2015/07/18/storm-cpu-overload/ 2015-07-18AUTHORDAIWA STORM在线业务实践-集群空闲 ...

  6. 现网CPU飙高,Full GC告警

    现网CPU飙高,Full GC告警 https://www.cnblogs.com/QG-whz/p/9647614.html 问题出现:现网CPU飙高,Full GC告警 CGI 服务发布到现网后, ...

  7. 一次FGC导致CPU飙高的排查过程

    今天测试团队反馈说,服务A的响应很慢,我在想,测试环境也会慢?于是我自己用postman请求了一下接口,真的很慢,竟然要2s左右,正常就50ms左右的. 于是去测试服务器看了一下,发现服务器负载很高, ...

  8. 你要偷偷学会排查线上CPU飙高的问题,然后惊艳所有人!

    GitHub 20k Star 的Java工程师成神之路,不来了解一下吗! GitHub 20k Star 的Java工程师成神之路,真的不来了解一下吗! GitHub 20k Star 的Java工 ...

  9. 生产系统CPU飙高问题排查

    现状 生产系统CPU占用过高,并且进行了报警 排查方法 执行top命令,查看是那个进程导致的,可以确定是pid为22168的java应用导致的 执行top -Hp命令,查看这个进程的那个线程导致cpu ...

随机推荐

  1. centos7.5优化系统脚本(虚拟机下)

    #!/usr/bin/bash #安装常用软件,首先必须自行调整好网卡配置文件,保证可以上网,否则,下列优化会失败 yum -y install wget vim lrzsz bash-complet ...

  2. make: *** No targets specified and no makefile found. Stop.错误

    # make make: *** No targets specified and no makefile found. Stop. # yum install gcc gcc-c++ gcc-g77 ...

  3. margin合并及解决办法

    外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距. 合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者 水平方向不会发生合并 只有普通文档流中块框的垂直外边距才会发生外边距合并 ...

  4. git 指令笔记

    狂躁,太狂躁!!赶上过年,赶上自己的懒癌,12月底就学完的教程直到今天才整理笔记,中途沉默在游戏中..... 只给出Windows下git指令操作,推荐大家去廖雪峰前辈那里学习(百度搜索:廖雪峰的官方 ...

  5. Leetcode:110. 平衡二叉树

    Leetcode:110. 平衡二叉树 Leetcode:110. 平衡二叉树 点链接就能看到原题啦~ 关于AVL的判断函数写法,请跳转:平衡二叉树的判断 废话不说直接上代码吧~主要的解析的都在上面的 ...

  6. hadoop完全分布式部署

    1.我们先看看一台节点的hdfs的信息:(已经安装了hadoop的虚拟机:安装hadoophttps://www.cnblogs.com/lyx666/p/12335360.html) start-d ...

  7. 邓Laravel2020-01-28

    ORM .一对一 hasOne $this->hasOne('外键的模型名称','外键的id','自己表里对应的id')// demo一对一 用户表里又area_id与地区表id一致 用户表和地 ...

  8. phpcms v9编辑器上传图片是否添加水印

    第一步:给图片上传对话框里面添加是否添加水印的多选框,找到: satics/js/ckeditor/ckeditor.js 第17554行 (需要格式化,我用的NetBeans)修改为 functio ...

  9. 在C#下使用TensorFlow.NET训练自己的数据集

    在C#下使用TensorFlow.NET训练自己的数据集 今天,我结合代码来详细介绍如何使用 SciSharp STACK 的 TensorFlow.NET 来训练CNN模型,该模型主要实现 图像的分 ...

  10. 在Idea中jdk11和jdk8环境变量的切换

    先配置好jdk11和jdk8环境变量 idea(2019.3)中jdk版本切换(jdk8和jdk11) 快捷键ctrl + shift + alt +s ,将jdk11修改为jdk1.8 完成后,显示 ...