cfq-iosched.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. CFQ (Complete Fairness Queueing)
  2. ===============================
  3. The main aim of CFQ scheduler is to provide a fair allocation of the disk
  4. I/O bandwidth for all the processes which requests an I/O operation.
  5. CFQ maintains the per process queue for the processes which request I/O
  6. operation(synchronous requests). In case of asynchronous requests, all the
  7. requests from all the processes are batched together according to their
  8. process's I/O priority.
  9. CFQ ioscheduler tunables
  10. ========================
  11. slice_idle
  12. ----------
  13. This specifies how long CFQ should idle for next request on certain cfq queues
  14. (for sequential workloads) and service trees (for random workloads) before
  15. queue is expired and CFQ selects next queue to dispatch from.
  16. By default slice_idle is a non-zero value. That means by default we idle on
  17. queues/service trees. This can be very helpful on highly seeky media like
  18. single spindle SATA/SAS disks where we can cut down on overall number of
  19. seeks and see improved throughput.
  20. Setting slice_idle to 0 will remove all the idling on queues/service tree
  21. level and one should see an overall improved throughput on faster storage
  22. devices like multiple SATA/SAS disks in hardware RAID configuration. The down
  23. side is that isolation provided from WRITES also goes down and notion of
  24. IO priority becomes weaker.
  25. So depending on storage and workload, it might be useful to set slice_idle=0.
  26. In general I think for SATA/SAS disks and software RAID of SATA/SAS disks
  27. keeping slice_idle enabled should be useful. For any configurations where
  28. there are multiple spindles behind single LUN (Host based hardware RAID
  29. controller or for storage arrays), setting slice_idle=0 might end up in better
  30. throughput and acceptable latencies.
  31. back_seek_max
  32. -------------
  33. This specifies, given in Kbytes, the maximum "distance" for backward seeking.
  34. The distance is the amount of space from the current head location to the
  35. sectors that are backward in terms of distance.
  36. This parameter allows the scheduler to anticipate requests in the "backward"
  37. direction and consider them as being the "next" if they are within this
  38. distance from the current head location.
  39. back_seek_penalty
  40. -----------------
  41. This parameter is used to compute the cost of backward seeking. If the
  42. backward distance of request is just 1/back_seek_penalty from a "front"
  43. request, then the seeking cost of two requests is considered equivalent.
  44. So scheduler will not bias toward one or the other request (otherwise scheduler
  45. will bias toward front request). Default value of back_seek_penalty is 2.
  46. fifo_expire_async
  47. -----------------
  48. This parameter is used to set the timeout of asynchronous requests. Default
  49. value of this is 248ms.
  50. fifo_expire_sync
  51. ----------------
  52. This parameter is used to set the timeout of synchronous requests. Default
  53. value of this is 124ms. In case to favor synchronous requests over asynchronous
  54. one, this value should be decreased relative to fifo_expire_async.
  55. group_idle
  56. -----------
  57. This parameter forces idling at the CFQ group level instead of CFQ
  58. queue level. This was introduced after a bottleneck was observed
  59. in higher end storage due to idle on sequential queue and allow dispatch
  60. from a single queue. The idea with this parameter is that it can be run with
  61. slice_idle=0 and group_idle=8, so that idling does not happen on individual
  62. queues in the group but happens overall on the group and thus still keeps the
  63. IO controller working.
  64. Not idling on individual queues in the group will dispatch requests from
  65. multiple queues in the group at the same time and achieve higher throughput
  66. on higher end storage.
  67. Default value for this parameter is 8ms.
  68. latency
  69. -------
  70. This parameter is used to enable/disable the latency mode of the CFQ
  71. scheduler. If latency mode (called low_latency) is enabled, CFQ tries
  72. to recompute the slice time for each process based on the target_latency set
  73. for the system. This favors fairness over throughput. Disabling low
  74. latency (setting it to 0) ignores target latency, allowing each process in the
  75. system to get a full time slice.
  76. By default low latency mode is enabled.
  77. target_latency
  78. --------------
  79. This parameter is used to calculate the time slice for a process if cfq's
  80. latency mode is enabled. It will ensure that sync requests have an estimated
  81. latency. But if sequential workload is higher(e.g. sequential read),
  82. then to meet the latency constraints, throughput may decrease because of less
  83. time for each process to issue I/O request before the cfq queue is switched.
  84. Though this can be overcome by disabling the latency_mode, it may increase
  85. the read latency for some applications. This parameter allows for changing
  86. target_latency through the sysfs interface which can provide the balanced
  87. throughput and read latency.
  88. Default value for target_latency is 300ms.
  89. slice_async
  90. -----------
  91. This parameter is same as of slice_sync but for asynchronous queue. The
  92. default value is 40ms.
  93. slice_async_rq
  94. --------------
  95. This parameter is used to limit the dispatching of asynchronous request to
  96. device request queue in queue's slice time. The maximum number of request that
  97. are allowed to be dispatched also depends upon the io priority. Default value
  98. for this is 2.
  99. slice_sync
  100. ----------
  101. When a queue is selected for execution, the queues IO requests are only
  102. executed for a certain amount of time(time_slice) before switching to another
  103. queue. This parameter is used to calculate the time slice of synchronous
  104. queue.
  105. time_slice is computed using the below equation:-
  106. time_slice = slice_sync + (slice_sync/5 * (4 - prio)). To increase the
  107. time_slice of synchronous queue, increase the value of slice_sync. Default
  108. value is 100ms.
  109. quantum
  110. -------
  111. This specifies the number of request dispatched to the device queue. In a
  112. queue's time slice, a request will not be dispatched if the number of request
  113. in the device exceeds this parameter. This parameter is used for synchronous
  114. request.
  115. In case of storage with several disk, this setting can limit the parallel
  116. processing of request. Therefore, increasing the value can improve the
  117. performance although this can cause the latency of some I/O to increase due
  118. to more number of requests.
  119. CFQ Group scheduling
  120. ====================
  121. CFQ supports blkio cgroup and has "blkio." prefixed files in each
  122. blkio cgroup directory. It is weight-based and there are four knobs
  123. for configuration - weight[_device] and leaf_weight[_device].
  124. Internal cgroup nodes (the ones with children) can also have tasks in
  125. them, so the former two configure how much proportion the cgroup as a
  126. whole is entitled to at its parent's level while the latter two
  127. configure how much proportion the tasks in the cgroup have compared to
  128. its direct children.
  129. Another way to think about it is assuming that each internal node has
  130. an implicit leaf child node which hosts all the tasks whose weight is
  131. configured by leaf_weight[_device]. Let's assume a blkio hierarchy
  132. composed of five cgroups - root, A, B, AA and AB - with the following
  133. weights where the names represent the hierarchy.
  134. weight leaf_weight
  135. root : 125 125
  136. A : 500 750
  137. B : 250 500
  138. AA : 500 500
  139. AB : 1000 500
  140. root never has a parent making its weight is meaningless. For backward
  141. compatibility, weight is always kept in sync with leaf_weight. B, AA
  142. and AB have no child and thus its tasks have no children cgroup to
  143. compete with. They always get 100% of what the cgroup won at the
  144. parent level. Considering only the weights which matter, the hierarchy
  145. looks like the following.
  146. root
  147. / | \
  148. A B leaf
  149. 500 250 125
  150. / | \
  151. AA AB leaf
  152. 500 1000 750
  153. If all cgroups have active IOs and competing with each other, disk
  154. time will be distributed like the following.
  155. Distribution below root. The total active weight at this level is
  156. A:500 + B:250 + C:125 = 875.
  157. root-leaf : 125 / 875 =~ 14%
  158. A : 500 / 875 =~ 57%
  159. B(-leaf) : 250 / 875 =~ 28%
  160. A has children and further distributes its 57% among the children and
  161. the implicit leaf node. The total active weight at this level is
  162. AA:500 + AB:1000 + A-leaf:750 = 2250.
  163. A-leaf : ( 750 / 2250) * A =~ 19%
  164. AA(-leaf) : ( 500 / 2250) * A =~ 12%
  165. AB(-leaf) : (1000 / 2250) * A =~ 25%
  166. CFQ IOPS Mode for group scheduling
  167. ===================================
  168. Basic CFQ design is to provide priority based time slices. Higher priority
  169. process gets bigger time slice and lower priority process gets smaller time
  170. slice. Measuring time becomes harder if storage is fast and supports NCQ and
  171. it would be better to dispatch multiple requests from multiple cfq queues in
  172. request queue at a time. In such scenario, it is not possible to measure time
  173. consumed by single queue accurately.
  174. What is possible though is to measure number of requests dispatched from a
  175. single queue and also allow dispatch from multiple cfq queue at the same time.
  176. This effectively becomes the fairness in terms of IOPS (IO operations per
  177. second).
  178. If one sets slice_idle=0 and if storage supports NCQ, CFQ internally switches
  179. to IOPS mode and starts providing fairness in terms of number of requests
  180. dispatched. Note that this mode switching takes effect only for group
  181. scheduling. For non-cgroup users nothing should change.
  182. CFQ IO scheduler Idling Theory
  183. ===============================
  184. Idling on a queue is primarily about waiting for the next request to come
  185. on same queue after completion of a request. In this process CFQ will not
  186. dispatch requests from other cfq queues even if requests are pending there.
  187. The rationale behind idling is that it can cut down on number of seeks
  188. on rotational media. For example, if a process is doing dependent
  189. sequential reads (next read will come on only after completion of previous
  190. one), then not dispatching request from other queue should help as we
  191. did not move the disk head and kept on dispatching sequential IO from
  192. one queue.
  193. CFQ has following service trees and various queues are put on these trees.
  194. sync-idle sync-noidle async
  195. All cfq queues doing synchronous sequential IO go on to sync-idle tree.
  196. On this tree we idle on each queue individually.
  197. All synchronous non-sequential queues go on sync-noidle tree. Also any
  198. request which are marked with REQ_NOIDLE go on this service tree. On this
  199. tree we do not idle on individual queues instead idle on the whole group
  200. of queues or the tree. So if there are 4 queues waiting for IO to dispatch
  201. we will idle only once last queue has dispatched the IO and there is
  202. no more IO on this service tree.
  203. All async writes go on async service tree. There is no idling on async
  204. queues.
  205. CFQ has some optimizations for SSDs and if it detects a non-rotational
  206. media which can support higher queue depth (multiple requests at in
  207. flight at a time), then it cuts down on idling of individual queues and
  208. all the queues move to sync-noidle tree and only tree idle remains. This
  209. tree idling provides isolation with buffered write queues on async tree.
  210. FAQ
  211. ===
  212. Q1. Why to idle at all on queues marked with REQ_NOIDLE.
  213. A1. We only do tree idle (all queues on sync-noidle tree) on queues marked
  214. with REQ_NOIDLE. This helps in providing isolation with all the sync-idle
  215. queues. Otherwise in presence of many sequential readers, other
  216. synchronous IO might not get fair share of disk.
  217. For example, if there are 10 sequential readers doing IO and they get
  218. 100ms each. If a REQ_NOIDLE request comes in, it will be scheduled
  219. roughly after 1 second. If after completion of REQ_NOIDLE request we
  220. do not idle, and after a couple of milli seconds a another REQ_NOIDLE
  221. request comes in, again it will be scheduled after 1second. Repeat it
  222. and notice how a workload can lose its disk share and suffer due to
  223. multiple sequential readers.
  224. fsync can generate dependent IO where bunch of data is written in the
  225. context of fsync, and later some journaling data is written. Journaling
  226. data comes in only after fsync has finished its IO (atleast for ext4
  227. that seemed to be the case). Now if one decides not to idle on fsync
  228. thread due to REQ_NOIDLE, then next journaling write will not get
  229. scheduled for another second. A process doing small fsync, will suffer
  230. badly in presence of multiple sequential readers.
  231. Hence doing tree idling on threads using REQ_NOIDLE flag on requests
  232. provides isolation from multiple sequential readers and at the same
  233. time we do not idle on individual threads.
  234. Q2. When to specify REQ_NOIDLE
  235. A2. I would think whenever one is doing synchronous write and not expecting
  236. more writes to be dispatched from same context soon, should be able
  237. to specify REQ_NOIDLE on writes and that probably should work well for
  238. most of the cases.