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