vdev_queue.c revision 260763
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd/* 22209962Smm * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23168404Spjd * Use is subject to license terms. 24168404Spjd */ 25168404Spjd 26247265Smm/* 27260763Savg * Copyright (c) 2013 by Delphix. All rights reserved. 28247265Smm */ 29247265Smm 30168404Spjd#include <sys/zfs_context.h> 31168404Spjd#include <sys/vdev_impl.h> 32260763Savg#include <sys/spa_impl.h> 33168404Spjd#include <sys/zio.h> 34168404Spjd#include <sys/avl.h> 35260763Savg#include <sys/dsl_pool.h> 36168404Spjd 37168404Spjd/* 38260763Savg * ZFS I/O Scheduler 39260763Savg * --------------- 40260763Savg * 41260763Savg * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The 42260763Savg * I/O scheduler determines when and in what order those operations are 43260763Savg * issued. The I/O scheduler divides operations into five I/O classes 44260763Savg * prioritized in the following order: sync read, sync write, async read, 45260763Savg * async write, and scrub/resilver. Each queue defines the minimum and 46260763Savg * maximum number of concurrent operations that may be issued to the device. 47260763Savg * In addition, the device has an aggregate maximum. Note that the sum of the 48260763Savg * per-queue minimums must not exceed the aggregate maximum, and if the 49260763Savg * aggregate maximum is equal to or greater than the sum of the per-queue 50260763Savg * maximums, the per-queue minimum has no effect. 51260763Savg * 52260763Savg * For many physical devices, throughput increases with the number of 53260763Savg * concurrent operations, but latency typically suffers. Further, physical 54260763Savg * devices typically have a limit at which more concurrent operations have no 55260763Savg * effect on throughput or can actually cause it to decrease. 56260763Savg * 57260763Savg * The scheduler selects the next operation to issue by first looking for an 58260763Savg * I/O class whose minimum has not been satisfied. Once all are satisfied and 59260763Savg * the aggregate maximum has not been hit, the scheduler looks for classes 60260763Savg * whose maximum has not been satisfied. Iteration through the I/O classes is 61260763Savg * done in the order specified above. No further operations are issued if the 62260763Savg * aggregate maximum number of concurrent operations has been hit or if there 63260763Savg * are no operations queued for an I/O class that has not hit its maximum. 64260763Savg * Every time an i/o is queued or an operation completes, the I/O scheduler 65260763Savg * looks for new operations to issue. 66260763Savg * 67260763Savg * All I/O classes have a fixed maximum number of outstanding operations 68260763Savg * except for the async write class. Asynchronous writes represent the data 69260763Savg * that is committed to stable storage during the syncing stage for 70260763Savg * transaction groups (see txg.c). Transaction groups enter the syncing state 71260763Savg * periodically so the number of queued async writes will quickly burst up and 72260763Savg * then bleed down to zero. Rather than servicing them as quickly as possible, 73260763Savg * the I/O scheduler changes the maximum number of active async write i/os 74260763Savg * according to the amount of dirty data in the pool (see dsl_pool.c). Since 75260763Savg * both throughput and latency typically increase with the number of 76260763Savg * concurrent operations issued to physical devices, reducing the burstiness 77260763Savg * in the number of concurrent operations also stabilizes the response time of 78260763Savg * operations from other -- and in particular synchronous -- queues. In broad 79260763Savg * strokes, the I/O scheduler will issue more concurrent operations from the 80260763Savg * async write queue as there's more dirty data in the pool. 81260763Savg * 82260763Savg * Async Writes 83260763Savg * 84260763Savg * The number of concurrent operations issued for the async write I/O class 85260763Savg * follows a piece-wise linear function defined by a few adjustable points. 86260763Savg * 87260763Savg * | o---------| <-- zfs_vdev_async_write_max_active 88260763Savg * ^ | /^ | 89260763Savg * | | / | | 90260763Savg * active | / | | 91260763Savg * I/O | / | | 92260763Savg * count | / | | 93260763Savg * | / | | 94260763Savg * |------------o | | <-- zfs_vdev_async_write_min_active 95260763Savg * 0|____________^______|_________| 96260763Savg * 0% | | 100% of zfs_dirty_data_max 97260763Savg * | | 98260763Savg * | `-- zfs_vdev_async_write_active_max_dirty_percent 99260763Savg * `--------- zfs_vdev_async_write_active_min_dirty_percent 100260763Savg * 101260763Savg * Until the amount of dirty data exceeds a minimum percentage of the dirty 102260763Savg * data allowed in the pool, the I/O scheduler will limit the number of 103260763Savg * concurrent operations to the minimum. As that threshold is crossed, the 104260763Savg * number of concurrent operations issued increases linearly to the maximum at 105260763Savg * the specified maximum percentage of the dirty data allowed in the pool. 106260763Savg * 107260763Savg * Ideally, the amount of dirty data on a busy pool will stay in the sloped 108260763Savg * part of the function between zfs_vdev_async_write_active_min_dirty_percent 109260763Savg * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the 110260763Savg * maximum percentage, this indicates that the rate of incoming data is 111260763Savg * greater than the rate that the backend storage can handle. In this case, we 112260763Savg * must further throttle incoming writes (see dmu_tx_delay() for details). 113168404Spjd */ 114251631Sdelphij 115260763Savg/* 116260763Savg * The maximum number of i/os active to each device. Ideally, this will be >= 117260763Savg * the sum of each queue's max_active. It must be at least the sum of each 118260763Savg * queue's min_active. 119260763Savg */ 120260763Savguint32_t zfs_vdev_max_active = 1000; 121251631Sdelphij 122168404Spjd/* 123260763Savg * Per-queue limits on the number of i/os active to each device. If the 124260763Savg * sum of the queue's max_active is < zfs_vdev_max_active, then the 125260763Savg * min_active comes into play. We will send min_active from each queue, 126260763Savg * and then select from queues in the order defined by zio_priority_t. 127260763Savg * 128260763Savg * In general, smaller max_active's will lead to lower latency of synchronous 129260763Savg * operations. Larger max_active's may lead to higher overall throughput, 130260763Savg * depending on underlying storage. 131260763Savg * 132260763Savg * The ratio of the queues' max_actives determines the balance of performance 133260763Savg * between reads, writes, and scrubs. E.g., increasing 134260763Savg * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete 135260763Savg * more quickly, but reads and writes to have higher latency and lower 136260763Savg * throughput. 137168404Spjd */ 138260763Savguint32_t zfs_vdev_sync_read_min_active = 10; 139260763Savguint32_t zfs_vdev_sync_read_max_active = 10; 140260763Savguint32_t zfs_vdev_sync_write_min_active = 10; 141260763Savguint32_t zfs_vdev_sync_write_max_active = 10; 142260763Savguint32_t zfs_vdev_async_read_min_active = 1; 143260763Savguint32_t zfs_vdev_async_read_max_active = 3; 144260763Savguint32_t zfs_vdev_async_write_min_active = 1; 145260763Savguint32_t zfs_vdev_async_write_max_active = 10; 146260763Savguint32_t zfs_vdev_scrub_min_active = 1; 147260763Savguint32_t zfs_vdev_scrub_max_active = 2; 148168404Spjd 149249206Smm/* 150260763Savg * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent 151260763Savg * dirty data, use zfs_vdev_async_write_min_active. When it has more than 152260763Savg * zfs_vdev_async_write_active_max_dirty_percent, use 153260763Savg * zfs_vdev_async_write_max_active. The value is linearly interpolated 154260763Savg * between min and max. 155249206Smm */ 156260763Savgint zfs_vdev_async_write_active_min_dirty_percent = 30; 157260763Savgint zfs_vdev_async_write_active_max_dirty_percent = 60; 158168404Spjd 159168404Spjd/* 160219089Spjd * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O. 161219089Spjd * For read I/Os, we also aggregate across small adjacency gaps; for writes 162219089Spjd * we include spans of optional I/Os to aid aggregation at the disk even when 163219089Spjd * they aren't able to help us aggregate at this level. 164168404Spjd */ 165168404Spjdint zfs_vdev_aggregation_limit = SPA_MAXBLOCKSIZE; 166209962Smmint zfs_vdev_read_gap_limit = 32 << 10; 167219089Spjdint zfs_vdev_write_gap_limit = 4 << 10; 168168404Spjd 169260763Savg#ifdef __FreeBSD__ 170185029SpjdSYSCTL_DECL(_vfs_zfs_vdev); 171260763SavgTUNABLE_INT("vfs.zfs.vdev.max_active", &zfs_vdev_max_active); 172260763SavgSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, max_active, CTLFLAG_RW, 173260763Savg &zfs_vdev_max_active, 0, 174260763Savg "The maximum number of i/os of all types active for each device."); 175260763Savg 176260763Savg#define ZFS_VDEV_QUEUE_KNOB_MIN(name) \ 177260763SavgTUNABLE_INT("vfs.zfs.vdev." #name "_min_active", \ 178260763Savg &zfs_vdev_ ## name ## _min_active); \ 179260763SavgSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _min_active, CTLFLAG_RW, \ 180260763Savg &zfs_vdev_ ## name ## _min_active, 0, \ 181260763Savg "Initial number of I/O requests of type " #name \ 182260763Savg " active for each device"); 183260763Savg 184260763Savg#define ZFS_VDEV_QUEUE_KNOB_MAX(name) \ 185260763SavgTUNABLE_INT("vfs.zfs.vdev." #name "_max_active", \ 186260763Savg &zfs_vdev_ ## name ## _max_active); \ 187260763SavgSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _max_active, CTLFLAG_RW, \ 188260763Savg &zfs_vdev_ ## name ## _max_active, 0, \ 189260763Savg "Maximum number of I/O requests of type " #name \ 190260763Savg " active for each device"); 191260763Savg 192260763SavgZFS_VDEV_QUEUE_KNOB_MIN(sync_read); 193260763SavgZFS_VDEV_QUEUE_KNOB_MAX(sync_read); 194260763SavgZFS_VDEV_QUEUE_KNOB_MIN(sync_write); 195260763SavgZFS_VDEV_QUEUE_KNOB_MAX(sync_write); 196260763SavgZFS_VDEV_QUEUE_KNOB_MIN(async_read); 197260763SavgZFS_VDEV_QUEUE_KNOB_MAX(async_read); 198260763SavgZFS_VDEV_QUEUE_KNOB_MIN(async_write); 199260763SavgZFS_VDEV_QUEUE_KNOB_MAX(async_write); 200260763SavgZFS_VDEV_QUEUE_KNOB_MIN(scrub); 201260763SavgZFS_VDEV_QUEUE_KNOB_MAX(scrub); 202260763Savg 203260763Savg#undef ZFS_VDEV_QUEUE_KNOB 204260763Savg 205185029SpjdTUNABLE_INT("vfs.zfs.vdev.aggregation_limit", &zfs_vdev_aggregation_limit); 206219089SpjdSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RW, 207185029Spjd &zfs_vdev_aggregation_limit, 0, 208185029Spjd "I/O requests are aggregated up to this size"); 209219089SpjdTUNABLE_INT("vfs.zfs.vdev.read_gap_limit", &zfs_vdev_read_gap_limit); 210219089SpjdSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RW, 211219089Spjd &zfs_vdev_read_gap_limit, 0, 212219089Spjd "Acceptable gap between two reads being aggregated"); 213219089SpjdTUNABLE_INT("vfs.zfs.vdev.write_gap_limit", &zfs_vdev_write_gap_limit); 214219089SpjdSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RW, 215219089Spjd &zfs_vdev_write_gap_limit, 0, 216219089Spjd "Acceptable gap between two writes being aggregated"); 217260763Savg#endif 218185029Spjd 219168404Spjdint 220260763Savgvdev_queue_offset_compare(const void *x1, const void *x2) 221168404Spjd{ 222168404Spjd const zio_t *z1 = x1; 223168404Spjd const zio_t *z2 = x2; 224168404Spjd 225168404Spjd if (z1->io_offset < z2->io_offset) 226168404Spjd return (-1); 227168404Spjd if (z1->io_offset > z2->io_offset) 228168404Spjd return (1); 229168404Spjd 230168404Spjd if (z1 < z2) 231168404Spjd return (-1); 232168404Spjd if (z1 > z2) 233168404Spjd return (1); 234168404Spjd 235168404Spjd return (0); 236168404Spjd} 237168404Spjd 238168404Spjdint 239260763Savgvdev_queue_timestamp_compare(const void *x1, const void *x2) 240168404Spjd{ 241168404Spjd const zio_t *z1 = x1; 242168404Spjd const zio_t *z2 = x2; 243168404Spjd 244260763Savg if (z1->io_timestamp < z2->io_timestamp) 245168404Spjd return (-1); 246260763Savg if (z1->io_timestamp > z2->io_timestamp) 247168404Spjd return (1); 248168404Spjd 249168404Spjd if (z1 < z2) 250168404Spjd return (-1); 251168404Spjd if (z1 > z2) 252168404Spjd return (1); 253168404Spjd 254168404Spjd return (0); 255168404Spjd} 256168404Spjd 257168404Spjdvoid 258168404Spjdvdev_queue_init(vdev_t *vd) 259168404Spjd{ 260168404Spjd vdev_queue_t *vq = &vd->vdev_queue; 261168404Spjd 262168404Spjd mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL); 263260763Savg vq->vq_vdev = vd; 264168404Spjd 265260763Savg avl_create(&vq->vq_active_tree, vdev_queue_offset_compare, 266260763Savg sizeof (zio_t), offsetof(struct zio, io_queue_node)); 267168404Spjd 268260763Savg for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 269260763Savg /* 270260763Savg * The synchronous i/o queues are FIFO rather than LBA ordered. 271260763Savg * This provides more consistent latency for these i/os, and 272260763Savg * they tend to not be tightly clustered anyway so there is 273260763Savg * little to no throughput loss. 274260763Savg */ 275260763Savg boolean_t fifo = (p == ZIO_PRIORITY_SYNC_READ || 276260763Savg p == ZIO_PRIORITY_SYNC_WRITE); 277260763Savg avl_create(&vq->vq_class[p].vqc_queued_tree, 278260763Savg fifo ? vdev_queue_timestamp_compare : 279260763Savg vdev_queue_offset_compare, 280260763Savg sizeof (zio_t), offsetof(struct zio, io_queue_node)); 281260763Savg } 282168404Spjd} 283168404Spjd 284168404Spjdvoid 285168404Spjdvdev_queue_fini(vdev_t *vd) 286168404Spjd{ 287168404Spjd vdev_queue_t *vq = &vd->vdev_queue; 288168404Spjd 289260763Savg for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) 290260763Savg avl_destroy(&vq->vq_class[p].vqc_queued_tree); 291260763Savg avl_destroy(&vq->vq_active_tree); 292168404Spjd 293168404Spjd mutex_destroy(&vq->vq_lock); 294168404Spjd} 295168404Spjd 296168404Spjdstatic void 297168404Spjdvdev_queue_io_add(vdev_queue_t *vq, zio_t *zio) 298168404Spjd{ 299260763Savg spa_t *spa = zio->io_spa; 300260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 301260763Savg avl_add(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio); 302260763Savg 303260763Savg#ifdef illumos 304260763Savg mutex_enter(&spa->spa_iokstat_lock); 305260763Savg spa->spa_queue_stats[zio->io_priority].spa_queued++; 306260763Savg if (spa->spa_iokstat != NULL) 307260763Savg kstat_waitq_enter(spa->spa_iokstat->ks_data); 308260763Savg mutex_exit(&spa->spa_iokstat_lock); 309260763Savg#endif 310168404Spjd} 311168404Spjd 312168404Spjdstatic void 313168404Spjdvdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio) 314168404Spjd{ 315260763Savg spa_t *spa = zio->io_spa; 316260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 317260763Savg avl_remove(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio); 318260763Savg 319260763Savg#ifdef illumos 320260763Savg mutex_enter(&spa->spa_iokstat_lock); 321260763Savg ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0); 322260763Savg spa->spa_queue_stats[zio->io_priority].spa_queued--; 323260763Savg if (spa->spa_iokstat != NULL) 324260763Savg kstat_waitq_exit(spa->spa_iokstat->ks_data); 325260763Savg mutex_exit(&spa->spa_iokstat_lock); 326260763Savg#endif 327168404Spjd} 328168404Spjd 329168404Spjdstatic void 330260763Savgvdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio) 331260763Savg{ 332260763Savg spa_t *spa = zio->io_spa; 333260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 334260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 335260763Savg vq->vq_class[zio->io_priority].vqc_active++; 336260763Savg avl_add(&vq->vq_active_tree, zio); 337260763Savg 338260763Savg#ifdef illumos 339260763Savg mutex_enter(&spa->spa_iokstat_lock); 340260763Savg spa->spa_queue_stats[zio->io_priority].spa_active++; 341260763Savg if (spa->spa_iokstat != NULL) 342260763Savg kstat_runq_enter(spa->spa_iokstat->ks_data); 343260763Savg mutex_exit(&spa->spa_iokstat_lock); 344260763Savg#endif 345260763Savg} 346260763Savg 347260763Savgstatic void 348260763Savgvdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio) 349260763Savg{ 350260763Savg spa_t *spa = zio->io_spa; 351260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 352260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 353260763Savg vq->vq_class[zio->io_priority].vqc_active--; 354260763Savg avl_remove(&vq->vq_active_tree, zio); 355260763Savg 356260763Savg#ifdef illumos 357260763Savg mutex_enter(&spa->spa_iokstat_lock); 358260763Savg ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0); 359260763Savg spa->spa_queue_stats[zio->io_priority].spa_active--; 360260763Savg if (spa->spa_iokstat != NULL) { 361260763Savg kstat_io_t *ksio = spa->spa_iokstat->ks_data; 362260763Savg 363260763Savg kstat_runq_exit(spa->spa_iokstat->ks_data); 364260763Savg if (zio->io_type == ZIO_TYPE_READ) { 365260763Savg ksio->reads++; 366260763Savg ksio->nread += zio->io_size; 367260763Savg } else if (zio->io_type == ZIO_TYPE_WRITE) { 368260763Savg ksio->writes++; 369260763Savg ksio->nwritten += zio->io_size; 370260763Savg } 371260763Savg } 372260763Savg mutex_exit(&spa->spa_iokstat_lock); 373260763Savg#endif 374260763Savg} 375260763Savg 376260763Savgstatic void 377168404Spjdvdev_queue_agg_io_done(zio_t *aio) 378168404Spjd{ 379260763Savg if (aio->io_type == ZIO_TYPE_READ) { 380260763Savg zio_t *pio; 381260763Savg while ((pio = zio_walk_parents(aio)) != NULL) { 382209962Smm bcopy((char *)aio->io_data + (pio->io_offset - 383209962Smm aio->io_offset), pio->io_data, pio->io_size); 384260763Savg } 385260763Savg } 386168404Spjd 387168404Spjd zio_buf_free(aio->io_data, aio->io_size); 388168404Spjd} 389168404Spjd 390260763Savgstatic int 391260763Savgvdev_queue_class_min_active(zio_priority_t p) 392260763Savg{ 393260763Savg switch (p) { 394260763Savg case ZIO_PRIORITY_SYNC_READ: 395260763Savg return (zfs_vdev_sync_read_min_active); 396260763Savg case ZIO_PRIORITY_SYNC_WRITE: 397260763Savg return (zfs_vdev_sync_write_min_active); 398260763Savg case ZIO_PRIORITY_ASYNC_READ: 399260763Savg return (zfs_vdev_async_read_min_active); 400260763Savg case ZIO_PRIORITY_ASYNC_WRITE: 401260763Savg return (zfs_vdev_async_write_min_active); 402260763Savg case ZIO_PRIORITY_SCRUB: 403260763Savg return (zfs_vdev_scrub_min_active); 404260763Savg default: 405260763Savg panic("invalid priority %u", p); 406260763Savg return (0); 407260763Savg } 408260763Savg} 409260763Savg 410260763Savgstatic int 411260763Savgvdev_queue_max_async_writes(uint64_t dirty) 412260763Savg{ 413260763Savg int writes; 414260763Savg uint64_t min_bytes = zfs_dirty_data_max * 415260763Savg zfs_vdev_async_write_active_min_dirty_percent / 100; 416260763Savg uint64_t max_bytes = zfs_dirty_data_max * 417260763Savg zfs_vdev_async_write_active_max_dirty_percent / 100; 418260763Savg 419260763Savg if (dirty < min_bytes) 420260763Savg return (zfs_vdev_async_write_min_active); 421260763Savg if (dirty > max_bytes) 422260763Savg return (zfs_vdev_async_write_max_active); 423260763Savg 424260763Savg /* 425260763Savg * linear interpolation: 426260763Savg * slope = (max_writes - min_writes) / (max_bytes - min_bytes) 427260763Savg * move right by min_bytes 428260763Savg * move up by min_writes 429260763Savg */ 430260763Savg writes = (dirty - min_bytes) * 431260763Savg (zfs_vdev_async_write_max_active - 432260763Savg zfs_vdev_async_write_min_active) / 433260763Savg (max_bytes - min_bytes) + 434260763Savg zfs_vdev_async_write_min_active; 435260763Savg ASSERT3U(writes, >=, zfs_vdev_async_write_min_active); 436260763Savg ASSERT3U(writes, <=, zfs_vdev_async_write_max_active); 437260763Savg return (writes); 438260763Savg} 439260763Savg 440260763Savgstatic int 441260763Savgvdev_queue_class_max_active(spa_t *spa, zio_priority_t p) 442260763Savg{ 443260763Savg switch (p) { 444260763Savg case ZIO_PRIORITY_SYNC_READ: 445260763Savg return (zfs_vdev_sync_read_max_active); 446260763Savg case ZIO_PRIORITY_SYNC_WRITE: 447260763Savg return (zfs_vdev_sync_write_max_active); 448260763Savg case ZIO_PRIORITY_ASYNC_READ: 449260763Savg return (zfs_vdev_async_read_max_active); 450260763Savg case ZIO_PRIORITY_ASYNC_WRITE: 451260763Savg return (vdev_queue_max_async_writes( 452260763Savg spa->spa_dsl_pool->dp_dirty_total)); 453260763Savg case ZIO_PRIORITY_SCRUB: 454260763Savg return (zfs_vdev_scrub_max_active); 455260763Savg default: 456260763Savg panic("invalid priority %u", p); 457260763Savg return (0); 458260763Savg } 459260763Savg} 460260763Savg 461209962Smm/* 462260763Savg * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if 463260763Savg * there is no eligible class. 464260763Savg */ 465260763Savgstatic zio_priority_t 466260763Savgvdev_queue_class_to_issue(vdev_queue_t *vq) 467260763Savg{ 468260763Savg spa_t *spa = vq->vq_vdev->vdev_spa; 469260763Savg zio_priority_t p; 470260763Savg 471260763Savg if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active) 472260763Savg return (ZIO_PRIORITY_NUM_QUEUEABLE); 473260763Savg 474260763Savg /* find a queue that has not reached its minimum # outstanding i/os */ 475260763Savg for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 476260763Savg if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 && 477260763Savg vq->vq_class[p].vqc_active < 478260763Savg vdev_queue_class_min_active(p)) 479260763Savg return (p); 480260763Savg } 481260763Savg 482260763Savg /* 483260763Savg * If we haven't found a queue, look for one that hasn't reached its 484260763Savg * maximum # outstanding i/os. 485260763Savg */ 486260763Savg for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 487260763Savg if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 && 488260763Savg vq->vq_class[p].vqc_active < 489260763Savg vdev_queue_class_max_active(spa, p)) 490260763Savg return (p); 491260763Savg } 492260763Savg 493260763Savg /* No eligible queued i/os */ 494260763Savg return (ZIO_PRIORITY_NUM_QUEUEABLE); 495260763Savg} 496260763Savg 497260763Savg/* 498209962Smm * Compute the range spanned by two i/os, which is the endpoint of the last 499209962Smm * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset). 500209962Smm * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio); 501209962Smm * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0. 502209962Smm */ 503209962Smm#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset) 504209962Smm#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio)) 505168404Spjd 506168404Spjdstatic zio_t * 507260763Savgvdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio) 508168404Spjd{ 509260763Savg zio_t *first, *last, *aio, *dio, *mandatory, *nio; 510260763Savg uint64_t maxgap = 0; 511260763Savg uint64_t size; 512260763Savg boolean_t stretch = B_FALSE; 513260763Savg vdev_queue_class_t *vqc = &vq->vq_class[zio->io_priority]; 514260763Savg avl_tree_t *t = &vqc->vqc_queued_tree; 515260763Savg enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT; 516168404Spjd 517260763Savg if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE) 518260763Savg return (NULL); 519168404Spjd 520260763Savg /* 521260763Savg * The synchronous i/o queues are not sorted by LBA, so we can't 522260763Savg * find adjacent i/os. These i/os tend to not be tightly clustered, 523260763Savg * or too large to aggregate, so this has little impact on performance. 524260763Savg */ 525260763Savg if (zio->io_priority == ZIO_PRIORITY_SYNC_READ || 526260763Savg zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) 527168404Spjd return (NULL); 528168404Spjd 529260763Savg first = last = zio; 530168404Spjd 531260763Savg if (zio->io_type == ZIO_TYPE_READ) 532260763Savg maxgap = zfs_vdev_read_gap_limit; 533168404Spjd 534260763Savg /* 535260763Savg * We can aggregate I/Os that are sufficiently adjacent and of 536260763Savg * the same flavor, as expressed by the AGG_INHERIT flags. 537260763Savg * The latter requirement is necessary so that certain 538260763Savg * attributes of the I/O, such as whether it's a normal I/O 539260763Savg * or a scrub/resilver, can be preserved in the aggregate. 540260763Savg * We can include optional I/Os, but don't allow them 541260763Savg * to begin a range as they add no benefit in that situation. 542260763Savg */ 543219089Spjd 544260763Savg /* 545260763Savg * We keep track of the last non-optional I/O. 546260763Savg */ 547260763Savg mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first; 548219089Spjd 549260763Savg /* 550260763Savg * Walk backwards through sufficiently contiguous I/Os 551260763Savg * recording the last non-option I/O. 552260763Savg */ 553260763Savg while ((dio = AVL_PREV(t, first)) != NULL && 554260763Savg (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 555260763Savg IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit && 556260763Savg IO_GAP(dio, first) <= maxgap) { 557260763Savg first = dio; 558260763Savg if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL)) 559260763Savg mandatory = first; 560260763Savg } 561168404Spjd 562260763Savg /* 563260763Savg * Skip any initial optional I/Os. 564260763Savg */ 565260763Savg while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) { 566260763Savg first = AVL_NEXT(t, first); 567260763Savg ASSERT(first != NULL); 568260763Savg } 569219089Spjd 570260763Savg /* 571260763Savg * Walk forward through sufficiently contiguous I/Os. 572260763Savg */ 573260763Savg while ((dio = AVL_NEXT(t, last)) != NULL && 574260763Savg (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 575260763Savg IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit && 576260763Savg IO_GAP(last, dio) <= maxgap) { 577260763Savg last = dio; 578260763Savg if (!(last->io_flags & ZIO_FLAG_OPTIONAL)) 579260763Savg mandatory = last; 580260763Savg } 581219089Spjd 582260763Savg /* 583260763Savg * Now that we've established the range of the I/O aggregation 584260763Savg * we must decide what to do with trailing optional I/Os. 585260763Savg * For reads, there's nothing to do. While we are unable to 586260763Savg * aggregate further, it's possible that a trailing optional 587260763Savg * I/O would allow the underlying device to aggregate with 588260763Savg * subsequent I/Os. We must therefore determine if the next 589260763Savg * non-optional I/O is close enough to make aggregation 590260763Savg * worthwhile. 591260763Savg */ 592260763Savg if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) { 593260763Savg zio_t *nio = last; 594260763Savg while ((dio = AVL_NEXT(t, nio)) != NULL && 595260763Savg IO_GAP(nio, dio) == 0 && 596260763Savg IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) { 597260763Savg nio = dio; 598260763Savg if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) { 599260763Savg stretch = B_TRUE; 600260763Savg break; 601219089Spjd } 602219089Spjd } 603260763Savg } 604219089Spjd 605260763Savg if (stretch) { 606260763Savg /* This may be a no-op. */ 607260763Savg dio = AVL_NEXT(t, last); 608260763Savg dio->io_flags &= ~ZIO_FLAG_OPTIONAL; 609260763Savg } else { 610260763Savg while (last != mandatory && last != first) { 611260763Savg ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL); 612260763Savg last = AVL_PREV(t, last); 613260763Savg ASSERT(last != NULL); 614219089Spjd } 615168404Spjd } 616168404Spjd 617260763Savg if (first == last) 618260763Savg return (NULL); 619168404Spjd 620260763Savg size = IO_SPAN(first, last); 621260763Savg ASSERT3U(size, <=, zfs_vdev_aggregation_limit); 622168404Spjd 623260763Savg aio = zio_vdev_delegated_io(first->io_vd, first->io_offset, 624260763Savg zio_buf_alloc(size), size, first->io_type, zio->io_priority, 625260763Savg flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE, 626260763Savg vdev_queue_agg_io_done, NULL); 627260763Savg aio->io_timestamp = first->io_timestamp; 628168404Spjd 629260763Savg nio = first; 630260763Savg do { 631260763Savg dio = nio; 632260763Savg nio = AVL_NEXT(t, dio); 633260763Savg ASSERT3U(dio->io_type, ==, aio->io_type); 634209962Smm 635260763Savg if (dio->io_flags & ZIO_FLAG_NODATA) { 636260763Savg ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE); 637260763Savg bzero((char *)aio->io_data + (dio->io_offset - 638260763Savg aio->io_offset), dio->io_size); 639260763Savg } else if (dio->io_type == ZIO_TYPE_WRITE) { 640260763Savg bcopy(dio->io_data, (char *)aio->io_data + 641260763Savg (dio->io_offset - aio->io_offset), 642260763Savg dio->io_size); 643260763Savg } 644168404Spjd 645260763Savg zio_add_child(dio, aio); 646260763Savg vdev_queue_io_remove(vq, dio); 647260763Savg zio_vdev_io_bypass(dio); 648260763Savg zio_execute(dio); 649260763Savg } while (dio != last); 650168404Spjd 651260763Savg return (aio); 652260763Savg} 653260763Savg 654260763Savgstatic zio_t * 655260763Savgvdev_queue_io_to_issue(vdev_queue_t *vq) 656260763Savg{ 657260763Savg zio_t *zio, *aio; 658260763Savg zio_priority_t p; 659260763Savg avl_index_t idx; 660260763Savg vdev_queue_class_t *vqc; 661260763Savg zio_t search; 662260763Savg 663260763Savgagain: 664260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 665260763Savg 666260763Savg p = vdev_queue_class_to_issue(vq); 667260763Savg 668260763Savg if (p == ZIO_PRIORITY_NUM_QUEUEABLE) { 669260763Savg /* No eligible queued i/os */ 670260763Savg return (NULL); 671168404Spjd } 672168404Spjd 673260763Savg /* 674260763Savg * For LBA-ordered queues (async / scrub), issue the i/o which follows 675260763Savg * the most recently issued i/o in LBA (offset) order. 676260763Savg * 677260763Savg * For FIFO queues (sync), issue the i/o with the lowest timestamp. 678260763Savg */ 679260763Savg vqc = &vq->vq_class[p]; 680260763Savg search.io_timestamp = 0; 681260763Savg search.io_offset = vq->vq_last_offset + 1; 682260763Savg VERIFY3P(avl_find(&vqc->vqc_queued_tree, &search, &idx), ==, NULL); 683260763Savg zio = avl_nearest(&vqc->vqc_queued_tree, idx, AVL_AFTER); 684260763Savg if (zio == NULL) 685260763Savg zio = avl_first(&vqc->vqc_queued_tree); 686260763Savg ASSERT3U(zio->io_priority, ==, p); 687168404Spjd 688260763Savg aio = vdev_queue_aggregate(vq, zio); 689260763Savg if (aio != NULL) 690260763Savg zio = aio; 691260763Savg else 692260763Savg vdev_queue_io_remove(vq, zio); 693260763Savg 694219089Spjd /* 695219089Spjd * If the I/O is or was optional and therefore has no data, we need to 696219089Spjd * simply discard it. We need to drop the vdev queue's lock to avoid a 697219089Spjd * deadlock that we could encounter since this I/O will complete 698219089Spjd * immediately. 699219089Spjd */ 700260763Savg if (zio->io_flags & ZIO_FLAG_NODATA) { 701219089Spjd mutex_exit(&vq->vq_lock); 702260763Savg zio_vdev_io_bypass(zio); 703260763Savg zio_execute(zio); 704219089Spjd mutex_enter(&vq->vq_lock); 705219089Spjd goto again; 706219089Spjd } 707219089Spjd 708260763Savg vdev_queue_pending_add(vq, zio); 709260763Savg vq->vq_last_offset = zio->io_offset; 710168404Spjd 711260763Savg return (zio); 712168404Spjd} 713168404Spjd 714168404Spjdzio_t * 715168404Spjdvdev_queue_io(zio_t *zio) 716168404Spjd{ 717168404Spjd vdev_queue_t *vq = &zio->io_vd->vdev_queue; 718168404Spjd zio_t *nio; 719168404Spjd 720168404Spjd if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 721168404Spjd return (zio); 722168404Spjd 723260763Savg /* 724260763Savg * Children i/os inherent their parent's priority, which might 725260763Savg * not match the child's i/o type. Fix it up here. 726260763Savg */ 727260763Savg if (zio->io_type == ZIO_TYPE_READ) { 728260763Savg if (zio->io_priority != ZIO_PRIORITY_SYNC_READ && 729260763Savg zio->io_priority != ZIO_PRIORITY_ASYNC_READ && 730260763Savg zio->io_priority != ZIO_PRIORITY_SCRUB) 731260763Savg zio->io_priority = ZIO_PRIORITY_ASYNC_READ; 732260763Savg } else { 733260763Savg ASSERT(zio->io_type == ZIO_TYPE_WRITE); 734260763Savg if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 735260763Savg zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE) 736260763Savg zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE; 737260763Savg } 738260763Savg 739168404Spjd zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 740168404Spjd 741168404Spjd mutex_enter(&vq->vq_lock); 742249206Smm zio->io_timestamp = gethrtime(); 743168404Spjd vdev_queue_io_add(vq, zio); 744260763Savg nio = vdev_queue_io_to_issue(vq); 745168404Spjd mutex_exit(&vq->vq_lock); 746168404Spjd 747185029Spjd if (nio == NULL) 748185029Spjd return (NULL); 749168404Spjd 750185029Spjd if (nio->io_done == vdev_queue_agg_io_done) { 751185029Spjd zio_nowait(nio); 752185029Spjd return (NULL); 753185029Spjd } 754185029Spjd 755185029Spjd return (nio); 756168404Spjd} 757168404Spjd 758168404Spjdvoid 759168404Spjdvdev_queue_io_done(zio_t *zio) 760168404Spjd{ 761168404Spjd vdev_queue_t *vq = &zio->io_vd->vdev_queue; 762260763Savg zio_t *nio; 763168404Spjd 764247265Smm if (zio_injection_enabled) 765247265Smm delay(SEC_TO_TICK(zio_handle_io_delay(zio))); 766247265Smm 767168404Spjd mutex_enter(&vq->vq_lock); 768168404Spjd 769260763Savg vdev_queue_pending_remove(vq, zio); 770168404Spjd 771249206Smm vq->vq_io_complete_ts = gethrtime(); 772247265Smm 773260763Savg while ((nio = vdev_queue_io_to_issue(vq)) != NULL) { 774168404Spjd mutex_exit(&vq->vq_lock); 775185029Spjd if (nio->io_done == vdev_queue_agg_io_done) { 776185029Spjd zio_nowait(nio); 777185029Spjd } else { 778168404Spjd zio_vdev_io_reissue(nio); 779185029Spjd zio_execute(nio); 780185029Spjd } 781168404Spjd mutex_enter(&vq->vq_lock); 782168404Spjd } 783168404Spjd 784168404Spjd mutex_exit(&vq->vq_lock); 785168404Spjd} 786