vdev_queue.c revision 288552
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 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE); 315 if (t == ZIO_TYPE_READ) 316 return (&vq->vq_read_offset_tree); 317 else 318 return (&vq->vq_write_offset_tree); 319} 320 321int 322vdev_queue_timestamp_compare(const void *x1, const void *x2) 323{ 324 const zio_t *z1 = x1; 325 const zio_t *z2 = x2; 326 327 if (z1->io_timestamp < z2->io_timestamp) 328 return (-1); 329 if (z1->io_timestamp > z2->io_timestamp) 330 return (1); 331 332 if (z1->io_offset < z2->io_offset) 333 return (-1); 334 if (z1->io_offset > z2->io_offset) 335 return (1); 336 337 if (z1 < z2) 338 return (-1); 339 if (z1 > z2) 340 return (1); 341 342 return (0); 343} 344 345void 346vdev_queue_init(vdev_t *vd) 347{ 348 vdev_queue_t *vq = &vd->vdev_queue; 349 350 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL); 351 vq->vq_vdev = vd; 352 353 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare, 354 sizeof (zio_t), offsetof(struct zio, io_queue_node)); 355 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ), 356 vdev_queue_offset_compare, sizeof (zio_t), 357 offsetof(struct zio, io_offset_node)); 358 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE), 359 vdev_queue_offset_compare, sizeof (zio_t), 360 offsetof(struct zio, io_offset_node)); 361 362 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 363 int (*compfn) (const void *, const void *); 364 365 /* 366 * The synchronous i/o queues are dispatched in FIFO rather 367 * than LBA order. This provides more consistent latency for 368 * these i/os. 369 */ 370 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE) 371 compfn = vdev_queue_timestamp_compare; 372 else 373 compfn = vdev_queue_offset_compare; 374 375 avl_create(vdev_queue_class_tree(vq, p), compfn, 376 sizeof (zio_t), offsetof(struct zio, io_queue_node)); 377 } 378 379 vq->vq_lastoffset = 0; 380} 381 382void 383vdev_queue_fini(vdev_t *vd) 384{ 385 vdev_queue_t *vq = &vd->vdev_queue; 386 387 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) 388 avl_destroy(vdev_queue_class_tree(vq, p)); 389 avl_destroy(&vq->vq_active_tree); 390 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ)); 391 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE)); 392 393 mutex_destroy(&vq->vq_lock); 394} 395 396static void 397vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio) 398{ 399 spa_t *spa = zio->io_spa; 400 ASSERT(MUTEX_HELD(&vq->vq_lock)); 401 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 402 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio); 403 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio); 404 405#ifdef illumos 406 mutex_enter(&spa->spa_iokstat_lock); 407 spa->spa_queue_stats[zio->io_priority].spa_queued++; 408 if (spa->spa_iokstat != NULL) 409 kstat_waitq_enter(spa->spa_iokstat->ks_data); 410 mutex_exit(&spa->spa_iokstat_lock); 411#endif 412} 413 414static void 415vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio) 416{ 417 spa_t *spa = zio->io_spa; 418 ASSERT(MUTEX_HELD(&vq->vq_lock)); 419 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 420 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio); 421 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio); 422 423#ifdef illumos 424 mutex_enter(&spa->spa_iokstat_lock); 425 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0); 426 spa->spa_queue_stats[zio->io_priority].spa_queued--; 427 if (spa->spa_iokstat != NULL) 428 kstat_waitq_exit(spa->spa_iokstat->ks_data); 429 mutex_exit(&spa->spa_iokstat_lock); 430#endif 431} 432 433static void 434vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio) 435{ 436 spa_t *spa = zio->io_spa; 437 ASSERT(MUTEX_HELD(&vq->vq_lock)); 438 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 439 vq->vq_class[zio->io_priority].vqc_active++; 440 avl_add(&vq->vq_active_tree, zio); 441 442#ifdef illumos 443 mutex_enter(&spa->spa_iokstat_lock); 444 spa->spa_queue_stats[zio->io_priority].spa_active++; 445 if (spa->spa_iokstat != NULL) 446 kstat_runq_enter(spa->spa_iokstat->ks_data); 447 mutex_exit(&spa->spa_iokstat_lock); 448#endif 449} 450 451static void 452vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio) 453{ 454 spa_t *spa = zio->io_spa; 455 ASSERT(MUTEX_HELD(&vq->vq_lock)); 456 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 457 vq->vq_class[zio->io_priority].vqc_active--; 458 avl_remove(&vq->vq_active_tree, zio); 459 460#ifdef illumos 461 mutex_enter(&spa->spa_iokstat_lock); 462 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0); 463 spa->spa_queue_stats[zio->io_priority].spa_active--; 464 if (spa->spa_iokstat != NULL) { 465 kstat_io_t *ksio = spa->spa_iokstat->ks_data; 466 467 kstat_runq_exit(spa->spa_iokstat->ks_data); 468 if (zio->io_type == ZIO_TYPE_READ) { 469 ksio->reads++; 470 ksio->nread += zio->io_size; 471 } else if (zio->io_type == ZIO_TYPE_WRITE) { 472 ksio->writes++; 473 ksio->nwritten += zio->io_size; 474 } 475 } 476 mutex_exit(&spa->spa_iokstat_lock); 477#endif 478} 479 480static void 481vdev_queue_agg_io_done(zio_t *aio) 482{ 483 if (aio->io_type == ZIO_TYPE_READ) { 484 zio_t *pio; 485 while ((pio = zio_walk_parents(aio)) != NULL) { 486 bcopy((char *)aio->io_data + (pio->io_offset - 487 aio->io_offset), pio->io_data, pio->io_size); 488 } 489 } 490 491 zio_buf_free(aio->io_data, aio->io_size); 492} 493 494static int 495vdev_queue_class_min_active(zio_priority_t p) 496{ 497 switch (p) { 498 case ZIO_PRIORITY_SYNC_READ: 499 return (zfs_vdev_sync_read_min_active); 500 case ZIO_PRIORITY_SYNC_WRITE: 501 return (zfs_vdev_sync_write_min_active); 502 case ZIO_PRIORITY_ASYNC_READ: 503 return (zfs_vdev_async_read_min_active); 504 case ZIO_PRIORITY_ASYNC_WRITE: 505 return (zfs_vdev_async_write_min_active); 506 case ZIO_PRIORITY_SCRUB: 507 return (zfs_vdev_scrub_min_active); 508 case ZIO_PRIORITY_TRIM: 509 return (zfs_vdev_trim_min_active); 510 default: 511 panic("invalid priority %u", p); 512 return (0); 513 } 514} 515 516static int 517vdev_queue_max_async_writes(spa_t *spa) 518{ 519 int writes; 520 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total; 521 uint64_t min_bytes = zfs_dirty_data_max * 522 zfs_vdev_async_write_active_min_dirty_percent / 100; 523 uint64_t max_bytes = zfs_dirty_data_max * 524 zfs_vdev_async_write_active_max_dirty_percent / 100; 525 526 /* 527 * Sync tasks correspond to interactive user actions. To reduce the 528 * execution time of those actions we push data out as fast as possible. 529 */ 530 if (spa_has_pending_synctask(spa)) { 531 return (zfs_vdev_async_write_max_active); 532 } 533 534 if (dirty < min_bytes) 535 return (zfs_vdev_async_write_min_active); 536 if (dirty > max_bytes) 537 return (zfs_vdev_async_write_max_active); 538 539 /* 540 * linear interpolation: 541 * slope = (max_writes - min_writes) / (max_bytes - min_bytes) 542 * move right by min_bytes 543 * move up by min_writes 544 */ 545 writes = (dirty - min_bytes) * 546 (zfs_vdev_async_write_max_active - 547 zfs_vdev_async_write_min_active) / 548 (max_bytes - min_bytes) + 549 zfs_vdev_async_write_min_active; 550 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active); 551 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active); 552 return (writes); 553} 554 555static int 556vdev_queue_class_max_active(spa_t *spa, zio_priority_t p) 557{ 558 switch (p) { 559 case ZIO_PRIORITY_SYNC_READ: 560 return (zfs_vdev_sync_read_max_active); 561 case ZIO_PRIORITY_SYNC_WRITE: 562 return (zfs_vdev_sync_write_max_active); 563 case ZIO_PRIORITY_ASYNC_READ: 564 return (zfs_vdev_async_read_max_active); 565 case ZIO_PRIORITY_ASYNC_WRITE: 566 return (vdev_queue_max_async_writes(spa)); 567 case ZIO_PRIORITY_SCRUB: 568 return (zfs_vdev_scrub_max_active); 569 case ZIO_PRIORITY_TRIM: 570 return (zfs_vdev_trim_max_active); 571 default: 572 panic("invalid priority %u", p); 573 return (0); 574 } 575} 576 577/* 578 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if 579 * there is no eligible class. 580 */ 581static zio_priority_t 582vdev_queue_class_to_issue(vdev_queue_t *vq) 583{ 584 spa_t *spa = vq->vq_vdev->vdev_spa; 585 zio_priority_t p; 586 587 ASSERT(MUTEX_HELD(&vq->vq_lock)); 588 589 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active) 590 return (ZIO_PRIORITY_NUM_QUEUEABLE); 591 592 /* find a queue that has not reached its minimum # outstanding i/os */ 593 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 594 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 && 595 vq->vq_class[p].vqc_active < 596 vdev_queue_class_min_active(p)) 597 return (p); 598 } 599 600 /* 601 * If we haven't found a queue, look for one that hasn't reached its 602 * maximum # outstanding i/os. 603 */ 604 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 605 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 && 606 vq->vq_class[p].vqc_active < 607 vdev_queue_class_max_active(spa, p)) 608 return (p); 609 } 610 611 /* No eligible queued i/os */ 612 return (ZIO_PRIORITY_NUM_QUEUEABLE); 613} 614 615/* 616 * Compute the range spanned by two i/os, which is the endpoint of the last 617 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset). 618 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio); 619 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0. 620 */ 621#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset) 622#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio)) 623 624static zio_t * 625vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio) 626{ 627 zio_t *first, *last, *aio, *dio, *mandatory, *nio; 628 uint64_t maxgap = 0; 629 uint64_t size; 630 boolean_t stretch; 631 avl_tree_t *t; 632 enum zio_flag flags; 633 634 ASSERT(MUTEX_HELD(&vq->vq_lock)); 635 636 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE) 637 return (NULL); 638 639 /* 640 * The synchronous i/o queues are not sorted by LBA, so we can't 641 * find adjacent i/os. These i/os tend to not be tightly clustered, 642 * or too large to aggregate, so this has little impact on performance. 643 */ 644 if (zio->io_priority == ZIO_PRIORITY_SYNC_READ || 645 zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) 646 return (NULL); 647 648 first = last = zio; 649 650 if (zio->io_type == ZIO_TYPE_READ) 651 maxgap = zfs_vdev_read_gap_limit; 652 653 /* 654 * We can aggregate I/Os that are sufficiently adjacent and of 655 * the same flavor, as expressed by the AGG_INHERIT flags. 656 * The latter requirement is necessary so that certain 657 * attributes of the I/O, such as whether it's a normal I/O 658 * or a scrub/resilver, can be preserved in the aggregate. 659 * We can include optional I/Os, but don't allow them 660 * to begin a range as they add no benefit in that situation. 661 */ 662 663 /* 664 * We keep track of the last non-optional I/O. 665 */ 666 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first; 667 668 /* 669 * Walk backwards through sufficiently contiguous I/Os 670 * recording the last non-option I/O. 671 */ 672 flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT; 673 t = vdev_queue_type_tree(vq, zio->io_type); 674 while ((dio = AVL_PREV(t, first)) != NULL && 675 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 676 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit && 677 IO_GAP(dio, first) <= maxgap) { 678 first = dio; 679 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL)) 680 mandatory = first; 681 } 682 683 /* 684 * Skip any initial optional I/Os. 685 */ 686 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) { 687 first = AVL_NEXT(t, first); 688 ASSERT(first != NULL); 689 } 690 691 /* 692 * Walk forward through sufficiently contiguous I/Os. 693 */ 694 while ((dio = AVL_NEXT(t, last)) != NULL && 695 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 696 IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit && 697 IO_GAP(last, dio) <= maxgap) { 698 last = dio; 699 if (!(last->io_flags & ZIO_FLAG_OPTIONAL)) 700 mandatory = last; 701 } 702 703 /* 704 * Now that we've established the range of the I/O aggregation 705 * we must decide what to do with trailing optional I/Os. 706 * For reads, there's nothing to do. While we are unable to 707 * aggregate further, it's possible that a trailing optional 708 * I/O would allow the underlying device to aggregate with 709 * subsequent I/Os. We must therefore determine if the next 710 * non-optional I/O is close enough to make aggregation 711 * worthwhile. 712 */ 713 stretch = B_FALSE; 714 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) { 715 zio_t *nio = last; 716 while ((dio = AVL_NEXT(t, nio)) != NULL && 717 IO_GAP(nio, dio) == 0 && 718 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) { 719 nio = dio; 720 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) { 721 stretch = B_TRUE; 722 break; 723 } 724 } 725 } 726 727 if (stretch) { 728 /* This may be a no-op. */ 729 dio = AVL_NEXT(t, last); 730 dio->io_flags &= ~ZIO_FLAG_OPTIONAL; 731 } else { 732 while (last != mandatory && last != first) { 733 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL); 734 last = AVL_PREV(t, last); 735 ASSERT(last != NULL); 736 } 737 } 738 739 if (first == last) 740 return (NULL); 741 742 size = IO_SPAN(first, last); 743 ASSERT3U(size, <=, zfs_vdev_aggregation_limit); 744 745 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset, 746 zio_buf_alloc(size), size, first->io_type, zio->io_priority, 747 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE, 748 vdev_queue_agg_io_done, NULL); 749 aio->io_timestamp = first->io_timestamp; 750 751 nio = first; 752 do { 753 dio = nio; 754 nio = AVL_NEXT(t, dio); 755 ASSERT3U(dio->io_type, ==, aio->io_type); 756 757 if (dio->io_flags & ZIO_FLAG_NODATA) { 758 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE); 759 bzero((char *)aio->io_data + (dio->io_offset - 760 aio->io_offset), dio->io_size); 761 } else if (dio->io_type == ZIO_TYPE_WRITE) { 762 bcopy(dio->io_data, (char *)aio->io_data + 763 (dio->io_offset - aio->io_offset), 764 dio->io_size); 765 } 766 767 zio_add_child(dio, aio); 768 vdev_queue_io_remove(vq, dio); 769 zio_vdev_io_bypass(dio); 770 zio_execute(dio); 771 } while (dio != last); 772 773 return (aio); 774} 775 776static zio_t * 777vdev_queue_io_to_issue(vdev_queue_t *vq) 778{ 779 zio_t *zio, *aio; 780 zio_priority_t p; 781 avl_index_t idx; 782 avl_tree_t *tree; 783 zio_t search; 784 785again: 786 ASSERT(MUTEX_HELD(&vq->vq_lock)); 787 788 p = vdev_queue_class_to_issue(vq); 789 790 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) { 791 /* No eligible queued i/os */ 792 return (NULL); 793 } 794 795 /* 796 * For LBA-ordered queues (async / scrub), issue the i/o which follows 797 * the most recently issued i/o in LBA (offset) order. 798 * 799 * For FIFO queues (sync), issue the i/o with the lowest timestamp. 800 */ 801 tree = vdev_queue_class_tree(vq, p); 802 search.io_timestamp = 0; 803 search.io_offset = vq->vq_last_offset + 1; 804 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL); 805 zio = avl_nearest(tree, idx, AVL_AFTER); 806 if (zio == NULL) 807 zio = avl_first(tree); 808 ASSERT3U(zio->io_priority, ==, p); 809 810 aio = vdev_queue_aggregate(vq, zio); 811 if (aio != NULL) 812 zio = aio; 813 else 814 vdev_queue_io_remove(vq, zio); 815 816 /* 817 * If the I/O is or was optional and therefore has no data, we need to 818 * simply discard it. We need to drop the vdev queue's lock to avoid a 819 * deadlock that we could encounter since this I/O will complete 820 * immediately. 821 */ 822 if (zio->io_flags & ZIO_FLAG_NODATA) { 823 mutex_exit(&vq->vq_lock); 824 zio_vdev_io_bypass(zio); 825 zio_execute(zio); 826 mutex_enter(&vq->vq_lock); 827 goto again; 828 } 829 830 vdev_queue_pending_add(vq, zio); 831 vq->vq_last_offset = zio->io_offset; 832 833 return (zio); 834} 835 836zio_t * 837vdev_queue_io(zio_t *zio) 838{ 839 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 840 zio_t *nio; 841 842 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 843 return (zio); 844 845 /* 846 * Children i/os inherent their parent's priority, which might 847 * not match the child's i/o type. Fix it up here. 848 */ 849 if (zio->io_type == ZIO_TYPE_READ) { 850 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ && 851 zio->io_priority != ZIO_PRIORITY_ASYNC_READ && 852 zio->io_priority != ZIO_PRIORITY_SCRUB) 853 zio->io_priority = ZIO_PRIORITY_ASYNC_READ; 854 } else if (zio->io_type == ZIO_TYPE_WRITE) { 855 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 856 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE) 857 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE; 858 } else { 859 ASSERT(zio->io_type == ZIO_TYPE_FREE); 860 zio->io_priority = ZIO_PRIORITY_TRIM; 861 } 862 863 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 864 865 mutex_enter(&vq->vq_lock); 866 zio->io_timestamp = gethrtime(); 867 vdev_queue_io_add(vq, zio); 868 nio = vdev_queue_io_to_issue(vq); 869 mutex_exit(&vq->vq_lock); 870 871 if (nio == NULL) 872 return (NULL); 873 874 if (nio->io_done == vdev_queue_agg_io_done) { 875 zio_nowait(nio); 876 return (NULL); 877 } 878 879 return (nio); 880} 881 882void 883vdev_queue_io_done(zio_t *zio) 884{ 885 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 886 zio_t *nio; 887 888 if (zio_injection_enabled) 889 delay(SEC_TO_TICK(zio_handle_io_delay(zio))); 890 891 mutex_enter(&vq->vq_lock); 892 893 vdev_queue_pending_remove(vq, zio); 894 895 vq->vq_io_complete_ts = gethrtime(); 896 897 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) { 898 mutex_exit(&vq->vq_lock); 899 if (nio->io_done == vdev_queue_agg_io_done) { 900 zio_nowait(nio); 901 } else { 902 zio_vdev_io_reissue(nio); 903 zio_execute(nio); 904 } 905 mutex_enter(&vq->vq_lock); 906 } 907 908 mutex_exit(&vq->vq_lock); 909} 910 911/* 912 * As these three methods are only used for load calculations we're not concerned 913 * if we get an incorrect value on 32bit platforms due to lack of vq_lock mutex 914 * use here, instead we prefer to keep it lock free for performance. 915 */ 916int 917vdev_queue_length(vdev_t *vd) 918{ 919 return (avl_numnodes(&vd->vdev_queue.vq_active_tree)); 920} 921 922uint64_t 923vdev_queue_lastoffset(vdev_t *vd) 924{ 925 return (vd->vdev_queue.vq_lastoffset); 926} 927 928void 929vdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio) 930{ 931 vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size; 932} 933