定義
每次執行uptime 都會顯示如下信息
root@user:~# uptime
20:54:13 up 428 days, 4:28, 7 users, load average: 0.00, 0.10, 0.55
對於每個信息的展示 可以通過man uptime 查看到
man uptime
UPTIME(1) User Commands UPTIME(1)
NAME
uptime - Tell how long the system has been running.
SYNOPSIS
uptime [options]
DESCRIPTION
uptime gives a one line display of the following information. The current time, how long the system has been running, how many users are currently logged on,
and the system load averages for the past 1, 5, and 15 minutes.
This is the same information contained in the header line displayed by w(1).
System load averages is the average number of processes that are either in a runnable or uninterruptable state. A process in a runnable state is either using
the CPU or waiting to use the CPU. A process in uninterruptable state is waiting for some I/O access, eg waiting for disk. The averages are taken over the
three time intervals. Load averages are not normalized for the number of CPUs in a system, so a load average of 1 means a single CPU system is loaded all the
time while on a 4 CPU system it means it was idle 75% of the time.
首先這個命令用來展示 主要當前系統已經運行了多長時間,第一列是當前時間,第二列是系統已經運行的時間,第三列當前已登陸用戶數,第四列展示過去1分鐘、5分鐘、15分鐘系統平均負載
20:54:13 //當前時間
up 428 days, 4:28, //系統已經運行的時間
7 users, //當前已登陸用戶數
load average: 0.00, 0.10, 0.55 //過去1分鐘、5分鐘、15分鐘系統平均負載
前三列都好理解,第四列什麼是系統平均負載?首先他不是cpu使用率,解釋中提到它是單位時間內系統處於**可運行狀態(runnable)以及不可中斷狀態(uninterruptable)**的平均進程數
- 可運行狀態(runnable) 就是進程處於正在使用cpu或者正在等待cpu的狀態
- 不可中斷狀態(uninterruptable) 是進程 處於內核態的原子方法調用,不可打斷
那如何理解系統平均負載?
如果系統平均負載是 2,那意味着:
- 在有2個cpu的系統上,所有cpu剛好被全部佔滿
- 在有4個cpu的系統上,cpu有50%的空閒
- 在有1個cpu的系統上,有50%的進程競爭不到cpu
系統負載 ?= cpu使用率
首先cpu使用一旦接近100% 肯定是有問題的那麼如何評價系統平均負載是合理的?根據上面的解釋,最理想的情況是每個cpu上運行着一個進程,那麼前提是我們要知道當前系統有多少cpu:
grep 'model name' /proc/cpuinfo -l
知道了cpu個數,當系統平均負載大於cpu個數的時候,系統肯定是超負荷的,實際的生產環境下,當系統平均負載 大於 cpu個數70%的時候,就需要排查問題了,因爲一旦負載過高,就會導致系統響應過慢,影響服務功能了
但是系統平均負載高就一定意味着cpu使用率高麼?
回看平均負載的定義,它不僅包括正在使用cpu的進程,還有等待cpu核等待io的進程,而cpu使用率的定義是單位時間內cpu處於佔用情況的統計,所以兩者並不是完全對等的
那就可能意味着:
- cpu密集型進程,由於大量使用cpu必然導致平均負載很高
- io密集型進程,等待也會導致平均負載很高,但是cpu使用率不一定高
- 大量等待cpu進程調度的進程,也會導致平均負載很高,cpu使用率也會很高
爲了驗證這三種情況,首先需要安裝幾個輔助性能測試工具
apt-get install -y stress
apt-get install -y stress-ng
apt-get install -y sysstat
- stress 是一個linux 系統壓力測試工具,用來模擬平均負載的情況,用來模擬cpu高的情況,但不適合模擬io高的情況,因爲stress使用的是 sync() 系統調用,它的作用是刷新緩衝區內存到磁盤中。對於新安裝的虛擬機,緩衝區可能比較小,無法產生大的IO壓力,這樣大部分就都是系統調用的消耗了,所以不能模擬io高的情況
- stress-ng stress的下一代stress-ng,有更豐富的選賢,用來模擬io高的情況,
- sysstat 是個監控和分析系統性能的工具,後面主要用到裏面的mpstat和pidstat,一定要安裝11.5.5版本以上的軟件,否則看不到%wait 列的信息
- mpstat 多核cpu系統系統分析工具,用來查看每個cpu的性能指標以及平均指標
- pidstat 進程性能分析工具,用來實時查看進程的cpu使用、內存使用、io、上下文切換等
cpu密集型
首先查看平均負載
root@user:~# uptime
21:53:02 up 428 days, 5:27, 7 users, load average: 0.01, 0.01, 0.00
在第一個終端執行stress 命令 模擬cpu使用100%
stress --cpu 1 --timeout 600
在第二個終端實時查看cpu平均負載
root@user:~# watch -d uptime //-d 會高亮顯示變化的區域
Every 2.0s: uptime Sat Jul 13 21:56:17 2019
21:56:17 up 428 days, 5:30, 7 users, load average: 0.12, 0.04, 0.01
第三個終端 使用mpstat,查看cpu使用率的變化情況
root@user:~/soft/systat/sysstat-11.5.5# mpstat -P ALL 5
Linux 4.4.0-105-generic (user) 07/13/2019 _x86_64_ (1 CPU)
10:06:23 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:06:28 PM all 99.58 0.00 0.42 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:06:28 PM 0 99.58 0.00 0.42 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:06:28 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:06:33 PM all 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:06:33 PM 0 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:06:33 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:06:38 PM all 99.78 0.00 0.22 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:06:38 PM 0 99.78 0.00 0.22 0.00 0.00 0.00 0.00 0.00 0.00 0.00
第四個終端使用pidstat查看到底哪個進程cpu使用率高
root@user:~# pidstat -u 5 1
Linux 4.4.0-105-generic (user) 07/13/2019 _x86_64_ (1 CPU)
10:18:43 PM UID PID %usr %system %guest %wait %CPU CPU Command
10:18:48 PM 0 9165 0.21 0.00 0.00 0.00 0.21 0 AliYunDun
10:18:48 PM 0 17652 100.00 0.00 0.00 0.64 100.00 0 stress
Average: UID PID %usr %system %guest %wait %CPU CPU Command
Average: 0 9165 0.21 0.00 0.00 0.00 0.21 - AliYunDun
Average: 0 17652 100.00 0.00 0.00 0.64 100.00 - stress
root@user:~# ps -ef| grep 17652
root 17652 17651 98 22:18 pts/5 00:00:53 stress --cpu 1 --timeout 600
root 17769 17695 0 22:19 pts/8 00:00:00 grep --color=auto 17652
終端2看到 系統平均負載會接近1
終端3看到cpu 0的使用率接近100%
終端4看到stress 進程使用率很高
io密集型
終端1執行stress 模擬io壓力
root@user:~# stress-ng -i 1 --hdd 1 --timeout 600
stress-ng: info: [17352] dispatching hogs: 1 hdd, 1 iosync
stress-ng: info: [17352] cache allocate: default cache size: 33792K
終端2查看平均負載
root@user:~# watch -d uptime //-d 會高亮顯示變化的區域
發現平均負載高到3 +
終端3查看cpu使用率以及io等待
root@user:~/soft/systat/sysstat-11.5.5# mpstat -P ALL 5
Linux 4.4.0-105-generic (user) 07/13/2019 _x86_64_ (1 CPU)
10:15:38 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:15:43 PM all 0.45 0.00 7.80 91.76 0.00 0.00 0.00 0.00 0.00 0.00
10:15:43 PM 0 0.45 0.00 7.80 91.76 0.00 0.00 0.00 0.00 0.00 0.00
10:15:43 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:15:48 PM all 0.43 0.00 13.36 86.21 0.00 0.00 0.00 0.00 0.00 0.00
10:15:48 PM 0 0.43 0.00 13.36 86.21 0.00 0.00 0.00 0.00 0.00 0.00
10:15:48 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:15:53 PM all 0.65 0.00 9.29 90.06 0.00 0.00 0.00 0.00 0.00 0.00
10:15:53 PM 0 0.65 0.00 9.29 90.06 0.00 0.00 0.00 0.00 0.00 0.00
10:15:53 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:15:58 PM all 0.67 0.00 7.78 91.56 0.00 0.00 0.00 0.00 0.00 0.00
10:15:58 PM 0 0.67 0.00 7.78 91.56 0.00 0.00 0.00 0.00 0.00 0.00
10:15:58 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:16:03 PM all 0.65 0.00 14.07 85.28 0.00 0.00 0.00 0.00 0.00 0.00
10:16:03 PM 0 0.65 0.00 14.07 85.28 0.00 0.00 0.00 0.00 0.00 0.00
發現cpu使用率只有14%,而iowait 高達90%多
終端4 查看到底哪個進行io高
root@user:~# pidstat -u 5 1
Linux 4.4.0-105-generic (user) 07/13/2019 _x86_64_ (1 CPU)
10:21:25 PM UID PID %usr %system %guest %wait %CPU CPU Command
10:21:30 PM 0 156 0.00 0.90 0.00 0.68 0.90 0 jbd2/vda1-8
10:21:30 PM 0 9165 0.45 0.00 0.00 0.00 0.45 0 AliYunDun
10:21:30 PM 0 15654 0.23 0.00 0.00 0.00 0.23 0 watch
10:21:30 PM 0 17918 0.00 15.32 0.00 0.90 15.32 0 stress-ng-hdd
10:21:30 PM 0 20490 0.00 2.03 0.00 0.45 2.03 0 kworker/u2:2
Average: UID PID %usr %system %guest %wait %CPU CPU Command
Average: 0 156 0.00 0.90 0.00 0.68 0.90 - jbd2/vda1-8
Average: 0 9165 0.45 0.00 0.00 0.00 0.45 - AliYunDun
Average: 0 15654 0.23 0.00 0.00 0.00 0.23 - watch
Average: 0 17918 0.00 15.32 0.00 0.90 15.32 - stress-ng-hdd
Average: 0 20490 0.00 2.03 0.00 0.45 2.03 - kworker/u2:2
root@user:~# ps -ef| grep 17918
root 17918 17917 14 22:20 pts/5 00:00:15 stress-ng -i 1 --hdd 1 --timeout 600
root 18083 17695 0 22:22 pts/8 00:00:00 grep --color=auto 17918
看到stress-ng io比較高
進程密集型
當系統中出現超出cpu運行能力時,就會出現cpu等待時間
終端1用stress 模擬8個進程
stress -c 8 --timeout 600
終端2查看系統負載
Every 2.0s: uptime Sat Jul 13 22:30:47 2019
22:30:57 up 428 days, 6:05, 4 users, load average: 7.91, 5.70, 3.19
看到系統負載接近運行的進程數
終端3查看cpu使用率
root@user:~/soft/systat/sysstat-11.5.5# mpstat -P ALL 5
Linux 4.4.0-105-generic (user) 07/13/2019 _x86_64_ (1 CPU)
10:27:21 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:27:26 PM all 99.56 0.00 0.44 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:26 PM 0 99.56 0.00 0.44 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:26 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:27:31 PM all 99.80 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:31 PM 0 99.80 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:31 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:27:36 PM all 99.57 0.00 0.43 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:36 PM 0 99.57 0.00 0.43 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:36 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:27:41 PM all 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:41 PM 0 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:41 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:27:46 PM all 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:46 PM 0 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:46 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:27:51 PM all 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
10:27:51 PM 0 99.79 0.00 0.21 0.00 0.00 0.00 0.00 0.00 0.00 0.00
可以看到cpu使用率到了100%
終端4查看 哪個進程導致cpu使用率高
root@user:~# pidstat -u 5 1
Linux 4.4.0-105-generic (user) 07/13/2019 _x86_64_ (1 CPU)
10:29:30 PM UID PID %usr %system %guest %wait %CPU CPU Command
10:29:35 PM 0 9165 0.21 0.21 0.00 0.00 0.43 0 AliYunDun
10:29:35 PM 0 18501 13.52 0.00 0.00 93.99 13.52 0 stress
10:29:35 PM 0 18502 13.52 0.00 0.00 94.21 13.52 0 stress
10:29:35 PM 0 18503 11.16 0.00 0.00 100.00 11.16 0 stress
10:29:35 PM 0 18504 14.38 0.00 0.00 83.48 14.38 0 stress
10:29:35 PM 0 18505 13.52 0.00 0.00 94.21 13.52 0 stress
10:29:35 PM 0 18506 13.52 0.00 0.00 94.21 13.52 0 stress
10:29:35 PM 0 18507 13.52 0.00 0.00 94.42 13.52 0 stress
10:29:35 PM 0 18508 13.30 0.00 0.00 93.78 13.30 0 stress
root@user:~# ps -ef| grep 18503
root 18503 18500 12 22:27 pts/5 00:00:36 stress -c 8 --timeout 600
root 18948 17695 0 22:32 pts/8 00:00:00 grep --color=auto 18503
可以看到時stress進程導致了cpu高
現在基本感受到了平均系統負載與cpu使用率之間的關係,以及我們如何判斷具體的情況