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