在开发和运维系统过程中,难免会碰到CPU负载异常高的问题,第一印象是边界情况没处好,导致死循环。如何快速定位占用CPU的线程,甚至具体的代码块,对于解决线上问题非常关键。结合Linux自带的top命令和Java jstack可以轻松解决。
进程负载
Linux top 命令会展示系统当前的整体负载(load average)情况以及进程所占用的资源(PID、CPU、Memory等),如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| top - 21:23:19 up 588 days, 3:17, 1 user, load average: 3.28, 3.84, 4.55 Tasks: 417 total, 2 running, 415 sleeping, 0 stopped, 0 zombie %Cpu(s): 8.4 us, 3.6 sy, 0.0 ni, 87.7 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 13141572+total, 4651624 free, 83008648 used, 43755452 buff/cache KiB Swap: 0 total, 0 free, 0 used. 43701956 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 9161 admin 20 0 0.105t 0.065t 15768 S 1709.2 53.4 109998:58 java 17551 root 20 0 3690796 32624 2548 S 18.8 0.0 309:22.70 lsecd 26748 root 20 0 146408 2156 1376 R 6.2 0.0 0:00.01 top 1 root 20 0 1403740 1.162g 1852 S 0.0 0.9 185:42.10 systemd 2 root 20 0 0 0 0 S 0.0 0.0 5:18.52 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 99:12.84 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+ 8 root rt 0 0 0 0 S 0.0 0.0 42:23.68 migration/0 9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0 11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/1 12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/2
|