vdev_queue.c revision 251631
1219567Smarius/*
2130293Sscottl * CDDL HEADER START
3130293Sscottl *
4130293Sscottl * The contents of this file are subject to the terms of the
5130293Sscottl * Common Development and Distribution License (the "License").
6130293Sscottl * You may not use this file except in compliance with the License.
7130293Sscottl *
8130293Sscottl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9130293Sscottl * or http://www.opensolaris.org/os/licensing.
10130293Sscottl * See the License for the specific language governing permissions
11130293Sscottl * and limitations under the License.
12130293Sscottl *
13130293Sscottl * When distributing Covered Code, include this CDDL HEADER in each
14130293Sscottl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15130293Sscottl * If applicable, add the following below this CDDL HEADER, with the
16130293Sscottl * fields enclosed by brackets "[]" replaced with your own identifying
17130293Sscottl * information: Portions Copyright [yyyy] [name of copyright owner]
18130293Sscottl *
19130293Sscottl * CDDL HEADER END
20130293Sscottl */
21130293Sscottl/*
22130293Sscottl * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23130293Sscottl * Use is subject to license terms.
24130293Sscottl */
25130293Sscottl
26130293Sscottl/*
27130293Sscottl * Copyright (c) 2012 by Delphix. All rights reserved.
28130293Sscottl */
29130293Sscottl
30130293Sscottl#include <sys/zfs_context.h>
31130293Sscottl#include <sys/vdev_impl.h>
32130293Sscottl#include <sys/zio.h>
33130293Sscottl#include <sys/avl.h>
34130293Sscottl
35130293Sscottl/*
36130293Sscottl * These tunables are for performance analysis.
37130293Sscottl */
38130293Sscottl
39130293Sscottl/* The maximum number of I/Os concurrently pending to each device. */
40130293Sscottlint zfs_vdev_max_pending = 10;
41130293Sscottl
42130293Sscottl/*
43130293Sscottl * The initial number of I/Os pending to each device, before it starts ramping
44130293Sscottl * up to zfs_vdev_max_pending.
45130293Sscottl */
46130293Sscottlint zfs_vdev_min_pending = 4;
47130293Sscottl
48130293Sscottl/*
49130293Sscottl * The deadlines are grouped into buckets based on zfs_vdev_time_shift:
50130293Sscottl * deadline = pri + gethrtime() >> time_shift)
51130293Sscottl */
52130293Sscottlint zfs_vdev_time_shift = 29; /* each bucket is 0.537 seconds */
53130293Sscottl
54130293Sscottl/* exponential I/O issue ramp-up rate */
55130293Sscottlint zfs_vdev_ramp_rate = 2;
56130293Sscottl
57130293Sscottl/*
58130293Sscottl * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
59130293Sscottl * For read I/Os, we also aggregate across small adjacency gaps; for writes
60130293Sscottl * we include spans of optional I/Os to aid aggregation at the disk even when
61130293Sscottl * they aren't able to help us aggregate at this level.
62130293Sscottl */
63130293Sscottlint zfs_vdev_aggregation_limit = SPA_MAXBLOCKSIZE;
64130293Sscottlint zfs_vdev_read_gap_limit = 32 << 10;
65130293Sscottlint zfs_vdev_write_gap_limit = 4 << 10;
66130293Sscottl
67130293SscottlSYSCTL_DECL(_vfs_zfs_vdev);
68130293SscottlTUNABLE_INT("vfs.zfs.vdev.max_pending", &zfs_vdev_max_pending);
69130293SscottlSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, max_pending, CTLFLAG_RW,
70130293Sscottl    &zfs_vdev_max_pending, 0, "Maximum I/O requests pending on each device");
71130293SscottlTUNABLE_INT("vfs.zfs.vdev.min_pending", &zfs_vdev_min_pending);
72130293SscottlSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, min_pending, CTLFLAG_RW,
73130293Sscottl    &zfs_vdev_min_pending, 0,
74130293Sscottl    "Initial number of I/O requests pending to each device");
75130293SscottlTUNABLE_INT("vfs.zfs.vdev.time_shift", &zfs_vdev_time_shift);
76130293SscottlSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, time_shift, CTLFLAG_RW,
77130293Sscottl    &zfs_vdev_time_shift, 0, "Used for calculating I/O request deadline");
78130293SscottlTUNABLE_INT("vfs.zfs.vdev.ramp_rate", &zfs_vdev_ramp_rate);
79130293SscottlSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, ramp_rate, CTLFLAG_RW,
80130293Sscottl    &zfs_vdev_ramp_rate, 0, "Exponential I/O issue ramp-up rate");
81130293SscottlTUNABLE_INT("vfs.zfs.vdev.aggregation_limit", &zfs_vdev_aggregation_limit);
82130293SscottlSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RW,
83130293Sscottl    &zfs_vdev_aggregation_limit, 0,
84130293Sscottl    "I/O requests are aggregated up to this size");
85130293SscottlTUNABLE_INT("vfs.zfs.vdev.read_gap_limit", &zfs_vdev_read_gap_limit);
86130293SscottlSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RW,
87130293Sscottl    &zfs_vdev_read_gap_limit, 0,
88130293Sscottl    "Acceptable gap between two reads being aggregated");
89130293SscottlTUNABLE_INT("vfs.zfs.vdev.write_gap_limit", &zfs_vdev_write_gap_limit);
90130293SscottlSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RW,
91130293Sscottl    &zfs_vdev_write_gap_limit, 0,
92130293Sscottl    "Acceptable gap between two writes being aggregated");
93130293Sscottl
94130293Sscottl/*
95130293Sscottl * Virtual device vector for disk I/O scheduling.
96130293Sscottl */
97130293Sscottlint
98130293Sscottlvdev_queue_deadline_compare(const void *x1, const void *x2)
99130293Sscottl{
100130293Sscottl	const zio_t *z1 = x1;
101130293Sscottl	const zio_t *z2 = x2;
102130293Sscottl
103130293Sscottl	if (z1->io_deadline < z2->io_deadline)
104130293Sscottl		return (-1);
105130293Sscottl	if (z1->io_deadline > z2->io_deadline)
106130293Sscottl		return (1);
107130293Sscottl
108130293Sscottl	if (z1->io_offset < z2->io_offset)
109130293Sscottl		return (-1);
110130293Sscottl	if (z1->io_offset > z2->io_offset)
111130293Sscottl		return (1);
112130293Sscottl
113130293Sscottl	if (z1 < z2)
114130293Sscottl		return (-1);
115130293Sscottl	if (z1 > z2)
116130293Sscottl		return (1);
117130293Sscottl
118130293Sscottl	return (0);
119130293Sscottl}
120130293Sscottl
121130293Sscottlint
122130293Sscottlvdev_queue_offset_compare(const void *x1, const void *x2)
123130293Sscottl{
124130293Sscottl	const zio_t *z1 = x1;
125130293Sscottl	const zio_t *z2 = x2;
126130293Sscottl
127130293Sscottl	if (z1->io_offset < z2->io_offset)
128130293Sscottl		return (-1);
129130293Sscottl	if (z1->io_offset > z2->io_offset)
130130293Sscottl		return (1);
131130293Sscottl
132130293Sscottl	if (z1 < z2)
133130293Sscottl		return (-1);
134130293Sscottl	if (z1 > z2)
135130293Sscottl		return (1);
136130293Sscottl
137130293Sscottl	return (0);
138130293Sscottl}
139130293Sscottl
140130293Sscottlvoid
141130293Sscottlvdev_queue_init(vdev_t *vd)
142130293Sscottl{
143130293Sscottl	vdev_queue_t *vq = &vd->vdev_queue;
144130293Sscottl
145130293Sscottl	mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
146130293Sscottl
147130293Sscottl	avl_create(&vq->vq_deadline_tree, vdev_queue_deadline_compare,
148130293Sscottl	    sizeof (zio_t), offsetof(struct zio, io_deadline_node));
149130293Sscottl
150130293Sscottl	avl_create(&vq->vq_read_tree, vdev_queue_offset_compare,
151130293Sscottl	    sizeof (zio_t), offsetof(struct zio, io_offset_node));
152130293Sscottl
153130293Sscottl	avl_create(&vq->vq_write_tree, vdev_queue_offset_compare,
154130293Sscottl	    sizeof (zio_t), offsetof(struct zio, io_offset_node));
155130293Sscottl
156130293Sscottl	avl_create(&vq->vq_pending_tree, vdev_queue_offset_compare,
157130293Sscottl	    sizeof (zio_t), offsetof(struct zio, io_offset_node));
158130293Sscottl}
159130293Sscottl
160130293Sscottlvoid
161130293Sscottlvdev_queue_fini(vdev_t *vd)
162130293Sscottl{
163130293Sscottl	vdev_queue_t *vq = &vd->vdev_queue;
164130293Sscottl
165130293Sscottl	avl_destroy(&vq->vq_deadline_tree);
166130293Sscottl	avl_destroy(&vq->vq_read_tree);
167130293Sscottl	avl_destroy(&vq->vq_write_tree);
168130293Sscottl	avl_destroy(&vq->vq_pending_tree);
169130293Sscottl
170130293Sscottl	mutex_destroy(&vq->vq_lock);
171130293Sscottl}
172130293Sscottl
173130293Sscottlstatic void
174130293Sscottlvdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
175130293Sscottl{
176130293Sscottl	avl_add(&vq->vq_deadline_tree, zio);
177130293Sscottl	avl_add(zio->io_vdev_tree, zio);
178130293Sscottl}
179130293Sscottl
180130293Sscottlstatic void
181130293Sscottlvdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
182130293Sscottl{
183130293Sscottl	avl_remove(&vq->vq_deadline_tree, zio);
184130293Sscottl	avl_remove(zio->io_vdev_tree, zio);
185130293Sscottl}
186130293Sscottl
187130293Sscottlstatic void
188130293Sscottlvdev_queue_agg_io_done(zio_t *aio)
189130293Sscottl{
190130293Sscottl	zio_t *pio;
191130293Sscottl
192130293Sscottl	while ((pio = zio_walk_parents(aio)) != NULL)
193130293Sscottl		if (aio->io_type == ZIO_TYPE_READ)
194130293Sscottl			bcopy((char *)aio->io_data + (pio->io_offset -
195130293Sscottl			    aio->io_offset), pio->io_data, pio->io_size);
196130293Sscottl
197130293Sscottl	zio_buf_free(aio->io_data, aio->io_size);
198130293Sscottl}
199
200/*
201 * Compute the range spanned by two i/os, which is the endpoint of the last
202 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
203 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
204 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
205 */
206#define	IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
207#define	IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
208
209static zio_t *
210vdev_queue_io_to_issue(vdev_queue_t *vq, uint64_t pending_limit)
211{
212	zio_t *fio, *lio, *aio, *dio, *nio, *mio;
213	avl_tree_t *t;
214	int flags;
215	uint64_t maxspan = zfs_vdev_aggregation_limit;
216	uint64_t maxgap;
217	int stretch;
218
219again:
220	ASSERT(MUTEX_HELD(&vq->vq_lock));
221
222	if (avl_numnodes(&vq->vq_pending_tree) >= pending_limit ||
223	    avl_numnodes(&vq->vq_deadline_tree) == 0)
224		return (NULL);
225
226	fio = lio = avl_first(&vq->vq_deadline_tree);
227
228	t = fio->io_vdev_tree;
229	flags = fio->io_flags & ZIO_FLAG_AGG_INHERIT;
230	maxgap = (t == &vq->vq_read_tree) ? zfs_vdev_read_gap_limit : 0;
231
232	if (!(flags & ZIO_FLAG_DONT_AGGREGATE)) {
233		/*
234		 * We can aggregate I/Os that are sufficiently adjacent and of
235		 * the same flavor, as expressed by the AGG_INHERIT flags.
236		 * The latter requirement is necessary so that certain
237		 * attributes of the I/O, such as whether it's a normal I/O
238		 * or a scrub/resilver, can be preserved in the aggregate.
239		 * We can include optional I/Os, but don't allow them
240		 * to begin a range as they add no benefit in that situation.
241		 */
242
243		/*
244		 * We keep track of the last non-optional I/O.
245		 */
246		mio = (fio->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : fio;
247
248		/*
249		 * Walk backwards through sufficiently contiguous I/Os
250		 * recording the last non-option I/O.
251		 */
252		while ((dio = AVL_PREV(t, fio)) != NULL &&
253		    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
254		    IO_SPAN(dio, lio) <= maxspan &&
255		    IO_GAP(dio, fio) <= maxgap) {
256			fio = dio;
257			if (mio == NULL && !(fio->io_flags & ZIO_FLAG_OPTIONAL))
258				mio = fio;
259		}
260
261		/*
262		 * Skip any initial optional I/Os.
263		 */
264		while ((fio->io_flags & ZIO_FLAG_OPTIONAL) && fio != lio) {
265			fio = AVL_NEXT(t, fio);
266			ASSERT(fio != NULL);
267		}
268
269		/*
270		 * Walk forward through sufficiently contiguous I/Os.
271		 */
272		while ((dio = AVL_NEXT(t, lio)) != NULL &&
273		    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
274		    IO_SPAN(fio, dio) <= maxspan &&
275		    IO_GAP(lio, dio) <= maxgap) {
276			lio = dio;
277			if (!(lio->io_flags & ZIO_FLAG_OPTIONAL))
278				mio = lio;
279		}
280
281		/*
282		 * Now that we've established the range of the I/O aggregation
283		 * we must decide what to do with trailing optional I/Os.
284		 * For reads, there's nothing to do. While we are unable to
285		 * aggregate further, it's possible that a trailing optional
286		 * I/O would allow the underlying device to aggregate with
287		 * subsequent I/Os. We must therefore determine if the next
288		 * non-optional I/O is close enough to make aggregation
289		 * worthwhile.
290		 */
291		stretch = B_FALSE;
292		if (t != &vq->vq_read_tree && mio != NULL) {
293			nio = lio;
294			while ((dio = AVL_NEXT(t, nio)) != NULL &&
295			    IO_GAP(nio, dio) == 0 &&
296			    IO_GAP(mio, dio) <= zfs_vdev_write_gap_limit) {
297				nio = dio;
298				if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
299					stretch = B_TRUE;
300					break;
301				}
302			}
303		}
304
305		if (stretch) {
306			/* This may be a no-op. */
307			VERIFY((dio = AVL_NEXT(t, lio)) != NULL);
308			dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
309		} else {
310			while (lio != mio && lio != fio) {
311				ASSERT(lio->io_flags & ZIO_FLAG_OPTIONAL);
312				lio = AVL_PREV(t, lio);
313				ASSERT(lio != NULL);
314			}
315		}
316	}
317
318	if (fio != lio) {
319		uint64_t size = IO_SPAN(fio, lio);
320		ASSERT(size <= zfs_vdev_aggregation_limit);
321
322		aio = zio_vdev_delegated_io(fio->io_vd, fio->io_offset,
323		    zio_buf_alloc(size), size, fio->io_type, ZIO_PRIORITY_AGG,
324		    flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
325		    vdev_queue_agg_io_done, NULL);
326		aio->io_timestamp = fio->io_timestamp;
327
328		nio = fio;
329		do {
330			dio = nio;
331			nio = AVL_NEXT(t, dio);
332			ASSERT(dio->io_type == aio->io_type);
333			ASSERT(dio->io_vdev_tree == t);
334
335			if (dio->io_flags & ZIO_FLAG_NODATA) {
336				ASSERT(dio->io_type == ZIO_TYPE_WRITE);
337				bzero((char *)aio->io_data + (dio->io_offset -
338				    aio->io_offset), dio->io_size);
339			} else if (dio->io_type == ZIO_TYPE_WRITE) {
340				bcopy(dio->io_data, (char *)aio->io_data +
341				    (dio->io_offset - aio->io_offset),
342				    dio->io_size);
343			}
344
345			zio_add_child(dio, aio);
346			vdev_queue_io_remove(vq, dio);
347			zio_vdev_io_bypass(dio);
348			zio_execute(dio);
349		} while (dio != lio);
350
351		avl_add(&vq->vq_pending_tree, aio);
352
353		return (aio);
354	}
355
356	ASSERT(fio->io_vdev_tree == t);
357	vdev_queue_io_remove(vq, fio);
358
359	/*
360	 * If the I/O is or was optional and therefore has no data, we need to
361	 * simply discard it. We need to drop the vdev queue's lock to avoid a
362	 * deadlock that we could encounter since this I/O will complete
363	 * immediately.
364	 */
365	if (fio->io_flags & ZIO_FLAG_NODATA) {
366		mutex_exit(&vq->vq_lock);
367		zio_vdev_io_bypass(fio);
368		zio_execute(fio);
369		mutex_enter(&vq->vq_lock);
370		goto again;
371	}
372
373	avl_add(&vq->vq_pending_tree, fio);
374
375	return (fio);
376}
377
378zio_t *
379vdev_queue_io(zio_t *zio)
380{
381	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
382	zio_t *nio;
383
384	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
385
386	if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
387		return (zio);
388
389	zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
390
391	if (zio->io_type == ZIO_TYPE_READ)
392		zio->io_vdev_tree = &vq->vq_read_tree;
393	else
394		zio->io_vdev_tree = &vq->vq_write_tree;
395
396	mutex_enter(&vq->vq_lock);
397
398	zio->io_timestamp = gethrtime();
399	zio->io_deadline = (zio->io_timestamp >> zfs_vdev_time_shift) +
400	    zio->io_priority;
401
402	vdev_queue_io_add(vq, zio);
403
404	nio = vdev_queue_io_to_issue(vq, zfs_vdev_min_pending);
405
406	mutex_exit(&vq->vq_lock);
407
408	if (nio == NULL)
409		return (NULL);
410
411	if (nio->io_done == vdev_queue_agg_io_done) {
412		zio_nowait(nio);
413		return (NULL);
414	}
415
416	return (nio);
417}
418
419void
420vdev_queue_io_done(zio_t *zio)
421{
422	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
423
424	if (zio_injection_enabled)
425		delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
426
427	mutex_enter(&vq->vq_lock);
428
429	avl_remove(&vq->vq_pending_tree, zio);
430
431	vq->vq_io_complete_ts = gethrtime();
432
433	for (int i = 0; i < zfs_vdev_ramp_rate; i++) {
434		zio_t *nio = vdev_queue_io_to_issue(vq, zfs_vdev_max_pending);
435		if (nio == NULL)
436			break;
437		mutex_exit(&vq->vq_lock);
438		if (nio->io_done == vdev_queue_agg_io_done) {
439			zio_nowait(nio);
440		} else {
441			zio_vdev_io_reissue(nio);
442			zio_execute(nio);
443		}
444		mutex_enter(&vq->vq_lock);
445	}
446
447	mutex_exit(&vq->vq_lock);
448}
449