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 http://www.opensolaris.org/os/licensing.
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, 2017 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>
38 #include <sys/spa_impl.h>
39 #include <sys/kstat.h>
46 * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
47 * I/O scheduler determines when and in what order those operations are
48 * issued. The I/O scheduler divides operations into five I/O classes
49 * prioritized in the following order: sync read, sync write, async read,
50 * async write, and scrub/resilver. Each queue defines the minimum and
51 * maximum number of concurrent operations that may be issued to the device.
52 * In addition, the device has an aggregate maximum. Note that the sum of the
53 * per-queue minimums must not exceed the aggregate maximum. If the
54 * sum of the per-queue maximums exceeds the aggregate maximum, then the
55 * number of active i/os may reach zfs_vdev_max_active, in which case no
56 * further i/os will be issued regardless of whether all per-queue
57 * minimums have been met.
59 * For many physical devices, throughput increases with the number of
60 * concurrent operations, but latency typically suffers. Further, physical
61 * devices typically have a limit at which more concurrent operations have no
62 * effect on throughput or can actually cause it to decrease.
64 * The scheduler selects the next operation to issue by first looking for an
65 * I/O class whose minimum has not been satisfied. Once all are satisfied and
66 * the aggregate maximum has not been hit, the scheduler looks for classes
67 * whose maximum has not been satisfied. Iteration through the I/O classes is
68 * done in the order specified above. No further operations are issued if the
69 * aggregate maximum number of concurrent operations has been hit or if there
70 * are no operations queued for an I/O class that has not hit its maximum.
71 * Every time an i/o is queued or an operation completes, the I/O scheduler
72 * looks for new operations to issue.
74 * All I/O classes have a fixed maximum number of outstanding operations
75 * except for the async write class. Asynchronous writes represent the data
76 * that is committed to stable storage during the syncing stage for
77 * transaction groups (see txg.c). Transaction groups enter the syncing state
78 * periodically so the number of queued async writes will quickly burst up and
79 * then bleed down to zero. Rather than servicing them as quickly as possible,
80 * the I/O scheduler changes the maximum number of active async write i/os
81 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
82 * both throughput and latency typically increase with the number of
83 * concurrent operations issued to physical devices, reducing the burstiness
84 * in the number of concurrent operations also stabilizes the response time of
85 * operations from other -- and in particular synchronous -- queues. In broad
86 * strokes, the I/O scheduler will issue more concurrent operations from the
87 * async write queue as there's more dirty data in the pool.
91 * The number of concurrent operations issued for the async write I/O class
92 * follows a piece-wise linear function defined by a few adjustable points.
94 * | o---------| <-- zfs_vdev_async_write_max_active
101 * |------------o | | <-- zfs_vdev_async_write_min_active
102 * 0|____________^______|_________|
103 * 0% | | 100% of zfs_dirty_data_max
105 * | `-- zfs_vdev_async_write_active_max_dirty_percent
106 * `--------- zfs_vdev_async_write_active_min_dirty_percent
108 * Until the amount of dirty data exceeds a minimum percentage of the dirty
109 * data allowed in the pool, the I/O scheduler will limit the number of
110 * concurrent operations to the minimum. As that threshold is crossed, the
111 * number of concurrent operations issued increases linearly to the maximum at
112 * the specified maximum percentage of the dirty data allowed in the pool.
114 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
115 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
116 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
117 * maximum percentage, this indicates that the rate of incoming data is
118 * greater than the rate that the backend storage can handle. In this case, we
119 * must further throttle incoming writes (see dmu_tx_delay() for details).
123 * The maximum number of i/os active to each device. Ideally, this will be >=
124 * the sum of each queue's max_active. It must be at least the sum of each
125 * queue's min_active.
127 uint32_t zfs_vdev_max_active
= 1000;
130 * Per-queue limits on the number of i/os active to each device. If the
131 * number of active i/os is < zfs_vdev_max_active, then the min_active comes
132 * into play. We will send min_active from each queue, and then select from
133 * queues in the order defined by zio_priority_t.
135 * In general, smaller max_active's will lead to lower latency of synchronous
136 * operations. Larger max_active's may lead to higher overall throughput,
137 * depending on underlying storage.
139 * The ratio of the queues' max_actives determines the balance of performance
140 * between reads, writes, and scrubs. E.g., increasing
141 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
142 * more quickly, but reads and writes to have higher latency and lower
145 uint32_t zfs_vdev_sync_read_min_active
= 10;
146 uint32_t zfs_vdev_sync_read_max_active
= 10;
147 uint32_t zfs_vdev_sync_write_min_active
= 10;
148 uint32_t zfs_vdev_sync_write_max_active
= 10;
149 uint32_t zfs_vdev_async_read_min_active
= 1;
150 uint32_t zfs_vdev_async_read_max_active
= 3;
151 uint32_t zfs_vdev_async_write_min_active
= 2;
152 uint32_t zfs_vdev_async_write_max_active
= 10;
153 uint32_t zfs_vdev_scrub_min_active
= 1;
154 uint32_t zfs_vdev_scrub_max_active
= 2;
155 uint32_t zfs_vdev_removal_min_active
= 1;
156 uint32_t zfs_vdev_removal_max_active
= 2;
159 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
160 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
161 * zfs_vdev_async_write_active_max_dirty_percent, use
162 * zfs_vdev_async_write_max_active. The value is linearly interpolated
163 * between min and max.
165 int zfs_vdev_async_write_active_min_dirty_percent
= 30;
166 int zfs_vdev_async_write_active_max_dirty_percent
= 60;
169 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
170 * For read I/Os, we also aggregate across small adjacency gaps; for writes
171 * we include spans of optional I/Os to aid aggregation at the disk even when
172 * they aren't able to help us aggregate at this level.
174 int zfs_vdev_aggregation_limit
= 1 << 20;
175 int zfs_vdev_read_gap_limit
= 32 << 10;
176 int zfs_vdev_write_gap_limit
= 4 << 10;
179 * Define the queue depth percentage for each top-level. This percentage is
180 * used in conjunction with zfs_vdev_async_max_active to determine how many
181 * allocations a specific top-level vdev should handle. Once the queue depth
182 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
183 * then allocator will stop allocating blocks on that top-level device.
184 * The default kernel setting is 1000% which will yield 100 allocations per
185 * device. For userland testing, the default setting is 300% which equates
186 * to 30 allocations per device.
189 int zfs_vdev_queue_depth_pct
= 1000;
191 int zfs_vdev_queue_depth_pct
= 300;
196 vdev_queue_offset_compare(const void *x1
, const void *x2
)
198 const zio_t
*z1
= (const zio_t
*)x1
;
199 const zio_t
*z2
= (const zio_t
*)x2
;
201 int cmp
= AVL_CMP(z1
->io_offset
, z2
->io_offset
);
206 return (AVL_PCMP(z1
, z2
));
209 static inline avl_tree_t
*
210 vdev_queue_class_tree(vdev_queue_t
*vq
, zio_priority_t p
)
212 return (&vq
->vq_class
[p
].vqc_queued_tree
);
215 static inline avl_tree_t
*
216 vdev_queue_type_tree(vdev_queue_t
*vq
, zio_type_t t
)
218 ASSERT(t
== ZIO_TYPE_READ
|| t
== ZIO_TYPE_WRITE
);
219 if (t
== ZIO_TYPE_READ
)
220 return (&vq
->vq_read_offset_tree
);
222 return (&vq
->vq_write_offset_tree
);
226 vdev_queue_timestamp_compare(const void *x1
, const void *x2
)
228 const zio_t
*z1
= (const zio_t
*)x1
;
229 const zio_t
*z2
= (const zio_t
*)x2
;
231 int cmp
= AVL_CMP(z1
->io_timestamp
, z2
->io_timestamp
);
236 return (AVL_PCMP(z1
, z2
));
240 vdev_queue_class_min_active(zio_priority_t p
)
243 case ZIO_PRIORITY_SYNC_READ
:
244 return (zfs_vdev_sync_read_min_active
);
245 case ZIO_PRIORITY_SYNC_WRITE
:
246 return (zfs_vdev_sync_write_min_active
);
247 case ZIO_PRIORITY_ASYNC_READ
:
248 return (zfs_vdev_async_read_min_active
);
249 case ZIO_PRIORITY_ASYNC_WRITE
:
250 return (zfs_vdev_async_write_min_active
);
251 case ZIO_PRIORITY_SCRUB
:
252 return (zfs_vdev_scrub_min_active
);
253 case ZIO_PRIORITY_REMOVAL
:
254 return (zfs_vdev_removal_min_active
);
256 panic("invalid priority %u", p
);
262 vdev_queue_max_async_writes(spa_t
*spa
)
266 dsl_pool_t
*dp
= spa_get_dsl(spa
);
267 uint64_t min_bytes
= zfs_dirty_data_max
*
268 zfs_vdev_async_write_active_min_dirty_percent
/ 100;
269 uint64_t max_bytes
= zfs_dirty_data_max
*
270 zfs_vdev_async_write_active_max_dirty_percent
/ 100;
273 * Async writes may occur before the assignment of the spa's
274 * dsl_pool_t if a self-healing zio is issued prior to the
275 * completion of dmu_objset_open_impl().
278 return (zfs_vdev_async_write_max_active
);
281 * Sync tasks correspond to interactive user actions. To reduce the
282 * execution time of those actions we push data out as fast as possible.
284 if (spa_has_pending_synctask(spa
))
285 return (zfs_vdev_async_write_max_active
);
287 dirty
= dp
->dp_dirty_total
;
288 if (dirty
< min_bytes
)
289 return (zfs_vdev_async_write_min_active
);
290 if (dirty
> max_bytes
)
291 return (zfs_vdev_async_write_max_active
);
294 * linear interpolation:
295 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
296 * move right by min_bytes
297 * move up by min_writes
299 writes
= (dirty
- min_bytes
) *
300 (zfs_vdev_async_write_max_active
-
301 zfs_vdev_async_write_min_active
) /
302 (max_bytes
- min_bytes
) +
303 zfs_vdev_async_write_min_active
;
304 ASSERT3U(writes
, >=, zfs_vdev_async_write_min_active
);
305 ASSERT3U(writes
, <=, zfs_vdev_async_write_max_active
);
310 vdev_queue_class_max_active(spa_t
*spa
, zio_priority_t p
)
313 case ZIO_PRIORITY_SYNC_READ
:
314 return (zfs_vdev_sync_read_max_active
);
315 case ZIO_PRIORITY_SYNC_WRITE
:
316 return (zfs_vdev_sync_write_max_active
);
317 case ZIO_PRIORITY_ASYNC_READ
:
318 return (zfs_vdev_async_read_max_active
);
319 case ZIO_PRIORITY_ASYNC_WRITE
:
320 return (vdev_queue_max_async_writes(spa
));
321 case ZIO_PRIORITY_SCRUB
:
322 return (zfs_vdev_scrub_max_active
);
323 case ZIO_PRIORITY_REMOVAL
:
324 return (zfs_vdev_removal_max_active
);
326 panic("invalid priority %u", p
);
332 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
333 * there is no eligible class.
335 static zio_priority_t
336 vdev_queue_class_to_issue(vdev_queue_t
*vq
)
338 spa_t
*spa
= vq
->vq_vdev
->vdev_spa
;
341 if (avl_numnodes(&vq
->vq_active_tree
) >= zfs_vdev_max_active
)
342 return (ZIO_PRIORITY_NUM_QUEUEABLE
);
344 /* find a queue that has not reached its minimum # outstanding i/os */
345 for (p
= 0; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++) {
346 if (avl_numnodes(vdev_queue_class_tree(vq
, p
)) > 0 &&
347 vq
->vq_class
[p
].vqc_active
<
348 vdev_queue_class_min_active(p
))
353 * If we haven't found a queue, look for one that hasn't reached its
354 * maximum # outstanding i/os.
356 for (p
= 0; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++) {
357 if (avl_numnodes(vdev_queue_class_tree(vq
, p
)) > 0 &&
358 vq
->vq_class
[p
].vqc_active
<
359 vdev_queue_class_max_active(spa
, p
))
363 /* No eligible queued i/os */
364 return (ZIO_PRIORITY_NUM_QUEUEABLE
);
368 vdev_queue_init(vdev_t
*vd
)
370 vdev_queue_t
*vq
= &vd
->vdev_queue
;
373 mutex_init(&vq
->vq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
375 taskq_init_ent(&vd
->vdev_queue
.vq_io_search
.io_tqent
);
377 avl_create(&vq
->vq_active_tree
, vdev_queue_offset_compare
,
378 sizeof (zio_t
), offsetof(struct zio
, io_queue_node
));
379 avl_create(vdev_queue_type_tree(vq
, ZIO_TYPE_READ
),
380 vdev_queue_offset_compare
, sizeof (zio_t
),
381 offsetof(struct zio
, io_offset_node
));
382 avl_create(vdev_queue_type_tree(vq
, ZIO_TYPE_WRITE
),
383 vdev_queue_offset_compare
, sizeof (zio_t
),
384 offsetof(struct zio
, io_offset_node
));
386 for (p
= 0; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++) {
387 int (*compfn
) (const void *, const void *);
390 * The synchronous i/o queues are dispatched in FIFO rather
391 * than LBA order. This provides more consistent latency for
394 if (p
== ZIO_PRIORITY_SYNC_READ
|| p
== ZIO_PRIORITY_SYNC_WRITE
)
395 compfn
= vdev_queue_timestamp_compare
;
397 compfn
= vdev_queue_offset_compare
;
398 avl_create(vdev_queue_class_tree(vq
, p
), compfn
,
399 sizeof (zio_t
), offsetof(struct zio
, io_queue_node
));
402 vq
->vq_last_offset
= 0;
406 vdev_queue_fini(vdev_t
*vd
)
408 vdev_queue_t
*vq
= &vd
->vdev_queue
;
410 for (zio_priority_t p
= 0; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++)
411 avl_destroy(vdev_queue_class_tree(vq
, p
));
412 avl_destroy(&vq
->vq_active_tree
);
413 avl_destroy(vdev_queue_type_tree(vq
, ZIO_TYPE_READ
));
414 avl_destroy(vdev_queue_type_tree(vq
, ZIO_TYPE_WRITE
));
416 mutex_destroy(&vq
->vq_lock
);
420 vdev_queue_io_add(vdev_queue_t
*vq
, zio_t
*zio
)
422 spa_t
*spa
= zio
->io_spa
;
423 spa_stats_history_t
*ssh
= &spa
->spa_stats
.io_history
;
425 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
426 avl_add(vdev_queue_class_tree(vq
, zio
->io_priority
), zio
);
427 avl_add(vdev_queue_type_tree(vq
, zio
->io_type
), zio
);
429 if (ssh
->kstat
!= NULL
) {
430 mutex_enter(&ssh
->lock
);
431 kstat_waitq_enter(ssh
->kstat
->ks_data
);
432 mutex_exit(&ssh
->lock
);
437 vdev_queue_io_remove(vdev_queue_t
*vq
, zio_t
*zio
)
439 spa_t
*spa
= zio
->io_spa
;
440 spa_stats_history_t
*ssh
= &spa
->spa_stats
.io_history
;
442 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
443 avl_remove(vdev_queue_class_tree(vq
, zio
->io_priority
), zio
);
444 avl_remove(vdev_queue_type_tree(vq
, zio
->io_type
), zio
);
446 if (ssh
->kstat
!= NULL
) {
447 mutex_enter(&ssh
->lock
);
448 kstat_waitq_exit(ssh
->kstat
->ks_data
);
449 mutex_exit(&ssh
->lock
);
454 vdev_queue_pending_add(vdev_queue_t
*vq
, zio_t
*zio
)
456 spa_t
*spa
= zio
->io_spa
;
457 spa_stats_history_t
*ssh
= &spa
->spa_stats
.io_history
;
459 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
460 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
461 vq
->vq_class
[zio
->io_priority
].vqc_active
++;
462 avl_add(&vq
->vq_active_tree
, zio
);
464 if (ssh
->kstat
!= NULL
) {
465 mutex_enter(&ssh
->lock
);
466 kstat_runq_enter(ssh
->kstat
->ks_data
);
467 mutex_exit(&ssh
->lock
);
472 vdev_queue_pending_remove(vdev_queue_t
*vq
, zio_t
*zio
)
474 spa_t
*spa
= zio
->io_spa
;
475 spa_stats_history_t
*ssh
= &spa
->spa_stats
.io_history
;
477 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
478 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
479 vq
->vq_class
[zio
->io_priority
].vqc_active
--;
480 avl_remove(&vq
->vq_active_tree
, zio
);
482 if (ssh
->kstat
!= NULL
) {
483 kstat_io_t
*ksio
= ssh
->kstat
->ks_data
;
485 mutex_enter(&ssh
->lock
);
486 kstat_runq_exit(ksio
);
487 if (zio
->io_type
== ZIO_TYPE_READ
) {
489 ksio
->nread
+= zio
->io_size
;
490 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
492 ksio
->nwritten
+= zio
->io_size
;
494 mutex_exit(&ssh
->lock
);
499 vdev_queue_agg_io_done(zio_t
*aio
)
501 if (aio
->io_type
== ZIO_TYPE_READ
) {
503 zio_link_t
*zl
= NULL
;
504 while ((pio
= zio_walk_parents(aio
, &zl
)) != NULL
) {
505 abd_copy_off(pio
->io_abd
, aio
->io_abd
,
506 0, pio
->io_offset
- aio
->io_offset
, pio
->io_size
);
510 abd_free(aio
->io_abd
);
514 * Compute the range spanned by two i/os, which is the endpoint of the last
515 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
516 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
517 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
519 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
520 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
523 vdev_queue_aggregate(vdev_queue_t
*vq
, zio_t
*zio
)
525 zio_t
*first
, *last
, *aio
, *dio
, *mandatory
, *nio
;
526 zio_link_t
*zl
= NULL
;
531 boolean_t stretch
= B_FALSE
;
532 avl_tree_t
*t
= vdev_queue_type_tree(vq
, zio
->io_type
);
533 enum zio_flag flags
= zio
->io_flags
& ZIO_FLAG_AGG_INHERIT
;
536 maxblocksize
= spa_maxblocksize(vq
->vq_vdev
->vdev_spa
);
537 limit
= MAX(MIN(zfs_vdev_aggregation_limit
, maxblocksize
), 0);
539 if (zio
->io_flags
& ZIO_FLAG_DONT_AGGREGATE
|| limit
== 0)
544 if (zio
->io_type
== ZIO_TYPE_READ
)
545 maxgap
= zfs_vdev_read_gap_limit
;
548 * We can aggregate I/Os that are sufficiently adjacent and of
549 * the same flavor, as expressed by the AGG_INHERIT flags.
550 * The latter requirement is necessary so that certain
551 * attributes of the I/O, such as whether it's a normal I/O
552 * or a scrub/resilver, can be preserved in the aggregate.
553 * We can include optional I/Os, but don't allow them
554 * to begin a range as they add no benefit in that situation.
558 * We keep track of the last non-optional I/O.
560 mandatory
= (first
->io_flags
& ZIO_FLAG_OPTIONAL
) ? NULL
: first
;
563 * Walk backwards through sufficiently contiguous I/Os
564 * recording the last non-optional I/O.
566 while ((dio
= AVL_PREV(t
, first
)) != NULL
&&
567 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
568 IO_SPAN(dio
, last
) <= limit
&&
569 IO_GAP(dio
, first
) <= maxgap
&&
570 dio
->io_type
== zio
->io_type
) {
572 if (mandatory
== NULL
&& !(first
->io_flags
& ZIO_FLAG_OPTIONAL
))
577 * Skip any initial optional I/Os.
579 while ((first
->io_flags
& ZIO_FLAG_OPTIONAL
) && first
!= last
) {
580 first
= AVL_NEXT(t
, first
);
581 ASSERT(first
!= NULL
);
586 * Walk forward through sufficiently contiguous I/Os.
587 * The aggregation limit does not apply to optional i/os, so that
588 * we can issue contiguous writes even if they are larger than the
591 while ((dio
= AVL_NEXT(t
, last
)) != NULL
&&
592 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
593 (IO_SPAN(first
, dio
) <= limit
||
594 (dio
->io_flags
& ZIO_FLAG_OPTIONAL
)) &&
595 IO_SPAN(first
, dio
) <= maxblocksize
&&
596 IO_GAP(last
, dio
) <= maxgap
&&
597 dio
->io_type
== zio
->io_type
) {
599 if (!(last
->io_flags
& ZIO_FLAG_OPTIONAL
))
604 * Now that we've established the range of the I/O aggregation
605 * we must decide what to do with trailing optional I/Os.
606 * For reads, there's nothing to do. While we are unable to
607 * aggregate further, it's possible that a trailing optional
608 * I/O would allow the underlying device to aggregate with
609 * subsequent I/Os. We must therefore determine if the next
610 * non-optional I/O is close enough to make aggregation
613 if (zio
->io_type
== ZIO_TYPE_WRITE
&& mandatory
!= NULL
) {
615 while ((dio
= AVL_NEXT(t
, nio
)) != NULL
&&
616 IO_GAP(nio
, dio
) == 0 &&
617 IO_GAP(mandatory
, dio
) <= zfs_vdev_write_gap_limit
) {
619 if (!(nio
->io_flags
& ZIO_FLAG_OPTIONAL
)) {
628 * We are going to include an optional io in our aggregated
629 * span, thus closing the write gap. Only mandatory i/os can
630 * start aggregated spans, so make sure that the next i/o
631 * after our span is mandatory.
633 dio
= AVL_NEXT(t
, last
);
634 dio
->io_flags
&= ~ZIO_FLAG_OPTIONAL
;
636 /* do not include the optional i/o */
637 while (last
!= mandatory
&& last
!= first
) {
638 ASSERT(last
->io_flags
& ZIO_FLAG_OPTIONAL
);
639 last
= AVL_PREV(t
, last
);
640 ASSERT(last
!= NULL
);
647 size
= IO_SPAN(first
, last
);
648 ASSERT3U(size
, <=, maxblocksize
);
650 abd
= abd_alloc_for_io(size
, B_TRUE
);
654 aio
= zio_vdev_delegated_io(first
->io_vd
, first
->io_offset
,
655 abd
, size
, first
->io_type
, zio
->io_priority
,
656 flags
| ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_QUEUE
,
657 vdev_queue_agg_io_done
, NULL
);
658 aio
->io_timestamp
= first
->io_timestamp
;
663 nio
= AVL_NEXT(t
, dio
);
664 ASSERT3U(dio
->io_type
, ==, aio
->io_type
);
666 if (dio
->io_flags
& ZIO_FLAG_NODATA
) {
667 ASSERT3U(dio
->io_type
, ==, ZIO_TYPE_WRITE
);
668 abd_zero_off(aio
->io_abd
,
669 dio
->io_offset
- aio
->io_offset
, dio
->io_size
);
670 } else if (dio
->io_type
== ZIO_TYPE_WRITE
) {
671 abd_copy_off(aio
->io_abd
, dio
->io_abd
,
672 dio
->io_offset
- aio
->io_offset
, 0, dio
->io_size
);
675 zio_add_child(dio
, aio
);
676 vdev_queue_io_remove(vq
, dio
);
677 } while (dio
!= last
);
680 * We need to drop the vdev queue's lock to avoid a deadlock that we
681 * could encounter since this I/O will complete immediately.
683 mutex_exit(&vq
->vq_lock
);
684 while ((dio
= zio_walk_parents(aio
, &zl
)) != NULL
) {
685 zio_vdev_io_bypass(dio
);
688 mutex_enter(&vq
->vq_lock
);
694 vdev_queue_io_to_issue(vdev_queue_t
*vq
)
702 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
704 p
= vdev_queue_class_to_issue(vq
);
706 if (p
== ZIO_PRIORITY_NUM_QUEUEABLE
) {
707 /* No eligible queued i/os */
712 * For LBA-ordered queues (async / scrub), issue the i/o which follows
713 * the most recently issued i/o in LBA (offset) order.
715 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
717 tree
= vdev_queue_class_tree(vq
, p
);
718 vq
->vq_io_search
.io_timestamp
= 0;
719 vq
->vq_io_search
.io_offset
= vq
->vq_last_offset
- 1;
720 VERIFY3P(avl_find(tree
, &vq
->vq_io_search
, &idx
), ==, NULL
);
721 zio
= avl_nearest(tree
, idx
, AVL_AFTER
);
723 zio
= avl_first(tree
);
724 ASSERT3U(zio
->io_priority
, ==, p
);
726 aio
= vdev_queue_aggregate(vq
, zio
);
730 vdev_queue_io_remove(vq
, zio
);
733 * If the I/O is or was optional and therefore has no data, we need to
734 * simply discard it. We need to drop the vdev queue's lock to avoid a
735 * deadlock that we could encounter since this I/O will complete
738 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
739 mutex_exit(&vq
->vq_lock
);
740 zio_vdev_io_bypass(zio
);
742 mutex_enter(&vq
->vq_lock
);
746 vdev_queue_pending_add(vq
, zio
);
747 vq
->vq_last_offset
= zio
->io_offset
+ zio
->io_size
;
753 vdev_queue_io(zio_t
*zio
)
755 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
758 if (zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)
762 * Children i/os inherent their parent's priority, which might
763 * not match the child's i/o type. Fix it up here.
765 if (zio
->io_type
== ZIO_TYPE_READ
) {
766 if (zio
->io_priority
!= ZIO_PRIORITY_SYNC_READ
&&
767 zio
->io_priority
!= ZIO_PRIORITY_ASYNC_READ
&&
768 zio
->io_priority
!= ZIO_PRIORITY_SCRUB
&&
769 zio
->io_priority
!= ZIO_PRIORITY_REMOVAL
)
770 zio
->io_priority
= ZIO_PRIORITY_ASYNC_READ
;
772 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
773 if (zio
->io_priority
!= ZIO_PRIORITY_SYNC_WRITE
&&
774 zio
->io_priority
!= ZIO_PRIORITY_ASYNC_WRITE
&&
775 zio
->io_priority
!= ZIO_PRIORITY_REMOVAL
)
776 zio
->io_priority
= ZIO_PRIORITY_ASYNC_WRITE
;
779 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_QUEUE
;
781 mutex_enter(&vq
->vq_lock
);
782 zio
->io_timestamp
= gethrtime();
783 vdev_queue_io_add(vq
, zio
);
784 nio
= vdev_queue_io_to_issue(vq
);
785 mutex_exit(&vq
->vq_lock
);
790 if (nio
->io_done
== vdev_queue_agg_io_done
) {
799 vdev_queue_io_done(zio_t
*zio
)
801 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
804 mutex_enter(&vq
->vq_lock
);
806 vdev_queue_pending_remove(vq
, zio
);
808 zio
->io_delta
= gethrtime() - zio
->io_timestamp
;
809 vq
->vq_io_complete_ts
= gethrtime();
810 vq
->vq_io_delta_ts
= vq
->vq_io_complete_ts
- zio
->io_timestamp
;
812 while ((nio
= vdev_queue_io_to_issue(vq
)) != NULL
) {
813 mutex_exit(&vq
->vq_lock
);
814 if (nio
->io_done
== vdev_queue_agg_io_done
) {
817 zio_vdev_io_reissue(nio
);
820 mutex_enter(&vq
->vq_lock
);
823 mutex_exit(&vq
->vq_lock
);
827 vdev_queue_change_io_priority(zio_t
*zio
, zio_priority_t priority
)
829 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
832 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
833 ASSERT3U(priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
835 if (zio
->io_type
== ZIO_TYPE_READ
) {
836 if (priority
!= ZIO_PRIORITY_SYNC_READ
&&
837 priority
!= ZIO_PRIORITY_ASYNC_READ
&&
838 priority
!= ZIO_PRIORITY_SCRUB
)
839 priority
= ZIO_PRIORITY_ASYNC_READ
;
841 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
842 if (priority
!= ZIO_PRIORITY_SYNC_WRITE
&&
843 priority
!= ZIO_PRIORITY_ASYNC_WRITE
)
844 priority
= ZIO_PRIORITY_ASYNC_WRITE
;
847 mutex_enter(&vq
->vq_lock
);
850 * If the zio is in none of the queues we can simply change
851 * the priority. If the zio is waiting to be submitted we must
852 * remove it from the queue and re-insert it with the new priority.
853 * Otherwise, the zio is currently active and we cannot change its
856 tree
= vdev_queue_class_tree(vq
, zio
->io_priority
);
857 if (avl_find(tree
, zio
, NULL
) == zio
) {
858 avl_remove(vdev_queue_class_tree(vq
, zio
->io_priority
), zio
);
859 zio
->io_priority
= priority
;
860 avl_add(vdev_queue_class_tree(vq
, zio
->io_priority
), zio
);
861 } else if (avl_find(&vq
->vq_active_tree
, zio
, NULL
) != zio
) {
862 zio
->io_priority
= priority
;
865 mutex_exit(&vq
->vq_lock
);
869 * As these two methods are only used for load calculations we're not
870 * concerned if we get an incorrect value on 32bit platforms due to lack of
871 * vq_lock mutex use here, instead we prefer to keep it lock free for
875 vdev_queue_length(vdev_t
*vd
)
877 return (avl_numnodes(&vd
->vdev_queue
.vq_active_tree
));
881 vdev_queue_last_offset(vdev_t
*vd
)
883 return (vd
->vdev_queue
.vq_last_offset
);
886 #if defined(_KERNEL) && defined(HAVE_SPL)
887 module_param(zfs_vdev_aggregation_limit
, int, 0644);
888 MODULE_PARM_DESC(zfs_vdev_aggregation_limit
, "Max vdev I/O aggregation size");
890 module_param(zfs_vdev_read_gap_limit
, int, 0644);
891 MODULE_PARM_DESC(zfs_vdev_read_gap_limit
, "Aggregate read I/O over gap");
893 module_param(zfs_vdev_write_gap_limit
, int, 0644);
894 MODULE_PARM_DESC(zfs_vdev_write_gap_limit
, "Aggregate write I/O over gap");
896 module_param(zfs_vdev_max_active
, int, 0644);
897 MODULE_PARM_DESC(zfs_vdev_max_active
, "Maximum number of active I/Os per vdev");
899 module_param(zfs_vdev_async_write_active_max_dirty_percent
, int, 0644);
900 MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent
,
901 "Async write concurrency max threshold");
903 module_param(zfs_vdev_async_write_active_min_dirty_percent
, int, 0644);
904 MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent
,
905 "Async write concurrency min threshold");
907 module_param(zfs_vdev_async_read_max_active
, int, 0644);
908 MODULE_PARM_DESC(zfs_vdev_async_read_max_active
,
909 "Max active async read I/Os per vdev");
911 module_param(zfs_vdev_async_read_min_active
, int, 0644);
912 MODULE_PARM_DESC(zfs_vdev_async_read_min_active
,
913 "Min active async read I/Os per vdev");
915 module_param(zfs_vdev_async_write_max_active
, int, 0644);
916 MODULE_PARM_DESC(zfs_vdev_async_write_max_active
,
917 "Max active async write I/Os per vdev");
919 module_param(zfs_vdev_async_write_min_active
, int, 0644);
920 MODULE_PARM_DESC(zfs_vdev_async_write_min_active
,
921 "Min active async write I/Os per vdev");
923 module_param(zfs_vdev_scrub_max_active
, int, 0644);
924 MODULE_PARM_DESC(zfs_vdev_scrub_max_active
, "Max active scrub I/Os per vdev");
926 module_param(zfs_vdev_scrub_min_active
, int, 0644);
927 MODULE_PARM_DESC(zfs_vdev_scrub_min_active
, "Min active scrub I/Os per vdev");
929 module_param(zfs_vdev_sync_read_max_active
, int, 0644);
930 MODULE_PARM_DESC(zfs_vdev_sync_read_max_active
,
931 "Max active sync read I/Os per vdev");
933 module_param(zfs_vdev_sync_read_min_active
, int, 0644);
934 MODULE_PARM_DESC(zfs_vdev_sync_read_min_active
,
935 "Min active sync read I/Os per vdev");
937 module_param(zfs_vdev_sync_write_max_active
, int, 0644);
938 MODULE_PARM_DESC(zfs_vdev_sync_write_max_active
,
939 "Max active sync write I/Os per vdev");
941 module_param(zfs_vdev_sync_write_min_active
, int, 0644);
942 MODULE_PARM_DESC(zfs_vdev_sync_write_min_active
,
943 "Min active sync write I/Os per vdev");
945 module_param(zfs_vdev_queue_depth_pct
, int, 0644);
946 MODULE_PARM_DESC(zfs_vdev_queue_depth_pct
,
947 "Queue depth percentage for each top-level vdev");