使用cgroup限制磁盤io讀寫速率


 

在MySQL innobackupex全備期間,磁盤io基本被打滿,設置過 --throttle效果不明顯,

  --throttle=#        This option specifies a number of I/O operations (pairs
                      of read+write) per second.  It accepts an integer
                      argument.  It is passed directly to xtrabackup's
                      --throttle option.

 

以下是使用cgroup方式進行io讀寫的限制測試

1. 安裝和啟動cgroup

【centos6】
yum install libcgroup
service cgconfig start

【centos7】
yum install -y libcgroup-tools.x86_64
systemctl start cgconfig

 

說明:

centos6的cgroup掛載在/cgroup對應的路徑下

centos7的cgroup掛載在/sys/fs/cgroup對應的路徑下

可使用lssubsys -M 或者lssubsys -am命令查看

 

2. 設置測試的磁盤盤符和讀寫速率限制

io_read_limit=1048576
io_write_limit=1024
#比如我測試的讀寫文件都發生在/data文件系統里

filesystem_mounted='/data'

lsblk -d -n | awk '{print $1}' >all_disks

while read line
do
aaa=$(df -h | grep -w ${filesystem_mounted} | grep $line)
if [[ ! -z aaa ]];then
    disk_name=$line
fi
done < all_disks

#echo $disk_name

 說明:以上主要是為了獲取到裸盤的盤符,而非分區后的盤符,例如/data 文件系統對應的盤符是vdb1,這里獲取的是vdb

3. 創建讀寫控制組

cgcreate -g blkio:test_read
cgcreate -g blkio:test_write

 

4.  將io讀寫限制策略綁定在指定的磁盤驅動號上

disk_id=$(ls -l ${disk_name} | awk '{print $5,$6}' | sed 's/ //g' | tr ',' ':')
cgset -r blkio.throttle.read_bps_device="${disk_id} ${io_read_limit}" test_read
cgset -r blkio.throttle.write_bps_device="${disk_id} ${io_write_limit}" test_write

5. 確認配置的限制策略

cgget -r blkio.throttle.read_bps_device test_read
cgget -r blkio.throttle.write_bps_device test_write

6. 讀測試對比

dd if=/dev/vdb of=/dev/null
cgexec -g blkio:test_read dd if=/dev/vdb of=/dev/null

  

7. 寫測試對比

dd if=/dev/zero of=/data/testfile bs=512 count=100000 oflag=dsync
cgexec -g blkio:test_write dd if=/dev/zero of=/data/testfile bs=1024 count=1000 oflag=dsync

8. 將正在執行的進程添加到限制策略里

#比如正在執行的dd命令對應的pid是5306
cgclassify -g blkio:test_write 5306

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM