4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2012 by Delphix. All rights reserved.
30 #include <sys/zfs_context.h>
31 #include <sys/vdev_impl.h>
36 * These tunables are for performance analysis.
39 * zfs_vdev_max_pending is the maximum number of i/os concurrently
40 * pending to each device. zfs_vdev_min_pending is the initial number
41 * of i/os pending to each device (before it starts ramping up to
44 int zfs_vdev_max_pending = 10;
45 int zfs_vdev_min_pending = 4;
48 * The deadlines are grouped into buckets based on zfs_vdev_time_shift:
49 * deadline = pri + gethrtime() >> time_shift)
51 int zfs_vdev_time_shift = 29; /* each bucket is 0.537 seconds */
53 /* exponential I/O issue ramp-up rate */
54 int zfs_vdev_ramp_rate = 2;
57 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
58 * For read I/Os, we also aggregate across small adjacency gaps; for writes
59 * we include spans of optional I/Os to aid aggregation at the disk even when
60 * they aren't able to help us aggregate at this level.
62 int zfs_vdev_aggregation_limit = SPA_MAXBLOCKSIZE;
63 int zfs_vdev_read_gap_limit = 32 << 10;
64 int zfs_vdev_write_gap_limit = 4 << 10;
66 SYSCTL_DECL(_vfs_zfs_vdev);
67 TUNABLE_INT("vfs.zfs.vdev.max_pending", &zfs_vdev_max_pending);
68 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, max_pending, CTLFLAG_RW,
69 &zfs_vdev_max_pending, 0, "Maximum I/O requests pending on each device");
70 TUNABLE_INT("vfs.zfs.vdev.min_pending", &zfs_vdev_min_pending);
71 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, min_pending, CTLFLAG_RW,
72 &zfs_vdev_min_pending, 0,
73 "Initial number of I/O requests pending to each device");
74 TUNABLE_INT("vfs.zfs.vdev.time_shift", &zfs_vdev_time_shift);
75 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, time_shift, CTLFLAG_RW,
76 &zfs_vdev_time_shift, 0, "Used for calculating I/O request deadline");
77 TUNABLE_INT("vfs.zfs.vdev.ramp_rate", &zfs_vdev_ramp_rate);
78 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, ramp_rate, CTLFLAG_RW,
79 &zfs_vdev_ramp_rate, 0, "Exponential I/O issue ramp-up rate");
80 TUNABLE_INT("vfs.zfs.vdev.aggregation_limit", &zfs_vdev_aggregation_limit);
81 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RW,
82 &zfs_vdev_aggregation_limit, 0,
83 "I/O requests are aggregated up to this size");
84 TUNABLE_INT("vfs.zfs.vdev.read_gap_limit", &zfs_vdev_read_gap_limit);
85 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RW,
86 &zfs_vdev_read_gap_limit, 0,
87 "Acceptable gap between two reads being aggregated");
88 TUNABLE_INT("vfs.zfs.vdev.write_gap_limit", &zfs_vdev_write_gap_limit);
89 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RW,
90 &zfs_vdev_write_gap_limit, 0,
91 "Acceptable gap between two writes being aggregated");
94 * Virtual device vector for disk I/O scheduling.
97 vdev_queue_deadline_compare(const void *x1, const void *x2)
100 const zio_t *z2 = x2;
102 if (z1->io_deadline < z2->io_deadline)
104 if (z1->io_deadline > z2->io_deadline)
107 if (z1->io_offset < z2->io_offset)
109 if (z1->io_offset > z2->io_offset)
121 vdev_queue_offset_compare(const void *x1, const void *x2)
123 const zio_t *z1 = x1;
124 const zio_t *z2 = x2;
126 if (z1->io_offset < z2->io_offset)
128 if (z1->io_offset > z2->io_offset)
140 vdev_queue_init(vdev_t *vd)
142 vdev_queue_t *vq = &vd->vdev_queue;
144 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
146 avl_create(&vq->vq_deadline_tree, vdev_queue_deadline_compare,
147 sizeof (zio_t), offsetof(struct zio, io_deadline_node));
149 avl_create(&vq->vq_read_tree, vdev_queue_offset_compare,
150 sizeof (zio_t), offsetof(struct zio, io_offset_node));
152 avl_create(&vq->vq_write_tree, vdev_queue_offset_compare,
153 sizeof (zio_t), offsetof(struct zio, io_offset_node));
155 avl_create(&vq->vq_pending_tree, vdev_queue_offset_compare,
156 sizeof (zio_t), offsetof(struct zio, io_offset_node));
160 vdev_queue_fini(vdev_t *vd)
162 vdev_queue_t *vq = &vd->vdev_queue;
164 avl_destroy(&vq->vq_deadline_tree);
165 avl_destroy(&vq->vq_read_tree);
166 avl_destroy(&vq->vq_write_tree);
167 avl_destroy(&vq->vq_pending_tree);
169 mutex_destroy(&vq->vq_lock);
173 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
175 avl_add(&vq->vq_deadline_tree, zio);
176 avl_add(zio->io_vdev_tree, zio);
180 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
182 avl_remove(&vq->vq_deadline_tree, zio);
183 avl_remove(zio->io_vdev_tree, zio);
187 vdev_queue_agg_io_done(zio_t *aio)
191 while ((pio = zio_walk_parents(aio)) != NULL)
192 if (aio->io_type == ZIO_TYPE_READ)
193 bcopy((char *)aio->io_data + (pio->io_offset -
194 aio->io_offset), pio->io_data, pio->io_size);
196 zio_buf_free(aio->io_data, aio->io_size);
200 * Compute the range spanned by two i/os, which is the endpoint of the last
201 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
202 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
203 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
205 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
206 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
209 vdev_queue_io_to_issue(vdev_queue_t *vq, uint64_t pending_limit)
211 zio_t *fio, *lio, *aio, *dio, *nio, *mio;
214 uint64_t maxspan = zfs_vdev_aggregation_limit;
219 ASSERT(MUTEX_HELD(&vq->vq_lock));
221 if (avl_numnodes(&vq->vq_pending_tree) >= pending_limit ||
222 avl_numnodes(&vq->vq_deadline_tree) == 0)
225 fio = lio = avl_first(&vq->vq_deadline_tree);
227 t = fio->io_vdev_tree;
228 flags = fio->io_flags & ZIO_FLAG_AGG_INHERIT;
229 maxgap = (t == &vq->vq_read_tree) ? zfs_vdev_read_gap_limit : 0;
231 if (!(flags & ZIO_FLAG_DONT_AGGREGATE)) {
233 * We can aggregate I/Os that are sufficiently adjacent and of
234 * the same flavor, as expressed by the AGG_INHERIT flags.
235 * The latter requirement is necessary so that certain
236 * attributes of the I/O, such as whether it's a normal I/O
237 * or a scrub/resilver, can be preserved in the aggregate.
238 * We can include optional I/Os, but don't allow them
239 * to begin a range as they add no benefit in that situation.
243 * We keep track of the last non-optional I/O.
245 mio = (fio->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : fio;
248 * Walk backwards through sufficiently contiguous I/Os
249 * recording the last non-option I/O.
251 while ((dio = AVL_PREV(t, fio)) != NULL &&
252 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
253 IO_SPAN(dio, lio) <= maxspan &&
254 IO_GAP(dio, fio) <= maxgap) {
256 if (mio == NULL && !(fio->io_flags & ZIO_FLAG_OPTIONAL))
261 * Skip any initial optional I/Os.
263 while ((fio->io_flags & ZIO_FLAG_OPTIONAL) && fio != lio) {
264 fio = AVL_NEXT(t, fio);
269 * Walk forward through sufficiently contiguous I/Os.
271 while ((dio = AVL_NEXT(t, lio)) != NULL &&
272 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
273 IO_SPAN(fio, dio) <= maxspan &&
274 IO_GAP(lio, dio) <= maxgap) {
276 if (!(lio->io_flags & ZIO_FLAG_OPTIONAL))
281 * Now that we've established the range of the I/O aggregation
282 * we must decide what to do with trailing optional I/Os.
283 * For reads, there's nothing to do. While we are unable to
284 * aggregate further, it's possible that a trailing optional
285 * I/O would allow the underlying device to aggregate with
286 * subsequent I/Os. We must therefore determine if the next
287 * non-optional I/O is close enough to make aggregation
291 if (t != &vq->vq_read_tree && mio != NULL) {
293 while ((dio = AVL_NEXT(t, nio)) != NULL &&
294 IO_GAP(nio, dio) == 0 &&
295 IO_GAP(mio, dio) <= zfs_vdev_write_gap_limit) {
297 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
305 /* This may be a no-op. */
306 VERIFY((dio = AVL_NEXT(t, lio)) != NULL);
307 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
309 while (lio != mio && lio != fio) {
310 ASSERT(lio->io_flags & ZIO_FLAG_OPTIONAL);
311 lio = AVL_PREV(t, lio);
318 uint64_t size = IO_SPAN(fio, lio);
319 ASSERT(size <= zfs_vdev_aggregation_limit);
321 aio = zio_vdev_delegated_io(fio->io_vd, fio->io_offset,
322 zio_buf_alloc(size), size, fio->io_type, ZIO_PRIORITY_AGG,
323 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
324 vdev_queue_agg_io_done, NULL);
325 aio->io_timestamp = fio->io_timestamp;
330 nio = AVL_NEXT(t, dio);
331 ASSERT(dio->io_type == aio->io_type);
332 ASSERT(dio->io_vdev_tree == t);
334 if (dio->io_flags & ZIO_FLAG_NODATA) {
335 ASSERT(dio->io_type == ZIO_TYPE_WRITE);
336 bzero((char *)aio->io_data + (dio->io_offset -
337 aio->io_offset), dio->io_size);
338 } else if (dio->io_type == ZIO_TYPE_WRITE) {
339 bcopy(dio->io_data, (char *)aio->io_data +
340 (dio->io_offset - aio->io_offset),
344 zio_add_child(dio, aio);
345 vdev_queue_io_remove(vq, dio);
346 zio_vdev_io_bypass(dio);
348 } while (dio != lio);
350 avl_add(&vq->vq_pending_tree, aio);
355 ASSERT(fio->io_vdev_tree == t);
356 vdev_queue_io_remove(vq, fio);
359 * If the I/O is or was optional and therefore has no data, we need to
360 * simply discard it. We need to drop the vdev queue's lock to avoid a
361 * deadlock that we could encounter since this I/O will complete
364 if (fio->io_flags & ZIO_FLAG_NODATA) {
365 mutex_exit(&vq->vq_lock);
366 zio_vdev_io_bypass(fio);
368 mutex_enter(&vq->vq_lock);
372 avl_add(&vq->vq_pending_tree, fio);
378 vdev_queue_io(zio_t *zio)
380 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
383 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
385 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
388 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
390 if (zio->io_type == ZIO_TYPE_READ)
391 zio->io_vdev_tree = &vq->vq_read_tree;
393 zio->io_vdev_tree = &vq->vq_write_tree;
395 mutex_enter(&vq->vq_lock);
397 zio->io_timestamp = gethrtime();
398 zio->io_deadline = (zio->io_timestamp >> zfs_vdev_time_shift) +
401 vdev_queue_io_add(vq, zio);
403 nio = vdev_queue_io_to_issue(vq, zfs_vdev_min_pending);
405 mutex_exit(&vq->vq_lock);
410 if (nio->io_done == vdev_queue_agg_io_done) {
419 vdev_queue_io_done(zio_t *zio)
421 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
423 if (zio_injection_enabled)
424 delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
426 mutex_enter(&vq->vq_lock);
428 avl_remove(&vq->vq_pending_tree, zio);
430 vq->vq_io_complete_ts = gethrtime();
432 for (int i = 0; i < zfs_vdev_ramp_rate; i++) {
433 zio_t *nio = vdev_queue_io_to_issue(vq, zfs_vdev_max_pending);
436 mutex_exit(&vq->vq_lock);
437 if (nio->io_done == vdev_queue_agg_io_done) {
440 zio_vdev_io_reissue(nio);
443 mutex_enter(&vq->vq_lock);
446 mutex_exit(&vq->vq_lock);