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.
26 #include <sys/zfs_context.h>
27 #include <sys/vdev_impl.h>
32 * These tunables are for performance analysis.
35 * zfs_vdev_max_pending is the maximum number of i/os concurrently
36 * pending to each device. zfs_vdev_min_pending is the initial number
37 * of i/os pending to each device (before it starts ramping up to
40 int zfs_vdev_max_pending
= 10;
41 int zfs_vdev_min_pending
= 4;
43 /* deadline = pri + ddi_get_lbolt64() >> time_shift) */
44 int zfs_vdev_time_shift
= 6;
46 /* exponential I/O issue ramp-up rate */
47 int zfs_vdev_ramp_rate
= 2;
50 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
51 * For read I/Os, we also aggregate across small adjacency gaps; for writes
52 * we include spans of optional I/Os to aid aggregation at the disk even when
53 * they aren't able to help us aggregate at this level.
55 int zfs_vdev_aggregation_limit
= SPA_MAXBLOCKSIZE
;
56 int zfs_vdev_read_gap_limit
= 32 << 10;
57 int zfs_vdev_write_gap_limit
= 4 << 10;
60 * Virtual device vector for disk I/O scheduling.
63 vdev_queue_deadline_compare(const void *x1
, const void *x2
)
68 if (z1
->io_deadline
< z2
->io_deadline
)
70 if (z1
->io_deadline
> z2
->io_deadline
)
73 if (z1
->io_offset
< z2
->io_offset
)
75 if (z1
->io_offset
> z2
->io_offset
)
87 vdev_queue_offset_compare(const void *x1
, const void *x2
)
92 if (z1
->io_offset
< z2
->io_offset
)
94 if (z1
->io_offset
> z2
->io_offset
)
106 vdev_queue_init(vdev_t
*vd
)
108 vdev_queue_t
*vq
= &vd
->vdev_queue
;
110 mutex_init(&vq
->vq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
112 avl_create(&vq
->vq_deadline_tree
, vdev_queue_deadline_compare
,
113 sizeof (zio_t
), offsetof(struct zio
, io_deadline_node
));
115 avl_create(&vq
->vq_read_tree
, vdev_queue_offset_compare
,
116 sizeof (zio_t
), offsetof(struct zio
, io_offset_node
));
118 avl_create(&vq
->vq_write_tree
, vdev_queue_offset_compare
,
119 sizeof (zio_t
), offsetof(struct zio
, io_offset_node
));
121 avl_create(&vq
->vq_pending_tree
, vdev_queue_offset_compare
,
122 sizeof (zio_t
), offsetof(struct zio
, io_offset_node
));
126 vdev_queue_fini(vdev_t
*vd
)
128 vdev_queue_t
*vq
= &vd
->vdev_queue
;
130 avl_destroy(&vq
->vq_deadline_tree
);
131 avl_destroy(&vq
->vq_read_tree
);
132 avl_destroy(&vq
->vq_write_tree
);
133 avl_destroy(&vq
->vq_pending_tree
);
135 mutex_destroy(&vq
->vq_lock
);
139 vdev_queue_io_add(vdev_queue_t
*vq
, zio_t
*zio
)
141 avl_add(&vq
->vq_deadline_tree
, zio
);
142 avl_add(zio
->io_vdev_tree
, zio
);
146 vdev_queue_io_remove(vdev_queue_t
*vq
, zio_t
*zio
)
148 avl_remove(&vq
->vq_deadline_tree
, zio
);
149 avl_remove(zio
->io_vdev_tree
, zio
);
153 vdev_queue_agg_io_done(zio_t
*aio
)
157 while ((pio
= zio_walk_parents(aio
)) != NULL
)
158 if (aio
->io_type
== ZIO_TYPE_READ
)
159 bcopy((char *)aio
->io_data
+ (pio
->io_offset
-
160 aio
->io_offset
), pio
->io_data
, pio
->io_size
);
162 zio_buf_free(aio
->io_data
, aio
->io_size
);
166 * Compute the range spanned by two i/os, which is the endpoint of the last
167 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
168 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
169 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
171 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
172 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
175 vdev_queue_io_to_issue(vdev_queue_t
*vq
, uint64_t pending_limit
)
177 zio_t
*fio
, *lio
, *aio
, *dio
, *nio
, *mio
;
180 uint64_t maxspan
= zfs_vdev_aggregation_limit
;
185 ASSERT(MUTEX_HELD(&vq
->vq_lock
));
187 if (avl_numnodes(&vq
->vq_pending_tree
) >= pending_limit
||
188 avl_numnodes(&vq
->vq_deadline_tree
) == 0)
191 fio
= lio
= avl_first(&vq
->vq_deadline_tree
);
193 t
= fio
->io_vdev_tree
;
194 flags
= fio
->io_flags
& ZIO_FLAG_AGG_INHERIT
;
195 maxgap
= (t
== &vq
->vq_read_tree
) ? zfs_vdev_read_gap_limit
: 0;
197 if (!(flags
& ZIO_FLAG_DONT_AGGREGATE
)) {
199 * We can aggregate I/Os that are sufficiently adjacent and of
200 * the same flavor, as expressed by the AGG_INHERIT flags.
201 * The latter requirement is necessary so that certain
202 * attributes of the I/O, such as whether it's a normal I/O
203 * or a scrub/resilver, can be preserved in the aggregate.
204 * We can include optional I/Os, but don't allow them
205 * to begin a range as they add no benefit in that situation.
209 * We keep track of the last non-optional I/O.
211 mio
= (fio
->io_flags
& ZIO_FLAG_OPTIONAL
) ? NULL
: fio
;
214 * Walk backwards through sufficiently contiguous I/Os
215 * recording the last non-option I/O.
217 while ((dio
= AVL_PREV(t
, fio
)) != NULL
&&
218 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
219 IO_SPAN(dio
, lio
) <= maxspan
&&
220 IO_GAP(dio
, fio
) <= maxgap
) {
222 if (mio
== NULL
&& !(fio
->io_flags
& ZIO_FLAG_OPTIONAL
))
227 * Skip any initial optional I/Os.
229 while ((fio
->io_flags
& ZIO_FLAG_OPTIONAL
) && fio
!= lio
) {
230 fio
= AVL_NEXT(t
, fio
);
235 * Walk forward through sufficiently contiguous I/Os.
237 while ((dio
= AVL_NEXT(t
, lio
)) != NULL
&&
238 (dio
->io_flags
& ZIO_FLAG_AGG_INHERIT
) == flags
&&
239 IO_SPAN(fio
, dio
) <= maxspan
&&
240 IO_GAP(lio
, dio
) <= maxgap
) {
242 if (!(lio
->io_flags
& ZIO_FLAG_OPTIONAL
))
247 * Now that we've established the range of the I/O aggregation
248 * we must decide what to do with trailing optional I/Os.
249 * For reads, there's nothing to do. While we are unable to
250 * aggregate further, it's possible that a trailing optional
251 * I/O would allow the underlying device to aggregate with
252 * subsequent I/Os. We must therefore determine if the next
253 * non-optional I/O is close enough to make aggregation
257 if (t
!= &vq
->vq_read_tree
&& mio
!= NULL
) {
259 while ((dio
= AVL_NEXT(t
, nio
)) != NULL
&&
260 IO_GAP(nio
, dio
) == 0 &&
261 IO_GAP(mio
, dio
) <= zfs_vdev_write_gap_limit
) {
263 if (!(nio
->io_flags
& ZIO_FLAG_OPTIONAL
)) {
271 /* This may be a no-op. */
272 VERIFY((dio
= AVL_NEXT(t
, lio
)) != NULL
);
273 dio
->io_flags
&= ~ZIO_FLAG_OPTIONAL
;
275 while (lio
!= mio
&& lio
!= fio
) {
276 ASSERT(lio
->io_flags
& ZIO_FLAG_OPTIONAL
);
277 lio
= AVL_PREV(t
, lio
);
284 uint64_t size
= IO_SPAN(fio
, lio
);
285 ASSERT(size
<= zfs_vdev_aggregation_limit
);
287 aio
= zio_vdev_delegated_io(fio
->io_vd
, fio
->io_offset
,
288 zio_buf_alloc(size
), size
, fio
->io_type
, ZIO_PRIORITY_AGG
,
289 flags
| ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_QUEUE
,
290 vdev_queue_agg_io_done
, NULL
);
295 nio
= AVL_NEXT(t
, dio
);
296 ASSERT(dio
->io_type
== aio
->io_type
);
297 ASSERT(dio
->io_vdev_tree
== t
);
299 if (dio
->io_flags
& ZIO_FLAG_NODATA
) {
300 ASSERT(dio
->io_type
== ZIO_TYPE_WRITE
);
301 bzero((char *)aio
->io_data
+ (dio
->io_offset
-
302 aio
->io_offset
), dio
->io_size
);
303 } else if (dio
->io_type
== ZIO_TYPE_WRITE
) {
304 bcopy(dio
->io_data
, (char *)aio
->io_data
+
305 (dio
->io_offset
- aio
->io_offset
),
309 zio_add_child(dio
, aio
);
310 vdev_queue_io_remove(vq
, dio
);
311 zio_vdev_io_bypass(dio
);
313 } while (dio
!= lio
);
315 avl_add(&vq
->vq_pending_tree
, aio
);
320 ASSERT(fio
->io_vdev_tree
== t
);
321 vdev_queue_io_remove(vq
, fio
);
324 * If the I/O is or was optional and therefore has no data, we need to
325 * simply discard it. We need to drop the vdev queue's lock to avoid a
326 * deadlock that we could encounter since this I/O will complete
329 if (fio
->io_flags
& ZIO_FLAG_NODATA
) {
330 mutex_exit(&vq
->vq_lock
);
331 zio_vdev_io_bypass(fio
);
333 mutex_enter(&vq
->vq_lock
);
337 avl_add(&vq
->vq_pending_tree
, fio
);
343 vdev_queue_io(zio_t
*zio
)
345 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
348 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
350 if (zio
->io_flags
& ZIO_FLAG_DONT_QUEUE
)
353 zio
->io_flags
|= ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_QUEUE
;
355 if (zio
->io_type
== ZIO_TYPE_READ
)
356 zio
->io_vdev_tree
= &vq
->vq_read_tree
;
358 zio
->io_vdev_tree
= &vq
->vq_write_tree
;
360 mutex_enter(&vq
->vq_lock
);
362 zio
->io_deadline
= (ddi_get_lbolt64() >> zfs_vdev_time_shift
) +
365 vdev_queue_io_add(vq
, zio
);
367 nio
= vdev_queue_io_to_issue(vq
, zfs_vdev_min_pending
);
369 mutex_exit(&vq
->vq_lock
);
374 if (nio
->io_done
== vdev_queue_agg_io_done
) {
383 vdev_queue_io_done(zio_t
*zio
)
385 vdev_queue_t
*vq
= &zio
->io_vd
->vdev_queue
;
387 mutex_enter(&vq
->vq_lock
);
389 avl_remove(&vq
->vq_pending_tree
, zio
);
391 for (int i
= 0; i
< zfs_vdev_ramp_rate
; i
++) {
392 zio_t
*nio
= vdev_queue_io_to_issue(vq
, zfs_vdev_max_pending
);
395 mutex_exit(&vq
->vq_lock
);
396 if (nio
->io_done
== vdev_queue_agg_io_done
) {
399 zio_vdev_io_reissue(nio
);
402 mutex_enter(&vq
->vq_lock
);
405 mutex_exit(&vq
->vq_lock
);