Merge tag 'gfs2-for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux...
[sfrench/cifs-2.6.git] / Documentation / block / bfq-iosched.txt
1 BFQ (Budget Fair Queueing)
2 ==========================
3
4 BFQ is a proportional-share I/O scheduler, with some extra
5 low-latency capabilities. In addition to cgroups support (blkio or io
6 controllers), BFQ's main features are:
7 - BFQ guarantees a high system and application responsiveness, and a
8   low latency for time-sensitive applications, such as audio or video
9   players;
10 - BFQ distributes bandwidth, and not just time, among processes or
11   groups (switching back to time distribution when needed to keep
12   throughput high).
13
14 In its default configuration, BFQ privileges latency over
15 throughput. So, when needed for achieving a lower latency, BFQ builds
16 schedules that may lead to a lower throughput. If your main or only
17 goal, for a given device, is to achieve the maximum-possible
18 throughput at all times, then do switch off all low-latency heuristics
19 for that device, by setting low_latency to 0. See Section 3 for
20 details on how to configure BFQ for the desired tradeoff between
21 latency and throughput, or on how to maximize throughput.
22
23 As every I/O scheduler, BFQ adds some overhead to per-I/O-request
24 processing. To give an idea of this overhead, the total,
25 single-lock-protected, per-request processing time of BFQ---i.e., the
26 sum of the execution times of the request insertion, dispatch and
27 completion hooks---is, e.g., 1.9 us on an Intel Core i7-2760QM@2.40GHz
28 (dated CPU for notebooks; time measured with simple code
29 instrumentation, and using the throughput-sync.sh script of the S
30 suite [1], in performance-profiling mode). To put this result into
31 context, the total, single-lock-protected, per-request execution time
32 of the lightest I/O scheduler available in blk-mq, mq-deadline, is 0.7
33 us (mq-deadline is ~800 LOC, against ~10500 LOC for BFQ).
34
35 Scheduling overhead further limits the maximum IOPS that a CPU can
36 process (already limited by the execution of the rest of the I/O
37 stack). To give an idea of the limits with BFQ, on slow or average
38 CPUs, here are, first, the limits of BFQ for three different CPUs, on,
39 respectively, an average laptop, an old desktop, and a cheap embedded
40 system, in case full hierarchical support is enabled (i.e.,
41 CONFIG_BFQ_GROUP_IOSCHED is set), but CONFIG_BFQ_CGROUP_DEBUG is not
42 set (Section 4-2):
43 - Intel i7-4850HQ: 400 KIOPS
44 - AMD A8-3850: 250 KIOPS
45 - ARM CortexTM-A53 Octa-core: 80 KIOPS
46
47 If CONFIG_BFQ_CGROUP_DEBUG is set (and of course full hierarchical
48 support is enabled), then the sustainable throughput with BFQ
49 decreases, because all blkio.bfq* statistics are created and updated
50 (Section 4-2). For BFQ, this leads to the following maximum
51 sustainable throughputs, on the same systems as above:
52 - Intel i7-4850HQ: 310 KIOPS
53 - AMD A8-3850: 200 KIOPS
54 - ARM CortexTM-A53 Octa-core: 56 KIOPS
55
56 BFQ works for multi-queue devices too.
57
58 The table of contents follow. Impatients can just jump to Section 3.
59
60 CONTENTS
61
62 1. When may BFQ be useful?
63  1-1 Personal systems
64  1-2 Server systems
65 2. How does BFQ work?
66 3. What are BFQ's tunables and how to properly configure BFQ?
67 4. BFQ group scheduling
68  4-1 Service guarantees provided
69  4-2 Interface
70
71 1. When may BFQ be useful?
72 ==========================
73
74 BFQ provides the following benefits on personal and server systems.
75
76 1-1 Personal systems
77 --------------------
78
79 Low latency for interactive applications
80
81 Regardless of the actual background workload, BFQ guarantees that, for
82 interactive tasks, the storage device is virtually as responsive as if
83 it was idle. For example, even if one or more of the following
84 background workloads are being executed:
85 - one or more large files are being read, written or copied,
86 - a tree of source files is being compiled,
87 - one or more virtual machines are performing I/O,
88 - a software update is in progress,
89 - indexing daemons are scanning filesystems and updating their
90   databases,
91 starting an application or loading a file from within an application
92 takes about the same time as if the storage device was idle. As a
93 comparison, with CFQ, NOOP or DEADLINE, and in the same conditions,
94 applications experience high latencies, or even become unresponsive
95 until the background workload terminates (also on SSDs).
96
97 Low latency for soft real-time applications
98
99 Also soft real-time applications, such as audio and video
100 players/streamers, enjoy a low latency and a low drop rate, regardless
101 of the background I/O workload. As a consequence, these applications
102 do not suffer from almost any glitch due to the background workload.
103
104 Higher speed for code-development tasks
105
106 If some additional workload happens to be executed in parallel, then
107 BFQ executes the I/O-related components of typical code-development
108 tasks (compilation, checkout, merge, ...) much more quickly than CFQ,
109 NOOP or DEADLINE.
110
111 High throughput
112
113 On hard disks, BFQ achieves up to 30% higher throughput than CFQ, and
114 up to 150% higher throughput than DEADLINE and NOOP, with all the
115 sequential workloads considered in our tests. With random workloads,
116 and with all the workloads on flash-based devices, BFQ achieves,
117 instead, about the same throughput as the other schedulers.
118
119 Strong fairness, bandwidth and delay guarantees
120
121 BFQ distributes the device throughput, and not just the device time,
122 among I/O-bound applications in proportion their weights, with any
123 workload and regardless of the device parameters. From these bandwidth
124 guarantees, it is possible to compute tight per-I/O-request delay
125 guarantees by a simple formula. If not configured for strict service
126 guarantees, BFQ switches to time-based resource sharing (only) for
127 applications that would otherwise cause a throughput loss.
128
129 1-2 Server systems
130 ------------------
131
132 Most benefits for server systems follow from the same service
133 properties as above. In particular, regardless of whether additional,
134 possibly heavy workloads are being served, BFQ guarantees:
135
136 . audio and video-streaming with zero or very low jitter and drop
137   rate;
138
139 . fast retrieval of WEB pages and embedded objects;
140
141 . real-time recording of data in live-dumping applications (e.g.,
142   packet logging);
143
144 . responsiveness in local and remote access to a server.
145
146
147 2. How does BFQ work?
148 =====================
149
150 BFQ is a proportional-share I/O scheduler, whose general structure,
151 plus a lot of code, are borrowed from CFQ.
152
153 - Each process doing I/O on a device is associated with a weight and a
154   (bfq_)queue.
155
156 - BFQ grants exclusive access to the device, for a while, to one queue
157   (process) at a time, and implements this service model by
158   associating every queue with a budget, measured in number of
159   sectors.
160
161   - After a queue is granted access to the device, the budget of the
162     queue is decremented, on each request dispatch, by the size of the
163     request.
164
165   - The in-service queue is expired, i.e., its service is suspended,
166     only if one of the following events occurs: 1) the queue finishes
167     its budget, 2) the queue empties, 3) a "budget timeout" fires.
168
169     - The budget timeout prevents processes doing random I/O from
170       holding the device for too long and dramatically reducing
171       throughput.
172
173     - Actually, as in CFQ, a queue associated with a process issuing
174       sync requests may not be expired immediately when it empties. In
175       contrast, BFQ may idle the device for a short time interval,
176       giving the process the chance to go on being served if it issues
177       a new request in time. Device idling typically boosts the
178       throughput on rotational devices and on non-queueing flash-based
179       devices, if processes do synchronous and sequential I/O. In
180       addition, under BFQ, device idling is also instrumental in
181       guaranteeing the desired throughput fraction to processes
182       issuing sync requests (see the description of the slice_idle
183       tunable in this document, or [1, 2], for more details).
184
185       - With respect to idling for service guarantees, if several
186         processes are competing for the device at the same time, but
187         all processes and groups have the same weight, then BFQ
188         guarantees the expected throughput distribution without ever
189         idling the device. Throughput is thus as high as possible in
190         this common scenario.
191
192      - On flash-based storage with internal queueing of commands
193        (typically NCQ), device idling happens to be always detrimental
194        for throughput. So, with these devices, BFQ performs idling
195        only when strictly needed for service guarantees, i.e., for
196        guaranteeing low latency or fairness. In these cases, overall
197        throughput may be sub-optimal. No solution currently exists to
198        provide both strong service guarantees and optimal throughput
199        on devices with internal queueing.
200
201   - If low-latency mode is enabled (default configuration), BFQ
202     executes some special heuristics to detect interactive and soft
203     real-time applications (e.g., video or audio players/streamers),
204     and to reduce their latency. The most important action taken to
205     achieve this goal is to give to the queues associated with these
206     applications more than their fair share of the device
207     throughput. For brevity, we call just "weight-raising" the whole
208     sets of actions taken by BFQ to privilege these queues. In
209     particular, BFQ provides a milder form of weight-raising for
210     interactive applications, and a stronger form for soft real-time
211     applications.
212
213   - BFQ automatically deactivates idling for queues born in a burst of
214     queue creations. In fact, these queues are usually associated with
215     the processes of applications and services that benefit mostly
216     from a high throughput. Examples are systemd during boot, or git
217     grep.
218
219   - As CFQ, BFQ merges queues performing interleaved I/O, i.e.,
220     performing random I/O that becomes mostly sequential if
221     merged. Differently from CFQ, BFQ achieves this goal with a more
222     reactive mechanism, called Early Queue Merge (EQM). EQM is so
223     responsive in detecting interleaved I/O (cooperating processes),
224     that it enables BFQ to achieve a high throughput, by queue
225     merging, even for queues for which CFQ needs a different
226     mechanism, preemption, to get a high throughput. As such EQM is a
227     unified mechanism to achieve a high throughput with interleaved
228     I/O.
229
230   - Queues are scheduled according to a variant of WF2Q+, named
231     B-WF2Q+, and implemented using an augmented rb-tree to preserve an
232     O(log N) overall complexity.  See [2] for more details. B-WF2Q+ is
233     also ready for hierarchical scheduling, details in Section 4.
234
235   - B-WF2Q+ guarantees a tight deviation with respect to an ideal,
236     perfectly fair, and smooth service. In particular, B-WF2Q+
237     guarantees that each queue receives a fraction of the device
238     throughput proportional to its weight, even if the throughput
239     fluctuates, and regardless of: the device parameters, the current
240     workload and the budgets assigned to the queue.
241
242   - The last, budget-independence, property (although probably
243     counterintuitive in the first place) is definitely beneficial, for
244     the following reasons:
245
246     - First, with any proportional-share scheduler, the maximum
247       deviation with respect to an ideal service is proportional to
248       the maximum budget (slice) assigned to queues. As a consequence,
249       BFQ can keep this deviation tight not only because of the
250       accurate service of B-WF2Q+, but also because BFQ *does not*
251       need to assign a larger budget to a queue to let the queue
252       receive a higher fraction of the device throughput.
253
254     - Second, BFQ is free to choose, for every process (queue), the
255       budget that best fits the needs of the process, or best
256       leverages the I/O pattern of the process. In particular, BFQ
257       updates queue budgets with a simple feedback-loop algorithm that
258       allows a high throughput to be achieved, while still providing
259       tight latency guarantees to time-sensitive applications. When
260       the in-service queue expires, this algorithm computes the next
261       budget of the queue so as to:
262
263       - Let large budgets be eventually assigned to the queues
264         associated with I/O-bound applications performing sequential
265         I/O: in fact, the longer these applications are served once
266         got access to the device, the higher the throughput is.
267
268       - Let small budgets be eventually assigned to the queues
269         associated with time-sensitive applications (which typically
270         perform sporadic and short I/O), because, the smaller the
271         budget assigned to a queue waiting for service is, the sooner
272         B-WF2Q+ will serve that queue (Subsec 3.3 in [2]).
273
274 - If several processes are competing for the device at the same time,
275   but all processes and groups have the same weight, then BFQ
276   guarantees the expected throughput distribution without ever idling
277   the device. It uses preemption instead. Throughput is then much
278   higher in this common scenario.
279
280 - ioprio classes are served in strict priority order, i.e.,
281   lower-priority queues are not served as long as there are
282   higher-priority queues.  Among queues in the same class, the
283   bandwidth is distributed in proportion to the weight of each
284   queue. A very thin extra bandwidth is however guaranteed to
285   the Idle class, to prevent it from starving.
286
287
288 3. What are BFQ's tunables and how to properly configure BFQ?
289 =============================================================
290
291 Most BFQ tunables affect service guarantees (basically latency and
292 fairness) and throughput. For full details on how to choose the
293 desired tradeoff between service guarantees and throughput, see the
294 parameters slice_idle, strict_guarantees and low_latency. For details
295 on how to maximise throughput, see slice_idle, timeout_sync and
296 max_budget. The other performance-related parameters have been
297 inherited from, and have been preserved mostly for compatibility with
298 CFQ. So far, no performance improvement has been reported after
299 changing the latter parameters in BFQ.
300
301 In particular, the tunables back_seek-max, back_seek_penalty,
302 fifo_expire_async and fifo_expire_sync below are the same as in
303 CFQ. Their description is just copied from that for CFQ. Some
304 considerations in the description of slice_idle are copied from CFQ
305 too.
306
307 per-process ioprio and weight
308 -----------------------------
309
310 Unless the cgroups interface is used (see "4. BFQ group scheduling"),
311 weights can be assigned to processes only indirectly, through I/O
312 priorities, and according to the relation:
313 weight = (IOPRIO_BE_NR - ioprio) * 10.
314
315 Beware that, if low-latency is set, then BFQ automatically raises the
316 weight of the queues associated with interactive and soft real-time
317 applications. Unset this tunable if you need/want to control weights.
318
319 slice_idle
320 ----------
321
322 This parameter specifies how long BFQ should idle for next I/O
323 request, when certain sync BFQ queues become empty. By default
324 slice_idle is a non-zero value. Idling has a double purpose: boosting
325 throughput and making sure that the desired throughput distribution is
326 respected (see the description of how BFQ works, and, if needed, the
327 papers referred there).
328
329 As for throughput, idling can be very helpful on highly seeky media
330 like single spindle SATA/SAS disks where we can cut down on overall
331 number of seeks and see improved throughput.
332
333 Setting slice_idle to 0 will remove all the idling on queues and one
334 should see an overall improved throughput on faster storage devices
335 like multiple SATA/SAS disks in hardware RAID configuration, as well
336 as flash-based storage with internal command queueing (and
337 parallelism).
338
339 So depending on storage and workload, it might be useful to set
340 slice_idle=0.  In general for SATA/SAS disks and software RAID of
341 SATA/SAS disks keeping slice_idle enabled should be useful. For any
342 configurations where there are multiple spindles behind single LUN
343 (Host based hardware RAID controller or for storage arrays), or with
344 flash-based fast storage, setting slice_idle=0 might end up in better
345 throughput and acceptable latencies.
346
347 Idling is however necessary to have service guarantees enforced in
348 case of differentiated weights or differentiated I/O-request lengths.
349 To see why, suppose that a given BFQ queue A must get several I/O
350 requests served for each request served for another queue B. Idling
351 ensures that, if A makes a new I/O request slightly after becoming
352 empty, then no request of B is dispatched in the middle, and thus A
353 does not lose the possibility to get more than one request dispatched
354 before the next request of B is dispatched. Note that idling
355 guarantees the desired differentiated treatment of queues only in
356 terms of I/O-request dispatches. To guarantee that the actual service
357 order then corresponds to the dispatch order, the strict_guarantees
358 tunable must be set too.
359
360 There is an important flipside for idling: apart from the above cases
361 where it is beneficial also for throughput, idling can severely impact
362 throughput. One important case is random workload. Because of this
363 issue, BFQ tends to avoid idling as much as possible, when it is not
364 beneficial also for throughput (as detailed in Section 2). As a
365 consequence of this behavior, and of further issues described for the
366 strict_guarantees tunable, short-term service guarantees may be
367 occasionally violated. And, in some cases, these guarantees may be
368 more important than guaranteeing maximum throughput. For example, in
369 video playing/streaming, a very low drop rate may be more important
370 than maximum throughput. In these cases, consider setting the
371 strict_guarantees parameter.
372
373 slice_idle_us
374 -------------
375
376 Controls the same tuning parameter as slice_idle, but in microseconds.
377 Either tunable can be used to set idling behavior.  Afterwards, the
378 other tunable will reflect the newly set value in sysfs.
379
380 strict_guarantees
381 -----------------
382
383 If this parameter is set (default: unset), then BFQ
384
385 - always performs idling when the in-service queue becomes empty;
386
387 - forces the device to serve one I/O request at a time, by dispatching a
388   new request only if there is no outstanding request.
389
390 In the presence of differentiated weights or I/O-request sizes, both
391 the above conditions are needed to guarantee that every BFQ queue
392 receives its allotted share of the bandwidth. The first condition is
393 needed for the reasons explained in the description of the slice_idle
394 tunable.  The second condition is needed because all modern storage
395 devices reorder internally-queued requests, which may trivially break
396 the service guarantees enforced by the I/O scheduler.
397
398 Setting strict_guarantees may evidently affect throughput.
399
400 back_seek_max
401 -------------
402
403 This specifies, given in Kbytes, the maximum "distance" for backward seeking.
404 The distance is the amount of space from the current head location to the
405 sectors that are backward in terms of distance.
406
407 This parameter allows the scheduler to anticipate requests in the "backward"
408 direction and consider them as being the "next" if they are within this
409 distance from the current head location.
410
411 back_seek_penalty
412 -----------------
413
414 This parameter is used to compute the cost of backward seeking. If the
415 backward distance of request is just 1/back_seek_penalty from a "front"
416 request, then the seeking cost of two requests is considered equivalent.
417
418 So scheduler will not bias toward one or the other request (otherwise scheduler
419 will bias toward front request). Default value of back_seek_penalty is 2.
420
421 fifo_expire_async
422 -----------------
423
424 This parameter is used to set the timeout of asynchronous requests. Default
425 value of this is 248ms.
426
427 fifo_expire_sync
428 ----------------
429
430 This parameter is used to set the timeout of synchronous requests. Default
431 value of this is 124ms. In case to favor synchronous requests over asynchronous
432 one, this value should be decreased relative to fifo_expire_async.
433
434 low_latency
435 -----------
436
437 This parameter is used to enable/disable BFQ's low latency mode. By
438 default, low latency mode is enabled. If enabled, interactive and soft
439 real-time applications are privileged and experience a lower latency,
440 as explained in more detail in the description of how BFQ works.
441
442 DISABLE this mode if you need full control on bandwidth
443 distribution. In fact, if it is enabled, then BFQ automatically
444 increases the bandwidth share of privileged applications, as the main
445 means to guarantee a lower latency to them.
446
447 In addition, as already highlighted at the beginning of this document,
448 DISABLE this mode if your only goal is to achieve a high throughput.
449 In fact, privileging the I/O of some application over the rest may
450 entail a lower throughput. To achieve the highest-possible throughput
451 on a non-rotational device, setting slice_idle to 0 may be needed too
452 (at the cost of giving up any strong guarantee on fairness and low
453 latency).
454
455 timeout_sync
456 ------------
457
458 Maximum amount of device time that can be given to a task (queue) once
459 it has been selected for service. On devices with costly seeks,
460 increasing this time usually increases maximum throughput. On the
461 opposite end, increasing this time coarsens the granularity of the
462 short-term bandwidth and latency guarantees, especially if the
463 following parameter is set to zero.
464
465 max_budget
466 ----------
467
468 Maximum amount of service, measured in sectors, that can be provided
469 to a BFQ queue once it is set in service (of course within the limits
470 of the above timeout). According to what said in the description of
471 the algorithm, larger values increase the throughput in proportion to
472 the percentage of sequential I/O requests issued. The price of larger
473 values is that they coarsen the granularity of short-term bandwidth
474 and latency guarantees.
475
476 The default value is 0, which enables auto-tuning: BFQ sets max_budget
477 to the maximum number of sectors that can be served during
478 timeout_sync, according to the estimated peak rate.
479
480 For specific devices, some users have occasionally reported to have
481 reached a higher throughput by setting max_budget explicitly, i.e., by
482 setting max_budget to a higher value than 0. In particular, they have
483 set max_budget to higher values than those to which BFQ would have set
484 it with auto-tuning. An alternative way to achieve this goal is to
485 just increase the value of timeout_sync, leaving max_budget equal to 0.
486
487 weights
488 -------
489
490 Read-only parameter, used to show the weights of the currently active
491 BFQ queues.
492
493
494 4. Group scheduling with BFQ
495 ============================
496
497 BFQ supports both cgroups-v1 and cgroups-v2 io controllers, namely
498 blkio and io. In particular, BFQ supports weight-based proportional
499 share. To activate cgroups support, set BFQ_GROUP_IOSCHED.
500
501 4-1 Service guarantees provided
502 -------------------------------
503
504 With BFQ, proportional share means true proportional share of the
505 device bandwidth, according to group weights. For example, a group
506 with weight 200 gets twice the bandwidth, and not just twice the time,
507 of a group with weight 100.
508
509 BFQ supports hierarchies (group trees) of any depth. Bandwidth is
510 distributed among groups and processes in the expected way: for each
511 group, the children of the group share the whole bandwidth of the
512 group in proportion to their weights. In particular, this implies
513 that, for each leaf group, every process of the group receives the
514 same share of the whole group bandwidth, unless the ioprio of the
515 process is modified.
516
517 The resource-sharing guarantee for a group may partially or totally
518 switch from bandwidth to time, if providing bandwidth guarantees to
519 the group lowers the throughput too much. This switch occurs on a
520 per-process basis: if a process of a leaf group causes throughput loss
521 if served in such a way to receive its share of the bandwidth, then
522 BFQ switches back to just time-based proportional share for that
523 process.
524
525 4-2 Interface
526 -------------
527
528 To get proportional sharing of bandwidth with BFQ for a given device,
529 BFQ must of course be the active scheduler for that device.
530
531 Within each group directory, the names of the files associated with
532 BFQ-specific cgroup parameters and stats begin with the "bfq."
533 prefix. So, with cgroups-v1 or cgroups-v2, the full prefix for
534 BFQ-specific files is "blkio.bfq." or "io.bfq." For example, the group
535 parameter to set the weight of a group with BFQ is blkio.bfq.weight
536 or io.bfq.weight.
537
538 As for cgroups-v1 (blkio controller), the exact set of stat files
539 created, and kept up-to-date by bfq, depends on whether
540 CONFIG_BFQ_CGROUP_DEBUG is set. If it is set, then bfq creates all
541 the stat files documented in
542 Documentation/cgroup-v1/blkio-controller.rst. If, instead,
543 CONFIG_BFQ_CGROUP_DEBUG is not set, then bfq creates only the files
544 blkio.bfq.io_service_bytes
545 blkio.bfq.io_service_bytes_recursive
546 blkio.bfq.io_serviced
547 blkio.bfq.io_serviced_recursive
548
549 The value of CONFIG_BFQ_CGROUP_DEBUG greatly influences the maximum
550 throughput sustainable with bfq, because updating the blkio.bfq.*
551 stats is rather costly, especially for some of the stats enabled by
552 CONFIG_BFQ_CGROUP_DEBUG.
553
554 Parameters to set
555 -----------------
556
557 For each group, there is only the following parameter to set.
558
559 weight (namely blkio.bfq.weight or io.bfq-weight): the weight of the
560 group inside its parent. Available values: 1..10000 (default 100). The
561 linear mapping between ioprio and weights, described at the beginning
562 of the tunable section, is still valid, but all weights higher than
563 IOPRIO_BE_NR*10 are mapped to ioprio 0.
564
565 Recall that, if low-latency is set, then BFQ automatically raises the
566 weight of the queues associated with interactive and soft real-time
567 applications. Unset this tunable if you need/want to control weights.
568
569
570 [1] P. Valente, A. Avanzini, "Evolution of the BFQ Storage I/O
571     Scheduler", Proceedings of the First Workshop on Mobile System
572     Technologies (MST-2015), May 2015.
573     http://algogroup.unimore.it/people/paolo/disk_sched/mst-2015.pdf
574
575 [2] P. Valente and M. Andreolini, "Improving Application
576     Responsiveness with the BFQ Disk I/O Scheduler", Proceedings of
577     the 5th Annual International Systems and Storage Conference
578     (SYSTOR '12), June 2012.
579     Slightly extended version:
580     http://algogroup.unimore.it/people/paolo/disk_sched/bfq-v1-suite-
581                                                         results.pdf
582
583 [3] https://github.com/Algodev-github/S