vdev_queue.c revision 321610
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, 2017 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 */ 123uint32_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 */ 141uint32_t zfs_vdev_sync_read_min_active = 10; 142uint32_t zfs_vdev_sync_read_max_active = 10; 143uint32_t zfs_vdev_sync_write_min_active = 10; 144uint32_t zfs_vdev_sync_write_max_active = 10; 145uint32_t zfs_vdev_async_read_min_active = 1; 146uint32_t zfs_vdev_async_read_max_active = 3; 147uint32_t zfs_vdev_async_write_min_active = 1; 148uint32_t zfs_vdev_async_write_max_active = 10; 149uint32_t zfs_vdev_scrub_min_active = 1; 150uint32_t zfs_vdev_scrub_max_active = 2; 151uint32_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 */ 157uint32_t zfs_vdev_trim_max_active = 64; 158 159 160/* 161 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent 162 * dirty data, use zfs_vdev_async_write_min_active. When it has more than 163 * zfs_vdev_async_write_active_max_dirty_percent, use 164 * zfs_vdev_async_write_max_active. The value is linearly interpolated 165 * between min and max. 166 */ 167int zfs_vdev_async_write_active_min_dirty_percent = 30; 168int zfs_vdev_async_write_active_max_dirty_percent = 60; 169 170/* 171 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O. 172 * For read I/Os, we also aggregate across small adjacency gaps; for writes 173 * we include spans of optional I/Os to aid aggregation at the disk even when 174 * they aren't able to help us aggregate at this level. 175 */ 176int zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE; 177int zfs_vdev_read_gap_limit = 32 << 10; 178int zfs_vdev_write_gap_limit = 4 << 10; 179 180/* 181 * Define the queue depth percentage for each top-level. This percentage is 182 * used in conjunction with zfs_vdev_async_max_active to determine how many 183 * allocations a specific top-level vdev should handle. Once the queue depth 184 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100 185 * then allocator will stop allocating blocks on that top-level device. 186 * The default kernel setting is 1000% which will yield 100 allocations per 187 * device. For userland testing, the default setting is 300% which equates 188 * to 30 allocations per device. 189 */ 190#ifdef _KERNEL 191int zfs_vdev_queue_depth_pct = 1000; 192#else 193int zfs_vdev_queue_depth_pct = 300; 194#endif 195 196 197#ifdef __FreeBSD__ 198#ifdef _KERNEL 199SYSCTL_DECL(_vfs_zfs_vdev); 200 201static int sysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS); 202SYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_min_dirty_percent, 203 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int), 204 sysctl_zfs_async_write_active_min_dirty_percent, "I", 205 "Percentage of async write dirty data below which " 206 "async_write_min_active is used."); 207 208static int sysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS); 209SYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_max_dirty_percent, 210 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int), 211 sysctl_zfs_async_write_active_max_dirty_percent, "I", 212 "Percentage of async write dirty data above which " 213 "async_write_max_active is used."); 214 215SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, max_active, CTLFLAG_RWTUN, 216 &zfs_vdev_max_active, 0, 217 "The maximum number of I/Os of all types active for each device."); 218 219#define ZFS_VDEV_QUEUE_KNOB_MIN(name) \ 220SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _min_active, CTLFLAG_RWTUN,\ 221 &zfs_vdev_ ## name ## _min_active, 0, \ 222 "Initial number of I/O requests of type " #name \ 223 " active for each device"); 224 225#define ZFS_VDEV_QUEUE_KNOB_MAX(name) \ 226SYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _max_active, CTLFLAG_RWTUN,\ 227 &zfs_vdev_ ## name ## _max_active, 0, \ 228 "Maximum number of I/O requests of type " #name \ 229 " active for each device"); 230 231ZFS_VDEV_QUEUE_KNOB_MIN(sync_read); 232ZFS_VDEV_QUEUE_KNOB_MAX(sync_read); 233ZFS_VDEV_QUEUE_KNOB_MIN(sync_write); 234ZFS_VDEV_QUEUE_KNOB_MAX(sync_write); 235ZFS_VDEV_QUEUE_KNOB_MIN(async_read); 236ZFS_VDEV_QUEUE_KNOB_MAX(async_read); 237ZFS_VDEV_QUEUE_KNOB_MIN(async_write); 238ZFS_VDEV_QUEUE_KNOB_MAX(async_write); 239ZFS_VDEV_QUEUE_KNOB_MIN(scrub); 240ZFS_VDEV_QUEUE_KNOB_MAX(scrub); 241ZFS_VDEV_QUEUE_KNOB_MIN(trim); 242ZFS_VDEV_QUEUE_KNOB_MAX(trim); 243 244#undef ZFS_VDEV_QUEUE_KNOB 245 246SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RWTUN, 247 &zfs_vdev_aggregation_limit, 0, 248 "I/O requests are aggregated up to this size"); 249SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RWTUN, 250 &zfs_vdev_read_gap_limit, 0, 251 "Acceptable gap between two reads being aggregated"); 252SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RWTUN, 253 &zfs_vdev_write_gap_limit, 0, 254 "Acceptable gap between two writes being aggregated"); 255SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, queue_depth_pct, CTLFLAG_RWTUN, 256 &zfs_vdev_queue_depth_pct, 0, 257 "Queue depth percentage for each top-level"); 258 259static int 260sysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS) 261{ 262 int val, err; 263 264 val = zfs_vdev_async_write_active_min_dirty_percent; 265 err = sysctl_handle_int(oidp, &val, 0, req); 266 if (err != 0 || req->newptr == NULL) 267 return (err); 268 269 if (val < 0 || val > 100 || 270 val >= zfs_vdev_async_write_active_max_dirty_percent) 271 return (EINVAL); 272 273 zfs_vdev_async_write_active_min_dirty_percent = val; 274 275 return (0); 276} 277 278static int 279sysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS) 280{ 281 int val, err; 282 283 val = zfs_vdev_async_write_active_max_dirty_percent; 284 err = sysctl_handle_int(oidp, &val, 0, req); 285 if (err != 0 || req->newptr == NULL) 286 return (err); 287 288 if (val < 0 || val > 100 || 289 val <= zfs_vdev_async_write_active_min_dirty_percent) 290 return (EINVAL); 291 292 zfs_vdev_async_write_active_max_dirty_percent = val; 293 294 return (0); 295} 296#endif 297#endif 298 299int 300vdev_queue_offset_compare(const void *x1, const void *x2) 301{ 302 const zio_t *z1 = x1; 303 const zio_t *z2 = x2; 304 305 if (z1->io_offset < z2->io_offset) 306 return (-1); 307 if (z1->io_offset > z2->io_offset) 308 return (1); 309 310 if (z1 < z2) 311 return (-1); 312 if (z1 > z2) 313 return (1); 314 315 return (0); 316} 317 318static inline avl_tree_t * 319vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p) 320{ 321 return (&vq->vq_class[p].vqc_queued_tree); 322} 323 324static inline avl_tree_t * 325vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t) 326{ 327 if (t == ZIO_TYPE_READ) 328 return (&vq->vq_read_offset_tree); 329 else if (t == ZIO_TYPE_WRITE) 330 return (&vq->vq_write_offset_tree); 331 else 332 return (NULL); 333} 334 335int 336vdev_queue_timestamp_compare(const void *x1, const void *x2) 337{ 338 const zio_t *z1 = x1; 339 const zio_t *z2 = x2; 340 341 if (z1->io_timestamp < z2->io_timestamp) 342 return (-1); 343 if (z1->io_timestamp > z2->io_timestamp) 344 return (1); 345 346 if (z1->io_offset < z2->io_offset) 347 return (-1); 348 if (z1->io_offset > z2->io_offset) 349 return (1); 350 351 if (z1 < z2) 352 return (-1); 353 if (z1 > z2) 354 return (1); 355 356 return (0); 357} 358 359void 360vdev_queue_init(vdev_t *vd) 361{ 362 vdev_queue_t *vq = &vd->vdev_queue; 363 364 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL); 365 vq->vq_vdev = vd; 366 367 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare, 368 sizeof (zio_t), offsetof(struct zio, io_queue_node)); 369 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ), 370 vdev_queue_offset_compare, sizeof (zio_t), 371 offsetof(struct zio, io_offset_node)); 372 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE), 373 vdev_queue_offset_compare, sizeof (zio_t), 374 offsetof(struct zio, io_offset_node)); 375 376 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 377 int (*compfn) (const void *, const void *); 378 379 /* 380 * The synchronous i/o queues are dispatched in FIFO rather 381 * than LBA order. This provides more consistent latency for 382 * these i/os. 383 */ 384 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE) 385 compfn = vdev_queue_timestamp_compare; 386 else 387 compfn = vdev_queue_offset_compare; 388 389 avl_create(vdev_queue_class_tree(vq, p), compfn, 390 sizeof (zio_t), offsetof(struct zio, io_queue_node)); 391 } 392 393 vq->vq_lastoffset = 0; 394} 395 396void 397vdev_queue_fini(vdev_t *vd) 398{ 399 vdev_queue_t *vq = &vd->vdev_queue; 400 401 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) 402 avl_destroy(vdev_queue_class_tree(vq, p)); 403 avl_destroy(&vq->vq_active_tree); 404 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ)); 405 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE)); 406 407 mutex_destroy(&vq->vq_lock); 408} 409 410static void 411vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio) 412{ 413 spa_t *spa = zio->io_spa; 414 avl_tree_t *qtt; 415 416 ASSERT(MUTEX_HELD(&vq->vq_lock)); 417 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 418 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio); 419 qtt = vdev_queue_type_tree(vq, zio->io_type); 420 if (qtt) 421 avl_add(qtt, zio); 422 423#ifdef illumos 424 mutex_enter(&spa->spa_iokstat_lock); 425 spa->spa_queue_stats[zio->io_priority].spa_queued++; 426 if (spa->spa_iokstat != NULL) 427 kstat_waitq_enter(spa->spa_iokstat->ks_data); 428 mutex_exit(&spa->spa_iokstat_lock); 429#endif 430} 431 432static void 433vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio) 434{ 435 spa_t *spa = zio->io_spa; 436 avl_tree_t *qtt; 437 438 ASSERT(MUTEX_HELD(&vq->vq_lock)); 439 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 440 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio); 441 qtt = vdev_queue_type_tree(vq, zio->io_type); 442 if (qtt) 443 avl_remove(qtt, zio); 444 445#ifdef illumos 446 mutex_enter(&spa->spa_iokstat_lock); 447 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0); 448 spa->spa_queue_stats[zio->io_priority].spa_queued--; 449 if (spa->spa_iokstat != NULL) 450 kstat_waitq_exit(spa->spa_iokstat->ks_data); 451 mutex_exit(&spa->spa_iokstat_lock); 452#endif 453} 454 455static void 456vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio) 457{ 458 spa_t *spa = zio->io_spa; 459 ASSERT(MUTEX_HELD(&vq->vq_lock)); 460 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 461 vq->vq_class[zio->io_priority].vqc_active++; 462 avl_add(&vq->vq_active_tree, zio); 463 464#ifdef illumos 465 mutex_enter(&spa->spa_iokstat_lock); 466 spa->spa_queue_stats[zio->io_priority].spa_active++; 467 if (spa->spa_iokstat != NULL) 468 kstat_runq_enter(spa->spa_iokstat->ks_data); 469 mutex_exit(&spa->spa_iokstat_lock); 470#endif 471} 472 473static void 474vdev_queue_pending_remove(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_remove(&vq->vq_active_tree, zio); 481 482#ifdef illumos 483 mutex_enter(&spa->spa_iokstat_lock); 484 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0); 485 spa->spa_queue_stats[zio->io_priority].spa_active--; 486 if (spa->spa_iokstat != NULL) { 487 kstat_io_t *ksio = spa->spa_iokstat->ks_data; 488 489 kstat_runq_exit(spa->spa_iokstat->ks_data); 490 if (zio->io_type == ZIO_TYPE_READ) { 491 ksio->reads++; 492 ksio->nread += zio->io_size; 493 } else if (zio->io_type == ZIO_TYPE_WRITE) { 494 ksio->writes++; 495 ksio->nwritten += zio->io_size; 496 } 497 } 498 mutex_exit(&spa->spa_iokstat_lock); 499#endif 500} 501 502static void 503vdev_queue_agg_io_done(zio_t *aio) 504{ 505 if (aio->io_type == ZIO_TYPE_READ) { 506 zio_t *pio; 507 zio_link_t *zl = NULL; 508 while ((pio = zio_walk_parents(aio, &zl)) != NULL) { 509 abd_copy_off(pio->io_abd, aio->io_abd, 510 0, pio->io_offset - aio->io_offset, pio->io_size); 511 } 512 } 513 514 abd_free(aio->io_abd); 515} 516 517static int 518vdev_queue_class_min_active(zio_priority_t p) 519{ 520 switch (p) { 521 case ZIO_PRIORITY_SYNC_READ: 522 return (zfs_vdev_sync_read_min_active); 523 case ZIO_PRIORITY_SYNC_WRITE: 524 return (zfs_vdev_sync_write_min_active); 525 case ZIO_PRIORITY_ASYNC_READ: 526 return (zfs_vdev_async_read_min_active); 527 case ZIO_PRIORITY_ASYNC_WRITE: 528 return (zfs_vdev_async_write_min_active); 529 case ZIO_PRIORITY_SCRUB: 530 return (zfs_vdev_scrub_min_active); 531 case ZIO_PRIORITY_TRIM: 532 return (zfs_vdev_trim_min_active); 533 default: 534 panic("invalid priority %u", p); 535 return (0); 536 } 537} 538 539static __noinline int 540vdev_queue_max_async_writes(spa_t *spa) 541{ 542 int writes; 543 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total; 544 uint64_t min_bytes = zfs_dirty_data_max * 545 zfs_vdev_async_write_active_min_dirty_percent / 100; 546 uint64_t max_bytes = zfs_dirty_data_max * 547 zfs_vdev_async_write_active_max_dirty_percent / 100; 548 549 /* 550 * Sync tasks correspond to interactive user actions. To reduce the 551 * execution time of those actions we push data out as fast as possible. 552 */ 553 if (spa_has_pending_synctask(spa)) { 554 return (zfs_vdev_async_write_max_active); 555 } 556 557 if (dirty < min_bytes) 558 return (zfs_vdev_async_write_min_active); 559 if (dirty > max_bytes) 560 return (zfs_vdev_async_write_max_active); 561 562 /* 563 * linear interpolation: 564 * slope = (max_writes - min_writes) / (max_bytes - min_bytes) 565 * move right by min_bytes 566 * move up by min_writes 567 */ 568 writes = (dirty - min_bytes) * 569 (zfs_vdev_async_write_max_active - 570 zfs_vdev_async_write_min_active) / 571 (max_bytes - min_bytes) + 572 zfs_vdev_async_write_min_active; 573 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active); 574 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active); 575 return (writes); 576} 577 578static int 579vdev_queue_class_max_active(spa_t *spa, zio_priority_t p) 580{ 581 switch (p) { 582 case ZIO_PRIORITY_SYNC_READ: 583 return (zfs_vdev_sync_read_max_active); 584 case ZIO_PRIORITY_SYNC_WRITE: 585 return (zfs_vdev_sync_write_max_active); 586 case ZIO_PRIORITY_ASYNC_READ: 587 return (zfs_vdev_async_read_max_active); 588 case ZIO_PRIORITY_ASYNC_WRITE: 589 return (vdev_queue_max_async_writes(spa)); 590 case ZIO_PRIORITY_SCRUB: 591 return (zfs_vdev_scrub_max_active); 592 case ZIO_PRIORITY_TRIM: 593 return (zfs_vdev_trim_max_active); 594 default: 595 panic("invalid priority %u", p); 596 return (0); 597 } 598} 599 600/* 601 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if 602 * there is no eligible class. 603 */ 604static zio_priority_t 605vdev_queue_class_to_issue(vdev_queue_t *vq) 606{ 607 spa_t *spa = vq->vq_vdev->vdev_spa; 608 zio_priority_t p; 609 610 ASSERT(MUTEX_HELD(&vq->vq_lock)); 611 612 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active) 613 return (ZIO_PRIORITY_NUM_QUEUEABLE); 614 615 /* find a queue that has not reached its minimum # outstanding i/os */ 616 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 617 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 && 618 vq->vq_class[p].vqc_active < 619 vdev_queue_class_min_active(p)) 620 return (p); 621 } 622 623 /* 624 * If we haven't found a queue, look for one that hasn't reached its 625 * maximum # outstanding i/os. 626 */ 627 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 628 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 && 629 vq->vq_class[p].vqc_active < 630 vdev_queue_class_max_active(spa, p)) 631 return (p); 632 } 633 634 /* No eligible queued i/os */ 635 return (ZIO_PRIORITY_NUM_QUEUEABLE); 636} 637 638/* 639 * Compute the range spanned by two i/os, which is the endpoint of the last 640 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset). 641 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio); 642 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0. 643 */ 644#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset) 645#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio)) 646 647static zio_t * 648vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio) 649{ 650 zio_t *first, *last, *aio, *dio, *mandatory, *nio; 651 uint64_t maxgap = 0; 652 uint64_t size; 653 boolean_t stretch; 654 avl_tree_t *t; 655 enum zio_flag flags; 656 657 ASSERT(MUTEX_HELD(&vq->vq_lock)); 658 659 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE) 660 return (NULL); 661 662 first = last = zio; 663 664 if (zio->io_type == ZIO_TYPE_READ) 665 maxgap = zfs_vdev_read_gap_limit; 666 667 /* 668 * We can aggregate I/Os that are sufficiently adjacent and of 669 * the same flavor, as expressed by the AGG_INHERIT flags. 670 * The latter requirement is necessary so that certain 671 * attributes of the I/O, such as whether it's a normal I/O 672 * or a scrub/resilver, can be preserved in the aggregate. 673 * We can include optional I/Os, but don't allow them 674 * to begin a range as they add no benefit in that situation. 675 */ 676 677 /* 678 * We keep track of the last non-optional I/O. 679 */ 680 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first; 681 682 /* 683 * Walk backwards through sufficiently contiguous I/Os 684 * recording the last non-optional I/O. 685 */ 686 flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT; 687 t = vdev_queue_type_tree(vq, zio->io_type); 688 while (t != NULL && (dio = AVL_PREV(t, first)) != NULL && 689 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 690 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit && 691 IO_GAP(dio, first) <= maxgap) { 692 first = dio; 693 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL)) 694 mandatory = first; 695 } 696 697 /* 698 * Skip any initial optional I/Os. 699 */ 700 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) { 701 first = AVL_NEXT(t, first); 702 ASSERT(first != NULL); 703 } 704 705 /* 706 * Walk forward through sufficiently contiguous I/Os. 707 * The aggregation limit does not apply to optional i/os, so that 708 * we can issue contiguous writes even if they are larger than the 709 * aggregation limit. 710 */ 711 while ((dio = AVL_NEXT(t, last)) != NULL && 712 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 713 (IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit || 714 (dio->io_flags & ZIO_FLAG_OPTIONAL)) && 715 IO_GAP(last, dio) <= maxgap) { 716 last = dio; 717 if (!(last->io_flags & ZIO_FLAG_OPTIONAL)) 718 mandatory = last; 719 } 720 721 /* 722 * Now that we've established the range of the I/O aggregation 723 * we must decide what to do with trailing optional I/Os. 724 * For reads, there's nothing to do. While we are unable to 725 * aggregate further, it's possible that a trailing optional 726 * I/O would allow the underlying device to aggregate with 727 * subsequent I/Os. We must therefore determine if the next 728 * non-optional I/O is close enough to make aggregation 729 * worthwhile. 730 */ 731 stretch = B_FALSE; 732 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) { 733 zio_t *nio = last; 734 while ((dio = AVL_NEXT(t, nio)) != NULL && 735 IO_GAP(nio, dio) == 0 && 736 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) { 737 nio = dio; 738 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) { 739 stretch = B_TRUE; 740 break; 741 } 742 } 743 } 744 745 if (stretch) { 746 /* 747 * We are going to include an optional io in our aggregated 748 * span, thus closing the write gap. Only mandatory i/os can 749 * start aggregated spans, so make sure that the next i/o 750 * after our span is mandatory. 751 */ 752 dio = AVL_NEXT(t, last); 753 dio->io_flags &= ~ZIO_FLAG_OPTIONAL; 754 } else { 755 /* do not include the optional i/o */ 756 while (last != mandatory && last != first) { 757 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL); 758 last = AVL_PREV(t, last); 759 ASSERT(last != NULL); 760 } 761 } 762 763 if (first == last) 764 return (NULL); 765 766 size = IO_SPAN(first, last); 767 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 768 769 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset, 770 abd_alloc_for_io(size, B_TRUE), size, first->io_type, 771 zio->io_priority, flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE, 772 vdev_queue_agg_io_done, NULL); 773 aio->io_timestamp = first->io_timestamp; 774 775 nio = first; 776 do { 777 dio = nio; 778 nio = AVL_NEXT(t, dio); 779 ASSERT3U(dio->io_type, ==, aio->io_type); 780 781 if (dio->io_flags & ZIO_FLAG_NODATA) { 782 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE); 783 abd_zero_off(aio->io_abd, 784 dio->io_offset - aio->io_offset, dio->io_size); 785 } else if (dio->io_type == ZIO_TYPE_WRITE) { 786 abd_copy_off(aio->io_abd, dio->io_abd, 787 dio->io_offset - aio->io_offset, 0, dio->io_size); 788 } 789 790 zio_add_child(dio, aio); 791 vdev_queue_io_remove(vq, dio); 792 zio_vdev_io_bypass(dio); 793 zio_execute(dio); 794 } while (dio != last); 795 796 return (aio); 797} 798 799static zio_t * 800vdev_queue_io_to_issue(vdev_queue_t *vq) 801{ 802 zio_t *zio, *aio; 803 zio_priority_t p; 804 avl_index_t idx; 805 avl_tree_t *tree; 806 zio_t search; 807 808again: 809 ASSERT(MUTEX_HELD(&vq->vq_lock)); 810 811 p = vdev_queue_class_to_issue(vq); 812 813 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) { 814 /* No eligible queued i/os */ 815 return (NULL); 816 } 817 818 /* 819 * For LBA-ordered queues (async / scrub), issue the i/o which follows 820 * the most recently issued i/o in LBA (offset) order. 821 * 822 * For FIFO queues (sync), issue the i/o with the lowest timestamp. 823 */ 824 tree = vdev_queue_class_tree(vq, p); 825 search.io_timestamp = 0; 826 search.io_offset = vq->vq_last_offset + 1; 827 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL); 828 zio = avl_nearest(tree, idx, AVL_AFTER); 829 if (zio == NULL) 830 zio = avl_first(tree); 831 ASSERT3U(zio->io_priority, ==, p); 832 833 aio = vdev_queue_aggregate(vq, zio); 834 if (aio != NULL) 835 zio = aio; 836 else 837 vdev_queue_io_remove(vq, zio); 838 839 /* 840 * If the I/O is or was optional and therefore has no data, we need to 841 * simply discard it. We need to drop the vdev queue's lock to avoid a 842 * deadlock that we could encounter since this I/O will complete 843 * immediately. 844 */ 845 if (zio->io_flags & ZIO_FLAG_NODATA) { 846 mutex_exit(&vq->vq_lock); 847 zio_vdev_io_bypass(zio); 848 zio_execute(zio); 849 mutex_enter(&vq->vq_lock); 850 goto again; 851 } 852 853 vdev_queue_pending_add(vq, zio); 854 vq->vq_last_offset = zio->io_offset; 855 856 return (zio); 857} 858 859zio_t * 860vdev_queue_io(zio_t *zio) 861{ 862 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 863 zio_t *nio; 864 865 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 866 return (zio); 867 868 /* 869 * Children i/os inherent their parent's priority, which might 870 * not match the child's i/o type. Fix it up here. 871 */ 872 if (zio->io_type == ZIO_TYPE_READ) { 873 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ && 874 zio->io_priority != ZIO_PRIORITY_ASYNC_READ && 875 zio->io_priority != ZIO_PRIORITY_SCRUB) 876 zio->io_priority = ZIO_PRIORITY_ASYNC_READ; 877 } else if (zio->io_type == ZIO_TYPE_WRITE) { 878 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 879 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE) 880 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE; 881 } else { 882 ASSERT(zio->io_type == ZIO_TYPE_FREE); 883 zio->io_priority = ZIO_PRIORITY_TRIM; 884 } 885 886 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 887 888 mutex_enter(&vq->vq_lock); 889 zio->io_timestamp = gethrtime(); 890 vdev_queue_io_add(vq, zio); 891 nio = vdev_queue_io_to_issue(vq); 892 mutex_exit(&vq->vq_lock); 893 894 if (nio == NULL) 895 return (NULL); 896 897 if (nio->io_done == vdev_queue_agg_io_done) { 898 zio_nowait(nio); 899 return (NULL); 900 } 901 902 return (nio); 903} 904 905void 906vdev_queue_io_done(zio_t *zio) 907{ 908 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 909 zio_t *nio; 910 911 mutex_enter(&vq->vq_lock); 912 913 vdev_queue_pending_remove(vq, zio); 914 915 vq->vq_io_complete_ts = gethrtime(); 916 917 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) { 918 mutex_exit(&vq->vq_lock); 919 if (nio->io_done == vdev_queue_agg_io_done) { 920 zio_nowait(nio); 921 } else { 922 zio_vdev_io_reissue(nio); 923 zio_execute(nio); 924 } 925 mutex_enter(&vq->vq_lock); 926 } 927 928 mutex_exit(&vq->vq_lock); 929} 930 931/* 932 * As these three methods are only used for load calculations we're not concerned 933 * if we get an incorrect value on 32bit platforms due to lack of vq_lock mutex 934 * use here, instead we prefer to keep it lock free for performance. 935 */ 936int 937vdev_queue_length(vdev_t *vd) 938{ 939 return (avl_numnodes(&vd->vdev_queue.vq_active_tree)); 940} 941 942uint64_t 943vdev_queue_lastoffset(vdev_t *vd) 944{ 945 return (vd->vdev_queue.vq_lastoffset); 946} 947 948void 949vdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio) 950{ 951 vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size; 952} 953