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