1 限制块设备读速率:
首先内核配置必须要满足如下要求:
- Enable Block IO controller
CONFIG_BLK_CGROUP=y
- Enable throttling in block layer
CONFIG_BLK_DEV_THROTTLING=y
这个可以从/boot 目录下的 config-xxx 文件确定。
挂载 blkio 子系统:
mount -t tmpfs cgroup_root /sys/fs/cgroup
mkdir /sys/fs/cgroup/blkio
mount -t cgroup -o blkio none /sys/fs/cgroup/blkio
sync /// 因为测试的是 buffered IO 所以这写些骤很重要。
echo 3 > /proc/sys/vm/drop_caches
限制一个设备的读速率,在这里限制的是U盘的速率:(其中sdb 对应 8:16 ,sda 对应着 8:0 )
sina@ubuntu:~$ df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/loop0 29979608 21393392 7063324 76% /
udev 966128 4 966124 1% /dev
tmpfs 389260 900 388360 1% /run
none 5120 0 5120 0% /run/lock
none 973144 340 972804 1% /run/shm
/dev/sda6 130095100 32334964 97760136 25% /host
cgroups 973144 0 973144 0% /sys/fs/cgroup
cgroup_root 973144 0 973144 0% /sys/fs/cgroup
/dev/sdb 7808092 6260552 1547540 81% /media/KINGSTON
echo "8:16 1048576" > /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device
以上限制U盘的读速率是 1.0 MB/s 。
用 disk文件测试:
root@ubuntu:~/work/test# dd if=/media/KINGSTON/disk.img of=/dev/null bs=1M count=512
记录了512+0 的读入
记录了512+0 的写出
536870912字节(537 MB)已复制,511.81 秒,1.0 MB/秒
同样也可以通过设置 blkio.throttle.write_bps_device 限制 U 盘的写速率为0.5M/s:(此时U盘挂载为sdd)
root@ubuntu:/sys/fs/cgroup/blkio# echo "8:48 524288" > /sys/fs/cgroup/blkio/blkio.throttle.write_bps_device
root@ubuntu:/sys/fs/cgroup/blkio# dd if=/dev/zero of=/media/KINGSTON/diskt.img bs=1M count=512
记录了512+0 的读入
记录了512+0 的写出
536870912字节(537 MB)已复制,913.309 秒,588 kB/秒
iostat 结果如下:
sina@ubuntu:~$ iostat -xm sdd 10
Linux 3.0.0-26-generic-pae (ubuntu) 2012年09月21日 _i686_ (4 CPU)
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdd 0.00 0.00 0.00 4.40 0.00 0.51 235.27 0.05 11.45 0.00 11.45 11.18 4.92
avg-cpu: %user %nice %system %iowait %steal %idle
0.67 0.00 0.52 24.94 0.00 73.86
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdd 0.00 1.30 0.60 5.00 0.00 0.50 183.29 0.18 31.93 0.67 35.68 24.00 13.44
avg-cpu: %user %nice %system %iowait %steal %idle
0.68 0.00 0.38 24.83 0.00 74.12
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdd 0.00 6.20 1.60 4.60 0.00 0.49 163.26 0.17 26.77 0.75 35.83 20.19 12.52
avg-cpu: %user %nice %system %iowait %steal %idle
4.41 0.00 1.15 24.79 0.00 69.65
通过写 blkio.throttle.read_iops_device 来限制 U盘的每秒IO 次数:
echo "<major>:<minor> <rate_io_per_second>" > /cgrp/blkio.throttle.read_iops_device
当用dd 测试 字符速率的时候(1M/s) 用命令 ( iostat -xm sdb 10 ) 来测试 IO 速率。
sina@ubuntu:~$ iostat -xm sdb 10
Linux 3.0.0-26-generic-pae (ubuntu) 2012年09月20日 _i686_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.85 0.00 0.63 24.59 0.00 71.93
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.00 0.00 18.00 0.00 1.00 0.00 113.89 0.07 4.00 4.00 0.00 1.78 3.20
avg-cpu: %user %nice %system %iowait %steal %idle
4.07 0.00 0.90 24.16 0.00 70.86
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.00 0.00 18.00 0.00 1.00 0.00 113.89 0.07 3.64 3.64 0.00 1.78 3.20
avg-cpu: %user %nice %system %iowait %steal %idle
3.85 0.00 0.95 24.64 0.00 70.55
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.00 0.00 18.00 0.00 1.00 0.00 113.89 0.07 4.00 4.00 0.00 1.78 3.20
avg-cpu: %user %nice %system %iowait %steal %idle
2.00 0.00 0.70 25.05 0.00 72.25
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.00 0.00 18.00 0.00 1.00 0.00 113.89 0.07 3.78 3.78 0.00 1.78 3.20
按照上面的读测试显示此时每秒的读 IO 次数 大概在 18次左右。
也可以计算出这个dd命令的IO速度:
root@ubuntu:~/TestDir# time dd if=/media/KINGSTON/busybox.img of=/dev/null bs=1M count=512
记录了512+0 的读入
记录了512+0 的写出
536870912字节(537 MB)已复制,500.458 秒,1.1 MB/秒
real 8m20.463s
user 0m0.000s
sys 0m0.828s
512 / (8 * 60 + 20.463) = 1 M/s
测试 将U盘每秒的IO速率限制为10.
# echo "8:16 10" > /sys/fs/cgroup/blkio/blkio.throttle.read_iops_device
此时再测试U盘的读速度:
root@ubuntu:~/TestDir# dd if=/media/KINGSTON/disk.img of=/dev/null bs=1M count=512
记录了512+0 的读入
记录了512+0 的写出
536870912字节(537 MB)已复制,914.64 秒,573 kB/秒
在这个过程中:
sina@ubuntu:~$ iostat -xm sdb 10
Linux 3.0.0-26-generic-pae (ubuntu) 2012年09月20日 _i686_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
7.88 0.00 1.77 22.80 0.00 67.55
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.20 0.00 9.80 0.00 0.54 0.00 113.01 0.03 2.61 2.61 0.00 1.71 1.68
avg-cpu: %user %nice %system %iowait %steal %idle
3.32 0.00 1.82 23.50 0.00 71.35
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.00 0.00 10.10 0.00 0.57 0.00 115.99 0.03 2.73 2.73 0.00 1.82 1.84
avg-cpu: %user %nice %system %iowait %steal %idle
4.63 0.00 1.48 24.29 0.00 69.60
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.00 0.00 10.00 0.00 0.55 0.00 112.91 0.03 2.64 2.64 0.00 1.76 1.76
avg-cpu: %user %nice %system %iowait %steal %idle
4.01 0.00 0.88 24.47 0.00 70.64
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.10 0.00 9.80 0.10 0.55 0.00 113.74 0.02 2.51 2.53 0.00 1.74 1.72
可见 当 read_iops_device 和 read_bps_device 同时设置的时候,那么会同时起限制的作用。
用blkio 限制指定进程的 读速度:
创建脚本 blk.sh
root@ubuntu:~/work/test# cat blk.sh
dd if=/media/KINGSTON/diskt.img of=/dev/null bs=1M count=512 &
echo $! > /sys/fs/cgroup/blkio/test1/tasks
在 cgroup 的 blkio 目录中创建一个目录 test1:
root@ubuntu:/sys/fs/cgroup/blkio# mkdir test1/
限制 test1 的读速率:
root@ubuntu:/sys/fs/cgroup/blkio# echo "8:48 524288" > /sys/fs/cgroup/blkio/test1/blkio.throttle.read_bps_device
开启一个dd ,将其PID attach 到 test1 的tasks 文件中。
root@ubuntu:~/work/test# ./blk.sh
用 iostat 测试其读速率:
sina@ubuntu:~$ iostat -xm sdb 10
avg-cpu: %user %nice %system %iowait %steal %idle
7.36 0.00 1.43 24.91 0.00 66.30
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdd 0.00 0.00 9.00 0.00 0.50 0.00 113.89 0.03 2.98 2.98 0.00 1.69 1.52
avg-cpu: %user %nice %system %iowait %steal %idle
4.27 0.00 2.39 23.57 0.00 69.77
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdd 0.10 0.00 8.20 0.00 0.50 0.00 124.90 0.03 3.95 3.95 0.00 1.95 1.60
avg-cpu: %user %nice %system %iowait %steal %idle
5.73 0.00 3.09 23.64 0.00 67.54
Device: rrqm/s wrqm/s r/s w/s rMB/s wMB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdd 0.00 0.00 9.00 0.00 0.50 0.00 113.89 0.04 4.00 4.00 0.00 1.78 1.60
root@ubuntu:~/work/test# ./blk.sh
root@ubuntu:~/work/test# 记录了512+0 的读入
记录了512+0 的写出
536870912字节(537 MB)已复制,1029.69 秒,521 kB/秒
可见其读速率 符合预先的设置。
同样的开启一个dd 在没有attack PID 的时候其读速率将不会受控:
root@ubuntu:~/work/test# dd if=/media/KINGSTON/diskt.img of=/dev/null bs=1M count=512
记录了512+0 的读入
记录了512+0 的写出
536870912字节(537 MB)已复制,12.4191 秒,43.2 MB/秒
fio配合cgroup测试存储设备IOPS分配
http://blog.yufeng.info/archives/2001
CGroup限制系统资源how to<命令行>
http://www.mysqlsky.com/201203/cgroup-os-how-to