]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev_queue.c
Fixes for procfs files backed by linked lists
[mirror_zfs.git] / module / zfs / vdev_queue.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
28 */
29
30 #include <sys/zfs_context.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/spa_impl.h>
33 #include <sys/zio.h>
34 #include <sys/avl.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/metaslab_impl.h>
37 #include <sys/spa.h>
38 #include <sys/spa_impl.h>
39 #include <sys/kstat.h>
40 #include <sys/abd.h>
41
42 /*
43 * ZFS I/O Scheduler
44 * ---------------
45 *
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.
58 *
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.
63 *
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.
73 *
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.
88 *
89 * Async Writes
90 *
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.
93 *
94 * | o---------| <-- zfs_vdev_async_write_max_active
95 * ^ | /^ |
96 * | | / | |
97 * active | / | |
98 * I/O | / | |
99 * count | / | |
100 * | / | |
101 * |------------o | | <-- zfs_vdev_async_write_min_active
102 * 0|____________^______|_________|
103 * 0% | | 100% of zfs_dirty_data_max
104 * | |
105 * | `-- zfs_vdev_async_write_active_max_dirty_percent
106 * `--------- zfs_vdev_async_write_active_min_dirty_percent
107 *
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.
113 *
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).
120 */
121
122 /*
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.
126 */
127 uint32_t zfs_vdev_max_active = 1000;
128
129 /*
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.
134 *
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.
138 *
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
143 * throughput.
144 */
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;
157
158 /*
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.
164 */
165 int zfs_vdev_async_write_active_min_dirty_percent = 30;
166 int zfs_vdev_async_write_active_max_dirty_percent = 60;
167
168 /*
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.
173 */
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;
177
178 /*
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.
187 */
188 #ifdef _KERNEL
189 int zfs_vdev_queue_depth_pct = 1000;
190 #else
191 int zfs_vdev_queue_depth_pct = 300;
192 #endif
193
194 /*
195 * When performing allocations for a given metaslab, we want to make sure that
196 * there are enough IOs to aggregate together to improve throughput. We want to
197 * ensure that there are at least 128k worth of IOs that can be aggregated, and
198 * we assume that the average allocation size is 4k, so we need the queue depth
199 * to be 32 per allocator to get good aggregation of sequential writes.
200 */
201 int zfs_vdev_def_queue_depth = 32;
202
203
204 int
205 vdev_queue_offset_compare(const void *x1, const void *x2)
206 {
207 const zio_t *z1 = (const zio_t *)x1;
208 const zio_t *z2 = (const zio_t *)x2;
209
210 int cmp = AVL_CMP(z1->io_offset, z2->io_offset);
211
212 if (likely(cmp))
213 return (cmp);
214
215 return (AVL_PCMP(z1, z2));
216 }
217
218 static inline avl_tree_t *
219 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
220 {
221 return (&vq->vq_class[p].vqc_queued_tree);
222 }
223
224 static inline avl_tree_t *
225 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
226 {
227 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
228 if (t == ZIO_TYPE_READ)
229 return (&vq->vq_read_offset_tree);
230 else
231 return (&vq->vq_write_offset_tree);
232 }
233
234 int
235 vdev_queue_timestamp_compare(const void *x1, const void *x2)
236 {
237 const zio_t *z1 = (const zio_t *)x1;
238 const zio_t *z2 = (const zio_t *)x2;
239
240 int cmp = AVL_CMP(z1->io_timestamp, z2->io_timestamp);
241
242 if (likely(cmp))
243 return (cmp);
244
245 return (AVL_PCMP(z1, z2));
246 }
247
248 static int
249 vdev_queue_class_min_active(zio_priority_t p)
250 {
251 switch (p) {
252 case ZIO_PRIORITY_SYNC_READ:
253 return (zfs_vdev_sync_read_min_active);
254 case ZIO_PRIORITY_SYNC_WRITE:
255 return (zfs_vdev_sync_write_min_active);
256 case ZIO_PRIORITY_ASYNC_READ:
257 return (zfs_vdev_async_read_min_active);
258 case ZIO_PRIORITY_ASYNC_WRITE:
259 return (zfs_vdev_async_write_min_active);
260 case ZIO_PRIORITY_SCRUB:
261 return (zfs_vdev_scrub_min_active);
262 case ZIO_PRIORITY_REMOVAL:
263 return (zfs_vdev_removal_min_active);
264 default:
265 panic("invalid priority %u", p);
266 return (0);
267 }
268 }
269
270 static int
271 vdev_queue_max_async_writes(spa_t *spa)
272 {
273 int writes;
274 uint64_t dirty = 0;
275 dsl_pool_t *dp = spa_get_dsl(spa);
276 uint64_t min_bytes = zfs_dirty_data_max *
277 zfs_vdev_async_write_active_min_dirty_percent / 100;
278 uint64_t max_bytes = zfs_dirty_data_max *
279 zfs_vdev_async_write_active_max_dirty_percent / 100;
280
281 /*
282 * Async writes may occur before the assignment of the spa's
283 * dsl_pool_t if a self-healing zio is issued prior to the
284 * completion of dmu_objset_open_impl().
285 */
286 if (dp == NULL)
287 return (zfs_vdev_async_write_max_active);
288
289 /*
290 * Sync tasks correspond to interactive user actions. To reduce the
291 * execution time of those actions we push data out as fast as possible.
292 */
293 if (spa_has_pending_synctask(spa))
294 return (zfs_vdev_async_write_max_active);
295
296 dirty = dp->dp_dirty_total;
297 if (dirty < min_bytes)
298 return (zfs_vdev_async_write_min_active);
299 if (dirty > max_bytes)
300 return (zfs_vdev_async_write_max_active);
301
302 /*
303 * linear interpolation:
304 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
305 * move right by min_bytes
306 * move up by min_writes
307 */
308 writes = (dirty - min_bytes) *
309 (zfs_vdev_async_write_max_active -
310 zfs_vdev_async_write_min_active) /
311 (max_bytes - min_bytes) +
312 zfs_vdev_async_write_min_active;
313 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
314 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
315 return (writes);
316 }
317
318 static int
319 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
320 {
321 switch (p) {
322 case ZIO_PRIORITY_SYNC_READ:
323 return (zfs_vdev_sync_read_max_active);
324 case ZIO_PRIORITY_SYNC_WRITE:
325 return (zfs_vdev_sync_write_max_active);
326 case ZIO_PRIORITY_ASYNC_READ:
327 return (zfs_vdev_async_read_max_active);
328 case ZIO_PRIORITY_ASYNC_WRITE:
329 return (vdev_queue_max_async_writes(spa));
330 case ZIO_PRIORITY_SCRUB:
331 return (zfs_vdev_scrub_max_active);
332 case ZIO_PRIORITY_REMOVAL:
333 return (zfs_vdev_removal_max_active);
334 default:
335 panic("invalid priority %u", p);
336 return (0);
337 }
338 }
339
340 /*
341 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
342 * there is no eligible class.
343 */
344 static zio_priority_t
345 vdev_queue_class_to_issue(vdev_queue_t *vq)
346 {
347 spa_t *spa = vq->vq_vdev->vdev_spa;
348 zio_priority_t p;
349
350 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
351 return (ZIO_PRIORITY_NUM_QUEUEABLE);
352
353 /* find a queue that has not reached its minimum # outstanding i/os */
354 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
355 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
356 vq->vq_class[p].vqc_active <
357 vdev_queue_class_min_active(p))
358 return (p);
359 }
360
361 /*
362 * If we haven't found a queue, look for one that hasn't reached its
363 * maximum # outstanding i/os.
364 */
365 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
366 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
367 vq->vq_class[p].vqc_active <
368 vdev_queue_class_max_active(spa, p))
369 return (p);
370 }
371
372 /* No eligible queued i/os */
373 return (ZIO_PRIORITY_NUM_QUEUEABLE);
374 }
375
376 void
377 vdev_queue_init(vdev_t *vd)
378 {
379 vdev_queue_t *vq = &vd->vdev_queue;
380 zio_priority_t p;
381
382 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
383 vq->vq_vdev = vd;
384 taskq_init_ent(&vd->vdev_queue.vq_io_search.io_tqent);
385
386 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
387 sizeof (zio_t), offsetof(struct zio, io_queue_node));
388 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
389 vdev_queue_offset_compare, sizeof (zio_t),
390 offsetof(struct zio, io_offset_node));
391 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
392 vdev_queue_offset_compare, sizeof (zio_t),
393 offsetof(struct zio, io_offset_node));
394
395 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
396 int (*compfn) (const void *, const void *);
397
398 /*
399 * The synchronous i/o queues are dispatched in FIFO rather
400 * than LBA order. This provides more consistent latency for
401 * these i/os.
402 */
403 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
404 compfn = vdev_queue_timestamp_compare;
405 else
406 compfn = vdev_queue_offset_compare;
407 avl_create(vdev_queue_class_tree(vq, p), compfn,
408 sizeof (zio_t), offsetof(struct zio, io_queue_node));
409 }
410
411 vq->vq_last_offset = 0;
412 }
413
414 void
415 vdev_queue_fini(vdev_t *vd)
416 {
417 vdev_queue_t *vq = &vd->vdev_queue;
418
419 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
420 avl_destroy(vdev_queue_class_tree(vq, p));
421 avl_destroy(&vq->vq_active_tree);
422 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
423 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
424
425 mutex_destroy(&vq->vq_lock);
426 }
427
428 static void
429 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
430 {
431 spa_t *spa = zio->io_spa;
432 spa_history_kstat_t *shk = &spa->spa_stats.io_history;
433
434 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
435 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
436 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
437
438 if (shk->kstat != NULL) {
439 mutex_enter(&shk->lock);
440 kstat_waitq_enter(shk->kstat->ks_data);
441 mutex_exit(&shk->lock);
442 }
443 }
444
445 static void
446 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
447 {
448 spa_t *spa = zio->io_spa;
449 spa_history_kstat_t *shk = &spa->spa_stats.io_history;
450
451 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
452 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
453 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
454
455 if (shk->kstat != NULL) {
456 mutex_enter(&shk->lock);
457 kstat_waitq_exit(shk->kstat->ks_data);
458 mutex_exit(&shk->lock);
459 }
460 }
461
462 static void
463 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
464 {
465 spa_t *spa = zio->io_spa;
466 spa_history_kstat_t *shk = &spa->spa_stats.io_history;
467
468 ASSERT(MUTEX_HELD(&vq->vq_lock));
469 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
470 vq->vq_class[zio->io_priority].vqc_active++;
471 avl_add(&vq->vq_active_tree, zio);
472
473 if (shk->kstat != NULL) {
474 mutex_enter(&shk->lock);
475 kstat_runq_enter(shk->kstat->ks_data);
476 mutex_exit(&shk->lock);
477 }
478 }
479
480 static void
481 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
482 {
483 spa_t *spa = zio->io_spa;
484 spa_history_kstat_t *shk = &spa->spa_stats.io_history;
485
486 ASSERT(MUTEX_HELD(&vq->vq_lock));
487 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
488 vq->vq_class[zio->io_priority].vqc_active--;
489 avl_remove(&vq->vq_active_tree, zio);
490
491 if (shk->kstat != NULL) {
492 kstat_io_t *ksio = shk->kstat->ks_data;
493
494 mutex_enter(&shk->lock);
495 kstat_runq_exit(ksio);
496 if (zio->io_type == ZIO_TYPE_READ) {
497 ksio->reads++;
498 ksio->nread += zio->io_size;
499 } else if (zio->io_type == ZIO_TYPE_WRITE) {
500 ksio->writes++;
501 ksio->nwritten += zio->io_size;
502 }
503 mutex_exit(&shk->lock);
504 }
505 }
506
507 static void
508 vdev_queue_agg_io_done(zio_t *aio)
509 {
510 if (aio->io_type == ZIO_TYPE_READ) {
511 zio_t *pio;
512 zio_link_t *zl = NULL;
513 while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
514 abd_copy_off(pio->io_abd, aio->io_abd,
515 0, pio->io_offset - aio->io_offset, pio->io_size);
516 }
517 }
518
519 abd_free(aio->io_abd);
520 }
521
522 /*
523 * Compute the range spanned by two i/os, which is the endpoint of the last
524 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
525 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
526 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
527 */
528 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
529 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
530
531 static zio_t *
532 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
533 {
534 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
535 zio_link_t *zl = NULL;
536 uint64_t maxgap = 0;
537 uint64_t size;
538 uint64_t limit;
539 int maxblocksize;
540 boolean_t stretch = B_FALSE;
541 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
542 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
543 abd_t *abd;
544
545 maxblocksize = spa_maxblocksize(vq->vq_vdev->vdev_spa);
546 limit = MAX(MIN(zfs_vdev_aggregation_limit, maxblocksize), 0);
547
548 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE || limit == 0)
549 return (NULL);
550
551 first = last = zio;
552
553 if (zio->io_type == ZIO_TYPE_READ)
554 maxgap = zfs_vdev_read_gap_limit;
555
556 /*
557 * We can aggregate I/Os that are sufficiently adjacent and of
558 * the same flavor, as expressed by the AGG_INHERIT flags.
559 * The latter requirement is necessary so that certain
560 * attributes of the I/O, such as whether it's a normal I/O
561 * or a scrub/resilver, can be preserved in the aggregate.
562 * We can include optional I/Os, but don't allow them
563 * to begin a range as they add no benefit in that situation.
564 */
565
566 /*
567 * We keep track of the last non-optional I/O.
568 */
569 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
570
571 /*
572 * Walk backwards through sufficiently contiguous I/Os
573 * recording the last non-optional I/O.
574 */
575 while ((dio = AVL_PREV(t, first)) != NULL &&
576 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
577 IO_SPAN(dio, last) <= limit &&
578 IO_GAP(dio, first) <= maxgap &&
579 dio->io_type == zio->io_type) {
580 first = dio;
581 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
582 mandatory = first;
583 }
584
585 /*
586 * Skip any initial optional I/Os.
587 */
588 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
589 first = AVL_NEXT(t, first);
590 ASSERT(first != NULL);
591 }
592
593
594 /*
595 * Walk forward through sufficiently contiguous I/Os.
596 * The aggregation limit does not apply to optional i/os, so that
597 * we can issue contiguous writes even if they are larger than the
598 * aggregation limit.
599 */
600 while ((dio = AVL_NEXT(t, last)) != NULL &&
601 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
602 (IO_SPAN(first, dio) <= limit ||
603 (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
604 IO_SPAN(first, dio) <= maxblocksize &&
605 IO_GAP(last, dio) <= maxgap &&
606 dio->io_type == zio->io_type) {
607 last = dio;
608 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
609 mandatory = last;
610 }
611
612 /*
613 * Now that we've established the range of the I/O aggregation
614 * we must decide what to do with trailing optional I/Os.
615 * For reads, there's nothing to do. While we are unable to
616 * aggregate further, it's possible that a trailing optional
617 * I/O would allow the underlying device to aggregate with
618 * subsequent I/Os. We must therefore determine if the next
619 * non-optional I/O is close enough to make aggregation
620 * worthwhile.
621 */
622 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
623 zio_t *nio = last;
624 while ((dio = AVL_NEXT(t, nio)) != NULL &&
625 IO_GAP(nio, dio) == 0 &&
626 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
627 nio = dio;
628 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
629 stretch = B_TRUE;
630 break;
631 }
632 }
633 }
634
635 if (stretch) {
636 /*
637 * We are going to include an optional io in our aggregated
638 * span, thus closing the write gap. Only mandatory i/os can
639 * start aggregated spans, so make sure that the next i/o
640 * after our span is mandatory.
641 */
642 dio = AVL_NEXT(t, last);
643 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
644 } else {
645 /* do not include the optional i/o */
646 while (last != mandatory && last != first) {
647 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
648 last = AVL_PREV(t, last);
649 ASSERT(last != NULL);
650 }
651 }
652
653 if (first == last)
654 return (NULL);
655
656 size = IO_SPAN(first, last);
657 ASSERT3U(size, <=, maxblocksize);
658
659 abd = abd_alloc_for_io(size, B_TRUE);
660 if (abd == NULL)
661 return (NULL);
662
663 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
664 abd, size, first->io_type, zio->io_priority,
665 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
666 vdev_queue_agg_io_done, NULL);
667 aio->io_timestamp = first->io_timestamp;
668
669 nio = first;
670 do {
671 dio = nio;
672 nio = AVL_NEXT(t, dio);
673 ASSERT3U(dio->io_type, ==, aio->io_type);
674
675 if (dio->io_flags & ZIO_FLAG_NODATA) {
676 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
677 abd_zero_off(aio->io_abd,
678 dio->io_offset - aio->io_offset, dio->io_size);
679 } else if (dio->io_type == ZIO_TYPE_WRITE) {
680 abd_copy_off(aio->io_abd, dio->io_abd,
681 dio->io_offset - aio->io_offset, 0, dio->io_size);
682 }
683
684 zio_add_child(dio, aio);
685 vdev_queue_io_remove(vq, dio);
686 } while (dio != last);
687
688 /*
689 * We need to drop the vdev queue's lock to avoid a deadlock that we
690 * could encounter since this I/O will complete immediately.
691 */
692 mutex_exit(&vq->vq_lock);
693 while ((dio = zio_walk_parents(aio, &zl)) != NULL) {
694 zio_vdev_io_bypass(dio);
695 zio_execute(dio);
696 }
697 mutex_enter(&vq->vq_lock);
698
699 return (aio);
700 }
701
702 static zio_t *
703 vdev_queue_io_to_issue(vdev_queue_t *vq)
704 {
705 zio_t *zio, *aio;
706 zio_priority_t p;
707 avl_index_t idx;
708 avl_tree_t *tree;
709
710 again:
711 ASSERT(MUTEX_HELD(&vq->vq_lock));
712
713 p = vdev_queue_class_to_issue(vq);
714
715 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
716 /* No eligible queued i/os */
717 return (NULL);
718 }
719
720 /*
721 * For LBA-ordered queues (async / scrub), issue the i/o which follows
722 * the most recently issued i/o in LBA (offset) order.
723 *
724 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
725 */
726 tree = vdev_queue_class_tree(vq, p);
727 vq->vq_io_search.io_timestamp = 0;
728 vq->vq_io_search.io_offset = vq->vq_last_offset - 1;
729 VERIFY3P(avl_find(tree, &vq->vq_io_search, &idx), ==, NULL);
730 zio = avl_nearest(tree, idx, AVL_AFTER);
731 if (zio == NULL)
732 zio = avl_first(tree);
733 ASSERT3U(zio->io_priority, ==, p);
734
735 aio = vdev_queue_aggregate(vq, zio);
736 if (aio != NULL)
737 zio = aio;
738 else
739 vdev_queue_io_remove(vq, zio);
740
741 /*
742 * If the I/O is or was optional and therefore has no data, we need to
743 * simply discard it. We need to drop the vdev queue's lock to avoid a
744 * deadlock that we could encounter since this I/O will complete
745 * immediately.
746 */
747 if (zio->io_flags & ZIO_FLAG_NODATA) {
748 mutex_exit(&vq->vq_lock);
749 zio_vdev_io_bypass(zio);
750 zio_execute(zio);
751 mutex_enter(&vq->vq_lock);
752 goto again;
753 }
754
755 vdev_queue_pending_add(vq, zio);
756 vq->vq_last_offset = zio->io_offset + zio->io_size;
757
758 return (zio);
759 }
760
761 zio_t *
762 vdev_queue_io(zio_t *zio)
763 {
764 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
765 zio_t *nio;
766
767 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
768 return (zio);
769
770 /*
771 * Children i/os inherent their parent's priority, which might
772 * not match the child's i/o type. Fix it up here.
773 */
774 if (zio->io_type == ZIO_TYPE_READ) {
775 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
776 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
777 zio->io_priority != ZIO_PRIORITY_SCRUB &&
778 zio->io_priority != ZIO_PRIORITY_REMOVAL)
779 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
780 } else {
781 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
782 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
783 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
784 zio->io_priority != ZIO_PRIORITY_REMOVAL)
785 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
786 }
787
788 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
789
790 mutex_enter(&vq->vq_lock);
791 zio->io_timestamp = gethrtime();
792 vdev_queue_io_add(vq, zio);
793 nio = vdev_queue_io_to_issue(vq);
794 mutex_exit(&vq->vq_lock);
795
796 if (nio == NULL)
797 return (NULL);
798
799 if (nio->io_done == vdev_queue_agg_io_done) {
800 zio_nowait(nio);
801 return (NULL);
802 }
803
804 return (nio);
805 }
806
807 void
808 vdev_queue_io_done(zio_t *zio)
809 {
810 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
811 zio_t *nio;
812
813 mutex_enter(&vq->vq_lock);
814
815 vdev_queue_pending_remove(vq, zio);
816
817 zio->io_delta = gethrtime() - zio->io_timestamp;
818 vq->vq_io_complete_ts = gethrtime();
819 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
820
821 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
822 mutex_exit(&vq->vq_lock);
823 if (nio->io_done == vdev_queue_agg_io_done) {
824 zio_nowait(nio);
825 } else {
826 zio_vdev_io_reissue(nio);
827 zio_execute(nio);
828 }
829 mutex_enter(&vq->vq_lock);
830 }
831
832 mutex_exit(&vq->vq_lock);
833 }
834
835 void
836 vdev_queue_change_io_priority(zio_t *zio, zio_priority_t priority)
837 {
838 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
839 avl_tree_t *tree;
840
841 /*
842 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
843 * code to issue IOs without adding them to the vdev queue. In this
844 * case, the zio is already going to be issued as quickly as possible
845 * and so it doesn't need any reprioitization to help.
846 */
847 if (zio->io_priority == ZIO_PRIORITY_NOW)
848 return;
849
850 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
851 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
852
853 if (zio->io_type == ZIO_TYPE_READ) {
854 if (priority != ZIO_PRIORITY_SYNC_READ &&
855 priority != ZIO_PRIORITY_ASYNC_READ &&
856 priority != ZIO_PRIORITY_SCRUB)
857 priority = ZIO_PRIORITY_ASYNC_READ;
858 } else {
859 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
860 if (priority != ZIO_PRIORITY_SYNC_WRITE &&
861 priority != ZIO_PRIORITY_ASYNC_WRITE)
862 priority = ZIO_PRIORITY_ASYNC_WRITE;
863 }
864
865 mutex_enter(&vq->vq_lock);
866
867 /*
868 * If the zio is in none of the queues we can simply change
869 * the priority. If the zio is waiting to be submitted we must
870 * remove it from the queue and re-insert it with the new priority.
871 * Otherwise, the zio is currently active and we cannot change its
872 * priority.
873 */
874 tree = vdev_queue_class_tree(vq, zio->io_priority);
875 if (avl_find(tree, zio, NULL) == zio) {
876 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
877 zio->io_priority = priority;
878 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
879 } else if (avl_find(&vq->vq_active_tree, zio, NULL) != zio) {
880 zio->io_priority = priority;
881 }
882
883 mutex_exit(&vq->vq_lock);
884 }
885
886 /*
887 * As these two methods are only used for load calculations we're not
888 * concerned if we get an incorrect value on 32bit platforms due to lack of
889 * vq_lock mutex use here, instead we prefer to keep it lock free for
890 * performance.
891 */
892 int
893 vdev_queue_length(vdev_t *vd)
894 {
895 return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
896 }
897
898 uint64_t
899 vdev_queue_last_offset(vdev_t *vd)
900 {
901 return (vd->vdev_queue.vq_last_offset);
902 }
903
904 #if defined(_KERNEL)
905 module_param(zfs_vdev_aggregation_limit, int, 0644);
906 MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
907
908 module_param(zfs_vdev_read_gap_limit, int, 0644);
909 MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
910
911 module_param(zfs_vdev_write_gap_limit, int, 0644);
912 MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
913
914 module_param(zfs_vdev_max_active, int, 0644);
915 MODULE_PARM_DESC(zfs_vdev_max_active, "Maximum number of active I/Os per vdev");
916
917 module_param(zfs_vdev_async_write_active_max_dirty_percent, int, 0644);
918 MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent,
919 "Async write concurrency max threshold");
920
921 module_param(zfs_vdev_async_write_active_min_dirty_percent, int, 0644);
922 MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent,
923 "Async write concurrency min threshold");
924
925 module_param(zfs_vdev_async_read_max_active, int, 0644);
926 MODULE_PARM_DESC(zfs_vdev_async_read_max_active,
927 "Max active async read I/Os per vdev");
928
929 module_param(zfs_vdev_async_read_min_active, int, 0644);
930 MODULE_PARM_DESC(zfs_vdev_async_read_min_active,
931 "Min active async read I/Os per vdev");
932
933 module_param(zfs_vdev_async_write_max_active, int, 0644);
934 MODULE_PARM_DESC(zfs_vdev_async_write_max_active,
935 "Max active async write I/Os per vdev");
936
937 module_param(zfs_vdev_async_write_min_active, int, 0644);
938 MODULE_PARM_DESC(zfs_vdev_async_write_min_active,
939 "Min active async write I/Os per vdev");
940
941 module_param(zfs_vdev_scrub_max_active, int, 0644);
942 MODULE_PARM_DESC(zfs_vdev_scrub_max_active, "Max active scrub I/Os per vdev");
943
944 module_param(zfs_vdev_scrub_min_active, int, 0644);
945 MODULE_PARM_DESC(zfs_vdev_scrub_min_active, "Min active scrub I/Os per vdev");
946
947 module_param(zfs_vdev_sync_read_max_active, int, 0644);
948 MODULE_PARM_DESC(zfs_vdev_sync_read_max_active,
949 "Max active sync read I/Os per vdev");
950
951 module_param(zfs_vdev_sync_read_min_active, int, 0644);
952 MODULE_PARM_DESC(zfs_vdev_sync_read_min_active,
953 "Min active sync read I/Os per vdev");
954
955 module_param(zfs_vdev_sync_write_max_active, int, 0644);
956 MODULE_PARM_DESC(zfs_vdev_sync_write_max_active,
957 "Max active sync write I/Os per vdev");
958
959 module_param(zfs_vdev_sync_write_min_active, int, 0644);
960 MODULE_PARM_DESC(zfs_vdev_sync_write_min_active,
961 "Min active sync write I/Os per vdev");
962
963 module_param(zfs_vdev_queue_depth_pct, int, 0644);
964 MODULE_PARM_DESC(zfs_vdev_queue_depth_pct,
965 "Queue depth percentage for each top-level vdev");
966 #endif