]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/vdev_queue.c
Imported Upstream version 0.6.5.4
[mirror_zfs-debian.git] / module / zfs / vdev_queue.c
CommitLineData
34dc7c2f
BB
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/*
d164b209 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
23 * Use is subject to license terms.
24 */
25
c06d4368 26/*
ea04106b 27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
c06d4368
AX
28 */
29
34dc7c2f 30#include <sys/zfs_context.h>
34dc7c2f 31#include <sys/vdev_impl.h>
a08ee875 32#include <sys/spa_impl.h>
34dc7c2f
BB
33#include <sys/zio.h>
34#include <sys/avl.h>
a08ee875
LG
35#include <sys/dsl_pool.h>
36#include <sys/spa.h>
37#include <sys/spa_impl.h>
38#include <sys/kstat.h>
34dc7c2f
BB
39
40/*
a08ee875
LG
41 * ZFS I/O Scheduler
42 * ---------------
43 *
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.
56 *
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.
61 *
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.
71 *
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.
86 *
87 * Async Writes
88 *
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.
91 *
92 * | o---------| <-- zfs_vdev_async_write_max_active
93 * ^ | /^ |
94 * | | / | |
95 * active | / | |
96 * I/O | / | |
97 * count | / | |
98 * | / | |
99 * |------------o | | <-- zfs_vdev_async_write_min_active
100 * 0|____________^______|_________|
101 * 0% | | 100% of zfs_dirty_data_max
102 * | |
103 * | `-- zfs_vdev_async_write_active_max_dirty_percent
104 * `--------- zfs_vdev_async_write_active_min_dirty_percent
105 *
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.
111 *
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).
34dc7c2f 118 */
a08ee875 119
34dc7c2f 120/*
a08ee875
LG
121 * The maximum number of i/os active to each device. Ideally, this will be >=
122 * the sum of each queue's max_active. It must be at least the sum of each
123 * queue's min_active.
34dc7c2f 124 */
a08ee875 125uint32_t zfs_vdev_max_active = 1000;
34dc7c2f 126
c06d4368 127/*
a08ee875
LG
128 * Per-queue limits on the number of i/os active to each device. If the
129 * number of active i/os is < zfs_vdev_max_active, then the min_active comes
130 * into play. We will send min_active from each queue, and then select from
131 * queues in the order defined by zio_priority_t.
132 *
133 * In general, smaller max_active's will lead to lower latency of synchronous
134 * operations. Larger max_active's may lead to higher overall throughput,
135 * depending on underlying storage.
136 *
137 * The ratio of the queues' max_actives determines the balance of performance
138 * between reads, writes, and scrubs. E.g., increasing
139 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
140 * more quickly, but reads and writes to have higher latency and lower
141 * throughput.
c06d4368 142 */
a08ee875
LG
143uint32_t zfs_vdev_sync_read_min_active = 10;
144uint32_t zfs_vdev_sync_read_max_active = 10;
145uint32_t zfs_vdev_sync_write_min_active = 10;
146uint32_t zfs_vdev_sync_write_max_active = 10;
147uint32_t zfs_vdev_async_read_min_active = 1;
148uint32_t zfs_vdev_async_read_max_active = 3;
149uint32_t zfs_vdev_async_write_min_active = 1;
150uint32_t zfs_vdev_async_write_max_active = 10;
151uint32_t zfs_vdev_scrub_min_active = 1;
152uint32_t zfs_vdev_scrub_max_active = 2;
34dc7c2f 153
a08ee875
LG
154/*
155 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
156 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
157 * zfs_vdev_async_write_active_max_dirty_percent, use
158 * zfs_vdev_async_write_max_active. The value is linearly interpolated
159 * between min and max.
160 */
161int zfs_vdev_async_write_active_min_dirty_percent = 30;
162int zfs_vdev_async_write_active_max_dirty_percent = 60;
34dc7c2f
BB
163
164/*
45d1cae3
BB
165 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
166 * For read I/Os, we also aggregate across small adjacency gaps; for writes
167 * we include spans of optional I/Os to aid aggregation at the disk even when
168 * they aren't able to help us aggregate at this level.
34dc7c2f 169 */
e10b0808 170int zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
9babb374 171int zfs_vdev_read_gap_limit = 32 << 10;
45d1cae3 172int zfs_vdev_write_gap_limit = 4 << 10;
34dc7c2f 173
34dc7c2f 174int
a08ee875 175vdev_queue_offset_compare(const void *x1, const void *x2)
34dc7c2f
BB
176{
177 const zio_t *z1 = x1;
178 const zio_t *z2 = x2;
179
34dc7c2f
BB
180 if (z1->io_offset < z2->io_offset)
181 return (-1);
182 if (z1->io_offset > z2->io_offset)
183 return (1);
184
185 if (z1 < z2)
186 return (-1);
187 if (z1 > z2)
188 return (1);
189
190 return (0);
191}
192
e10b0808
AX
193static inline avl_tree_t *
194vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
195{
196 return (&vq->vq_class[p].vqc_queued_tree);
197}
198
199static inline avl_tree_t *
200vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
201{
202 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
203 if (t == ZIO_TYPE_READ)
204 return (&vq->vq_read_offset_tree);
205 else
206 return (&vq->vq_write_offset_tree);
207}
208
34dc7c2f 209int
a08ee875 210vdev_queue_timestamp_compare(const void *x1, const void *x2)
34dc7c2f
BB
211{
212 const zio_t *z1 = x1;
213 const zio_t *z2 = x2;
214
a08ee875 215 if (z1->io_timestamp < z2->io_timestamp)
34dc7c2f 216 return (-1);
a08ee875 217 if (z1->io_timestamp > z2->io_timestamp)
34dc7c2f
BB
218 return (1);
219
220 if (z1 < z2)
221 return (-1);
222 if (z1 > z2)
223 return (1);
224
225 return (0);
226}
227
a08ee875
LG
228static int
229vdev_queue_class_min_active(zio_priority_t p)
230{
231 switch (p) {
232 case ZIO_PRIORITY_SYNC_READ:
233 return (zfs_vdev_sync_read_min_active);
234 case ZIO_PRIORITY_SYNC_WRITE:
235 return (zfs_vdev_sync_write_min_active);
236 case ZIO_PRIORITY_ASYNC_READ:
237 return (zfs_vdev_async_read_min_active);
238 case ZIO_PRIORITY_ASYNC_WRITE:
239 return (zfs_vdev_async_write_min_active);
240 case ZIO_PRIORITY_SCRUB:
241 return (zfs_vdev_scrub_min_active);
242 default:
243 panic("invalid priority %u", p);
244 return (0);
245 }
246}
247
248static int
ea04106b 249vdev_queue_max_async_writes(spa_t *spa)
a08ee875
LG
250{
251 int writes;
ea04106b 252 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
a08ee875
LG
253 uint64_t min_bytes = zfs_dirty_data_max *
254 zfs_vdev_async_write_active_min_dirty_percent / 100;
255 uint64_t max_bytes = zfs_dirty_data_max *
256 zfs_vdev_async_write_active_max_dirty_percent / 100;
257
ea04106b
AX
258 /*
259 * Sync tasks correspond to interactive user actions. To reduce the
260 * execution time of those actions we push data out as fast as possible.
261 */
262 if (spa_has_pending_synctask(spa)) {
263 return (zfs_vdev_async_write_max_active);
264 }
265
a08ee875
LG
266 if (dirty < min_bytes)
267 return (zfs_vdev_async_write_min_active);
268 if (dirty > max_bytes)
269 return (zfs_vdev_async_write_max_active);
270
271 /*
272 * linear interpolation:
273 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
274 * move right by min_bytes
275 * move up by min_writes
276 */
277 writes = (dirty - min_bytes) *
278 (zfs_vdev_async_write_max_active -
279 zfs_vdev_async_write_min_active) /
280 (max_bytes - min_bytes) +
281 zfs_vdev_async_write_min_active;
282 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
283 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
284 return (writes);
285}
286
287static int
288vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
289{
290 switch (p) {
291 case ZIO_PRIORITY_SYNC_READ:
292 return (zfs_vdev_sync_read_max_active);
293 case ZIO_PRIORITY_SYNC_WRITE:
294 return (zfs_vdev_sync_write_max_active);
295 case ZIO_PRIORITY_ASYNC_READ:
296 return (zfs_vdev_async_read_max_active);
297 case ZIO_PRIORITY_ASYNC_WRITE:
ea04106b 298 return (vdev_queue_max_async_writes(spa));
a08ee875
LG
299 case ZIO_PRIORITY_SCRUB:
300 return (zfs_vdev_scrub_max_active);
301 default:
302 panic("invalid priority %u", p);
303 return (0);
304 }
305}
306
307/*
308 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
309 * there is no eligible class.
310 */
311static zio_priority_t
312vdev_queue_class_to_issue(vdev_queue_t *vq)
313{
314 spa_t *spa = vq->vq_vdev->vdev_spa;
315 zio_priority_t p;
316
317 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
318 return (ZIO_PRIORITY_NUM_QUEUEABLE);
319
320 /* find a queue that has not reached its minimum # outstanding i/os */
321 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
e10b0808 322 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
a08ee875
LG
323 vq->vq_class[p].vqc_active <
324 vdev_queue_class_min_active(p))
325 return (p);
326 }
327
328 /*
329 * If we haven't found a queue, look for one that hasn't reached its
330 * maximum # outstanding i/os.
331 */
332 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
e10b0808 333 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
a08ee875
LG
334 vq->vq_class[p].vqc_active <
335 vdev_queue_class_max_active(spa, p))
336 return (p);
337 }
338
339 /* No eligible queued i/os */
340 return (ZIO_PRIORITY_NUM_QUEUEABLE);
341}
342
34dc7c2f
BB
343void
344vdev_queue_init(vdev_t *vd)
345{
346 vdev_queue_t *vq = &vd->vdev_queue;
a08ee875 347 zio_priority_t p;
34dc7c2f
BB
348
349 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
a08ee875 350 vq->vq_vdev = vd;
e10b0808 351 taskq_init_ent(&vd->vdev_queue.vq_io_search.io_tqent);
34dc7c2f 352
a08ee875
LG
353 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
354 sizeof (zio_t), offsetof(struct zio, io_queue_node));
e10b0808
AX
355 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
356 vdev_queue_offset_compare, sizeof (zio_t),
357 offsetof(struct zio, io_offset_node));
358 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
359 vdev_queue_offset_compare, sizeof (zio_t),
360 offsetof(struct zio, io_offset_node));
34dc7c2f 361
a08ee875 362 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
e10b0808
AX
363 int (*compfn) (const void *, const void *);
364
a08ee875 365 /*
e10b0808
AX
366 * The synchronous i/o queues are dispatched in FIFO rather
367 * than LBA order. This provides more consistent latency for
368 * these i/os.
a08ee875 369 */
e10b0808
AX
370 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
371 compfn = vdev_queue_timestamp_compare;
372 else
373 compfn = vdev_queue_offset_compare;
374 avl_create(vdev_queue_class_tree(vq, p), compfn,
375 sizeof (zio_t), offsetof(struct zio, io_queue_node));
a08ee875 376 }
34dc7c2f
BB
377}
378
379void
380vdev_queue_fini(vdev_t *vd)
381{
382 vdev_queue_t *vq = &vd->vdev_queue;
a08ee875 383 zio_priority_t p;
34dc7c2f 384
a08ee875 385 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
e10b0808 386 avl_destroy(vdev_queue_class_tree(vq, p));
a08ee875 387 avl_destroy(&vq->vq_active_tree);
e10b0808
AX
388 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
389 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
34dc7c2f
BB
390
391 mutex_destroy(&vq->vq_lock);
392}
393
394static void
395vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
396{
a08ee875
LG
397 spa_t *spa = zio->io_spa;
398 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
399
400 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
e10b0808
AX
401 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
402 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
a08ee875
LG
403
404 if (ssh->kstat != NULL) {
405 mutex_enter(&ssh->lock);
406 kstat_waitq_enter(ssh->kstat->ks_data);
407 mutex_exit(&ssh->lock);
408 }
34dc7c2f
BB
409}
410
411static void
412vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
413{
a08ee875
LG
414 spa_t *spa = zio->io_spa;
415 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
416
417 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
e10b0808
AX
418 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
419 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
a08ee875
LG
420
421 if (ssh->kstat != NULL) {
422 mutex_enter(&ssh->lock);
423 kstat_waitq_exit(ssh->kstat->ks_data);
424 mutex_exit(&ssh->lock);
425 }
426}
427
428static void
429vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
430{
431 spa_t *spa = zio->io_spa;
432 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
433
434 ASSERT(MUTEX_HELD(&vq->vq_lock));
435 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
436 vq->vq_class[zio->io_priority].vqc_active++;
437 avl_add(&vq->vq_active_tree, zio);
438
439 if (ssh->kstat != NULL) {
440 mutex_enter(&ssh->lock);
441 kstat_runq_enter(ssh->kstat->ks_data);
442 mutex_exit(&ssh->lock);
443 }
444}
445
446static void
447vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
448{
449 spa_t *spa = zio->io_spa;
450 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
451
452 ASSERT(MUTEX_HELD(&vq->vq_lock));
453 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
454 vq->vq_class[zio->io_priority].vqc_active--;
455 avl_remove(&vq->vq_active_tree, zio);
456
457 if (ssh->kstat != NULL) {
458 kstat_io_t *ksio = ssh->kstat->ks_data;
459
460 mutex_enter(&ssh->lock);
461 kstat_runq_exit(ksio);
462 if (zio->io_type == ZIO_TYPE_READ) {
463 ksio->reads++;
464 ksio->nread += zio->io_size;
465 } else if (zio->io_type == ZIO_TYPE_WRITE) {
466 ksio->writes++;
467 ksio->nwritten += zio->io_size;
468 }
469 mutex_exit(&ssh->lock);
470 }
34dc7c2f
BB
471}
472
473static void
474vdev_queue_agg_io_done(zio_t *aio)
475{
a08ee875
LG
476 if (aio->io_type == ZIO_TYPE_READ) {
477 zio_t *pio;
478 while ((pio = zio_walk_parents(aio)) != NULL) {
d164b209
BB
479 bcopy((char *)aio->io_data + (pio->io_offset -
480 aio->io_offset), pio->io_data, pio->io_size);
a08ee875
LG
481 }
482 }
34dc7c2f 483
ea04106b 484 zio_buf_free(aio->io_data, aio->io_size);
34dc7c2f
BB
485}
486
9babb374
BB
487/*
488 * Compute the range spanned by two i/os, which is the endpoint of the last
489 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
490 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
491 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
492 */
493#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
494#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
34dc7c2f
BB
495
496static zio_t *
a08ee875 497vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
34dc7c2f 498{
a08ee875
LG
499 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
500 uint64_t maxgap = 0;
501 uint64_t size;
502 boolean_t stretch = B_FALSE;
e10b0808 503 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
a08ee875 504 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
94a40997 505 void *buf;
a08ee875
LG
506
507 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
508 return (NULL);
34dc7c2f 509
a08ee875
LG
510 /*
511 * Prevent users from setting the zfs_vdev_aggregation_limit
512 * tuning larger than SPA_MAXBLOCKSIZE.
513 */
514 zfs_vdev_aggregation_limit =
515 MIN(zfs_vdev_aggregation_limit, SPA_MAXBLOCKSIZE);
34dc7c2f 516
a08ee875 517 first = last = zio;
34dc7c2f 518
a08ee875
LG
519 if (zio->io_type == ZIO_TYPE_READ)
520 maxgap = zfs_vdev_read_gap_limit;
fb5f0bc8 521
a08ee875
LG
522 /*
523 * We can aggregate I/Os that are sufficiently adjacent and of
524 * the same flavor, as expressed by the AGG_INHERIT flags.
525 * The latter requirement is necessary so that certain
526 * attributes of the I/O, such as whether it's a normal I/O
527 * or a scrub/resilver, can be preserved in the aggregate.
528 * We can include optional I/Os, but don't allow them
529 * to begin a range as they add no benefit in that situation.
530 */
45d1cae3 531
a08ee875
LG
532 /*
533 * We keep track of the last non-optional I/O.
534 */
535 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
45d1cae3 536
a08ee875
LG
537 /*
538 * Walk backwards through sufficiently contiguous I/Os
539 * recording the last non-option I/O.
540 */
541 while ((dio = AVL_PREV(t, first)) != NULL &&
542 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
543 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
544 IO_GAP(dio, first) <= maxgap) {
545 first = dio;
546 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
547 mandatory = first;
548 }
45d1cae3 549
a08ee875
LG
550 /*
551 * Skip any initial optional I/Os.
552 */
553 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
554 first = AVL_NEXT(t, first);
555 ASSERT(first != NULL);
556 }
9babb374 557
45d1cae3 558
a08ee875
LG
559 /*
560 * Walk forward through sufficiently contiguous I/Os.
561 */
562 while ((dio = AVL_NEXT(t, last)) != NULL &&
563 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
564 IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
565 IO_GAP(last, dio) <= maxgap) {
566 last = dio;
567 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
568 mandatory = last;
569 }
570
571 /*
572 * Now that we've established the range of the I/O aggregation
573 * we must decide what to do with trailing optional I/Os.
574 * For reads, there's nothing to do. While we are unable to
575 * aggregate further, it's possible that a trailing optional
576 * I/O would allow the underlying device to aggregate with
577 * subsequent I/Os. We must therefore determine if the next
578 * non-optional I/O is close enough to make aggregation
579 * worthwhile.
580 */
581 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
582 zio_t *nio = last;
583 while ((dio = AVL_NEXT(t, nio)) != NULL &&
584 IO_GAP(nio, dio) == 0 &&
585 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
586 nio = dio;
587 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
588 stretch = B_TRUE;
589 break;
45d1cae3
BB
590 }
591 }
a08ee875 592 }
45d1cae3 593
a08ee875
LG
594 if (stretch) {
595 /* This may be a no-op. */
596 dio = AVL_NEXT(t, last);
597 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
598 } else {
599 while (last != mandatory && last != first) {
600 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
601 last = AVL_PREV(t, last);
602 ASSERT(last != NULL);
45d1cae3 603 }
34dc7c2f
BB
604 }
605
a08ee875
LG
606 if (first == last)
607 return (NULL);
d164b209 608
a08ee875
LG
609 size = IO_SPAN(first, last);
610 ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
611
94a40997
AX
612 buf = zio_buf_alloc_flags(size, KM_NOSLEEP);
613 if (buf == NULL)
614 return (NULL);
615
a08ee875 616 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
94a40997 617 buf, size, first->io_type, zio->io_priority,
a08ee875
LG
618 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
619 vdev_queue_agg_io_done, NULL);
620 aio->io_timestamp = first->io_timestamp;
621
622 nio = first;
623 do {
624 dio = nio;
625 nio = AVL_NEXT(t, dio);
626 ASSERT3U(dio->io_type, ==, aio->io_type);
627
628 if (dio->io_flags & ZIO_FLAG_NODATA) {
629 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
630 bzero((char *)aio->io_data + (dio->io_offset -
631 aio->io_offset), dio->io_size);
632 } else if (dio->io_type == ZIO_TYPE_WRITE) {
633 bcopy(dio->io_data, (char *)aio->io_data +
634 (dio->io_offset - aio->io_offset),
635 dio->io_size);
636 }
34dc7c2f 637
a08ee875
LG
638 zio_add_child(dio, aio);
639 vdev_queue_io_remove(vq, dio);
640 zio_vdev_io_bypass(dio);
641 zio_execute(dio);
642 } while (dio != last);
643
a08ee875
LG
644 return (aio);
645}
34dc7c2f 646
a08ee875
LG
647static zio_t *
648vdev_queue_io_to_issue(vdev_queue_t *vq)
649{
650 zio_t *zio, *aio;
651 zio_priority_t p;
652 avl_index_t idx;
e10b0808 653 avl_tree_t *tree;
a08ee875
LG
654
655again:
656 ASSERT(MUTEX_HELD(&vq->vq_lock));
657
658 p = vdev_queue_class_to_issue(vq);
659
660 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
661 /* No eligible queued i/os */
662 return (NULL);
34dc7c2f
BB
663 }
664
a08ee875
LG
665 /*
666 * For LBA-ordered queues (async / scrub), issue the i/o which follows
667 * the most recently issued i/o in LBA (offset) order.
668 *
669 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
670 */
e10b0808 671 tree = vdev_queue_class_tree(vq, p);
ea04106b
AX
672 vq->vq_io_search.io_timestamp = 0;
673 vq->vq_io_search.io_offset = vq->vq_last_offset + 1;
e10b0808 674 VERIFY3P(avl_find(tree, &vq->vq_io_search,
ea04106b 675 &idx), ==, NULL);
e10b0808 676 zio = avl_nearest(tree, idx, AVL_AFTER);
a08ee875 677 if (zio == NULL)
e10b0808 678 zio = avl_first(tree);
a08ee875
LG
679 ASSERT3U(zio->io_priority, ==, p);
680
681 aio = vdev_queue_aggregate(vq, zio);
682 if (aio != NULL)
683 zio = aio;
684 else
685 vdev_queue_io_remove(vq, zio);
34dc7c2f 686
45d1cae3
BB
687 /*
688 * If the I/O is or was optional and therefore has no data, we need to
689 * simply discard it. We need to drop the vdev queue's lock to avoid a
690 * deadlock that we could encounter since this I/O will complete
691 * immediately.
692 */
a08ee875 693 if (zio->io_flags & ZIO_FLAG_NODATA) {
45d1cae3 694 mutex_exit(&vq->vq_lock);
a08ee875
LG
695 zio_vdev_io_bypass(zio);
696 zio_execute(zio);
45d1cae3
BB
697 mutex_enter(&vq->vq_lock);
698 goto again;
699 }
700
a08ee875
LG
701 vdev_queue_pending_add(vq, zio);
702 vq->vq_last_offset = zio->io_offset;
34dc7c2f 703
a08ee875 704 return (zio);
34dc7c2f
BB
705}
706
707zio_t *
708vdev_queue_io(zio_t *zio)
709{
710 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
711 zio_t *nio;
712
34dc7c2f
BB
713 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
714 return (zio);
715
a08ee875
LG
716 /*
717 * Children i/os inherent their parent's priority, which might
718 * not match the child's i/o type. Fix it up here.
719 */
720 if (zio->io_type == ZIO_TYPE_READ) {
721 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
722 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
723 zio->io_priority != ZIO_PRIORITY_SCRUB)
724 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
725 } else {
726 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
727 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
728 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
729 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
730 }
34dc7c2f 731
a08ee875 732 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
34dc7c2f
BB
733
734 mutex_enter(&vq->vq_lock);
c06d4368 735 zio->io_timestamp = gethrtime();
34dc7c2f 736 vdev_queue_io_add(vq, zio);
a08ee875 737 nio = vdev_queue_io_to_issue(vq);
34dc7c2f
BB
738 mutex_exit(&vq->vq_lock);
739
740 if (nio == NULL)
741 return (NULL);
742
743 if (nio->io_done == vdev_queue_agg_io_done) {
744 zio_nowait(nio);
745 return (NULL);
746 }
747
748 return (nio);
749}
750
751void
752vdev_queue_io_done(zio_t *zio)
753{
754 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
a08ee875 755 zio_t *nio;
34dc7c2f 756
c06d4368
AX
757 if (zio_injection_enabled)
758 delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
759
34dc7c2f
BB
760 mutex_enter(&vq->vq_lock);
761
a08ee875 762 vdev_queue_pending_remove(vq, zio);
34dc7c2f 763
c06d4368
AX
764 zio->io_delta = gethrtime() - zio->io_timestamp;
765 vq->vq_io_complete_ts = gethrtime();
766 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
767
a08ee875 768 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
34dc7c2f
BB
769 mutex_exit(&vq->vq_lock);
770 if (nio->io_done == vdev_queue_agg_io_done) {
771 zio_nowait(nio);
772 } else {
773 zio_vdev_io_reissue(nio);
774 zio_execute(nio);
775 }
776 mutex_enter(&vq->vq_lock);
777 }
778
779 mutex_exit(&vq->vq_lock);
780}
c28b2279
BB
781
782#if defined(_KERNEL) && defined(HAVE_SPL)
c28b2279 783module_param(zfs_vdev_aggregation_limit, int, 0644);
c409e464
BB
784MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
785
c409e464
BB
786module_param(zfs_vdev_read_gap_limit, int, 0644);
787MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
788
789module_param(zfs_vdev_write_gap_limit, int, 0644);
790MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
a08ee875
LG
791
792module_param(zfs_vdev_max_active, int, 0644);
793MODULE_PARM_DESC(zfs_vdev_max_active, "Maximum number of active I/Os per vdev");
794
795module_param(zfs_vdev_async_write_active_max_dirty_percent, int, 0644);
796MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent,
797 "Async write concurrency max threshold");
798
799module_param(zfs_vdev_async_write_active_min_dirty_percent, int, 0644);
800MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent,
801 "Async write concurrency min threshold");
802
803module_param(zfs_vdev_async_read_max_active, int, 0644);
804MODULE_PARM_DESC(zfs_vdev_async_read_max_active,
805 "Max active async read I/Os per vdev");
806
807module_param(zfs_vdev_async_read_min_active, int, 0644);
808MODULE_PARM_DESC(zfs_vdev_async_read_min_active,
809 "Min active async read I/Os per vdev");
810
811module_param(zfs_vdev_async_write_max_active, int, 0644);
812MODULE_PARM_DESC(zfs_vdev_async_write_max_active,
813 "Max active async write I/Os per vdev");
814
815module_param(zfs_vdev_async_write_min_active, int, 0644);
816MODULE_PARM_DESC(zfs_vdev_async_write_min_active,
817 "Min active async write I/Os per vdev");
818
819module_param(zfs_vdev_scrub_max_active, int, 0644);
820MODULE_PARM_DESC(zfs_vdev_scrub_max_active, "Max active scrub I/Os per vdev");
821
822module_param(zfs_vdev_scrub_min_active, int, 0644);
823MODULE_PARM_DESC(zfs_vdev_scrub_min_active, "Min active scrub I/Os per vdev");
824
825module_param(zfs_vdev_sync_read_max_active, int, 0644);
826MODULE_PARM_DESC(zfs_vdev_sync_read_max_active,
827 "Max active sync read I/Os per vdev");
828
829module_param(zfs_vdev_sync_read_min_active, int, 0644);
830MODULE_PARM_DESC(zfs_vdev_sync_read_min_active,
831 "Min active sync read I/Os per vdev");
832
833module_param(zfs_vdev_sync_write_max_active, int, 0644);
834MODULE_PARM_DESC(zfs_vdev_sync_write_max_active,
835 "Max active sync write I/Os per vdev");
836
837module_param(zfs_vdev_sync_write_min_active, int, 0644);
838MODULE_PARM_DESC(zfs_vdev_sync_write_min_active,
e10b0808 839 "Min active sync write I/Os per vdev");
c28b2279 840#endif