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, 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 uint32_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 uint32_t zfs_vdev_sync_read_min_active
= 10;
145 uint32_t zfs_vdev_sync_read_max_active
= 10;
146 uint32_t zfs_vdev_sync_write_min_active
= 10;
147 uint32_t zfs_vdev_sync_write_max_active
= 10;
148 uint32_t zfs_vdev_async_read_min_active
= 1;
149 uint32_t zfs_vdev_async_read_max_active
= 3;
150 uint32_t zfs_vdev_async_write_min_active
= 2;
151 uint32_t zfs_vdev_async_write_max_active
= 10;
152 uint32_t zfs_vdev_scrub_min_active
= 1;
153 uint32_t zfs_vdev_scrub_max_active
= 3;
154 uint32_t zfs_vdev_removal_min_active
= 1;
155 uint32_t zfs_vdev_removal_max_active
= 2;
156 uint32_t zfs_vdev_initializing_min_active
= 1;
157 uint32_t zfs_vdev_initializing_max_active
= 1;
158 uint32_t zfs_vdev_trim_min_active
= 1;
159 uint32_t zfs_vdev_trim_max_active
= 2;
160 uint32_t zfs_vdev_rebuild_min_active
= 1;
161 uint32_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 int zfs_vdev_async_write_active_min_dirty_percent
= 30;
171 int 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 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 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 int zfs_vdev_aggregation_limit
= 1 << 20;
202 int zfs_vdev_aggregation_limit_non_rotating
= SPA_OLD_MAXBLOCKSIZE
;
203 int zfs_vdev_read_gap_limit
= 32 << 10;
204 int 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 int zfs_vdev_queue_depth_pct
= 1000;
219 int 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 int 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 int 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_MAX_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
;
602 zio_link_t
*zl
= NULL
;
607 boolean_t stretch
= B_FALSE
;
608 avl_tree_t
*t
= vdev_queue_type_tree(vq
, zio
->io_type
);
609 enum zio_flag flags
= zio
->io_flags
& ZIO_FLAG_AGG_INHERIT
;
610 uint64_t next_offset
;
613 maxblocksize
= spa_maxblocksize(vq
->vq_vdev
->vdev_spa
);
614 if (vq
->vq_vdev
->vdev_nonrot
)
615 limit
= zfs_vdev_aggregation_limit_non_rotating
;
617 limit
= zfs_vdev_aggregation_limit
;
618 limit
= MAX(MIN(limit
, maxblocksize
), 0);
620 if (zio
->io_flags
& ZIO_FLAG_DONT_AGGREGATE
|| limit
== 0)
624 * While TRIM commands could be aggregated based on offset this
625 * behavior is disabled until it's determined to be beneficial.
627 if (zio
->io_type
== ZIO_TYPE_TRIM
&& !zfs_vdev_aggregate_trim
)
631 * I/Os to distributed spares are directly dispatched to the dRAID
632 * leaf vdevs for aggregation. See the comment at the end of the
633 * zio_vdev_io_start() function.
635 ASSERT(vq
->vq_vdev
->vdev_ops
!= &vdev_draid_spare_ops
);
639 if (zio
->io_type
== ZIO_TYPE_READ
)
640 maxgap
= zfs_vdev_read_gap_limit
;
643 * We can aggregate I/Os that are sufficiently adjacent and of
644 * the same flavor, as expressed by the AGG_INHERIT flags.
645 * The latter requirement is necessary so that certain
646 * attributes of the I/O, such as whether it's a normal I/O
647 * or a scrub/resilver, can be preserved in the aggregate.
648 * We can include optional I/Os, but don't allow them
649 * to begin a range as they add no benefit in that situation.
653 * We keep track of the last non-optional I/O.
655 mandatory
= (first
->io_flags
& ZIO_FLAG_OPTIONAL
) ? NULL
: first
;
658 * Walk backwards through sufficiently contiguous I/Os
659 * recording the last non-optional I/O.
661 while ((dio
= AVL_PREV(t
, first
)) != NULL
&&
662 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
663 IO_SPAN(dio
, last
) <= limit
&&
664 IO_GAP(dio
, first
) <= maxgap
&&
665 dio
->io_type
== zio
->io_type
) {
667 if (mandatory
== NULL
&& !(first
->io_flags
& ZIO_FLAG_OPTIONAL
))
672 * Skip any initial optional I/Os.
674 while ((first
->io_flags
& ZIO_FLAG_OPTIONAL
) && first
!= last
) {
675 first
= AVL_NEXT(t
, first
);
676 ASSERT(first
!= NULL
);
681 * Walk forward through sufficiently contiguous I/Os.
682 * The aggregation limit does not apply to optional i/os, so that
683 * we can issue contiguous writes even if they are larger than the
686 while ((dio
= AVL_NEXT(t
, last
)) != NULL
&&
687 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
688 (IO_SPAN(first
, dio
) <= limit
||
689 (dio
->io_flags
& ZIO_FLAG_OPTIONAL
)) &&
690 IO_SPAN(first
, dio
) <= maxblocksize
&&
691 IO_GAP(last
, dio
) <= maxgap
&&
692 dio
->io_type
== zio
->io_type
) {
694 if (!(last
->io_flags
& ZIO_FLAG_OPTIONAL
))
699 * Now that we've established the range of the I/O aggregation
700 * we must decide what to do with trailing optional I/Os.
701 * For reads, there's nothing to do. While we are unable to
702 * aggregate further, it's possible that a trailing optional
703 * I/O would allow the underlying device to aggregate with
704 * subsequent I/Os. We must therefore determine if the next
705 * non-optional I/O is close enough to make aggregation
708 if (zio
->io_type
== ZIO_TYPE_WRITE
&& mandatory
!= NULL
) {
710 while ((dio
= AVL_NEXT(t
, nio
)) != NULL
&&
711 IO_GAP(nio
, dio
) == 0 &&
712 IO_GAP(mandatory
, dio
) <= zfs_vdev_write_gap_limit
) {
714 if (!(nio
->io_flags
& ZIO_FLAG_OPTIONAL
)) {
723 * We are going to include an optional io in our aggregated
724 * span, thus closing the write gap. Only mandatory i/os can
725 * start aggregated spans, so make sure that the next i/o
726 * after our span is mandatory.
728 dio
= AVL_NEXT(t
, last
);
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 zio_add_child(dio
, aio
);
761 vdev_queue_io_remove(vq
, dio
);
763 if (dio
->io_offset
!= next_offset
) {
764 /* allocate a buffer for a read gap */
765 ASSERT3U(dio
->io_type
, ==, ZIO_TYPE_READ
);
766 ASSERT3U(dio
->io_offset
, >, next_offset
);
767 abd
= abd_alloc_for_io(
768 dio
->io_offset
- next_offset
, B_TRUE
);
769 abd_gang_add(aio
->io_abd
, abd
, B_TRUE
);
772 (dio
->io_size
!= abd_get_size(dio
->io_abd
))) {
773 /* abd size not the same as IO size */
774 ASSERT3U(abd_get_size(dio
->io_abd
), >, dio
->io_size
);
775 abd
= abd_get_offset_size(dio
->io_abd
, 0, dio
->io_size
);
776 abd_gang_add(aio
->io_abd
, abd
, B_TRUE
);
778 if (dio
->io_flags
& ZIO_FLAG_NODATA
) {
779 /* allocate a buffer for a write gap */
780 ASSERT3U(dio
->io_type
, ==, ZIO_TYPE_WRITE
);
781 ASSERT3P(dio
->io_abd
, ==, NULL
);
782 abd_gang_add(aio
->io_abd
,
783 abd_get_zeros(dio
->io_size
), B_TRUE
);
786 * We pass B_FALSE to abd_gang_add()
787 * because we did not allocate a new
788 * ABD, so it is assumed the caller
789 * will free this ABD.
791 abd_gang_add(aio
->io_abd
, dio
->io_abd
,
795 next_offset
= dio
->io_offset
+ dio
->io_size
;
796 } while (dio
!= last
);
797 ASSERT3U(abd_get_size(aio
->io_abd
), ==, aio
->io_size
);
800 * We need to drop the vdev queue's lock during zio_execute() to
801 * avoid a deadlock that we could encounter due to lock order
802 * reversal between vq_lock and io_lock in zio_change_priority().
804 mutex_exit(&vq
->vq_lock
);
805 while ((dio
= zio_walk_parents(aio
, &zl
)) != NULL
) {
806 ASSERT3U(dio
->io_type
, ==, aio
->io_type
);
808 zio_vdev_io_bypass(dio
);
811 mutex_enter(&vq
->vq_lock
);
817 vdev_queue_io_to_issue(vdev_queue_t
*vq
)
825 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
827 p
= vdev_queue_class_to_issue(vq
);
829 if (p
== ZIO_PRIORITY_NUM_QUEUEABLE
) {
830 /* No eligible queued i/os */
835 * For LBA-ordered queues (async / scrub / initializing), issue the
836 * i/o which follows the most recently issued i/o in LBA (offset) order.
838 * For FIFO queues (sync/trim), issue the i/o with the lowest timestamp.
840 tree
= vdev_queue_class_tree(vq
, p
);
841 vq
->vq_io_search
.io_timestamp
= 0;
842 vq
->vq_io_search
.io_offset
= vq
->vq_last_offset
- 1;
843 VERIFY3P(avl_find(tree
, &vq
->vq_io_search
, &idx
), ==, NULL
);
844 zio
= avl_nearest(tree
, idx
, AVL_AFTER
);
846 zio
= avl_first(tree
);
847 ASSERT3U(zio
->io_priority
, ==, p
);
849 aio
= vdev_queue_aggregate(vq
, zio
);
853 vdev_queue_io_remove(vq
, zio
);
856 * If the I/O is or was optional and therefore has no data, we need to
857 * simply discard it. We need to drop the vdev queue's lock to avoid a
858 * deadlock that we could encounter since this I/O will complete
861 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
862 mutex_exit(&vq
->vq_lock
);
863 zio_vdev_io_bypass(zio
);
865 mutex_enter(&vq
->vq_lock
);
869 vdev_queue_pending_add(vq
, zio
);
870 vq
->vq_last_offset
= zio
->io_offset
+ zio
->io_size
;
876 vdev_queue_io(zio_t
*zio
)
878 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
881 if (zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)
885 * Children i/os inherent their parent's priority, which might
886 * not match the child's i/o type. Fix it up here.
888 if (zio
->io_type
== ZIO_TYPE_READ
) {
889 ASSERT(zio
->io_priority
!= ZIO_PRIORITY_TRIM
);
891 if (zio
->io_priority
!= ZIO_PRIORITY_SYNC_READ
&&
892 zio
->io_priority
!= ZIO_PRIORITY_ASYNC_READ
&&
893 zio
->io_priority
!= ZIO_PRIORITY_SCRUB
&&
894 zio
->io_priority
!= ZIO_PRIORITY_REMOVAL
&&
895 zio
->io_priority
!= ZIO_PRIORITY_INITIALIZING
&&
896 zio
->io_priority
!= ZIO_PRIORITY_REBUILD
) {
897 zio
->io_priority
= ZIO_PRIORITY_ASYNC_READ
;
899 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
900 ASSERT(zio
->io_priority
!= ZIO_PRIORITY_TRIM
);
902 if (zio
->io_priority
!= ZIO_PRIORITY_SYNC_WRITE
&&
903 zio
->io_priority
!= ZIO_PRIORITY_ASYNC_WRITE
&&
904 zio
->io_priority
!= ZIO_PRIORITY_REMOVAL
&&
905 zio
->io_priority
!= ZIO_PRIORITY_INITIALIZING
&&
906 zio
->io_priority
!= ZIO_PRIORITY_REBUILD
) {
907 zio
->io_priority
= ZIO_PRIORITY_ASYNC_WRITE
;
910 ASSERT(zio
->io_type
== ZIO_TYPE_TRIM
);
911 ASSERT(zio
->io_priority
== ZIO_PRIORITY_TRIM
);
914 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_QUEUE
;
915 zio
->io_timestamp
= gethrtime();
917 mutex_enter(&vq
->vq_lock
);
918 vdev_queue_io_add(vq
, zio
);
919 nio
= vdev_queue_io_to_issue(vq
);
920 mutex_exit(&vq
->vq_lock
);
925 if (nio
->io_done
== vdev_queue_agg_io_done
) {
934 vdev_queue_io_done(zio_t
*zio
)
936 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
939 hrtime_t now
= gethrtime();
940 vq
->vq_io_complete_ts
= now
;
941 vq
->vq_io_delta_ts
= zio
->io_delta
= now
- zio
->io_timestamp
;
943 mutex_enter(&vq
->vq_lock
);
944 vdev_queue_pending_remove(vq
, zio
);
946 while ((nio
= vdev_queue_io_to_issue(vq
)) != NULL
) {
947 mutex_exit(&vq
->vq_lock
);
948 if (nio
->io_done
== vdev_queue_agg_io_done
) {
951 zio_vdev_io_reissue(nio
);
954 mutex_enter(&vq
->vq_lock
);
957 mutex_exit(&vq
->vq_lock
);
961 vdev_queue_change_io_priority(zio_t
*zio
, zio_priority_t priority
)
963 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
967 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
968 * code to issue IOs without adding them to the vdev queue. In this
969 * case, the zio is already going to be issued as quickly as possible
970 * and so it doesn't need any reprioritization to help.
972 if (zio
->io_priority
== ZIO_PRIORITY_NOW
)
975 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
976 ASSERT3U(priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
978 if (zio
->io_type
== ZIO_TYPE_READ
) {
979 if (priority
!= ZIO_PRIORITY_SYNC_READ
&&
980 priority
!= ZIO_PRIORITY_ASYNC_READ
&&
981 priority
!= ZIO_PRIORITY_SCRUB
)
982 priority
= ZIO_PRIORITY_ASYNC_READ
;
984 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
985 if (priority
!= ZIO_PRIORITY_SYNC_WRITE
&&
986 priority
!= ZIO_PRIORITY_ASYNC_WRITE
)
987 priority
= ZIO_PRIORITY_ASYNC_WRITE
;
990 mutex_enter(&vq
->vq_lock
);
993 * If the zio is in none of the queues we can simply change
994 * the priority. If the zio is waiting to be submitted we must
995 * remove it from the queue and re-insert it with the new priority.
996 * Otherwise, the zio is currently active and we cannot change its
999 tree
= vdev_queue_class_tree(vq
, zio
->io_priority
);
1000 if (avl_find(tree
, zio
, NULL
) == zio
) {
1001 avl_remove(vdev_queue_class_tree(vq
, zio
->io_priority
), zio
);
1002 zio
->io_priority
= priority
;
1003 avl_add(vdev_queue_class_tree(vq
, zio
->io_priority
), zio
);
1004 } else if (avl_find(&vq
->vq_active_tree
, zio
, NULL
) != zio
) {
1005 zio
->io_priority
= priority
;
1008 mutex_exit(&vq
->vq_lock
);
1012 * As these two methods are only used for load calculations we're not
1013 * concerned if we get an incorrect value on 32bit platforms due to lack of
1014 * vq_lock mutex use here, instead we prefer to keep it lock free for
1018 vdev_queue_length(vdev_t
*vd
)
1020 return (avl_numnodes(&vd
->vdev_queue
.vq_active_tree
));
1024 vdev_queue_last_offset(vdev_t
*vd
)
1026 return (vd
->vdev_queue
.vq_last_offset
);
1030 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, aggregation_limit
, INT
, ZMOD_RW
,
1031 "Max vdev I/O aggregation size");
1033 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, aggregation_limit_non_rotating
, INT
, ZMOD_RW
,
1034 "Max vdev I/O aggregation size for non-rotating media");
1036 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, aggregate_trim
, INT
, ZMOD_RW
,
1037 "Allow TRIM I/O to be aggregated");
1039 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, read_gap_limit
, INT
, ZMOD_RW
,
1040 "Aggregate read I/O over gap");
1042 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, write_gap_limit
, INT
, ZMOD_RW
,
1043 "Aggregate write I/O over gap");
1045 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, max_active
, INT
, ZMOD_RW
,
1046 "Maximum number of active I/Os per vdev");
1048 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_active_max_dirty_percent
, INT
, ZMOD_RW
,
1049 "Async write concurrency max threshold");
1051 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_active_min_dirty_percent
, INT
, ZMOD_RW
,
1052 "Async write concurrency min threshold");
1054 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_read_max_active
, INT
, ZMOD_RW
,
1055 "Max active async read I/Os per vdev");
1057 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_read_min_active
, INT
, ZMOD_RW
,
1058 "Min active async read I/Os per vdev");
1060 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_max_active
, INT
, ZMOD_RW
,
1061 "Max active async write I/Os per vdev");
1063 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_min_active
, INT
, ZMOD_RW
,
1064 "Min active async write I/Os per vdev");
1066 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, initializing_max_active
, INT
, ZMOD_RW
,
1067 "Max active initializing I/Os per vdev");
1069 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, initializing_min_active
, INT
, ZMOD_RW
,
1070 "Min active initializing I/Os per vdev");
1072 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, removal_max_active
, INT
, ZMOD_RW
,
1073 "Max active removal I/Os per vdev");
1075 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, removal_min_active
, INT
, ZMOD_RW
,
1076 "Min active removal I/Os per vdev");
1078 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, scrub_max_active
, INT
, ZMOD_RW
,
1079 "Max active scrub I/Os per vdev");
1081 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, scrub_min_active
, INT
, ZMOD_RW
,
1082 "Min active scrub I/Os per vdev");
1084 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_read_max_active
, INT
, ZMOD_RW
,
1085 "Max active sync read I/Os per vdev");
1087 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_read_min_active
, INT
, ZMOD_RW
,
1088 "Min active sync read I/Os per vdev");
1090 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_write_max_active
, INT
, ZMOD_RW
,
1091 "Max active sync write I/Os per vdev");
1093 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_write_min_active
, INT
, ZMOD_RW
,
1094 "Min active sync write I/Os per vdev");
1096 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, trim_max_active
, INT
, ZMOD_RW
,
1097 "Max active trim/discard I/Os per vdev");
1099 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, trim_min_active
, INT
, ZMOD_RW
,
1100 "Min active trim/discard I/Os per vdev");
1102 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, rebuild_max_active
, INT
, ZMOD_RW
,
1103 "Max active rebuild I/Os per vdev");
1105 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, rebuild_min_active
, INT
, ZMOD_RW
,
1106 "Min active rebuild I/Os per vdev");
1108 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, nia_credit
, INT
, ZMOD_RW
,
1109 "Number of non-interactive I/Os to allow in sequence");
1111 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, nia_delay
, INT
, ZMOD_RW
,
1112 "Number of non-interactive I/Os before _max_active");
1114 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, queue_depth_pct
, INT
, ZMOD_RW
,
1115 "Queue depth percentage for each top-level vdev");