通過top或着uptime命令可以看到系統的平均負載,如下,分別表示過去 1 分鍾、5 分鍾、15 分鍾的系統平均負載(之所以統計三個時間點數值,是為了更好的反映系統整體的負載趨勢)
[root@k8s-master ~]# uptime 10:54:36 up 8 days, 12:31, 1 user, load average: 0.25, 0.51, 1.19
平均負載的含義:系統處於可運行狀態和不可中斷狀態的平均進程數,也就是平均活躍進程數,這里的平均指的是指數衰減平均值,對應到進程的狀態如下
可運行狀態(Running或Runnable)
不可中斷狀態(Uninterruptible Sleep,也稱為 Disk Sleep)
#查找R或D狀態的進程 ps aux | awk '{if($8 ~ /R|D/) print $0 }' [root@k8s-master ~]# ps aux | awk '{if($8 ~ /R|D/) print $0 }' root 9 0.0 0.0 0 0 ? R Jun30 6:08 [rcu_sched] root 30474 0.0 0.0 157456 1912 pts/0 R+ 11:10 0:00 ps aux root 30475 0.0 0.0 113548 1232 pts/0 R+ 11:10 0:00 awk {if($8 ~ /R|D/) print $0 }
根據上述平均負載的定義,能夠導致平均負載升高的場景有:
1、處於Running狀態的進程大量消耗cpu(CPU密集型進程)
2、大量處於Runnable的進程,cpu會頻繁進行上下文切換(寄存器、程序計數器)
操作系統管理的任務包括進程(線程),還有硬件通過觸發信號,會導致中斷處理程序的調用
上下文切換包括:進程上下文切換(虛擬內存、棧、全局變量等用戶空間的資源,還包括了內核堆棧、寄存器等內核空間的狀態)、線程上下文切換、以及中斷上下文切換
特權模式切換:用戶態到內核態的上下文切換
查看系統總體的上下文切換情況 vmstat 5 [root@k8s-master sysstat]# vmstat 5 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 131084 203200 1801028 0 0 1724 166 2 8 12 7 79 2 0 0 0 0 130480 203216 1801272 0 0 42 81 1829 5047 8 5 86 1 0 0 0 0 129676 203232 1802092 0 0 161 110 1814 4840 16 6 76 1 0 0 0 0 129344 203236 1802496 0 0 78 70 1939 5258 14 12 73 1 0 0 0 0 128464 203248 1803156 0 0 120 106 1836 5195 9 6 84 1 0 1 0 0 128000 203256 1803612 0 0 91 98 1807 4726 16 6 77 1 0 cs(context switch)是每秒上下文切換的次數。
in(interrupt)則是每秒中斷的次數。
r(Running or Runnable)是就緒隊列的長度,
也就是正在運行和等待 CPU 的進程數。
b(Blocked)則是處於不可中斷睡眠狀態的進程數。
各類型中斷次數統計
watch -d cat /proc/interrupts 查看每個進程的上下文切換次數
pidstat -w -t 1 (-t顯示線程上下文切換統計) 05:17:54 PM UID PID cswch/s nvcswch/s Command 05:17:55 PM 0 1 1.03 0.00 systemd 05:17:55 PM 0 3 21.65 0.00 ksoftirqd/0 05:17:55 PM 0 9 100.00 0.00 rcu_sched 05:17:55 PM 0 296 12.37 0.00 kworker/0:1H 05:17:55 PM 0 320 6.19 0.00 jbd2/vda1-8 05:17:55 PM 0 1139 1.03 0.00 iscsid 05:17:55 PM 1337 5766 13.40 0.00 envoy 05:17:55 PM 1337 6061 16.49 1.03 envoy 05:17:55 PM 1337 6065 16.49 0.00 envoy 05:17:55 PM 1 6141 1.03 2.06 python 05:17:55 PM 1337 6331 13.40 0.00 envoy 05:17:55 PM 0 10774 14.43 0.00 envoy 05:17:55 PM 0 10805 2.06 0.00 YDLive 05:17:55 PM 0 10844 15.46 0.00 envoy 05:17:55 PM 0 11532 3.09 1.03 coredns 05:17:55 PM 0 12767 40.21 15.46 etcd 05:17:55 PM 0 13170 10.31 0.00 kube-proxy 05:17:55 PM 0 18892 1.03 0.00 sshd 05:17:55 PM 0 19758 1.03 0.00 kworker/u2:2 05:17:55 PM 0 27112 8.25 0.00 kworker/0:2 05:17:55 PM 0 28294 1.03 1.03 pidstat 05:17:55 PM 0 28365 2.06 0.00 YDService cswch:自願上下文切換,是指進程無法獲取所需資源,導致的上下文切換。比如說, I/O、內存等系統資源不足時,就會發生自願上下文切換。 nvcswch:非自願上下文切換,則是指進程由於時間片已到等原因,被系統強制調度,進而發生的上下文切換
3、存在處於D狀態的進程
centos安裝 pidstat和mpstat工具 yum install -y sysstat
#模擬進程消耗cpu stress -i 1 --timeout 600 #查看各cpu使用情況(我這里只有一個cpu) (取5s內的數據計算一組平均值) [root@k8s-master ~]# date;mpstat -P ALL 5 1 Thu Jul 9 14:32:06 CST 2020 Linux 3.10.0-957.27.2.el7.x86_64 (k8s-master.com) 07/09/2020 _x86_64_ (1 CPU) 02:32:07 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 02:32:12 PM all 96.79 0.00 3.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:32:12 PM 0 96.79 0.00 3.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle Average: all 96.79 0.00 3.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: 0 96.79 0.00 3.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00 #確定占用cpu較多的進程 pidstat的-u參數表示查看cpu指標 [root@k8s-master ~]# pidstat -u 5 1 Linux 3.10.0-957.27.2.el7.x86_64 (k8s-master.com) 07/09/2020 _x86_64_ (1 CPU) 02:37:27 PM UID PID %usr %system %guest %CPU CPU Command 02:37:33 PM 0 25315 80.90 0.00 0.00 80.90 0 stress 02:37:33 PM 0 25539 0.00 0.39 0.00 0.39 0 pidstat Average: UID PID %usr %system %guest %CPU CPU Command Average: 0 25315 80.90 0.00 0.00 80.90 - stress Average: 0 25539 0.00 0.39 0.00 0.39 - pidstat
stress -i 1 --timeout 600 通過mpstat可以看到cpu0有60%的時間片都在用於等待io,且這個過錯不可中斷 [root@k8s-master ~]# mpstat -P ALL 5 1 Linux 3.10.0-957.27.2.el7.x86_64 (k8s-master.com) 07/09/2020 _x86_64_ (1 CPU) 03:07:38 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 03:07:43 PM all 9.58 0.00 30.00 60.00 0.00 0.42 0.00 0.00 0.00 0.00 03:07:43 PM 0 9.58 0.00 30.00 60.00 0.00 0.42 0.00 0.00 0.00 0.00 Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle Average: all 9.58 0.00 30.00 60.00 0.00 0.42 0.00 0.00 0.00 0.00 Average: 0 9.58 0.00 30.00 60.00 0.00 0.42 0.00 0.00 0.00 0.00
通過前面的命令ps aux也能看到stess進程狀態為D
為什么會有D狀態的進程?
不可中斷狀態的進程則是正處於內核態關鍵流程中的進程,並且這些流程是不可打斷的,比如最常見的是等待硬件設備的 I/O 響應,也就是我們在 ps 命令中看到的 D 狀態(Uninterruptible Sleep,也稱為 Disk Sleep)的進程。比如,當一個進程向磁盤讀寫數據時,為了保證數據的一致性,在得到磁盤回復前,它是不能被其他進程或者中斷打斷的,這個時候的進程就處於不可中斷狀態。如果此時的進程被打斷了,就容易出現磁盤數據與進程數據不一致的問題。所以,不可中斷狀態實際上是系統對進程和硬件設備的一種保護機制。
ps aux中各進程狀態說明(man ps)
PROCESS STATE CODES Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process: D uninterruptible sleep (usually IO) R running or runnable (on run queue) S interruptible sleep (waiting for an event to complete) T stopped by job control signal t stopped by debugger during the tracing W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z defunct ("zombie") process, terminated but not reaped by its parent For BSD formats and when the stat keyword is used, additional characters may be displayed: < high-priority (not nice to other users) N low-priority (nice to other users) L has pages locked into memory (for real-time and custom IO) s is a session leader l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) + is in the foreground process group