1. 為什么系統(tǒng)負(fù)載很高,CPU使用率不高?
1)大量的中斷
可能是在不斷磁盤讀寫,網(wǎng)絡(luò)通訊, 也可能是模塊使用不當(dāng)或者硬件上出問題導(dǎo)致外設(shè)不斷給CPU送中斷;
2)系統(tǒng)負(fù)載高(load/loadavg)
負(fù)載高表示有很多程序等待調(diào)度運(yùn)行,它會(huì)導(dǎo)致上下文切換頻繁。
3) 上下文切換過于頻繁
上下文切換是指CPU從一個(gè)進(jìn)程切換到另一個(gè)進(jìn)程,這個(gè)過程也是需要消耗一定時(shí)間的。如果說上下文切換過于頻繁,說明CPU用于執(zhí)行進(jìn)程代碼的時(shí)間少了。第2點(diǎn)有提到負(fù)載高會(huì)引起上下文切換頻繁,但是上下文切換頻繁負(fù)載不一定就高。
2. 系統(tǒng)統(tǒng)計(jì):
# vmstat -s
? ? ? 3850536 K total memory
?? ? ? 435656 K used memory
?? ? ? 444704 K active memory
?? ? ? 482420 K inactive memory
? ? ? 2749692 K free memory
? ? ? ? 35036 K buffer memory
?? ? ? 630152 K swap cache
? ? ? 4190204 K total swap
? ? ? ? ? ? 0 K used swap
? ? ? 4190204 K free swap
? ? ? ? 22097 non-nice user cpu ticks
? ? ? ? ? 398 nice user cpu ticks
? ? ? ? 57515 system cpu ticks
? ? ? 1832006 idle cpu ticks
? ? ? ? 19672 IO-wait cpu ticks
? ? ? ? ? ? 0 IRQ cpu ticks
? ? ? ? 19937 softirq cpu ticks
? ? ? ? ? ? 0 stolen cpu ticks
?? ? ? 521649 pages paged in
?? ? ? 747828 pages paged out
? ? ? ? ? ? 0 pages swapped in
? ? ? ? ? ? 0 pages swapped out
? ? ? 8836085 interrupts
?? ? 13743858 CPU context switches
?? 1618561648 boot time
? ? ? ? 84026 forks
cat /proc/stat
- ctxt 169484918? 系統(tǒng)的上下文切換數(shù)
cat /proc/interrupts
- LOC:? 16646624? 17617208? 19843962? 18641958? Local timer interrupts
系統(tǒng)timer中斷數(shù)
vmstat -n 1
每秒更新狀態(tài),有runqueue大小,cs的數(shù)量,swap in/out的數(shù)量,cpu的狀態(tài)
進(jìn)程統(tǒng)計(jì):
pidstat -u -w 1
每秒刷新進(jìn)程的cpu使用和上下文切換數(shù)量統(tǒng)計(jì)
cat /proc/752/status
- voluntary_ctxt_switches: 1625? 主動(dòng)放棄cpu執(zhí)行的調(diào)度、切換
- nonvoluntary_ctxt_switches: 26? 非自愿被調(diào)度,不然timer中斷、外部中斷
cat /proc/752/stack
當(dāng)前wait chain (wchan)上的調(diào)用棧,backtrace(是不是感覺很不錯(cuò))
3. CPU密集型:cpu時(shí)間片不夠用,cpu使用率高
IO密集型: 進(jìn)程頻繁切換,執(zhí)行一點(diǎn)就自動(dòng)放棄,cpu使用率低。一般為磁盤讀寫、網(wǎng)絡(luò)數(shù)據(jù)
有兩個(gè)內(nèi)核線程kswapd和events(新內(nèi)核改為kworker),在特定情況下會(huì)表現(xiàn)得像IO密集型進(jìn)程。
其中kswapd是用于管理虛擬內(nèi)存的,當(dāng)物理內(nèi)存不足,需要頻繁交換虛擬內(nèi)存,kswapd的上下文切換將明顯增多;
而events用來處理工作隊(duì)列,當(dāng)不斷有work進(jìn)入排隊(duì)且這些work處理時(shí)間很短時(shí),
events的上下文切換會(huì)明顯增多
iotop

展示進(jìn)程的io的使用
注意展示結(jié)果,可能和想的不一樣,因?yàn)楹芏嗟膇o操作都是在內(nèi)核線程完成的
100%IO并不意味著它正在使用所有IO操作.這意味著除了等待IO之外什么都不做.因此,具有低/零磁盤帶寬的高%IO可以是正常的。btrfs-submit就是這種,死等IO結(jié)束什么也不做。