我们在下来的的项目中倾向于用cgroup来做资源的隔离和限制,原因是cgroup的开销很小,而且很易用。cgroup 可以参考这里
我们特别关心cgroup的blkio子模块,他有2种限制模式:
1. throttle,限制每个进程能使用的IOPS或者吞吐量。
2. weight,现在每个进程能使用的IOPS的能力的比例,必须通过CFQ调度器来实现。
要使用blkio的weight限制需要注意几个事情:
1. 必须走directio, 如果buffered io因为最终写IO的进程不是发起IO的进程,结果会有很大的偏差。
2. 调度器必须是CFQ。
3. 测试工具必须支持cgroup的相关限制。
新版本的支持cgroup的fio可以在这里下载 git clone git://git.kernel.dk/fio.git, 更多参看这里。
man fio 可以看看cgroup相关的文档:
cgroup=str
Add job to this control group. If it doesn’t exist, it will be created. The system must have a mounted cgroup blkio mount
point for this to work. If your system doesn’t have it mounted, you can do so with:
# mount -t cgroup -o blkio none /cgroup
cgroup_weight=int
Set the weight of the cgroup to this value. See the documentation that comes with the kernel, allowed values are in the
range of 100..1000.
cgroup_nodelete=bool
Normally fio will delete the cgroups it has created after the job completion. To override this behavior and to leave
cgroups around after the job completion, set cgroup_nodelete=1. This can be useful if one wants to inspect various cgroup
files after job completion. Default: false
这里只是粗粗演示下如何用fio按照比例来限制进程使用的IO, 我们来构造下场景:
我们在创建2个1g大小的文件,进行随机的混合读写,一个给500的比例,一个给100的比例,总的比例是1000。那么理论上可以看到A进程可以得到多于B进程5倍的IO能力。
操作如下:
$ cat test.fio
[global]
bs=4k
ioengine=libaio
iodepth=32
direct=1
rw=randrw
rwmixread=90
time_based
runtime=180
cgroup_nodelete=1
[test1]
filename=test1.dat
size=1G
cgroup_weight=500
cgroup=test1
[test2]
filename=test2.dat
size=1G
cgroup_weight=100
cgroup=test2
$ sudo fio test.fio
test1: (g=0): rw=randrw, bs=4K-4K/4K-4K, ioengine=libaio, iodepth=32
test2: (g=0): rw=randrw, bs=4K-4K/4K-4K, ioengine=libaio, iodepth=32
fio 2.0
Starting 2 processes
Jobs: 2 (f=2): [mm] [5.5% done] [618K/90K /s] [151 /22 iops] [eta 02m:51s]
...
我们从另外一个终端可以看到IO能力的分配情况:
差不多是5:1的比例,符合预期。
我们在使用的时候会担心kernel的稳定性,所以用fio能够大压力,长时间的来测试cgroup模块的可靠性,收集数据作为应用的参考。
