Linux下CPU占用率高分析方法

时间:2024-01-11 23:18:02

一般解决方法是通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用
1. 通过top命令找到可疑进程PID

top - 09:37:18 up 70 days, 16:29, 2 users, load average: 1.13, 1.04, 0.97
Tasks: 105 total, 1 running, 104 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.0%us, 4.9%sy, 0.0%ni, 93.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.5%st
Mem: 2067816k total, 1756680k used, 311136k free, 236340k buffers
Swap: 524284k total, 255508k used, 268776k free, 277040k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24138 apache 20 0 1273m 384m 3668 S 103.3 19.0 1232:39 java
3359 root 39 19 2704 36 0 S 0.3 0.0 4:39.34 gam_server
6696 root 20 0 34148 1604 244 S 0.3 0.1 5:06.63 httpd
19254 root 20 0 785m 221m 3176 S 0.3 11.0 9:04.36 java
1 root 20 0 2224 28 0 S 0.0 0.0 1:22.46 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:33.42 ksoftirqd/0
5 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kworker/u:0

从上面命令中可以看出java进程CPU利用率一直保持100%,稳居不下,找到PID 24138

2. 找出消耗资源最高的线程

top -H -p 24138 可以不用第一步,直接执行命令 top -H ,就可以查看到消耗资源最高的线程

top - 09:49:49 up 70 days, 16:41, 2 users, load average: 1.01, 1.04, 1.00
Tasks: 72 total, 1 running, 71 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.6%us, 1.3%sy, 0.0%ni, 97.7%id, 0.1%wa, 0.0%hi, 0.0%si, 0.2%st
Mem: 2067816k total, 1760840k used, 306976k free, 236744k buffers
Swap: 524284k total, 253344k used, 270940k free, 279092k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24167 apache 20 0 1273m 384m 3688 R 99.1 19.0 1169:43 java
24152 apache 20 0 1273m 384m 3688 S 2.0 19.0 0:28.58 java
24188 apache 20 0 1273m 384m 3688 S 2.0 19.0 4:56.69 java
24138 apache 20 0 1273m 384m 3688 S 0.0 19.0 0:00.00 java

3. 查看这个线程所有系统调用

strace -p 24167