技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> Linux --> Linux系统的CPU使用率和Load

Linux系统的CPU使用率和Load

浏览:3137次  出处信息

   好吧,我承认这个话题是老生常谈,我自己也在2009年刚接触性能测试时就已经开始关注并略知一二;然而,对于CPU使用率和系统负载Load的确切含义以及它们之间的关系,我相信不是每个人都说得清楚的(包括我自己)。也时常被了解性能测试的新手问道CPU使用率和Load方面的基本慨念,所以我决定还是自己写篇文章吧,让自己也梳理得更加清晰一点。

   Linux系统的CPU使用率的概念是比较容易理解的,顾名思义就是CPU的使用状况,也就是一段时间之中,CPU用于执行任务占用的时间与总的时间的比率。

   在top、sar、vmstat、mpstat等命令中可以看到CPU使用率通常包含如下几种统计(摘自 man sar):

   

   %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占用率。

   %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值有点高。

   %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越空闲。

   

   查看CPU使用率,推荐如下Linux命令:

1
2
3
4
[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的命令,推荐如下:

1
2
3
[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


建议继续学习:

  1. Linux如何统计进程的CPU利用率    (阅读:14093)
  2. 解剖CPU    (阅读:7918)
  3. 查看 CPU, Memory, I/O and NetFlow    (阅读:6232)
  4. 如何查看Linux 硬件配置信息    (阅读:5684)
  5. Linux下进程绑定多CPU运行    (阅读:5597)
  6. 从Java视角理解CPU上下文切换(Context Switch)    (阅读:5348)
  7. 从load data引发的死锁说起    (阅读:4993)
  8. Linux下的CPU使用率与服务器负载的关系与区别    (阅读:4955)
  9. Linux下CPU的利用率    (阅读:4716)
  10. 写Java也得了解CPU缓存    (阅读:4341)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1