java cpu 分析工具_java性能分析 - CPU飙高分析工具

news/2024/10/17 22:20:00/

背景

有处理过生产问题的同学基本都能遇到系统忽然缓慢,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 <

Usage: ${PROG} [OPTION]

Find out the highest cpu consumed threads of java processes,

and print the stack of these threads.

Example:

${PROG} -p -c 5 # show top 5 busy java threads info

Output control:

-p, --pid find out the highest cpu consumed threads from

the specified java process.

default from all java process.

-c, --count 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


http://www.ppmy.cn/news/429068.html

相关文章

跑分cpu_【新机】天玑800+跑分性能出炉:CPU干翻骁龙765G,比肩麒麟985 | 骁龙768G性能曝光...

近日一款型号为M2004J7BC的小米新机出现在了GeekBench跑分库中&#xff0c;应该就是即将亮相发布的redmi Note 9系列。新机搭载了天玑820&#xff0c;型号为MT6875(天玑800为MT6873)。CPU由4颗2.6GHz的A76大核4颗2.0GHz的A55小核构成&#xff0c;GPU为Mali-G57。 Geekbench5 天…

跑分cpu_跑分完爆骁龙 865?明年这些中端处理器真的要起飞

2013 年&#xff0c;骁龙 800 处理器手机的安兔兔跑分&#xff0c;是 3.3 万分。 2020 年&#xff0c;骁龙 865 处理器手机跑分是 —— 67 万分。(这个分数要记住&#xff0c;重点) 七年时间&#xff0c;同一系列在同一平台跑出了将近 20 倍的分数差距。 这&#xff0c;也就是手…

不同操作系统进行跑分测试【win、linux】

不同操作系统进行跑分测试【win、linux】 跑分通常是指&#xff0c;我们通过一些专业的软件来综合检测一个计算机的总体性能的方法 1 Windows 1.1 操作步骤 从网上下载一些专业的软件【如&#xff1a;鲁大师、everest等】如果是win10的话&#xff0c;可以使用微软自带的跑分工…

linux 性能测试 跑分,测试10秒钟,分析8小时,性能评估只看跑分可不够

写在前面:大约4年前在中科院软件所,为评估国产兆芯CPU性能,我接触到一些benchmark,涉及CPU指标的就有ubench、sysbench、c-ray、SPEC和unixbench等。 工具在手,不要动脑,跑起来就行。后来情况变了,每个月都有数个系统性能调优的kpi压在身上,你不光要知道这些性能怎么测…

[C语言]CPU跑分程序

|版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 Code: 1 #include <stdio.h>2 #include <conio.h>3 #include <time.h>//clock()所属头文件4 const int N_qsort10000;//快排的数据规模5 const int M20000,N50000;//整点、浮点运…

用Python实现一个CPU跑分程序

一般我们要对cpu进行跑分测试&#xff0c;在不同的系统有时测试软件不能统一&#xff0c;比如Linux下就没有CineBench&#xff0c;导致跑分成绩对比不方便&#xff0c;所以想着自己写一个简单的跑分程序。 代码用Python实现&#xff0c;可以分别进行单核和多核测试&#xff0c…

python获取某乎热搜数据并保存成Excel

python获取知乎热搜数据 一、获取目标、准备工作二、开始编码三、总结 一、获取目标、准备工作 1、获取目标&#xff1a; 本次获取教程目标&#xff1a;某乎热搜 2、准备工作 环境python3.xrequestspandas requests跟pandas为本次教程所需的库&#xff0c;requests用于模拟h…

模糊测试不“模糊”,高效发掘未知漏洞与 0day 攻击

近日&#xff0c;在「DevSecOps软件安全开发实践」课程上&#xff0c;极狐(GitLab) 高级测试工程师衡韬、极狐(GitLab) 高级后端开发工程师田鲁&#xff0c;分享了模糊测试的概念、必要性和在极狐GitLab 上的实践。 以下内容整理自本次直播&#xff0c;你也可以点击&#x1f44…