透過下面的指令可以判斷 CPU 是否出現瓶頸,再搭配 top、ps 等指令進一步檢查,就能定位到是哪些行程造成 CPU 負載過高。
[blackfox@localhost ~]$ vmstat 2 3 # 間隔2s,打印3次 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 244824 932 497892 0 0 0 0 3 9 0 0 100 0 0 0 0 0 244824 932 497892 0 0 0 0 38 91 0 1 99 0 0 0 0 0 244824 932 497892 0 0 0 0 40 90 0 1 99 0 0
情境:在多 CPU 的系統中,整體 CPU 使用率不高,但應用回應卻很慢。
結論:單執行緒只會用到一個 CPU,導致該 CPU 占用率 100% 而無法處理其他請求,其餘 CPU 卻閒置,於是出現整體使用率不高但應用緩慢的現象。
# sar -P 0 3 5 # 對第一個CPU進行統計 [blackfox@localhost ~]$ sar -u 3 5 # 顯示CPU利用率,間隔3s,顯示5次 Linux 3.10.0-327.el7.x86_64 (localhost.localdomain) 01/22/2017 _x86_64_ (1 CPU) 07:35:52 AM CPU %user %nice %system %iowait %steal %idle 07:35:55 AM all 0.00 0.00 0.00 0.00 0.00 100.00 07:35:58 AM all 0.00 0.00 0.34 0.00 0.00 99.66 07:36:01 AM all 0.34 0.00 0.34 0.00 0.00 99.32 07:36:04 AM all 0.00 0.00 0.34 0.00 0.00 99.66 07:36:07 AM all 0.34 0.00 0.00 0.00 0.00 99.66 Average: all 0.14 0.00 0.20 0.00 0.00 99.66
[blackfox@localhost ~]$ iostat -c Linux 3.10.0-327.el7.x86_64 (localhost.localdomain) 01/22/2017 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.13 0.00 0.23 0.01 0.00 99.63
經驗:8 核 CPU 若 load average 三個值長期大於 8,代表負載很高,會拖累系統效能。
[blackfox@localhost ~]$ uptime 07:54:27 up 17 days, 16:56, 1 user, load average: 0.00, 0.02, 0.05
經驗:可用記憶體/物理記憶體 < 20% 代表記憶體吃緊,需要擴充。
[blackfox@localhost ~]$ free -m # -m 以MB為單位,-s 時間段內不間斷監控 total used free shared buff/cache available Mem: 977 249 240 49 487 488 Swap: 2048 0 2048
經驗:
1、正常情況下 svctm < await。
2、svctm 接近 await 代表幾乎沒有 I/O 等待,效能很好。
3、CPU/記憶體複合或過多請求都會使 svctm 增加。
4、%util 接近 100% 代表 I/O 已滿負荷。
[blackfox@localhost ~]$ sar -d Linux 3.10.0-327.el7.x86_64 (localhost.localdomain) 01/22/2017 _x86_64_ (1 CPU) 12:00:01 AM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 12:10:01 AM dev8-0 0.10 0.00 0.89 9.03 0.00 3.97 1.10 0.01 12:20:01 AM dev8-0 0.05 0.00 0.35 7.56 0.00 64.63 61.70 0.28 12:30:01 AM dev8-0 0.02 0.00 0.16 7.08 0.01 254.77 128.
[blackfox@localhost ~]$ iostat -dx /dev/sda3 Linux 3.10.0-327.el7.x86_64 (localhost.localdomain) 01/22/2017 _x86_64_ (1 CPU) Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util sda3 0.00 0.01 0.01 0.05 0.21 0.34 17.98 0.00 41.36 30.56 42.60 7.77 0.05
[blackfox@localhost ~]$ sar -n DEV 2 3 # DEV:網絡接口,EDEV:網絡錯誤統計數據,SOCK:套接字信息,FULL:顯示所有 Linux 3.10.0-327.el7.x86_64 (localhost.localdomain) 01/22/2017 _x86_64_ (1 CPU) 09:26:26 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 09:26:28 AM eno16777736 0.00 0.00 0.00 0.00 0.00 0.00 0.00 09:26:28 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[blackfox@localhost ~]$ watch -n 3 -d free # -n 重復執行時間,-d 高亮顯示變動 Every 3.0s: free Sun Jan 22 09:21:48 2017 total used free shared buff/cache available Mem: 1001332 256792 245500 50948 499040 498864 Swap: 2098172 0 2098172
參考資料:https://man.linuxde.net/par/3