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/* 27269418Sdelphij * Copyright (c) 2012, 2014 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 43270312Ssmh * issued. The I/O scheduler divides operations into six I/O classes 44260763Savg * prioritized in the following order: sync read, sync write, async read, 45270312Ssmh * async write, scrub/resilver and trim. 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. 64270312Ssmh * 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, 73270312Ssmh * 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/* 116270312Ssmh * 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/* 123270312Ssmh * 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; 148270312Ssmhuint32_t zfs_vdev_trim_min_active = 1; 149270312Ssmh/* 150270312Ssmh * TRIM max active is large in comparison to the other values due to the fact 151270312Ssmh * that TRIM IOs are coalesced at the device layer. This value is set such 152270312Ssmh * that a typical SSD can process the queued IOs in a single request. 153270312Ssmh */ 154270312Ssmhuint32_t zfs_vdev_trim_max_active = 64; 155168404Spjd 156270312Ssmh 157249206Smm/* 158260763Savg * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent 159260763Savg * dirty data, use zfs_vdev_async_write_min_active. When it has more than 160260763Savg * zfs_vdev_async_write_active_max_dirty_percent, use 161260763Savg * zfs_vdev_async_write_max_active. The value is linearly interpolated 162260763Savg * between min and max. 163249206Smm */ 164260763Savgint zfs_vdev_async_write_active_min_dirty_percent = 30; 165260763Savgint zfs_vdev_async_write_active_max_dirty_percent = 60; 166168404Spjd 167168404Spjd/* 168219089Spjd * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O. 169219089Spjd * For read I/Os, we also aggregate across small adjacency gaps; for writes 170219089Spjd * we include spans of optional I/Os to aid aggregation at the disk even when 171219089Spjd * they aren't able to help us aggregate at this level. 172168404Spjd */ 173276081Sdelphijint zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE; 174209962Smmint zfs_vdev_read_gap_limit = 32 << 10; 175219089Spjdint zfs_vdev_write_gap_limit = 4 << 10; 176168404Spjd 177260763Savg#ifdef __FreeBSD__ 178185029SpjdSYSCTL_DECL(_vfs_zfs_vdev); 179272882Ssmh 180272882SsmhTUNABLE_INT("vfs.zfs.vdev.async_write_active_min_dirty_percent", 181272882Ssmh &zfs_vdev_async_write_active_min_dirty_percent); 182272882Ssmhstatic int sysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS); 183272882SsmhSYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_min_dirty_percent, 184272882Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int), 185272882Ssmh sysctl_zfs_async_write_active_min_dirty_percent, "I", 186272882Ssmh "Percentage of async write dirty data below which " 187272882Ssmh "async_write_min_active is used."); 188272882Ssmh 189272882SsmhTUNABLE_INT("vfs.zfs.vdev.async_write_active_max_dirty_percent", 190272882Ssmh &zfs_vdev_async_write_active_max_dirty_percent); 191272882Ssmhstatic int sysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS); 192272882SsmhSYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_max_dirty_percent, 193272882Ssmh CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int), 194272882Ssmh sysctl_zfs_async_write_active_max_dirty_percent, "I", 195272882Ssmh "Percentage of async write dirty data above which " 196272882Ssmh "async_write_max_active is used."); 197272882Ssmh 198260763SavgTUNABLE_INT("vfs.zfs.vdev.max_active", &zfs_vdev_max_active); 199272882SsmhSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, max_active, CTLFLAG_RWTUN, 200260763Savg &zfs_vdev_max_active, 0, 201270312Ssmh "The maximum number of I/Os of all types active for each device."); 202260763Savg 203260763Savg#define ZFS_VDEV_QUEUE_KNOB_MIN(name) \ 204260763SavgTUNABLE_INT("vfs.zfs.vdev." #name "_min_active", \ 205260763Savg &zfs_vdev_ ## name ## _min_active); \ 206272882SsmhSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _min_active, \ 207272882Ssmh CTLFLAG_RWTUN, &zfs_vdev_ ## name ## _min_active, 0, \ 208260763Savg "Initial number of I/O requests of type " #name \ 209260763Savg " active for each device"); 210260763Savg 211260763Savg#define ZFS_VDEV_QUEUE_KNOB_MAX(name) \ 212260763SavgTUNABLE_INT("vfs.zfs.vdev." #name "_max_active", \ 213260763Savg &zfs_vdev_ ## name ## _max_active); \ 214272882SsmhSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _max_active, \ 215272882Ssmh CTLFLAG_RWTUN, &zfs_vdev_ ## name ## _max_active, 0, \ 216260763Savg "Maximum number of I/O requests of type " #name \ 217260763Savg " active for each device"); 218260763Savg 219260763SavgZFS_VDEV_QUEUE_KNOB_MIN(sync_read); 220260763SavgZFS_VDEV_QUEUE_KNOB_MAX(sync_read); 221260763SavgZFS_VDEV_QUEUE_KNOB_MIN(sync_write); 222260763SavgZFS_VDEV_QUEUE_KNOB_MAX(sync_write); 223260763SavgZFS_VDEV_QUEUE_KNOB_MIN(async_read); 224260763SavgZFS_VDEV_QUEUE_KNOB_MAX(async_read); 225260763SavgZFS_VDEV_QUEUE_KNOB_MIN(async_write); 226260763SavgZFS_VDEV_QUEUE_KNOB_MAX(async_write); 227260763SavgZFS_VDEV_QUEUE_KNOB_MIN(scrub); 228260763SavgZFS_VDEV_QUEUE_KNOB_MAX(scrub); 229270312SsmhZFS_VDEV_QUEUE_KNOB_MIN(trim); 230270312SsmhZFS_VDEV_QUEUE_KNOB_MAX(trim); 231260763Savg 232260763Savg#undef ZFS_VDEV_QUEUE_KNOB 233260763Savg 234185029SpjdTUNABLE_INT("vfs.zfs.vdev.aggregation_limit", &zfs_vdev_aggregation_limit); 235272882SsmhSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RWTUN, 236185029Spjd &zfs_vdev_aggregation_limit, 0, 237185029Spjd "I/O requests are aggregated up to this size"); 238219089SpjdTUNABLE_INT("vfs.zfs.vdev.read_gap_limit", &zfs_vdev_read_gap_limit); 239272882SsmhSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RWTUN, 240219089Spjd &zfs_vdev_read_gap_limit, 0, 241219089Spjd "Acceptable gap between two reads being aggregated"); 242219089SpjdTUNABLE_INT("vfs.zfs.vdev.write_gap_limit", &zfs_vdev_write_gap_limit); 243272882SsmhSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RWTUN, 244219089Spjd &zfs_vdev_write_gap_limit, 0, 245219089Spjd "Acceptable gap between two writes being aggregated"); 246272882Ssmh 247272882Ssmhstatic int 248272882Ssmhsysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS) 249272882Ssmh{ 250272882Ssmh int val, err; 251272882Ssmh 252272882Ssmh val = zfs_vdev_async_write_active_min_dirty_percent; 253272882Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 254272882Ssmh if (err != 0 || req->newptr == NULL) 255272882Ssmh return (err); 256272882Ssmh 257272882Ssmh if (val < 0 || val > 100 || 258272882Ssmh val >= zfs_vdev_async_write_active_max_dirty_percent) 259272882Ssmh return (EINVAL); 260272882Ssmh 261272882Ssmh zfs_vdev_async_write_active_min_dirty_percent = val; 262272882Ssmh 263272882Ssmh return (0); 264272882Ssmh} 265272882Ssmh 266272882Ssmhstatic int 267272882Ssmhsysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS) 268272882Ssmh{ 269272882Ssmh int val, err; 270272882Ssmh 271272882Ssmh val = zfs_vdev_async_write_active_max_dirty_percent; 272272882Ssmh err = sysctl_handle_int(oidp, &val, 0, req); 273272882Ssmh if (err != 0 || req->newptr == NULL) 274272882Ssmh return (err); 275272882Ssmh 276272882Ssmh if (val < 0 || val > 100 || 277272882Ssmh val <= zfs_vdev_async_write_active_min_dirty_percent) 278272882Ssmh return (EINVAL); 279272882Ssmh 280272882Ssmh zfs_vdev_async_write_active_max_dirty_percent = val; 281272882Ssmh 282272882Ssmh return (0); 283272882Ssmh} 284260763Savg#endif 285185029Spjd 286168404Spjdint 287260763Savgvdev_queue_offset_compare(const void *x1, const void *x2) 288168404Spjd{ 289168404Spjd const zio_t *z1 = x1; 290168404Spjd const zio_t *z2 = x2; 291168404Spjd 292168404Spjd if (z1->io_offset < z2->io_offset) 293168404Spjd return (-1); 294168404Spjd if (z1->io_offset > z2->io_offset) 295168404Spjd return (1); 296168404Spjd 297168404Spjd if (z1 < z2) 298168404Spjd return (-1); 299168404Spjd if (z1 > z2) 300168404Spjd return (1); 301168404Spjd 302168404Spjd return (0); 303168404Spjd} 304168404Spjd 305168404Spjdint 306260763Savgvdev_queue_timestamp_compare(const void *x1, const void *x2) 307168404Spjd{ 308168404Spjd const zio_t *z1 = x1; 309168404Spjd const zio_t *z2 = x2; 310168404Spjd 311260763Savg if (z1->io_timestamp < z2->io_timestamp) 312168404Spjd return (-1); 313260763Savg if (z1->io_timestamp > z2->io_timestamp) 314168404Spjd return (1); 315168404Spjd 316277700Smav if (z1->io_offset < z2->io_offset) 317277700Smav return (-1); 318277700Smav if (z1->io_offset > z2->io_offset) 319277700Smav return (1); 320277700Smav 321168404Spjd if (z1 < z2) 322168404Spjd return (-1); 323168404Spjd if (z1 > z2) 324168404Spjd return (1); 325168404Spjd 326168404Spjd return (0); 327168404Spjd} 328168404Spjd 329168404Spjdvoid 330168404Spjdvdev_queue_init(vdev_t *vd) 331168404Spjd{ 332168404Spjd vdev_queue_t *vq = &vd->vdev_queue; 333168404Spjd 334168404Spjd mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL); 335260763Savg vq->vq_vdev = vd; 336168404Spjd 337260763Savg avl_create(&vq->vq_active_tree, vdev_queue_offset_compare, 338260763Savg sizeof (zio_t), offsetof(struct zio, io_queue_node)); 339168404Spjd 340260763Savg for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 341260763Savg /* 342260763Savg * The synchronous i/o queues are FIFO rather than LBA ordered. 343260763Savg * This provides more consistent latency for these i/os, and 344260763Savg * they tend to not be tightly clustered anyway so there is 345260763Savg * little to no throughput loss. 346260763Savg */ 347260763Savg boolean_t fifo = (p == ZIO_PRIORITY_SYNC_READ || 348260763Savg p == ZIO_PRIORITY_SYNC_WRITE); 349260763Savg avl_create(&vq->vq_class[p].vqc_queued_tree, 350260763Savg fifo ? vdev_queue_timestamp_compare : 351260763Savg vdev_queue_offset_compare, 352260763Savg sizeof (zio_t), offsetof(struct zio, io_queue_node)); 353260763Savg } 354271238Ssmh 355271238Ssmh vq->vq_lastoffset = 0; 356168404Spjd} 357168404Spjd 358168404Spjdvoid 359168404Spjdvdev_queue_fini(vdev_t *vd) 360168404Spjd{ 361168404Spjd vdev_queue_t *vq = &vd->vdev_queue; 362168404Spjd 363260763Savg for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) 364260763Savg avl_destroy(&vq->vq_class[p].vqc_queued_tree); 365260763Savg avl_destroy(&vq->vq_active_tree); 366168404Spjd 367168404Spjd mutex_destroy(&vq->vq_lock); 368168404Spjd} 369168404Spjd 370168404Spjdstatic void 371168404Spjdvdev_queue_io_add(vdev_queue_t *vq, zio_t *zio) 372168404Spjd{ 373260763Savg spa_t *spa = zio->io_spa; 374270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 375260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 376260763Savg avl_add(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio); 377260763Savg 378260763Savg#ifdef illumos 379260763Savg mutex_enter(&spa->spa_iokstat_lock); 380260763Savg spa->spa_queue_stats[zio->io_priority].spa_queued++; 381260763Savg if (spa->spa_iokstat != NULL) 382260763Savg kstat_waitq_enter(spa->spa_iokstat->ks_data); 383260763Savg mutex_exit(&spa->spa_iokstat_lock); 384260763Savg#endif 385168404Spjd} 386168404Spjd 387168404Spjdstatic void 388168404Spjdvdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio) 389168404Spjd{ 390260763Savg spa_t *spa = zio->io_spa; 391270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 392260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 393260763Savg avl_remove(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio); 394260763Savg 395260763Savg#ifdef illumos 396260763Savg mutex_enter(&spa->spa_iokstat_lock); 397260763Savg ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0); 398260763Savg spa->spa_queue_stats[zio->io_priority].spa_queued--; 399260763Savg if (spa->spa_iokstat != NULL) 400260763Savg kstat_waitq_exit(spa->spa_iokstat->ks_data); 401260763Savg mutex_exit(&spa->spa_iokstat_lock); 402260763Savg#endif 403168404Spjd} 404168404Spjd 405168404Spjdstatic void 406260763Savgvdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio) 407260763Savg{ 408260763Savg spa_t *spa = zio->io_spa; 409260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 410260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 411260763Savg vq->vq_class[zio->io_priority].vqc_active++; 412260763Savg avl_add(&vq->vq_active_tree, zio); 413260763Savg 414260763Savg#ifdef illumos 415260763Savg mutex_enter(&spa->spa_iokstat_lock); 416260763Savg spa->spa_queue_stats[zio->io_priority].spa_active++; 417260763Savg if (spa->spa_iokstat != NULL) 418260763Savg kstat_runq_enter(spa->spa_iokstat->ks_data); 419260763Savg mutex_exit(&spa->spa_iokstat_lock); 420260763Savg#endif 421260763Savg} 422260763Savg 423260763Savgstatic void 424260763Savgvdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio) 425260763Savg{ 426260763Savg spa_t *spa = zio->io_spa; 427260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 428260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 429260763Savg vq->vq_class[zio->io_priority].vqc_active--; 430260763Savg avl_remove(&vq->vq_active_tree, zio); 431260763Savg 432260763Savg#ifdef illumos 433260763Savg mutex_enter(&spa->spa_iokstat_lock); 434260763Savg ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0); 435260763Savg spa->spa_queue_stats[zio->io_priority].spa_active--; 436260763Savg if (spa->spa_iokstat != NULL) { 437260763Savg kstat_io_t *ksio = spa->spa_iokstat->ks_data; 438260763Savg 439260763Savg kstat_runq_exit(spa->spa_iokstat->ks_data); 440260763Savg if (zio->io_type == ZIO_TYPE_READ) { 441260763Savg ksio->reads++; 442260763Savg ksio->nread += zio->io_size; 443260763Savg } else if (zio->io_type == ZIO_TYPE_WRITE) { 444260763Savg ksio->writes++; 445260763Savg ksio->nwritten += zio->io_size; 446260763Savg } 447260763Savg } 448260763Savg mutex_exit(&spa->spa_iokstat_lock); 449260763Savg#endif 450260763Savg} 451260763Savg 452260763Savgstatic void 453168404Spjdvdev_queue_agg_io_done(zio_t *aio) 454168404Spjd{ 455260763Savg if (aio->io_type == ZIO_TYPE_READ) { 456260763Savg zio_t *pio; 457260763Savg while ((pio = zio_walk_parents(aio)) != NULL) { 458209962Smm bcopy((char *)aio->io_data + (pio->io_offset - 459209962Smm aio->io_offset), pio->io_data, pio->io_size); 460260763Savg } 461260763Savg } 462168404Spjd 463168404Spjd zio_buf_free(aio->io_data, aio->io_size); 464168404Spjd} 465168404Spjd 466260763Savgstatic int 467260763Savgvdev_queue_class_min_active(zio_priority_t p) 468260763Savg{ 469260763Savg switch (p) { 470260763Savg case ZIO_PRIORITY_SYNC_READ: 471260763Savg return (zfs_vdev_sync_read_min_active); 472260763Savg case ZIO_PRIORITY_SYNC_WRITE: 473260763Savg return (zfs_vdev_sync_write_min_active); 474260763Savg case ZIO_PRIORITY_ASYNC_READ: 475260763Savg return (zfs_vdev_async_read_min_active); 476260763Savg case ZIO_PRIORITY_ASYNC_WRITE: 477260763Savg return (zfs_vdev_async_write_min_active); 478260763Savg case ZIO_PRIORITY_SCRUB: 479260763Savg return (zfs_vdev_scrub_min_active); 480270312Ssmh case ZIO_PRIORITY_TRIM: 481270312Ssmh return (zfs_vdev_trim_min_active); 482260763Savg default: 483260763Savg panic("invalid priority %u", p); 484260763Savg return (0); 485260763Savg } 486260763Savg} 487260763Savg 488260763Savgstatic int 489269418Sdelphijvdev_queue_max_async_writes(spa_t *spa) 490260763Savg{ 491260763Savg int writes; 492269418Sdelphij uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total; 493260763Savg uint64_t min_bytes = zfs_dirty_data_max * 494260763Savg zfs_vdev_async_write_active_min_dirty_percent / 100; 495260763Savg uint64_t max_bytes = zfs_dirty_data_max * 496260763Savg zfs_vdev_async_write_active_max_dirty_percent / 100; 497260763Savg 498269418Sdelphij /* 499269418Sdelphij * Sync tasks correspond to interactive user actions. To reduce the 500269418Sdelphij * execution time of those actions we push data out as fast as possible. 501269418Sdelphij */ 502269418Sdelphij if (spa_has_pending_synctask(spa)) { 503269418Sdelphij return (zfs_vdev_async_write_max_active); 504269418Sdelphij } 505269418Sdelphij 506260763Savg if (dirty < min_bytes) 507260763Savg return (zfs_vdev_async_write_min_active); 508260763Savg if (dirty > max_bytes) 509260763Savg return (zfs_vdev_async_write_max_active); 510260763Savg 511260763Savg /* 512260763Savg * linear interpolation: 513260763Savg * slope = (max_writes - min_writes) / (max_bytes - min_bytes) 514260763Savg * move right by min_bytes 515260763Savg * move up by min_writes 516260763Savg */ 517260763Savg writes = (dirty - min_bytes) * 518260763Savg (zfs_vdev_async_write_max_active - 519260763Savg zfs_vdev_async_write_min_active) / 520260763Savg (max_bytes - min_bytes) + 521260763Savg zfs_vdev_async_write_min_active; 522260763Savg ASSERT3U(writes, >=, zfs_vdev_async_write_min_active); 523260763Savg ASSERT3U(writes, <=, zfs_vdev_async_write_max_active); 524260763Savg return (writes); 525260763Savg} 526260763Savg 527260763Savgstatic int 528260763Savgvdev_queue_class_max_active(spa_t *spa, zio_priority_t p) 529260763Savg{ 530260763Savg switch (p) { 531260763Savg case ZIO_PRIORITY_SYNC_READ: 532260763Savg return (zfs_vdev_sync_read_max_active); 533260763Savg case ZIO_PRIORITY_SYNC_WRITE: 534260763Savg return (zfs_vdev_sync_write_max_active); 535260763Savg case ZIO_PRIORITY_ASYNC_READ: 536260763Savg return (zfs_vdev_async_read_max_active); 537260763Savg case ZIO_PRIORITY_ASYNC_WRITE: 538269418Sdelphij return (vdev_queue_max_async_writes(spa)); 539260763Savg case ZIO_PRIORITY_SCRUB: 540260763Savg return (zfs_vdev_scrub_max_active); 541270312Ssmh case ZIO_PRIORITY_TRIM: 542270312Ssmh return (zfs_vdev_trim_max_active); 543260763Savg default: 544260763Savg panic("invalid priority %u", p); 545260763Savg return (0); 546260763Savg } 547260763Savg} 548260763Savg 549209962Smm/* 550260763Savg * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if 551260763Savg * there is no eligible class. 552260763Savg */ 553260763Savgstatic zio_priority_t 554260763Savgvdev_queue_class_to_issue(vdev_queue_t *vq) 555260763Savg{ 556260763Savg spa_t *spa = vq->vq_vdev->vdev_spa; 557260763Savg zio_priority_t p; 558260763Savg 559270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 560270312Ssmh 561260763Savg if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active) 562260763Savg return (ZIO_PRIORITY_NUM_QUEUEABLE); 563260763Savg 564260763Savg /* find a queue that has not reached its minimum # outstanding i/os */ 565260763Savg for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 566260763Savg if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 && 567260763Savg vq->vq_class[p].vqc_active < 568260763Savg vdev_queue_class_min_active(p)) 569260763Savg return (p); 570260763Savg } 571260763Savg 572260763Savg /* 573260763Savg * If we haven't found a queue, look for one that hasn't reached its 574260763Savg * maximum # outstanding i/os. 575260763Savg */ 576260763Savg for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 577260763Savg if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 && 578260763Savg vq->vq_class[p].vqc_active < 579260763Savg vdev_queue_class_max_active(spa, p)) 580260763Savg return (p); 581260763Savg } 582260763Savg 583260763Savg /* No eligible queued i/os */ 584260763Savg return (ZIO_PRIORITY_NUM_QUEUEABLE); 585260763Savg} 586260763Savg 587260763Savg/* 588209962Smm * Compute the range spanned by two i/os, which is the endpoint of the last 589209962Smm * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset). 590209962Smm * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio); 591209962Smm * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0. 592209962Smm */ 593209962Smm#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset) 594209962Smm#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio)) 595168404Spjd 596168404Spjdstatic zio_t * 597260763Savgvdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio) 598168404Spjd{ 599260763Savg zio_t *first, *last, *aio, *dio, *mandatory, *nio; 600260763Savg uint64_t maxgap = 0; 601260763Savg uint64_t size; 602270312Ssmh boolean_t stretch; 603270312Ssmh avl_tree_t *t; 604270312Ssmh enum zio_flag flags; 605168404Spjd 606270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 607270312Ssmh 608260763Savg if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE) 609260763Savg return (NULL); 610168404Spjd 611260763Savg /* 612260763Savg * The synchronous i/o queues are not sorted by LBA, so we can't 613260763Savg * find adjacent i/os. These i/os tend to not be tightly clustered, 614260763Savg * or too large to aggregate, so this has little impact on performance. 615260763Savg */ 616260763Savg if (zio->io_priority == ZIO_PRIORITY_SYNC_READ || 617260763Savg zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) 618168404Spjd return (NULL); 619168404Spjd 620260763Savg first = last = zio; 621168404Spjd 622260763Savg if (zio->io_type == ZIO_TYPE_READ) 623260763Savg maxgap = zfs_vdev_read_gap_limit; 624168404Spjd 625260763Savg /* 626260763Savg * We can aggregate I/Os that are sufficiently adjacent and of 627260763Savg * the same flavor, as expressed by the AGG_INHERIT flags. 628260763Savg * The latter requirement is necessary so that certain 629260763Savg * attributes of the I/O, such as whether it's a normal I/O 630260763Savg * or a scrub/resilver, can be preserved in the aggregate. 631260763Savg * We can include optional I/Os, but don't allow them 632260763Savg * to begin a range as they add no benefit in that situation. 633260763Savg */ 634219089Spjd 635260763Savg /* 636260763Savg * We keep track of the last non-optional I/O. 637260763Savg */ 638260763Savg mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first; 639219089Spjd 640260763Savg /* 641260763Savg * Walk backwards through sufficiently contiguous I/Os 642260763Savg * recording the last non-option I/O. 643260763Savg */ 644270312Ssmh flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT; 645270312Ssmh t = &vq->vq_class[zio->io_priority].vqc_queued_tree; 646260763Savg while ((dio = AVL_PREV(t, first)) != NULL && 647260763Savg (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 648260763Savg IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit && 649260763Savg IO_GAP(dio, first) <= maxgap) { 650260763Savg first = dio; 651260763Savg if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL)) 652260763Savg mandatory = first; 653260763Savg } 654168404Spjd 655260763Savg /* 656260763Savg * Skip any initial optional I/Os. 657260763Savg */ 658260763Savg while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) { 659260763Savg first = AVL_NEXT(t, first); 660260763Savg ASSERT(first != NULL); 661260763Savg } 662219089Spjd 663260763Savg /* 664260763Savg * Walk forward through sufficiently contiguous I/Os. 665260763Savg */ 666260763Savg while ((dio = AVL_NEXT(t, last)) != NULL && 667260763Savg (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 668260763Savg IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit && 669260763Savg IO_GAP(last, dio) <= maxgap) { 670260763Savg last = dio; 671260763Savg if (!(last->io_flags & ZIO_FLAG_OPTIONAL)) 672260763Savg mandatory = last; 673260763Savg } 674219089Spjd 675260763Savg /* 676260763Savg * Now that we've established the range of the I/O aggregation 677260763Savg * we must decide what to do with trailing optional I/Os. 678260763Savg * For reads, there's nothing to do. While we are unable to 679260763Savg * aggregate further, it's possible that a trailing optional 680260763Savg * I/O would allow the underlying device to aggregate with 681260763Savg * subsequent I/Os. We must therefore determine if the next 682260763Savg * non-optional I/O is close enough to make aggregation 683260763Savg * worthwhile. 684260763Savg */ 685270312Ssmh stretch = B_FALSE; 686260763Savg if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) { 687260763Savg zio_t *nio = last; 688260763Savg while ((dio = AVL_NEXT(t, nio)) != NULL && 689260763Savg IO_GAP(nio, dio) == 0 && 690260763Savg IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) { 691260763Savg nio = dio; 692260763Savg if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) { 693260763Savg stretch = B_TRUE; 694260763Savg break; 695219089Spjd } 696219089Spjd } 697260763Savg } 698219089Spjd 699260763Savg if (stretch) { 700260763Savg /* This may be a no-op. */ 701260763Savg dio = AVL_NEXT(t, last); 702260763Savg dio->io_flags &= ~ZIO_FLAG_OPTIONAL; 703260763Savg } else { 704260763Savg while (last != mandatory && last != first) { 705260763Savg ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL); 706260763Savg last = AVL_PREV(t, last); 707260763Savg ASSERT(last != NULL); 708219089Spjd } 709168404Spjd } 710168404Spjd 711260763Savg if (first == last) 712260763Savg return (NULL); 713168404Spjd 714260763Savg size = IO_SPAN(first, last); 715260763Savg ASSERT3U(size, <=, zfs_vdev_aggregation_limit); 716168404Spjd 717260763Savg aio = zio_vdev_delegated_io(first->io_vd, first->io_offset, 718260763Savg zio_buf_alloc(size), size, first->io_type, zio->io_priority, 719260763Savg flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE, 720260763Savg vdev_queue_agg_io_done, NULL); 721260763Savg aio->io_timestamp = first->io_timestamp; 722168404Spjd 723260763Savg nio = first; 724260763Savg do { 725260763Savg dio = nio; 726260763Savg nio = AVL_NEXT(t, dio); 727260763Savg ASSERT3U(dio->io_type, ==, aio->io_type); 728209962Smm 729260763Savg if (dio->io_flags & ZIO_FLAG_NODATA) { 730260763Savg ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE); 731260763Savg bzero((char *)aio->io_data + (dio->io_offset - 732260763Savg aio->io_offset), dio->io_size); 733260763Savg } else if (dio->io_type == ZIO_TYPE_WRITE) { 734260763Savg bcopy(dio->io_data, (char *)aio->io_data + 735260763Savg (dio->io_offset - aio->io_offset), 736260763Savg dio->io_size); 737260763Savg } 738168404Spjd 739260763Savg zio_add_child(dio, aio); 740260763Savg vdev_queue_io_remove(vq, dio); 741260763Savg zio_vdev_io_bypass(dio); 742260763Savg zio_execute(dio); 743260763Savg } while (dio != last); 744168404Spjd 745260763Savg return (aio); 746260763Savg} 747260763Savg 748260763Savgstatic zio_t * 749260763Savgvdev_queue_io_to_issue(vdev_queue_t *vq) 750260763Savg{ 751260763Savg zio_t *zio, *aio; 752260763Savg zio_priority_t p; 753260763Savg avl_index_t idx; 754260763Savg vdev_queue_class_t *vqc; 755260763Savg zio_t search; 756260763Savg 757260763Savgagain: 758260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 759260763Savg 760260763Savg p = vdev_queue_class_to_issue(vq); 761260763Savg 762260763Savg if (p == ZIO_PRIORITY_NUM_QUEUEABLE) { 763260763Savg /* No eligible queued i/os */ 764260763Savg return (NULL); 765168404Spjd } 766168404Spjd 767260763Savg /* 768260763Savg * For LBA-ordered queues (async / scrub), issue the i/o which follows 769260763Savg * the most recently issued i/o in LBA (offset) order. 770260763Savg * 771260763Savg * For FIFO queues (sync), issue the i/o with the lowest timestamp. 772260763Savg */ 773260763Savg vqc = &vq->vq_class[p]; 774260763Savg search.io_timestamp = 0; 775260763Savg search.io_offset = vq->vq_last_offset + 1; 776260763Savg VERIFY3P(avl_find(&vqc->vqc_queued_tree, &search, &idx), ==, NULL); 777260763Savg zio = avl_nearest(&vqc->vqc_queued_tree, idx, AVL_AFTER); 778260763Savg if (zio == NULL) 779260763Savg zio = avl_first(&vqc->vqc_queued_tree); 780260763Savg ASSERT3U(zio->io_priority, ==, p); 781168404Spjd 782260763Savg aio = vdev_queue_aggregate(vq, zio); 783260763Savg if (aio != NULL) 784260763Savg zio = aio; 785260763Savg else 786260763Savg vdev_queue_io_remove(vq, zio); 787260763Savg 788219089Spjd /* 789219089Spjd * If the I/O is or was optional and therefore has no data, we need to 790219089Spjd * simply discard it. We need to drop the vdev queue's lock to avoid a 791219089Spjd * deadlock that we could encounter since this I/O will complete 792219089Spjd * immediately. 793219089Spjd */ 794260763Savg if (zio->io_flags & ZIO_FLAG_NODATA) { 795219089Spjd mutex_exit(&vq->vq_lock); 796260763Savg zio_vdev_io_bypass(zio); 797260763Savg zio_execute(zio); 798219089Spjd mutex_enter(&vq->vq_lock); 799219089Spjd goto again; 800219089Spjd } 801219089Spjd 802260763Savg vdev_queue_pending_add(vq, zio); 803260763Savg vq->vq_last_offset = zio->io_offset; 804168404Spjd 805260763Savg return (zio); 806168404Spjd} 807168404Spjd 808168404Spjdzio_t * 809168404Spjdvdev_queue_io(zio_t *zio) 810168404Spjd{ 811168404Spjd vdev_queue_t *vq = &zio->io_vd->vdev_queue; 812168404Spjd zio_t *nio; 813168404Spjd 814168404Spjd if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 815168404Spjd return (zio); 816168404Spjd 817260763Savg /* 818260763Savg * Children i/os inherent their parent's priority, which might 819260763Savg * not match the child's i/o type. Fix it up here. 820260763Savg */ 821260763Savg if (zio->io_type == ZIO_TYPE_READ) { 822260763Savg if (zio->io_priority != ZIO_PRIORITY_SYNC_READ && 823260763Savg zio->io_priority != ZIO_PRIORITY_ASYNC_READ && 824260763Savg zio->io_priority != ZIO_PRIORITY_SCRUB) 825260763Savg zio->io_priority = ZIO_PRIORITY_ASYNC_READ; 826270312Ssmh } else if (zio->io_type == ZIO_TYPE_WRITE) { 827260763Savg if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 828260763Savg zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE) 829260763Savg zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE; 830270312Ssmh } else { 831270312Ssmh ASSERT(zio->io_type == ZIO_TYPE_FREE); 832270312Ssmh zio->io_priority = ZIO_PRIORITY_TRIM; 833260763Savg } 834260763Savg 835168404Spjd zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 836168404Spjd 837168404Spjd mutex_enter(&vq->vq_lock); 838249206Smm zio->io_timestamp = gethrtime(); 839168404Spjd vdev_queue_io_add(vq, zio); 840260763Savg nio = vdev_queue_io_to_issue(vq); 841168404Spjd mutex_exit(&vq->vq_lock); 842168404Spjd 843185029Spjd if (nio == NULL) 844185029Spjd return (NULL); 845168404Spjd 846185029Spjd if (nio->io_done == vdev_queue_agg_io_done) { 847185029Spjd zio_nowait(nio); 848185029Spjd return (NULL); 849185029Spjd } 850185029Spjd 851185029Spjd return (nio); 852168404Spjd} 853168404Spjd 854168404Spjdvoid 855168404Spjdvdev_queue_io_done(zio_t *zio) 856168404Spjd{ 857168404Spjd vdev_queue_t *vq = &zio->io_vd->vdev_queue; 858260763Savg zio_t *nio; 859168404Spjd 860247265Smm if (zio_injection_enabled) 861247265Smm delay(SEC_TO_TICK(zio_handle_io_delay(zio))); 862247265Smm 863168404Spjd mutex_enter(&vq->vq_lock); 864168404Spjd 865260763Savg vdev_queue_pending_remove(vq, zio); 866168404Spjd 867249206Smm vq->vq_io_complete_ts = gethrtime(); 868247265Smm 869260763Savg while ((nio = vdev_queue_io_to_issue(vq)) != NULL) { 870168404Spjd mutex_exit(&vq->vq_lock); 871185029Spjd if (nio->io_done == vdev_queue_agg_io_done) { 872185029Spjd zio_nowait(nio); 873185029Spjd } else { 874168404Spjd zio_vdev_io_reissue(nio); 875185029Spjd zio_execute(nio); 876185029Spjd } 877168404Spjd mutex_enter(&vq->vq_lock); 878168404Spjd } 879168404Spjd 880168404Spjd mutex_exit(&vq->vq_lock); 881168404Spjd} 882271238Ssmh 883271238Ssmh/* 884271238Ssmh * As these three methods are only used for load calculations we're not concerned 885271238Ssmh * if we get an incorrect value on 32bit platforms due to lack of vq_lock mutex 886271238Ssmh * use here, instead we prefer to keep it lock free for performance. 887271238Ssmh */ 888271238Ssmhint 889271238Ssmhvdev_queue_length(vdev_t *vd) 890271238Ssmh{ 891271238Ssmh return (avl_numnodes(&vd->vdev_queue.vq_active_tree)); 892271238Ssmh} 893271238Ssmh 894271238Ssmhuint64_t 895271238Ssmhvdev_queue_lastoffset(vdev_t *vd) 896271238Ssmh{ 897271238Ssmh return (vd->vdev_queue.vq_lastoffset); 898271238Ssmh} 899271238Ssmh 900271238Ssmhvoid 901271238Ssmhvdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio) 902271238Ssmh{ 903271238Ssmh vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size; 904271238Ssmh} 905