抛砖引玉
Load average
#==========================================================================================================
在Linux系统中,uptime、w、top等命令都会有系统平均负载load average的输出,那么什么是系统平均负载呢?系统平均负载被定义为在特定时间间隔内运行队列中的平均进程数。
如果一个进程满足以下条件则其就会位于运行队列中:
- 它没有在等待I/O操作的结果                      #也就是说它只是在等待CPU操作
- 它没有主动进入等待状态(也就是没有调用'wait')    #它是被逼等待的
- 没有被停止(例如:等待终止)                     #它还是可以继续运行的load average是什么?通过top命令可以查看系统的load average,它显示的是系统在1分钟,5分钟,15分钟之内的load情况。
通俗的说,load是指run-queue length (i.e., the sum of the number of processes waiting in the run-queue plus the number currently executing).
专业一点说,A exponentially-damped time-dependent averageload average怎么计算?
这其中涉及到不少数学知识为了使内核可以高效计算load average,采用了fixed-point arithmetic。fixed-point arithmetic是一种非常快速的模拟浮点运算的方法,
特别是在没有FPU(float point unit)部件的处理器上,非常有用。
计算公式:load(t) = load(t-1) e^(-5/60) + n (1 - e^(-5/60)),迭代计算,其中n为run-queue length。
为什么采用这个计算公式呢?
由Exponential Smoothing方程有,Y(t)= Y(t-1) + a*[X(t) - Y(t-1)],whereX(t) is the input raw data, Y(t - 1) is the value due to the previoussmoothing iteration and Y(t) is the new smoothed value.
这个公式就当作公理吧,不要问我为什么,我也不知道。
令a=1-b,b为e^(-5/60),就可以得到load average的计算公式
采用此公式的好处:局部的load抖动不会对load average造成重大影响,使其平滑。http://blog.csdn.net/naivebaby/archive/2006/11/15/1386577.aspxIn Linux, a process can be either runnable or blocked waiting for an event to complete. 
A blocked process may be waiting for data from an I/O device or the results of a system call. 
If a process is runnable, that means that it is competing for the CPU time with the other processes that are also runnable. 
A runnable process is not necessarily using the CPU, but when the Linux scheduler is deciding which process to run next, 
it picks from the list of runnable processes. When these processes are runnable, but waiting to use the processor, 
they form a line called the run queue. The longer the run queue, the more processes wait in line. 
Another common system statistic is that of load average. The load on a system is the total amount of running and runnable process. 
For example, if two processes were running and three were available to run, the system's load would be five. 
The load average is the amount of load over a given amount of time. Typically, the load average is taken over 1 minute, 5 minutes, and 15 minutes. 
This enables you to see how the load changes over time.其实一直不知道load具体是什么,只知道是系统的负荷。今天看资料找到了答案。简单翻译一下在Linux中,进程分为三种状态,一种是阻塞的进程blocked process,一种是可运行的进程runnable process,另外就是正在运行的进程running process。
当进程阻塞时,进程会等待I/O设备的数据或者系统调用。进程可运行状态时,它处在一个运行队列run queue中,与其他可运行进程争夺CPU时间。 
系统的load是指正在运行running one和准备好运行runnable one的进程的总数。
比如现在系统有2个正在运行的进程,3个可运行进程,那么系统的load就是5。
load average就是一定时间内的load数量。#==================================================================================================================================
参考文献
http://blog.52news.com/article.asp?id=1571
http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
上面的链接形象的描述了负载的过程。这里的负载,确切的来说指的是CPU的负载,和内存,磁盘无关。
明确一个事情:CPU在处理进程的时候,是挨个处理的,如果同一时刻有多个进程需要处理,那么就必须排队。虽然CPU做了时间分片,伪装了一下“排队”,让每个进程看起来是在同时运行,
但实际上分片后它还是在排队。这是论述LOAD的前提。在“过桥”模型里,是没有考虑CPU时间分片的,这也就是他说的LOAD>1就不行的原因。下面以一台单核CPU机器(单车道),运行QQ和BMW为例子(2辆车)。
1:车还没发动,此时道路是空的,此时LOAD=0。
2:QQ来了,道路上只有它一辆车在跑,此时LOAD=1。它跑得很爽。
3:车道上同时来了QQ和BMW,QQ在前,BMW在后,那么此时车道上有2辆车。比较下进入队列的条件
- 它没有在等待I/O操作的结果                      #车都已经发动,正常开动了
- 它没有主动进入等待状态(也就是没有调用'wait')    #它是被逼排队的,不是在等人。
- 没有被停止(例如:等待终止)                     #它还是可以继续运行的,没有抛锚
条件满足,BMW排队。此时LOAD=2 。正在运行线程(1,QQ)+等待线程(1,BMW)=2. QQ跑的很爽,MSN得等待。
4:QQ通过道路了,路上只有BMW,此时LOAD=1,它跑得很爽。
5:BMW通过道路了,路面又空了,此时LOAD=0。扩展一下,如果同时来了10辆车,那么有9辆得等待。此时LOAD=10. 司机们很郁闷,于是开始狂躁起来,世界就不和谐了,整个交通系统就慢下来了。
应付这个局面有2个办法
1:提高通行速度(CPU主频)
2:提高通道数量(CPU核数)因为CPU时间分片的存在,可以想象成很宽的单车道,可以同时容纳10辆车(10车道?),但是有2个规则:
1:同时只有一辆车在开动。
2:每辆车的运行时间相同。
如果10辆车里有2辆法拉利,2辆宝马,2辆QQ,2辆牛车。那么很明显,法拉利会在牛车前通过,即使牛车开始是排在法拉利前面。
对应到CPU里,需要CPU时间越少的进程,就能够越快的被执行完。这时候虽然9辆车在排队,但是并不是停滞不前的,而是在“都”慢慢的往前挪。
这样一来司机们的抱怨就能降低不少,至少他们都能看到“希望”。我觉得这就是为什么LOAD数可以大于CPU*核数的原因。uptime里看到的是单位时间内的平均LOAD,单位时间等于多少?我也不知道。有网上照抄的,有自己理解的。希望能多有人来探讨。

解决方案 »

  1.   

    我认为“车道”模型并不能反应出CPU的工作,
    1:时间分片
    2:超线程技术
    3:2颗单核CPU的处理能力并不能和1颗2核CPU等同。
      

  2.   

    多核下,CPU负载比较麻烦,没什么定论。单核,在目前的指令流水线下,主要是看 CPU 执行了多少运算。 但具体软件的显示则要看这个软件本身是根据什么算法得出来的了。