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 vdev_queue_offset_compare(const void *x1, const void *x2)
234 const zio_t *z1 = (const zio_t *)x1;
235 const zio_t *z2 = (const zio_t *)x2;
237 int cmp = TREE_CMP(z1->io_offset, z2->io_offset);
242 return (TREE_PCMP(z1, z2));
245 #define VDQ_T_SHIFT 29
248 vdev_queue_to_compare(const void *x1, const void *x2)
250 const zio_t *z1 = (const zio_t *)x1;
251 const zio_t *z2 = (const zio_t *)x2;
253 int tcmp = TREE_CMP(z1->io_timestamp >> VDQ_T_SHIFT,
254 z2->io_timestamp >> VDQ_T_SHIFT);
255 int ocmp = TREE_CMP(z1->io_offset, z2->io_offset);
256 int cmp = tcmp ? tcmp : ocmp;
258 if (likely(cmp | (z1->io_queue_state == ZIO_QS_NONE)))
261 return (TREE_PCMP(z1, z2));
264 static inline boolean_t
265 vdev_queue_class_fifo(zio_priority_t p)
267 return (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE ||
268 p == ZIO_PRIORITY_TRIM);
272 vdev_queue_class_add(vdev_queue_t *vq, zio_t *zio)
274 zio_priority_t p = zio->io_priority;
275 vq->vq_cqueued |= 1U << p;
276 if (vdev_queue_class_fifo(p))
277 list_insert_tail(&vq->vq_class[p].vqc_list, zio);
279 avl_add(&vq->vq_class[p].vqc_tree, zio);
283 vdev_queue_class_remove(vdev_queue_t *vq, zio_t *zio)
285 zio_priority_t p = zio->io_priority;
287 if (vdev_queue_class_fifo(p)) {
288 list_t *list = &vq->vq_class[p].vqc_list;
289 list_remove(list, zio);
290 empty = list_is_empty(list);
292 avl_tree_t *tree = &vq->vq_class[p].vqc_tree;
293 avl_remove(tree, zio);
294 empty = avl_is_empty(tree);
296 vq->vq_cqueued &= ~(empty << p);
300 vdev_queue_class_min_active(vdev_queue_t *vq, zio_priority_t p)
303 case ZIO_PRIORITY_SYNC_READ:
304 return (zfs_vdev_sync_read_min_active);
305 case ZIO_PRIORITY_SYNC_WRITE:
306 return (zfs_vdev_sync_write_min_active);
307 case ZIO_PRIORITY_ASYNC_READ:
308 return (zfs_vdev_async_read_min_active);
309 case ZIO_PRIORITY_ASYNC_WRITE:
310 return (zfs_vdev_async_write_min_active);
311 case ZIO_PRIORITY_SCRUB:
312 return (vq->vq_ia_active == 0 ? zfs_vdev_scrub_min_active :
313 MIN(vq->vq_nia_credit, zfs_vdev_scrub_min_active));
314 case ZIO_PRIORITY_REMOVAL:
315 return (vq->vq_ia_active == 0 ? zfs_vdev_removal_min_active :
316 MIN(vq->vq_nia_credit, zfs_vdev_removal_min_active));
317 case ZIO_PRIORITY_INITIALIZING:
318 return (vq->vq_ia_active == 0 ?zfs_vdev_initializing_min_active:
319 MIN(vq->vq_nia_credit, zfs_vdev_initializing_min_active));
320 case ZIO_PRIORITY_TRIM:
321 return (zfs_vdev_trim_min_active);
322 case ZIO_PRIORITY_REBUILD:
323 return (vq->vq_ia_active == 0 ? zfs_vdev_rebuild_min_active :
324 MIN(vq->vq_nia_credit, zfs_vdev_rebuild_min_active));
326 panic("invalid priority %u", p);
332 vdev_queue_max_async_writes(spa_t *spa)
336 dsl_pool_t *dp = spa_get_dsl(spa);
337 uint64_t min_bytes = zfs_dirty_data_max *
338 zfs_vdev_async_write_active_min_dirty_percent / 100;
339 uint64_t max_bytes = zfs_dirty_data_max *
340 zfs_vdev_async_write_active_max_dirty_percent / 100;
343 * Async writes may occur before the assignment of the spa's
344 * dsl_pool_t if a self-healing zio is issued prior to the
345 * completion of dmu_objset_open_impl().
348 return (zfs_vdev_async_write_max_active);
351 * Sync tasks correspond to interactive user actions. To reduce the
352 * execution time of those actions we push data out as fast as possible.
354 dirty = dp->dp_dirty_total;
355 if (dirty > max_bytes || spa_has_pending_synctask(spa))
356 return (zfs_vdev_async_write_max_active);
358 if (dirty < min_bytes)
359 return (zfs_vdev_async_write_min_active);
362 * linear interpolation:
363 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
364 * move right by min_bytes
365 * move up by min_writes
367 writes = (dirty - min_bytes) *
368 (zfs_vdev_async_write_max_active -
369 zfs_vdev_async_write_min_active) /
370 (max_bytes - min_bytes) +
371 zfs_vdev_async_write_min_active;
372 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
373 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
378 vdev_queue_class_max_active(vdev_queue_t *vq, zio_priority_t p)
381 case ZIO_PRIORITY_SYNC_READ:
382 return (zfs_vdev_sync_read_max_active);
383 case ZIO_PRIORITY_SYNC_WRITE:
384 return (zfs_vdev_sync_write_max_active);
385 case ZIO_PRIORITY_ASYNC_READ:
386 return (zfs_vdev_async_read_max_active);
387 case ZIO_PRIORITY_ASYNC_WRITE:
388 return (vdev_queue_max_async_writes(vq->vq_vdev->vdev_spa));
389 case ZIO_PRIORITY_SCRUB:
390 if (vq->vq_ia_active > 0) {
391 return (MIN(vq->vq_nia_credit,
392 zfs_vdev_scrub_min_active));
393 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
394 return (MAX(1, zfs_vdev_scrub_min_active));
395 return (zfs_vdev_scrub_max_active);
396 case ZIO_PRIORITY_REMOVAL:
397 if (vq->vq_ia_active > 0) {
398 return (MIN(vq->vq_nia_credit,
399 zfs_vdev_removal_min_active));
400 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
401 return (MAX(1, zfs_vdev_removal_min_active));
402 return (zfs_vdev_removal_max_active);
403 case ZIO_PRIORITY_INITIALIZING:
404 if (vq->vq_ia_active > 0) {
405 return (MIN(vq->vq_nia_credit,
406 zfs_vdev_initializing_min_active));
407 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
408 return (MAX(1, zfs_vdev_initializing_min_active));
409 return (zfs_vdev_initializing_max_active);
410 case ZIO_PRIORITY_TRIM:
411 return (zfs_vdev_trim_max_active);
412 case ZIO_PRIORITY_REBUILD:
413 if (vq->vq_ia_active > 0) {
414 return (MIN(vq->vq_nia_credit,
415 zfs_vdev_rebuild_min_active));
416 } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
417 return (MAX(1, zfs_vdev_rebuild_min_active));
418 return (zfs_vdev_rebuild_max_active);
420 panic("invalid priority %u", p);
426 * Return the i/o class to issue from, or ZIO_PRIORITY_NUM_QUEUEABLE if
427 * there is no eligible class.
429 static zio_priority_t
430 vdev_queue_class_to_issue(vdev_queue_t *vq)
432 uint32_t cq = vq->vq_cqueued;
433 zio_priority_t p, p1;
435 if (cq == 0 || vq->vq_active >= zfs_vdev_max_active)
436 return (ZIO_PRIORITY_NUM_QUEUEABLE);
439 * Find a queue that has not reached its minimum # outstanding i/os.
440 * Do round-robin to reduce starvation due to zfs_vdev_max_active
441 * and vq_nia_credit limits.
443 p1 = vq->vq_last_prio + 1;
444 if (p1 >= ZIO_PRIORITY_NUM_QUEUEABLE)
446 for (p = p1; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
447 if ((cq & (1U << p)) != 0 && vq->vq_cactive[p] <
448 vdev_queue_class_min_active(vq, p))
451 for (p = 0; p < p1; p++) {
452 if ((cq & (1U << p)) != 0 && vq->vq_cactive[p] <
453 vdev_queue_class_min_active(vq, p))
458 * If we haven't found a queue, look for one that hasn't reached its
459 * maximum # outstanding i/os.
461 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
462 if ((cq & (1U << p)) != 0 && vq->vq_cactive[p] <
463 vdev_queue_class_max_active(vq, p))
468 vq->vq_last_prio = p;
473 vdev_queue_init(vdev_t *vd)
475 vdev_queue_t *vq = &vd->vdev_queue;
480 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
481 if (vdev_queue_class_fifo(p)) {
482 list_create(&vq->vq_class[p].vqc_list,
484 offsetof(struct zio, io_queue_node.l));
486 avl_create(&vq->vq_class[p].vqc_tree,
487 vdev_queue_to_compare, sizeof (zio_t),
488 offsetof(struct zio, io_queue_node.a));
491 avl_create(&vq->vq_read_offset_tree,
492 vdev_queue_offset_compare, sizeof (zio_t),
493 offsetof(struct zio, io_offset_node));
494 avl_create(&vq->vq_write_offset_tree,
495 vdev_queue_offset_compare, sizeof (zio_t),
496 offsetof(struct zio, io_offset_node));
498 vq->vq_last_offset = 0;
499 list_create(&vq->vq_active_list, sizeof (struct zio),
500 offsetof(struct zio, io_queue_node.l));
501 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
505 vdev_queue_fini(vdev_t *vd)
507 vdev_queue_t *vq = &vd->vdev_queue;
509 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
510 if (vdev_queue_class_fifo(p))
511 list_destroy(&vq->vq_class[p].vqc_list);
513 avl_destroy(&vq->vq_class[p].vqc_tree);
515 avl_destroy(&vq->vq_read_offset_tree);
516 avl_destroy(&vq->vq_write_offset_tree);
518 list_destroy(&vq->vq_active_list);
519 mutex_destroy(&vq->vq_lock);
523 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
525 zio->io_queue_state = ZIO_QS_QUEUED;
526 vdev_queue_class_add(vq, zio);
527 if (zio->io_type == ZIO_TYPE_READ)
528 avl_add(&vq->vq_read_offset_tree, zio);
529 else if (zio->io_type == ZIO_TYPE_WRITE)
530 avl_add(&vq->vq_write_offset_tree, zio);
534 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
536 vdev_queue_class_remove(vq, zio);
537 if (zio->io_type == ZIO_TYPE_READ)
538 avl_remove(&vq->vq_read_offset_tree, zio);
539 else if (zio->io_type == ZIO_TYPE_WRITE)
540 avl_remove(&vq->vq_write_offset_tree, zio);
541 zio->io_queue_state = ZIO_QS_NONE;
545 vdev_queue_is_interactive(zio_priority_t p)
548 case ZIO_PRIORITY_SCRUB:
549 case ZIO_PRIORITY_REMOVAL:
550 case ZIO_PRIORITY_INITIALIZING:
551 case ZIO_PRIORITY_REBUILD:
559 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
561 ASSERT(MUTEX_HELD(&vq->vq_lock));
562 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
563 vq->vq_cactive[zio->io_priority]++;
565 if (vdev_queue_is_interactive(zio->io_priority)) {
566 if (++vq->vq_ia_active == 1)
567 vq->vq_nia_credit = 1;
568 } else if (vq->vq_ia_active > 0) {
571 zio->io_queue_state = ZIO_QS_ACTIVE;
572 list_insert_tail(&vq->vq_active_list, zio);
576 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
578 ASSERT(MUTEX_HELD(&vq->vq_lock));
579 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
580 vq->vq_cactive[zio->io_priority]--;
582 if (vdev_queue_is_interactive(zio->io_priority)) {
583 if (--vq->vq_ia_active == 0)
584 vq->vq_nia_credit = 0;
586 vq->vq_nia_credit = zfs_vdev_nia_credit;
587 } else if (vq->vq_ia_active == 0)
589 list_remove(&vq->vq_active_list, zio);
590 zio->io_queue_state = ZIO_QS_NONE;
594 vdev_queue_agg_io_done(zio_t *aio)
596 abd_free(aio->io_abd);
600 * Compute the range spanned by two i/os, which is the endpoint of the last
601 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
602 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
603 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
605 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
606 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
609 * Sufficiently adjacent io_offset's in ZIOs will be aggregated. We do this
610 * by creating a gang ABD from the adjacent ZIOs io_abd's. By using
611 * a gang ABD we avoid doing memory copies to and from the parent,
612 * child ZIOs. The gang ABD also accounts for gaps between adjacent
613 * io_offsets by simply getting the zero ABD for writes or allocating
614 * a new ABD for reads and placing them in the gang ABD as well.
617 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
619 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
623 boolean_t stretch = B_FALSE;
624 uint64_t next_offset;
629 * TRIM aggregation should not be needed since code in zfs_trim.c can
630 * submit TRIM I/O for extents up to zfs_trim_extent_bytes_max (128M).
632 if (zio->io_type == ZIO_TYPE_TRIM)
635 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
638 if (vq->vq_vdev->vdev_nonrot)
639 limit = zfs_vdev_aggregation_limit_non_rotating;
641 limit = zfs_vdev_aggregation_limit;
644 limit = MIN(limit, SPA_MAXBLOCKSIZE);
647 * I/Os to distributed spares are directly dispatched to the dRAID
648 * leaf vdevs for aggregation. See the comment at the end of the
649 * zio_vdev_io_start() function.
651 ASSERT(vq->vq_vdev->vdev_ops != &vdev_draid_spare_ops);
655 if (zio->io_type == ZIO_TYPE_READ) {
656 maxgap = zfs_vdev_read_gap_limit;
657 t = &vq->vq_read_offset_tree;
659 ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
660 t = &vq->vq_write_offset_tree;
664 * We can aggregate I/Os that are sufficiently adjacent and of
665 * the same flavor, as expressed by the AGG_INHERIT flags.
666 * The latter requirement is necessary so that certain
667 * attributes of the I/O, such as whether it's a normal I/O
668 * or a scrub/resilver, can be preserved in the aggregate.
669 * We can include optional I/Os, but don't allow them
670 * to begin a range as they add no benefit in that situation.
674 * We keep track of the last non-optional I/O.
676 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
679 * Walk backwards through sufficiently contiguous I/Os
680 * recording the last non-optional I/O.
682 zio_flag_t flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
683 while ((dio = AVL_PREV(t, first)) != NULL &&
684 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
685 IO_SPAN(dio, last) <= limit &&
686 IO_GAP(dio, first) <= maxgap &&
687 dio->io_type == zio->io_type) {
689 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
694 * Skip any initial optional I/Os.
696 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
697 first = AVL_NEXT(t, first);
698 ASSERT(first != NULL);
703 * Walk forward through sufficiently contiguous I/Os.
704 * The aggregation limit does not apply to optional i/os, so that
705 * we can issue contiguous writes even if they are larger than the
708 while ((dio = AVL_NEXT(t, last)) != NULL &&
709 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
710 (IO_SPAN(first, dio) <= limit ||
711 (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
712 IO_SPAN(first, dio) <= SPA_MAXBLOCKSIZE &&
713 IO_GAP(last, dio) <= maxgap &&
714 dio->io_type == zio->io_type) {
716 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
721 * Now that we've established the range of the I/O aggregation
722 * we must decide what to do with trailing optional I/Os.
723 * For reads, there's nothing to do. While we are unable to
724 * aggregate further, it's possible that a trailing optional
725 * I/O would allow the underlying device to aggregate with
726 * subsequent I/Os. We must therefore determine if the next
727 * non-optional I/O is close enough to make aggregation
730 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
732 while ((dio = AVL_NEXT(t, nio)) != NULL &&
733 IO_GAP(nio, dio) == 0 &&
734 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
736 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
745 * We are going to include an optional io in our aggregated
746 * span, thus closing the write gap. Only mandatory i/os can
747 * start aggregated spans, so make sure that the next i/o
748 * after our span is mandatory.
750 dio = AVL_NEXT(t, last);
751 ASSERT3P(dio, !=, NULL);
752 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
754 /* do not include the optional i/o */
755 while (last != mandatory && last != first) {
756 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
757 last = AVL_PREV(t, last);
758 ASSERT(last != NULL);
765 size = IO_SPAN(first, last);
766 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
768 abd = abd_alloc_gang();
772 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
773 abd, size, first->io_type, zio->io_priority,
774 flags | ZIO_FLAG_DONT_QUEUE, vdev_queue_agg_io_done, NULL);
775 aio->io_timestamp = first->io_timestamp;
778 next_offset = first->io_offset;
781 nio = AVL_NEXT(t, dio);
782 ASSERT3P(dio, !=, NULL);
783 zio_add_child(dio, aio);
784 vdev_queue_io_remove(vq, dio);
786 if (dio->io_offset != next_offset) {
787 /* allocate a buffer for a read gap */
788 ASSERT3U(dio->io_type, ==, ZIO_TYPE_READ);
789 ASSERT3U(dio->io_offset, >, next_offset);
790 abd = abd_alloc_for_io(
791 dio->io_offset - next_offset, B_TRUE);
792 abd_gang_add(aio->io_abd, abd, B_TRUE);
795 (dio->io_size != abd_get_size(dio->io_abd))) {
796 /* abd size not the same as IO size */
797 ASSERT3U(abd_get_size(dio->io_abd), >, dio->io_size);
798 abd = abd_get_offset_size(dio->io_abd, 0, dio->io_size);
799 abd_gang_add(aio->io_abd, abd, B_TRUE);
801 if (dio->io_flags & ZIO_FLAG_NODATA) {
802 /* allocate a buffer for a write gap */
803 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
804 ASSERT3P(dio->io_abd, ==, NULL);
805 abd_gang_add(aio->io_abd,
806 abd_get_zeros(dio->io_size), B_TRUE);
809 * We pass B_FALSE to abd_gang_add()
810 * because we did not allocate a new
811 * ABD, so it is assumed the caller
812 * will free this ABD.
814 abd_gang_add(aio->io_abd, dio->io_abd,
818 next_offset = dio->io_offset + dio->io_size;
819 } while (dio != last);
820 ASSERT3U(abd_get_size(aio->io_abd), ==, aio->io_size);
823 * Callers must call zio_vdev_io_bypass() and zio_execute() for
824 * aggregated (parent) I/Os so that we could avoid dropping the
825 * queue's lock here to avoid a deadlock that we could encounter
826 * due to lock order reversal between vq_lock and io_lock in
827 * zio_change_priority().
833 vdev_queue_io_to_issue(vdev_queue_t *vq)
841 ASSERT(MUTEX_HELD(&vq->vq_lock));
843 p = vdev_queue_class_to_issue(vq);
845 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
846 /* No eligible queued i/os */
850 if (vdev_queue_class_fifo(p)) {
851 zio = list_head(&vq->vq_class[p].vqc_list);
854 * For LBA-ordered queues (async / scrub / initializing),
855 * issue the I/O which follows the most recently issued I/O
856 * in LBA (offset) order, but to avoid starvation only within
857 * the same 0.5 second interval as the first I/O.
859 tree = &vq->vq_class[p].vqc_tree;
860 zio = aio = avl_first(tree);
861 if (zio->io_offset < vq->vq_last_offset) {
862 vq->vq_io_search.io_timestamp = zio->io_timestamp;
863 vq->vq_io_search.io_offset = vq->vq_last_offset;
864 zio = avl_find(tree, &vq->vq_io_search, &idx);
866 zio = avl_nearest(tree, idx, AVL_AFTER);
868 (zio->io_timestamp >> VDQ_T_SHIFT) !=
869 (aio->io_timestamp >> VDQ_T_SHIFT))
874 ASSERT3U(zio->io_priority, ==, p);
876 aio = vdev_queue_aggregate(vq, zio);
880 vdev_queue_io_remove(vq, zio);
883 * If the I/O is or was optional and therefore has no data, we
884 * need to simply discard it. We need to drop the vdev queue's
885 * lock to avoid a deadlock that we could encounter since this
886 * I/O will complete immediately.
888 if (zio->io_flags & ZIO_FLAG_NODATA) {
889 mutex_exit(&vq->vq_lock);
890 zio_vdev_io_bypass(zio);
892 mutex_enter(&vq->vq_lock);
897 vdev_queue_pending_add(vq, zio);
898 vq->vq_last_offset = zio->io_offset + zio->io_size;
904 vdev_queue_io(zio_t *zio)
906 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
908 zio_link_t *zl = NULL;
910 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
914 * Children i/os inherent their parent's priority, which might
915 * not match the child's i/o type. Fix it up here.
917 if (zio->io_type == ZIO_TYPE_READ) {
918 ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
920 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
921 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
922 zio->io_priority != ZIO_PRIORITY_SCRUB &&
923 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
924 zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
925 zio->io_priority != ZIO_PRIORITY_REBUILD) {
926 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
928 } else if (zio->io_type == ZIO_TYPE_WRITE) {
929 ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
931 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
932 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
933 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
934 zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
935 zio->io_priority != ZIO_PRIORITY_REBUILD) {
936 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
939 ASSERT(zio->io_type == ZIO_TYPE_TRIM);
940 ASSERT(zio->io_priority == ZIO_PRIORITY_TRIM);
943 zio->io_flags |= ZIO_FLAG_DONT_QUEUE;
944 zio->io_timestamp = gethrtime();
946 mutex_enter(&vq->vq_lock);
947 vdev_queue_io_add(vq, zio);
948 nio = vdev_queue_io_to_issue(vq);
949 mutex_exit(&vq->vq_lock);
954 if (nio->io_done == vdev_queue_agg_io_done) {
955 while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
956 ASSERT3U(dio->io_type, ==, nio->io_type);
957 zio_vdev_io_bypass(dio);
968 vdev_queue_io_done(zio_t *zio)
970 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
972 zio_link_t *zl = NULL;
974 hrtime_t now = gethrtime();
975 vq->vq_io_complete_ts = now;
976 vq->vq_io_delta_ts = zio->io_delta = now - zio->io_timestamp;
978 mutex_enter(&vq->vq_lock);
979 vdev_queue_pending_remove(vq, zio);
981 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
982 mutex_exit(&vq->vq_lock);
983 if (nio->io_done == vdev_queue_agg_io_done) {
984 while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
985 ASSERT3U(dio->io_type, ==, nio->io_type);
986 zio_vdev_io_bypass(dio);
991 zio_vdev_io_reissue(nio);
994 mutex_enter(&vq->vq_lock);
997 mutex_exit(&vq->vq_lock);
1001 vdev_queue_change_io_priority(zio_t *zio, zio_priority_t priority)
1003 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
1006 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
1007 * code to issue IOs without adding them to the vdev queue. In this
1008 * case, the zio is already going to be issued as quickly as possible
1009 * and so it doesn't need any reprioritization to help.
1011 if (zio->io_priority == ZIO_PRIORITY_NOW)
1014 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
1015 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
1017 if (zio->io_type == ZIO_TYPE_READ) {
1018 if (priority != ZIO_PRIORITY_SYNC_READ &&
1019 priority != ZIO_PRIORITY_ASYNC_READ &&
1020 priority != ZIO_PRIORITY_SCRUB)
1021 priority = ZIO_PRIORITY_ASYNC_READ;
1023 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1024 if (priority != ZIO_PRIORITY_SYNC_WRITE &&
1025 priority != ZIO_PRIORITY_ASYNC_WRITE)
1026 priority = ZIO_PRIORITY_ASYNC_WRITE;
1029 mutex_enter(&vq->vq_lock);
1032 * If the zio is in none of the queues we can simply change
1033 * the priority. If the zio is waiting to be submitted we must
1034 * remove it from the queue and re-insert it with the new priority.
1035 * Otherwise, the zio is currently active and we cannot change its
1038 if (zio->io_queue_state == ZIO_QS_QUEUED) {
1039 vdev_queue_class_remove(vq, zio);
1040 zio->io_priority = priority;
1041 vdev_queue_class_add(vq, zio);
1042 } else if (zio->io_queue_state == ZIO_QS_NONE) {
1043 zio->io_priority = priority;
1046 mutex_exit(&vq->vq_lock);
1050 * As these two methods are only used for load calculations we're not
1051 * concerned if we get an incorrect value on 32bit platforms due to lack of
1052 * vq_lock mutex use here, instead we prefer to keep it lock free for
1056 vdev_queue_length(vdev_t *vd)
1058 return (vd->vdev_queue.vq_active);
1062 vdev_queue_last_offset(vdev_t *vd)
1064 return (vd->vdev_queue.vq_last_offset);
1068 vdev_queue_class_length(vdev_t *vd, zio_priority_t p)
1070 vdev_queue_t *vq = &vd->vdev_queue;
1071 if (vdev_queue_class_fifo(p))
1072 return (list_is_empty(&vq->vq_class[p].vqc_list) == 0);
1074 return (avl_numnodes(&vq->vq_class[p].vqc_tree));
1077 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit, UINT, ZMOD_RW,
1078 "Max vdev I/O aggregation size");
1080 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit_non_rotating, UINT,
1081 ZMOD_RW, "Max vdev I/O aggregation size for non-rotating media");
1083 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, read_gap_limit, UINT, ZMOD_RW,
1084 "Aggregate read I/O over gap");
1086 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, write_gap_limit, UINT, ZMOD_RW,
1087 "Aggregate write I/O over gap");
1089 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_active, UINT, ZMOD_RW,
1090 "Maximum number of active I/Os per vdev");
1092 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_max_dirty_percent,
1093 UINT, ZMOD_RW, "Async write concurrency max threshold");
1095 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_min_dirty_percent,
1096 UINT, ZMOD_RW, "Async write concurrency min threshold");
1098 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_max_active, UINT, ZMOD_RW,
1099 "Max active async read I/Os per vdev");
1101 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_min_active, UINT, ZMOD_RW,
1102 "Min active async read I/Os per vdev");
1104 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_max_active, UINT, ZMOD_RW,
1105 "Max active async write I/Os per vdev");
1107 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_min_active, UINT, ZMOD_RW,
1108 "Min active async write I/Os per vdev");
1110 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_max_active, UINT, ZMOD_RW,
1111 "Max active initializing I/Os per vdev");
1113 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_min_active, UINT, ZMOD_RW,
1114 "Min active initializing I/Os per vdev");
1116 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_max_active, UINT, ZMOD_RW,
1117 "Max active removal I/Os per vdev");
1119 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_min_active, UINT, ZMOD_RW,
1120 "Min active removal I/Os per vdev");
1122 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_max_active, UINT, ZMOD_RW,
1123 "Max active scrub I/Os per vdev");
1125 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_min_active, UINT, ZMOD_RW,
1126 "Min active scrub I/Os per vdev");
1128 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_max_active, UINT, ZMOD_RW,
1129 "Max active sync read I/Os per vdev");
1131 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_min_active, UINT, ZMOD_RW,
1132 "Min active sync read I/Os per vdev");
1134 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_max_active, UINT, ZMOD_RW,
1135 "Max active sync write I/Os per vdev");
1137 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_min_active, UINT, ZMOD_RW,
1138 "Min active sync write I/Os per vdev");
1140 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_max_active, UINT, ZMOD_RW,
1141 "Max active trim/discard I/Os per vdev");
1143 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_min_active, UINT, ZMOD_RW,
1144 "Min active trim/discard I/Os per vdev");
1146 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_max_active, UINT, ZMOD_RW,
1147 "Max active rebuild I/Os per vdev");
1149 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_min_active, UINT, ZMOD_RW,
1150 "Min active rebuild I/Os per vdev");
1152 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_credit, UINT, ZMOD_RW,
1153 "Number of non-interactive I/Os to allow in sequence");
1155 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_delay, UINT, ZMOD_RW,
1156 "Number of non-interactive I/Os before _max_active");
1158 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, queue_depth_pct, UINT, ZMOD_RW,
1159 "Queue depth percentage for each top-level vdev");
1161 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, def_queue_depth, UINT, ZMOD_RW,
1162 "Default queue depth for each allocator");