]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/vdev_queue.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.git] / module / zfs / vdev_queue.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
d164b209 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
23 * Use is subject to license terms.
24 */
25
c06d4368 26/*
ea04106b 27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
c06d4368
AX
28 */
29
34dc7c2f 30#include <sys/zfs_context.h>
34dc7c2f 31#include <sys/vdev_impl.h>
a08ee875 32#include <sys/spa_impl.h>
34dc7c2f
BB
33#include <sys/zio.h>
34#include <sys/avl.h>
a08ee875
LG
35#include <sys/dsl_pool.h>
36#include <sys/spa.h>
37#include <sys/spa_impl.h>
38#include <sys/kstat.h>
34dc7c2f
BB
39
40/*
a08ee875
LG
41 * ZFS I/O Scheduler
42 * ---------------
43 *
44 * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
45 * I/O scheduler determines when and in what order those operations are
46 * issued. The I/O scheduler divides operations into five I/O classes
47 * prioritized in the following order: sync read, sync write, async read,
48 * async write, and scrub/resilver. Each queue defines the minimum and
49 * maximum number of concurrent operations that may be issued to the device.
50 * In addition, the device has an aggregate maximum. Note that the sum of the
51 * per-queue minimums must not exceed the aggregate maximum. If the
52 * sum of the per-queue maximums exceeds the aggregate maximum, then the
53 * number of active i/os may reach zfs_vdev_max_active, in which case no
54 * further i/os will be issued regardless of whether all per-queue
55 * minimums have been met.
56 *
57 * For many physical devices, throughput increases with the number of
58 * concurrent operations, but latency typically suffers. Further, physical
59 * devices typically have a limit at which more concurrent operations have no
60 * effect on throughput or can actually cause it to decrease.
61 *
62 * The scheduler selects the next operation to issue by first looking for an
63 * I/O class whose minimum has not been satisfied. Once all are satisfied and
64 * the aggregate maximum has not been hit, the scheduler looks for classes
65 * whose maximum has not been satisfied. Iteration through the I/O classes is
66 * done in the order specified above. No further operations are issued if the
67 * aggregate maximum number of concurrent operations has been hit or if there
68 * are no operations queued for an I/O class that has not hit its maximum.
69 * Every time an i/o is queued or an operation completes, the I/O scheduler
70 * looks for new operations to issue.
71 *
72 * All I/O classes have a fixed maximum number of outstanding operations
73 * except for the async write class. Asynchronous writes represent the data
74 * that is committed to stable storage during the syncing stage for
75 * transaction groups (see txg.c). Transaction groups enter the syncing state
76 * periodically so the number of queued async writes will quickly burst up and
77 * then bleed down to zero. Rather than servicing them as quickly as possible,
78 * the I/O scheduler changes the maximum number of active async write i/os
79 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
80 * both throughput and latency typically increase with the number of
81 * concurrent operations issued to physical devices, reducing the burstiness
82 * in the number of concurrent operations also stabilizes the response time of
83 * operations from other -- and in particular synchronous -- queues. In broad
84 * strokes, the I/O scheduler will issue more concurrent operations from the
85 * async write queue as there's more dirty data in the pool.
86 *
87 * Async Writes
88 *
89 * The number of concurrent operations issued for the async write I/O class
90 * follows a piece-wise linear function defined by a few adjustable points.
91 *
92 * | o---------| <-- zfs_vdev_async_write_max_active
93 * ^ | /^ |
94 * | | / | |
95 * active | / | |
96 * I/O | / | |
97 * count | / | |
98 * | / | |
99 * |------------o | | <-- zfs_vdev_async_write_min_active
100 * 0|____________^______|_________|
101 * 0% | | 100% of zfs_dirty_data_max
102 * | |
103 * | `-- zfs_vdev_async_write_active_max_dirty_percent
104 * `--------- zfs_vdev_async_write_active_min_dirty_percent
105 *
106 * Until the amount of dirty data exceeds a minimum percentage of the dirty
107 * data allowed in the pool, the I/O scheduler will limit the number of
108 * concurrent operations to the minimum. As that threshold is crossed, the
109 * number of concurrent operations issued increases linearly to the maximum at
110 * the specified maximum percentage of the dirty data allowed in the pool.
111 *
112 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
113 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
114 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
115 * maximum percentage, this indicates that the rate of incoming data is
116 * greater than the rate that the backend storage can handle. In this case, we
117 * must further throttle incoming writes (see dmu_tx_delay() for details).
34dc7c2f 118 */
a08ee875 119
34dc7c2f 120/*
a08ee875
LG
121 * The maximum number of i/os active to each device. Ideally, this will be >=
122 * the sum of each queue's max_active. It must be at least the sum of each
123 * queue's min_active.
34dc7c2f 124 */
a08ee875 125uint32_t zfs_vdev_max_active = 1000;
34dc7c2f 126
c06d4368 127/*
a08ee875
LG
128 * Per-queue limits on the number of i/os active to each device. If the
129 * number of active i/os is < zfs_vdev_max_active, then the min_active comes
130 * into play. We will send min_active from each queue, and then select from
131 * queues in the order defined by zio_priority_t.
132 *
133 * In general, smaller max_active's will lead to lower latency of synchronous
134 * operations. Larger max_active's may lead to higher overall throughput,
135 * depending on underlying storage.
136 *
137 * The ratio of the queues' max_actives determines the balance of performance
138 * between reads, writes, and scrubs. E.g., increasing
139 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
140 * more quickly, but reads and writes to have higher latency and lower
141 * throughput.
c06d4368 142 */
a08ee875
LG
143uint32_t zfs_vdev_sync_read_min_active = 10;
144uint32_t zfs_vdev_sync_read_max_active = 10;
145uint32_t zfs_vdev_sync_write_min_active = 10;
146uint32_t zfs_vdev_sync_write_max_active = 10;
147uint32_t zfs_vdev_async_read_min_active = 1;
148uint32_t zfs_vdev_async_read_max_active = 3;
149uint32_t zfs_vdev_async_write_min_active = 1;
150uint32_t zfs_vdev_async_write_max_active = 10;
151uint32_t zfs_vdev_scrub_min_active = 1;
152uint32_t zfs_vdev_scrub_max_active = 2;
34dc7c2f 153
a08ee875
LG
154/*
155 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
156 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
157 * zfs_vdev_async_write_active_max_dirty_percent, use
158 * zfs_vdev_async_write_max_active. The value is linearly interpolated
159 * between min and max.
160 */
161int zfs_vdev_async_write_active_min_dirty_percent = 30;
162int zfs_vdev_async_write_active_max_dirty_percent = 60;
34dc7c2f
BB
163
164/*
45d1cae3
BB
165 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
166 * For read I/Os, we also aggregate across small adjacency gaps; for writes
167 * we include spans of optional I/Os to aid aggregation at the disk even when
168 * they aren't able to help us aggregate at this level.
34dc7c2f
BB
169 */
170int zfs_vdev_aggregation_limit = SPA_MAXBLOCKSIZE;
9babb374 171int zfs_vdev_read_gap_limit = 32 << 10;
45d1cae3 172int zfs_vdev_write_gap_limit = 4 << 10;
34dc7c2f 173
34dc7c2f 174int
a08ee875 175vdev_queue_offset_compare(const void *x1, const void *x2)
34dc7c2f
BB
176{
177 const zio_t *z1 = x1;
178 const zio_t *z2 = x2;
179
34dc7c2f
BB
180 if (z1->io_offset < z2->io_offset)
181 return (-1);
182 if (z1->io_offset > z2->io_offset)
183 return (1);
184
185 if (z1 < z2)
186 return (-1);
187 if (z1 > z2)
188 return (1);
189
190 return (0);
191}
192
193int
a08ee875 194vdev_queue_timestamp_compare(const void *x1, const void *x2)
34dc7c2f
BB
195{
196 const zio_t *z1 = x1;
197 const zio_t *z2 = x2;
198
a08ee875 199 if (z1->io_timestamp < z2->io_timestamp)
34dc7c2f 200 return (-1);
a08ee875 201 if (z1->io_timestamp > z2->io_timestamp)
34dc7c2f
BB
202 return (1);
203
204 if (z1 < z2)
205 return (-1);
206 if (z1 > z2)
207 return (1);
208
209 return (0);
210}
211
a08ee875
LG
212static int
213vdev_queue_class_min_active(zio_priority_t p)
214{
215 switch (p) {
216 case ZIO_PRIORITY_SYNC_READ:
217 return (zfs_vdev_sync_read_min_active);
218 case ZIO_PRIORITY_SYNC_WRITE:
219 return (zfs_vdev_sync_write_min_active);
220 case ZIO_PRIORITY_ASYNC_READ:
221 return (zfs_vdev_async_read_min_active);
222 case ZIO_PRIORITY_ASYNC_WRITE:
223 return (zfs_vdev_async_write_min_active);
224 case ZIO_PRIORITY_SCRUB:
225 return (zfs_vdev_scrub_min_active);
226 default:
227 panic("invalid priority %u", p);
228 return (0);
229 }
230}
231
232static int
ea04106b 233vdev_queue_max_async_writes(spa_t *spa)
a08ee875
LG
234{
235 int writes;
ea04106b 236 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
a08ee875
LG
237 uint64_t min_bytes = zfs_dirty_data_max *
238 zfs_vdev_async_write_active_min_dirty_percent / 100;
239 uint64_t max_bytes = zfs_dirty_data_max *
240 zfs_vdev_async_write_active_max_dirty_percent / 100;
241
ea04106b
AX
242 /*
243 * Sync tasks correspond to interactive user actions. To reduce the
244 * execution time of those actions we push data out as fast as possible.
245 */
246 if (spa_has_pending_synctask(spa)) {
247 return (zfs_vdev_async_write_max_active);
248 }
249
a08ee875
LG
250 if (dirty < min_bytes)
251 return (zfs_vdev_async_write_min_active);
252 if (dirty > max_bytes)
253 return (zfs_vdev_async_write_max_active);
254
255 /*
256 * linear interpolation:
257 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
258 * move right by min_bytes
259 * move up by min_writes
260 */
261 writes = (dirty - min_bytes) *
262 (zfs_vdev_async_write_max_active -
263 zfs_vdev_async_write_min_active) /
264 (max_bytes - min_bytes) +
265 zfs_vdev_async_write_min_active;
266 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
267 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
268 return (writes);
269}
270
271static int
272vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
273{
274 switch (p) {
275 case ZIO_PRIORITY_SYNC_READ:
276 return (zfs_vdev_sync_read_max_active);
277 case ZIO_PRIORITY_SYNC_WRITE:
278 return (zfs_vdev_sync_write_max_active);
279 case ZIO_PRIORITY_ASYNC_READ:
280 return (zfs_vdev_async_read_max_active);
281 case ZIO_PRIORITY_ASYNC_WRITE:
ea04106b 282 return (vdev_queue_max_async_writes(spa));
a08ee875
LG
283 case ZIO_PRIORITY_SCRUB:
284 return (zfs_vdev_scrub_max_active);
285 default:
286 panic("invalid priority %u", p);
287 return (0);
288 }
289}
290
291/*
292 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
293 * there is no eligible class.
294 */
295static zio_priority_t
296vdev_queue_class_to_issue(vdev_queue_t *vq)
297{
298 spa_t *spa = vq->vq_vdev->vdev_spa;
299 zio_priority_t p;
300
301 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
302 return (ZIO_PRIORITY_NUM_QUEUEABLE);
303
304 /* find a queue that has not reached its minimum # outstanding i/os */
305 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
306 if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 &&
307 vq->vq_class[p].vqc_active <
308 vdev_queue_class_min_active(p))
309 return (p);
310 }
311
312 /*
313 * If we haven't found a queue, look for one that hasn't reached its
314 * maximum # outstanding i/os.
315 */
316 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
317 if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 &&
318 vq->vq_class[p].vqc_active <
319 vdev_queue_class_max_active(spa, p))
320 return (p);
321 }
322
323 /* No eligible queued i/os */
324 return (ZIO_PRIORITY_NUM_QUEUEABLE);
325}
326
34dc7c2f
BB
327void
328vdev_queue_init(vdev_t *vd)
329{
330 vdev_queue_t *vq = &vd->vdev_queue;
a08ee875 331 zio_priority_t p;
34dc7c2f
BB
332
333 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
a08ee875 334 vq->vq_vdev = vd;
34dc7c2f 335
a08ee875
LG
336 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
337 sizeof (zio_t), offsetof(struct zio, io_queue_node));
34dc7c2f 338
a08ee875
LG
339 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
340 /*
341 * The synchronous i/o queues are FIFO rather than LBA ordered.
342 * This provides more consistent latency for these i/os, and
343 * they tend to not be tightly clustered anyway so there is
344 * little to no throughput loss.
345 */
346 boolean_t fifo = (p == ZIO_PRIORITY_SYNC_READ ||
347 p == ZIO_PRIORITY_SYNC_WRITE);
348 avl_create(&vq->vq_class[p].vqc_queued_tree,
349 fifo ? vdev_queue_timestamp_compare :
350 vdev_queue_offset_compare,
351 sizeof (zio_t), offsetof(struct zio, io_queue_node));
352 }
34dc7c2f
BB
353}
354
355void
356vdev_queue_fini(vdev_t *vd)
357{
358 vdev_queue_t *vq = &vd->vdev_queue;
a08ee875 359 zio_priority_t p;
34dc7c2f 360
a08ee875
LG
361 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
362 avl_destroy(&vq->vq_class[p].vqc_queued_tree);
363 avl_destroy(&vq->vq_active_tree);
34dc7c2f
BB
364
365 mutex_destroy(&vq->vq_lock);
366}
367
368static void
369vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
370{
a08ee875
LG
371 spa_t *spa = zio->io_spa;
372 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
373
374 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
375 avl_add(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio);
376
377 if (ssh->kstat != NULL) {
378 mutex_enter(&ssh->lock);
379 kstat_waitq_enter(ssh->kstat->ks_data);
380 mutex_exit(&ssh->lock);
381 }
34dc7c2f
BB
382}
383
384static void
385vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
386{
a08ee875
LG
387 spa_t *spa = zio->io_spa;
388 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
389
390 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
391 avl_remove(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio);
392
393 if (ssh->kstat != NULL) {
394 mutex_enter(&ssh->lock);
395 kstat_waitq_exit(ssh->kstat->ks_data);
396 mutex_exit(&ssh->lock);
397 }
398}
399
400static void
401vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
402{
403 spa_t *spa = zio->io_spa;
404 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
405
406 ASSERT(MUTEX_HELD(&vq->vq_lock));
407 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
408 vq->vq_class[zio->io_priority].vqc_active++;
409 avl_add(&vq->vq_active_tree, zio);
410
411 if (ssh->kstat != NULL) {
412 mutex_enter(&ssh->lock);
413 kstat_runq_enter(ssh->kstat->ks_data);
414 mutex_exit(&ssh->lock);
415 }
416}
417
418static void
419vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
420{
421 spa_t *spa = zio->io_spa;
422 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
423
424 ASSERT(MUTEX_HELD(&vq->vq_lock));
425 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
426 vq->vq_class[zio->io_priority].vqc_active--;
427 avl_remove(&vq->vq_active_tree, zio);
428
429 if (ssh->kstat != NULL) {
430 kstat_io_t *ksio = ssh->kstat->ks_data;
431
432 mutex_enter(&ssh->lock);
433 kstat_runq_exit(ksio);
434 if (zio->io_type == ZIO_TYPE_READ) {
435 ksio->reads++;
436 ksio->nread += zio->io_size;
437 } else if (zio->io_type == ZIO_TYPE_WRITE) {
438 ksio->writes++;
439 ksio->nwritten += zio->io_size;
440 }
441 mutex_exit(&ssh->lock);
442 }
34dc7c2f
BB
443}
444
445static void
446vdev_queue_agg_io_done(zio_t *aio)
447{
a08ee875
LG
448 if (aio->io_type == ZIO_TYPE_READ) {
449 zio_t *pio;
450 while ((pio = zio_walk_parents(aio)) != NULL) {
d164b209
BB
451 bcopy((char *)aio->io_data + (pio->io_offset -
452 aio->io_offset), pio->io_data, pio->io_size);
a08ee875
LG
453 }
454 }
34dc7c2f 455
ea04106b 456 zio_buf_free(aio->io_data, aio->io_size);
34dc7c2f
BB
457}
458
9babb374
BB
459/*
460 * Compute the range spanned by two i/os, which is the endpoint of the last
461 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
462 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
463 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
464 */
465#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
466#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
34dc7c2f
BB
467
468static zio_t *
a08ee875 469vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
34dc7c2f 470{
a08ee875
LG
471 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
472 uint64_t maxgap = 0;
473 uint64_t size;
474 boolean_t stretch = B_FALSE;
475 vdev_queue_class_t *vqc = &vq->vq_class[zio->io_priority];
476 avl_tree_t *t = &vqc->vqc_queued_tree;
477 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
478
479 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
480 return (NULL);
34dc7c2f 481
a08ee875
LG
482 /*
483 * Prevent users from setting the zfs_vdev_aggregation_limit
484 * tuning larger than SPA_MAXBLOCKSIZE.
485 */
486 zfs_vdev_aggregation_limit =
487 MIN(zfs_vdev_aggregation_limit, SPA_MAXBLOCKSIZE);
34dc7c2f 488
a08ee875
LG
489 /*
490 * The synchronous i/o queues are not sorted by LBA, so we can't
491 * find adjacent i/os. These i/os tend to not be tightly clustered,
492 * or too large to aggregate, so this has little impact on performance.
493 */
494 if (zio->io_priority == ZIO_PRIORITY_SYNC_READ ||
495 zio->io_priority == ZIO_PRIORITY_SYNC_WRITE)
34dc7c2f
BB
496 return (NULL);
497
a08ee875 498 first = last = zio;
34dc7c2f 499
a08ee875
LG
500 if (zio->io_type == ZIO_TYPE_READ)
501 maxgap = zfs_vdev_read_gap_limit;
fb5f0bc8 502
a08ee875
LG
503 /*
504 * We can aggregate I/Os that are sufficiently adjacent and of
505 * the same flavor, as expressed by the AGG_INHERIT flags.
506 * The latter requirement is necessary so that certain
507 * attributes of the I/O, such as whether it's a normal I/O
508 * or a scrub/resilver, can be preserved in the aggregate.
509 * We can include optional I/Os, but don't allow them
510 * to begin a range as they add no benefit in that situation.
511 */
45d1cae3 512
a08ee875
LG
513 /*
514 * We keep track of the last non-optional I/O.
515 */
516 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
45d1cae3 517
a08ee875
LG
518 /*
519 * Walk backwards through sufficiently contiguous I/Os
520 * recording the last non-option I/O.
521 */
522 while ((dio = AVL_PREV(t, first)) != NULL &&
523 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
524 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
525 IO_GAP(dio, first) <= maxgap) {
526 first = dio;
527 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
528 mandatory = first;
529 }
45d1cae3 530
a08ee875
LG
531 /*
532 * Skip any initial optional I/Os.
533 */
534 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
535 first = AVL_NEXT(t, first);
536 ASSERT(first != NULL);
537 }
9babb374 538
45d1cae3 539
a08ee875
LG
540 /*
541 * Walk forward through sufficiently contiguous I/Os.
542 */
543 while ((dio = AVL_NEXT(t, last)) != NULL &&
544 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
545 IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
546 IO_GAP(last, dio) <= maxgap) {
547 last = dio;
548 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
549 mandatory = last;
550 }
551
552 /*
553 * Now that we've established the range of the I/O aggregation
554 * we must decide what to do with trailing optional I/Os.
555 * For reads, there's nothing to do. While we are unable to
556 * aggregate further, it's possible that a trailing optional
557 * I/O would allow the underlying device to aggregate with
558 * subsequent I/Os. We must therefore determine if the next
559 * non-optional I/O is close enough to make aggregation
560 * worthwhile.
561 */
562 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
563 zio_t *nio = last;
564 while ((dio = AVL_NEXT(t, nio)) != NULL &&
565 IO_GAP(nio, dio) == 0 &&
566 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
567 nio = dio;
568 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
569 stretch = B_TRUE;
570 break;
45d1cae3
BB
571 }
572 }
a08ee875 573 }
45d1cae3 574
a08ee875
LG
575 if (stretch) {
576 /* This may be a no-op. */
577 dio = AVL_NEXT(t, last);
578 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
579 } else {
580 while (last != mandatory && last != first) {
581 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
582 last = AVL_PREV(t, last);
583 ASSERT(last != NULL);
45d1cae3 584 }
34dc7c2f
BB
585 }
586
a08ee875
LG
587 if (first == last)
588 return (NULL);
d164b209 589
a08ee875
LG
590 size = IO_SPAN(first, last);
591 ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
592
593 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
ea04106b 594 zio_buf_alloc(size), size, first->io_type, zio->io_priority,
a08ee875
LG
595 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
596 vdev_queue_agg_io_done, NULL);
597 aio->io_timestamp = first->io_timestamp;
598
599 nio = first;
600 do {
601 dio = nio;
602 nio = AVL_NEXT(t, dio);
603 ASSERT3U(dio->io_type, ==, aio->io_type);
604
605 if (dio->io_flags & ZIO_FLAG_NODATA) {
606 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
607 bzero((char *)aio->io_data + (dio->io_offset -
608 aio->io_offset), dio->io_size);
609 } else if (dio->io_type == ZIO_TYPE_WRITE) {
610 bcopy(dio->io_data, (char *)aio->io_data +
611 (dio->io_offset - aio->io_offset),
612 dio->io_size);
613 }
34dc7c2f 614
a08ee875
LG
615 zio_add_child(dio, aio);
616 vdev_queue_io_remove(vq, dio);
617 zio_vdev_io_bypass(dio);
618 zio_execute(dio);
619 } while (dio != last);
620
a08ee875
LG
621 return (aio);
622}
34dc7c2f 623
a08ee875
LG
624static zio_t *
625vdev_queue_io_to_issue(vdev_queue_t *vq)
626{
627 zio_t *zio, *aio;
628 zio_priority_t p;
629 avl_index_t idx;
630 vdev_queue_class_t *vqc;
a08ee875
LG
631
632again:
633 ASSERT(MUTEX_HELD(&vq->vq_lock));
634
635 p = vdev_queue_class_to_issue(vq);
636
637 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
638 /* No eligible queued i/os */
639 return (NULL);
34dc7c2f
BB
640 }
641
a08ee875
LG
642 /*
643 * For LBA-ordered queues (async / scrub), issue the i/o which follows
644 * the most recently issued i/o in LBA (offset) order.
645 *
646 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
647 */
648 vqc = &vq->vq_class[p];
ea04106b
AX
649 vq->vq_io_search.io_timestamp = 0;
650 vq->vq_io_search.io_offset = vq->vq_last_offset + 1;
651 VERIFY3P(avl_find(&vqc->vqc_queued_tree, &vq->vq_io_search,
652 &idx), ==, NULL);
a08ee875
LG
653 zio = avl_nearest(&vqc->vqc_queued_tree, idx, AVL_AFTER);
654 if (zio == NULL)
655 zio = avl_first(&vqc->vqc_queued_tree);
656 ASSERT3U(zio->io_priority, ==, p);
657
658 aio = vdev_queue_aggregate(vq, zio);
659 if (aio != NULL)
660 zio = aio;
661 else
662 vdev_queue_io_remove(vq, zio);
34dc7c2f 663
45d1cae3
BB
664 /*
665 * If the I/O is or was optional and therefore has no data, we need to
666 * simply discard it. We need to drop the vdev queue's lock to avoid a
667 * deadlock that we could encounter since this I/O will complete
668 * immediately.
669 */
a08ee875 670 if (zio->io_flags & ZIO_FLAG_NODATA) {
45d1cae3 671 mutex_exit(&vq->vq_lock);
a08ee875
LG
672 zio_vdev_io_bypass(zio);
673 zio_execute(zio);
45d1cae3
BB
674 mutex_enter(&vq->vq_lock);
675 goto again;
676 }
677
a08ee875
LG
678 vdev_queue_pending_add(vq, zio);
679 vq->vq_last_offset = zio->io_offset;
34dc7c2f 680
a08ee875 681 return (zio);
34dc7c2f
BB
682}
683
684zio_t *
685vdev_queue_io(zio_t *zio)
686{
687 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
688 zio_t *nio;
689
34dc7c2f
BB
690 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
691 return (zio);
692
a08ee875
LG
693 /*
694 * Children i/os inherent their parent's priority, which might
695 * not match the child's i/o type. Fix it up here.
696 */
697 if (zio->io_type == ZIO_TYPE_READ) {
698 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
699 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
700 zio->io_priority != ZIO_PRIORITY_SCRUB)
701 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
702 } else {
703 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
704 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
705 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
706 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
707 }
34dc7c2f 708
a08ee875 709 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
34dc7c2f
BB
710
711 mutex_enter(&vq->vq_lock);
c06d4368 712 zio->io_timestamp = gethrtime();
34dc7c2f 713 vdev_queue_io_add(vq, zio);
a08ee875 714 nio = vdev_queue_io_to_issue(vq);
34dc7c2f
BB
715 mutex_exit(&vq->vq_lock);
716
717 if (nio == NULL)
718 return (NULL);
719
720 if (nio->io_done == vdev_queue_agg_io_done) {
721 zio_nowait(nio);
722 return (NULL);
723 }
724
725 return (nio);
726}
727
728void
729vdev_queue_io_done(zio_t *zio)
730{
731 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
a08ee875 732 zio_t *nio;
34dc7c2f 733
c06d4368
AX
734 if (zio_injection_enabled)
735 delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
736
34dc7c2f
BB
737 mutex_enter(&vq->vq_lock);
738
a08ee875 739 vdev_queue_pending_remove(vq, zio);
34dc7c2f 740
c06d4368
AX
741 zio->io_delta = gethrtime() - zio->io_timestamp;
742 vq->vq_io_complete_ts = gethrtime();
743 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
744
a08ee875 745 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
34dc7c2f
BB
746 mutex_exit(&vq->vq_lock);
747 if (nio->io_done == vdev_queue_agg_io_done) {
748 zio_nowait(nio);
749 } else {
750 zio_vdev_io_reissue(nio);
751 zio_execute(nio);
752 }
753 mutex_enter(&vq->vq_lock);
754 }
755
756 mutex_exit(&vq->vq_lock);
757}
c28b2279
BB
758
759#if defined(_KERNEL) && defined(HAVE_SPL)
c28b2279 760module_param(zfs_vdev_aggregation_limit, int, 0644);
c409e464
BB
761MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
762
c409e464
BB
763module_param(zfs_vdev_read_gap_limit, int, 0644);
764MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
765
766module_param(zfs_vdev_write_gap_limit, int, 0644);
767MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
a08ee875
LG
768
769module_param(zfs_vdev_max_active, int, 0644);
770MODULE_PARM_DESC(zfs_vdev_max_active, "Maximum number of active I/Os per vdev");
771
772module_param(zfs_vdev_async_write_active_max_dirty_percent, int, 0644);
773MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent,
774 "Async write concurrency max threshold");
775
776module_param(zfs_vdev_async_write_active_min_dirty_percent, int, 0644);
777MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent,
778 "Async write concurrency min threshold");
779
780module_param(zfs_vdev_async_read_max_active, int, 0644);
781MODULE_PARM_DESC(zfs_vdev_async_read_max_active,
782 "Max active async read I/Os per vdev");
783
784module_param(zfs_vdev_async_read_min_active, int, 0644);
785MODULE_PARM_DESC(zfs_vdev_async_read_min_active,
786 "Min active async read I/Os per vdev");
787
788module_param(zfs_vdev_async_write_max_active, int, 0644);
789MODULE_PARM_DESC(zfs_vdev_async_write_max_active,
790 "Max active async write I/Os per vdev");
791
792module_param(zfs_vdev_async_write_min_active, int, 0644);
793MODULE_PARM_DESC(zfs_vdev_async_write_min_active,
794 "Min active async write I/Os per vdev");
795
796module_param(zfs_vdev_scrub_max_active, int, 0644);
797MODULE_PARM_DESC(zfs_vdev_scrub_max_active, "Max active scrub I/Os per vdev");
798
799module_param(zfs_vdev_scrub_min_active, int, 0644);
800MODULE_PARM_DESC(zfs_vdev_scrub_min_active, "Min active scrub I/Os per vdev");
801
802module_param(zfs_vdev_sync_read_max_active, int, 0644);
803MODULE_PARM_DESC(zfs_vdev_sync_read_max_active,
804 "Max active sync read I/Os per vdev");
805
806module_param(zfs_vdev_sync_read_min_active, int, 0644);
807MODULE_PARM_DESC(zfs_vdev_sync_read_min_active,
808 "Min active sync read I/Os per vdev");
809
810module_param(zfs_vdev_sync_write_max_active, int, 0644);
811MODULE_PARM_DESC(zfs_vdev_sync_write_max_active,
812 "Max active sync write I/Os per vdev");
813
814module_param(zfs_vdev_sync_write_min_active, int, 0644);
815MODULE_PARM_DESC(zfs_vdev_sync_write_min_active,
816 "Min active sync write I/Osper vdev");
c28b2279 817#endif