]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/vdev_queue.c
cstyle: Resolve C style issues
[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/*
e8b96c60 27 * Copyright (c) 2013 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
BB
169 */
170int zfs_vdev_aggregation_limit = SPA_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
BB
176{
177 const zio_t *z1 = x1;
178 const zio_t *z2 = x2;
179
34dc7c2f
BB
180 if (z1->io_offset < z2->io_offset)
181 return (-1);
182 if (z1->io_offset > z2->io_offset)
183 return (1);
184
185 if (z1 < z2)
186 return (-1);
187 if (z1 > z2)
188 return (1);
189
190 return (0);
191}
192
193int
e8b96c60 194vdev_queue_timestamp_compare(const void *x1, const void *x2)
34dc7c2f
BB
195{
196 const zio_t *z1 = x1;
197 const zio_t *z2 = x2;
198
e8b96c60 199 if (z1->io_timestamp < z2->io_timestamp)
34dc7c2f 200 return (-1);
e8b96c60 201 if (z1->io_timestamp > z2->io_timestamp)
34dc7c2f
BB
202 return (1);
203
204 if (z1 < z2)
205 return (-1);
206 if (z1 > z2)
207 return (1);
208
209 return (0);
210}
211
e8b96c60
MA
212static int
213vdev_queue_class_min_active(zio_priority_t p)
214{
215 switch (p) {
216 case ZIO_PRIORITY_SYNC_READ:
217 return (zfs_vdev_sync_read_min_active);
218 case ZIO_PRIORITY_SYNC_WRITE:
219 return (zfs_vdev_sync_write_min_active);
220 case ZIO_PRIORITY_ASYNC_READ:
221 return (zfs_vdev_async_read_min_active);
222 case ZIO_PRIORITY_ASYNC_WRITE:
223 return (zfs_vdev_async_write_min_active);
224 case ZIO_PRIORITY_SCRUB:
225 return (zfs_vdev_scrub_min_active);
226 default:
227 panic("invalid priority %u", p);
228 return (0);
229 }
230}
231
232static int
233vdev_queue_max_async_writes(uint64_t dirty)
234{
235 int writes;
236 uint64_t min_bytes = zfs_dirty_data_max *
237 zfs_vdev_async_write_active_min_dirty_percent / 100;
238 uint64_t max_bytes = zfs_dirty_data_max *
239 zfs_vdev_async_write_active_max_dirty_percent / 100;
240
241 if (dirty < min_bytes)
242 return (zfs_vdev_async_write_min_active);
243 if (dirty > max_bytes)
244 return (zfs_vdev_async_write_max_active);
245
246 /*
247 * linear interpolation:
248 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
249 * move right by min_bytes
250 * move up by min_writes
251 */
252 writes = (dirty - min_bytes) *
253 (zfs_vdev_async_write_max_active -
254 zfs_vdev_async_write_min_active) /
255 (max_bytes - min_bytes) +
256 zfs_vdev_async_write_min_active;
257 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
258 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
259 return (writes);
260}
261
262static int
263vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
264{
265 switch (p) {
266 case ZIO_PRIORITY_SYNC_READ:
267 return (zfs_vdev_sync_read_max_active);
268 case ZIO_PRIORITY_SYNC_WRITE:
269 return (zfs_vdev_sync_write_max_active);
270 case ZIO_PRIORITY_ASYNC_READ:
271 return (zfs_vdev_async_read_max_active);
272 case ZIO_PRIORITY_ASYNC_WRITE:
273 return (vdev_queue_max_async_writes(
274 spa->spa_dsl_pool->dp_dirty_total));
275 case ZIO_PRIORITY_SCRUB:
276 return (zfs_vdev_scrub_max_active);
277 default:
278 panic("invalid priority %u", p);
279 return (0);
280 }
281}
282
283/*
284 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
285 * there is no eligible class.
286 */
287static zio_priority_t
288vdev_queue_class_to_issue(vdev_queue_t *vq)
289{
290 spa_t *spa = vq->vq_vdev->vdev_spa;
291 zio_priority_t p;
292
293 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
294 return (ZIO_PRIORITY_NUM_QUEUEABLE);
295
296 /* find a queue that has not reached its minimum # outstanding i/os */
297 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
298 if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 &&
299 vq->vq_class[p].vqc_active <
300 vdev_queue_class_min_active(p))
301 return (p);
302 }
303
304 /*
305 * If we haven't found a queue, look for one that hasn't reached its
306 * maximum # outstanding i/os.
307 */
308 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
309 if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 &&
310 vq->vq_class[p].vqc_active <
311 vdev_queue_class_max_active(spa, p))
312 return (p);
313 }
314
315 /* No eligible queued i/os */
316 return (ZIO_PRIORITY_NUM_QUEUEABLE);
317}
318
34dc7c2f
BB
319void
320vdev_queue_init(vdev_t *vd)
321{
322 vdev_queue_t *vq = &vd->vdev_queue;
e8b96c60
MA
323 int max_active_sum;
324 zio_priority_t p;
86dd0fd9 325 int i;
34dc7c2f
BB
326
327 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
e8b96c60 328 vq->vq_vdev = vd;
34dc7c2f 329
e8b96c60
MA
330 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
331 sizeof (zio_t), offsetof(struct zio, io_queue_node));
34dc7c2f 332
e8b96c60
MA
333 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
334 /*
335 * The synchronous i/o queues are FIFO rather than LBA ordered.
336 * This provides more consistent latency for these i/os, and
337 * they tend to not be tightly clustered anyway so there is
338 * little to no throughput loss.
339 */
340 boolean_t fifo = (p == ZIO_PRIORITY_SYNC_READ ||
341 p == ZIO_PRIORITY_SYNC_WRITE);
342 avl_create(&vq->vq_class[p].vqc_queued_tree,
343 fifo ? vdev_queue_timestamp_compare :
344 vdev_queue_offset_compare,
345 sizeof (zio_t), offsetof(struct zio, io_queue_node));
346 }
86dd0fd9
BB
347
348 /*
349 * A list of buffers which can be used for aggregate I/O, this
350 * avoids the need to allocate them on demand when memory is low.
351 */
352 list_create(&vq->vq_io_list, sizeof (vdev_io_t),
353 offsetof(vdev_io_t, vi_node));
354
e8b96c60
MA
355 max_active_sum = zfs_vdev_sync_read_max_active +
356 zfs_vdev_sync_write_max_active + zfs_vdev_async_read_max_active +
357 zfs_vdev_async_write_max_active + zfs_vdev_scrub_max_active;
358 for (i = 0; i < max_active_sum; i++)
86dd0fd9 359 list_insert_tail(&vq->vq_io_list, zio_vdev_alloc());
34dc7c2f
BB
360}
361
362void
363vdev_queue_fini(vdev_t *vd)
364{
365 vdev_queue_t *vq = &vd->vdev_queue;
86dd0fd9 366 vdev_io_t *vi;
e8b96c60 367 zio_priority_t p;
34dc7c2f 368
e8b96c60
MA
369 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
370 avl_destroy(&vq->vq_class[p].vqc_queued_tree);
371 avl_destroy(&vq->vq_active_tree);
34dc7c2f 372
86dd0fd9
BB
373 while ((vi = list_head(&vq->vq_io_list)) != NULL) {
374 list_remove(&vq->vq_io_list, vi);
375 zio_vdev_free(vi);
376 }
377
378 list_destroy(&vq->vq_io_list);
379
34dc7c2f
BB
380 mutex_destroy(&vq->vq_lock);
381}
382
383static void
384vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
385{
330847ff
MA
386 spa_t *spa = zio->io_spa;
387 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
388
e8b96c60
MA
389 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
390 avl_add(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio);
330847ff
MA
391
392 if (ssh->kstat != NULL) {
393 mutex_enter(&ssh->lock);
394 kstat_waitq_enter(ssh->kstat->ks_data);
395 mutex_exit(&ssh->lock);
396 }
34dc7c2f
BB
397}
398
399static void
400vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
401{
330847ff
MA
402 spa_t *spa = zio->io_spa;
403 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
404
e8b96c60
MA
405 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
406 avl_remove(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio);
330847ff
MA
407
408 if (ssh->kstat != NULL) {
409 mutex_enter(&ssh->lock);
410 kstat_waitq_exit(ssh->kstat->ks_data);
411 mutex_exit(&ssh->lock);
412 }
413}
414
415static void
416vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
417{
418 spa_t *spa = zio->io_spa;
419 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
420
e8b96c60
MA
421 ASSERT(MUTEX_HELD(&vq->vq_lock));
422 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
423 vq->vq_class[zio->io_priority].vqc_active++;
424 avl_add(&vq->vq_active_tree, zio);
330847ff
MA
425
426 if (ssh->kstat != NULL) {
427 mutex_enter(&ssh->lock);
428 kstat_runq_enter(ssh->kstat->ks_data);
429 mutex_exit(&ssh->lock);
430 }
431}
432
433static void
434vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
435{
436 spa_t *spa = zio->io_spa;
437 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
438
e8b96c60
MA
439 ASSERT(MUTEX_HELD(&vq->vq_lock));
440 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
441 vq->vq_class[zio->io_priority].vqc_active--;
442 avl_remove(&vq->vq_active_tree, zio);
330847ff
MA
443
444 if (ssh->kstat != NULL) {
445 kstat_io_t *ksio = ssh->kstat->ks_data;
446
447 mutex_enter(&ssh->lock);
448 kstat_runq_exit(ksio);
449 if (zio->io_type == ZIO_TYPE_READ) {
450 ksio->reads++;
451 ksio->nread += zio->io_size;
452 } else if (zio->io_type == ZIO_TYPE_WRITE) {
453 ksio->writes++;
454 ksio->nwritten += zio->io_size;
455 }
456 mutex_exit(&ssh->lock);
457 }
34dc7c2f
BB
458}
459
460static void
461vdev_queue_agg_io_done(zio_t *aio)
462{
86dd0fd9
BB
463 vdev_queue_t *vq = &aio->io_vd->vdev_queue;
464 vdev_io_t *vi = aio->io_data;
34dc7c2f 465
e8b96c60
MA
466 if (aio->io_type == ZIO_TYPE_READ) {
467 zio_t *pio;
468 while ((pio = zio_walk_parents(aio)) != NULL) {
d164b209
BB
469 bcopy((char *)aio->io_data + (pio->io_offset -
470 aio->io_offset), pio->io_data, pio->io_size);
e8b96c60
MA
471 }
472 }
34dc7c2f 473
86dd0fd9
BB
474 mutex_enter(&vq->vq_lock);
475 list_insert_tail(&vq->vq_io_list, vi);
476 mutex_exit(&vq->vq_lock);
34dc7c2f
BB
477}
478
9babb374
BB
479/*
480 * Compute the range spanned by two i/os, which is the endpoint of the last
481 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
482 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
483 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
484 */
485#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
486#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
34dc7c2f
BB
487
488static zio_t *
e8b96c60 489vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
34dc7c2f 490{
86dd0fd9 491 vdev_io_t *vi;
e8b96c60
MA
492 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
493 uint64_t maxgap = 0;
494 uint64_t size;
495 boolean_t stretch = B_FALSE;
496 vdev_queue_class_t *vqc = &vq->vq_class[zio->io_priority];
497 avl_tree_t *t = &vqc->vqc_queued_tree;
498 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
499
500 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
501 return (NULL);
34dc7c2f 502
d1d7e268
MK
503 /*
504 * Prevent users from setting the zfs_vdev_aggregation_limit
505 * tuning larger than SPA_MAXBLOCKSIZE.
506 */
e8b96c60
MA
507 zfs_vdev_aggregation_limit =
508 MIN(zfs_vdev_aggregation_limit, SPA_MAXBLOCKSIZE);
34dc7c2f 509
e8b96c60
MA
510 /*
511 * The synchronous i/o queues are not sorted by LBA, so we can't
512 * find adjacent i/os. These i/os tend to not be tightly clustered,
513 * or too large to aggregate, so this has little impact on performance.
514 */
515 if (zio->io_priority == ZIO_PRIORITY_SYNC_READ ||
516 zio->io_priority == ZIO_PRIORITY_SYNC_WRITE)
34dc7c2f
BB
517 return (NULL);
518
e8b96c60 519 first = last = zio;
34dc7c2f 520
e8b96c60
MA
521 if (zio->io_type == ZIO_TYPE_READ)
522 maxgap = zfs_vdev_read_gap_limit;
fb5f0bc8 523
86dd0fd9
BB
524 vi = list_head(&vq->vq_io_list);
525 if (vi == NULL) {
526 vi = zio_vdev_alloc();
527 list_insert_head(&vq->vq_io_list, vi);
528 }
529
e8b96c60
MA
530 /*
531 * We can aggregate I/Os that are sufficiently adjacent and of
532 * the same flavor, as expressed by the AGG_INHERIT flags.
533 * The latter requirement is necessary so that certain
534 * attributes of the I/O, such as whether it's a normal I/O
535 * or a scrub/resilver, can be preserved in the aggregate.
536 * We can include optional I/Os, but don't allow them
537 * to begin a range as they add no benefit in that situation.
538 */
45d1cae3 539
e8b96c60
MA
540 /*
541 * We keep track of the last non-optional I/O.
542 */
543 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
45d1cae3 544
e8b96c60
MA
545 /*
546 * Walk backwards through sufficiently contiguous I/Os
547 * recording the last non-option I/O.
548 */
549 while ((dio = AVL_PREV(t, first)) != NULL &&
550 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
551 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
552 IO_GAP(dio, first) <= maxgap) {
553 first = dio;
554 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
555 mandatory = first;
556 }
45d1cae3 557
e8b96c60
MA
558 /*
559 * Skip any initial optional I/Os.
560 */
561 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
562 first = AVL_NEXT(t, first);
563 ASSERT(first != NULL);
564 }
9babb374 565
45d1cae3 566
e8b96c60
MA
567 /*
568 * Walk forward through sufficiently contiguous I/Os.
569 */
570 while ((dio = AVL_NEXT(t, last)) != NULL &&
571 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
572 IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
573 IO_GAP(last, dio) <= maxgap) {
574 last = dio;
575 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
576 mandatory = last;
577 }
578
579 /*
580 * Now that we've established the range of the I/O aggregation
581 * we must decide what to do with trailing optional I/Os.
582 * For reads, there's nothing to do. While we are unable to
583 * aggregate further, it's possible that a trailing optional
584 * I/O would allow the underlying device to aggregate with
585 * subsequent I/Os. We must therefore determine if the next
586 * non-optional I/O is close enough to make aggregation
587 * worthwhile.
588 */
589 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
590 zio_t *nio = last;
591 while ((dio = AVL_NEXT(t, nio)) != NULL &&
592 IO_GAP(nio, dio) == 0 &&
593 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
594 nio = dio;
595 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
596 stretch = B_TRUE;
597 break;
45d1cae3
BB
598 }
599 }
e8b96c60 600 }
45d1cae3 601
e8b96c60
MA
602 if (stretch) {
603 /* This may be a no-op. */
604 dio = AVL_NEXT(t, last);
605 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
606 } else {
607 while (last != mandatory && last != first) {
608 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
609 last = AVL_PREV(t, last);
610 ASSERT(last != NULL);
45d1cae3 611 }
34dc7c2f
BB
612 }
613
e8b96c60
MA
614 if (first == last)
615 return (NULL);
616
617 ASSERT(vi != NULL);
618
619 size = IO_SPAN(first, last);
620 ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
621
622 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
623 vi, size, first->io_type, zio->io_priority,
624 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
625 vdev_queue_agg_io_done, NULL);
626 aio->io_timestamp = first->io_timestamp;
627
628 nio = first;
629 do {
630 dio = nio;
631 nio = AVL_NEXT(t, dio);
632 ASSERT3U(dio->io_type, ==, aio->io_type);
633
634 if (dio->io_flags & ZIO_FLAG_NODATA) {
635 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
636 bzero((char *)aio->io_data + (dio->io_offset -
637 aio->io_offset), dio->io_size);
638 } else if (dio->io_type == ZIO_TYPE_WRITE) {
639 bcopy(dio->io_data, (char *)aio->io_data +
640 (dio->io_offset - aio->io_offset),
641 dio->io_size);
642 }
d164b209 643
e8b96c60
MA
644 zio_add_child(dio, aio);
645 vdev_queue_io_remove(vq, dio);
646 zio_vdev_io_bypass(dio);
647 zio_execute(dio);
648 } while (dio != last);
34dc7c2f 649
e8b96c60
MA
650 list_remove(&vq->vq_io_list, vi);
651
652 return (aio);
653}
654
655static zio_t *
656vdev_queue_io_to_issue(vdev_queue_t *vq)
657{
658 zio_t *zio, *aio;
659 zio_priority_t p;
660 avl_index_t idx;
661 vdev_queue_class_t *vqc;
662 zio_t *search;
663
664again:
665 ASSERT(MUTEX_HELD(&vq->vq_lock));
666
667 p = vdev_queue_class_to_issue(vq);
34dc7c2f 668
e8b96c60
MA
669 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
670 /* No eligible queued i/os */
671 return (NULL);
34dc7c2f
BB
672 }
673
e8b96c60
MA
674 /*
675 * For LBA-ordered queues (async / scrub), issue the i/o which follows
676 * the most recently issued i/o in LBA (offset) order.
677 *
678 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
679 */
680 vqc = &vq->vq_class[p];
d1d7e268 681 search = zio_buf_alloc(sizeof (*search));
e8b96c60
MA
682 search->io_timestamp = 0;
683 search->io_offset = vq->vq_last_offset + 1;
684 VERIFY3P(avl_find(&vqc->vqc_queued_tree, search, &idx), ==, NULL);
d1d7e268 685 zio_buf_free(search, sizeof (*search));
e8b96c60
MA
686 zio = avl_nearest(&vqc->vqc_queued_tree, idx, AVL_AFTER);
687 if (zio == NULL)
688 zio = avl_first(&vqc->vqc_queued_tree);
689 ASSERT3U(zio->io_priority, ==, p);
690
691 aio = vdev_queue_aggregate(vq, zio);
692 if (aio != NULL)
693 zio = aio;
694 else
695 vdev_queue_io_remove(vq, zio);
34dc7c2f 696
45d1cae3
BB
697 /*
698 * If the I/O is or was optional and therefore has no data, we need to
699 * simply discard it. We need to drop the vdev queue's lock to avoid a
700 * deadlock that we could encounter since this I/O will complete
701 * immediately.
702 */
e8b96c60 703 if (zio->io_flags & ZIO_FLAG_NODATA) {
45d1cae3 704 mutex_exit(&vq->vq_lock);
e8b96c60
MA
705 zio_vdev_io_bypass(zio);
706 zio_execute(zio);
45d1cae3
BB
707 mutex_enter(&vq->vq_lock);
708 goto again;
709 }
710
e8b96c60
MA
711 vdev_queue_pending_add(vq, zio);
712 vq->vq_last_offset = zio->io_offset;
34dc7c2f 713
e8b96c60 714 return (zio);
34dc7c2f
BB
715}
716
717zio_t *
718vdev_queue_io(zio_t *zio)
719{
720 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
721 zio_t *nio;
722
34dc7c2f
BB
723 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
724 return (zio);
725
e8b96c60
MA
726 /*
727 * Children i/os inherent their parent's priority, which might
728 * not match the child's i/o type. Fix it up here.
729 */
730 if (zio->io_type == ZIO_TYPE_READ) {
731 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
732 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
733 zio->io_priority != ZIO_PRIORITY_SCRUB)
734 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
735 } else {
736 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
737 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
738 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
739 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
740 }
34dc7c2f 741
e8b96c60 742 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
34dc7c2f
BB
743
744 mutex_enter(&vq->vq_lock);
cb682a17 745 zio->io_timestamp = gethrtime();
34dc7c2f 746 vdev_queue_io_add(vq, zio);
e8b96c60 747 nio = vdev_queue_io_to_issue(vq);
34dc7c2f
BB
748 mutex_exit(&vq->vq_lock);
749
750 if (nio == NULL)
751 return (NULL);
752
753 if (nio->io_done == vdev_queue_agg_io_done) {
754 zio_nowait(nio);
755 return (NULL);
756 }
757
758 return (nio);
759}
760
761void
762vdev_queue_io_done(zio_t *zio)
763{
764 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
e8b96c60 765 zio_t *nio;
34dc7c2f 766
cc92e9d0
GW
767 if (zio_injection_enabled)
768 delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
769
34dc7c2f
BB
770 mutex_enter(&vq->vq_lock);
771
330847ff 772 vdev_queue_pending_remove(vq, zio);
34dc7c2f 773
cb682a17
MA
774 zio->io_delta = gethrtime() - zio->io_timestamp;
775 vq->vq_io_complete_ts = gethrtime();
cc92e9d0
GW
776 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
777
e8b96c60 778 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
34dc7c2f
BB
779 mutex_exit(&vq->vq_lock);
780 if (nio->io_done == vdev_queue_agg_io_done) {
781 zio_nowait(nio);
782 } else {
783 zio_vdev_io_reissue(nio);
784 zio_execute(nio);
785 }
786 mutex_enter(&vq->vq_lock);
787 }
788
789 mutex_exit(&vq->vq_lock);
790}
c28b2279
BB
791
792#if defined(_KERNEL) && defined(HAVE_SPL)
c28b2279 793module_param(zfs_vdev_aggregation_limit, int, 0644);
c409e464
BB
794MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
795
c409e464
BB
796module_param(zfs_vdev_read_gap_limit, int, 0644);
797MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
798
799module_param(zfs_vdev_write_gap_limit, int, 0644);
800MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
e8b96c60
MA
801
802module_param(zfs_vdev_max_active, int, 0644);
803MODULE_PARM_DESC(zfs_vdev_max_active, "Maximum number of active I/Os per vdev");
804
805module_param(zfs_vdev_async_write_active_max_dirty_percent, int, 0644);
806MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent,
d1d7e268 807 "Async write concurrency max threshold");
e8b96c60
MA
808
809module_param(zfs_vdev_async_write_active_min_dirty_percent, int, 0644);
810MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent,
d1d7e268 811 "Async write concurrency min threshold");
e8b96c60
MA
812
813module_param(zfs_vdev_async_read_max_active, int, 0644);
814MODULE_PARM_DESC(zfs_vdev_async_read_max_active,
d1d7e268 815 "Max active async read I/Os per vdev");
e8b96c60
MA
816
817module_param(zfs_vdev_async_read_min_active, int, 0644);
818MODULE_PARM_DESC(zfs_vdev_async_read_min_active,
d1d7e268 819 "Min active async read I/Os per vdev");
e8b96c60
MA
820
821module_param(zfs_vdev_async_write_max_active, int, 0644);
822MODULE_PARM_DESC(zfs_vdev_async_write_max_active,
d1d7e268 823 "Max active async write I/Os per vdev");
e8b96c60
MA
824
825module_param(zfs_vdev_async_write_min_active, int, 0644);
826MODULE_PARM_DESC(zfs_vdev_async_write_min_active,
d1d7e268 827 "Min active async write I/Os per vdev");
e8b96c60
MA
828
829module_param(zfs_vdev_scrub_max_active, int, 0644);
830MODULE_PARM_DESC(zfs_vdev_scrub_max_active, "Max active scrub I/Os per vdev");
831
832module_param(zfs_vdev_scrub_min_active, int, 0644);
833MODULE_PARM_DESC(zfs_vdev_scrub_min_active, "Min active scrub I/Os per vdev");
834
835module_param(zfs_vdev_sync_read_max_active, int, 0644);
836MODULE_PARM_DESC(zfs_vdev_sync_read_max_active,
d1d7e268 837 "Max active sync read I/Os per vdev");
e8b96c60
MA
838
839module_param(zfs_vdev_sync_read_min_active, int, 0644);
840MODULE_PARM_DESC(zfs_vdev_sync_read_min_active,
d1d7e268 841 "Min active sync read I/Os per vdev");
e8b96c60
MA
842
843module_param(zfs_vdev_sync_write_max_active, int, 0644);
844MODULE_PARM_DESC(zfs_vdev_sync_write_max_active,
d1d7e268 845 "Max active sync write I/Os per vdev");
e8b96c60
MA
846
847module_param(zfs_vdev_sync_write_min_active, int, 0644);
848MODULE_PARM_DESC(zfs_vdev_sync_write_min_active,
d1d7e268 849 "Min active sync write I/Osper vdev");
c28b2279 850#endif