Java CPU性能分析工具代码实例
作者:cjunn 发布时间:2023-09-28 04:52:54
标签:Java,CPU,性能,分析
这篇文章主要介绍了Java CPU性能分析工具代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
背景
有处理过生产问题的同学基本都能遇到系统忽然缓慢,CPU突然飙升,甚至整个应用请求不可用。当出现这种情况下,在不影响数据准确性的前提下,我们应该尽快导出jstack和内存信息,然后重启系统,尽快回复系统的可用性,避免用户体验过差。本文针对CPU飙升问题,提供该问题的排查思路,从而能够快速定位到某线程甚至某快代码导致CPU飙升,从而提供处理该问题的思路。
排查过程
通过top命令查看cpu飙升的java进程pid
通过ps -mp [pid] -o THREAD,tid,time查看该进程下所拥有的线程及各个线程占用cpu的使用率,并且记录CPU使用率过高的线程ID号
将线程ID号转换为16进程的数值记为tid_hex
使用jdk自带jstack监控命令
使用命令jstack [pid] | grep tid_hex -A100命令输出该线程的堆栈信息
根据堆栈信息分析代码。
通过以上步骤可以查找出导致cpu飙升的相关代码位置,然后对代码进行code review即可。
工具封装
以上步骤已经封装为脚本文件,通过以下脚本文件只需要指定进程ID即pid即可导出默认前5条导致CPU率过高的堆栈信息。
已上传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;
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.
-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
;;
-h|--help)
usage
exit 0;
;;
--)
shift
break
;;
*)
shift
if [ -z "$1" ] ; then
break
fi
;;
esac
done
fi
if [ ! -n "$pid" ] ;then
echo "error: -p is empty"
exit 1;
fi
function worker(){
#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.
local whilec=0;
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
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 "";
whilec=$[$whilec+1];
done
if [ $whilec -eq 0 ] ; then
echo "error : thread not found, make sure pid exists.";
fi
}
worker
来源:https://www.cnblogs.com/cjunn/p/12185276.html


猜你喜欢
- 目标:查询数据库中的字段,然后转换成 JSON 格式的数据,返回前台。环境:idea 2016.3.4, jdk 1.8, mysql 5.
- 缘起标准的RABC, 权限需要支持动态配置,spring security默认是在代码里约定好权限,真实的业务场景通常需要可以支持动态配置角
- 迭代器是一种模式,它可以使得对于序列类型的数据结构的遍历行为与被遍历的对象分离,即我们无需关心该序列的底层结构是什么样子的。只要拿到这个对象
- 1. 背景从JDK1.5开始,Java支持个数可变的形参,类似:public class ParamDemo { public static
- 前言《布谷鸟闯关-简单版》是一个基于java的布谷鸟闯关游戏,摁上键控制鸟的位置穿过管道间的缝隙,需要做碰撞检测,监听键盘事件,背景图片的切
- 1. strlen —— 求字符串长度1.1 strlen 的声明与用处strlen ,我们有一些英
- springboot环境切换失效概述最近在使用-Dspring.profiles.active=te 来切换spring-boot的环境时,
- 第1部分 List概括List的框架图List 是一个接口,它继承于Collection的接口。它代表着有序的队列。AbstractList
- Java * 。具体有如下四步骤:通过实现 InvocationHandler 接口创建自己的调用处理器;通过为 Proxy 类指定 C
- 先看看效果图:package com.fenghuo.struts.download;import java.net.URLEncoder;
- 为了实现不同环境构建的不同需求,这里使用到了 profile。因为 profile 能够在构建时修改 pom 的一个子集,或者添加额外的配置
- 一. 多任务和Task、启动模式Android 手机在早期,下方通常会内置三个实体的触摸按键,分别是:桌面、菜单、返回。大概在Android
- 这篇文章主要介绍了如何通过Java实现时间轴过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 方案一.使用国内的镜像阿里仓库等首先通过maven的路径找到setting.xml的文件然后在其中修改mirror和profile保存一下就
- 目录第一点——加载网页结构第二点——解析网页结构,转变为符合需求的数据实体第三点——保存数据实体,转变为符合需求的数据实体第四点——main
- 在页面提交到tomcat乱码 解决方法是在tomcat/conf/server.xml中进行配置以tomcat6.0.32为例,需将以下代码
- MybatisMyBatis ,是国内最火的持久层框架采用了ORM思想解决了实体类和数据库表映射的问题。对JDBC进行了封装,屏蔽了JDBC
- List 的方法列表方法名功能说明ArrayList()构造方法,用于创建一个空的数组列表add(E e)将指定的元素添加到此列表的尾部ge
- 这篇文章主要介绍了SpringBoot如何读取war包jar包和Resource资源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具
- 导航和路由Flutter提供了一个完整的用于在屏幕之间导航和处理深层链接的系统。没有复杂深度链接的小型应用程序可以使用Navigator,而