xref: /titanic_50/usr/src/uts/common/fs/zfs/vdev_queue.c (revision f5c2e7ea56aaa46a9976476fb0cb1f02b9426f07)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c) 2012 by Delphix. All rights reserved.
28  */
29 
30 #include <sys/zfs_context.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/zio.h>
33 #include <sys/avl.h>
34 
35 /*
36  * These tunables are for performance analysis.
37  */
38 /*
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
42  * max_pending).
43  */
44 int zfs_vdev_max_pending = 10;
45 int zfs_vdev_min_pending = 4;
46 
47 /* deadline = pri + ddi_get_lbolt64() >> time_shift) */
48 int zfs_vdev_time_shift = 6;
49 
50 /* exponential I/O issue ramp-up rate */
51 int zfs_vdev_ramp_rate = 2;
52 
53 /*
54  * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
55  * For read I/Os, we also aggregate across small adjacency gaps; for writes
56  * we include spans of optional I/Os to aid aggregation at the disk even when
57  * they aren't able to help us aggregate at this level.
58  */
59 int zfs_vdev_aggregation_limit = SPA_MAXBLOCKSIZE;
60 int zfs_vdev_read_gap_limit = 32 << 10;
61 int zfs_vdev_write_gap_limit = 4 << 10;
62 
63 /*
64  * Virtual device vector for disk I/O scheduling.
65  */
66 int
67 vdev_queue_deadline_compare(const void *x1, const void *x2)
68 {
69 	const zio_t *z1 = x1;
70 	const zio_t *z2 = x2;
71 
72 	if (z1->io_deadline < z2->io_deadline)
73 		return (-1);
74 	if (z1->io_deadline > z2->io_deadline)
75 		return (1);
76 
77 	if (z1->io_offset < z2->io_offset)
78 		return (-1);
79 	if (z1->io_offset > z2->io_offset)
80 		return (1);
81 
82 	if (z1 < z2)
83 		return (-1);
84 	if (z1 > z2)
85 		return (1);
86 
87 	return (0);
88 }
89 
90 int
91 vdev_queue_offset_compare(const void *x1, const void *x2)
92 {
93 	const zio_t *z1 = x1;
94 	const zio_t *z2 = x2;
95 
96 	if (z1->io_offset < z2->io_offset)
97 		return (-1);
98 	if (z1->io_offset > z2->io_offset)
99 		return (1);
100 
101 	if (z1 < z2)
102 		return (-1);
103 	if (z1 > z2)
104 		return (1);
105 
106 	return (0);
107 }
108 
109 void
110 vdev_queue_init(vdev_t *vd)
111 {
112 	vdev_queue_t *vq = &vd->vdev_queue;
113 
114 	mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
115 
116 	avl_create(&vq->vq_deadline_tree, vdev_queue_deadline_compare,
117 	    sizeof (zio_t), offsetof(struct zio, io_deadline_node));
118 
119 	avl_create(&vq->vq_read_tree, vdev_queue_offset_compare,
120 	    sizeof (zio_t), offsetof(struct zio, io_offset_node));
121 
122 	avl_create(&vq->vq_write_tree, vdev_queue_offset_compare,
123 	    sizeof (zio_t), offsetof(struct zio, io_offset_node));
124 
125 	avl_create(&vq->vq_pending_tree, vdev_queue_offset_compare,
126 	    sizeof (zio_t), offsetof(struct zio, io_offset_node));
127 }
128 
129 void
130 vdev_queue_fini(vdev_t *vd)
131 {
132 	vdev_queue_t *vq = &vd->vdev_queue;
133 
134 	avl_destroy(&vq->vq_deadline_tree);
135 	avl_destroy(&vq->vq_read_tree);
136 	avl_destroy(&vq->vq_write_tree);
137 	avl_destroy(&vq->vq_pending_tree);
138 
139 	mutex_destroy(&vq->vq_lock);
140 }
141 
142 static void
143 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
144 {
145 	avl_add(&vq->vq_deadline_tree, zio);
146 	avl_add(zio->io_vdev_tree, zio);
147 }
148 
149 static void
150 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
151 {
152 	avl_remove(&vq->vq_deadline_tree, zio);
153 	avl_remove(zio->io_vdev_tree, zio);
154 }
155 
156 static void
157 vdev_queue_agg_io_done(zio_t *aio)
158 {
159 	zio_t *pio;
160 
161 	while ((pio = zio_walk_parents(aio)) != NULL)
162 		if (aio->io_type == ZIO_TYPE_READ)
163 			bcopy((char *)aio->io_data + (pio->io_offset -
164 			    aio->io_offset), pio->io_data, pio->io_size);
165 
166 	zio_buf_free(aio->io_data, aio->io_size);
167 }
168 
169 /*
170  * Compute the range spanned by two i/os, which is the endpoint of the last
171  * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
172  * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
173  * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
174  */
175 #define	IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
176 #define	IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
177 
178 static zio_t *
179 vdev_queue_io_to_issue(vdev_queue_t *vq, uint64_t pending_limit)
180 {
181 	zio_t *fio, *lio, *aio, *dio, *nio, *mio;
182 	avl_tree_t *t;
183 	int flags;
184 	uint64_t maxspan = zfs_vdev_aggregation_limit;
185 	uint64_t maxgap;
186 	int stretch;
187 
188 again:
189 	ASSERT(MUTEX_HELD(&vq->vq_lock));
190 
191 	if (avl_numnodes(&vq->vq_pending_tree) >= pending_limit ||
192 	    avl_numnodes(&vq->vq_deadline_tree) == 0)
193 		return (NULL);
194 
195 	fio = lio = avl_first(&vq->vq_deadline_tree);
196 
197 	t = fio->io_vdev_tree;
198 	flags = fio->io_flags & ZIO_FLAG_AGG_INHERIT;
199 	maxgap = (t == &vq->vq_read_tree) ? zfs_vdev_read_gap_limit : 0;
200 
201 	if (!(flags & ZIO_FLAG_DONT_AGGREGATE)) {
202 		/*
203 		 * We can aggregate I/Os that are sufficiently adjacent and of
204 		 * the same flavor, as expressed by the AGG_INHERIT flags.
205 		 * The latter requirement is necessary so that certain
206 		 * attributes of the I/O, such as whether it's a normal I/O
207 		 * or a scrub/resilver, can be preserved in the aggregate.
208 		 * We can include optional I/Os, but don't allow them
209 		 * to begin a range as they add no benefit in that situation.
210 		 */
211 
212 		/*
213 		 * We keep track of the last non-optional I/O.
214 		 */
215 		mio = (fio->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : fio;
216 
217 		/*
218 		 * Walk backwards through sufficiently contiguous I/Os
219 		 * recording the last non-option I/O.
220 		 */
221 		while ((dio = AVL_PREV(t, fio)) != NULL &&
222 		    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
223 		    IO_SPAN(dio, lio) <= maxspan &&
224 		    IO_GAP(dio, fio) <= maxgap) {
225 			fio = dio;
226 			if (mio == NULL && !(fio->io_flags & ZIO_FLAG_OPTIONAL))
227 				mio = fio;
228 		}
229 
230 		/*
231 		 * Skip any initial optional I/Os.
232 		 */
233 		while ((fio->io_flags & ZIO_FLAG_OPTIONAL) && fio != lio) {
234 			fio = AVL_NEXT(t, fio);
235 			ASSERT(fio != NULL);
236 		}
237 
238 		/*
239 		 * Walk forward through sufficiently contiguous I/Os.
240 		 */
241 		while ((dio = AVL_NEXT(t, lio)) != NULL &&
242 		    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
243 		    IO_SPAN(fio, dio) <= maxspan &&
244 		    IO_GAP(lio, dio) <= maxgap) {
245 			lio = dio;
246 			if (!(lio->io_flags & ZIO_FLAG_OPTIONAL))
247 				mio = lio;
248 		}
249 
250 		/*
251 		 * Now that we've established the range of the I/O aggregation
252 		 * we must decide what to do with trailing optional I/Os.
253 		 * For reads, there's nothing to do. While we are unable to
254 		 * aggregate further, it's possible that a trailing optional
255 		 * I/O would allow the underlying device to aggregate with
256 		 * subsequent I/Os. We must therefore determine if the next
257 		 * non-optional I/O is close enough to make aggregation
258 		 * worthwhile.
259 		 */
260 		stretch = B_FALSE;
261 		if (t != &vq->vq_read_tree && mio != NULL) {
262 			nio = lio;
263 			while ((dio = AVL_NEXT(t, nio)) != NULL &&
264 			    IO_GAP(nio, dio) == 0 &&
265 			    IO_GAP(mio, dio) <= zfs_vdev_write_gap_limit) {
266 				nio = dio;
267 				if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
268 					stretch = B_TRUE;
269 					break;
270 				}
271 			}
272 		}
273 
274 		if (stretch) {
275 			/* This may be a no-op. */
276 			VERIFY((dio = AVL_NEXT(t, lio)) != NULL);
277 			dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
278 		} else {
279 			while (lio != mio && lio != fio) {
280 				ASSERT(lio->io_flags & ZIO_FLAG_OPTIONAL);
281 				lio = AVL_PREV(t, lio);
282 				ASSERT(lio != NULL);
283 			}
284 		}
285 	}
286 
287 	if (fio != lio) {
288 		uint64_t size = IO_SPAN(fio, lio);
289 		ASSERT(size <= zfs_vdev_aggregation_limit);
290 
291 		aio = zio_vdev_delegated_io(fio->io_vd, fio->io_offset,
292 		    zio_buf_alloc(size), size, fio->io_type, ZIO_PRIORITY_AGG,
293 		    flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
294 		    vdev_queue_agg_io_done, NULL);
295 		aio->io_timestamp = fio->io_timestamp;
296 
297 		nio = fio;
298 		do {
299 			dio = nio;
300 			nio = AVL_NEXT(t, dio);
301 			ASSERT(dio->io_type == aio->io_type);
302 			ASSERT(dio->io_vdev_tree == t);
303 
304 			if (dio->io_flags & ZIO_FLAG_NODATA) {
305 				ASSERT(dio->io_type == ZIO_TYPE_WRITE);
306 				bzero((char *)aio->io_data + (dio->io_offset -
307 				    aio->io_offset), dio->io_size);
308 			} else if (dio->io_type == ZIO_TYPE_WRITE) {
309 				bcopy(dio->io_data, (char *)aio->io_data +
310 				    (dio->io_offset - aio->io_offset),
311 				    dio->io_size);
312 			}
313 
314 			zio_add_child(dio, aio);
315 			vdev_queue_io_remove(vq, dio);
316 			zio_vdev_io_bypass(dio);
317 			zio_execute(dio);
318 		} while (dio != lio);
319 
320 		avl_add(&vq->vq_pending_tree, aio);
321 
322 		return (aio);
323 	}
324 
325 	ASSERT(fio->io_vdev_tree == t);
326 	vdev_queue_io_remove(vq, fio);
327 
328 	/*
329 	 * If the I/O is or was optional and therefore has no data, we need to
330 	 * simply discard it. We need to drop the vdev queue's lock to avoid a
331 	 * deadlock that we could encounter since this I/O will complete
332 	 * immediately.
333 	 */
334 	if (fio->io_flags & ZIO_FLAG_NODATA) {
335 		mutex_exit(&vq->vq_lock);
336 		zio_vdev_io_bypass(fio);
337 		zio_execute(fio);
338 		mutex_enter(&vq->vq_lock);
339 		goto again;
340 	}
341 
342 	avl_add(&vq->vq_pending_tree, fio);
343 
344 	return (fio);
345 }
346 
347 zio_t *
348 vdev_queue_io(zio_t *zio)
349 {
350 	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
351 	zio_t *nio;
352 
353 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
354 
355 	if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
356 		return (zio);
357 
358 	zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
359 
360 	if (zio->io_type == ZIO_TYPE_READ)
361 		zio->io_vdev_tree = &vq->vq_read_tree;
362 	else
363 		zio->io_vdev_tree = &vq->vq_write_tree;
364 
365 	mutex_enter(&vq->vq_lock);
366 
367 	zio->io_timestamp = ddi_get_lbolt64();
368 	zio->io_deadline = (zio->io_timestamp >> zfs_vdev_time_shift) +
369 	    zio->io_priority;
370 
371 	vdev_queue_io_add(vq, zio);
372 
373 	nio = vdev_queue_io_to_issue(vq, zfs_vdev_min_pending);
374 
375 	mutex_exit(&vq->vq_lock);
376 
377 	if (nio == NULL)
378 		return (NULL);
379 
380 	if (nio->io_done == vdev_queue_agg_io_done) {
381 		zio_nowait(nio);
382 		return (NULL);
383 	}
384 
385 	return (nio);
386 }
387 
388 void
389 vdev_queue_io_done(zio_t *zio)
390 {
391 	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
392 
393 	if (zio_injection_enabled)
394 		delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
395 
396 	mutex_enter(&vq->vq_lock);
397 
398 	avl_remove(&vq->vq_pending_tree, zio);
399 
400 	vq->vq_io_complete_ts = ddi_get_lbolt64();
401 	vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
402 
403 	for (int i = 0; i < zfs_vdev_ramp_rate; i++) {
404 		zio_t *nio = vdev_queue_io_to_issue(vq, zfs_vdev_max_pending);
405 		if (nio == NULL)
406 			break;
407 		mutex_exit(&vq->vq_lock);
408 		if (nio->io_done == vdev_queue_agg_io_done) {
409 			zio_nowait(nio);
410 		} else {
411 			zio_vdev_io_reissue(nio);
412 			zio_execute(nio);
413 		}
414 		mutex_enter(&vq->vq_lock);
415 	}
416 
417 	mutex_exit(&vq->vq_lock);
418 }
419