前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux LoadAvg 编程比赛

Linux LoadAvg 编程比赛

作者头像
byronhe
发布2021-06-25 10:51:25
4860
发布2021-06-25 10:51:25
举报
文章被收录于专栏:Tech ExplorerTech Explorer

问题背景

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 等资源的需求,是一种广义的负载。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题背景
  • 1. 究极版之 stap 大法
  • 2. 用 O_SYNC 制造 uninterruptible 进程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档