zoukankan      html  css  js  c++  java
  • 【转载】Linux cgroup资源隔离各个击破之

    Linux Cgroup blkio子系统的用法.

     

    blkio子系统支持的两种IO隔离策略

    .1. (Completely Fair Queuing 完全公平队列)cfq io调度策略,支持按权重分配IO处理的时间片,从而达到IO在各资源组直接的调度和限制的目的,权重取值范围100-1000。
    通过以下两个文件进行配置。

    blkio.weight                默认值
    blkio.weight_device   块设备级的值 (major:minor weight) (优先级高于blkio.weight)
    

    例子

    echo 500 > blkio.weight
    echo 8:0 500 > blkio.weight_device
    

    cfq 调度器以及其他IO调度器的介绍详见内核文档.
    Documentation/block/

    .2. 限制IOPS使用上限
    例子如下

    bytes/s
    echo "8:0 10485760" > /cgroup/blkio/test/blkio.throttle.read_bps_device
    io/s
    echo "8:0 10" > /cgroup/blkio/test/blkio.throttle.read_iops_device
    bytes/s
    echo "8:0 10485760" > /cgroup/blkio/test/blkio.throttle.write_bps_device
    io/s
    echo "8:0 10" > /cgroup/blkio/test/blkio.throttle.write_iops_device
    



    这两种资源限制手段各有特色,CFQ的方法比较公平,互相不会干扰,在确保最低使用IO比例的情况下,如果IO设备空闲,还能超限使用。 (一句话就是保证最低的 IOPS)
    限制IOPS上限的方法,所有组加起来的IOPS可以超出块设备的最大IOPS指标,实现错峰使用的目的,但是坏处是如果超出最大指标太多,平时可能造成争抢导致SLA无法保证。 (一句话就是限制最高的 IOPS)



    注意,当前BLKIO子系统不统计buffered write操作,仅仅统计direct i/o的操作。但是buffered read是统计在内的。

    blkio子系统的统计报告

    blkio.throttle.io_serviced  报告iops,含当前队列中的。
    major, minor, operation(read, write, sync, or async), and numbers.
    
    blkio.throttle.io_service_bytes  报告bps,含当前队列中的。
    major, minor, operation(read, write, sync, or async), and bytes.
    
    blkio.time  报告设备I/O使用时间
    major, minor, and time. (ms)
    
    blkio.sectors  报告读入或读出的扇区数量
    major, minor, and sectors.
    
    blkio.avg_queue_size  报告块设备的平均队列大小(内核CONFIG_DEBUG_BLK_CGROUP=y 必须设置此宏)
    blkio.group_wait_time  报告队列等待时间片的总等待时间(ns)  (内核CONFIG_DEBUG_BLK_CGROUP=y 必须设置此宏)
    blkio.empty_time  报告块设备空闲时间(没有pending 请求)(ns) (内核CONFIG_DEBUG_BLK_CGROUP=y 必须设置此宏)
    blkio.idle_time  报告块设备空闲时间(等待请求合并?)(ns) (内核CONFIG_DEBUG_BLK_CGROUP=y 必须设置此宏)
    blkio.dequeue  报告块设备请求出列次数major, minor, and number  (内核CONFIG_DEBUG_BLK_CGROUP=y 必须设置此宏)
    blkio.io_serviced  报告IOPS,major, minor, operation(read, write, sync, or async), and numbers. 不含当前队列中的。
    blkio.io_service_bytes  报告bps,major, minor, operation(read, write, sync, or async), and bytes. 不含当前队列中的。
    blkio.io_service_time   报告从发送IO请求到IO完成的时间。major, minor, operation(read, write, sync, or async), and time(ns).  
    blkio.io_wait_time      报告IO等待的时间,可能会超出总的时间线,因为同时可能有多个IO请求在等待。  major, minor, operation(read, write, sync, or async), and time(ns).  
    blkio.io_merged         报告IO合并的次数,operation(read, write, sync, or async), and numbers.
    blkio.io_queued         报告IO入列的次数,operation(read, write, sync, or async), and numbers.
    

    blkio子系统的使用例子

    .1. 通过权重的方法限制IO的使用比例。

    挂载blkio子系统
    Mount the blkio subsystem:
    ~]# mount -t cgroup -o blkio blkio /cgroup/blkio/
    
    创建两个资源组
    Create two cgroups for the blkio subsystem:
    ~]# mkdir /cgroup/blkio/test1/
    ~]# mkdir /cgroup/blkio/test2/
    
    设置每个组的IO权重
    Set blkio weights in the previously created cgroups:
    ~]# echo 1000 > /cgroup/blkio/test1/blkio.weight
    ~]# echo 500 > /cgroup/blkio/test2/blkio.weight
    
    创建两个一样大的文件
    ~]# dd if=/dev/zero of=file_1 bs=1M count=4000
    ~]# dd if=/dev/zero of=file_2 bs=1M count=4000
    
    把文件的page cache刷出去  
    ~]# sync
    ~]# echo 3 > /proc/sys/vm/drop_caches
    
    启动两个读文件的进程,分别在两个资源组中。  
    ~]# cgexec -g blkio:test1 time dd if=file_1 of=/dev/null
    ~]# cgexec -g blkio:test2 time dd if=file_2 of=/dev/null
    
    查看IOTOP,可以看到权重均匀。  
    # iotop
    Total DISK READ: 83.16 M/s | Total DISK WRITE: 0.00 B/s
        TIME  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN      IO    COMMAND
    15:18:04 15071 be/4 root       27.64 M/s    0.00 B/s  0.00 % 92.30 % dd if=file_2 of=/dev/null
    15:18:04 15069 be/4 root       55.52 M/s    0.00 B/s  0.00 % 88.48 % dd if=file_1 of=/dev/null
    



    blkio子系统中需要用到块设备的major, minor号,可以通过以下方法查看

    # ll /dev/|grep "^b"
    brw-rw----  1 root cdrom    11,   0 Jun 10 15:50 sr0
    brw-rw----  1 root disk    253,   0 Jun 10 15:50 vda
    brw-rw----  1 root disk    253,   1 Jun 10 15:50 vda1
    

     

    CFQ调度公平性

    对于权重的方法,如果要对离散的I/O操作起到公平的调度,必须打开块设备的group_isolation设置。
    Additionally, you can enable group isolation which provides stronger isolation between groups at the expense of throughput. 
    When group isolation is disabled, fairness can be expected only for a sequential workload. 
    By default, group isolation is enabled and fairness can be expected for random I/O workloads as well. 
    To enable group isolation, use the following command:

    echo 1 > /sys/block/<disk_device>/queue/iosched/group_isolation
    

     

     

     
  • 相关阅读:
    linux系统防火墙对访问服务器的影响
    Android 样式的开发(转)
    Android开发学习总结(三)——appcompat_v7项目说明
    Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
    BootStrap 常用控件总结
    mybatis自定义代码生成器(Generator)——自动生成model&dao代码
    MySql的下载和安装(解压版)
    jquery mobile 表单提交 图片/文件 上传
    java读取.properties配置文件的几种方法
    AngularJS------认识AngularJS
  • 原文地址:https://www.cnblogs.com/conanwang/p/5848908.html
Copyright © 2011-2022 走看看