好吧,我認可這個話題是老生常談,我本身也在2009年剛接觸性能測試時就已經開始關注並略知一二;然而,對於CPU使用率和系統負載Load的確切含義以及它們之間的關係,我相信不是每一個人都說得清楚的(包括我本身)。也時常被瞭解性能測試的新手問道CPU使用率和Load方面的基本慨念,因此我決定仍是本身寫篇文章吧,讓本身也梳理得更加清晰一點。html
Linux系統的CPU使用率的概念是比較容易理解的,顧名思義就是CPU的使用情況,也就是一段時間之中,CPU用於執行任務佔用的時間與總的時間的比率。java
在top、sar、vmstat、mpstat等命令中能夠看到CPU使用率一般包含以下幾種統計(摘自 man sar):linux
%user Percentage of CPU utilization that occurred while executing at the user level (application). Note that this field includes time spent running virtual processors. (未標誌nice值的)用戶態程序的CPU佔用率。app
%nice Percentage of CPU utilization that occurred while executing at the user level with nice priority. 標誌了nice值的用戶態程序的CPU佔用率。工具
%system Percentage of CPU utilization that occurred while executing at the system level (kernel). Note that this field includes time spent servicing hardware and software interrupts. 系統態(內核)程序的CPU佔用率。性能
%iowait Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request. I/O等待的CPU佔用率。測試
%steal Percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor. 這個通常是在虛擬機中才能看到數值,好比:個人VPS供應商CPU overcommitment很嚴重,故我偶爾能看到%steal值有點高。this
%idle Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request. %idle越高,說明CPU越空閒。spa
查看CPU使用率,推薦以下Linux命令:.net
[root@jay-linux ~]# top [root@jay-linux ~]# sar -u 1 5 [root@jay-linux ~]# vmstat -n 1 5 [root@jay-linux ~]# mpstat -P ALL 1 5
Linux的Load(系統負載),是一個讓新手不太容易瞭解的概念。top/uptime等工具默認會顯示1分鐘、5分鐘、15分鐘的平均Load。具體來講,平均Load是指,在特定的一段時間內統計的正在CPU中運行的(R狀態)、正在等待CPU運行的、處於不可中斷睡眠的(D狀態)的任務數量的平均值。
我估計也沒說的太清楚,看下wikipedia上的一段話吧:An idle computer has a load number of 0. Each process using or waiting for CPU (the ready queue or run queue) increments the load number by 1. Most UNIX systems count only processes in the running (on CPU) or runnable (waiting for CPU) states. However, Linux also includes processes in uninterruptible sleep states (usually waiting for disk activity), which can lead to markedly different results if many processes remain blocked in I/O due to a busy or stalled I/O system.
還有man sar中的解釋:The load average is calculated as the average number of runnable or running tasks (R state), and the number of tasks in uninterruptible sleep (D state) over the specified interval.
對於一個系統來講,多少的Load算合理,多少又算Load高呢?
通常來講,對於Load的數值不要大於系統的CPU核數(或者開啓了超線程,超線程也當成CPU core吧)。固然,有人以爲Load等於CPU core數量的2倍也沒事,不過,我本身是在Load達到CPU core數量時,通常都會去查看下是什麼具體緣由致使load較高的。
Linux中查看Load的命令,推薦以下:
[root@jay-linux ~]# top [root@jay-linux ~]# uptime [root@jay-linux ~]# sar -q 1 5
最後,說一下CPU使用率和Load的關係吧。若是主要是CPU密集型的程序在運行(If CPU utilization is near 100 percent (user + nice + system), the workload sampled is CPU-bound.),那麼CPU利用率高,Load通常也會比較高。而I/O密集型的程序在運行,可能看到CPU的%user, %system都不高,%iowait可能會有點高,這時的Load一般比較高。同理,程序讀寫慢速I/O設備(如磁盤、NFS)比較多時,Load可能會比較,而CPU利用率不必定高。這種狀況,還常常發生在系統內存不足並開始使用swap的時候,Load通常會比較高,而CPU使用率並不高。
本文就簡單說這麼多了,想了解更全面的信息,可參考如下方法:
1. man sar, man top (認真看相關解析,定有收穫)
2. wikipedia:http://en.wikipedia.org/wiki/Load_%28computing%29
3. 幫助理解Load:http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
4. 幫助理解load的中文博客:http://www.blogjava.net/cenwenchu/archive/2008/06/30/211712.html