1.用top命令查看哪一個進程佔用CPU高
gateway網關進程14094佔用CPU高達891%,這個數值是進程內各個線程佔用CPU的累加值。
ssh
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
14094 root 15 0 315m 10m 7308 S 891% 2.2 1:49.01 gateway
20642 root 17 0 17784 4148 2220 S 0.5 0.8 2:39.96 microdasys
1679 root 18 0 10984 1856 1556 R 0.3 0.4 0:22.21 sshd
22563 root 18 0 2424 1060 800 R 0.3 0.2 0:00.03 top
1 root 18 0 2156 492 460 S 0.0 0.1 0:01.59 init ide
2.用top -H -p pid命令查看進程內各個線程佔用的CPU百分比
#top -H -p 14094
top中能夠看到有107個線程,可是下面9個線程佔用CPU很高,下面以線程14086爲主,分析其爲什麼high CPU
函數
PID USER PR NI VIRT RES SHR S %CPU MEM TIME+ COMMAND spa
14086 root 25 0 922m 914m 538m R 101 10.0 21:35.46 gateway 線程
14087 root 25 0 922m 914m 538m R 101 10.0 10:50.22 gateway 調試
14081 root 25 0 922m 914m 538m S 99 10.0 8:57.36 gateway orm
14082 root 25 0 922m 914m 538m R 99 10.0 11:51.92 gateway 進程
14089 root 25 0 922m 914m 538m R 99 10.0 21:21.77 gateway 內存
14092 root 25 0 922m 914m 538m R 99 10.0 19:55.47 gateway ci
14094 root 25 0 922m 914m 538m R 99 10.0 21:02.21 gateway
14083 root 25 0 922m 914m 538m R 97 10.0 21:32.39 gateway
14088 root 25 0 922m 914m 538m R 97 10.0 11:23.12 gateway
3.使用gstack命令查看進程中各線程的函數調用棧
#gstack 14094 > gstack.log
在gstack.log中查找線程ID14086,因爲函數棧會暴露函數細節,所以只顯示了兩個函數楨,線程ID14086對應線程號是37
Thread 37 (Thread 0x4696ab90 (LWP 14086)):
#0 0x40000410 in __kernel_vsyscall ()
#1 0x40241f33 in poll () from /lib/i686/nosegneg/libc.so.6
4.使用gcore命令轉存進程映像及內存上下文
#gcore 14094
該命令生成core文件core.14094
5。用strace命令查看系統調用和花費的時間
#strace -T -r -c -p 14094
-c參數顯示統計信息,去掉此參數能夠查看每一個系統調用話費的時間及返回值。
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------------------
99.99 22.683879 3385 6702 poll
0.00 0.001132 0 6702 gettimeofday
0.00 0.000127 1 208 208 accept
0.00 0.000022 22 1 read
0.00 0.000000 0 1 write
0.00 0.000000 0 1 close
0.00 0.000000 0 14 time
0.00 0.000000 0 2 stat64
0.00 0.000000 0 4 clock_gettime
0.00 0.000000 0 7 send
0.00 0.000000 0 10 10 recvfrom
------ ----------- ----------- --------- --------- ------------------------------
100.00 22.685160 13652 218 total
6.用gdb調試core文件,併線程切換到37號線程
gcore和實際的core dump時產生的core文件幾乎同樣,只是不能用gdb進行某些動態調試
(gdb) gdb gateway core.14094
(gdb) thread 37
[Switching to thread 37 (Thread 0x4696ab90 (LWP 14086))]#0 0x40000410 in __kernel_vsyscall ()
(gdb) where
#0 0x40000410 in __kernel_vsyscall ()
#1 0x40241f33 in poll () from /lib/i686/nosegneg/libc.so.6
能夠根據詳細的函數棧進行gdb調試,打印一些變量值,並結合源代碼分析爲什麼會poll調用佔用很高的CPU。由於代碼涉及到公司產權,顧不在此作詳細分析,須要明白的是分析的流程和使用的命令。流程爲:進程ID->線程ID->線程函數調用棧->函數耗時和調用統計->源代碼分析