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 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 */
30
31 #include <sys/zfs_context.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/spa_impl.h>
34 #include <sys/zio.h>
35 #include <sys/avl.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/metaslab_impl.h>
38 #include <sys/abd.h>
39
40 /*
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 six I/O classes
47 * prioritized in the following order: sync read, sync write, async read,
48 * async write, scrub/resilver and trim. 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, and if the
52 * aggregate maximum is equal to or greater than the sum of the per-queue
53 * maximums, the per-queue minimum has no effect.
54 *
55 * For many physical devices, throughput increases with the number of
56 * concurrent operations, but latency typically suffers. Further, physical
57 * devices typically have a limit at which more concurrent operations have no
58 * effect on throughput or can actually cause it to decrease.
59 *
60 * The scheduler selects the next operation to issue by first looking for an
61 * I/O class whose minimum has not been satisfied. Once all are satisfied and
62 * the aggregate maximum has not been hit, the scheduler looks for classes
63 * whose maximum has not been satisfied. Iteration through the I/O classes is
64 * done in the order specified above. No further operations are issued if the
65 * aggregate maximum number of concurrent operations has been hit or if there
66 * are no operations queued for an I/O class that has not hit its maximum.
67 * Every time an I/O is queued or an operation completes, the I/O scheduler
68 * looks for new operations to issue.
69 *
70 * All I/O classes have a fixed maximum number of outstanding operations
71 * except for the async write class. Asynchronous writes represent the data
72 * that is committed to stable storage during the syncing stage for
73 * transaction groups (see txg.c). Transaction groups enter the syncing state
74 * periodically so the number of queued async writes will quickly burst up and
75 * then bleed down to zero. Rather than servicing them as quickly as possible,
76 * the I/O scheduler changes the maximum number of active async write I/Os
77 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
78 * both throughput and latency typically increase with the number of
79 * concurrent operations issued to physical devices, reducing the burstiness
80 * in the number of concurrent operations also stabilizes the response time of
81 * operations from other -- and in particular synchronous -- queues. In broad
82 * strokes, the I/O scheduler will issue more concurrent operations from the
83 * async write queue as there's more dirty data in the pool.
84 *
85 * Async Writes
86 *
87 * The number of concurrent operations issued for the async write I/O class
88 * follows a piece-wise linear function defined by a few adjustable points.
89 *
90 * | o---------| <-- zfs_vdev_async_write_max_active
91 * ^ | /^ |
92 * | | / | |
93 * active | / | |
94 * I/O | / | |
95 * count | / | |
96 * | / | |
97 * |------------o | | <-- zfs_vdev_async_write_min_active
98 * 0|____________^______|_________|
99 * 0% | | 100% of zfs_dirty_data_max
100 * | |
101 * | `-- zfs_vdev_async_write_active_max_dirty_percent
102 * `--------- zfs_vdev_async_write_active_min_dirty_percent
103 *
104 * Until the amount of dirty data exceeds a minimum percentage of the dirty
105 * data allowed in the pool, the I/O scheduler will limit the number of
106 * concurrent operations to the minimum. As that threshold is crossed, the
107 * number of concurrent operations issued increases linearly to the maximum at
108 * the specified maximum percentage of the dirty data allowed in the pool.
109 *
110 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
111 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
112 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
113 * maximum percentage, this indicates that the rate of incoming data is
114 * greater than the rate that the backend storage can handle. In this case, we
115 * must further throttle incoming writes (see dmu_tx_delay() for details).
116 */
117
118 /*
119 * The maximum number of I/Os active to each device. Ideally, this will be >=
120 * the sum of each queue's max_active. It must be at least the sum of each
121 * queue's min_active.
122 */
123 uint32_t zfs_vdev_max_active = 1000;
124
125 /*
126 * Per-queue limits on the number of I/Os active to each device. If the
127 * sum of the queue's max_active is < zfs_vdev_max_active, then the
128 * min_active comes into play. We will send min_active from each queue,
129 * and then select from queues in the order defined by zio_priority_t.
130 *
131 * In general, smaller max_active's will lead to lower latency of synchronous
132 * operations. Larger max_active's may lead to higher overall throughput,
133 * depending on underlying storage.
134 *
135 * The ratio of the queues' max_actives determines the balance of performance
136 * between reads, writes, and scrubs. E.g., increasing
137 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
138 * more quickly, but reads and writes to have higher latency and lower
139 * throughput.
140 */
141 uint32_t zfs_vdev_sync_read_min_active = 10;
142 uint32_t zfs_vdev_sync_read_max_active = 10;
143 uint32_t zfs_vdev_sync_write_min_active = 10;
144 uint32_t zfs_vdev_sync_write_max_active = 10;
145 uint32_t zfs_vdev_async_read_min_active = 1;
146 uint32_t zfs_vdev_async_read_max_active = 3;
147 uint32_t zfs_vdev_async_write_min_active = 1;
148 uint32_t zfs_vdev_async_write_max_active = 10;
149 uint32_t zfs_vdev_scrub_min_active = 1;
150 uint32_t zfs_vdev_scrub_max_active = 2;
151 uint32_t zfs_vdev_trim_min_active = 1;
152 /*
153 * TRIM max active is large in comparison to the other values due to the fact
154 * that TRIM IOs are coalesced at the device layer. This value is set such
155 * that a typical SSD can process the queued IOs in a single request.
156 */
157 uint32_t zfs_vdev_trim_max_active = 64;
158 uint32_t zfs_vdev_removal_min_active = 1;
159 uint32_t zfs_vdev_removal_max_active = 2;
160 uint32_t zfs_vdev_initializing_min_active = 1;
161 uint32_t zfs_vdev_initializing_max_active = 1;
162
163
164 /*
165 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
166 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
167 * zfs_vdev_async_write_active_max_dirty_percent, use
168 * zfs_vdev_async_write_max_active. The value is linearly interpolated
169 * between min and max.
170 */
171 int zfs_vdev_async_write_active_min_dirty_percent = 30;
172 int zfs_vdev_async_write_active_max_dirty_percent = 60;
173
174 /*
175 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
176 * For read I/Os, we also aggregate across small adjacency gaps; for writes
177 * we include spans of optional I/Os to aid aggregation at the disk even when
178 * they aren't able to help us aggregate at this level.
179 */
180 int zfs_vdev_aggregation_limit = 1 << 20;
181 int zfs_vdev_aggregation_limit_non_rotating = SPA_OLD_MAXBLOCKSIZE;
182 int zfs_vdev_read_gap_limit = 32 << 10;
183 int zfs_vdev_write_gap_limit = 4 << 10;
184
185 /*
186 * Define the queue depth percentage for each top-level. This percentage is
187 * used in conjunction with zfs_vdev_async_max_active to determine how many
188 * allocations a specific top-level vdev should handle. Once the queue depth
189 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
190 * then allocator will stop allocating blocks on that top-level device.
191 * The default kernel setting is 1000% which will yield 100 allocations per
192 * device. For userland testing, the default setting is 300% which equates
193 * to 30 allocations per device.
194 */
195 #ifdef _KERNEL
196 int zfs_vdev_queue_depth_pct = 1000;
197 #else
198 int zfs_vdev_queue_depth_pct = 300;
199 #endif
200
201 /*
202 * When performing allocations for a given metaslab, we want to make sure that
203 * there are enough IOs to aggregate together to improve throughput. We want to
204 * ensure that there are at least 128k worth of IOs that can be aggregated, and
205 * we assume that the average allocation size is 4k, so we need the queue depth
206 * to be 32 per allocator to get good aggregation of sequential writes.
207 */
208 int zfs_vdev_def_queue_depth = 32;
209
210 #ifdef __FreeBSD__
211 #ifdef _KERNEL
212 SYSCTL_DECL(_vfs_zfs_vdev);
213
214 static int sysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS);
215 SYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_min_dirty_percent,
216 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int),
217 sysctl_zfs_async_write_active_min_dirty_percent, "I",
218 "Percentage of async write dirty data below which "
219 "async_write_min_active is used.");
220
221 static int sysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS);
222 SYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_max_dirty_percent,
223 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int),
224 sysctl_zfs_async_write_active_max_dirty_percent, "I",
225 "Percentage of async write dirty data above which "
226 "async_write_max_active is used.");
227
228 SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, max_active, CTLFLAG_RWTUN,
229 &zfs_vdev_max_active, 0,
230 "The maximum number of I/Os of all types active for each device.");
231
232 #define ZFS_VDEV_QUEUE_KNOB_MIN(name) \
233 SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _min_active, CTLFLAG_RWTUN,\
234 &zfs_vdev_ ## name ## _min_active, 0, \
235 "Initial number of I/O requests of type " #name \
236 " active for each device");
237
238 #define ZFS_VDEV_QUEUE_KNOB_MAX(name) \
239 SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _max_active, CTLFLAG_RWTUN,\
240 &zfs_vdev_ ## name ## _max_active, 0, \
241 "Maximum number of I/O requests of type " #name \
242 " active for each device");
243
244 ZFS_VDEV_QUEUE_KNOB_MIN(sync_read);
245 ZFS_VDEV_QUEUE_KNOB_MAX(sync_read);
246 ZFS_VDEV_QUEUE_KNOB_MIN(sync_write);
247 ZFS_VDEV_QUEUE_KNOB_MAX(sync_write);
248 ZFS_VDEV_QUEUE_KNOB_MIN(async_read);
249 ZFS_VDEV_QUEUE_KNOB_MAX(async_read);
250 ZFS_VDEV_QUEUE_KNOB_MIN(async_write);
251 ZFS_VDEV_QUEUE_KNOB_MAX(async_write);
252 ZFS_VDEV_QUEUE_KNOB_MIN(scrub);
253 ZFS_VDEV_QUEUE_KNOB_MAX(scrub);
254 ZFS_VDEV_QUEUE_KNOB_MIN(trim);
255 ZFS_VDEV_QUEUE_KNOB_MAX(trim);
256 ZFS_VDEV_QUEUE_KNOB_MIN(removal);
257 ZFS_VDEV_QUEUE_KNOB_MAX(removal);
258 ZFS_VDEV_QUEUE_KNOB_MIN(initializing);
259 ZFS_VDEV_QUEUE_KNOB_MAX(initializing);
260
261 #undef ZFS_VDEV_QUEUE_KNOB
262
263 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RWTUN,
264 &zfs_vdev_aggregation_limit, 0,
265 "I/O requests are aggregated up to this size");
266 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit_non_rotating, CTLFLAG_RWTUN,
267 &zfs_vdev_aggregation_limit_non_rotating, 0,
268 "I/O requests are aggregated up to this size for non-rotating media");
269 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RWTUN,
270 &zfs_vdev_read_gap_limit, 0,
271 "Acceptable gap between two reads being aggregated");
272 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RWTUN,
273 &zfs_vdev_write_gap_limit, 0,
274 "Acceptable gap between two writes being aggregated");
275 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, queue_depth_pct, CTLFLAG_RWTUN,
276 &zfs_vdev_queue_depth_pct, 0,
277 "Queue depth percentage for each top-level");
278 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, def_queue_depth, CTLFLAG_RWTUN,
279 &zfs_vdev_def_queue_depth, 0,
280 "Default queue depth for each allocator");
281
282 static int
sysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS)283 sysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS)
284 {
285 int val, err;
286
287 val = zfs_vdev_async_write_active_min_dirty_percent;
288 err = sysctl_handle_int(oidp, &val, 0, req);
289 if (err != 0 || req->newptr == NULL)
290 return (err);
291
292 if (val < 0 || val > 100 ||
293 val >= zfs_vdev_async_write_active_max_dirty_percent)
294 return (EINVAL);
295
296 zfs_vdev_async_write_active_min_dirty_percent = val;
297
298 return (0);
299 }
300
301 static int
sysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS)302 sysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS)
303 {
304 int val, err;
305
306 val = zfs_vdev_async_write_active_max_dirty_percent;
307 err = sysctl_handle_int(oidp, &val, 0, req);
308 if (err != 0 || req->newptr == NULL)
309 return (err);
310
311 if (val < 0 || val > 100 ||
312 val <= zfs_vdev_async_write_active_min_dirty_percent)
313 return (EINVAL);
314
315 zfs_vdev_async_write_active_max_dirty_percent = val;
316
317 return (0);
318 }
319 #endif
320 #endif
321
322 int
vdev_queue_offset_compare(const void * x1,const void * x2)323 vdev_queue_offset_compare(const void *x1, const void *x2)
324 {
325 const zio_t *z1 = (const zio_t *)x1;
326 const zio_t *z2 = (const zio_t *)x2;
327
328 int cmp = AVL_CMP(z1->io_offset, z2->io_offset);
329
330 if (likely(cmp))
331 return (cmp);
332
333 return (AVL_PCMP(z1, z2));
334 }
335
336 static inline avl_tree_t *
vdev_queue_class_tree(vdev_queue_t * vq,zio_priority_t p)337 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
338 {
339 return (&vq->vq_class[p].vqc_queued_tree);
340 }
341
342 static inline avl_tree_t *
vdev_queue_type_tree(vdev_queue_t * vq,zio_type_t t)343 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
344 {
345 if (t == ZIO_TYPE_READ)
346 return (&vq->vq_read_offset_tree);
347 else if (t == ZIO_TYPE_WRITE)
348 return (&vq->vq_write_offset_tree);
349 else
350 return (NULL);
351 }
352
353 int
vdev_queue_timestamp_compare(const void * x1,const void * x2)354 vdev_queue_timestamp_compare(const void *x1, const void *x2)
355 {
356 const zio_t *z1 = x1;
357 const zio_t *z2 = x2;
358
359 if (z1->io_timestamp < z2->io_timestamp)
360 return (-1);
361 if (z1->io_timestamp > z2->io_timestamp)
362 return (1);
363
364 if (z1->io_offset < z2->io_offset)
365 return (-1);
366 if (z1->io_offset > z2->io_offset)
367 return (1);
368
369 if (z1 < z2)
370 return (-1);
371 if (z1 > z2)
372 return (1);
373
374 return (0);
375 }
376
377 void
vdev_queue_init(vdev_t * vd)378 vdev_queue_init(vdev_t *vd)
379 {
380 vdev_queue_t *vq = &vd->vdev_queue;
381
382 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
383 vq->vq_vdev = vd;
384
385 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
386 sizeof (zio_t), offsetof(struct zio, io_queue_node));
387 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
388 vdev_queue_offset_compare, sizeof (zio_t),
389 offsetof(struct zio, io_offset_node));
390 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
391 vdev_queue_offset_compare, sizeof (zio_t),
392 offsetof(struct zio, io_offset_node));
393
394 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
395 int (*compfn) (const void *, const void *);
396
397 /*
398 * The synchronous i/o queues are dispatched in FIFO rather
399 * than LBA order. This provides more consistent latency for
400 * these i/os.
401 */
402 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
403 compfn = vdev_queue_timestamp_compare;
404 else
405 compfn = vdev_queue_offset_compare;
406
407 avl_create(vdev_queue_class_tree(vq, p), compfn,
408 sizeof (zio_t), offsetof(struct zio, io_queue_node));
409 }
410
411 vq->vq_lastoffset = 0;
412 }
413
414 void
vdev_queue_fini(vdev_t * vd)415 vdev_queue_fini(vdev_t *vd)
416 {
417 vdev_queue_t *vq = &vd->vdev_queue;
418
419 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
420 avl_destroy(vdev_queue_class_tree(vq, p));
421 avl_destroy(&vq->vq_active_tree);
422 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
423 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
424
425 mutex_destroy(&vq->vq_lock);
426 }
427
428 static void
vdev_queue_io_add(vdev_queue_t * vq,zio_t * zio)429 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
430 {
431 spa_t *spa = zio->io_spa;
432 avl_tree_t *qtt;
433
434 ASSERT(MUTEX_HELD(&vq->vq_lock));
435 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
436 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
437 qtt = vdev_queue_type_tree(vq, zio->io_type);
438 if (qtt)
439 avl_add(qtt, zio);
440
441 #ifdef illumos
442 mutex_enter(&spa->spa_iokstat_lock);
443 spa->spa_queue_stats[zio->io_priority].spa_queued++;
444 if (spa->spa_iokstat != NULL)
445 kstat_waitq_enter(spa->spa_iokstat->ks_data);
446 mutex_exit(&spa->spa_iokstat_lock);
447 #endif
448 }
449
450 static void
vdev_queue_io_remove(vdev_queue_t * vq,zio_t * zio)451 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
452 {
453 spa_t *spa = zio->io_spa;
454 avl_tree_t *qtt;
455
456 ASSERT(MUTEX_HELD(&vq->vq_lock));
457 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
458 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
459 qtt = vdev_queue_type_tree(vq, zio->io_type);
460 if (qtt)
461 avl_remove(qtt, zio);
462
463 #ifdef illumos
464 mutex_enter(&spa->spa_iokstat_lock);
465 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
466 spa->spa_queue_stats[zio->io_priority].spa_queued--;
467 if (spa->spa_iokstat != NULL)
468 kstat_waitq_exit(spa->spa_iokstat->ks_data);
469 mutex_exit(&spa->spa_iokstat_lock);
470 #endif
471 }
472
473 static void
vdev_queue_pending_add(vdev_queue_t * vq,zio_t * zio)474 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
475 {
476 spa_t *spa = zio->io_spa;
477 ASSERT(MUTEX_HELD(&vq->vq_lock));
478 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
479 vq->vq_class[zio->io_priority].vqc_active++;
480 avl_add(&vq->vq_active_tree, zio);
481
482 #ifdef illumos
483 mutex_enter(&spa->spa_iokstat_lock);
484 spa->spa_queue_stats[zio->io_priority].spa_active++;
485 if (spa->spa_iokstat != NULL)
486 kstat_runq_enter(spa->spa_iokstat->ks_data);
487 mutex_exit(&spa->spa_iokstat_lock);
488 #endif
489 }
490
491 static void
vdev_queue_pending_remove(vdev_queue_t * vq,zio_t * zio)492 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
493 {
494 spa_t *spa = zio->io_spa;
495 ASSERT(MUTEX_HELD(&vq->vq_lock));
496 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
497 vq->vq_class[zio->io_priority].vqc_active--;
498 avl_remove(&vq->vq_active_tree, zio);
499
500 #ifdef illumos
501 mutex_enter(&spa->spa_iokstat_lock);
502 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
503 spa->spa_queue_stats[zio->io_priority].spa_active--;
504 if (spa->spa_iokstat != NULL) {
505 kstat_io_t *ksio = spa->spa_iokstat->ks_data;
506
507 kstat_runq_exit(spa->spa_iokstat->ks_data);
508 if (zio->io_type == ZIO_TYPE_READ) {
509 ksio->reads++;
510 ksio->nread += zio->io_size;
511 } else if (zio->io_type == ZIO_TYPE_WRITE) {
512 ksio->writes++;
513 ksio->nwritten += zio->io_size;
514 }
515 }
516 mutex_exit(&spa->spa_iokstat_lock);
517 #endif
518 }
519
520 static void
vdev_queue_agg_io_done(zio_t * aio)521 vdev_queue_agg_io_done(zio_t *aio)
522 {
523 if (aio->io_type == ZIO_TYPE_READ) {
524 zio_t *pio;
525 zio_link_t *zl = NULL;
526 while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
527 abd_copy_off(pio->io_abd, aio->io_abd,
528 0, pio->io_offset - aio->io_offset, pio->io_size);
529 }
530 }
531
532 abd_free(aio->io_abd);
533 }
534
535 static int
vdev_queue_class_min_active(zio_priority_t p)536 vdev_queue_class_min_active(zio_priority_t p)
537 {
538 switch (p) {
539 case ZIO_PRIORITY_SYNC_READ:
540 return (zfs_vdev_sync_read_min_active);
541 case ZIO_PRIORITY_SYNC_WRITE:
542 return (zfs_vdev_sync_write_min_active);
543 case ZIO_PRIORITY_ASYNC_READ:
544 return (zfs_vdev_async_read_min_active);
545 case ZIO_PRIORITY_ASYNC_WRITE:
546 return (zfs_vdev_async_write_min_active);
547 case ZIO_PRIORITY_SCRUB:
548 return (zfs_vdev_scrub_min_active);
549 case ZIO_PRIORITY_TRIM:
550 return (zfs_vdev_trim_min_active);
551 case ZIO_PRIORITY_REMOVAL:
552 return (zfs_vdev_removal_min_active);
553 case ZIO_PRIORITY_INITIALIZING:
554 return (zfs_vdev_initializing_min_active);
555 default:
556 panic("invalid priority %u", p);
557 return (0);
558 }
559 }
560
561 static __noinline int
vdev_queue_max_async_writes(spa_t * spa)562 vdev_queue_max_async_writes(spa_t *spa)
563 {
564 int writes;
565 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
566 uint64_t min_bytes = zfs_dirty_data_max *
567 zfs_vdev_async_write_active_min_dirty_percent / 100;
568 uint64_t max_bytes = zfs_dirty_data_max *
569 zfs_vdev_async_write_active_max_dirty_percent / 100;
570
571 /*
572 * Sync tasks correspond to interactive user actions. To reduce the
573 * execution time of those actions we push data out as fast as possible.
574 */
575 if (spa_has_pending_synctask(spa)) {
576 return (zfs_vdev_async_write_max_active);
577 }
578
579 if (dirty < min_bytes)
580 return (zfs_vdev_async_write_min_active);
581 if (dirty > max_bytes)
582 return (zfs_vdev_async_write_max_active);
583
584 /*
585 * linear interpolation:
586 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
587 * move right by min_bytes
588 * move up by min_writes
589 */
590 writes = (dirty - min_bytes) *
591 (zfs_vdev_async_write_max_active -
592 zfs_vdev_async_write_min_active) /
593 (max_bytes - min_bytes) +
594 zfs_vdev_async_write_min_active;
595 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
596 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
597 return (writes);
598 }
599
600 static int
vdev_queue_class_max_active(spa_t * spa,zio_priority_t p)601 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
602 {
603 switch (p) {
604 case ZIO_PRIORITY_SYNC_READ:
605 return (zfs_vdev_sync_read_max_active);
606 case ZIO_PRIORITY_SYNC_WRITE:
607 return (zfs_vdev_sync_write_max_active);
608 case ZIO_PRIORITY_ASYNC_READ:
609 return (zfs_vdev_async_read_max_active);
610 case ZIO_PRIORITY_ASYNC_WRITE:
611 return (vdev_queue_max_async_writes(spa));
612 case ZIO_PRIORITY_SCRUB:
613 return (zfs_vdev_scrub_max_active);
614 case ZIO_PRIORITY_TRIM:
615 return (zfs_vdev_trim_max_active);
616 case ZIO_PRIORITY_REMOVAL:
617 return (zfs_vdev_removal_max_active);
618 case ZIO_PRIORITY_INITIALIZING:
619 return (zfs_vdev_initializing_max_active);
620 default:
621 panic("invalid priority %u", p);
622 return (0);
623 }
624 }
625
626 /*
627 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
628 * there is no eligible class.
629 */
630 static zio_priority_t
vdev_queue_class_to_issue(vdev_queue_t * vq)631 vdev_queue_class_to_issue(vdev_queue_t *vq)
632 {
633 spa_t *spa = vq->vq_vdev->vdev_spa;
634 zio_priority_t p;
635
636 ASSERT(MUTEX_HELD(&vq->vq_lock));
637
638 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
639 return (ZIO_PRIORITY_NUM_QUEUEABLE);
640
641 /* find a queue that has not reached its minimum # outstanding i/os */
642 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
643 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
644 vq->vq_class[p].vqc_active <
645 vdev_queue_class_min_active(p))
646 return (p);
647 }
648
649 /*
650 * If we haven't found a queue, look for one that hasn't reached its
651 * maximum # outstanding i/os.
652 */
653 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
654 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
655 vq->vq_class[p].vqc_active <
656 vdev_queue_class_max_active(spa, p))
657 return (p);
658 }
659
660 /* No eligible queued i/os */
661 return (ZIO_PRIORITY_NUM_QUEUEABLE);
662 }
663
664 /*
665 * Compute the range spanned by two i/os, which is the endpoint of the last
666 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
667 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
668 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
669 */
670 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
671 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
672
673 static zio_t *
vdev_queue_aggregate(vdev_queue_t * vq,zio_t * zio)674 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
675 {
676 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
677 zio_link_t *zl = NULL;
678 uint64_t maxgap = 0;
679 uint64_t size;
680 uint64_t limit;
681 int maxblocksize;
682 boolean_t stretch;
683 avl_tree_t *t;
684 enum zio_flag flags;
685
686 ASSERT(MUTEX_HELD(&vq->vq_lock));
687
688 maxblocksize = spa_maxblocksize(vq->vq_vdev->vdev_spa);
689 if (vq->vq_vdev->vdev_nonrot)
690 limit = zfs_vdev_aggregation_limit_non_rotating;
691 else
692 limit = zfs_vdev_aggregation_limit;
693 limit = MAX(MIN(limit, maxblocksize), 0);
694
695 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE || limit == 0)
696 return (NULL);
697
698 first = last = zio;
699
700 if (zio->io_type == ZIO_TYPE_READ)
701 maxgap = zfs_vdev_read_gap_limit;
702
703 /*
704 * We can aggregate I/Os that are sufficiently adjacent and of
705 * the same flavor, as expressed by the AGG_INHERIT flags.
706 * The latter requirement is necessary so that certain
707 * attributes of the I/O, such as whether it's a normal I/O
708 * or a scrub/resilver, can be preserved in the aggregate.
709 * We can include optional I/Os, but don't allow them
710 * to begin a range as they add no benefit in that situation.
711 */
712
713 /*
714 * We keep track of the last non-optional I/O.
715 */
716 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
717
718 /*
719 * Walk backwards through sufficiently contiguous I/Os
720 * recording the last non-optional I/O.
721 */
722 flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
723 t = vdev_queue_type_tree(vq, zio->io_type);
724 while (t != NULL && (dio = AVL_PREV(t, first)) != NULL &&
725 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
726 IO_SPAN(dio, last) <= limit &&
727 IO_GAP(dio, first) <= maxgap &&
728 dio->io_type == zio->io_type) {
729 first = dio;
730 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
731 mandatory = first;
732 }
733
734 /*
735 * Skip any initial optional I/Os.
736 */
737 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
738 first = AVL_NEXT(t, first);
739 ASSERT(first != NULL);
740 }
741
742 /*
743 * Walk forward through sufficiently contiguous I/Os.
744 * The aggregation limit does not apply to optional i/os, so that
745 * we can issue contiguous writes even if they are larger than the
746 * aggregation limit.
747 */
748 while ((dio = AVL_NEXT(t, last)) != NULL &&
749 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
750 (IO_SPAN(first, dio) <= limit ||
751 (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
752 IO_SPAN(first, dio) <= maxblocksize &&
753 IO_GAP(last, dio) <= maxgap &&
754 dio->io_type == zio->io_type) {
755 last = dio;
756 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
757 mandatory = last;
758 }
759
760 /*
761 * Now that we've established the range of the I/O aggregation
762 * we must decide what to do with trailing optional I/Os.
763 * For reads, there's nothing to do. While we are unable to
764 * aggregate further, it's possible that a trailing optional
765 * I/O would allow the underlying device to aggregate with
766 * subsequent I/Os. We must therefore determine if the next
767 * non-optional I/O is close enough to make aggregation
768 * worthwhile.
769 */
770 stretch = B_FALSE;
771 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
772 zio_t *nio = last;
773 while ((dio = AVL_NEXT(t, nio)) != NULL &&
774 IO_GAP(nio, dio) == 0 &&
775 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
776 nio = dio;
777 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
778 stretch = B_TRUE;
779 break;
780 }
781 }
782 }
783
784 if (stretch) {
785 /*
786 * We are going to include an optional io in our aggregated
787 * span, thus closing the write gap. Only mandatory i/os can
788 * start aggregated spans, so make sure that the next i/o
789 * after our span is mandatory.
790 */
791 dio = AVL_NEXT(t, last);
792 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
793 } else {
794 /* do not include the optional i/o */
795 while (last != mandatory && last != first) {
796 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
797 last = AVL_PREV(t, last);
798 ASSERT(last != NULL);
799 }
800 }
801
802 if (first == last)
803 return (NULL);
804
805 size = IO_SPAN(first, last);
806 ASSERT3U(size, <=, maxblocksize);
807
808 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
809 abd_alloc_for_io(size, B_TRUE), size, first->io_type,
810 zio->io_priority, flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
811 vdev_queue_agg_io_done, NULL);
812 aio->io_timestamp = first->io_timestamp;
813
814 nio = first;
815 do {
816 dio = nio;
817 nio = AVL_NEXT(t, dio);
818 zio_add_child(dio, aio);
819 vdev_queue_io_remove(vq, dio);
820 } while (dio != last);
821
822 /*
823 * We need to drop the vdev queue's lock during zio_execute() to
824 * avoid a deadlock that we could encounter due to lock order
825 * reversal between vq_lock and io_lock in zio_change_priority().
826 * Use the dropped lock to do memory copy without congestion.
827 */
828 mutex_exit(&vq->vq_lock);
829 while ((dio = zio_walk_parents(aio, &zl)) != NULL) {
830 ASSERT3U(dio->io_type, ==, aio->io_type);
831
832 if (dio->io_flags & ZIO_FLAG_NODATA) {
833 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
834 abd_zero_off(aio->io_abd,
835 dio->io_offset - aio->io_offset, dio->io_size);
836 } else if (dio->io_type == ZIO_TYPE_WRITE) {
837 abd_copy_off(aio->io_abd, dio->io_abd,
838 dio->io_offset - aio->io_offset, 0, dio->io_size);
839 }
840
841 zio_vdev_io_bypass(dio);
842 zio_execute(dio);
843 }
844 mutex_enter(&vq->vq_lock);
845
846 return (aio);
847 }
848
849 static zio_t *
vdev_queue_io_to_issue(vdev_queue_t * vq)850 vdev_queue_io_to_issue(vdev_queue_t *vq)
851 {
852 zio_t *zio, *aio;
853 zio_priority_t p;
854 avl_index_t idx;
855 avl_tree_t *tree;
856 zio_t search;
857
858 again:
859 ASSERT(MUTEX_HELD(&vq->vq_lock));
860
861 p = vdev_queue_class_to_issue(vq);
862
863 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
864 /* No eligible queued i/os */
865 return (NULL);
866 }
867
868 /*
869 * For LBA-ordered queues (async / scrub / initializing), issue the
870 * i/o which follows the most recently issued i/o in LBA (offset) order.
871 *
872 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
873 */
874 tree = vdev_queue_class_tree(vq, p);
875 search.io_timestamp = 0;
876 search.io_offset = vq->vq_last_offset + 1;
877 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
878 zio = avl_nearest(tree, idx, AVL_AFTER);
879 if (zio == NULL)
880 zio = avl_first(tree);
881 ASSERT3U(zio->io_priority, ==, p);
882
883 aio = vdev_queue_aggregate(vq, zio);
884 if (aio != NULL)
885 zio = aio;
886 else
887 vdev_queue_io_remove(vq, zio);
888
889 /*
890 * If the I/O is or was optional and therefore has no data, we need to
891 * simply discard it. We need to drop the vdev queue's lock to avoid a
892 * deadlock that we could encounter since this I/O will complete
893 * immediately.
894 */
895 if (zio->io_flags & ZIO_FLAG_NODATA) {
896 mutex_exit(&vq->vq_lock);
897 zio_vdev_io_bypass(zio);
898 zio_execute(zio);
899 mutex_enter(&vq->vq_lock);
900 goto again;
901 }
902
903 vdev_queue_pending_add(vq, zio);
904 vq->vq_last_offset = zio->io_offset;
905
906 return (zio);
907 }
908
909 zio_t *
vdev_queue_io(zio_t * zio)910 vdev_queue_io(zio_t *zio)
911 {
912 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
913 zio_t *nio;
914
915 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
916 return (zio);
917
918 /*
919 * Children i/os inherent their parent's priority, which might
920 * not match the child's i/o type. Fix it up here.
921 */
922 if (zio->io_type == ZIO_TYPE_READ) {
923 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
924 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
925 zio->io_priority != ZIO_PRIORITY_SCRUB &&
926 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
927 zio->io_priority != ZIO_PRIORITY_INITIALIZING)
928 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
929 } else if (zio->io_type == ZIO_TYPE_WRITE) {
930 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
931 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
932 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
933 zio->io_priority != ZIO_PRIORITY_INITIALIZING)
934 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
935 } else {
936 ASSERT(zio->io_type == ZIO_TYPE_FREE);
937 zio->io_priority = ZIO_PRIORITY_TRIM;
938 }
939
940 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
941
942 mutex_enter(&vq->vq_lock);
943 zio->io_timestamp = gethrtime();
944 vdev_queue_io_add(vq, zio);
945 nio = vdev_queue_io_to_issue(vq);
946 mutex_exit(&vq->vq_lock);
947
948 if (nio == NULL)
949 return (NULL);
950
951 if (nio->io_done == vdev_queue_agg_io_done) {
952 zio_nowait(nio);
953 return (NULL);
954 }
955
956 return (nio);
957 }
958
959 void
vdev_queue_io_done(zio_t * zio)960 vdev_queue_io_done(zio_t *zio)
961 {
962 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
963 zio_t *nio;
964
965 mutex_enter(&vq->vq_lock);
966
967 vdev_queue_pending_remove(vq, zio);
968
969 vq->vq_io_complete_ts = gethrtime();
970
971 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
972 mutex_exit(&vq->vq_lock);
973 if (nio->io_done == vdev_queue_agg_io_done) {
974 zio_nowait(nio);
975 } else {
976 zio_vdev_io_reissue(nio);
977 zio_execute(nio);
978 }
979 mutex_enter(&vq->vq_lock);
980 }
981
982 mutex_exit(&vq->vq_lock);
983 }
984
985 void
vdev_queue_change_io_priority(zio_t * zio,zio_priority_t priority)986 vdev_queue_change_io_priority(zio_t *zio, zio_priority_t priority)
987 {
988 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
989 avl_tree_t *tree;
990
991 /*
992 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
993 * code to issue IOs without adding them to the vdev queue. In this
994 * case, the zio is already going to be issued as quickly as possible
995 * and so it doesn't need any reprioitization to help.
996 */
997 if (zio->io_priority == ZIO_PRIORITY_NOW)
998 return;
999
1000 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
1001 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
1002
1003 if (zio->io_type == ZIO_TYPE_READ) {
1004 if (priority != ZIO_PRIORITY_SYNC_READ &&
1005 priority != ZIO_PRIORITY_ASYNC_READ &&
1006 priority != ZIO_PRIORITY_SCRUB)
1007 priority = ZIO_PRIORITY_ASYNC_READ;
1008 } else {
1009 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1010 if (priority != ZIO_PRIORITY_SYNC_WRITE &&
1011 priority != ZIO_PRIORITY_ASYNC_WRITE)
1012 priority = ZIO_PRIORITY_ASYNC_WRITE;
1013 }
1014
1015 mutex_enter(&vq->vq_lock);
1016
1017 /*
1018 * If the zio is in none of the queues we can simply change
1019 * the priority. If the zio is waiting to be submitted we must
1020 * remove it from the queue and re-insert it with the new priority.
1021 * Otherwise, the zio is currently active and we cannot change its
1022 * priority.
1023 */
1024 tree = vdev_queue_class_tree(vq, zio->io_priority);
1025 if (avl_find(tree, zio, NULL) == zio) {
1026 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
1027 zio->io_priority = priority;
1028 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
1029 } else if (avl_find(&vq->vq_active_tree, zio, NULL) != zio) {
1030 zio->io_priority = priority;
1031 }
1032
1033 mutex_exit(&vq->vq_lock);
1034 }
1035
1036 /*
1037 * As these three methods are only used for load calculations we're not concerned
1038 * if we get an incorrect value on 32bit platforms due to lack of vq_lock mutex
1039 * use here, instead we prefer to keep it lock free for performance.
1040 */
1041 int
vdev_queue_length(vdev_t * vd)1042 vdev_queue_length(vdev_t *vd)
1043 {
1044 return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
1045 }
1046
1047 uint64_t
vdev_queue_lastoffset(vdev_t * vd)1048 vdev_queue_lastoffset(vdev_t *vd)
1049 {
1050 return (vd->vdev_queue.vq_lastoffset);
1051 }
1052
1053 void
vdev_queue_register_lastoffset(vdev_t * vd,zio_t * zio)1054 vdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio)
1055 {
1056 vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size;
1057 }
1058