Path: blob/master/Documentation/cgroups/blkio-controller.txt
10821 views
Block IO Controller1===================2Overview3========4cgroup subsys "blkio" implements the block io controller. There seems to be5a need of various kinds of IO control policies (like proportional BW, max BW)6both at leaf nodes as well as at intermediate nodes in a storage hierarchy.7Plan is to use the same cgroup based management interface for blkio controller8and based on user options switch IO policies in the background.910Currently two IO control policies are implemented. First one is proportional11weight time based division of disk policy. It is implemented in CFQ. Hence12this policy takes effect only on leaf nodes when CFQ is being used. The second13one is throttling policy which can be used to specify upper IO rate limits14on devices. This policy is implemented in generic block layer and can be15used on leaf nodes as well as higher level logical devices like device mapper.1617HOWTO18=====19Proportional Weight division of bandwidth20-----------------------------------------21You can do a very simple testing of running two dd threads in two different22cgroups. Here is what you can do.2324- Enable Block IO controller25CONFIG_BLK_CGROUP=y2627- Enable group scheduling in CFQ28CONFIG_CFQ_GROUP_IOSCHED=y2930- Compile and boot into kernel and mount IO controller (blkio); see31cgroups.txt, Why are cgroups needed?.3233mount -t tmpfs cgroup_root /sys/fs/cgroup34mkdir /sys/fs/cgroup/blkio35mount -t cgroup -o blkio none /sys/fs/cgroup/blkio3637- Create two cgroups38mkdir -p /sys/fs/cgroup/blkio/test1/ /sys/fs/cgroup/blkio/test23940- Set weights of group test1 and test241echo 1000 > /sys/fs/cgroup/blkio/test1/blkio.weight42echo 500 > /sys/fs/cgroup/blkio/test2/blkio.weight4344- Create two same size files (say 512MB each) on same disk (file1, file2) and45launch two dd threads in different cgroup to read those files.4647sync48echo 3 > /proc/sys/vm/drop_caches4950dd if=/mnt/sdb/zerofile1 of=/dev/null &51echo $! > /sys/fs/cgroup/blkio/test1/tasks52cat /sys/fs/cgroup/blkio/test1/tasks5354dd if=/mnt/sdb/zerofile2 of=/dev/null &55echo $! > /sys/fs/cgroup/blkio/test2/tasks56cat /sys/fs/cgroup/blkio/test2/tasks5758- At macro level, first dd should finish first. To get more precise data, keep59on looking at (with the help of script), at blkio.disk_time and60blkio.disk_sectors files of both test1 and test2 groups. This will tell how61much disk time (in milli seconds), each group got and how many secotors each62group dispatched to the disk. We provide fairness in terms of disk time, so63ideally io.disk_time of cgroups should be in proportion to the weight.6465Throttling/Upper Limit policy66-----------------------------67- Enable Block IO controller68CONFIG_BLK_CGROUP=y6970- Enable throttling in block layer71CONFIG_BLK_DEV_THROTTLING=y7273- Mount blkio controller (see cgroups.txt, Why are cgroups needed?)74mount -t cgroup -o blkio none /sys/fs/cgroup/blkio7576- Specify a bandwidth rate on particular device for root group. The format77for policy is "<major>:<minor> <byes_per_second>".7879echo "8:16 1048576" > /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device8081Above will put a limit of 1MB/second on reads happening for root group82on device having major/minor number 8:16.8384- Run dd to read a file and see if rate is throttled to 1MB/s or not.8586# dd if=/mnt/common/zerofile of=/dev/null bs=4K count=102487# iflag=direct881024+0 records in891024+0 records out904194304 bytes (4.2 MB) copied, 4.0001 s, 1.0 MB/s9192Limits for writes can be put using blkio.throttle.write_bps_device file.9394Hierarchical Cgroups95====================96- Currently none of the IO control policy supports hierarhical groups. But97cgroup interface does allow creation of hierarhical cgroups and internally98IO policies treat them as flat hierarchy.99100So this patch will allow creation of cgroup hierarhcy but at the backend101everything will be treated as flat. So if somebody created a hierarchy like102as follows.103104root105/ \106test1 test2107|108test3109110CFQ and throttling will practically treat all groups at same level.111112pivot113/ / \ \114root test1 test2 test3115116Down the line we can implement hierarchical accounting/control support117and also introduce a new cgroup file "use_hierarchy" which will control118whether cgroup hierarchy is viewed as flat or hierarchical by the policy..119This is how memory controller also has implemented the things.120121Various user visible config options122===================================123CONFIG_BLK_CGROUP124- Block IO controller.125126CONFIG_DEBUG_BLK_CGROUP127- Debug help. Right now some additional stats file show up in cgroup128if this option is enabled.129130CONFIG_CFQ_GROUP_IOSCHED131- Enables group scheduling in CFQ. Currently only 1 level of group132creation is allowed.133134CONFIG_BLK_DEV_THROTTLING135- Enable block device throttling support in block layer.136137Details of cgroup files138=======================139Proportional weight policy files140--------------------------------141- blkio.weight142- Specifies per cgroup weight. This is default weight of the group143on all the devices until and unless overridden by per device rule.144(See blkio.weight_device).145Currently allowed range of weights is from 10 to 1000.146147- blkio.weight_device148- One can specify per cgroup per device rules using this interface.149These rules override the default value of group weight as specified150by blkio.weight.151152Following is the format.153154# echo dev_maj:dev_minor weight > blkio.weight_device155Configure weight=300 on /dev/sdb (8:16) in this cgroup156# echo 8:16 300 > blkio.weight_device157# cat blkio.weight_device158dev weight1598:16 300160161Configure weight=500 on /dev/sda (8:0) in this cgroup162# echo 8:0 500 > blkio.weight_device163# cat blkio.weight_device164dev weight1658:0 5001668:16 300167168Remove specific weight for /dev/sda in this cgroup169# echo 8:0 0 > blkio.weight_device170# cat blkio.weight_device171dev weight1728:16 300173174- blkio.time175- disk time allocated to cgroup per device in milliseconds. First176two fields specify the major and minor number of the device and177third field specifies the disk time allocated to group in178milliseconds.179180- blkio.sectors181- number of sectors transferred to/from disk by the group. First182two fields specify the major and minor number of the device and183third field specifies the number of sectors transferred by the184group to/from the device.185186- blkio.io_service_bytes187- Number of bytes transferred to/from the disk by the group. These188are further divided by the type of operation - read or write, sync189or async. First two fields specify the major and minor number of the190device, third field specifies the operation type and the fourth field191specifies the number of bytes.192193- blkio.io_serviced194- Number of IOs completed to/from the disk by the group. These195are further divided by the type of operation - read or write, sync196or async. First two fields specify the major and minor number of the197device, third field specifies the operation type and the fourth field198specifies the number of IOs.199200- blkio.io_service_time201- Total amount of time between request dispatch and request completion202for the IOs done by this cgroup. This is in nanoseconds to make it203meaningful for flash devices too. For devices with queue depth of 1,204this time represents the actual service time. When queue_depth > 1,205that is no longer true as requests may be served out of order. This206may cause the service time for a given IO to include the service time207of multiple IOs when served out of order which may result in total208io_service_time > actual time elapsed. This time is further divided by209the type of operation - read or write, sync or async. First two fields210specify the major and minor number of the device, third field211specifies the operation type and the fourth field specifies the212io_service_time in ns.213214- blkio.io_wait_time215- Total amount of time the IOs for this cgroup spent waiting in the216scheduler queues for service. This can be greater than the total time217elapsed since it is cumulative io_wait_time for all IOs. It is not a218measure of total time the cgroup spent waiting but rather a measure of219the wait_time for its individual IOs. For devices with queue_depth > 1220this metric does not include the time spent waiting for service once221the IO is dispatched to the device but till it actually gets serviced222(there might be a time lag here due to re-ordering of requests by the223device). This is in nanoseconds to make it meaningful for flash224devices too. This time is further divided by the type of operation -225read or write, sync or async. First two fields specify the major and226minor number of the device, third field specifies the operation type227and the fourth field specifies the io_wait_time in ns.228229- blkio.io_merged230- Total number of bios/requests merged into requests belonging to this231cgroup. This is further divided by the type of operation - read or232write, sync or async.233234- blkio.io_queued235- Total number of requests queued up at any given instant for this236cgroup. This is further divided by the type of operation - read or237write, sync or async.238239- blkio.avg_queue_size240- Debugging aid only enabled if CONFIG_DEBUG_BLK_CGROUP=y.241The average queue size for this cgroup over the entire time of this242cgroup's existence. Queue size samples are taken each time one of the243queues of this cgroup gets a timeslice.244245- blkio.group_wait_time246- Debugging aid only enabled if CONFIG_DEBUG_BLK_CGROUP=y.247This is the amount of time the cgroup had to wait since it became busy248(i.e., went from 0 to 1 request queued) to get a timeslice for one of249its queues. This is different from the io_wait_time which is the250cumulative total of the amount of time spent by each IO in that cgroup251waiting in the scheduler queue. This is in nanoseconds. If this is252read when the cgroup is in a waiting (for timeslice) state, the stat253will only report the group_wait_time accumulated till the last time it254got a timeslice and will not include the current delta.255256- blkio.empty_time257- Debugging aid only enabled if CONFIG_DEBUG_BLK_CGROUP=y.258This is the amount of time a cgroup spends without any pending259requests when not being served, i.e., it does not include any time260spent idling for one of the queues of the cgroup. This is in261nanoseconds. If this is read when the cgroup is in an empty state,262the stat will only report the empty_time accumulated till the last263time it had a pending request and will not include the current delta.264265- blkio.idle_time266- Debugging aid only enabled if CONFIG_DEBUG_BLK_CGROUP=y.267This is the amount of time spent by the IO scheduler idling for a268given cgroup in anticipation of a better request than the exising ones269from other queues/cgroups. This is in nanoseconds. If this is read270when the cgroup is in an idling state, the stat will only report the271idle_time accumulated till the last idle period and will not include272the current delta.273274- blkio.dequeue275- Debugging aid only enabled if CONFIG_DEBUG_BLK_CGROUP=y. This276gives the statistics about how many a times a group was dequeued277from service tree of the device. First two fields specify the major278and minor number of the device and third field specifies the number279of times a group was dequeued from a particular device.280281Throttling/Upper limit policy files282-----------------------------------283- blkio.throttle.read_bps_device284- Specifies upper limit on READ rate from the device. IO rate is285specified in bytes per second. Rules are per deivce. Following is286the format.287288echo "<major>:<minor> <rate_bytes_per_second>" > /cgrp/blkio.throttle.read_bps_device289290- blkio.throttle.write_bps_device291- Specifies upper limit on WRITE rate to the device. IO rate is292specified in bytes per second. Rules are per deivce. Following is293the format.294295echo "<major>:<minor> <rate_bytes_per_second>" > /cgrp/blkio.throttle.write_bps_device296297- blkio.throttle.read_iops_device298- Specifies upper limit on READ rate from the device. IO rate is299specified in IO per second. Rules are per deivce. Following is300the format.301302echo "<major>:<minor> <rate_io_per_second>" > /cgrp/blkio.throttle.read_iops_device303304- blkio.throttle.write_iops_device305- Specifies upper limit on WRITE rate to the device. IO rate is306specified in io per second. Rules are per deivce. Following is307the format.308309echo "<major>:<minor> <rate_io_per_second>" > /cgrp/blkio.throttle.write_iops_device310311Note: If both BW and IOPS rules are specified for a device, then IO is312subjectd to both the constraints.313314- blkio.throttle.io_serviced315- Number of IOs (bio) completed to/from the disk by the group (as316seen by throttling policy). These are further divided by the type317of operation - read or write, sync or async. First two fields specify318the major and minor number of the device, third field specifies the319operation type and the fourth field specifies the number of IOs.320321blkio.io_serviced does accounting as seen by CFQ and counts are in322number of requests (struct request). On the other hand,323blkio.throttle.io_serviced counts number of IO in terms of number324of bios as seen by throttling policy. These bios can later be325merged by elevator and total number of requests completed can be326lesser.327328- blkio.throttle.io_service_bytes329- Number of bytes transferred to/from the disk by the group. These330are further divided by the type of operation - read or write, sync331or async. First two fields specify the major and minor number of the332device, third field specifies the operation type and the fourth field333specifies the number of bytes.334335These numbers should roughly be same as blkio.io_service_bytes as336updated by CFQ. The difference between two is that337blkio.io_service_bytes will not be updated if CFQ is not operating338on request queue.339340Common files among various policies341-----------------------------------342- blkio.reset_stats343- Writing an int to this file will result in resetting all the stats344for that cgroup.345346CFQ sysfs tunable347=================348/sys/block/<disk>/queue/iosched/slice_idle349------------------------------------------350On a faster hardware CFQ can be slow, especially with sequential workload.351This happens because CFQ idles on a single queue and single queue might not352drive deeper request queue depths to keep the storage busy. In such scenarios353one can try setting slice_idle=0 and that would switch CFQ to IOPS354(IO operations per second) mode on NCQ supporting hardware.355356That means CFQ will not idle between cfq queues of a cfq group and hence be357able to driver higher queue depth and achieve better throughput. That also358means that cfq provides fairness among groups in terms of IOPS and not in359terms of disk time.360361/sys/block/<disk>/queue/iosched/group_idle362------------------------------------------363If one disables idling on individual cfq queues and cfq service trees by364setting slice_idle=0, group_idle kicks in. That means CFQ will still idle365on the group in an attempt to provide fairness among groups.366367By default group_idle is same as slice_idle and does not do anything if368slice_idle is enabled.369370One can experience an overall throughput drop if you have created multiple371groups and put applications in that group which are not driving enough372IO to keep disk busy. In that case set group_idle=0, and CFQ will not idle373on individual groups and throughput should improve.374375What works376==========377- Currently only sync IO queues are support. All the buffered writes are378still system wide and not per group. Hence we will not see service379differentiation between buffered writes between groups.380381382