Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2373898
  • 博文数量: 298
  • 博客积分: 7876
  • 博客等级: 准将
  • 技术积分: 5500
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 13:39
文章存档

2013年(2)

2012年(142)

2011年(154)

分类: LINUX

2012-02-21 15:56:37

服务器负载探讨

 

转自:http://blog.chinaunix.net/space.php?uid=24086995&do=blog&id=349694

 

Load average
#==========================================================================================================
Linux系统中,uptimewtop等命令都会有系统平均负载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 average

load average
怎么计算?
这其中涉及到不少数学知识

为了使内核可以高效计算load average,采用了fixed-point arithmeticfixed-point arithmetic是一种非常快速的模拟浮点运算的方法,
特别是在没有FPUfloat point unit)部件的处理器上,非常有用。
计算公式:load(t) = load(t-1) e^(-5/60) + n (1 - e^(-5/60)),迭代计算,其中nrun-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.
这个公式就当作公理吧,不要问我为什么,我也不知道。
a1-bbe^(-5/60),就可以得到load average的计算公式
采用此公式的好处:局部的load抖动不会对load average造成重大影响,使其平滑。

http://blog.csdn.net/naivebaby/archive/2006/11/15/1386577.aspx

In 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机器(单车道),运行QQBMW为例子(2辆车)。
1
:车还没发动,此时道路是空的,此时LOAD=0
2
QQ来了,道路上只有它一辆车在跑,此时LOAD=1。它跑得很爽。
3
:车道上同时来了QQBMW,QQ在前,BMW在后,那么此时车道上有2辆车。比较下进入队列的条件
    -
它没有在等待I/O操作的结果                      #车都已经发动,正常开动了
    -
它没有主动进入等待状态(也就是没有调用'wait')    #它是被逼排队的,不是在等人。
    -
没有被停止(例如:等待终止)                     #它还是可以继续运行的,没有抛锚
条件满足,BMW排队。此时LOAD=2 。正在运行线程(1QQ+等待线程(1BMW=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辆宝马,2QQ2辆牛车。那么很明显,法拉利会在牛车前通过,即使牛车开始是排在法拉利前面。
对应到CPU里,需要CPU时间越少的进程,就能够越快的被执行完。

这时候虽然9辆车在排队,但是并不是停滞不前的,而是在慢慢的往前挪。
这样一来司机们的抱怨就能降低不少,至少他们都能看到希望。我觉得这就是为什么LOAD数可以大于CPU*核数的原因。

uptime
里看到的是单位时间内的平均LOAD,单位时间等于多少?我也不知道。

阅读(984) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~