线上资源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
测试接口2,并查看cpu情况
curl http://localhost:9901/thing-test/run
三、排查
- 通过
top
命令可以查看到有一个java进程占用cpu资源异常 - 获取pid为
32306
通过命令查询
tid
命令:ps -mp 【pid】 -o THREAD,tid,time 实例:ps -mp 32306 -o THREAD,tid,time
- 可以看到引起cpu异常的tid是
32327
因为现在的tid
32327
是十进制的,需要将其转化为十六进制命令:printf "%x\n" 【十进制tid】 实例:printf "%x\n" 32327
根据pid 和 tid查询导致cpu飙升的代码
命令:jstack 【10进制pid】 | grep 【16进制tid】 -A 20 实例:jstack 32306 | grep 7e47 -A 20
END.....
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。