查看CPU上下文切換的命令
了解了CPU上下文切會導(dǎo)致系統(tǒng)負(fù)載升高卵沉,下面我們介紹下用linux命令查看系統(tǒng)CPU上下文切換信息
vmstat
# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
4 0 0 864188 45680 761716 0 0 1 12 4 5 0 0 99 0 0
和cpu上下文切換相關(guān)的乾翔,我們需要關(guān)注四列
- r(running or runnable)就緒隊(duì)列長度悬垃,正在運(yùn)行和等待cpu的進(jìn)程數(shù)
- b (Blocked) 處于不可中斷睡眠狀態(tài)的進(jìn)程數(shù)
- in (interrupt)每秒中斷次數(shù)
- cs(context switch)每秒上下文切換次數(shù)
上面vmstat
命令給出的輸出是系統(tǒng)總體的上下文切換
pidstat
通過pidstat
可以查看詳細(xì)進(jìn)程的上下文切換信息
pidstat -w 1
Linux 3.10.0-693.2.2.el7.x86_64 (iz2zeab8t820b32tt9i96gz) 2019年12月13日 _x86_64_ (1 CPU)
21時32分22秒 UID PID cswch/s nvcswch/s Command
21時32分23秒 0 3 1.00 0.00 ksoftirqd/0
21時32分23秒 0 9 33.00 0.00 rcu_sched
21時32分23秒 0 10 1.00 0.00 watchdog/0
21時32分23秒 38 797 1.00 0.00 ntpd
21時32分23秒 0 10370 9.00 0.00 kworker/0:2
21時32分23秒 0 10387 1.00 0.00 sshd
21時32分23秒 0 10408 1.00 0.00 pidstat
- cswch(voluntary context switches): 表示每秒自愿上下文切換次數(shù)
- 自愿上下文切換:進(jìn)程無法獲取所需自愿,導(dǎo)致上下文切換,比如I/O梯捕,內(nèi)存等資源不足時
- nvcswch(non voluntary context switches):表示每秒非自愿上下文切換次數(shù)
- 非自愿上下文切換:進(jìn)程由于時間片到等原因匙瘪,被系統(tǒng)強(qiáng)制調(diào)度铆铆,發(fā)生的上下文切換
案例分析
機(jī)器配置:2C4G蝶缀,centos系統(tǒng)
預(yù)先安裝:sysbench,sysstat
開四個終端薄货,用于監(jiān)控性能
- 通過第一個終端sysbench模擬多線程調(diào)度瓶頸
$ sysbench --threads=10 --max-time=300 threads run
- 第二個終端用vmstat監(jiān)控上下文情況
# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
8 0 0 7331532 4172 450216 0 0 15 2778 623 2142 3 3 94 0 0
4 0 0 7331532 4172 450216 0 0 0 0 35229 5074073 15 84 2 0 0
7 0 0 7331532 4172 450216 0 0 0 0 34426 4942170 14 83 3 0 0
5 0 0 7331532 4172 450216 0 0 0 0 37055 5134408 14 84 3 0 0
6 0 0 7331532 4172 450216 0 0 0 0 43965 4843946 13 83 4 0 0
9 0 0 7331532 4172 450216 0 0 0 0 31367 5100571 14 83 3 0 0
- cs上下文切換從2142 升高到了500萬
- r列:就緒隊(duì)列長度到了8-9翁都,遠(yuǎn)遠(yuǎn)超過了系統(tǒng)CPU的個數(shù)
- us(user)和sy(system)列:這兩列的CPU使用率加起來上升到了100%,其中sy列使用率達(dá)到84%說明CPU主要被內(nèi)核占用
- in列:中斷次數(shù)也上升到了1萬左右谅猾,說明中斷處理也是個潛在問題
- 第三個終端使用pidstat查看
上面vmstat
輸出的是系統(tǒng)總體的CPU上下文切換情況柄慰,我們需要用pidstat
查看具體那個進(jìn)程或者線程占用了CPU
pidstat -wt -u 1
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 2019年12月13日 _x86_64_ (2 CPU)
23時05分46秒 UID TGID TID %usr %system %guest %CPU CPU Command
23時05分47秒 0 13722 - 0.00 0.99 0.00 0.99 0 kworker/0:3
23時05分47秒 0 - 13722 0.00 0.99 0.00 0.99 0 |__kworker/0:3
23時05分47秒 0 16060 - 27.72 100.00 0.00 100.00 0 sysbench
23時05分47秒 0 - 16061 1.98 16.83 0.00 18.81 0 |__sysbench
23時05分47秒 0 - 16062 2.97 15.84 0.00 18.81 1 |__sysbench
23時05分47秒 0 - 16063 3.96 16.83 0.00 20.79 1 |__sysbench
23時05分47秒 0 - 16064 2.97 16.83 0.00 19.80 0 |__sysbench
23時05分47秒 0 - 16065 1.98 14.85 0.00 16.83 1 |__sysbench
23時05分47秒 0 - 16066 3.96 15.84 0.00 19.80 1 |__sysbench
23時05分47秒 0 - 16067 1.98 16.83 0.00 18.81 1 |__sysbench
23時05分47秒 0 - 16068 2.97 15.84 0.00 18.81 1 |__sysbench
23時05分47秒 0 - 16069 1.98 17.82 0.00 19.80 0 |__sysbench
23時05分47秒 0 - 16070 2.97 15.84 0.00 18.81 1 |__sysbench
23時05分47秒 0 16075 - 0.00 0.99 0.00 0.99 0 pidstat
23時05分47秒 0 - 16075 0.00 0.99 0.00 0.99 0 |__pidstat
23時05分46秒 UID TGID TID cswch/s nvcswch/s Command
23時05分47秒 0 3 - 2.97 0.00 ksoftirqd/0
23時05分47秒 0 - 3 2.97 0.00 |__ksoftirqd/0
23時05分47秒 0 9 - 8.91 0.00 rcu_sched
23時05分47秒 0 - 9 8.91 0.00 |__rcu_sched
23時05分47秒 0 - 671 0.99 0.00 |__in:imjournal
23時05分47秒 0 669 - 9.90 0.00 vmtoolsd
23時05分47秒 0 - 669 9.90 0.00 |__vmtoolsd
23時05分47秒 0 - 1147 0.99 0.00 |__tuned
23時05分47秒 0 13670 - 0.99 0.00 kworker/u256:2
23時05分47秒 0 - 13670 0.99 0.00 |__kworker/u256:2
23時05分47秒 0 13722 - 1.98 0.00 kworker/0:3
23時05分47秒 0 - 13722 1.98 0.00 |__kworker/0:3
23時05分47秒 0 15871 - 3.96 0.00 kworker/1:3
23時05分47秒 0 - 15871 3.96 0.00 |__kworker/1:3
23時05分47秒 0 15982 - 0.99 0.00 kworker/1:1
23時05分47秒 0 - 15982 0.99 0.00 |__kworker/1:1
23時05分47秒 0 - 16061 62697.03 429596.04 |__sysbench
23時05分47秒 0 - 16062 69033.66 437688.12 |__sysbench
23時05分47秒 0 - 16063 49371.29 490245.54 |__sysbench
23時05分47秒 0 - 16064 57375.25 434861.39 |__sysbench
23時05分47秒 0 - 16065 59927.72 426571.29 |__sysbench
23時05分47秒 0 - 16066 54672.28 434754.46 |__sysbench
23時05分47秒 0 - 16067 58420.79 441480.20 |__sysbench
23時05分47秒 0 - 16068 47862.38 467382.18 |__sysbench
23時05分47秒 0 - 16069 71267.33 426084.16 |__sysbench
23時05分47秒 0 - 16070 48990.10 461634.65 |__sysbench
23時05分47秒 0 16075 - 0.99 0.00 pidstat
23時05分47秒 0 - 16075 0.99 0.00 |__pidstat
上面pidstat
的輸出你可以發(fā)現(xiàn),CPU使用率最高的為sysbench導(dǎo)致税娜,sysbench的線程上下文切換很多坐搔,看來上下文切換罪魁禍?zhǔn)祝巧厦娌榭粗袛啻螖?shù)也很高
- 第四個終端查看中斷次數(shù)
$ watch -d cat /proc/interrupts
...
RES: 9781574 10163271 Rescheduling interrupts
...
變化速度最快的是重調(diào)度中斷(RES)
敬矩,這個中斷類型表示概行,喚醒空閑狀態(tài)CPU來調(diào)度新的任務(wù)運(yùn)行。這是多處理器系統(tǒng)(SMP)中弧岳,調(diào)度器用來分散任務(wù)到不通CPU的機(jī)制凳忙,通常也被成為 處理器間中斷 (inter processor interrupts, IPI)
所以這里中斷升高還是因?yàn)檫^多的任務(wù)調(diào)度問題
- 結(jié)論
因?yàn)閟ysbench較多線程使用CPU,導(dǎo)致大量非自愿上下文切換導(dǎo)致系統(tǒng)負(fù)載升高
小結(jié)
- 系統(tǒng)上下文切換從數(shù)百到一萬以內(nèi)禽炬,都算是正常的
- 自愿上下文切換變多了涧卵,說明進(jìn)程都在等待資源,有可能發(fā)生了I/O等其他問題
- 非自愿上下文切變多了瞎抛,說明進(jìn)程都在被強(qiáng)制調(diào)度艺演,也就是都在搶占CPU,說明CPU的確成了瓶頸
- 中斷次數(shù)變多了桐臊,說明CPU被中斷處理程序占用胎撤,還需要查看/proc/interroupts 文件分析具體中斷類型