Java CPU性能分析工具代码实例

 更新时间:2020年01月15日 10:36:30   作者:cjunn  
这篇文章主要介绍了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • nacos注册中心单节点ap架构源码解析(最新推荐)

    nacos注册中心单节点ap架构源码解析(最新推荐)

    这篇文章主要介绍了nacos注册中心单节点ap架构源码解析,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • 详解SpringBoot Redis自适应配置(Cluster Standalone Sentinel)

    详解SpringBoot Redis自适应配置(Cluster Standalone Sentinel)

    这篇文章主要介绍了详解SpringBoot Redis自适应配置(Cluster Standalone Sentinel),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java基础-Java的体系结构

    Java基础-Java的体系结构

    这篇文章主要介绍了Java的体系结构,Java几乎成为了“开源”的代名词。第三方开源软件和框架。如Tomcat、Struts,MyBatis,Spring等,下面我们来看看文章具体的内容介绍吧
    2022-01-01
  • IDEA 快速返回上次查看代码的位置的方法

    IDEA 快速返回上次查看代码的位置的方法

    这篇文章主要介绍了IDEA 快速返回上次查看代码的位置的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • SpringBoot 整合mybatis+mybatis-plus的详细步骤

    SpringBoot 整合mybatis+mybatis-plus的详细步骤

    这篇文章主要介绍了SpringBoot 整合mybatis+mybatis-plus的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • java实现ThreadLocal线程局部变量的实现

    java实现ThreadLocal线程局部变量的实现

    本文主要介绍了java实现ThreadLocal线程局部变量的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • MyBatis批量插入/修改/删除MySql数据

    MyBatis批量插入/修改/删除MySql数据

    这篇文章主要给大家介绍了关于MyBatis批量插入/修改/删除MySql数据的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Java8如何基于flatMap处理异常函数

    Java8如何基于flatMap处理异常函数

    这篇文章主要介绍了Java8如何基于flatMap处理异常函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Springboot整合knife4j与shiro的操作

    Springboot整合knife4j与shiro的操作

    这篇文章主要介绍了Springboot整合knife4j与shiro的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java老手该当心的13个错误

    Java老手该当心的13个错误

    这篇文章主要介绍了Java老手该当心的13个错误,需要的朋友可以参考下
    2015-04-04

最新评论