CPU飙升
线上资源cpu飙升是我们工作中常见的问题,一篇文章搞定排查方法
一、问题复现
- 现在我有两个接口,代码如下
java
@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升高。
二、测试
- 我们将项目打包部署在服务器上,并启动

- 测试接口
curl http://localhost:9901/thing-test/hello
三、排查
- 通过top命令可以查看到有一个java进程占用cpu资源异常
- 获取pid为32306
- 通过命令查询tid
shell
命令:ps -mp 【pid】 -o THREAD,tid,time
实例:ps -mp 32306 -o THREAD,tid,time
- 可以看到引起cpu异常的tid是32327
- 因为现在的tid32327是十进制的,需要将其转化为十六进制
命令:printf "%x\n" 【十进制tid】
实例:printf "%x\n" 32327
- 根据pid 和 tid查询导致cpu飙升的代码
shell
命令:jstack 【10进制pid】 | grep 【16进制tid】 -A 20
实例:jstack 32306 | grep 7e47 -A 20

end....