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