pidstat

1、工具簡介
mpstat 用來查看系統(tǒng)每個 CPU 的性能指標(biāo),以及所有 CPU 的平均指標(biāo)。
vmstat 可以查看系統(tǒng)上下文切換、內(nèi)存。
前兩個只給出了系統(tǒng)總體的情況,要想查看每個進(jìn)程的詳細(xì)情況,就需要使用 pidstat
pidstat 用來查看具體進(jìn)程的 CPU、內(nèi)存、I/O 以及上下文切換等性能指標(biāo)。
pidstat 默認(rèn)顯示進(jìn)程的數(shù)據(jù),加 -t 參數(shù)可以輸出線程的數(shù)據(jù)。加 -w 參數(shù)可以輸出上下文切換情況。

2、平均負(fù)載

$ uptime
 12:49:05 up 2 days, 10:26,  2 users,  load average: 0.11, 0.08, 0.02

12:49:05 是當(dāng)前時間;
up 2 days, 10:26 是系統(tǒng)運(yùn)行時間;
2 users 是正在登錄用戶數(shù)(因?yàn)槲议_了 2 個ssh終端,所以是 2);
load average 三個值分別是過去 1、5、15 分鐘的平均負(fù)載(top命令的第一行也有輸出)。平均負(fù)載是平均活躍進(jìn)程數(shù),如果這三個值相差不大,說明系統(tǒng)負(fù)載很平穩(wěn)。

獲取 CPU 數(shù)量可以用命令:

$ grep 'model name' /proc/cpuinfo | wc -l
1

最理想的情況是每個 CPU 上都剛好運(yùn)行著一個進(jìn)程,每個 CPU 都得到了充分利用。
當(dāng)平均負(fù)載比 CPU 個數(shù)大的時候,系統(tǒng)已經(jīng)出現(xiàn)了過載。
當(dāng)平均負(fù)載高于 CPU 數(shù)量 70% 的時候,就應(yīng)該分析排查負(fù)載高的問題了。

下面用 stress 命令模擬一個 CPU 壓力的場景,先安裝 stress

sudo apt install stress
stress --cpu 1 --timeout 600

--cpu 1是產(chǎn)生 1 個進(jìn)程不停的計算隨機(jī)數(shù)的平方根, --timeout 600 是在 600 秒之后結(jié)束。
在另一個終端查看 uptime 輸出,平均負(fù)載很快就接近 1 了。

$ uptime
 13:07:39 up 2 days, 10:45,  2 users,  load average: 0.01, 0.01, 0.00
$ uptime 
 13:10:49 up 2 days, 10:48,  2 users,  load average: 0.94, 0.40, 0.15
$ uptime 
 13:12:33 up 2 days, 10:50,  2 users,  load average: 0.99, 0.58, 0.25

或者用 watch -d -n 1 uptime 可以自動刷新,也可以觀測到平均負(fù)載在變高。
-d 表示變化的部分可以高亮顯示,
-n 是刷新間隔,這里選每 1 秒刷新,

mpstat 也可以觀察到,%usr 在升高:

$ mpstat 
Linux 4.15.0-117-generic (uu)   09/14/2020  _x86_64_    (1 CPU)

01:09:57 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
01:09:57 PM  all    0.27    0.01    0.34    0.09    0.00    0.01    0.00    0.00    0.00   99.28
$ mpstat
Linux 4.15.0-117-generic (uu)   09/14/2020  _x86_64_    (1 CPU)

01:13:44 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
01:13:44 PM  all    0.38    0.01    0.34    0.09    0.00    0.01    0.00    0.00    0.00   99.17
$ mpstat
Linux 4.15.0-117-generic (uu)   09/14/2020  _x86_64_    (1 CPU)

01:14:54 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
01:14:54 PM  all    0.42    0.01    0.34    0.09    0.00    0.01    0.00    0.00    0.00   99.14

pidstat 可以查看具體哪個進(jìn)程的 %usr 在變高。

3、上下文切換

$ vmstat 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0    268 550080 108920 1077896    0    0  1677    26  86  127  1  1 99  0  0

指標(biāo)含義:
cs(context switch)是每秒 上下文切換的次數(shù)。
r(Running or Runnable)是就緒隊(duì)列的長度,也就是正在運(yùn)行和等待 CPU 的進(jìn)程數(shù)。
in(interrupt)則是每秒 中斷的次數(shù),可以從 /proc/interrupts 文件中讀取。

pidstat -w -t
指標(biāo)含義:
cswch 表示每秒自愿上下文切換(voluntary context switches)的次數(shù),比如 I/O、內(nèi)存 等資源不足時,就會發(fā)生自愿上下文切換。
nvcswch 表示每秒非自愿上下文切換(non voluntary context switches)的次數(shù)。比如大量進(jìn)程在爭搶 CPU。

sudo apt install -y sysbench

下面用 sysbench 做實(shí)驗(yàn),模擬系統(tǒng)多線程調(diào)度,--threads=2是 2 個線程,--max-time=60是運(yùn)行60秒的基準(zhǔn)測試:

sysbench --threads=2 --max-time=60 threads run

可以看到 vmstatcs 升高了:

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
12  0    268 470996 110748 1154896    0    0  1669    26   86  128  1  1 99  0  0
$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 6  0    268 469180 110756 1155052    0    0  1669    26   86  196  1  1 99  0  0

再用 pidstat -w -t 可以看到具體是 __sysbench 線程的上下文切換比較多。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容