试着推导一下Linux中计算Load Average的算式

90 阅读1分钟

试着推导一下Linux中计算Load Average的算式

参考:www.fortra.com/resources/g…

ldavg-3-8.jpg

对于这个RC电路,根据串联电路的电压关系,

(1)

Vb=VC+VRV_b = V_C + V_R

又因为

(2)

VC=1CqV_C = \frac {1} {C}q

(3)

VR=RIV_R = RI

将(2)、(3)代入(1),并整理,得到

(4)

RI=1Cq+VbRI = -\frac {1} {C}q + V_b

两边同时乘以CC

(5)

RIC=q+CVbRIC = -q+CV_b

考虑tt时刻的瞬时电流

(6),

I(t)=dq(t)dtI(t) = \frac {dq(t)} {dt}

将(6)代入(5),并整理

(7)

RC(dq(t)dt)=q(t)+CVb(t)RC(\frac {dq(t)} {dt}) = -q(t) + CV_b(t)

解微分方程(7),得到

(8)

q(t)=et+cRC+CVb(t)q(t) = e^{-\frac {t+c} {RC}} + CV_b(t)

忽略常数项cc

(9)

q(t)=etRC+CVb(t)q(t) = e^{-\frac {t} {RC}} + CV_b(t)

考虑(tΔt)(t-\Delta t)时刻的状态

(10)

q(tΔt)=etΔtRC+CVb(tΔt)q(t-\Delta t) = e^{-\frac {t-\Delta t} {RC}} + CV_b(t-\Delta t)

(10)两端同时乘以下面这个因数

(11)

eΔtRCe^{-\frac {\Delta t} {RC}}

(12)

eΔtRCq(tΔt)=eΔtRCetΔtRC+eΔtRCCVb(tΔt)e^{-\frac {\Delta t} {RC}} q(t-\Delta t) = e^{-\frac {\Delta t} {RC}} e^{-\frac {t-\Delta t} {RC}} + e^{-\frac {\Delta t} {RC}} CV_b(t-\Delta t)

化简(12)

(13)

eΔtRCq(tΔt)=eΔtRCtΔtRC+eΔtRCCVb(tΔt)e^{-\frac {\Delta t} {RC}} q(t-\Delta t) = e^{-\frac {\Delta t} {RC} - \frac {t-\Delta t} {RC}} + e^{-\frac {\Delta t} {RC}} CV_b(t-\Delta t)

(14)

eΔtRCq(tΔt)=etRC+eΔtRCCVb(tΔt)e^{-\frac {\Delta t} {RC}} q(t-\Delta t) = e^{-\frac {t} {RC}} + e^{-\frac {\Delta t} {RC}} CV_b(t-\Delta t)

带入(9),并整理

(14)

eΔtRCq(tΔt)=q(t)CVb(t)+eΔtRCCVb(tΔt)e^{-\frac {\Delta t} {RC}} q(t-\Delta t) = q(t) - CV_b(t) + e^{-\frac {\Delta t} {RC}} CV_b(t-\Delta t)

(15)

q(t)=eΔtRCq(tΔt)+CVb(t)eΔtRCCVb(tΔt)q(t) = e^{-\frac {\Delta t} {RC}} q(t-\Delta t) + CV_b(t) - e^{-\frac {\Delta t} {RC}} CV_b(t-\Delta t)

假设在很短的Δt\Delta t时间内,Vb(t)=Vb(tΔt)=VbV_b(t) = V_b(t-\Delta t) = V_b,则

(17)

q(t)=eΔtRCq(tΔt)+(1eΔtRC)CVbq(t) = e^{-\frac {\Delta t} {RC}} q(t-\Delta t) + (1 - e^{-\frac {\Delta t} {RC}}) CV_b

而Linux的Load Average为

avenrun[t] = avenrun[t-Δt] * exp + nr_active * (1 - exp)

其中,

q(t) = avenrun[t] -> run queue length
exp = e^(-Δt/RC) -> weight
nr_active = CV_b -> current active processes

The sampled value of the electric charge (q(t)) at time step (t) corresponds to the sampled run-queue length, q(t-Δt) is the previous sample of the run-queue, and the amount of charge remaining in the capacitor represents the number of active processes (CV_b).