cfq-iosched.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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(syncronous 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. slice_async
  56. -----------
  57. This parameter is same as of slice_sync but for asynchronous queue. The
  58. default value is 40ms.
  59. slice_async_rq
  60. --------------
  61. This parameter is used to limit the dispatching of asynchronous request to
  62. device request queue in queue's slice time. The maximum number of request that
  63. are allowed to be dispatched also depends upon the io priority. Default value
  64. for this is 2.
  65. slice_sync
  66. ----------
  67. When a queue is selected for execution, the queues IO requests are only
  68. executed for a certain amount of time(time_slice) before switching to another
  69. queue. This parameter is used to calculate the time slice of synchronous
  70. queue.
  71. time_slice is computed using the below equation:-
  72. time_slice = slice_sync + (slice_sync/5 * (4 - prio)). To increase the
  73. time_slice of synchronous queue, increase the value of slice_sync. Default
  74. value is 100ms.
  75. quantum
  76. -------
  77. This specifies the number of request dispatched to the device queue. In a
  78. queue's time slice, a request will not be dispatched if the number of request
  79. in the device exceeds this parameter. This parameter is used for synchronous
  80. request.
  81. In case of storage with several disk, this setting can limit the parallel
  82. processing of request. Therefore, increasing the value can imporve the
  83. performace although this can cause the latency of some I/O to increase due
  84. to more number of requests.
  85. CFQ IOPS Mode for group scheduling
  86. ===================================
  87. Basic CFQ design is to provide priority based time slices. Higher priority
  88. process gets bigger time slice and lower priority process gets smaller time
  89. slice. Measuring time becomes harder if storage is fast and supports NCQ and
  90. it would be better to dispatch multiple requests from multiple cfq queues in
  91. request queue at a time. In such scenario, it is not possible to measure time
  92. consumed by single queue accurately.
  93. What is possible though is to measure number of requests dispatched from a
  94. single queue and also allow dispatch from multiple cfq queue at the same time.
  95. This effectively becomes the fairness in terms of IOPS (IO operations per
  96. second).
  97. If one sets slice_idle=0 and if storage supports NCQ, CFQ internally switches
  98. to IOPS mode and starts providing fairness in terms of number of requests
  99. dispatched. Note that this mode switching takes effect only for group
  100. scheduling. For non-cgroup users nothing should change.
  101. CFQ IO scheduler Idling Theory
  102. ===============================
  103. Idling on a queue is primarily about waiting for the next request to come
  104. on same queue after completion of a request. In this process CFQ will not
  105. dispatch requests from other cfq queues even if requests are pending there.
  106. The rationale behind idling is that it can cut down on number of seeks
  107. on rotational media. For example, if a process is doing dependent
  108. sequential reads (next read will come on only after completion of previous
  109. one), then not dispatching request from other queue should help as we
  110. did not move the disk head and kept on dispatching sequential IO from
  111. one queue.
  112. CFQ has following service trees and various queues are put on these trees.
  113. sync-idle sync-noidle async
  114. All cfq queues doing synchronous sequential IO go on to sync-idle tree.
  115. On this tree we idle on each queue individually.
  116. All synchronous non-sequential queues go on sync-noidle tree. Also any
  117. request which are marked with REQ_NOIDLE go on this service tree. On this
  118. tree we do not idle on individual queues instead idle on the whole group
  119. of queues or the tree. So if there are 4 queues waiting for IO to dispatch
  120. we will idle only once last queue has dispatched the IO and there is
  121. no more IO on this service tree.
  122. All async writes go on async service tree. There is no idling on async
  123. queues.
  124. CFQ has some optimizations for SSDs and if it detects a non-rotational
  125. media which can support higher queue depth (multiple requests at in
  126. flight at a time), then it cuts down on idling of individual queues and
  127. all the queues move to sync-noidle tree and only tree idle remains. This
  128. tree idling provides isolation with buffered write queues on async tree.
  129. FAQ
  130. ===
  131. Q1. Why to idle at all on queues marked with REQ_NOIDLE.
  132. A1. We only do tree idle (all queues on sync-noidle tree) on queues marked
  133. with REQ_NOIDLE. This helps in providing isolation with all the sync-idle
  134. queues. Otherwise in presence of many sequential readers, other
  135. synchronous IO might not get fair share of disk.
  136. For example, if there are 10 sequential readers doing IO and they get
  137. 100ms each. If a REQ_NOIDLE request comes in, it will be scheduled
  138. roughly after 1 second. If after completion of REQ_NOIDLE request we
  139. do not idle, and after a couple of milli seconds a another REQ_NOIDLE
  140. request comes in, again it will be scheduled after 1second. Repeat it
  141. and notice how a workload can lose its disk share and suffer due to
  142. multiple sequential readers.
  143. fsync can generate dependent IO where bunch of data is written in the
  144. context of fsync, and later some journaling data is written. Journaling
  145. data comes in only after fsync has finished its IO (atleast for ext4
  146. that seemed to be the case). Now if one decides not to idle on fsync
  147. thread due to REQ_NOIDLE, then next journaling write will not get
  148. scheduled for another second. A process doing small fsync, will suffer
  149. badly in presence of multiple sequential readers.
  150. Hence doing tree idling on threads using REQ_NOIDLE flag on requests
  151. provides isolation from multiple sequential readers and at the same
  152. time we do not idle on individual threads.
  153. Q2. When to specify REQ_NOIDLE
  154. A2. I would think whenever one is doing synchronous write and not expecting
  155. more writes to be dispatched from same context soon, should be able
  156. to specify REQ_NOIDLE on writes and that probably should work well for
  157. most of the cases.