]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/vdev_queue.c
zstreamdump needs to initialize fletcher 4 support
[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/*
acbad6ff 27 * Copyright (c) 2012, 2014 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>
34dc7c2f
BB
40
41/*
e8b96c60
MA
42 * ZFS I/O Scheduler
43 * ---------------
44 *
45 * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
46 * I/O scheduler determines when and in what order those operations are
47 * issued. The I/O scheduler divides operations into five I/O classes
48 * prioritized in the following order: sync read, sync write, async read,
49 * async write, and scrub/resilver. Each queue defines the minimum and
50 * maximum number of concurrent operations that may be issued to the device.
51 * In addition, the device has an aggregate maximum. Note that the sum of the
52 * per-queue minimums must not exceed the aggregate maximum. If the
53 * sum of the per-queue maximums exceeds the aggregate maximum, then the
54 * number of active i/os may reach zfs_vdev_max_active, in which case no
55 * further i/os will be issued regardless of whether all per-queue
56 * minimums have been met.
57 *
58 * For many physical devices, throughput increases with the number of
59 * concurrent operations, but latency typically suffers. Further, physical
60 * devices typically have a limit at which more concurrent operations have no
61 * effect on throughput or can actually cause it to decrease.
62 *
63 * The scheduler selects the next operation to issue by first looking for an
64 * I/O class whose minimum has not been satisfied. Once all are satisfied and
65 * the aggregate maximum has not been hit, the scheduler looks for classes
66 * whose maximum has not been satisfied. Iteration through the I/O classes is
67 * done in the order specified above. No further operations are issued if the
68 * aggregate maximum number of concurrent operations has been hit or if there
69 * are no operations queued for an I/O class that has not hit its maximum.
70 * Every time an i/o is queued or an operation completes, the I/O scheduler
71 * looks for new operations to issue.
72 *
73 * All I/O classes have a fixed maximum number of outstanding operations
74 * except for the async write class. Asynchronous writes represent the data
75 * that is committed to stable storage during the syncing stage for
76 * transaction groups (see txg.c). Transaction groups enter the syncing state
77 * periodically so the number of queued async writes will quickly burst up and
78 * then bleed down to zero. Rather than servicing them as quickly as possible,
79 * the I/O scheduler changes the maximum number of active async write i/os
80 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
81 * both throughput and latency typically increase with the number of
82 * concurrent operations issued to physical devices, reducing the burstiness
83 * in the number of concurrent operations also stabilizes the response time of
84 * operations from other -- and in particular synchronous -- queues. In broad
85 * strokes, the I/O scheduler will issue more concurrent operations from the
86 * async write queue as there's more dirty data in the pool.
87 *
88 * Async Writes
89 *
90 * The number of concurrent operations issued for the async write I/O class
91 * follows a piece-wise linear function defined by a few adjustable points.
92 *
93 * | o---------| <-- zfs_vdev_async_write_max_active
94 * ^ | /^ |
95 * | | / | |
96 * active | / | |
97 * I/O | / | |
98 * count | / | |
99 * | / | |
100 * |------------o | | <-- zfs_vdev_async_write_min_active
101 * 0|____________^______|_________|
102 * 0% | | 100% of zfs_dirty_data_max
103 * | |
104 * | `-- zfs_vdev_async_write_active_max_dirty_percent
105 * `--------- zfs_vdev_async_write_active_min_dirty_percent
106 *
107 * Until the amount of dirty data exceeds a minimum percentage of the dirty
108 * data allowed in the pool, the I/O scheduler will limit the number of
109 * concurrent operations to the minimum. As that threshold is crossed, the
110 * number of concurrent operations issued increases linearly to the maximum at
111 * the specified maximum percentage of the dirty data allowed in the pool.
112 *
113 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
114 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
115 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
116 * maximum percentage, this indicates that the rate of incoming data is
117 * greater than the rate that the backend storage can handle. In this case, we
118 * must further throttle incoming writes (see dmu_tx_delay() for details).
34dc7c2f 119 */
d3cc8b15 120
34dc7c2f 121/*
e8b96c60
MA
122 * The maximum number of i/os active to each device. Ideally, this will be >=
123 * the sum of each queue's max_active. It must be at least the sum of each
124 * queue's min_active.
34dc7c2f 125 */
e8b96c60 126uint32_t zfs_vdev_max_active = 1000;
34dc7c2f 127
cb682a17 128/*
e8b96c60
MA
129 * Per-queue limits on the number of i/os active to each device. If the
130 * number of active i/os is < zfs_vdev_max_active, then the min_active comes
131 * into play. We will send min_active from each queue, and then select from
132 * queues in the order defined by zio_priority_t.
133 *
134 * In general, smaller max_active's will lead to lower latency of synchronous
135 * operations. Larger max_active's may lead to higher overall throughput,
136 * depending on underlying storage.
137 *
138 * The ratio of the queues' max_actives determines the balance of performance
139 * between reads, writes, and scrubs. E.g., increasing
140 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
141 * more quickly, but reads and writes to have higher latency and lower
142 * throughput.
cb682a17 143 */
e8b96c60
MA
144uint32_t zfs_vdev_sync_read_min_active = 10;
145uint32_t zfs_vdev_sync_read_max_active = 10;
146uint32_t zfs_vdev_sync_write_min_active = 10;
147uint32_t zfs_vdev_sync_write_max_active = 10;
148uint32_t zfs_vdev_async_read_min_active = 1;
149uint32_t zfs_vdev_async_read_max_active = 3;
150uint32_t zfs_vdev_async_write_min_active = 1;
151uint32_t zfs_vdev_async_write_max_active = 10;
152uint32_t zfs_vdev_scrub_min_active = 1;
153uint32_t zfs_vdev_scrub_max_active = 2;
34dc7c2f 154
e8b96c60
MA
155/*
156 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
157 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
158 * zfs_vdev_async_write_active_max_dirty_percent, use
159 * zfs_vdev_async_write_max_active. The value is linearly interpolated
160 * between min and max.
161 */
162int zfs_vdev_async_write_active_min_dirty_percent = 30;
163int zfs_vdev_async_write_active_max_dirty_percent = 60;
34dc7c2f
BB
164
165/*
45d1cae3
BB
166 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
167 * For read I/Os, we also aggregate across small adjacency gaps; for writes
168 * we include spans of optional I/Os to aid aggregation at the disk even when
169 * they aren't able to help us aggregate at this level.
34dc7c2f 170 */
f1512ee6 171int zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
9babb374 172int zfs_vdev_read_gap_limit = 32 << 10;
45d1cae3 173int zfs_vdev_write_gap_limit = 4 << 10;
34dc7c2f 174
3dfb57a3
DB
175/*
176 * Define the queue depth percentage for each top-level. This percentage is
177 * used in conjunction with zfs_vdev_async_max_active to determine how many
178 * allocations a specific top-level vdev should handle. Once the queue depth
179 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
180 * then allocator will stop allocating blocks on that top-level device.
181 * The default kernel setting is 1000% which will yield 100 allocations per
182 * device. For userland testing, the default setting is 300% which equates
183 * to 30 allocations per device.
184 */
185#ifdef _KERNEL
186int zfs_vdev_queue_depth_pct = 1000;
187#else
188int zfs_vdev_queue_depth_pct = 300;
189#endif
190
191
34dc7c2f 192int
e8b96c60 193vdev_queue_offset_compare(const void *x1, const void *x2)
34dc7c2f 194{
ee36c709
GN
195 const zio_t *z1 = (const zio_t *)x1;
196 const zio_t *z2 = (const zio_t *)x2;
34dc7c2f 197
ee36c709 198 int cmp = AVL_CMP(z1->io_offset, z2->io_offset);
34dc7c2f 199
ee36c709
GN
200 if (likely(cmp))
201 return (cmp);
34dc7c2f 202
ee36c709 203 return (AVL_PCMP(z1, z2));
34dc7c2f
BB
204}
205
ec8501ee
JG
206static inline avl_tree_t *
207vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
208{
209 return (&vq->vq_class[p].vqc_queued_tree);
210}
211
212static inline avl_tree_t *
213vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
214{
215 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
216 if (t == ZIO_TYPE_READ)
217 return (&vq->vq_read_offset_tree);
218 else
219 return (&vq->vq_write_offset_tree);
220}
221
34dc7c2f 222int
e8b96c60 223vdev_queue_timestamp_compare(const void *x1, const void *x2)
34dc7c2f 224{
ee36c709
GN
225 const zio_t *z1 = (const zio_t *)x1;
226 const zio_t *z2 = (const zio_t *)x2;
34dc7c2f 227
ee36c709 228 int cmp = AVL_CMP(z1->io_timestamp, z2->io_timestamp);
34dc7c2f 229
ee36c709
GN
230 if (likely(cmp))
231 return (cmp);
34dc7c2f 232
ee36c709 233 return (AVL_PCMP(z1, z2));
34dc7c2f
BB
234}
235
e8b96c60
MA
236static int
237vdev_queue_class_min_active(zio_priority_t p)
238{
239 switch (p) {
240 case ZIO_PRIORITY_SYNC_READ:
241 return (zfs_vdev_sync_read_min_active);
242 case ZIO_PRIORITY_SYNC_WRITE:
243 return (zfs_vdev_sync_write_min_active);
244 case ZIO_PRIORITY_ASYNC_READ:
245 return (zfs_vdev_async_read_min_active);
246 case ZIO_PRIORITY_ASYNC_WRITE:
247 return (zfs_vdev_async_write_min_active);
248 case ZIO_PRIORITY_SCRUB:
249 return (zfs_vdev_scrub_min_active);
250 default:
251 panic("invalid priority %u", p);
252 return (0);
253 }
254}
255
256static int
acbad6ff 257vdev_queue_max_async_writes(spa_t *spa)
e8b96c60
MA
258{
259 int writes;
b7faa7aa
G
260 uint64_t dirty = 0;
261 dsl_pool_t *dp = spa_get_dsl(spa);
e8b96c60
MA
262 uint64_t min_bytes = zfs_dirty_data_max *
263 zfs_vdev_async_write_active_min_dirty_percent / 100;
264 uint64_t max_bytes = zfs_dirty_data_max *
265 zfs_vdev_async_write_active_max_dirty_percent / 100;
266
b7faa7aa
G
267 /*
268 * Async writes may occur before the assignment of the spa's
269 * dsl_pool_t if a self-healing zio is issued prior to the
270 * completion of dmu_objset_open_impl().
271 */
272 if (dp == NULL)
273 return (zfs_vdev_async_write_max_active);
274
acbad6ff
AR
275 /*
276 * Sync tasks correspond to interactive user actions. To reduce the
277 * execution time of those actions we push data out as fast as possible.
278 */
b7faa7aa 279 if (spa_has_pending_synctask(spa))
acbad6ff 280 return (zfs_vdev_async_write_max_active);
acbad6ff 281
b7faa7aa 282 dirty = dp->dp_dirty_total;
e8b96c60
MA
283 if (dirty < min_bytes)
284 return (zfs_vdev_async_write_min_active);
285 if (dirty > max_bytes)
286 return (zfs_vdev_async_write_max_active);
287
288 /*
289 * linear interpolation:
290 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
291 * move right by min_bytes
292 * move up by min_writes
293 */
294 writes = (dirty - min_bytes) *
295 (zfs_vdev_async_write_max_active -
296 zfs_vdev_async_write_min_active) /
297 (max_bytes - min_bytes) +
298 zfs_vdev_async_write_min_active;
299 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
300 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
301 return (writes);
302}
303
304static int
305vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
306{
307 switch (p) {
308 case ZIO_PRIORITY_SYNC_READ:
309 return (zfs_vdev_sync_read_max_active);
310 case ZIO_PRIORITY_SYNC_WRITE:
311 return (zfs_vdev_sync_write_max_active);
312 case ZIO_PRIORITY_ASYNC_READ:
313 return (zfs_vdev_async_read_max_active);
314 case ZIO_PRIORITY_ASYNC_WRITE:
acbad6ff 315 return (vdev_queue_max_async_writes(spa));
e8b96c60
MA
316 case ZIO_PRIORITY_SCRUB:
317 return (zfs_vdev_scrub_max_active);
318 default:
319 panic("invalid priority %u", p);
320 return (0);
321 }
322}
323
324/*
325 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
326 * there is no eligible class.
327 */
328static zio_priority_t
329vdev_queue_class_to_issue(vdev_queue_t *vq)
330{
331 spa_t *spa = vq->vq_vdev->vdev_spa;
332 zio_priority_t p;
333
334 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
335 return (ZIO_PRIORITY_NUM_QUEUEABLE);
336
337 /* find a queue that has not reached its minimum # outstanding i/os */
338 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
ec8501ee 339 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
e8b96c60
MA
340 vq->vq_class[p].vqc_active <
341 vdev_queue_class_min_active(p))
342 return (p);
343 }
344
345 /*
346 * If we haven't found a queue, look for one that hasn't reached its
347 * maximum # outstanding i/os.
348 */
349 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
ec8501ee 350 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
e8b96c60
MA
351 vq->vq_class[p].vqc_active <
352 vdev_queue_class_max_active(spa, p))
353 return (p);
354 }
355
356 /* No eligible queued i/os */
357 return (ZIO_PRIORITY_NUM_QUEUEABLE);
358}
359
34dc7c2f
BB
360void
361vdev_queue_init(vdev_t *vd)
362{
363 vdev_queue_t *vq = &vd->vdev_queue;
e8b96c60 364 zio_priority_t p;
34dc7c2f
BB
365
366 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
e8b96c60 367 vq->vq_vdev = vd;
36b454ab 368 taskq_init_ent(&vd->vdev_queue.vq_io_search.io_tqent);
34dc7c2f 369
e8b96c60
MA
370 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
371 sizeof (zio_t), offsetof(struct zio, io_queue_node));
ec8501ee
JG
372 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
373 vdev_queue_offset_compare, sizeof (zio_t),
374 offsetof(struct zio, io_offset_node));
375 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
376 vdev_queue_offset_compare, sizeof (zio_t),
377 offsetof(struct zio, io_offset_node));
34dc7c2f 378
e8b96c60 379 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
ec8501ee
JG
380 int (*compfn) (const void *, const void *);
381
e8b96c60 382 /*
ec8501ee
JG
383 * The synchronous i/o queues are dispatched in FIFO rather
384 * than LBA order. This provides more consistent latency for
385 * these i/os.
e8b96c60 386 */
ec8501ee
JG
387 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
388 compfn = vdev_queue_timestamp_compare;
389 else
390 compfn = vdev_queue_offset_compare;
391 avl_create(vdev_queue_class_tree(vq, p), compfn,
392 sizeof (zio_t), offsetof(struct zio, io_queue_node));
e8b96c60 393 }
9f500936 394
395 vq->vq_lastoffset = 0;
34dc7c2f
BB
396}
397
398void
399vdev_queue_fini(vdev_t *vd)
400{
401 vdev_queue_t *vq = &vd->vdev_queue;
e8b96c60 402 zio_priority_t p;
34dc7c2f 403
e8b96c60 404 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
ec8501ee 405 avl_destroy(vdev_queue_class_tree(vq, p));
e8b96c60 406 avl_destroy(&vq->vq_active_tree);
ec8501ee
JG
407 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
408 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
34dc7c2f
BB
409
410 mutex_destroy(&vq->vq_lock);
411}
412
413static void
414vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
415{
330847ff
MA
416 spa_t *spa = zio->io_spa;
417 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
418
e8b96c60 419 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
ec8501ee
JG
420 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
421 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
330847ff
MA
422
423 if (ssh->kstat != NULL) {
424 mutex_enter(&ssh->lock);
425 kstat_waitq_enter(ssh->kstat->ks_data);
426 mutex_exit(&ssh->lock);
427 }
34dc7c2f
BB
428}
429
430static void
431vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
432{
330847ff
MA
433 spa_t *spa = zio->io_spa;
434 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
435
e8b96c60 436 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
ec8501ee
JG
437 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
438 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
330847ff
MA
439
440 if (ssh->kstat != NULL) {
441 mutex_enter(&ssh->lock);
442 kstat_waitq_exit(ssh->kstat->ks_data);
443 mutex_exit(&ssh->lock);
444 }
445}
446
447static void
448vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
449{
450 spa_t *spa = zio->io_spa;
451 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
452
e8b96c60
MA
453 ASSERT(MUTEX_HELD(&vq->vq_lock));
454 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
455 vq->vq_class[zio->io_priority].vqc_active++;
456 avl_add(&vq->vq_active_tree, zio);
330847ff
MA
457
458 if (ssh->kstat != NULL) {
459 mutex_enter(&ssh->lock);
460 kstat_runq_enter(ssh->kstat->ks_data);
461 mutex_exit(&ssh->lock);
462 }
463}
464
465static void
466vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
467{
468 spa_t *spa = zio->io_spa;
469 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
470
e8b96c60
MA
471 ASSERT(MUTEX_HELD(&vq->vq_lock));
472 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
473 vq->vq_class[zio->io_priority].vqc_active--;
474 avl_remove(&vq->vq_active_tree, zio);
330847ff
MA
475
476 if (ssh->kstat != NULL) {
477 kstat_io_t *ksio = ssh->kstat->ks_data;
478
479 mutex_enter(&ssh->lock);
480 kstat_runq_exit(ksio);
481 if (zio->io_type == ZIO_TYPE_READ) {
482 ksio->reads++;
483 ksio->nread += zio->io_size;
484 } else if (zio->io_type == ZIO_TYPE_WRITE) {
485 ksio->writes++;
486 ksio->nwritten += zio->io_size;
487 }
488 mutex_exit(&ssh->lock);
489 }
34dc7c2f
BB
490}
491
492static void
493vdev_queue_agg_io_done(zio_t *aio)
494{
e8b96c60
MA
495 if (aio->io_type == ZIO_TYPE_READ) {
496 zio_t *pio;
3dfb57a3
DB
497 zio_link_t *zl = NULL;
498 while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
d164b209
BB
499 bcopy((char *)aio->io_data + (pio->io_offset -
500 aio->io_offset), pio->io_data, pio->io_size);
e8b96c60
MA
501 }
502 }
34dc7c2f 503
285b29d9 504 zio_buf_free(aio->io_data, aio->io_size);
34dc7c2f
BB
505}
506
9babb374
BB
507/*
508 * Compute the range spanned by two i/os, which is the endpoint of the last
509 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
510 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
511 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
512 */
513#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
514#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
34dc7c2f
BB
515
516static zio_t *
e8b96c60 517vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
34dc7c2f 518{
e8b96c60
MA
519 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
520 uint64_t maxgap = 0;
521 uint64_t size;
a58df6f5 522 uint64_t limit;
e8b96c60 523 boolean_t stretch = B_FALSE;
ec8501ee 524 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
e8b96c60 525 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
6fe53787 526 void *buf;
e8b96c60 527
a58df6f5
BB
528 limit = MAX(MIN(zfs_vdev_aggregation_limit,
529 spa_maxblocksize(vq->vq_vdev->vdev_spa)), 0);
34dc7c2f 530
a58df6f5
BB
531 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE || limit == 0)
532 return (NULL);
34dc7c2f 533
e8b96c60 534 first = last = zio;
34dc7c2f 535
e8b96c60
MA
536 if (zio->io_type == ZIO_TYPE_READ)
537 maxgap = zfs_vdev_read_gap_limit;
fb5f0bc8 538
e8b96c60
MA
539 /*
540 * We can aggregate I/Os that are sufficiently adjacent and of
541 * the same flavor, as expressed by the AGG_INHERIT flags.
542 * The latter requirement is necessary so that certain
543 * attributes of the I/O, such as whether it's a normal I/O
544 * or a scrub/resilver, can be preserved in the aggregate.
545 * We can include optional I/Os, but don't allow them
546 * to begin a range as they add no benefit in that situation.
547 */
45d1cae3 548
e8b96c60
MA
549 /*
550 * We keep track of the last non-optional I/O.
551 */
552 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
45d1cae3 553
e8b96c60
MA
554 /*
555 * Walk backwards through sufficiently contiguous I/Os
556 * recording the last non-option I/O.
557 */
558 while ((dio = AVL_PREV(t, first)) != NULL &&
559 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
a58df6f5 560 IO_SPAN(dio, last) <= limit &&
e8b96c60
MA
561 IO_GAP(dio, first) <= maxgap) {
562 first = dio;
563 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
564 mandatory = first;
565 }
45d1cae3 566
e8b96c60
MA
567 /*
568 * Skip any initial optional I/Os.
569 */
570 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
571 first = AVL_NEXT(t, first);
572 ASSERT(first != NULL);
573 }
9babb374 574
45d1cae3 575
e8b96c60
MA
576 /*
577 * Walk forward through sufficiently contiguous I/Os.
578 */
579 while ((dio = AVL_NEXT(t, last)) != NULL &&
580 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
a58df6f5 581 IO_SPAN(first, dio) <= limit &&
e8b96c60
MA
582 IO_GAP(last, dio) <= maxgap) {
583 last = dio;
584 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
585 mandatory = last;
586 }
587
588 /*
589 * Now that we've established the range of the I/O aggregation
590 * we must decide what to do with trailing optional I/Os.
591 * For reads, there's nothing to do. While we are unable to
592 * aggregate further, it's possible that a trailing optional
593 * I/O would allow the underlying device to aggregate with
594 * subsequent I/Os. We must therefore determine if the next
595 * non-optional I/O is close enough to make aggregation
596 * worthwhile.
597 */
598 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
599 zio_t *nio = last;
600 while ((dio = AVL_NEXT(t, nio)) != NULL &&
601 IO_GAP(nio, dio) == 0 &&
602 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
603 nio = dio;
604 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
605 stretch = B_TRUE;
606 break;
45d1cae3
BB
607 }
608 }
e8b96c60 609 }
45d1cae3 610
e8b96c60
MA
611 if (stretch) {
612 /* This may be a no-op. */
613 dio = AVL_NEXT(t, last);
614 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
615 } else {
616 while (last != mandatory && last != first) {
617 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
618 last = AVL_PREV(t, last);
619 ASSERT(last != NULL);
45d1cae3 620 }
34dc7c2f
BB
621 }
622
e8b96c60
MA
623 if (first == last)
624 return (NULL);
625
e8b96c60 626 size = IO_SPAN(first, last);
a58df6f5 627 ASSERT3U(size, <=, limit);
e8b96c60 628
6fe53787
BB
629 buf = zio_buf_alloc_flags(size, KM_NOSLEEP);
630 if (buf == NULL)
631 return (NULL);
632
e8b96c60 633 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
6fe53787 634 buf, size, first->io_type, zio->io_priority,
e8b96c60
MA
635 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
636 vdev_queue_agg_io_done, NULL);
637 aio->io_timestamp = first->io_timestamp;
638
639 nio = first;
640 do {
641 dio = nio;
642 nio = AVL_NEXT(t, dio);
643 ASSERT3U(dio->io_type, ==, aio->io_type);
644
645 if (dio->io_flags & ZIO_FLAG_NODATA) {
646 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
647 bzero((char *)aio->io_data + (dio->io_offset -
648 aio->io_offset), dio->io_size);
649 } else if (dio->io_type == ZIO_TYPE_WRITE) {
650 bcopy(dio->io_data, (char *)aio->io_data +
651 (dio->io_offset - aio->io_offset),
652 dio->io_size);
653 }
d164b209 654
e8b96c60
MA
655 zio_add_child(dio, aio);
656 vdev_queue_io_remove(vq, dio);
657 zio_vdev_io_bypass(dio);
658 zio_execute(dio);
659 } while (dio != last);
34dc7c2f 660
e8b96c60
MA
661 return (aio);
662}
663
664static zio_t *
665vdev_queue_io_to_issue(vdev_queue_t *vq)
666{
667 zio_t *zio, *aio;
668 zio_priority_t p;
669 avl_index_t idx;
ec8501ee 670 avl_tree_t *tree;
e8b96c60
MA
671
672again:
673 ASSERT(MUTEX_HELD(&vq->vq_lock));
674
675 p = vdev_queue_class_to_issue(vq);
34dc7c2f 676
e8b96c60
MA
677 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
678 /* No eligible queued i/os */
679 return (NULL);
34dc7c2f
BB
680 }
681
e8b96c60
MA
682 /*
683 * For LBA-ordered queues (async / scrub), issue the i/o which follows
684 * the most recently issued i/o in LBA (offset) order.
685 *
686 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
687 */
ec8501ee 688 tree = vdev_queue_class_tree(vq, p);
50b25b21
BB
689 vq->vq_io_search.io_timestamp = 0;
690 vq->vq_io_search.io_offset = vq->vq_last_offset + 1;
ec8501ee 691 VERIFY3P(avl_find(tree, &vq->vq_io_search,
50b25b21 692 &idx), ==, NULL);
ec8501ee 693 zio = avl_nearest(tree, idx, AVL_AFTER);
e8b96c60 694 if (zio == NULL)
ec8501ee 695 zio = avl_first(tree);
e8b96c60
MA
696 ASSERT3U(zio->io_priority, ==, p);
697
698 aio = vdev_queue_aggregate(vq, zio);
699 if (aio != NULL)
700 zio = aio;
701 else
702 vdev_queue_io_remove(vq, zio);
34dc7c2f 703
45d1cae3
BB
704 /*
705 * If the I/O is or was optional and therefore has no data, we need to
706 * simply discard it. We need to drop the vdev queue's lock to avoid a
707 * deadlock that we could encounter since this I/O will complete
708 * immediately.
709 */
e8b96c60 710 if (zio->io_flags & ZIO_FLAG_NODATA) {
45d1cae3 711 mutex_exit(&vq->vq_lock);
e8b96c60
MA
712 zio_vdev_io_bypass(zio);
713 zio_execute(zio);
45d1cae3
BB
714 mutex_enter(&vq->vq_lock);
715 goto again;
716 }
717
e8b96c60
MA
718 vdev_queue_pending_add(vq, zio);
719 vq->vq_last_offset = zio->io_offset;
34dc7c2f 720
e8b96c60 721 return (zio);
34dc7c2f
BB
722}
723
724zio_t *
725vdev_queue_io(zio_t *zio)
726{
727 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
728 zio_t *nio;
729
34dc7c2f
BB
730 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
731 return (zio);
732
e8b96c60
MA
733 /*
734 * Children i/os inherent their parent's priority, which might
735 * not match the child's i/o type. Fix it up here.
736 */
737 if (zio->io_type == ZIO_TYPE_READ) {
738 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
739 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
740 zio->io_priority != ZIO_PRIORITY_SCRUB)
741 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
742 } else {
743 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
744 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
745 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
746 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
747 }
34dc7c2f 748
e8b96c60 749 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
34dc7c2f
BB
750
751 mutex_enter(&vq->vq_lock);
cb682a17 752 zio->io_timestamp = gethrtime();
34dc7c2f 753 vdev_queue_io_add(vq, zio);
e8b96c60 754 nio = vdev_queue_io_to_issue(vq);
34dc7c2f
BB
755 mutex_exit(&vq->vq_lock);
756
757 if (nio == NULL)
758 return (NULL);
759
760 if (nio->io_done == vdev_queue_agg_io_done) {
761 zio_nowait(nio);
762 return (NULL);
763 }
764
765 return (nio);
766}
767
768void
769vdev_queue_io_done(zio_t *zio)
770{
771 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
e8b96c60 772 zio_t *nio;
34dc7c2f
BB
773
774 mutex_enter(&vq->vq_lock);
775
330847ff 776 vdev_queue_pending_remove(vq, zio);
34dc7c2f 777
cb682a17
MA
778 zio->io_delta = gethrtime() - zio->io_timestamp;
779 vq->vq_io_complete_ts = gethrtime();
cc92e9d0
GW
780 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
781
e8b96c60 782 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
34dc7c2f
BB
783 mutex_exit(&vq->vq_lock);
784 if (nio->io_done == vdev_queue_agg_io_done) {
785 zio_nowait(nio);
786 } else {
787 zio_vdev_io_reissue(nio);
788 zio_execute(nio);
789 }
790 mutex_enter(&vq->vq_lock);
791 }
792
793 mutex_exit(&vq->vq_lock);
794}
c28b2279 795
9f500936 796/*
797 * As these three methods are only used for load calculations we're not
798 * concerned if we get an incorrect value on 32bit platforms due to lack of
799 * vq_lock mutex use here, instead we prefer to keep it lock free for
800 * performance.
801 */
802int
803vdev_queue_length(vdev_t *vd)
804{
805 return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
806}
807
808uint64_t
809vdev_queue_lastoffset(vdev_t *vd)
810{
811 return (vd->vdev_queue.vq_lastoffset);
812}
813
814void
815vdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio)
816{
817 vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size;
818}
819
c28b2279 820#if defined(_KERNEL) && defined(HAVE_SPL)
c28b2279 821module_param(zfs_vdev_aggregation_limit, int, 0644);
c409e464
BB
822MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
823
c409e464
BB
824module_param(zfs_vdev_read_gap_limit, int, 0644);
825MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
826
827module_param(zfs_vdev_write_gap_limit, int, 0644);
828MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
e8b96c60
MA
829
830module_param(zfs_vdev_max_active, int, 0644);
831MODULE_PARM_DESC(zfs_vdev_max_active, "Maximum number of active I/Os per vdev");
832
833module_param(zfs_vdev_async_write_active_max_dirty_percent, int, 0644);
834MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent,
d1d7e268 835 "Async write concurrency max threshold");
e8b96c60
MA
836
837module_param(zfs_vdev_async_write_active_min_dirty_percent, int, 0644);
838MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent,
d1d7e268 839 "Async write concurrency min threshold");
e8b96c60
MA
840
841module_param(zfs_vdev_async_read_max_active, int, 0644);
842MODULE_PARM_DESC(zfs_vdev_async_read_max_active,
d1d7e268 843 "Max active async read I/Os per vdev");
e8b96c60
MA
844
845module_param(zfs_vdev_async_read_min_active, int, 0644);
846MODULE_PARM_DESC(zfs_vdev_async_read_min_active,
d1d7e268 847 "Min active async read I/Os per vdev");
e8b96c60
MA
848
849module_param(zfs_vdev_async_write_max_active, int, 0644);
850MODULE_PARM_DESC(zfs_vdev_async_write_max_active,
d1d7e268 851 "Max active async write I/Os per vdev");
e8b96c60
MA
852
853module_param(zfs_vdev_async_write_min_active, int, 0644);
854MODULE_PARM_DESC(zfs_vdev_async_write_min_active,
d1d7e268 855 "Min active async write I/Os per vdev");
e8b96c60
MA
856
857module_param(zfs_vdev_scrub_max_active, int, 0644);
858MODULE_PARM_DESC(zfs_vdev_scrub_max_active, "Max active scrub I/Os per vdev");
859
860module_param(zfs_vdev_scrub_min_active, int, 0644);
861MODULE_PARM_DESC(zfs_vdev_scrub_min_active, "Min active scrub I/Os per vdev");
862
863module_param(zfs_vdev_sync_read_max_active, int, 0644);
864MODULE_PARM_DESC(zfs_vdev_sync_read_max_active,
d1d7e268 865 "Max active sync read I/Os per vdev");
e8b96c60
MA
866
867module_param(zfs_vdev_sync_read_min_active, int, 0644);
868MODULE_PARM_DESC(zfs_vdev_sync_read_min_active,
d1d7e268 869 "Min active sync read I/Os per vdev");
e8b96c60
MA
870
871module_param(zfs_vdev_sync_write_max_active, int, 0644);
872MODULE_PARM_DESC(zfs_vdev_sync_write_max_active,
d1d7e268 873 "Max active sync write I/Os per vdev");
e8b96c60
MA
874
875module_param(zfs_vdev_sync_write_min_active, int, 0644);
876MODULE_PARM_DESC(zfs_vdev_sync_write_min_active,
3757bff3 877 "Min active sync write I/Os per vdev");
3dfb57a3
DB
878
879module_param(zfs_vdev_queue_depth_pct, int, 0644);
880MODULE_PARM_DESC(zfs_vdev_queue_depth_pct,
881 "Queue depth percentage for each top-level vdev");
c28b2279 882#endif