vdev_queue.c revision 97e81309571898df9fdd94aab1216dfcf23e060b
4496171313bed39e96f21bc2f9faf2868e267ae3girish/*
4496171313bed39e96f21bc2f9faf2868e267ae3girish * CDDL HEADER START
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * The contents of this file are subject to the terms of the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Common Development and Distribution License (the "License").
4496171313bed39e96f21bc2f9faf2868e267ae3girish * You may not use this file except in compliance with the License.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
4496171313bed39e96f21bc2f9faf2868e267ae3girish * or http://www.opensolaris.org/os/licensing.
4496171313bed39e96f21bc2f9faf2868e267ae3girish * See the License for the specific language governing permissions
4496171313bed39e96f21bc2f9faf2868e267ae3girish * and limitations under the License.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * When distributing Covered Code, include this CDDL HEADER in each
4496171313bed39e96f21bc2f9faf2868e267ae3girish * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
4496171313bed39e96f21bc2f9faf2868e267ae3girish * If applicable, add the following below this CDDL HEADER, with the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * fields enclosed by brackets "[]" replaced with your own identifying
4496171313bed39e96f21bc2f9faf2868e267ae3girish * information: Portions Copyright [yyyy] [name of copyright owner]
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * CDDL HEADER END
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda/*
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * Use is subject to license terms.
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish/*
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish#include <sys/zfs_context.h>
4496171313bed39e96f21bc2f9faf2868e267ae3girish#include <sys/vdev_impl.h>
4496171313bed39e96f21bc2f9faf2868e267ae3girish#include <sys/spa_impl.h>
4496171313bed39e96f21bc2f9faf2868e267ae3girish#include <sys/zio.h>
4496171313bed39e96f21bc2f9faf2868e267ae3girish#include <sys/avl.h>
4496171313bed39e96f21bc2f9faf2868e267ae3girish#include <sys/dsl_pool.h>
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish/*
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * ZFS I/O Scheduler
4496171313bed39e96f21bc2f9faf2868e267ae3girish * ---------------
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
4496171313bed39e96f21bc2f9faf2868e267ae3girish * I/O scheduler determines when and in what order those operations are
4496171313bed39e96f21bc2f9faf2868e267ae3girish * issued. The I/O scheduler divides operations into five I/O classes
4496171313bed39e96f21bc2f9faf2868e267ae3girish * prioritized in the following order: sync read, sync write, async read,
4496171313bed39e96f21bc2f9faf2868e267ae3girish * async write, and scrub/resilver. Each queue defines the minimum and
4496171313bed39e96f21bc2f9faf2868e267ae3girish * maximum number of concurrent operations that may be issued to the device.
4496171313bed39e96f21bc2f9faf2868e267ae3girish * In addition, the device has an aggregate maximum. Note that the sum of the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * per-queue minimums must not exceed the aggregate maximum, and if the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * aggregate maximum is equal to or greater than the sum of the per-queue
4496171313bed39e96f21bc2f9faf2868e267ae3girish * maximums, the per-queue minimum has no effect.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * For many physical devices, throughput increases with the number of
4496171313bed39e96f21bc2f9faf2868e267ae3girish * concurrent operations, but latency typically suffers. Further, physical
4496171313bed39e96f21bc2f9faf2868e267ae3girish * devices typically have a limit at which more concurrent operations have no
4496171313bed39e96f21bc2f9faf2868e267ae3girish * effect on throughput or can actually cause it to decrease.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * The scheduler selects the next operation to issue by first looking for an
4496171313bed39e96f21bc2f9faf2868e267ae3girish * I/O class whose minimum has not been satisfied. Once all are satisfied and
4496171313bed39e96f21bc2f9faf2868e267ae3girish * the aggregate maximum has not been hit, the scheduler looks for classes
4496171313bed39e96f21bc2f9faf2868e267ae3girish * whose maximum has not been satisfied. Iteration through the I/O classes is
4496171313bed39e96f21bc2f9faf2868e267ae3girish * done in the order specified above. No further operations are issued if the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * aggregate maximum number of concurrent operations has been hit or if there
4496171313bed39e96f21bc2f9faf2868e267ae3girish * are no operations queued for an I/O class that has not hit its maximum.
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Every time an i/o is queued or an operation completes, the I/O scheduler
4496171313bed39e96f21bc2f9faf2868e267ae3girish * looks for new operations to issue.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * All I/O classes have a fixed maximum number of outstanding operations
4496171313bed39e96f21bc2f9faf2868e267ae3girish * except for the async write class. Asynchronous writes represent the data
4496171313bed39e96f21bc2f9faf2868e267ae3girish * that is committed to stable storage during the syncing stage for
4496171313bed39e96f21bc2f9faf2868e267ae3girish * transaction groups (see txg.c). Transaction groups enter the syncing state
4496171313bed39e96f21bc2f9faf2868e267ae3girish * periodically so the number of queued async writes will quickly burst up and
4496171313bed39e96f21bc2f9faf2868e267ae3girish * then bleed down to zero. Rather than servicing them as quickly as possible,
4496171313bed39e96f21bc2f9faf2868e267ae3girish * the I/O scheduler changes the maximum number of active async write i/os
4496171313bed39e96f21bc2f9faf2868e267ae3girish * according to the amount of dirty data in the pool (see dsl_pool.c). Since
4496171313bed39e96f21bc2f9faf2868e267ae3girish * both throughput and latency typically increase with the number of
4496171313bed39e96f21bc2f9faf2868e267ae3girish * concurrent operations issued to physical devices, reducing the burstiness
4496171313bed39e96f21bc2f9faf2868e267ae3girish * in the number of concurrent operations also stabilizes the response time of
4496171313bed39e96f21bc2f9faf2868e267ae3girish * operations from other -- and in particular synchronous -- queues. In broad
4496171313bed39e96f21bc2f9faf2868e267ae3girish * strokes, the I/O scheduler will issue more concurrent operations from the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * async write queue as there's more dirty data in the pool.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Async Writes
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * The number of concurrent operations issued for the async write I/O class
4496171313bed39e96f21bc2f9faf2868e267ae3girish * follows a piece-wise linear function defined by a few adjustable points.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * | o---------| <-- zfs_vdev_async_write_max_active
4496171313bed39e96f21bc2f9faf2868e267ae3girish * ^ | /^ |
4496171313bed39e96f21bc2f9faf2868e267ae3girish * | | / | |
4496171313bed39e96f21bc2f9faf2868e267ae3girish * active | / | |
4496171313bed39e96f21bc2f9faf2868e267ae3girish * I/O | / | |
4496171313bed39e96f21bc2f9faf2868e267ae3girish * count | / | |
4496171313bed39e96f21bc2f9faf2868e267ae3girish * | / | |
4496171313bed39e96f21bc2f9faf2868e267ae3girish * |------------o | | <-- zfs_vdev_async_write_min_active
4496171313bed39e96f21bc2f9faf2868e267ae3girish * 0|____________^______|_________|
4496171313bed39e96f21bc2f9faf2868e267ae3girish * 0% | | 100% of zfs_dirty_data_max
4496171313bed39e96f21bc2f9faf2868e267ae3girish * | |
4496171313bed39e96f21bc2f9faf2868e267ae3girish * | `-- zfs_vdev_async_write_active_max_dirty_percent
4496171313bed39e96f21bc2f9faf2868e267ae3girish * `--------- zfs_vdev_async_write_active_min_dirty_percent
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Until the amount of dirty data exceeds a minimum percentage of the dirty
4496171313bed39e96f21bc2f9faf2868e267ae3girish * data allowed in the pool, the I/O scheduler will limit the number of
4496171313bed39e96f21bc2f9faf2868e267ae3girish * concurrent operations to the minimum. As that threshold is crossed, the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * number of concurrent operations issued increases linearly to the maximum at
4496171313bed39e96f21bc2f9faf2868e267ae3girish * the specified maximum percentage of the dirty data allowed in the pool.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Ideally, the amount of dirty data on a busy pool will stay in the sloped
4496171313bed39e96f21bc2f9faf2868e267ae3girish * part of the function between zfs_vdev_async_write_active_min_dirty_percent
4496171313bed39e96f21bc2f9faf2868e267ae3girish * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * maximum percentage, this indicates that the rate of incoming data is
4496171313bed39e96f21bc2f9faf2868e267ae3girish * greater than the rate that the backend storage can handle. In this case, we
4496171313bed39e96f21bc2f9faf2868e267ae3girish * must further throttle incoming writes (see dmu_tx_delay() for details).
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish/*
4496171313bed39e96f21bc2f9faf2868e267ae3girish * The maximum number of i/os active to each device. Ideally, this will be >=
4496171313bed39e96f21bc2f9faf2868e267ae3girish * the sum of each queue's max_active. It must be at least the sum of each
4496171313bed39e96f21bc2f9faf2868e267ae3girish * queue's min_active.
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_max_active = 1000;
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish/*
4496171313bed39e96f21bc2f9faf2868e267ae3girish * Per-queue limits on the number of i/os active to each device. If the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * sum of the queue's max_active is < zfs_vdev_max_active, then the
4496171313bed39e96f21bc2f9faf2868e267ae3girish * min_active comes into play. We will send min_active from each queue,
4496171313bed39e96f21bc2f9faf2868e267ae3girish * and then select from queues in the order defined by zio_priority_t.
4496171313bed39e96f21bc2f9faf2868e267ae3girish *
4496171313bed39e96f21bc2f9faf2868e267ae3girish * In general, smaller max_active's will lead to lower latency of synchronous
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * operations. Larger max_active's may lead to higher overall throughput,
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * depending on underlying storage.
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp *
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * The ratio of the queues' max_actives determines the balance of performance
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * between reads, writes, and scrubs. E.g., increasing
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * more quickly, but reads and writes to have higher latency and lower
4496171313bed39e96f21bc2f9faf2868e267ae3girish * throughput.
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_sync_read_min_active = 10;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_sync_read_max_active = 10;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_sync_write_min_active = 10;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_sync_write_max_active = 10;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_async_read_min_active = 1;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_async_read_max_active = 3;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_async_write_min_active = 1;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_async_write_max_active = 10;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_scrub_min_active = 1;
4496171313bed39e96f21bc2f9faf2868e267ae3girishuint32_t zfs_vdev_scrub_max_active = 2;
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish/*
4496171313bed39e96f21bc2f9faf2868e267ae3girish * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
4496171313bed39e96f21bc2f9faf2868e267ae3girish * dirty data, use zfs_vdev_async_write_min_active. When it has more than
4496171313bed39e96f21bc2f9faf2868e267ae3girish * zfs_vdev_async_write_active_max_dirty_percent, use
4496171313bed39e96f21bc2f9faf2868e267ae3girish * zfs_vdev_async_write_max_active. The value is linearly interpolated
4496171313bed39e96f21bc2f9faf2868e267ae3girish * between min and max.
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
4496171313bed39e96f21bc2f9faf2868e267ae3girishint zfs_vdev_async_write_active_min_dirty_percent = 30;
4496171313bed39e96f21bc2f9faf2868e267ae3girishint zfs_vdev_async_write_active_max_dirty_percent = 60;
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda
4496171313bed39e96f21bc2f9faf2868e267ae3girish/*
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
4496171313bed39e96f21bc2f9faf2868e267ae3girish * For read I/Os, we also aggregate across small adjacency gaps; for writes
4496171313bed39e96f21bc2f9faf2868e267ae3girish * we include spans of optional I/Os to aid aggregation at the disk even when
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe * they aren't able to help us aggregate at this level.
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda */
459190a5c46206e7885f6a649a055ceb46be49a7rsmaedaint zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
4496171313bed39e96f21bc2f9faf2868e267ae3girishint zfs_vdev_read_gap_limit = 32 << 10;
4496171313bed39e96f21bc2f9faf2868e267ae3girishint zfs_vdev_write_gap_limit = 4 << 10;
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girishint
4496171313bed39e96f21bc2f9faf2868e267ae3girishvdev_queue_offset_compare(const void *x1, const void *x2)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe{
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe const zio_t *z1 = x1;
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe const zio_t *z2 = x2;
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe if (z1->io_offset < z2->io_offset)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (-1);
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe if (z1->io_offset > z2->io_offset)
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe return (1);
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda if (z1 < z2)
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda return (-1);
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda if (z1 > z2)
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda return (1);
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda return (0);
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda}
fb2f18f820d90b001aea4fb27dd654bc1263c440esaxe
4496171313bed39e96f21bc2f9faf2868e267ae3girishstatic inline avl_tree_t *
459190a5c46206e7885f6a649a055ceb46be49a7rsmaedavdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda{
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda return (&vq->vq_class[p].vqc_queued_tree);
4496171313bed39e96f21bc2f9faf2868e267ae3girish}
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda
4496171313bed39e96f21bc2f9faf2868e267ae3girishstatic inline avl_tree_t *
4496171313bed39e96f21bc2f9faf2868e267ae3girishvdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
4496171313bed39e96f21bc2f9faf2868e267ae3girish{
4496171313bed39e96f21bc2f9faf2868e267ae3girish ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
4496171313bed39e96f21bc2f9faf2868e267ae3girish if (t == ZIO_TYPE_READ)
4496171313bed39e96f21bc2f9faf2868e267ae3girish return (&vq->vq_read_offset_tree);
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda else
4496171313bed39e96f21bc2f9faf2868e267ae3girish return (&vq->vq_write_offset_tree);
4496171313bed39e96f21bc2f9faf2868e267ae3girish}
459190a5c46206e7885f6a649a055ceb46be49a7rsmaeda
4496171313bed39e96f21bc2f9faf2868e267ae3girishint
459190a5c46206e7885f6a649a055ceb46be49a7rsmaedavdev_queue_timestamp_compare(const void *x1, const void *x2)
4496171313bed39e96f21bc2f9faf2868e267ae3girish{
4496171313bed39e96f21bc2f9faf2868e267ae3girish const zio_t *z1 = x1;
4496171313bed39e96f21bc2f9faf2868e267ae3girish const zio_t *z2 = x2;
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish if (z1->io_timestamp < z2->io_timestamp)
4496171313bed39e96f21bc2f9faf2868e267ae3girish return (-1);
4496171313bed39e96f21bc2f9faf2868e267ae3girish if (z1->io_timestamp > z2->io_timestamp)
4496171313bed39e96f21bc2f9faf2868e267ae3girish return (1);
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish if (z1 < z2)
4496171313bed39e96f21bc2f9faf2868e267ae3girish return (-1);
4496171313bed39e96f21bc2f9faf2868e267ae3girish if (z1 > z2)
4496171313bed39e96f21bc2f9faf2868e267ae3girish return (1);
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish return (0);
4496171313bed39e96f21bc2f9faf2868e267ae3girish}
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girishvoid
4496171313bed39e96f21bc2f9faf2868e267ae3girishvdev_queue_init(vdev_t *vd)
4496171313bed39e96f21bc2f9faf2868e267ae3girish{
4496171313bed39e96f21bc2f9faf2868e267ae3girish vdev_queue_t *vq = &vd->vdev_queue;
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
4496171313bed39e96f21bc2f9faf2868e267ae3girish vq->vq_vdev = vd;
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
4496171313bed39e96f21bc2f9faf2868e267ae3girish sizeof (zio_t), offsetof(struct zio, io_queue_node));
4496171313bed39e96f21bc2f9faf2868e267ae3girish avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
4496171313bed39e96f21bc2f9faf2868e267ae3girish vdev_queue_offset_compare, sizeof (zio_t),
4496171313bed39e96f21bc2f9faf2868e267ae3girish offsetof(struct zio, io_offset_node));
4496171313bed39e96f21bc2f9faf2868e267ae3girish avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
4496171313bed39e96f21bc2f9faf2868e267ae3girish vdev_queue_offset_compare, sizeof (zio_t),
4496171313bed39e96f21bc2f9faf2868e267ae3girish offsetof(struct zio, io_offset_node));
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
4496171313bed39e96f21bc2f9faf2868e267ae3girish int (*compfn) (const void *, const void *);
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish /*
4496171313bed39e96f21bc2f9faf2868e267ae3girish * The synchronous i/o queues are dispatched in FIFO rather
4496171313bed39e96f21bc2f9faf2868e267ae3girish * than LBA order. This provides more consistent latency for
4496171313bed39e96f21bc2f9faf2868e267ae3girish * these i/os.
4496171313bed39e96f21bc2f9faf2868e267ae3girish */
4496171313bed39e96f21bc2f9faf2868e267ae3girish if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
4496171313bed39e96f21bc2f9faf2868e267ae3girish compfn = vdev_queue_timestamp_compare;
4496171313bed39e96f21bc2f9faf2868e267ae3girish else
4496171313bed39e96f21bc2f9faf2868e267ae3girish compfn = vdev_queue_offset_compare;
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girish avl_create(vdev_queue_class_tree(vq, p), compfn,
4496171313bed39e96f21bc2f9faf2868e267ae3girish sizeof (zio_t), offsetof(struct zio, io_queue_node));
4496171313bed39e96f21bc2f9faf2868e267ae3girish }
4496171313bed39e96f21bc2f9faf2868e267ae3girish}
4496171313bed39e96f21bc2f9faf2868e267ae3girish
4496171313bed39e96f21bc2f9faf2868e267ae3girishvoid
4496171313bed39e96f21bc2f9faf2868e267ae3girishvdev_queue_fini(vdev_t *vd)
4496171313bed39e96f21bc2f9faf2868e267ae3girish{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp vdev_queue_t *vq = &vd->vdev_queue;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_destroy(vdev_queue_class_tree(vq, p));
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_destroy(&vq->vq_active_tree);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_destroy(&vq->vq_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic void
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa_t *spa = zio->io_spa;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_enter(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa->spa_queue_stats[zio->io_priority].spa_queued++;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (spa->spa_iokstat != NULL)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp kstat_waitq_enter(spa->spa_iokstat->ks_data);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_exit(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic void
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa_t *spa = zio->io_spa;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_enter(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa->spa_queue_stats[zio->io_priority].spa_queued--;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (spa->spa_iokstat != NULL)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp kstat_waitq_exit(spa->spa_iokstat->ks_data);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_exit(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic void
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa_t *spa = zio->io_spa;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT(MUTEX_HELD(&vq->vq_lock));
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp vq->vq_class[zio->io_priority].vqc_active++;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_add(&vq->vq_active_tree, zio);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_enter(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa->spa_queue_stats[zio->io_priority].spa_active++;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (spa->spa_iokstat != NULL)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp kstat_runq_enter(spa->spa_iokstat->ks_data);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_exit(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic void
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa_t *spa = zio->io_spa;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT(MUTEX_HELD(&vq->vq_lock));
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp vq->vq_class[zio->io_priority].vqc_active--;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp avl_remove(&vq->vq_active_tree, zio);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_enter(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa->spa_queue_stats[zio->io_priority].spa_active--;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (spa->spa_iokstat != NULL) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp kstat_io_t *ksio = spa->spa_iokstat->ks_data;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp kstat_runq_exit(spa->spa_iokstat->ks_data);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (zio->io_type == ZIO_TYPE_READ) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ksio->reads++;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ksio->nread += zio->io_size;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp } else if (zio->io_type == ZIO_TYPE_WRITE) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ksio->writes++;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ksio->nwritten += zio->io_size;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp }
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp }
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp mutex_exit(&spa->spa_iokstat_lock);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic void
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_agg_io_done(zio_t *aio)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (aio->io_type == ZIO_TYPE_READ) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp zio_t *pio;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp while ((pio = zio_walk_parents(aio)) != NULL) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp bcopy((char *)aio->io_data + (pio->io_offset -
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp aio->io_offset), pio->io_data, pio->io_size);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp }
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp }
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp zio_buf_free(aio->io_data, aio->io_size);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic int
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_class_min_active(zio_priority_t p)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp switch (p) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_SYNC_READ:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_sync_read_min_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_SYNC_WRITE:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_sync_write_min_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_ASYNC_READ:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_async_read_min_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_ASYNC_WRITE:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_async_write_min_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_SCRUB:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_scrub_min_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp default:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp panic("invalid priority %u", p);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (0);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp }
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic int
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_max_async_writes(spa_t *spa)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp int writes;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp uint64_t min_bytes = zfs_dirty_data_max *
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp zfs_vdev_async_write_active_min_dirty_percent / 100;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp uint64_t max_bytes = zfs_dirty_data_max *
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp zfs_vdev_async_write_active_max_dirty_percent / 100;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp /*
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * Sync tasks correspond to interactive user actions. To reduce the
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * execution time of those actions we push data out as fast as possible.
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp */
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (spa_has_pending_synctask(spa)) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_async_write_max_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp }
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (dirty < min_bytes)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_async_write_min_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (dirty > max_bytes)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_async_write_max_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp /*
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * linear interpolation:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * move right by min_bytes
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * move up by min_writes
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp */
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp writes = (dirty - min_bytes) *
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp (zfs_vdev_async_write_max_active -
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp zfs_vdev_async_write_min_active) /
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp (max_bytes - min_bytes) +
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp zfs_vdev_async_write_min_active;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (writes);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic int
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp switch (p) {
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_SYNC_READ:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_sync_read_max_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_SYNC_WRITE:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_sync_write_max_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_ASYNC_READ:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_async_read_max_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_ASYNC_WRITE:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (vdev_queue_max_async_writes(spa));
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp case ZIO_PRIORITY_SCRUB:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (zfs_vdev_scrub_max_active);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp default:
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp panic("invalid priority %u", p);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (0);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp }
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp}
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp/*
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp * there is no eligible class.
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp */
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpstatic zio_priority_t
102033aa92edf302ad31b3bdd7c6fcd2d6910903dpvdev_queue_class_to_issue(vdev_queue_t *vq)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp{
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp spa_t *spa = vq->vq_vdev->vdev_spa;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp zio_priority_t p;
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp return (ZIO_PRIORITY_NUM_QUEUEABLE);
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp /* find a queue that has not reached its minimum # outstanding i/os */
102033aa92edf302ad31b3bdd7c6fcd2d6910903dp for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp vq->vq_class[p].vqc_active <
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp vdev_queue_class_min_active(p))
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp return (p);
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp }
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp /*
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp * If we haven't found a queue, look for one that hasn't reached its
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp * maximum # outstanding i/os.
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp */
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp vq->vq_class[p].vqc_active <
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp vdev_queue_class_max_active(spa, p))
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp return (p);
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp }
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp /* No eligible queued i/os */
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp return (ZIO_PRIORITY_NUM_QUEUEABLE);
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp}
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp/*
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp * Compute the range spanned by two i/os, which is the endpoint of the last
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp */
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdpstatic zio_t *
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdpvdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp{
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp zio_t *first, *last, *aio, *dio, *mandatory, *nio;
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp uint64_t maxgap = 0;
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp uint64_t size;
fe70c9cf90dfc23d18485fb7b4b20a1175d53a8bdp boolean_t stretch = B_FALSE;
avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
return (NULL);
first = last = zio;
if (zio->io_type == ZIO_TYPE_READ)
maxgap = zfs_vdev_read_gap_limit;
/*
* We can aggregate I/Os that are sufficiently adjacent and of
* the same flavor, as expressed by the AGG_INHERIT flags.
* The latter requirement is necessary so that certain
* attributes of the I/O, such as whether it's a normal I/O
* or a scrub/resilver, can be preserved in the aggregate.
* We can include optional I/Os, but don't allow them
* to begin a range as they add no benefit in that situation.
*/
/*
* We keep track of the last non-optional I/O.
*/
mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
/*
* Walk backwards through sufficiently contiguous I/Os
* recording the last non-option I/O.
*/
while ((dio = AVL_PREV(t, first)) != NULL &&
(dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
IO_GAP(dio, first) <= maxgap) {
first = dio;
if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
mandatory = first;
}
/*
* Skip any initial optional I/Os.
*/
while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
first = AVL_NEXT(t, first);
ASSERT(first != NULL);
}
/*
* Walk forward through sufficiently contiguous I/Os.
*/
while ((dio = AVL_NEXT(t, last)) != NULL &&
(dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
IO_GAP(last, dio) <= maxgap) {
last = dio;
if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
mandatory = last;
}
/*
* Now that we've established the range of the I/O aggregation
* we must decide what to do with trailing optional I/Os.
* For reads, there's nothing to do. While we are unable to
* aggregate further, it's possible that a trailing optional
* I/O would allow the underlying device to aggregate with
* subsequent I/Os. We must therefore determine if the next
* non-optional I/O is close enough to make aggregation
* worthwhile.
*/
if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
zio_t *nio = last;
while ((dio = AVL_NEXT(t, nio)) != NULL &&
IO_GAP(nio, dio) == 0 &&
IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
nio = dio;
if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
stretch = B_TRUE;
break;
}
}
}
if (stretch) {
/* This may be a no-op. */
dio = AVL_NEXT(t, last);
dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
} else {
while (last != mandatory && last != first) {
ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
last = AVL_PREV(t, last);
ASSERT(last != NULL);
}
}
if (first == last)
return (NULL);
size = IO_SPAN(first, last);
ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
zio_buf_alloc(size), size, first->io_type, zio->io_priority,
flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
vdev_queue_agg_io_done, NULL);
aio->io_timestamp = first->io_timestamp;
nio = first;
do {
dio = nio;
nio = AVL_NEXT(t, dio);
ASSERT3U(dio->io_type, ==, aio->io_type);
if (dio->io_flags & ZIO_FLAG_NODATA) {
ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
bzero((char *)aio->io_data + (dio->io_offset -
aio->io_offset), dio->io_size);
} else if (dio->io_type == ZIO_TYPE_WRITE) {
bcopy(dio->io_data, (char *)aio->io_data +
(dio->io_offset - aio->io_offset),
dio->io_size);
}
zio_add_child(dio, aio);
vdev_queue_io_remove(vq, dio);
zio_vdev_io_bypass(dio);
zio_execute(dio);
} while (dio != last);
return (aio);
}
static zio_t *
vdev_queue_io_to_issue(vdev_queue_t *vq)
{
zio_t *zio, *aio;
zio_priority_t p;
avl_index_t idx;
avl_tree_t *tree;
zio_t search;
again:
ASSERT(MUTEX_HELD(&vq->vq_lock));
p = vdev_queue_class_to_issue(vq);
if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
/* No eligible queued i/os */
return (NULL);
}
/*
* For LBA-ordered queues (async / scrub), issue the i/o which follows
* the most recently issued i/o in LBA (offset) order.
*
* For FIFO queues (sync), issue the i/o with the lowest timestamp.
*/
tree = vdev_queue_class_tree(vq, p);
search.io_timestamp = 0;
search.io_offset = vq->vq_last_offset + 1;
VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
zio = avl_nearest(tree, idx, AVL_AFTER);
if (zio == NULL)
zio = avl_first(tree);
ASSERT3U(zio->io_priority, ==, p);
aio = vdev_queue_aggregate(vq, zio);
if (aio != NULL)
zio = aio;
else
vdev_queue_io_remove(vq, zio);
/*
* If the I/O is or was optional and therefore has no data, we need to
* simply discard it. We need to drop the vdev queue's lock to avoid a
* deadlock that we could encounter since this I/O will complete
* immediately.
*/
if (zio->io_flags & ZIO_FLAG_NODATA) {
mutex_exit(&vq->vq_lock);
zio_vdev_io_bypass(zio);
zio_execute(zio);
mutex_enter(&vq->vq_lock);
goto again;
}
vdev_queue_pending_add(vq, zio);
vq->vq_last_offset = zio->io_offset;
return (zio);
}
zio_t *
vdev_queue_io(zio_t *zio)
{
vdev_queue_t *vq = &zio->io_vd->vdev_queue;
zio_t *nio;
if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
return (zio);
/*
* Children i/os inherent their parent's priority, which might
* not match the child's i/o type. Fix it up here.
*/
if (zio->io_type == ZIO_TYPE_READ) {
if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
zio->io_priority != ZIO_PRIORITY_SCRUB)
zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
} else {
ASSERT(zio->io_type == ZIO_TYPE_WRITE);
if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
}
zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
mutex_enter(&vq->vq_lock);
zio->io_timestamp = gethrtime();
vdev_queue_io_add(vq, zio);
nio = vdev_queue_io_to_issue(vq);
mutex_exit(&vq->vq_lock);
if (nio == NULL)
return (NULL);
if (nio->io_done == vdev_queue_agg_io_done) {
zio_nowait(nio);
return (NULL);
}
return (nio);
}
void
vdev_queue_io_done(zio_t *zio)
{
vdev_queue_t *vq = &zio->io_vd->vdev_queue;
zio_t *nio;
mutex_enter(&vq->vq_lock);
vdev_queue_pending_remove(vq, zio);
vq->vq_io_complete_ts = gethrtime();
while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
mutex_exit(&vq->vq_lock);
if (nio->io_done == vdev_queue_agg_io_done) {
zio_nowait(nio);
} else {
zio_vdev_io_reissue(nio);
zio_execute(nio);
}
mutex_enter(&vq->vq_lock);
}
mutex_exit(&vq->vq_lock);
}