线上资源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升高。
我们将项目打包部署在服务器上,并启动
测试接口
curl http://localhost:9901/thing-test/hello
pid
通过top
命令可以查看到有一个java进程占用cpu资源异常
获取pid为32306
通过命令查询tid
命令:ps -mp 【pid】 -o THREAD,tid,time
实例:ps -mp 32306 -o THREAD,tid,time
tid
可以看到引起cpu异常的tid是32327
因为现在的tid32327
是十进制的,需要将其转化为十六进制
命令:printf "%x\n" 【十进制tid】
实例:printf "%x\n" 32327
nid
根据pid 和 tid查询导致cpu飙升的代码
命令:jstack 【10进制pid】 | grep 【16进制tid】 -A 20
实例:jstack 32306 | grep 7e47 -A 20
jstack
src
end....