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
);
278 vq
->vq_class
[p
].vqc_list_numnodes
++;
281 avl_add(&vq
->vq_class
[p
].vqc_tree
, zio
);
285 vdev_queue_class_remove(vdev_queue_t
*vq
, zio_t
*zio
)
287 zio_priority_t p
= zio
->io_priority
;
289 if (vdev_queue_class_fifo(p
)) {
290 list_t
*list
= &vq
->vq_class
[p
].vqc_list
;
291 list_remove(list
, zio
);
292 empty
= list_is_empty(list
);
293 vq
->vq_class
[p
].vqc_list_numnodes
--;
295 avl_tree_t
*tree
= &vq
->vq_class
[p
].vqc_tree
;
296 avl_remove(tree
, zio
);
297 empty
= avl_is_empty(tree
);
299 vq
->vq_cqueued
&= ~(empty
<< p
);
303 vdev_queue_class_min_active(vdev_queue_t
*vq
, zio_priority_t p
)
306 case ZIO_PRIORITY_SYNC_READ
:
307 return (zfs_vdev_sync_read_min_active
);
308 case ZIO_PRIORITY_SYNC_WRITE
:
309 return (zfs_vdev_sync_write_min_active
);
310 case ZIO_PRIORITY_ASYNC_READ
:
311 return (zfs_vdev_async_read_min_active
);
312 case ZIO_PRIORITY_ASYNC_WRITE
:
313 return (zfs_vdev_async_write_min_active
);
314 case ZIO_PRIORITY_SCRUB
:
315 return (vq
->vq_ia_active
== 0 ? zfs_vdev_scrub_min_active
:
316 MIN(vq
->vq_nia_credit
, zfs_vdev_scrub_min_active
));
317 case ZIO_PRIORITY_REMOVAL
:
318 return (vq
->vq_ia_active
== 0 ? zfs_vdev_removal_min_active
:
319 MIN(vq
->vq_nia_credit
, zfs_vdev_removal_min_active
));
320 case ZIO_PRIORITY_INITIALIZING
:
321 return (vq
->vq_ia_active
== 0 ?zfs_vdev_initializing_min_active
:
322 MIN(vq
->vq_nia_credit
, zfs_vdev_initializing_min_active
));
323 case ZIO_PRIORITY_TRIM
:
324 return (zfs_vdev_trim_min_active
);
325 case ZIO_PRIORITY_REBUILD
:
326 return (vq
->vq_ia_active
== 0 ? zfs_vdev_rebuild_min_active
:
327 MIN(vq
->vq_nia_credit
, zfs_vdev_rebuild_min_active
));
329 panic("invalid priority %u", p
);
335 vdev_queue_max_async_writes(spa_t
*spa
)
339 dsl_pool_t
*dp
= spa_get_dsl(spa
);
340 uint64_t min_bytes
= zfs_dirty_data_max
*
341 zfs_vdev_async_write_active_min_dirty_percent
/ 100;
342 uint64_t max_bytes
= zfs_dirty_data_max
*
343 zfs_vdev_async_write_active_max_dirty_percent
/ 100;
346 * Async writes may occur before the assignment of the spa's
347 * dsl_pool_t if a self-healing zio is issued prior to the
348 * completion of dmu_objset_open_impl().
351 return (zfs_vdev_async_write_max_active
);
354 * Sync tasks correspond to interactive user actions. To reduce the
355 * execution time of those actions we push data out as fast as possible.
357 dirty
= dp
->dp_dirty_total
;
358 if (dirty
> max_bytes
|| spa_has_pending_synctask(spa
))
359 return (zfs_vdev_async_write_max_active
);
361 if (dirty
< min_bytes
)
362 return (zfs_vdev_async_write_min_active
);
365 * linear interpolation:
366 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
367 * move right by min_bytes
368 * move up by min_writes
370 writes
= (dirty
- min_bytes
) *
371 (zfs_vdev_async_write_max_active
-
372 zfs_vdev_async_write_min_active
) /
373 (max_bytes
- min_bytes
) +
374 zfs_vdev_async_write_min_active
;
375 ASSERT3U(writes
, >=, zfs_vdev_async_write_min_active
);
376 ASSERT3U(writes
, <=, zfs_vdev_async_write_max_active
);
381 vdev_queue_class_max_active(vdev_queue_t
*vq
, zio_priority_t p
)
384 case ZIO_PRIORITY_SYNC_READ
:
385 return (zfs_vdev_sync_read_max_active
);
386 case ZIO_PRIORITY_SYNC_WRITE
:
387 return (zfs_vdev_sync_write_max_active
);
388 case ZIO_PRIORITY_ASYNC_READ
:
389 return (zfs_vdev_async_read_max_active
);
390 case ZIO_PRIORITY_ASYNC_WRITE
:
391 return (vdev_queue_max_async_writes(vq
->vq_vdev
->vdev_spa
));
392 case ZIO_PRIORITY_SCRUB
:
393 if (vq
->vq_ia_active
> 0) {
394 return (MIN(vq
->vq_nia_credit
,
395 zfs_vdev_scrub_min_active
));
396 } else if (vq
->vq_nia_credit
< zfs_vdev_nia_delay
)
397 return (MAX(1, zfs_vdev_scrub_min_active
));
398 return (zfs_vdev_scrub_max_active
);
399 case ZIO_PRIORITY_REMOVAL
:
400 if (vq
->vq_ia_active
> 0) {
401 return (MIN(vq
->vq_nia_credit
,
402 zfs_vdev_removal_min_active
));
403 } else if (vq
->vq_nia_credit
< zfs_vdev_nia_delay
)
404 return (MAX(1, zfs_vdev_removal_min_active
));
405 return (zfs_vdev_removal_max_active
);
406 case ZIO_PRIORITY_INITIALIZING
:
407 if (vq
->vq_ia_active
> 0) {
408 return (MIN(vq
->vq_nia_credit
,
409 zfs_vdev_initializing_min_active
));
410 } else if (vq
->vq_nia_credit
< zfs_vdev_nia_delay
)
411 return (MAX(1, zfs_vdev_initializing_min_active
));
412 return (zfs_vdev_initializing_max_active
);
413 case ZIO_PRIORITY_TRIM
:
414 return (zfs_vdev_trim_max_active
);
415 case ZIO_PRIORITY_REBUILD
:
416 if (vq
->vq_ia_active
> 0) {
417 return (MIN(vq
->vq_nia_credit
,
418 zfs_vdev_rebuild_min_active
));
419 } else if (vq
->vq_nia_credit
< zfs_vdev_nia_delay
)
420 return (MAX(1, zfs_vdev_rebuild_min_active
));
421 return (zfs_vdev_rebuild_max_active
);
423 panic("invalid priority %u", p
);
429 * Return the i/o class to issue from, or ZIO_PRIORITY_NUM_QUEUEABLE if
430 * there is no eligible class.
432 static zio_priority_t
433 vdev_queue_class_to_issue(vdev_queue_t
*vq
)
435 uint32_t cq
= vq
->vq_cqueued
;
436 zio_priority_t p
, p1
;
438 if (cq
== 0 || vq
->vq_active
>= zfs_vdev_max_active
)
439 return (ZIO_PRIORITY_NUM_QUEUEABLE
);
442 * Find a queue that has not reached its minimum # outstanding i/os.
443 * Do round-robin to reduce starvation due to zfs_vdev_max_active
444 * and vq_nia_credit limits.
446 p1
= vq
->vq_last_prio
+ 1;
447 if (p1
>= ZIO_PRIORITY_NUM_QUEUEABLE
)
449 for (p
= p1
; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++) {
450 if ((cq
& (1U << p
)) != 0 && vq
->vq_cactive
[p
] <
451 vdev_queue_class_min_active(vq
, p
))
454 for (p
= 0; p
< p1
; p
++) {
455 if ((cq
& (1U << p
)) != 0 && vq
->vq_cactive
[p
] <
456 vdev_queue_class_min_active(vq
, p
))
461 * If we haven't found a queue, look for one that hasn't reached its
462 * maximum # outstanding i/os.
464 for (p
= 0; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++) {
465 if ((cq
& (1U << p
)) != 0 && vq
->vq_cactive
[p
] <
466 vdev_queue_class_max_active(vq
, p
))
471 vq
->vq_last_prio
= p
;
476 vdev_queue_init(vdev_t
*vd
)
478 vdev_queue_t
*vq
= &vd
->vdev_queue
;
483 for (p
= 0; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++) {
484 if (vdev_queue_class_fifo(p
)) {
485 list_create(&vq
->vq_class
[p
].vqc_list
,
487 offsetof(struct zio
, io_queue_node
.l
));
489 avl_create(&vq
->vq_class
[p
].vqc_tree
,
490 vdev_queue_to_compare
, sizeof (zio_t
),
491 offsetof(struct zio
, io_queue_node
.a
));
494 avl_create(&vq
->vq_read_offset_tree
,
495 vdev_queue_offset_compare
, sizeof (zio_t
),
496 offsetof(struct zio
, io_offset_node
));
497 avl_create(&vq
->vq_write_offset_tree
,
498 vdev_queue_offset_compare
, sizeof (zio_t
),
499 offsetof(struct zio
, io_offset_node
));
501 vq
->vq_last_offset
= 0;
502 list_create(&vq
->vq_active_list
, sizeof (struct zio
),
503 offsetof(struct zio
, io_queue_node
.l
));
504 mutex_init(&vq
->vq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
508 vdev_queue_fini(vdev_t
*vd
)
510 vdev_queue_t
*vq
= &vd
->vdev_queue
;
512 for (zio_priority_t p
= 0; p
< ZIO_PRIORITY_NUM_QUEUEABLE
; p
++) {
513 if (vdev_queue_class_fifo(p
))
514 list_destroy(&vq
->vq_class
[p
].vqc_list
);
516 avl_destroy(&vq
->vq_class
[p
].vqc_tree
);
518 avl_destroy(&vq
->vq_read_offset_tree
);
519 avl_destroy(&vq
->vq_write_offset_tree
);
521 list_destroy(&vq
->vq_active_list
);
522 mutex_destroy(&vq
->vq_lock
);
526 vdev_queue_io_add(vdev_queue_t
*vq
, zio_t
*zio
)
528 zio
->io_queue_state
= ZIO_QS_QUEUED
;
529 vdev_queue_class_add(vq
, zio
);
530 if (zio
->io_type
== ZIO_TYPE_READ
)
531 avl_add(&vq
->vq_read_offset_tree
, zio
);
532 else if (zio
->io_type
== ZIO_TYPE_WRITE
)
533 avl_add(&vq
->vq_write_offset_tree
, zio
);
537 vdev_queue_io_remove(vdev_queue_t
*vq
, zio_t
*zio
)
539 vdev_queue_class_remove(vq
, zio
);
540 if (zio
->io_type
== ZIO_TYPE_READ
)
541 avl_remove(&vq
->vq_read_offset_tree
, zio
);
542 else if (zio
->io_type
== ZIO_TYPE_WRITE
)
543 avl_remove(&vq
->vq_write_offset_tree
, zio
);
544 zio
->io_queue_state
= ZIO_QS_NONE
;
548 vdev_queue_is_interactive(zio_priority_t p
)
551 case ZIO_PRIORITY_SCRUB
:
552 case ZIO_PRIORITY_REMOVAL
:
553 case ZIO_PRIORITY_INITIALIZING
:
554 case ZIO_PRIORITY_REBUILD
:
562 vdev_queue_pending_add(vdev_queue_t
*vq
, zio_t
*zio
)
564 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
565 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
566 vq
->vq_cactive
[zio
->io_priority
]++;
568 if (vdev_queue_is_interactive(zio
->io_priority
)) {
569 if (++vq
->vq_ia_active
== 1)
570 vq
->vq_nia_credit
= 1;
571 } else if (vq
->vq_ia_active
> 0) {
574 zio
->io_queue_state
= ZIO_QS_ACTIVE
;
575 list_insert_tail(&vq
->vq_active_list
, zio
);
579 vdev_queue_pending_remove(vdev_queue_t
*vq
, zio_t
*zio
)
581 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
582 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
583 vq
->vq_cactive
[zio
->io_priority
]--;
585 if (vdev_queue_is_interactive(zio
->io_priority
)) {
586 if (--vq
->vq_ia_active
== 0)
587 vq
->vq_nia_credit
= 0;
589 vq
->vq_nia_credit
= zfs_vdev_nia_credit
;
590 } else if (vq
->vq_ia_active
== 0)
592 list_remove(&vq
->vq_active_list
, zio
);
593 zio
->io_queue_state
= ZIO_QS_NONE
;
597 vdev_queue_agg_io_done(zio_t
*aio
)
599 abd_free(aio
->io_abd
);
603 * Compute the range spanned by two i/os, which is the endpoint of the last
604 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
605 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
606 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
608 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
609 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
612 * Sufficiently adjacent io_offset's in ZIOs will be aggregated. We do this
613 * by creating a gang ABD from the adjacent ZIOs io_abd's. By using
614 * a gang ABD we avoid doing memory copies to and from the parent,
615 * child ZIOs. The gang ABD also accounts for gaps between adjacent
616 * io_offsets by simply getting the zero ABD for writes or allocating
617 * a new ABD for reads and placing them in the gang ABD as well.
620 vdev_queue_aggregate(vdev_queue_t
*vq
, zio_t
*zio
)
622 zio_t
*first
, *last
, *aio
, *dio
, *mandatory
, *nio
;
626 boolean_t stretch
= B_FALSE
;
627 uint64_t next_offset
;
632 * TRIM aggregation should not be needed since code in zfs_trim.c can
633 * submit TRIM I/O for extents up to zfs_trim_extent_bytes_max (128M).
635 if (zio
->io_type
== ZIO_TYPE_TRIM
)
638 if (zio
->io_flags
& ZIO_FLAG_DONT_AGGREGATE
)
641 if (vq
->vq_vdev
->vdev_nonrot
)
642 limit
= zfs_vdev_aggregation_limit_non_rotating
;
644 limit
= zfs_vdev_aggregation_limit
;
647 limit
= MIN(limit
, SPA_MAXBLOCKSIZE
);
650 * I/Os to distributed spares are directly dispatched to the dRAID
651 * leaf vdevs for aggregation. See the comment at the end of the
652 * zio_vdev_io_start() function.
654 ASSERT(vq
->vq_vdev
->vdev_ops
!= &vdev_draid_spare_ops
);
658 if (zio
->io_type
== ZIO_TYPE_READ
) {
659 maxgap
= zfs_vdev_read_gap_limit
;
660 t
= &vq
->vq_read_offset_tree
;
662 ASSERT3U(zio
->io_type
, ==, ZIO_TYPE_WRITE
);
663 t
= &vq
->vq_write_offset_tree
;
667 * We can aggregate I/Os that are sufficiently adjacent and of
668 * the same flavor, as expressed by the AGG_INHERIT flags.
669 * The latter requirement is necessary so that certain
670 * attributes of the I/O, such as whether it's a normal I/O
671 * or a scrub/resilver, can be preserved in the aggregate.
672 * We can include optional I/Os, but don't allow them
673 * to begin a range as they add no benefit in that situation.
677 * We keep track of the last non-optional I/O.
679 mandatory
= (first
->io_flags
& ZIO_FLAG_OPTIONAL
) ? NULL
: first
;
682 * Walk backwards through sufficiently contiguous I/Os
683 * recording the last non-optional I/O.
685 zio_flag_t flags
= zio
->io_flags
& ZIO_FLAG_AGG_INHERIT
;
686 while ((dio
= AVL_PREV(t
, first
)) != NULL
&&
687 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
688 IO_SPAN(dio
, last
) <= limit
&&
689 IO_GAP(dio
, first
) <= maxgap
&&
690 dio
->io_type
== zio
->io_type
) {
692 if (mandatory
== NULL
&& !(first
->io_flags
& ZIO_FLAG_OPTIONAL
))
697 * Skip any initial optional I/Os.
699 while ((first
->io_flags
& ZIO_FLAG_OPTIONAL
) && first
!= last
) {
700 first
= AVL_NEXT(t
, first
);
701 ASSERT(first
!= NULL
);
706 * Walk forward through sufficiently contiguous I/Os.
707 * The aggregation limit does not apply to optional i/os, so that
708 * we can issue contiguous writes even if they are larger than the
711 while ((dio
= AVL_NEXT(t
, last
)) != NULL
&&
712 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
713 (IO_SPAN(first
, dio
) <= limit
||
714 (dio
->io_flags
& ZIO_FLAG_OPTIONAL
)) &&
715 IO_SPAN(first
, dio
) <= SPA_MAXBLOCKSIZE
&&
716 IO_GAP(last
, dio
) <= maxgap
&&
717 dio
->io_type
== zio
->io_type
) {
719 if (!(last
->io_flags
& ZIO_FLAG_OPTIONAL
))
724 * Now that we've established the range of the I/O aggregation
725 * we must decide what to do with trailing optional I/Os.
726 * For reads, there's nothing to do. While we are unable to
727 * aggregate further, it's possible that a trailing optional
728 * I/O would allow the underlying device to aggregate with
729 * subsequent I/Os. We must therefore determine if the next
730 * non-optional I/O is close enough to make aggregation
733 if (zio
->io_type
== ZIO_TYPE_WRITE
&& mandatory
!= NULL
) {
735 while ((dio
= AVL_NEXT(t
, nio
)) != NULL
&&
736 IO_GAP(nio
, dio
) == 0 &&
737 IO_GAP(mandatory
, dio
) <= zfs_vdev_write_gap_limit
) {
739 if (!(nio
->io_flags
& ZIO_FLAG_OPTIONAL
)) {
748 * We are going to include an optional io in our aggregated
749 * span, thus closing the write gap. Only mandatory i/os can
750 * start aggregated spans, so make sure that the next i/o
751 * after our span is mandatory.
753 dio
= AVL_NEXT(t
, last
);
754 ASSERT3P(dio
, !=, NULL
);
755 dio
->io_flags
&= ~ZIO_FLAG_OPTIONAL
;
757 /* do not include the optional i/o */
758 while (last
!= mandatory
&& last
!= first
) {
759 ASSERT(last
->io_flags
& ZIO_FLAG_OPTIONAL
);
760 last
= AVL_PREV(t
, last
);
761 ASSERT(last
!= NULL
);
768 size
= IO_SPAN(first
, last
);
769 ASSERT3U(size
, <=, SPA_MAXBLOCKSIZE
);
771 abd
= abd_alloc_gang();
775 aio
= zio_vdev_delegated_io(first
->io_vd
, first
->io_offset
,
776 abd
, size
, first
->io_type
, zio
->io_priority
,
777 flags
| ZIO_FLAG_DONT_QUEUE
, vdev_queue_agg_io_done
, NULL
);
778 aio
->io_timestamp
= first
->io_timestamp
;
781 next_offset
= first
->io_offset
;
784 nio
= AVL_NEXT(t
, dio
);
785 ASSERT3P(dio
, !=, NULL
);
786 zio_add_child(dio
, aio
);
787 vdev_queue_io_remove(vq
, dio
);
789 if (dio
->io_offset
!= next_offset
) {
790 /* allocate a buffer for a read gap */
791 ASSERT3U(dio
->io_type
, ==, ZIO_TYPE_READ
);
792 ASSERT3U(dio
->io_offset
, >, next_offset
);
793 abd
= abd_alloc_for_io(
794 dio
->io_offset
- next_offset
, B_TRUE
);
795 abd_gang_add(aio
->io_abd
, abd
, B_TRUE
);
798 (dio
->io_size
!= abd_get_size(dio
->io_abd
))) {
799 /* abd size not the same as IO size */
800 ASSERT3U(abd_get_size(dio
->io_abd
), >, dio
->io_size
);
801 abd
= abd_get_offset_size(dio
->io_abd
, 0, dio
->io_size
);
802 abd_gang_add(aio
->io_abd
, abd
, B_TRUE
);
804 if (dio
->io_flags
& ZIO_FLAG_NODATA
) {
805 /* allocate a buffer for a write gap */
806 ASSERT3U(dio
->io_type
, ==, ZIO_TYPE_WRITE
);
807 ASSERT3P(dio
->io_abd
, ==, NULL
);
808 abd_gang_add(aio
->io_abd
,
809 abd_get_zeros(dio
->io_size
), B_TRUE
);
812 * We pass B_FALSE to abd_gang_add()
813 * because we did not allocate a new
814 * ABD, so it is assumed the caller
815 * will free this ABD.
817 abd_gang_add(aio
->io_abd
, dio
->io_abd
,
821 next_offset
= dio
->io_offset
+ dio
->io_size
;
822 } while (dio
!= last
);
823 ASSERT3U(abd_get_size(aio
->io_abd
), ==, aio
->io_size
);
826 * Callers must call zio_vdev_io_bypass() and zio_execute() for
827 * aggregated (parent) I/Os so that we could avoid dropping the
828 * queue's lock here to avoid a deadlock that we could encounter
829 * due to lock order reversal between vq_lock and io_lock in
830 * zio_change_priority().
836 vdev_queue_io_to_issue(vdev_queue_t
*vq
)
844 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
846 p
= vdev_queue_class_to_issue(vq
);
848 if (p
== ZIO_PRIORITY_NUM_QUEUEABLE
) {
849 /* No eligible queued i/os */
853 if (vdev_queue_class_fifo(p
)) {
854 zio
= list_head(&vq
->vq_class
[p
].vqc_list
);
857 * For LBA-ordered queues (async / scrub / initializing),
858 * issue the I/O which follows the most recently issued I/O
859 * in LBA (offset) order, but to avoid starvation only within
860 * the same 0.5 second interval as the first I/O.
862 tree
= &vq
->vq_class
[p
].vqc_tree
;
863 zio
= aio
= avl_first(tree
);
864 if (zio
->io_offset
< vq
->vq_last_offset
) {
865 vq
->vq_io_search
.io_timestamp
= zio
->io_timestamp
;
866 vq
->vq_io_search
.io_offset
= vq
->vq_last_offset
;
867 zio
= avl_find(tree
, &vq
->vq_io_search
, &idx
);
869 zio
= avl_nearest(tree
, idx
, AVL_AFTER
);
871 (zio
->io_timestamp
>> VDQ_T_SHIFT
) !=
872 (aio
->io_timestamp
>> VDQ_T_SHIFT
))
877 ASSERT3U(zio
->io_priority
, ==, p
);
879 aio
= vdev_queue_aggregate(vq
, zio
);
883 vdev_queue_io_remove(vq
, zio
);
886 * If the I/O is or was optional and therefore has no data, we
887 * need to simply discard it. We need to drop the vdev queue's
888 * lock to avoid a deadlock that we could encounter since this
889 * I/O will complete immediately.
891 if (zio
->io_flags
& ZIO_FLAG_NODATA
) {
892 mutex_exit(&vq
->vq_lock
);
893 zio_vdev_io_bypass(zio
);
895 mutex_enter(&vq
->vq_lock
);
900 vdev_queue_pending_add(vq
, zio
);
901 vq
->vq_last_offset
= zio
->io_offset
+ zio
->io_size
;
907 vdev_queue_io(zio_t
*zio
)
909 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
911 zio_link_t
*zl
= NULL
;
913 if (zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)
917 * Children i/os inherent their parent's priority, which might
918 * not match the child's i/o type. Fix it up here.
920 if (zio
->io_type
== ZIO_TYPE_READ
) {
921 ASSERT(zio
->io_priority
!= ZIO_PRIORITY_TRIM
);
923 if (zio
->io_priority
!= ZIO_PRIORITY_SYNC_READ
&&
924 zio
->io_priority
!= ZIO_PRIORITY_ASYNC_READ
&&
925 zio
->io_priority
!= ZIO_PRIORITY_SCRUB
&&
926 zio
->io_priority
!= ZIO_PRIORITY_REMOVAL
&&
927 zio
->io_priority
!= ZIO_PRIORITY_INITIALIZING
&&
928 zio
->io_priority
!= ZIO_PRIORITY_REBUILD
) {
929 zio
->io_priority
= ZIO_PRIORITY_ASYNC_READ
;
931 } else if (zio
->io_type
== ZIO_TYPE_WRITE
) {
932 ASSERT(zio
->io_priority
!= ZIO_PRIORITY_TRIM
);
934 if (zio
->io_priority
!= ZIO_PRIORITY_SYNC_WRITE
&&
935 zio
->io_priority
!= ZIO_PRIORITY_ASYNC_WRITE
&&
936 zio
->io_priority
!= ZIO_PRIORITY_REMOVAL
&&
937 zio
->io_priority
!= ZIO_PRIORITY_INITIALIZING
&&
938 zio
->io_priority
!= ZIO_PRIORITY_REBUILD
) {
939 zio
->io_priority
= ZIO_PRIORITY_ASYNC_WRITE
;
942 ASSERT(zio
->io_type
== ZIO_TYPE_TRIM
);
943 ASSERT(zio
->io_priority
== ZIO_PRIORITY_TRIM
);
946 zio
->io_flags
|= ZIO_FLAG_DONT_QUEUE
;
947 zio
->io_timestamp
= gethrtime();
949 mutex_enter(&vq
->vq_lock
);
950 vdev_queue_io_add(vq
, zio
);
951 nio
= vdev_queue_io_to_issue(vq
);
952 mutex_exit(&vq
->vq_lock
);
957 if (nio
->io_done
== vdev_queue_agg_io_done
) {
958 while ((dio
= zio_walk_parents(nio
, &zl
)) != NULL
) {
959 ASSERT3U(dio
->io_type
, ==, nio
->io_type
);
960 zio_vdev_io_bypass(dio
);
971 vdev_queue_io_done(zio_t
*zio
)
973 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
975 zio_link_t
*zl
= NULL
;
977 hrtime_t now
= gethrtime();
978 vq
->vq_io_complete_ts
= now
;
979 vq
->vq_io_delta_ts
= zio
->io_delta
= now
- zio
->io_timestamp
;
981 mutex_enter(&vq
->vq_lock
);
982 vdev_queue_pending_remove(vq
, zio
);
984 while ((nio
= vdev_queue_io_to_issue(vq
)) != NULL
) {
985 mutex_exit(&vq
->vq_lock
);
986 if (nio
->io_done
== vdev_queue_agg_io_done
) {
987 while ((dio
= zio_walk_parents(nio
, &zl
)) != NULL
) {
988 ASSERT3U(dio
->io_type
, ==, nio
->io_type
);
989 zio_vdev_io_bypass(dio
);
994 zio_vdev_io_reissue(nio
);
997 mutex_enter(&vq
->vq_lock
);
1000 mutex_exit(&vq
->vq_lock
);
1004 vdev_queue_change_io_priority(zio_t
*zio
, zio_priority_t priority
)
1006 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
1009 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
1010 * code to issue IOs without adding them to the vdev queue. In this
1011 * case, the zio is already going to be issued as quickly as possible
1012 * and so it doesn't need any reprioritization to help.
1014 if (zio
->io_priority
== ZIO_PRIORITY_NOW
)
1017 ASSERT3U(zio
->io_priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
1018 ASSERT3U(priority
, <, ZIO_PRIORITY_NUM_QUEUEABLE
);
1020 if (zio
->io_type
== ZIO_TYPE_READ
) {
1021 if (priority
!= ZIO_PRIORITY_SYNC_READ
&&
1022 priority
!= ZIO_PRIORITY_ASYNC_READ
&&
1023 priority
!= ZIO_PRIORITY_SCRUB
)
1024 priority
= ZIO_PRIORITY_ASYNC_READ
;
1026 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
1027 if (priority
!= ZIO_PRIORITY_SYNC_WRITE
&&
1028 priority
!= ZIO_PRIORITY_ASYNC_WRITE
)
1029 priority
= ZIO_PRIORITY_ASYNC_WRITE
;
1032 mutex_enter(&vq
->vq_lock
);
1035 * If the zio is in none of the queues we can simply change
1036 * the priority. If the zio is waiting to be submitted we must
1037 * remove it from the queue and re-insert it with the new priority.
1038 * Otherwise, the zio is currently active and we cannot change its
1041 if (zio
->io_queue_state
== ZIO_QS_QUEUED
) {
1042 vdev_queue_class_remove(vq
, zio
);
1043 zio
->io_priority
= priority
;
1044 vdev_queue_class_add(vq
, zio
);
1045 } else if (zio
->io_queue_state
== ZIO_QS_NONE
) {
1046 zio
->io_priority
= priority
;
1049 mutex_exit(&vq
->vq_lock
);
1053 * As these two methods are only used for load calculations we're not
1054 * concerned if we get an incorrect value on 32bit platforms due to lack of
1055 * vq_lock mutex use here, instead we prefer to keep it lock free for
1059 vdev_queue_length(vdev_t
*vd
)
1061 return (vd
->vdev_queue
.vq_active
);
1065 vdev_queue_last_offset(vdev_t
*vd
)
1067 return (vd
->vdev_queue
.vq_last_offset
);
1071 vdev_queue_class_length(vdev_t
*vd
, zio_priority_t p
)
1073 vdev_queue_t
*vq
= &vd
->vdev_queue
;
1074 if (vdev_queue_class_fifo(p
))
1075 return (vq
->vq_class
[p
].vqc_list_numnodes
);
1077 return (avl_numnodes(&vq
->vq_class
[p
].vqc_tree
));
1080 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, aggregation_limit
, UINT
, ZMOD_RW
,
1081 "Max vdev I/O aggregation size");
1083 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, aggregation_limit_non_rotating
, UINT
,
1084 ZMOD_RW
, "Max vdev I/O aggregation size for non-rotating media");
1086 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, read_gap_limit
, UINT
, ZMOD_RW
,
1087 "Aggregate read I/O over gap");
1089 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, write_gap_limit
, UINT
, ZMOD_RW
,
1090 "Aggregate write I/O over gap");
1092 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, max_active
, UINT
, ZMOD_RW
,
1093 "Maximum number of active I/Os per vdev");
1095 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_active_max_dirty_percent
,
1096 UINT
, ZMOD_RW
, "Async write concurrency max threshold");
1098 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_active_min_dirty_percent
,
1099 UINT
, ZMOD_RW
, "Async write concurrency min threshold");
1101 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_read_max_active
, UINT
, ZMOD_RW
,
1102 "Max active async read I/Os per vdev");
1104 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_read_min_active
, UINT
, ZMOD_RW
,
1105 "Min active async read I/Os per vdev");
1107 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_max_active
, UINT
, ZMOD_RW
,
1108 "Max active async write I/Os per vdev");
1110 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, async_write_min_active
, UINT
, ZMOD_RW
,
1111 "Min active async write I/Os per vdev");
1113 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, initializing_max_active
, UINT
, ZMOD_RW
,
1114 "Max active initializing I/Os per vdev");
1116 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, initializing_min_active
, UINT
, ZMOD_RW
,
1117 "Min active initializing I/Os per vdev");
1119 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, removal_max_active
, UINT
, ZMOD_RW
,
1120 "Max active removal I/Os per vdev");
1122 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, removal_min_active
, UINT
, ZMOD_RW
,
1123 "Min active removal I/Os per vdev");
1125 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, scrub_max_active
, UINT
, ZMOD_RW
,
1126 "Max active scrub I/Os per vdev");
1128 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, scrub_min_active
, UINT
, ZMOD_RW
,
1129 "Min active scrub I/Os per vdev");
1131 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_read_max_active
, UINT
, ZMOD_RW
,
1132 "Max active sync read I/Os per vdev");
1134 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_read_min_active
, UINT
, ZMOD_RW
,
1135 "Min active sync read I/Os per vdev");
1137 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_write_max_active
, UINT
, ZMOD_RW
,
1138 "Max active sync write I/Os per vdev");
1140 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, sync_write_min_active
, UINT
, ZMOD_RW
,
1141 "Min active sync write I/Os per vdev");
1143 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, trim_max_active
, UINT
, ZMOD_RW
,
1144 "Max active trim/discard I/Os per vdev");
1146 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, trim_min_active
, UINT
, ZMOD_RW
,
1147 "Min active trim/discard I/Os per vdev");
1149 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, rebuild_max_active
, UINT
, ZMOD_RW
,
1150 "Max active rebuild I/Os per vdev");
1152 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, rebuild_min_active
, UINT
, ZMOD_RW
,
1153 "Min active rebuild I/Os per vdev");
1155 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, nia_credit
, UINT
, ZMOD_RW
,
1156 "Number of non-interactive I/Os to allow in sequence");
1158 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, nia_delay
, UINT
, ZMOD_RW
,
1159 "Number of non-interactive I/Os before _max_active");
1161 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, queue_depth_pct
, UINT
, ZMOD_RW
,
1162 "Queue depth percentage for each top-level vdev");
1164 ZFS_MODULE_PARAM(zfs_vdev
, zfs_vdev_
, def_queue_depth
, UINT
, ZMOD_RW
,
1165 "Default queue depth for each allocator");