JAVA 六月 24, 2023

【转】CPU飙升排查

文章字数 2.1k 阅读约需 2 mins. 阅读次数

原文地址:https://softleadergy.github.io/CPU%E9%A3%99%E5%8D%87%E6%8E%92%E6%9F%A5/

CPU飙升

线上资源cpu飙升是我们工作中常见的问题,一篇文章搞定排查方法

一、问题复现

现在我有两个接口,代码如下

@RestController
public class CPUCheck {
  @RequestMapping("/hello")
  public String helloWorld(){
      return "hello World";
  }

  @RequestMapping("/run")
  public void run(){
      while (true){

      }
  }
}

代码很简单 接口1“/hello” 返回“hello World”,接口2“/run” 进入死循环,这样就保证了访问接口2cpu升高。

二、测试

  1. 我们将项目打包部署在服务器上,并启动
    jar
  2. 测试接口
     curl http://localhost:9901/thing-test/hello
    pid

三、排查

  1. 通过top命令可以查看到有一个java进程占用cpu资源异常
  2. 获取pid为32306
  3. 通过命令查询tid
     命令:ps -mp 【pid】 -o THREAD,tid,time
     实例:ps -mp 32306 -o THREAD,tid,time
    tid
  4. 可以看到引起cpu异常的tid是32327
  5. 因为现在的tid32327是十进制的,需要将其转化为十六进制
     命令:printf "%x\n" 【十进制tid】
     实例:printf "%x\n" 32327
    nid
  6. 根据pid 和 tid查询导致cpu飙升的代码
     命令:jstack 【10进制pid】 | grep 【16进制tid】 -A 20
     实例:jstack 32306 | grep 7e47 -A 20
    jstack

src


end….

0%