use cgroup blkio resource control limit throttle

简介:
本文将测试一下使用cgroup的blkio组来控制IO吞吐量 : 
测试环境CentOS 7.x x64
创建一个继承组
[root@150 rg1]# cd /sys/fs/cgroup/blkio/
[root@150 blkio]# mkdir rg1

继承组自动创建对应的限制文件
[root@150 blkio]# cd rg1
[root@150 rg1]# ll
total 0
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_merged
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_merged_recursive
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_queued
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_queued_recursive
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_service_bytes
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_service_bytes_recursive
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_serviced
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_serviced_recursive
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_service_time
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_service_time_recursive
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_wait_time
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.io_wait_time_recursive
-rw-r--r-- 1 root root 0 Dec  4 00:26 blkio.leaf_weight
-rw-r--r-- 1 root root 0 Dec  4 00:26 blkio.leaf_weight_device
--w------- 1 root root 0 Dec  4 00:26 blkio.reset_stats
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.sectors
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.sectors_recursive
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.throttle.io_service_bytes
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.throttle.io_serviced
-rw-r--r-- 1 root root 0 Dec  4 00:29 blkio.throttle.read_bps_device
-rw-r--r-- 1 root root 0 Dec  4 00:26 blkio.throttle.read_iops_device
-rw-r--r-- 1 root root 0 Dec  4 00:26 blkio.throttle.write_bps_device
-rw-r--r-- 1 root root 0 Dec  4 00:26 blkio.throttle.write_iops_device
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.time
-r--r--r-- 1 root root 0 Dec  4 00:26 blkio.time_recursive
-rw-r--r-- 1 root root 0 Dec  4 00:26 blkio.weight
-rw-r--r-- 1 root root 0 Dec  4 00:26 blkio.weight_device
-rw-r--r-- 1 root root 0 Dec  4 00:26 cgroup.clone_children
--w--w--w- 1 root root 0 Dec  4 00:26 cgroup.event_control
-rw-r--r-- 1 root root 0 Dec  4 00:26 cgroup.procs
-rw-r--r-- 1 root root 0 Dec  4 00:26 notify_on_release
-rw-r--r-- 1 root root 0 Dec  4 00:27 tasks

继承组的tasks为空.

找一个块设备作为测试目标, 注意现在只能控制块设备, 不能控制单个分区.

[root@150 ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/sda1                   40G  2.0G   38G   6% /
devtmpfs                    48G     0   48G   0% /dev
tmpfs                       48G     0   48G   0% /dev/shm
tmpfs                       48G   25M   48G   1% /run
tmpfs                       48G     0   48G   0% /sys/fs/cgroup
/dev/sda3                   95G  2.8G   87G   4% /opt

例如我这里要控制/dev/sda这个块设备 : 
[root@150 opt]# ll /dev/sda
brw-rw---- 1 root disk 8, 0 Nov 27 19:03 /dev/sda

将文件写入/dev/sda的一个文件系统中.
[root@150 rg1]# cd /opt
[root@150 opt]# dd if=/dev/zero of=./test.img bs=1k count=102400 
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 0.350907 s, 299 MB/s

直接从块设备读取到/dev/null, 不限制的话, 速度是14MB/S
[root@150 opt]# dd if=./test.img of=/dev/null bs=1k count=102400 iflag=direct
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 7.43936 s, 14.1 MB/s

限制读吞吐量是1MB/s : 
- Specify a bandwidth rate on particular device for root group. The format
  for policy is "<major>:<minor>  <bytes_per_second>".
[root@150 opt]# echo "8:0  1048576" > /sys/fs/cgroup/blkio/rg1/blkio.throttle.read_bps_device
将当前shell PID放到该组中
[root@150 opt]# echo $$ > /sys/fs/cgroup/blkio/rg1/tasks

再次读取, 速度被限制到1MB/S
[root@150 opt]# dd if=./test.img of=/dev/null bs=1k count=102400 iflag=direct
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 100.001 s, 1.0 MB/s

清除限制
[root@150 opt]# echo "8:0  0" > /sys/fs/cgroup/blkio/rg1/blkio.throttle.read_bps_device

清除后速度恢复
[root@150 opt]# dd if=./test.img of=/dev/null bs=1k count=102400 iflag=direct
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 7.32235 s, 14.3 MB/s

[参考]
目录
相关文章
|
5月前
|
缓存 监控 时序数据库
influxdb报错:cache-max-memory-size exceeded
influxdb报错:cache-max-memory-size exceeded
159 0
|
7月前
|
Oracle 关系型数据库
Customer RecommendedORA-27090 - Unable to Reserve Kernel Resources for Asynchronous Disk I/O
Customer RecommendedORA-27090 - Unable to Reserve Kernel Resources for Asynchronous Disk I/O
50 4
|
存储 缓存 大数据
Starrocks执行查询报错:Memory of process exceed limit. Used: XXX, Limit: XXX. Mem usage has exceed the limit of BE
Starrocks执行查询报错:Memory of process exceed limit. Used: XXX, Limit: XXX. Mem usage has exceed the limit of BE
|
Java Spring
Spring上传文件报错the request was rejected because its size (15920203) exceeds the configured maximum (104...
Spring上传文件报错the request was rejected because its size (15920203) exceeds the configured maximum (104...
1197 0
|
索引
Resource temporarily unavailable, will not index. Try --rotate option.
Resource temporarily unavailable, will not index. Try --rotate option.
174 1
|
缓存 数据可视化
'dict' object has no attribute '_txn_read_preference' && Sort exceeded memory limit of 10485760
'dict' object has no attribute '_txn_read_preference' && Sort exceeded memory limit of 10485760
212 0
|
监控
tailf报错limit of inotify watches was reached
tailf报错limit of inotify watches was reached
3540 0