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