18c2ecf20Sopenharmony_ci===================
28c2ecf20Sopenharmony_ciBlock IO Controller
38c2ecf20Sopenharmony_ci===================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciOverview
68c2ecf20Sopenharmony_ci========
78c2ecf20Sopenharmony_cicgroup subsys "blkio" implements the block io controller. There seems to be
88c2ecf20Sopenharmony_cia need of various kinds of IO control policies (like proportional BW, max BW)
98c2ecf20Sopenharmony_ciboth at leaf nodes as well as at intermediate nodes in a storage hierarchy.
108c2ecf20Sopenharmony_ciPlan is to use the same cgroup based management interface for blkio controller
118c2ecf20Sopenharmony_ciand based on user options switch IO policies in the background.
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciOne IO control policy is throttling policy which can be used to
148c2ecf20Sopenharmony_cispecify upper IO rate limits on devices. This policy is implemented in
158c2ecf20Sopenharmony_cigeneric block layer and can be used on leaf nodes as well as higher
168c2ecf20Sopenharmony_cilevel logical devices like device mapper.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciHOWTO
198c2ecf20Sopenharmony_ci=====
208c2ecf20Sopenharmony_ciThrottling/Upper Limit policy
218c2ecf20Sopenharmony_ci-----------------------------
228c2ecf20Sopenharmony_ci- Enable Block IO controller::
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	CONFIG_BLK_CGROUP=y
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci- Enable throttling in block layer::
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	CONFIG_BLK_DEV_THROTTLING=y
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci- Mount blkio controller (see cgroups.txt, Why are cgroups needed?)::
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci        mount -t cgroup -o blkio none /sys/fs/cgroup/blkio
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci- Specify a bandwidth rate on particular device for root group. The format
358c2ecf20Sopenharmony_ci  for policy is "<major>:<minor>  <bytes_per_second>"::
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci        echo "8:16  1048576" > /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci  Above will put a limit of 1MB/second on reads happening for root group
408c2ecf20Sopenharmony_ci  on device having major/minor number 8:16.
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci- Run dd to read a file and see if rate is throttled to 1MB/s or not::
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci        # dd iflag=direct if=/mnt/common/zerofile of=/dev/null bs=4K count=1024
458c2ecf20Sopenharmony_ci        1024+0 records in
468c2ecf20Sopenharmony_ci        1024+0 records out
478c2ecf20Sopenharmony_ci        4194304 bytes (4.2 MB) copied, 4.0001 s, 1.0 MB/s
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci Limits for writes can be put using blkio.throttle.write_bps_device file.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciHierarchical Cgroups
528c2ecf20Sopenharmony_ci====================
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciThrottling implements hierarchy support; however,
558c2ecf20Sopenharmony_cithrottling's hierarchy support is enabled iff "sane_behavior" is
568c2ecf20Sopenharmony_cienabled from cgroup side, which currently is a development option and
578c2ecf20Sopenharmony_cinot publicly available.
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciIf somebody created a hierarchy like as follows::
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci			root
628c2ecf20Sopenharmony_ci			/  \
638c2ecf20Sopenharmony_ci		     test1 test2
648c2ecf20Sopenharmony_ci			|
658c2ecf20Sopenharmony_ci		     test3
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciThrottling with "sane_behavior" will handle the
688c2ecf20Sopenharmony_cihierarchy correctly. For throttling, all limits apply
698c2ecf20Sopenharmony_cito the whole subtree while all statistics are local to the IOs
708c2ecf20Sopenharmony_cidirectly generated by tasks in that cgroup.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciThrottling without "sane_behavior" enabled from cgroup side will
738c2ecf20Sopenharmony_cipractically treat all groups at same level as if it looks like the
748c2ecf20Sopenharmony_cifollowing::
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci				pivot
778c2ecf20Sopenharmony_ci			     /  /   \  \
788c2ecf20Sopenharmony_ci			root  test1 test2  test3
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciVarious user visible config options
818c2ecf20Sopenharmony_ci===================================
828c2ecf20Sopenharmony_ciCONFIG_BLK_CGROUP
838c2ecf20Sopenharmony_ci	- Block IO controller.
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciCONFIG_BFQ_CGROUP_DEBUG
868c2ecf20Sopenharmony_ci	- Debug help. Right now some additional stats file show up in cgroup
878c2ecf20Sopenharmony_ci	  if this option is enabled.
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciCONFIG_BLK_DEV_THROTTLING
908c2ecf20Sopenharmony_ci	- Enable block device throttling support in block layer.
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciDetails of cgroup files
938c2ecf20Sopenharmony_ci=======================
948c2ecf20Sopenharmony_ciProportional weight policy files
958c2ecf20Sopenharmony_ci--------------------------------
968c2ecf20Sopenharmony_ci- blkio.weight
978c2ecf20Sopenharmony_ci	- Specifies per cgroup weight. This is default weight of the group
988c2ecf20Sopenharmony_ci	  on all the devices until and unless overridden by per device rule.
998c2ecf20Sopenharmony_ci	  (See blkio.weight_device).
1008c2ecf20Sopenharmony_ci	  Currently allowed range of weights is from 10 to 1000.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci- blkio.weight_device
1038c2ecf20Sopenharmony_ci	- One can specify per cgroup per device rules using this interface.
1048c2ecf20Sopenharmony_ci	  These rules override the default value of group weight as specified
1058c2ecf20Sopenharmony_ci	  by blkio.weight.
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	  Following is the format::
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	    # echo dev_maj:dev_minor weight > blkio.weight_device
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	  Configure weight=300 on /dev/sdb (8:16) in this cgroup::
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	    # echo 8:16 300 > blkio.weight_device
1148c2ecf20Sopenharmony_ci	    # cat blkio.weight_device
1158c2ecf20Sopenharmony_ci	    dev     weight
1168c2ecf20Sopenharmony_ci	    8:16    300
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	  Configure weight=500 on /dev/sda (8:0) in this cgroup::
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	    # echo 8:0 500 > blkio.weight_device
1218c2ecf20Sopenharmony_ci	    # cat blkio.weight_device
1228c2ecf20Sopenharmony_ci	    dev     weight
1238c2ecf20Sopenharmony_ci	    8:0     500
1248c2ecf20Sopenharmony_ci	    8:16    300
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	  Remove specific weight for /dev/sda in this cgroup::
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	    # echo 8:0 0 > blkio.weight_device
1298c2ecf20Sopenharmony_ci	    # cat blkio.weight_device
1308c2ecf20Sopenharmony_ci	    dev     weight
1318c2ecf20Sopenharmony_ci	    8:16    300
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci- blkio.time
1348c2ecf20Sopenharmony_ci	- disk time allocated to cgroup per device in milliseconds. First
1358c2ecf20Sopenharmony_ci	  two fields specify the major and minor number of the device and
1368c2ecf20Sopenharmony_ci	  third field specifies the disk time allocated to group in
1378c2ecf20Sopenharmony_ci	  milliseconds.
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci- blkio.sectors
1408c2ecf20Sopenharmony_ci	- number of sectors transferred to/from disk by the group. First
1418c2ecf20Sopenharmony_ci	  two fields specify the major and minor number of the device and
1428c2ecf20Sopenharmony_ci	  third field specifies the number of sectors transferred by the
1438c2ecf20Sopenharmony_ci	  group to/from the device.
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci- blkio.io_service_bytes
1468c2ecf20Sopenharmony_ci	- Number of bytes transferred to/from the disk by the group. These
1478c2ecf20Sopenharmony_ci	  are further divided by the type of operation - read or write, sync
1488c2ecf20Sopenharmony_ci	  or async. First two fields specify the major and minor number of the
1498c2ecf20Sopenharmony_ci	  device, third field specifies the operation type and the fourth field
1508c2ecf20Sopenharmony_ci	  specifies the number of bytes.
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci- blkio.io_serviced
1538c2ecf20Sopenharmony_ci	- Number of IOs (bio) issued to the disk by the group. These
1548c2ecf20Sopenharmony_ci	  are further divided by the type of operation - read or write, sync
1558c2ecf20Sopenharmony_ci	  or async. First two fields specify the major and minor number of the
1568c2ecf20Sopenharmony_ci	  device, third field specifies the operation type and the fourth field
1578c2ecf20Sopenharmony_ci	  specifies the number of IOs.
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci- blkio.io_service_time
1608c2ecf20Sopenharmony_ci	- Total amount of time between request dispatch and request completion
1618c2ecf20Sopenharmony_ci	  for the IOs done by this cgroup. This is in nanoseconds to make it
1628c2ecf20Sopenharmony_ci	  meaningful for flash devices too. For devices with queue depth of 1,
1638c2ecf20Sopenharmony_ci	  this time represents the actual service time. When queue_depth > 1,
1648c2ecf20Sopenharmony_ci	  that is no longer true as requests may be served out of order. This
1658c2ecf20Sopenharmony_ci	  may cause the service time for a given IO to include the service time
1668c2ecf20Sopenharmony_ci	  of multiple IOs when served out of order which may result in total
1678c2ecf20Sopenharmony_ci	  io_service_time > actual time elapsed. This time is further divided by
1688c2ecf20Sopenharmony_ci	  the type of operation - read or write, sync or async. First two fields
1698c2ecf20Sopenharmony_ci	  specify the major and minor number of the device, third field
1708c2ecf20Sopenharmony_ci	  specifies the operation type and the fourth field specifies the
1718c2ecf20Sopenharmony_ci	  io_service_time in ns.
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci- blkio.io_wait_time
1748c2ecf20Sopenharmony_ci	- Total amount of time the IOs for this cgroup spent waiting in the
1758c2ecf20Sopenharmony_ci	  scheduler queues for service. This can be greater than the total time
1768c2ecf20Sopenharmony_ci	  elapsed since it is cumulative io_wait_time for all IOs. It is not a
1778c2ecf20Sopenharmony_ci	  measure of total time the cgroup spent waiting but rather a measure of
1788c2ecf20Sopenharmony_ci	  the wait_time for its individual IOs. For devices with queue_depth > 1
1798c2ecf20Sopenharmony_ci	  this metric does not include the time spent waiting for service once
1808c2ecf20Sopenharmony_ci	  the IO is dispatched to the device but till it actually gets serviced
1818c2ecf20Sopenharmony_ci	  (there might be a time lag here due to re-ordering of requests by the
1828c2ecf20Sopenharmony_ci	  device). This is in nanoseconds to make it meaningful for flash
1838c2ecf20Sopenharmony_ci	  devices too. This time is further divided by the type of operation -
1848c2ecf20Sopenharmony_ci	  read or write, sync or async. First two fields specify the major and
1858c2ecf20Sopenharmony_ci	  minor number of the device, third field specifies the operation type
1868c2ecf20Sopenharmony_ci	  and the fourth field specifies the io_wait_time in ns.
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci- blkio.io_merged
1898c2ecf20Sopenharmony_ci	- Total number of bios/requests merged into requests belonging to this
1908c2ecf20Sopenharmony_ci	  cgroup. This is further divided by the type of operation - read or
1918c2ecf20Sopenharmony_ci	  write, sync or async.
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci- blkio.io_queued
1948c2ecf20Sopenharmony_ci	- Total number of requests queued up at any given instant for this
1958c2ecf20Sopenharmony_ci	  cgroup. This is further divided by the type of operation - read or
1968c2ecf20Sopenharmony_ci	  write, sync or async.
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci- blkio.avg_queue_size
1998c2ecf20Sopenharmony_ci	- Debugging aid only enabled if CONFIG_BFQ_CGROUP_DEBUG=y.
2008c2ecf20Sopenharmony_ci	  The average queue size for this cgroup over the entire time of this
2018c2ecf20Sopenharmony_ci	  cgroup's existence. Queue size samples are taken each time one of the
2028c2ecf20Sopenharmony_ci	  queues of this cgroup gets a timeslice.
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci- blkio.group_wait_time
2058c2ecf20Sopenharmony_ci	- Debugging aid only enabled if CONFIG_BFQ_CGROUP_DEBUG=y.
2068c2ecf20Sopenharmony_ci	  This is the amount of time the cgroup had to wait since it became busy
2078c2ecf20Sopenharmony_ci	  (i.e., went from 0 to 1 request queued) to get a timeslice for one of
2088c2ecf20Sopenharmony_ci	  its queues. This is different from the io_wait_time which is the
2098c2ecf20Sopenharmony_ci	  cumulative total of the amount of time spent by each IO in that cgroup
2108c2ecf20Sopenharmony_ci	  waiting in the scheduler queue. This is in nanoseconds. If this is
2118c2ecf20Sopenharmony_ci	  read when the cgroup is in a waiting (for timeslice) state, the stat
2128c2ecf20Sopenharmony_ci	  will only report the group_wait_time accumulated till the last time it
2138c2ecf20Sopenharmony_ci	  got a timeslice and will not include the current delta.
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci- blkio.empty_time
2168c2ecf20Sopenharmony_ci	- Debugging aid only enabled if CONFIG_BFQ_CGROUP_DEBUG=y.
2178c2ecf20Sopenharmony_ci	  This is the amount of time a cgroup spends without any pending
2188c2ecf20Sopenharmony_ci	  requests when not being served, i.e., it does not include any time
2198c2ecf20Sopenharmony_ci	  spent idling for one of the queues of the cgroup. This is in
2208c2ecf20Sopenharmony_ci	  nanoseconds. If this is read when the cgroup is in an empty state,
2218c2ecf20Sopenharmony_ci	  the stat will only report the empty_time accumulated till the last
2228c2ecf20Sopenharmony_ci	  time it had a pending request and will not include the current delta.
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci- blkio.idle_time
2258c2ecf20Sopenharmony_ci	- Debugging aid only enabled if CONFIG_BFQ_CGROUP_DEBUG=y.
2268c2ecf20Sopenharmony_ci	  This is the amount of time spent by the IO scheduler idling for a
2278c2ecf20Sopenharmony_ci	  given cgroup in anticipation of a better request than the existing ones
2288c2ecf20Sopenharmony_ci	  from other queues/cgroups. This is in nanoseconds. If this is read
2298c2ecf20Sopenharmony_ci	  when the cgroup is in an idling state, the stat will only report the
2308c2ecf20Sopenharmony_ci	  idle_time accumulated till the last idle period and will not include
2318c2ecf20Sopenharmony_ci	  the current delta.
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci- blkio.dequeue
2348c2ecf20Sopenharmony_ci	- Debugging aid only enabled if CONFIG_BFQ_CGROUP_DEBUG=y. This
2358c2ecf20Sopenharmony_ci	  gives the statistics about how many a times a group was dequeued
2368c2ecf20Sopenharmony_ci	  from service tree of the device. First two fields specify the major
2378c2ecf20Sopenharmony_ci	  and minor number of the device and third field specifies the number
2388c2ecf20Sopenharmony_ci	  of times a group was dequeued from a particular device.
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci- blkio.*_recursive
2418c2ecf20Sopenharmony_ci	- Recursive version of various stats. These files show the
2428c2ecf20Sopenharmony_ci          same information as their non-recursive counterparts but
2438c2ecf20Sopenharmony_ci          include stats from all the descendant cgroups.
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ciThrottling/Upper limit policy files
2468c2ecf20Sopenharmony_ci-----------------------------------
2478c2ecf20Sopenharmony_ci- blkio.throttle.read_bps_device
2488c2ecf20Sopenharmony_ci	- Specifies upper limit on READ rate from the device. IO rate is
2498c2ecf20Sopenharmony_ci	  specified in bytes per second. Rules are per device. Following is
2508c2ecf20Sopenharmony_ci	  the format::
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	    echo "<major>:<minor>  <rate_bytes_per_second>" > /cgrp/blkio.throttle.read_bps_device
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci- blkio.throttle.write_bps_device
2558c2ecf20Sopenharmony_ci	- Specifies upper limit on WRITE rate to the device. IO rate is
2568c2ecf20Sopenharmony_ci	  specified in bytes per second. Rules are per device. Following is
2578c2ecf20Sopenharmony_ci	  the format::
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	    echo "<major>:<minor>  <rate_bytes_per_second>" > /cgrp/blkio.throttle.write_bps_device
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci- blkio.throttle.read_iops_device
2628c2ecf20Sopenharmony_ci	- Specifies upper limit on READ rate from the device. IO rate is
2638c2ecf20Sopenharmony_ci	  specified in IO per second. Rules are per device. Following is
2648c2ecf20Sopenharmony_ci	  the format::
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	   echo "<major>:<minor>  <rate_io_per_second>" > /cgrp/blkio.throttle.read_iops_device
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci- blkio.throttle.write_iops_device
2698c2ecf20Sopenharmony_ci	- Specifies upper limit on WRITE rate to the device. IO rate is
2708c2ecf20Sopenharmony_ci	  specified in io per second. Rules are per device. Following is
2718c2ecf20Sopenharmony_ci	  the format::
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	    echo "<major>:<minor>  <rate_io_per_second>" > /cgrp/blkio.throttle.write_iops_device
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ciNote: If both BW and IOPS rules are specified for a device, then IO is
2768c2ecf20Sopenharmony_ci      subjected to both the constraints.
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci- blkio.throttle.io_serviced
2798c2ecf20Sopenharmony_ci	- Number of IOs (bio) issued to the disk by the group. These
2808c2ecf20Sopenharmony_ci	  are further divided by the type of operation - read or write, sync
2818c2ecf20Sopenharmony_ci	  or async. First two fields specify the major and minor number of the
2828c2ecf20Sopenharmony_ci	  device, third field specifies the operation type and the fourth field
2838c2ecf20Sopenharmony_ci	  specifies the number of IOs.
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci- blkio.throttle.io_service_bytes
2868c2ecf20Sopenharmony_ci	- Number of bytes transferred to/from the disk by the group. These
2878c2ecf20Sopenharmony_ci	  are further divided by the type of operation - read or write, sync
2888c2ecf20Sopenharmony_ci	  or async. First two fields specify the major and minor number of the
2898c2ecf20Sopenharmony_ci	  device, third field specifies the operation type and the fourth field
2908c2ecf20Sopenharmony_ci	  specifies the number of bytes.
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ciCommon files among various policies
2938c2ecf20Sopenharmony_ci-----------------------------------
2948c2ecf20Sopenharmony_ci- blkio.reset_stats
2958c2ecf20Sopenharmony_ci	- Writing an int to this file will result in resetting all the stats
2968c2ecf20Sopenharmony_ci	  for that cgroup.
297