vdev_queue.c revision 276081
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 316168404Spjd if (z1 < z2) 317168404Spjd return (-1); 318168404Spjd if (z1 > z2) 319168404Spjd return (1); 320168404Spjd 321168404Spjd return (0); 322168404Spjd} 323168404Spjd 324168404Spjdvoid 325168404Spjdvdev_queue_init(vdev_t *vd) 326168404Spjd{ 327168404Spjd vdev_queue_t *vq = &vd->vdev_queue; 328168404Spjd 329168404Spjd mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL); 330260763Savg vq->vq_vdev = vd; 331168404Spjd 332260763Savg avl_create(&vq->vq_active_tree, vdev_queue_offset_compare, 333260763Savg sizeof (zio_t), offsetof(struct zio, io_queue_node)); 334168404Spjd 335260763Savg for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 336260763Savg /* 337260763Savg * The synchronous i/o queues are FIFO rather than LBA ordered. 338260763Savg * This provides more consistent latency for these i/os, and 339260763Savg * they tend to not be tightly clustered anyway so there is 340260763Savg * little to no throughput loss. 341260763Savg */ 342260763Savg boolean_t fifo = (p == ZIO_PRIORITY_SYNC_READ || 343260763Savg p == ZIO_PRIORITY_SYNC_WRITE); 344260763Savg avl_create(&vq->vq_class[p].vqc_queued_tree, 345260763Savg fifo ? vdev_queue_timestamp_compare : 346260763Savg vdev_queue_offset_compare, 347260763Savg sizeof (zio_t), offsetof(struct zio, io_queue_node)); 348260763Savg } 349271238Ssmh 350271238Ssmh vq->vq_lastoffset = 0; 351168404Spjd} 352168404Spjd 353168404Spjdvoid 354168404Spjdvdev_queue_fini(vdev_t *vd) 355168404Spjd{ 356168404Spjd vdev_queue_t *vq = &vd->vdev_queue; 357168404Spjd 358260763Savg for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) 359260763Savg avl_destroy(&vq->vq_class[p].vqc_queued_tree); 360260763Savg avl_destroy(&vq->vq_active_tree); 361168404Spjd 362168404Spjd mutex_destroy(&vq->vq_lock); 363168404Spjd} 364168404Spjd 365168404Spjdstatic void 366168404Spjdvdev_queue_io_add(vdev_queue_t *vq, zio_t *zio) 367168404Spjd{ 368260763Savg spa_t *spa = zio->io_spa; 369270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 370260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 371260763Savg avl_add(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio); 372260763Savg 373260763Savg#ifdef illumos 374260763Savg mutex_enter(&spa->spa_iokstat_lock); 375260763Savg spa->spa_queue_stats[zio->io_priority].spa_queued++; 376260763Savg if (spa->spa_iokstat != NULL) 377260763Savg kstat_waitq_enter(spa->spa_iokstat->ks_data); 378260763Savg mutex_exit(&spa->spa_iokstat_lock); 379260763Savg#endif 380168404Spjd} 381168404Spjd 382168404Spjdstatic void 383168404Spjdvdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio) 384168404Spjd{ 385260763Savg spa_t *spa = zio->io_spa; 386270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 387260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 388260763Savg avl_remove(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio); 389260763Savg 390260763Savg#ifdef illumos 391260763Savg mutex_enter(&spa->spa_iokstat_lock); 392260763Savg ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0); 393260763Savg spa->spa_queue_stats[zio->io_priority].spa_queued--; 394260763Savg if (spa->spa_iokstat != NULL) 395260763Savg kstat_waitq_exit(spa->spa_iokstat->ks_data); 396260763Savg mutex_exit(&spa->spa_iokstat_lock); 397260763Savg#endif 398168404Spjd} 399168404Spjd 400168404Spjdstatic void 401260763Savgvdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio) 402260763Savg{ 403260763Savg spa_t *spa = zio->io_spa; 404260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 405260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 406260763Savg vq->vq_class[zio->io_priority].vqc_active++; 407260763Savg avl_add(&vq->vq_active_tree, zio); 408260763Savg 409260763Savg#ifdef illumos 410260763Savg mutex_enter(&spa->spa_iokstat_lock); 411260763Savg spa->spa_queue_stats[zio->io_priority].spa_active++; 412260763Savg if (spa->spa_iokstat != NULL) 413260763Savg kstat_runq_enter(spa->spa_iokstat->ks_data); 414260763Savg mutex_exit(&spa->spa_iokstat_lock); 415260763Savg#endif 416260763Savg} 417260763Savg 418260763Savgstatic void 419260763Savgvdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio) 420260763Savg{ 421260763Savg spa_t *spa = zio->io_spa; 422260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 423260763Savg ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 424260763Savg vq->vq_class[zio->io_priority].vqc_active--; 425260763Savg avl_remove(&vq->vq_active_tree, zio); 426260763Savg 427260763Savg#ifdef illumos 428260763Savg mutex_enter(&spa->spa_iokstat_lock); 429260763Savg ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0); 430260763Savg spa->spa_queue_stats[zio->io_priority].spa_active--; 431260763Savg if (spa->spa_iokstat != NULL) { 432260763Savg kstat_io_t *ksio = spa->spa_iokstat->ks_data; 433260763Savg 434260763Savg kstat_runq_exit(spa->spa_iokstat->ks_data); 435260763Savg if (zio->io_type == ZIO_TYPE_READ) { 436260763Savg ksio->reads++; 437260763Savg ksio->nread += zio->io_size; 438260763Savg } else if (zio->io_type == ZIO_TYPE_WRITE) { 439260763Savg ksio->writes++; 440260763Savg ksio->nwritten += zio->io_size; 441260763Savg } 442260763Savg } 443260763Savg mutex_exit(&spa->spa_iokstat_lock); 444260763Savg#endif 445260763Savg} 446260763Savg 447260763Savgstatic void 448168404Spjdvdev_queue_agg_io_done(zio_t *aio) 449168404Spjd{ 450260763Savg if (aio->io_type == ZIO_TYPE_READ) { 451260763Savg zio_t *pio; 452260763Savg while ((pio = zio_walk_parents(aio)) != NULL) { 453209962Smm bcopy((char *)aio->io_data + (pio->io_offset - 454209962Smm aio->io_offset), pio->io_data, pio->io_size); 455260763Savg } 456260763Savg } 457168404Spjd 458168404Spjd zio_buf_free(aio->io_data, aio->io_size); 459168404Spjd} 460168404Spjd 461260763Savgstatic int 462260763Savgvdev_queue_class_min_active(zio_priority_t p) 463260763Savg{ 464260763Savg switch (p) { 465260763Savg case ZIO_PRIORITY_SYNC_READ: 466260763Savg return (zfs_vdev_sync_read_min_active); 467260763Savg case ZIO_PRIORITY_SYNC_WRITE: 468260763Savg return (zfs_vdev_sync_write_min_active); 469260763Savg case ZIO_PRIORITY_ASYNC_READ: 470260763Savg return (zfs_vdev_async_read_min_active); 471260763Savg case ZIO_PRIORITY_ASYNC_WRITE: 472260763Savg return (zfs_vdev_async_write_min_active); 473260763Savg case ZIO_PRIORITY_SCRUB: 474260763Savg return (zfs_vdev_scrub_min_active); 475270312Ssmh case ZIO_PRIORITY_TRIM: 476270312Ssmh return (zfs_vdev_trim_min_active); 477260763Savg default: 478260763Savg panic("invalid priority %u", p); 479260763Savg return (0); 480260763Savg } 481260763Savg} 482260763Savg 483260763Savgstatic int 484269418Sdelphijvdev_queue_max_async_writes(spa_t *spa) 485260763Savg{ 486260763Savg int writes; 487269418Sdelphij uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total; 488260763Savg uint64_t min_bytes = zfs_dirty_data_max * 489260763Savg zfs_vdev_async_write_active_min_dirty_percent / 100; 490260763Savg uint64_t max_bytes = zfs_dirty_data_max * 491260763Savg zfs_vdev_async_write_active_max_dirty_percent / 100; 492260763Savg 493269418Sdelphij /* 494269418Sdelphij * Sync tasks correspond to interactive user actions. To reduce the 495269418Sdelphij * execution time of those actions we push data out as fast as possible. 496269418Sdelphij */ 497269418Sdelphij if (spa_has_pending_synctask(spa)) { 498269418Sdelphij return (zfs_vdev_async_write_max_active); 499269418Sdelphij } 500269418Sdelphij 501260763Savg if (dirty < min_bytes) 502260763Savg return (zfs_vdev_async_write_min_active); 503260763Savg if (dirty > max_bytes) 504260763Savg return (zfs_vdev_async_write_max_active); 505260763Savg 506260763Savg /* 507260763Savg * linear interpolation: 508260763Savg * slope = (max_writes - min_writes) / (max_bytes - min_bytes) 509260763Savg * move right by min_bytes 510260763Savg * move up by min_writes 511260763Savg */ 512260763Savg writes = (dirty - min_bytes) * 513260763Savg (zfs_vdev_async_write_max_active - 514260763Savg zfs_vdev_async_write_min_active) / 515260763Savg (max_bytes - min_bytes) + 516260763Savg zfs_vdev_async_write_min_active; 517260763Savg ASSERT3U(writes, >=, zfs_vdev_async_write_min_active); 518260763Savg ASSERT3U(writes, <=, zfs_vdev_async_write_max_active); 519260763Savg return (writes); 520260763Savg} 521260763Savg 522260763Savgstatic int 523260763Savgvdev_queue_class_max_active(spa_t *spa, zio_priority_t p) 524260763Savg{ 525260763Savg switch (p) { 526260763Savg case ZIO_PRIORITY_SYNC_READ: 527260763Savg return (zfs_vdev_sync_read_max_active); 528260763Savg case ZIO_PRIORITY_SYNC_WRITE: 529260763Savg return (zfs_vdev_sync_write_max_active); 530260763Savg case ZIO_PRIORITY_ASYNC_READ: 531260763Savg return (zfs_vdev_async_read_max_active); 532260763Savg case ZIO_PRIORITY_ASYNC_WRITE: 533269418Sdelphij return (vdev_queue_max_async_writes(spa)); 534260763Savg case ZIO_PRIORITY_SCRUB: 535260763Savg return (zfs_vdev_scrub_max_active); 536270312Ssmh case ZIO_PRIORITY_TRIM: 537270312Ssmh return (zfs_vdev_trim_max_active); 538260763Savg default: 539260763Savg panic("invalid priority %u", p); 540260763Savg return (0); 541260763Savg } 542260763Savg} 543260763Savg 544209962Smm/* 545260763Savg * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if 546260763Savg * there is no eligible class. 547260763Savg */ 548260763Savgstatic zio_priority_t 549260763Savgvdev_queue_class_to_issue(vdev_queue_t *vq) 550260763Savg{ 551260763Savg spa_t *spa = vq->vq_vdev->vdev_spa; 552260763Savg zio_priority_t p; 553260763Savg 554270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 555270312Ssmh 556260763Savg if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active) 557260763Savg return (ZIO_PRIORITY_NUM_QUEUEABLE); 558260763Savg 559260763Savg /* find a queue that has not reached its minimum # outstanding i/os */ 560260763Savg for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 561260763Savg if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 && 562260763Savg vq->vq_class[p].vqc_active < 563260763Savg vdev_queue_class_min_active(p)) 564260763Savg return (p); 565260763Savg } 566260763Savg 567260763Savg /* 568260763Savg * If we haven't found a queue, look for one that hasn't reached its 569260763Savg * maximum # outstanding i/os. 570260763Savg */ 571260763Savg for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 572260763Savg if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 && 573260763Savg vq->vq_class[p].vqc_active < 574260763Savg vdev_queue_class_max_active(spa, p)) 575260763Savg return (p); 576260763Savg } 577260763Savg 578260763Savg /* No eligible queued i/os */ 579260763Savg return (ZIO_PRIORITY_NUM_QUEUEABLE); 580260763Savg} 581260763Savg 582260763Savg/* 583209962Smm * Compute the range spanned by two i/os, which is the endpoint of the last 584209962Smm * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset). 585209962Smm * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio); 586209962Smm * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0. 587209962Smm */ 588209962Smm#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset) 589209962Smm#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio)) 590168404Spjd 591168404Spjdstatic zio_t * 592260763Savgvdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio) 593168404Spjd{ 594260763Savg zio_t *first, *last, *aio, *dio, *mandatory, *nio; 595260763Savg uint64_t maxgap = 0; 596260763Savg uint64_t size; 597270312Ssmh boolean_t stretch; 598270312Ssmh avl_tree_t *t; 599270312Ssmh enum zio_flag flags; 600168404Spjd 601270312Ssmh ASSERT(MUTEX_HELD(&vq->vq_lock)); 602270312Ssmh 603260763Savg if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE) 604260763Savg return (NULL); 605168404Spjd 606260763Savg /* 607260763Savg * The synchronous i/o queues are not sorted by LBA, so we can't 608260763Savg * find adjacent i/os. These i/os tend to not be tightly clustered, 609260763Savg * or too large to aggregate, so this has little impact on performance. 610260763Savg */ 611260763Savg if (zio->io_priority == ZIO_PRIORITY_SYNC_READ || 612260763Savg zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) 613168404Spjd return (NULL); 614168404Spjd 615260763Savg first = last = zio; 616168404Spjd 617260763Savg if (zio->io_type == ZIO_TYPE_READ) 618260763Savg maxgap = zfs_vdev_read_gap_limit; 619168404Spjd 620260763Savg /* 621260763Savg * We can aggregate I/Os that are sufficiently adjacent and of 622260763Savg * the same flavor, as expressed by the AGG_INHERIT flags. 623260763Savg * The latter requirement is necessary so that certain 624260763Savg * attributes of the I/O, such as whether it's a normal I/O 625260763Savg * or a scrub/resilver, can be preserved in the aggregate. 626260763Savg * We can include optional I/Os, but don't allow them 627260763Savg * to begin a range as they add no benefit in that situation. 628260763Savg */ 629219089Spjd 630260763Savg /* 631260763Savg * We keep track of the last non-optional I/O. 632260763Savg */ 633260763Savg mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first; 634219089Spjd 635260763Savg /* 636260763Savg * Walk backwards through sufficiently contiguous I/Os 637260763Savg * recording the last non-option I/O. 638260763Savg */ 639270312Ssmh flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT; 640270312Ssmh t = &vq->vq_class[zio->io_priority].vqc_queued_tree; 641260763Savg while ((dio = AVL_PREV(t, first)) != NULL && 642260763Savg (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 643260763Savg IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit && 644260763Savg IO_GAP(dio, first) <= maxgap) { 645260763Savg first = dio; 646260763Savg if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL)) 647260763Savg mandatory = first; 648260763Savg } 649168404Spjd 650260763Savg /* 651260763Savg * Skip any initial optional I/Os. 652260763Savg */ 653260763Savg while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) { 654260763Savg first = AVL_NEXT(t, first); 655260763Savg ASSERT(first != NULL); 656260763Savg } 657219089Spjd 658260763Savg /* 659260763Savg * Walk forward through sufficiently contiguous I/Os. 660260763Savg */ 661260763Savg while ((dio = AVL_NEXT(t, last)) != NULL && 662260763Savg (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 663260763Savg IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit && 664260763Savg IO_GAP(last, dio) <= maxgap) { 665260763Savg last = dio; 666260763Savg if (!(last->io_flags & ZIO_FLAG_OPTIONAL)) 667260763Savg mandatory = last; 668260763Savg } 669219089Spjd 670260763Savg /* 671260763Savg * Now that we've established the range of the I/O aggregation 672260763Savg * we must decide what to do with trailing optional I/Os. 673260763Savg * For reads, there's nothing to do. While we are unable to 674260763Savg * aggregate further, it's possible that a trailing optional 675260763Savg * I/O would allow the underlying device to aggregate with 676260763Savg * subsequent I/Os. We must therefore determine if the next 677260763Savg * non-optional I/O is close enough to make aggregation 678260763Savg * worthwhile. 679260763Savg */ 680270312Ssmh stretch = B_FALSE; 681260763Savg if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) { 682260763Savg zio_t *nio = last; 683260763Savg while ((dio = AVL_NEXT(t, nio)) != NULL && 684260763Savg IO_GAP(nio, dio) == 0 && 685260763Savg IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) { 686260763Savg nio = dio; 687260763Savg if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) { 688260763Savg stretch = B_TRUE; 689260763Savg break; 690219089Spjd } 691219089Spjd } 692260763Savg } 693219089Spjd 694260763Savg if (stretch) { 695260763Savg /* This may be a no-op. */ 696260763Savg dio = AVL_NEXT(t, last); 697260763Savg dio->io_flags &= ~ZIO_FLAG_OPTIONAL; 698260763Savg } else { 699260763Savg while (last != mandatory && last != first) { 700260763Savg ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL); 701260763Savg last = AVL_PREV(t, last); 702260763Savg ASSERT(last != NULL); 703219089Spjd } 704168404Spjd } 705168404Spjd 706260763Savg if (first == last) 707260763Savg return (NULL); 708168404Spjd 709260763Savg size = IO_SPAN(first, last); 710260763Savg ASSERT3U(size, <=, zfs_vdev_aggregation_limit); 711168404Spjd 712260763Savg aio = zio_vdev_delegated_io(first->io_vd, first->io_offset, 713260763Savg zio_buf_alloc(size), size, first->io_type, zio->io_priority, 714260763Savg flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE, 715260763Savg vdev_queue_agg_io_done, NULL); 716260763Savg aio->io_timestamp = first->io_timestamp; 717168404Spjd 718260763Savg nio = first; 719260763Savg do { 720260763Savg dio = nio; 721260763Savg nio = AVL_NEXT(t, dio); 722260763Savg ASSERT3U(dio->io_type, ==, aio->io_type); 723209962Smm 724260763Savg if (dio->io_flags & ZIO_FLAG_NODATA) { 725260763Savg ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE); 726260763Savg bzero((char *)aio->io_data + (dio->io_offset - 727260763Savg aio->io_offset), dio->io_size); 728260763Savg } else if (dio->io_type == ZIO_TYPE_WRITE) { 729260763Savg bcopy(dio->io_data, (char *)aio->io_data + 730260763Savg (dio->io_offset - aio->io_offset), 731260763Savg dio->io_size); 732260763Savg } 733168404Spjd 734260763Savg zio_add_child(dio, aio); 735260763Savg vdev_queue_io_remove(vq, dio); 736260763Savg zio_vdev_io_bypass(dio); 737260763Savg zio_execute(dio); 738260763Savg } while (dio != last); 739168404Spjd 740260763Savg return (aio); 741260763Savg} 742260763Savg 743260763Savgstatic zio_t * 744260763Savgvdev_queue_io_to_issue(vdev_queue_t *vq) 745260763Savg{ 746260763Savg zio_t *zio, *aio; 747260763Savg zio_priority_t p; 748260763Savg avl_index_t idx; 749260763Savg vdev_queue_class_t *vqc; 750260763Savg zio_t search; 751260763Savg 752260763Savgagain: 753260763Savg ASSERT(MUTEX_HELD(&vq->vq_lock)); 754260763Savg 755260763Savg p = vdev_queue_class_to_issue(vq); 756260763Savg 757260763Savg if (p == ZIO_PRIORITY_NUM_QUEUEABLE) { 758260763Savg /* No eligible queued i/os */ 759260763Savg return (NULL); 760168404Spjd } 761168404Spjd 762260763Savg /* 763260763Savg * For LBA-ordered queues (async / scrub), issue the i/o which follows 764260763Savg * the most recently issued i/o in LBA (offset) order. 765260763Savg * 766260763Savg * For FIFO queues (sync), issue the i/o with the lowest timestamp. 767260763Savg */ 768260763Savg vqc = &vq->vq_class[p]; 769260763Savg search.io_timestamp = 0; 770260763Savg search.io_offset = vq->vq_last_offset + 1; 771260763Savg VERIFY3P(avl_find(&vqc->vqc_queued_tree, &search, &idx), ==, NULL); 772260763Savg zio = avl_nearest(&vqc->vqc_queued_tree, idx, AVL_AFTER); 773260763Savg if (zio == NULL) 774260763Savg zio = avl_first(&vqc->vqc_queued_tree); 775260763Savg ASSERT3U(zio->io_priority, ==, p); 776168404Spjd 777260763Savg aio = vdev_queue_aggregate(vq, zio); 778260763Savg if (aio != NULL) 779260763Savg zio = aio; 780260763Savg else 781260763Savg vdev_queue_io_remove(vq, zio); 782260763Savg 783219089Spjd /* 784219089Spjd * If the I/O is or was optional and therefore has no data, we need to 785219089Spjd * simply discard it. We need to drop the vdev queue's lock to avoid a 786219089Spjd * deadlock that we could encounter since this I/O will complete 787219089Spjd * immediately. 788219089Spjd */ 789260763Savg if (zio->io_flags & ZIO_FLAG_NODATA) { 790219089Spjd mutex_exit(&vq->vq_lock); 791260763Savg zio_vdev_io_bypass(zio); 792260763Savg zio_execute(zio); 793219089Spjd mutex_enter(&vq->vq_lock); 794219089Spjd goto again; 795219089Spjd } 796219089Spjd 797260763Savg vdev_queue_pending_add(vq, zio); 798260763Savg vq->vq_last_offset = zio->io_offset; 799168404Spjd 800260763Savg return (zio); 801168404Spjd} 802168404Spjd 803168404Spjdzio_t * 804168404Spjdvdev_queue_io(zio_t *zio) 805168404Spjd{ 806168404Spjd vdev_queue_t *vq = &zio->io_vd->vdev_queue; 807168404Spjd zio_t *nio; 808168404Spjd 809168404Spjd if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 810168404Spjd return (zio); 811168404Spjd 812260763Savg /* 813260763Savg * Children i/os inherent their parent's priority, which might 814260763Savg * not match the child's i/o type. Fix it up here. 815260763Savg */ 816260763Savg if (zio->io_type == ZIO_TYPE_READ) { 817260763Savg if (zio->io_priority != ZIO_PRIORITY_SYNC_READ && 818260763Savg zio->io_priority != ZIO_PRIORITY_ASYNC_READ && 819260763Savg zio->io_priority != ZIO_PRIORITY_SCRUB) 820260763Savg zio->io_priority = ZIO_PRIORITY_ASYNC_READ; 821270312Ssmh } else if (zio->io_type == ZIO_TYPE_WRITE) { 822260763Savg if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 823260763Savg zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE) 824260763Savg zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE; 825270312Ssmh } else { 826270312Ssmh ASSERT(zio->io_type == ZIO_TYPE_FREE); 827270312Ssmh zio->io_priority = ZIO_PRIORITY_TRIM; 828260763Savg } 829260763Savg 830168404Spjd zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 831168404Spjd 832168404Spjd mutex_enter(&vq->vq_lock); 833249206Smm zio->io_timestamp = gethrtime(); 834168404Spjd vdev_queue_io_add(vq, zio); 835260763Savg nio = vdev_queue_io_to_issue(vq); 836168404Spjd mutex_exit(&vq->vq_lock); 837168404Spjd 838185029Spjd if (nio == NULL) 839185029Spjd return (NULL); 840168404Spjd 841185029Spjd if (nio->io_done == vdev_queue_agg_io_done) { 842185029Spjd zio_nowait(nio); 843185029Spjd return (NULL); 844185029Spjd } 845185029Spjd 846185029Spjd return (nio); 847168404Spjd} 848168404Spjd 849168404Spjdvoid 850168404Spjdvdev_queue_io_done(zio_t *zio) 851168404Spjd{ 852168404Spjd vdev_queue_t *vq = &zio->io_vd->vdev_queue; 853260763Savg zio_t *nio; 854168404Spjd 855247265Smm if (zio_injection_enabled) 856247265Smm delay(SEC_TO_TICK(zio_handle_io_delay(zio))); 857247265Smm 858168404Spjd mutex_enter(&vq->vq_lock); 859168404Spjd 860260763Savg vdev_queue_pending_remove(vq, zio); 861168404Spjd 862249206Smm vq->vq_io_complete_ts = gethrtime(); 863247265Smm 864260763Savg while ((nio = vdev_queue_io_to_issue(vq)) != NULL) { 865168404Spjd mutex_exit(&vq->vq_lock); 866185029Spjd if (nio->io_done == vdev_queue_agg_io_done) { 867185029Spjd zio_nowait(nio); 868185029Spjd } else { 869168404Spjd zio_vdev_io_reissue(nio); 870185029Spjd zio_execute(nio); 871185029Spjd } 872168404Spjd mutex_enter(&vq->vq_lock); 873168404Spjd } 874168404Spjd 875168404Spjd mutex_exit(&vq->vq_lock); 876168404Spjd} 877271238Ssmh 878271238Ssmh/* 879271238Ssmh * As these three methods are only used for load calculations we're not concerned 880271238Ssmh * if we get an incorrect value on 32bit platforms due to lack of vq_lock mutex 881271238Ssmh * use here, instead we prefer to keep it lock free for performance. 882271238Ssmh */ 883271238Ssmhint 884271238Ssmhvdev_queue_length(vdev_t *vd) 885271238Ssmh{ 886271238Ssmh return (avl_numnodes(&vd->vdev_queue.vq_active_tree)); 887271238Ssmh} 888271238Ssmh 889271238Ssmhuint64_t 890271238Ssmhvdev_queue_lastoffset(vdev_t *vd) 891271238Ssmh{ 892271238Ssmh return (vd->vdev_queue.vq_lastoffset); 893271238Ssmh} 894271238Ssmh 895271238Ssmhvoid 896271238Ssmhvdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio) 897271238Ssmh{ 898271238Ssmh vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size; 899271238Ssmh} 900