问题背景

https://timyang.net/programming/load-average/

2015.08.13 高可用架构群 Load 编程比赛,Tim 在群征集一段代码使 load average 最高

多线程写几个 while 循环的方法太 trivial 了,就不提了。

下面是 byronhe 的两个解:

1. 究极版之 stap 大法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
vagrant@vagrant-ubuntu-trusty-64:~$ cat loadavg.stap

probe kernel.function("get_avenrun") {
    $avenrun[0]= ($1<<11);
    $avenrun[1]=$avenrun[0];
    $avenrun[2]=$avenrun[1];
}

vagrant@vagrant-ubuntu-trusty-64:~$ sudo stap  -g loadavg.stap  $(((1<<(64-11))-1))

效果如下:

https://github.com/torvalds/linux/blob/master/fs/proc/loadavg.c

https://github.com/torvalds/linux/blob/master/kernel/sched/loadavg.c

已经到达内核变量可以表示的最大值了,所以是究极版。嘿嘿嘿。

《用systemtap来修改下linux内核变量的值》 http://blog.yufeng.info/archives/102

《Install SystemTap in Ubuntu 14.04 》 http://blog.jeffli.me/blog/2014/10/10/install-systemtap-in-ubuntu-14-dot-04/

2. 用 O_SYNC 制造 uninterruptible 进程

利用 O_SYNC 制造同步请求,使得进程进入 D 状态,增大 loadavg

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env python
 import os
 import sys

 def load_add_1():
     fd=os.open("test.txt",os.O_CREAT|os.O_RDWR|os.O_SYNC, 0644)
     for i in xrange(10000*100):
         os.write(fd," "*100)
     sys.exit(0)

 for i in xrange(10):
     if os.fork() == 0:
         load_add_1()

《Linux Load Averages: Solving the Mystery》 深度解析 loadavg 的历史根源演变和意义

http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html

When load averages first appeared in Linux, they reflected CPU demand, as with other operating systems. But later on Linux changed them to include not only runnable tasks, but also tasks in the uninterruptible state (TASK_UNINTERRUPTIBLE or nr_uninterruptible). This state is used by code paths that want to avoid interruptions by signals, which includes tasks blocked on disk I/O and some locks. You may have seen this state before: it shows up as the “D” state in the output ps and top. The ps(1) man page calls it “uninterruptible sleep (usually IO)".

On Linux, load averages are (or try to be) “system load averages”, for the system as a whole, measuring the number of threads that are working and waiting to work (CPU, disk, uninterruptible locks). Put differently, it measures the number of threads that aren’t completely idle. Advantage: includes demand for different resources.

解释: linux 下的 loadavg ,表示 runnable 进程数,加上 uninterruptible 状态的进程数。 uninterruptible 状态出现在进程不希望被信号打断时,比如阻塞在磁盘 IO 上,或者某些锁上。 就是有时候会在 ps 或者 top 中看到的 “D” 状态。

因此 linux 下的 load average,不是整个系统对各种资源的需求,不仅包含对 cpu 的需求,也包含了对 磁盘 iops 等资源的需求,是一种广义的负载。