↓ 跳过正文
  1. Posts/

Linux LoadAvg 编程比赛

·547 字·2 分钟·

问题背景
#

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

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

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

下面是 byronhe 的两个解:

1. 究极版之 stap 大法
#

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

#!/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年]

·5739 字·12 分钟
本文介绍目前现代密码学的最先进技术, 前半部分主要翻译自 《Cryptographic Right Answers》,附上收集的资料,和byron个人的理解。 密码学理论艰深,概念繁多,本人知识水平有限,错误难免,如果您发现错误,请务必指出,非常感谢! 下文分类介绍在各种适用场景下,你应该使用的现代密码学算法 # 1. 加密数据 : # 按照优先级,应该选择: (1) 首选 NaCl库,或者libsodium库,使用里面的crypto_secretbox()/crypto_secretbox_open() 函数 (2) Chacha20-Poly1305 算法 (3) AES-GCM 算法 适用场景:当你需要避免把明文数据在网络上传输的时候。 以上3种算法,都是AEAD类的算法,AEAD是2015年最好的选择。 其中的(2)和(3)在结构上类似:一个流加密模式的算法,配合一个多项式结构的MAC。 (2)是一个流加密算法,配合一个为通用cpu优化的MAC算法, 对密码学库的实现者来说,Poly1305也比GCM更容易安全地实现。 AES-GCM是工业标准(TLS目前主要用的就是AES-GCM),现代CPU通常都有专门为AES-GCM设计的硬件指令,但是在没有硬件指令支持的CPU上(比如32位的arm),(3)性能低于(2)。

nginx下配置高性能,高安全性的https TLS服务

·1433 字·3 分钟
下文以nginx为例,介绍如何部署一个高性能,高安全性的https服务器。 并附送一个优化出来的openssl编译脚本,可以编译出一个高性能,高安全性的openssl库,您可以直接复制粘贴使用。 此处直接给出实践指导,后续再写文章解释tls协议的这些原理细节。 nginx下https配置的优化点,主要有: session ticket session id cache ocsp stapling http KeepAlive ECDHE等ciphersuite优化 openssl 编译优化 一, nginx https的配置 # 先贴一下nginx配置,如下。 是根据mozilla的权威文档 ,和生成工具(选择 nginx,intermediate ) 生成的配置为基础,加入session ticket等配置的结果