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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
30 #include <sys/zfs_context.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/spa_impl.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/metaslab_impl.h>
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.
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.
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.
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.
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.
92 * | o---------| <-- zfs_vdev_async_write_max_active
99 * |------------o | | <-- zfs_vdev_async_write_min_active
100 * 0|____________^______|_________|
101 * 0% | | 100% of zfs_dirty_data_max
103 * | `-- zfs_vdev_async_write_active_max_dirty_percent
104 * `--------- zfs_vdev_async_write_active_min_dirty_percent
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.
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).
121 * The maximum number of i/os active to each device. Ideally, this will be >=
122 * the sum of each queue's max_active.
124 uint_t zfs_vdev_max_active = 1000;
127 * Per-queue limits on the number of i/os active to each device. If the
128 * number of active i/os is < zfs_vdev_max_active, then the min_active comes
129 * into play. We will send min_active from each queue round-robin, and then
130 * send from queues in the order defined by zio_priority_t up to max_active.
131 * Some queues have additional mechanisms to limit number of active I/Os in
132 * addition to min_active and max_active, see below.
134 * In general, smaller max_active's will lead to lower latency of synchronous
135 * operations. Larger max_active's may lead to higher overall throughput,
136 * depending on underlying storage.
138 * The ratio of the queues' max_actives determines the balance of performance
139 * between reads, writes, and scrubs. E.g., increasing
140 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
141 * more quickly, but reads and writes to have higher latency and lower
144 static uint_t zfs_vdev_sync_read_min_active = 10;
145 static uint_t zfs_vdev_sync_read_max_active = 10;
146 static uint_t zfs_vdev_sync_write_min_active = 10;
147 static uint_t zfs_vdev_sync_write_max_active = 10;
148 static uint_t zfs_vdev_async_read_min_active = 1;
149 /* */ uint_t zfs_vdev_async_read_max_active = 3;
150 static uint_t zfs_vdev_async_write_min_active = 2;
151 /* */ uint_t zfs_vdev_async_write_max_active = 10;
152 static uint_t zfs_vdev_scrub_min_active = 1;
153 static uint_t zfs_vdev_scrub_max_active = 3;
154 static uint_t zfs_vdev_removal_min_active = 1;
155 static uint_t zfs_vdev_removal_max_active = 2;
156 static uint_t zfs_vdev_initializing_min_active = 1;
157 static uint_t zfs_vdev_initializing_max_active = 1;
158 static uint_t zfs_vdev_trim_min_active = 1;
159 static uint_t zfs_vdev_trim_max_active = 2;
160 static uint_t zfs_vdev_rebuild_min_active = 1;
161 static uint_t zfs_vdev_rebuild_max_active = 3;
164 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
165 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
166 * zfs_vdev_async_write_active_max_dirty_percent, use
167 * zfs_vdev_async_write_max_active. The value is linearly interpolated
168 * between min and max.
170 uint_t zfs_vdev_async_write_active_min_dirty_percent = 30;
171 uint_t zfs_vdev_async_write_active_max_dirty_percent = 60;
174 * For non-interactive I/O (scrub, resilver, removal, initialize and rebuild),
175 * the number of concurrently-active I/O's is limited to *_min_active, unless
176 * the vdev is "idle". When there are no interactive I/Os active (sync or
177 * async), and zfs_vdev_nia_delay I/Os have completed since the last
178 * interactive I/O, then the vdev is considered to be "idle", and the number
179 * of concurrently-active non-interactive I/O's is increased to *_max_active.
181 static uint_t zfs_vdev_nia_delay = 5;
184 * Some HDDs tend to prioritize sequential I/O so high that concurrent
185 * random I/O latency reaches several seconds. On some HDDs it happens
186 * even if sequential I/Os are submitted one at a time, and so setting
187 * *_max_active to 1 does not help. To prevent non-interactive I/Os, like
188 * scrub, from monopolizing the device no more than zfs_vdev_nia_credit
189 * I/Os can be sent while there are outstanding incomplete interactive
190 * I/Os. This enforced wait ensures the HDD services the interactive I/O
191 * within a reasonable amount of time.
193 static uint_t zfs_vdev_nia_credit = 5;
196 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
197 * For read I/Os, we also aggregate across small adjacency gaps; for writes
198 * we include spans of optional I/Os to aid aggregation at the disk even when
199 * they aren't able to help us aggregate at this level.
201 static uint_t zfs_vdev_aggregation_limit = 1 << 20;
202 static uint_t zfs_vdev_aggregation_limit_non_rotating = SPA_OLD_MAXBLOCKSIZE;
203 static uint_t zfs_vdev_read_gap_limit = 32 << 10;
204 static uint_t zfs_vdev_write_gap_limit = 4 << 10;
207 * Define the queue depth percentage for each top-level. This percentage is
208 * used in conjunction with zfs_vdev_async_max_active to determine how many
209 * allocations a specific top-level vdev should handle. Once the queue depth
210 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
211 * then allocator will stop allocating blocks on that top-level device.
212 * The default kernel setting is 1000% which will yield 100 allocations per
213 * device. For userland testing, the default setting is 300% which equates
214 * to 30 allocations per device.
217 uint_t zfs_vdev_queue_depth_pct = 1000;
219 uint_t zfs_vdev_queue_depth_pct = 300;
223 * When performing allocations for a given metaslab, we want to make sure that
224 * there are enough IOs to aggregate together to improve throughput. We want to
225 * ensure that there are at least 128k worth of IOs that can be aggregated, and
226 * we assume that the average allocation size is 4k, so we need the queue depth
227 * to be 32 per allocator to get good aggregation of sequential writes.
229 uint_t zfs_vdev_def_queue_depth = 32;
232 * Allow TRIM I/Os to be aggregated. This should normally not be needed since
233 * TRIM I/O for extents up to zfs_trim_extent_bytes_max (128M) can be submitted
234 * by the TRIM code in zfs_trim.c.
236 static uint_t zfs_vdev_aggregate_trim = 0;
239 vdev_queue_offset_compare(const void *x1, const void *x2)
241 const zio_t *z1 = (const zio_t *)x1;
242 const zio_t *z2 = (const zio_t *)x2;
244 int cmp = TREE_CMP(z1->io_offset, z2->io_offset);
249 return (TREE_PCMP(z1, z2));
252 static inline avl_tree_t *
253 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
255 return (&vq->vq_class[p].vqc_queued_tree);
258 static inline avl_tree_t *
259 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
261 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE || t == ZIO_TYPE_TRIM);
262 if (t == ZIO_TYPE_READ)
263 return (&vq->vq_read_offset_tree);
264 else if (t == ZIO_TYPE_WRITE)
265 return (&vq->vq_write_offset_tree);
267 return (&vq->vq_trim_offset_tree);
271 vdev_queue_timestamp_compare(const void *x1, const void *x2)
273 const zio_t *z1 = (const zio_t *)x1;
274 const zio_t *z2 = (const zio_t *)x2;
276 int cmp = TREE_CMP(z1->io_timestamp, z2->io_timestamp);
281 return (TREE_PCMP(z1, z2));
285 vdev_queue_class_min_active(vdev_queue_t *vq, zio_priority_t p)
288 case ZIO_PRIORITY_SYNC_READ:
289 return (zfs_vdev_sync_read_min_active);
290 case ZIO_PRIORITY_SYNC_WRITE:
291 return (zfs_vdev_sync_write_min_active);
292 case ZIO_PRIORITY_ASYNC_READ:
293 return (zfs_vdev_async_read_min_active);
294 case ZIO_PRIORITY_ASYNC_WRITE:
295 return (zfs_vdev_async_write_min_active);
296 case ZIO_PRIORITY_SCRUB:
297 return (vq->vq_ia_active == 0 ? zfs_vdev_scrub_min_active :
298 MIN(vq->vq_nia_credit, zfs_vdev_scrub_min_active));
299 case ZIO_PRIORITY_REMOVAL:
300 return (vq->vq_ia_active == 0 ? zfs_vdev_removal_min_active :
301 MIN(vq->vq_nia_credit, zfs_vdev_removal_min_active));
302 case ZIO_PRIORITY_INITIALIZING:
303 return (vq->vq_ia_active == 0 ?zfs_vdev_initializing_min_active:
304 MIN(vq->vq_nia_credit, zfs_vdev_initializing_min_active));
305 case ZIO_PRIORITY_TRIM:
306 return (zfs_vdev_trim_min_active);
307 case ZIO_PRIORITY_REBUILD:
308 return (vq->vq_ia_active == 0 ? zfs_vdev_rebuild_min_active :
309 MIN(vq->vq_nia_credit, zfs_vdev_rebuild_min_active));
311 panic("invalid priority %u", p);
317 vdev_queue_max_async_writes(spa_t *spa)
321 dsl_pool_t *dp = spa_get_dsl(spa);
322 uint64_t min_bytes = zfs_dirty_data_max *
323 zfs_vdev_async_write_active_min_dirty_percent / 100;
324 uint64_t max_bytes = zfs_dirty_data_max *
325 zfs_vdev_async_write_active_max_dirty_percent / 100;
328 * Async writes may occur before the assignment of the spa's
329 * dsl_pool_t if a self-healing zio is issued prior to the
330 * completion of dmu_objset_open_impl().
333 return (zfs_vdev_async_write_max_active);
336 * Sync tasks correspond to interactive user actions. To reduce the
337 * execution time of those actions we push data out as fast as possible.
339 dirty = dp->dp_dirty_total;
340 if (dirty > max_bytes || spa_has_pending_synctask(spa))
341 return (zfs_vdev_async_write_max_active);
343 if (dirty < min_bytes)
344 return (zfs_vdev_async_write_min_active);
347 * linear interpolation:
348 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
349 * move right by min_bytes
350 * move up by min_writes
352 writes = (dirty - min_bytes) *
353 (zfs_vdev_async_write_max_active -
354 zfs_vdev_async_write_min_active) /
355 (max_bytes - min_bytes) +
356 zfs_vdev_async_write_min_active;
357 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
358 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
363 vdev_queue_class_max_active(spa_t *spa, vdev_queue_t *vq, zio_priority_t p)
366 case ZIO_PRIORITY_SYNC_READ:
367 return (zfs_vdev_sync_read_max_active);
368 case ZIO_PRIORITY_SYNC_WRITE:
369 return (zfs_vdev_sync_write_max_active);
370 case ZIO_PRIORITY_ASYNC_READ:
371 return (zfs_vdev_async_read_max_active);
372 case ZIO_PRIORITY_ASYNC_WRITE:
373 return (vdev_queue_max_async_writes(spa));
374 case ZIO_PRIORITY_SCRUB:
375 if (vq->vq_ia_active > 0) {
376 return (MIN(vq->vq_nia_credit,
377 zfs_vdev_scrub_min_active));
378 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
379 return (MAX(1, zfs_vdev_scrub_min_active));
380 return (zfs_vdev_scrub_max_active);
381 case ZIO_PRIORITY_REMOVAL:
382 if (vq->vq_ia_active > 0) {
383 return (MIN(vq->vq_nia_credit,
384 zfs_vdev_removal_min_active));
385 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
386 return (MAX(1, zfs_vdev_removal_min_active));
387 return (zfs_vdev_removal_max_active);
388 case ZIO_PRIORITY_INITIALIZING:
389 if (vq->vq_ia_active > 0) {
390 return (MIN(vq->vq_nia_credit,
391 zfs_vdev_initializing_min_active));
392 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
393 return (MAX(1, zfs_vdev_initializing_min_active));
394 return (zfs_vdev_initializing_max_active);
395 case ZIO_PRIORITY_TRIM:
396 return (zfs_vdev_trim_max_active);
397 case ZIO_PRIORITY_REBUILD:
398 if (vq->vq_ia_active > 0) {
399 return (MIN(vq->vq_nia_credit,
400 zfs_vdev_rebuild_min_active));
401 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
402 return (MAX(1, zfs_vdev_rebuild_min_active));
403 return (zfs_vdev_rebuild_max_active);
405 panic("invalid priority %u", p);
411 * Return the i/o class to issue from, or ZIO_PRIORITY_NUM_QUEUEABLE if
412 * there is no eligible class.
414 static zio_priority_t
415 vdev_queue_class_to_issue(vdev_queue_t *vq)
417 spa_t *spa = vq->vq_vdev->vdev_spa;
420 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
421 return (ZIO_PRIORITY_NUM_QUEUEABLE);
424 * Find a queue that has not reached its minimum # outstanding i/os.
425 * Do round-robin to reduce starvation due to zfs_vdev_max_active
426 * and vq_nia_credit limits.
428 for (n = 0; n < ZIO_PRIORITY_NUM_QUEUEABLE; n++) {
429 p = (vq->vq_last_prio + n + 1) % ZIO_PRIORITY_NUM_QUEUEABLE;
430 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
431 vq->vq_class[p].vqc_active <
432 vdev_queue_class_min_active(vq, p)) {
433 vq->vq_last_prio = p;
439 * If we haven't found a queue, look for one that hasn't reached its
440 * maximum # outstanding i/os.
442 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
443 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
444 vq->vq_class[p].vqc_active <
445 vdev_queue_class_max_active(spa, vq, p)) {
446 vq->vq_last_prio = p;
451 /* No eligible queued i/os */
452 return (ZIO_PRIORITY_NUM_QUEUEABLE);
456 vdev_queue_init(vdev_t *vd)
458 vdev_queue_t *vq = &vd->vdev_queue;
461 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
463 taskq_init_ent(&vd->vdev_queue.vq_io_search.io_tqent);
465 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
466 sizeof (zio_t), offsetof(struct zio, io_queue_node));
467 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
468 vdev_queue_offset_compare, sizeof (zio_t),
469 offsetof(struct zio, io_offset_node));
470 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
471 vdev_queue_offset_compare, sizeof (zio_t),
472 offsetof(struct zio, io_offset_node));
473 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_TRIM),
474 vdev_queue_offset_compare, sizeof (zio_t),
475 offsetof(struct zio, io_offset_node));
477 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
478 int (*compfn) (const void *, const void *);
481 * The synchronous/trim i/o queues are dispatched in FIFO rather
482 * than LBA order. This provides more consistent latency for
485 if (p == ZIO_PRIORITY_SYNC_READ ||
486 p == ZIO_PRIORITY_SYNC_WRITE ||
487 p == ZIO_PRIORITY_TRIM) {
488 compfn = vdev_queue_timestamp_compare;
490 compfn = vdev_queue_offset_compare;
492 avl_create(vdev_queue_class_tree(vq, p), compfn,
493 sizeof (zio_t), offsetof(struct zio, io_queue_node));
496 vq->vq_last_offset = 0;
500 vdev_queue_fini(vdev_t *vd)
502 vdev_queue_t *vq = &vd->vdev_queue;
504 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
505 avl_destroy(vdev_queue_class_tree(vq, p));
506 avl_destroy(&vq->vq_active_tree);
507 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
508 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
509 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_TRIM));
511 mutex_destroy(&vq->vq_lock);
515 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
517 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
518 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
519 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
523 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
525 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
526 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
527 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
531 vdev_queue_is_interactive(zio_priority_t p)
534 case ZIO_PRIORITY_SCRUB:
535 case ZIO_PRIORITY_REMOVAL:
536 case ZIO_PRIORITY_INITIALIZING:
537 case ZIO_PRIORITY_REBUILD:
545 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
547 ASSERT(MUTEX_HELD(&vq->vq_lock));
548 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
549 vq->vq_class[zio->io_priority].vqc_active++;
550 if (vdev_queue_is_interactive(zio->io_priority)) {
551 if (++vq->vq_ia_active == 1)
552 vq->vq_nia_credit = 1;
553 } else if (vq->vq_ia_active > 0) {
556 avl_add(&vq->vq_active_tree, zio);
560 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
562 ASSERT(MUTEX_HELD(&vq->vq_lock));
563 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
564 vq->vq_class[zio->io_priority].vqc_active--;
565 if (vdev_queue_is_interactive(zio->io_priority)) {
566 if (--vq->vq_ia_active == 0)
567 vq->vq_nia_credit = 0;
569 vq->vq_nia_credit = zfs_vdev_nia_credit;
570 } else if (vq->vq_ia_active == 0)
572 avl_remove(&vq->vq_active_tree, zio);
576 vdev_queue_agg_io_done(zio_t *aio)
578 abd_free(aio->io_abd);
582 * Compute the range spanned by two i/os, which is the endpoint of the last
583 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
584 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
585 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
587 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
588 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
591 * Sufficiently adjacent io_offset's in ZIOs will be aggregated. We do this
592 * by creating a gang ABD from the adjacent ZIOs io_abd's. By using
593 * a gang ABD we avoid doing memory copies to and from the parent,
594 * child ZIOs. The gang ABD also accounts for gaps between adjacent
595 * io_offsets by simply getting the zero ABD for writes or allocating
596 * a new ABD for reads and placing them in the gang ABD as well.
599 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
601 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
606 boolean_t stretch = B_FALSE;
607 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
608 zio_flag_t flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
609 uint64_t next_offset;
612 maxblocksize = spa_maxblocksize(vq->vq_vdev->vdev_spa);
613 if (vq->vq_vdev->vdev_nonrot)
614 limit = zfs_vdev_aggregation_limit_non_rotating;
616 limit = zfs_vdev_aggregation_limit;
617 limit = MIN(limit, maxblocksize);
619 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE || limit == 0)
623 * While TRIM commands could be aggregated based on offset this
624 * behavior is disabled until it's determined to be beneficial.
626 if (zio->io_type == ZIO_TYPE_TRIM && !zfs_vdev_aggregate_trim)
630 * I/Os to distributed spares are directly dispatched to the dRAID
631 * leaf vdevs for aggregation. See the comment at the end of the
632 * zio_vdev_io_start() function.
634 ASSERT(vq->vq_vdev->vdev_ops != &vdev_draid_spare_ops);
638 if (zio->io_type == ZIO_TYPE_READ)
639 maxgap = zfs_vdev_read_gap_limit;
642 * We can aggregate I/Os that are sufficiently adjacent and of
643 * the same flavor, as expressed by the AGG_INHERIT flags.
644 * The latter requirement is necessary so that certain
645 * attributes of the I/O, such as whether it's a normal I/O
646 * or a scrub/resilver, can be preserved in the aggregate.
647 * We can include optional I/Os, but don't allow them
648 * to begin a range as they add no benefit in that situation.
652 * We keep track of the last non-optional I/O.
654 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
657 * Walk backwards through sufficiently contiguous I/Os
658 * recording the last non-optional I/O.
660 while ((dio = AVL_PREV(t, first)) != NULL &&
661 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
662 IO_SPAN(dio, last) <= limit &&
663 IO_GAP(dio, first) <= maxgap &&
664 dio->io_type == zio->io_type) {
666 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
671 * Skip any initial optional I/Os.
673 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
674 first = AVL_NEXT(t, first);
675 ASSERT(first != NULL);
680 * Walk forward through sufficiently contiguous I/Os.
681 * The aggregation limit does not apply to optional i/os, so that
682 * we can issue contiguous writes even if they are larger than the
685 while ((dio = AVL_NEXT(t, last)) != NULL &&
686 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
687 (IO_SPAN(first, dio) <= limit ||
688 (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
689 IO_SPAN(first, dio) <= maxblocksize &&
690 IO_GAP(last, dio) <= maxgap &&
691 dio->io_type == zio->io_type) {
693 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
698 * Now that we've established the range of the I/O aggregation
699 * we must decide what to do with trailing optional I/Os.
700 * For reads, there's nothing to do. While we are unable to
701 * aggregate further, it's possible that a trailing optional
702 * I/O would allow the underlying device to aggregate with
703 * subsequent I/Os. We must therefore determine if the next
704 * non-optional I/O is close enough to make aggregation
707 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
709 while ((dio = AVL_NEXT(t, nio)) != NULL &&
710 IO_GAP(nio, dio) == 0 &&
711 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
713 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
722 * We are going to include an optional io in our aggregated
723 * span, thus closing the write gap. Only mandatory i/os can
724 * start aggregated spans, so make sure that the next i/o
725 * after our span is mandatory.
727 dio = AVL_NEXT(t, last);
728 ASSERT3P(dio, !=, NULL);
729 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
731 /* do not include the optional i/o */
732 while (last != mandatory && last != first) {
733 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
734 last = AVL_PREV(t, last);
735 ASSERT(last != NULL);
742 size = IO_SPAN(first, last);
743 ASSERT3U(size, <=, maxblocksize);
745 abd = abd_alloc_gang();
749 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
750 abd, size, first->io_type, zio->io_priority,
751 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
752 vdev_queue_agg_io_done, NULL);
753 aio->io_timestamp = first->io_timestamp;
756 next_offset = first->io_offset;
759 nio = AVL_NEXT(t, dio);
760 ASSERT3P(dio, !=, NULL);
761 zio_add_child(dio, aio);
762 vdev_queue_io_remove(vq, dio);
764 if (dio->io_offset != next_offset) {
765 /* allocate a buffer for a read gap */
766 ASSERT3U(dio->io_type, ==, ZIO_TYPE_READ);
767 ASSERT3U(dio->io_offset, >, next_offset);
768 abd = abd_alloc_for_io(
769 dio->io_offset - next_offset, B_TRUE);
770 abd_gang_add(aio->io_abd, abd, B_TRUE);
773 (dio->io_size != abd_get_size(dio->io_abd))) {
774 /* abd size not the same as IO size */
775 ASSERT3U(abd_get_size(dio->io_abd), >, dio->io_size);
776 abd = abd_get_offset_size(dio->io_abd, 0, dio->io_size);
777 abd_gang_add(aio->io_abd, abd, B_TRUE);
779 if (dio->io_flags & ZIO_FLAG_NODATA) {
780 /* allocate a buffer for a write gap */
781 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
782 ASSERT3P(dio->io_abd, ==, NULL);
783 abd_gang_add(aio->io_abd,
784 abd_get_zeros(dio->io_size), B_TRUE);
787 * We pass B_FALSE to abd_gang_add()
788 * because we did not allocate a new
789 * ABD, so it is assumed the caller
790 * will free this ABD.
792 abd_gang_add(aio->io_abd, dio->io_abd,
796 next_offset = dio->io_offset + dio->io_size;
797 } while (dio != last);
798 ASSERT3U(abd_get_size(aio->io_abd), ==, aio->io_size);
801 * Callers must call zio_vdev_io_bypass() and zio_execute() for
802 * aggregated (parent) I/Os so that we could avoid dropping the
803 * queue's lock here to avoid a deadlock that we could encounter
804 * due to lock order reversal between vq_lock and io_lock in
805 * zio_change_priority().
811 vdev_queue_io_to_issue(vdev_queue_t *vq)
819 ASSERT(MUTEX_HELD(&vq->vq_lock));
821 p = vdev_queue_class_to_issue(vq);
823 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
824 /* No eligible queued i/os */
829 * For LBA-ordered queues (async / scrub / initializing), issue the
830 * i/o which follows the most recently issued i/o in LBA (offset) order.
832 * For FIFO queues (sync/trim), issue the i/o with the lowest timestamp.
834 tree = vdev_queue_class_tree(vq, p);
835 vq->vq_io_search.io_timestamp = 0;
836 vq->vq_io_search.io_offset = vq->vq_last_offset - 1;
837 VERIFY3P(avl_find(tree, &vq->vq_io_search, &idx), ==, NULL);
838 zio = avl_nearest(tree, idx, AVL_AFTER);
840 zio = avl_first(tree);
841 ASSERT3U(zio->io_priority, ==, p);
843 aio = vdev_queue_aggregate(vq, zio);
847 vdev_queue_io_remove(vq, zio);
850 * If the I/O is or was optional and therefore has no data, we
851 * need to simply discard it. We need to drop the vdev queue's
852 * lock to avoid a deadlock that we could encounter since this
853 * I/O will complete immediately.
855 if (zio->io_flags & ZIO_FLAG_NODATA) {
856 mutex_exit(&vq->vq_lock);
857 zio_vdev_io_bypass(zio);
859 mutex_enter(&vq->vq_lock);
864 vdev_queue_pending_add(vq, zio);
865 vq->vq_last_offset = zio->io_offset + zio->io_size;
871 vdev_queue_io(zio_t *zio)
873 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
875 zio_link_t *zl = NULL;
877 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
881 * Children i/os inherent their parent's priority, which might
882 * not match the child's i/o type. Fix it up here.
884 if (zio->io_type == ZIO_TYPE_READ) {
885 ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
887 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
888 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
889 zio->io_priority != ZIO_PRIORITY_SCRUB &&
890 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
891 zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
892 zio->io_priority != ZIO_PRIORITY_REBUILD) {
893 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
895 } else if (zio->io_type == ZIO_TYPE_WRITE) {
896 ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
898 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
899 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
900 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
901 zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
902 zio->io_priority != ZIO_PRIORITY_REBUILD) {
903 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
906 ASSERT(zio->io_type == ZIO_TYPE_TRIM);
907 ASSERT(zio->io_priority == ZIO_PRIORITY_TRIM);
910 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
911 zio->io_timestamp = gethrtime();
913 mutex_enter(&vq->vq_lock);
914 vdev_queue_io_add(vq, zio);
915 nio = vdev_queue_io_to_issue(vq);
916 mutex_exit(&vq->vq_lock);
921 if (nio->io_done == vdev_queue_agg_io_done) {
922 while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
923 ASSERT3U(dio->io_type, ==, nio->io_type);
924 zio_vdev_io_bypass(dio);
935 vdev_queue_io_done(zio_t *zio)
937 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
939 zio_link_t *zl = NULL;
941 hrtime_t now = gethrtime();
942 vq->vq_io_complete_ts = now;
943 vq->vq_io_delta_ts = zio->io_delta = now - zio->io_timestamp;
945 mutex_enter(&vq->vq_lock);
946 vdev_queue_pending_remove(vq, zio);
948 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
949 mutex_exit(&vq->vq_lock);
950 if (nio->io_done == vdev_queue_agg_io_done) {
951 while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
952 ASSERT3U(dio->io_type, ==, nio->io_type);
953 zio_vdev_io_bypass(dio);
958 zio_vdev_io_reissue(nio);
961 mutex_enter(&vq->vq_lock);
964 mutex_exit(&vq->vq_lock);
968 vdev_queue_change_io_priority(zio_t *zio, zio_priority_t priority)
970 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
974 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
975 * code to issue IOs without adding them to the vdev queue. In this
976 * case, the zio is already going to be issued as quickly as possible
977 * and so it doesn't need any reprioritization to help.
979 if (zio->io_priority == ZIO_PRIORITY_NOW)
982 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
983 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
985 if (zio->io_type == ZIO_TYPE_READ) {
986 if (priority != ZIO_PRIORITY_SYNC_READ &&
987 priority != ZIO_PRIORITY_ASYNC_READ &&
988 priority != ZIO_PRIORITY_SCRUB)
989 priority = ZIO_PRIORITY_ASYNC_READ;
991 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
992 if (priority != ZIO_PRIORITY_SYNC_WRITE &&
993 priority != ZIO_PRIORITY_ASYNC_WRITE)
994 priority = ZIO_PRIORITY_ASYNC_WRITE;
997 mutex_enter(&vq->vq_lock);
1000 * If the zio is in none of the queues we can simply change
1001 * the priority. If the zio is waiting to be submitted we must
1002 * remove it from the queue and re-insert it with the new priority.
1003 * Otherwise, the zio is currently active and we cannot change its
1006 tree = vdev_queue_class_tree(vq, zio->io_priority);
1007 if (avl_find(tree, zio, NULL) == zio) {
1008 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
1009 zio->io_priority = priority;
1010 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
1011 } else if (avl_find(&vq->vq_active_tree, zio, NULL) != zio) {
1012 zio->io_priority = priority;
1015 mutex_exit(&vq->vq_lock);
1019 * As these two methods are only used for load calculations we're not
1020 * concerned if we get an incorrect value on 32bit platforms due to lack of
1021 * vq_lock mutex use here, instead we prefer to keep it lock free for
1025 vdev_queue_length(vdev_t *vd)
1027 return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
1031 vdev_queue_last_offset(vdev_t *vd)
1033 return (vd->vdev_queue.vq_last_offset);
1036 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit, UINT, ZMOD_RW,
1037 "Max vdev I/O aggregation size");
1039 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit_non_rotating, UINT,
1040 ZMOD_RW, "Max vdev I/O aggregation size for non-rotating media");
1042 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregate_trim, UINT, ZMOD_RW,
1043 "Allow TRIM I/O to be aggregated");
1045 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, read_gap_limit, UINT, ZMOD_RW,
1046 "Aggregate read I/O over gap");
1048 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, write_gap_limit, UINT, ZMOD_RW,
1049 "Aggregate write I/O over gap");
1051 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_active, UINT, ZMOD_RW,
1052 "Maximum number of active I/Os per vdev");
1054 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_max_dirty_percent,
1055 UINT, ZMOD_RW, "Async write concurrency max threshold");
1057 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_min_dirty_percent,
1058 UINT, ZMOD_RW, "Async write concurrency min threshold");
1060 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_max_active, UINT, ZMOD_RW,
1061 "Max active async read I/Os per vdev");
1063 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_min_active, UINT, ZMOD_RW,
1064 "Min active async read I/Os per vdev");
1066 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_max_active, UINT, ZMOD_RW,
1067 "Max active async write I/Os per vdev");
1069 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_min_active, UINT, ZMOD_RW,
1070 "Min active async write I/Os per vdev");
1072 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_max_active, UINT, ZMOD_RW,
1073 "Max active initializing I/Os per vdev");
1075 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_min_active, UINT, ZMOD_RW,
1076 "Min active initializing I/Os per vdev");
1078 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_max_active, UINT, ZMOD_RW,
1079 "Max active removal I/Os per vdev");
1081 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_min_active, UINT, ZMOD_RW,
1082 "Min active removal I/Os per vdev");
1084 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_max_active, UINT, ZMOD_RW,
1085 "Max active scrub I/Os per vdev");
1087 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_min_active, UINT, ZMOD_RW,
1088 "Min active scrub I/Os per vdev");
1090 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_max_active, UINT, ZMOD_RW,
1091 "Max active sync read I/Os per vdev");
1093 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_min_active, UINT, ZMOD_RW,
1094 "Min active sync read I/Os per vdev");
1096 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_max_active, UINT, ZMOD_RW,
1097 "Max active sync write I/Os per vdev");
1099 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_min_active, UINT, ZMOD_RW,
1100 "Min active sync write I/Os per vdev");
1102 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_max_active, UINT, ZMOD_RW,
1103 "Max active trim/discard I/Os per vdev");
1105 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_min_active, UINT, ZMOD_RW,
1106 "Min active trim/discard I/Os per vdev");
1108 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_max_active, UINT, ZMOD_RW,
1109 "Max active rebuild I/Os per vdev");
1111 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_min_active, UINT, ZMOD_RW,
1112 "Min active rebuild I/Os per vdev");
1114 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_credit, UINT, ZMOD_RW,
1115 "Number of non-interactive I/Os to allow in sequence");
1117 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_delay, UINT, ZMOD_RW,
1118 "Number of non-interactive I/Os before _max_active");
1120 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, queue_depth_pct, UINT, ZMOD_RW,
1121 "Queue depth percentage for each top-level vdev");
1123 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, def_queue_depth, UINT, ZMOD_RW,
1124 "Default queue depth for each allocator");