]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/vdev_queue.c
Merge branch 'zfsonlinux/merge-spl'
[mirror_zfs.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
cc92e9d0 26/*
8542ef85 27 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
cc92e9d0
GW
28 */
29
34dc7c2f 30#include <sys/zfs_context.h>
34dc7c2f 31#include <sys/vdev_impl.h>
330847ff 32#include <sys/spa_impl.h>
34dc7c2f
BB
33#include <sys/zio.h>
34#include <sys/avl.h>
e8b96c60 35#include <sys/dsl_pool.h>
3dfb57a3 36#include <sys/metaslab_impl.h>
e8b96c60
MA
37#include <sys/spa.h>
38#include <sys/spa_impl.h>
330847ff 39#include <sys/kstat.h>
a6255b7f 40#include <sys/abd.h>
34dc7c2f
BB
41
42/*
e8b96c60
MA
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 */
d3cc8b15 121
34dc7c2f 122/*
e8b96c60
MA
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 */
e8b96c60 127uint32_t zfs_vdev_max_active = 1000;
34dc7c2f 128
cb682a17 129/*
e8b96c60
MA
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.
cb682a17 144 */
e8b96c60
MA
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;
06226b59 151uint32_t zfs_vdev_async_write_min_active = 2;
e8b96c60
MA
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;
a1d477c2
MA
155uint32_t zfs_vdev_removal_min_active = 1;
156uint32_t zfs_vdev_removal_max_active = 2;
34dc7c2f 157
e8b96c60
MA
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 */
165int zfs_vdev_async_write_active_min_dirty_percent = 30;
166int zfs_vdev_async_write_active_max_dirty_percent = 60;
34dc7c2f
BB
167
168/*
45d1cae3
BB
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.
34dc7c2f 173 */
d4a72f23 174int zfs_vdev_aggregation_limit = 1 << 20;
9babb374 175int zfs_vdev_read_gap_limit = 32 << 10;
45d1cae3 176int zfs_vdev_write_gap_limit = 4 << 10;
34dc7c2f 177
3dfb57a3
DB
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
189int zfs_vdev_queue_depth_pct = 1000;
190#else
191int zfs_vdev_queue_depth_pct = 300;
192#endif
193
194
34dc7c2f 195int
e8b96c60 196vdev_queue_offset_compare(const void *x1, const void *x2)
34dc7c2f 197{
ee36c709
GN
198 const zio_t *z1 = (const zio_t *)x1;
199 const zio_t *z2 = (const zio_t *)x2;
34dc7c2f 200
ee36c709 201 int cmp = AVL_CMP(z1->io_offset, z2->io_offset);
34dc7c2f 202
ee36c709
GN
203 if (likely(cmp))
204 return (cmp);
34dc7c2f 205
ee36c709 206 return (AVL_PCMP(z1, z2));
34dc7c2f
BB
207}
208
ec8501ee
JG
209static inline avl_tree_t *
210vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
211{
212 return (&vq->vq_class[p].vqc_queued_tree);
213}
214
215static inline avl_tree_t *
216vdev_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
34dc7c2f 225int
e8b96c60 226vdev_queue_timestamp_compare(const void *x1, const void *x2)
34dc7c2f 227{
ee36c709
GN
228 const zio_t *z1 = (const zio_t *)x1;
229 const zio_t *z2 = (const zio_t *)x2;
34dc7c2f 230
ee36c709 231 int cmp = AVL_CMP(z1->io_timestamp, z2->io_timestamp);
34dc7c2f 232
ee36c709
GN
233 if (likely(cmp))
234 return (cmp);
34dc7c2f 235
ee36c709 236 return (AVL_PCMP(z1, z2));
34dc7c2f
BB
237}
238
e8b96c60
MA
239static int
240vdev_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);
a1d477c2
MA
253 case ZIO_PRIORITY_REMOVAL:
254 return (zfs_vdev_removal_min_active);
e8b96c60
MA
255 default:
256 panic("invalid priority %u", p);
257 return (0);
258 }
259}
260
261static int
acbad6ff 262vdev_queue_max_async_writes(spa_t *spa)
e8b96c60
MA
263{
264 int writes;
b7faa7aa
G
265 uint64_t dirty = 0;
266 dsl_pool_t *dp = spa_get_dsl(spa);
e8b96c60
MA
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
b7faa7aa
G
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
acbad6ff
AR
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 */
b7faa7aa 284 if (spa_has_pending_synctask(spa))
acbad6ff 285 return (zfs_vdev_async_write_max_active);
acbad6ff 286
b7faa7aa 287 dirty = dp->dp_dirty_total;
e8b96c60
MA
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
309static int
310vdev_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:
acbad6ff 320 return (vdev_queue_max_async_writes(spa));
e8b96c60
MA
321 case ZIO_PRIORITY_SCRUB:
322 return (zfs_vdev_scrub_max_active);
a1d477c2
MA
323 case ZIO_PRIORITY_REMOVAL:
324 return (zfs_vdev_removal_max_active);
e8b96c60
MA
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 */
335static zio_priority_t
336vdev_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++) {
ec8501ee 346 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
e8b96c60
MA
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++) {
ec8501ee 357 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
e8b96c60
MA
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
34dc7c2f
BB
367void
368vdev_queue_init(vdev_t *vd)
369{
370 vdev_queue_t *vq = &vd->vdev_queue;
e8b96c60 371 zio_priority_t p;
34dc7c2f
BB
372
373 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
e8b96c60 374 vq->vq_vdev = vd;
36b454ab 375 taskq_init_ent(&vd->vdev_queue.vq_io_search.io_tqent);
34dc7c2f 376
e8b96c60
MA
377 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
378 sizeof (zio_t), offsetof(struct zio, io_queue_node));
ec8501ee 379 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
02730c33
BB
380 vdev_queue_offset_compare, sizeof (zio_t),
381 offsetof(struct zio, io_offset_node));
ec8501ee 382 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
02730c33
BB
383 vdev_queue_offset_compare, sizeof (zio_t),
384 offsetof(struct zio, io_offset_node));
34dc7c2f 385
e8b96c60 386 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
ec8501ee
JG
387 int (*compfn) (const void *, const void *);
388
e8b96c60 389 /*
ec8501ee
JG
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.
e8b96c60 393 */
ec8501ee
JG
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,
02730c33 399 sizeof (zio_t), offsetof(struct zio, io_queue_node));
e8b96c60 400 }
9f500936 401
d6c6590c 402 vq->vq_last_offset = 0;
34dc7c2f
BB
403}
404
405void
406vdev_queue_fini(vdev_t *vd)
407{
408 vdev_queue_t *vq = &vd->vdev_queue;
409
1c27024e 410 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
ec8501ee 411 avl_destroy(vdev_queue_class_tree(vq, p));
e8b96c60 412 avl_destroy(&vq->vq_active_tree);
ec8501ee
JG
413 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
414 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
34dc7c2f
BB
415
416 mutex_destroy(&vq->vq_lock);
417}
418
419static void
420vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
421{
330847ff
MA
422 spa_t *spa = zio->io_spa;
423 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
424
e8b96c60 425 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
ec8501ee
JG
426 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
427 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
330847ff
MA
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 }
34dc7c2f
BB
434}
435
436static void
437vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
438{
330847ff
MA
439 spa_t *spa = zio->io_spa;
440 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
441
e8b96c60 442 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
ec8501ee
JG
443 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
444 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
330847ff
MA
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
453static void
454vdev_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
e8b96c60
MA
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);
330847ff
MA
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
471static void
472vdev_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
e8b96c60
MA
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);
330847ff
MA
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 }
34dc7c2f
BB
496}
497
498static void
499vdev_queue_agg_io_done(zio_t *aio)
500{
e8b96c60
MA
501 if (aio->io_type == ZIO_TYPE_READ) {
502 zio_t *pio;
3dfb57a3
DB
503 zio_link_t *zl = NULL;
504 while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
a6255b7f
DQ
505 abd_copy_off(pio->io_abd, aio->io_abd,
506 0, pio->io_offset - aio->io_offset, pio->io_size);
e8b96c60
MA
507 }
508 }
34dc7c2f 509
a6255b7f 510 abd_free(aio->io_abd);
34dc7c2f
BB
511}
512
9babb374
BB
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))
34dc7c2f
BB
521
522static zio_t *
e8b96c60 523vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
34dc7c2f 524{
e8b96c60 525 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
a76f3d04 526 zio_link_t *zl = NULL;
e8b96c60
MA
527 uint64_t maxgap = 0;
528 uint64_t size;
a58df6f5 529 uint64_t limit;
2d678f77 530 int maxblocksize;
e8b96c60 531 boolean_t stretch = B_FALSE;
ec8501ee 532 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
e8b96c60 533 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
a6255b7f 534 abd_t *abd;
e8b96c60 535
2d678f77
BB
536 maxblocksize = spa_maxblocksize(vq->vq_vdev->vdev_spa);
537 limit = MAX(MIN(zfs_vdev_aggregation_limit, maxblocksize), 0);
34dc7c2f 538
a58df6f5
BB
539 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE || limit == 0)
540 return (NULL);
34dc7c2f 541
e8b96c60 542 first = last = zio;
34dc7c2f 543
e8b96c60
MA
544 if (zio->io_type == ZIO_TYPE_READ)
545 maxgap = zfs_vdev_read_gap_limit;
fb5f0bc8 546
e8b96c60
MA
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 */
45d1cae3 556
e8b96c60
MA
557 /*
558 * We keep track of the last non-optional I/O.
559 */
560 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
45d1cae3 561
e8b96c60
MA
562 /*
563 * Walk backwards through sufficiently contiguous I/Os
8542ef85 564 * recording the last non-optional I/O.
e8b96c60
MA
565 */
566 while ((dio = AVL_PREV(t, first)) != NULL &&
567 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
a58df6f5 568 IO_SPAN(dio, last) <= limit &&
a1d477c2
MA
569 IO_GAP(dio, first) <= maxgap &&
570 dio->io_type == zio->io_type) {
e8b96c60
MA
571 first = dio;
572 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
573 mandatory = first;
574 }
45d1cae3 575
e8b96c60
MA
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 }
9babb374 583
45d1cae3 584
e8b96c60
MA
585 /*
586 * Walk forward through sufficiently contiguous I/Os.
8542ef85
MA
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.
e8b96c60
MA
590 */
591 while ((dio = AVL_NEXT(t, last)) != NULL &&
592 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
8542ef85
MA
593 (IO_SPAN(first, dio) <= limit ||
594 (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
2d678f77 595 IO_SPAN(first, dio) <= maxblocksize &&
a1d477c2
MA
596 IO_GAP(last, dio) <= maxgap &&
597 dio->io_type == zio->io_type) {
e8b96c60
MA
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;
45d1cae3
BB
622 }
623 }
e8b96c60 624 }
45d1cae3 625
e8b96c60 626 if (stretch) {
8542ef85
MA
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 */
e8b96c60
MA
633 dio = AVL_NEXT(t, last);
634 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
635 } else {
8542ef85 636 /* do not include the optional i/o */
e8b96c60
MA
637 while (last != mandatory && last != first) {
638 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
639 last = AVL_PREV(t, last);
640 ASSERT(last != NULL);
45d1cae3 641 }
34dc7c2f
BB
642 }
643
e8b96c60
MA
644 if (first == last)
645 return (NULL);
646
e8b96c60 647 size = IO_SPAN(first, last);
2d678f77 648 ASSERT3U(size, <=, maxblocksize);
e8b96c60 649
a6255b7f
DQ
650 abd = abd_alloc_for_io(size, B_TRUE);
651 if (abd == NULL)
6fe53787
BB
652 return (NULL);
653
e8b96c60 654 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
a6255b7f 655 abd, size, first->io_type, zio->io_priority,
e8b96c60
MA
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);
a6255b7f
DQ
668 abd_zero_off(aio->io_abd,
669 dio->io_offset - aio->io_offset, dio->io_size);
e8b96c60 670 } else if (dio->io_type == ZIO_TYPE_WRITE) {
a6255b7f
DQ
671 abd_copy_off(aio->io_abd, dio->io_abd,
672 dio->io_offset - aio->io_offset, 0, dio->io_size);
e8b96c60 673 }
d164b209 674
e8b96c60
MA
675 zio_add_child(dio, aio);
676 vdev_queue_io_remove(vq, dio);
a76f3d04
BB
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) {
e8b96c60
MA
685 zio_vdev_io_bypass(dio);
686 zio_execute(dio);
a76f3d04
BB
687 }
688 mutex_enter(&vq->vq_lock);
34dc7c2f 689
e8b96c60
MA
690 return (aio);
691}
692
693static zio_t *
694vdev_queue_io_to_issue(vdev_queue_t *vq)
695{
696 zio_t *zio, *aio;
697 zio_priority_t p;
698 avl_index_t idx;
ec8501ee 699 avl_tree_t *tree;
e8b96c60
MA
700
701again:
702 ASSERT(MUTEX_HELD(&vq->vq_lock));
703
704 p = vdev_queue_class_to_issue(vq);
34dc7c2f 705
e8b96c60
MA
706 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
707 /* No eligible queued i/os */
708 return (NULL);
34dc7c2f
BB
709 }
710
e8b96c60
MA
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 */
ec8501ee 717 tree = vdev_queue_class_tree(vq, p);
50b25b21 718 vq->vq_io_search.io_timestamp = 0;
d6c6590c
GN
719 vq->vq_io_search.io_offset = vq->vq_last_offset - 1;
720 VERIFY3P(avl_find(tree, &vq->vq_io_search, &idx), ==, NULL);
ec8501ee 721 zio = avl_nearest(tree, idx, AVL_AFTER);
e8b96c60 722 if (zio == NULL)
ec8501ee 723 zio = avl_first(tree);
e8b96c60
MA
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);
34dc7c2f 731
45d1cae3
BB
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 */
e8b96c60 738 if (zio->io_flags & ZIO_FLAG_NODATA) {
45d1cae3 739 mutex_exit(&vq->vq_lock);
e8b96c60
MA
740 zio_vdev_io_bypass(zio);
741 zio_execute(zio);
45d1cae3
BB
742 mutex_enter(&vq->vq_lock);
743 goto again;
744 }
745
e8b96c60 746 vdev_queue_pending_add(vq, zio);
d6c6590c 747 vq->vq_last_offset = zio->io_offset + zio->io_size;
34dc7c2f 748
e8b96c60 749 return (zio);
34dc7c2f
BB
750}
751
752zio_t *
753vdev_queue_io(zio_t *zio)
754{
755 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
756 zio_t *nio;
757
34dc7c2f
BB
758 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
759 return (zio);
760
e8b96c60
MA
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 &&
a1d477c2
MA
768 zio->io_priority != ZIO_PRIORITY_SCRUB &&
769 zio->io_priority != ZIO_PRIORITY_REMOVAL)
e8b96c60
MA
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 &&
a1d477c2
MA
774 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
775 zio->io_priority != ZIO_PRIORITY_REMOVAL)
e8b96c60
MA
776 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
777 }
34dc7c2f 778
e8b96c60 779 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
34dc7c2f
BB
780
781 mutex_enter(&vq->vq_lock);
cb682a17 782 zio->io_timestamp = gethrtime();
34dc7c2f 783 vdev_queue_io_add(vq, zio);
e8b96c60 784 nio = vdev_queue_io_to_issue(vq);
34dc7c2f
BB
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
798void
799vdev_queue_io_done(zio_t *zio)
800{
801 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
e8b96c60 802 zio_t *nio;
34dc7c2f
BB
803
804 mutex_enter(&vq->vq_lock);
805
330847ff 806 vdev_queue_pending_remove(vq, zio);
34dc7c2f 807
cb682a17
MA
808 zio->io_delta = gethrtime() - zio->io_timestamp;
809 vq->vq_io_complete_ts = gethrtime();
cc92e9d0
GW
810 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
811
e8b96c60 812 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
34dc7c2f
BB
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}
c28b2279 825
a8b2e306
TC
826void
827vdev_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
9f500936 868/*
d6c6590c 869 * As these two methods are only used for load calculations we're not
9f500936 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 */
874int
875vdev_queue_length(vdev_t *vd)
876{
877 return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
878}
879
880uint64_t
d6c6590c 881vdev_queue_last_offset(vdev_t *vd)
9f500936 882{
d6c6590c 883 return (vd->vdev_queue.vq_last_offset);
9f500936 884}
885
c28b2279 886#if defined(_KERNEL) && defined(HAVE_SPL)
c28b2279 887module_param(zfs_vdev_aggregation_limit, int, 0644);
c409e464
BB
888MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
889
c409e464
BB
890module_param(zfs_vdev_read_gap_limit, int, 0644);
891MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
892
893module_param(zfs_vdev_write_gap_limit, int, 0644);
894MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
e8b96c60
MA
895
896module_param(zfs_vdev_max_active, int, 0644);
897MODULE_PARM_DESC(zfs_vdev_max_active, "Maximum number of active I/Os per vdev");
898
899module_param(zfs_vdev_async_write_active_max_dirty_percent, int, 0644);
900MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent,
d1d7e268 901 "Async write concurrency max threshold");
e8b96c60
MA
902
903module_param(zfs_vdev_async_write_active_min_dirty_percent, int, 0644);
904MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent,
d1d7e268 905 "Async write concurrency min threshold");
e8b96c60
MA
906
907module_param(zfs_vdev_async_read_max_active, int, 0644);
908MODULE_PARM_DESC(zfs_vdev_async_read_max_active,
d1d7e268 909 "Max active async read I/Os per vdev");
e8b96c60
MA
910
911module_param(zfs_vdev_async_read_min_active, int, 0644);
912MODULE_PARM_DESC(zfs_vdev_async_read_min_active,
d1d7e268 913 "Min active async read I/Os per vdev");
e8b96c60
MA
914
915module_param(zfs_vdev_async_write_max_active, int, 0644);
916MODULE_PARM_DESC(zfs_vdev_async_write_max_active,
d1d7e268 917 "Max active async write I/Os per vdev");
e8b96c60
MA
918
919module_param(zfs_vdev_async_write_min_active, int, 0644);
920MODULE_PARM_DESC(zfs_vdev_async_write_min_active,
d1d7e268 921 "Min active async write I/Os per vdev");
e8b96c60
MA
922
923module_param(zfs_vdev_scrub_max_active, int, 0644);
924MODULE_PARM_DESC(zfs_vdev_scrub_max_active, "Max active scrub I/Os per vdev");
925
926module_param(zfs_vdev_scrub_min_active, int, 0644);
927MODULE_PARM_DESC(zfs_vdev_scrub_min_active, "Min active scrub I/Os per vdev");
928
929module_param(zfs_vdev_sync_read_max_active, int, 0644);
930MODULE_PARM_DESC(zfs_vdev_sync_read_max_active,
d1d7e268 931 "Max active sync read I/Os per vdev");
e8b96c60
MA
932
933module_param(zfs_vdev_sync_read_min_active, int, 0644);
934MODULE_PARM_DESC(zfs_vdev_sync_read_min_active,
d1d7e268 935 "Min active sync read I/Os per vdev");
e8b96c60
MA
936
937module_param(zfs_vdev_sync_write_max_active, int, 0644);
938MODULE_PARM_DESC(zfs_vdev_sync_write_max_active,
d1d7e268 939 "Max active sync write I/Os per vdev");
e8b96c60
MA
940
941module_param(zfs_vdev_sync_write_min_active, int, 0644);
942MODULE_PARM_DESC(zfs_vdev_sync_write_min_active,
3757bff3 943 "Min active sync write I/Os per vdev");
3dfb57a3
DB
944
945module_param(zfs_vdev_queue_depth_pct, int, 0644);
946MODULE_PARM_DESC(zfs_vdev_queue_depth_pct,
947 "Queue depth percentage for each top-level vdev");
c28b2279 948#endif