]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev_queue.c
Illumos #3618 ::zio dcmd does not show timestamp data
[mirror_zfs.git] / module / zfs / vdev_queue.c
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 /*
48 * The deadlines are grouped into buckets based on zfs_vdev_time_shift:
49 * deadline = pri + gethrtime() >> time_shift)
50 */
51 int zfs_vdev_time_shift = 29; /* each bucket is 0.537 seconds */
52
53 /* exponential I/O issue ramp-up rate */
54 int zfs_vdev_ramp_rate = 2;
55
56 /*
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.
61 */
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;
65
66 /*
67 * Virtual device vector for disk I/O scheduling.
68 */
69 int
70 vdev_queue_deadline_compare(const void *x1, const void *x2)
71 {
72 const zio_t *z1 = x1;
73 const zio_t *z2 = x2;
74
75 if (z1->io_deadline < z2->io_deadline)
76 return (-1);
77 if (z1->io_deadline > z2->io_deadline)
78 return (1);
79
80 if (z1->io_offset < z2->io_offset)
81 return (-1);
82 if (z1->io_offset > z2->io_offset)
83 return (1);
84
85 if (z1 < z2)
86 return (-1);
87 if (z1 > z2)
88 return (1);
89
90 return (0);
91 }
92
93 int
94 vdev_queue_offset_compare(const void *x1, const void *x2)
95 {
96 const zio_t *z1 = x1;
97 const zio_t *z2 = x2;
98
99 if (z1->io_offset < z2->io_offset)
100 return (-1);
101 if (z1->io_offset > z2->io_offset)
102 return (1);
103
104 if (z1 < z2)
105 return (-1);
106 if (z1 > z2)
107 return (1);
108
109 return (0);
110 }
111
112 void
113 vdev_queue_init(vdev_t *vd)
114 {
115 vdev_queue_t *vq = &vd->vdev_queue;
116 int i;
117
118 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
119
120 avl_create(&vq->vq_deadline_tree, vdev_queue_deadline_compare,
121 sizeof (zio_t), offsetof(struct zio, io_deadline_node));
122
123 avl_create(&vq->vq_read_tree, vdev_queue_offset_compare,
124 sizeof (zio_t), offsetof(struct zio, io_offset_node));
125
126 avl_create(&vq->vq_write_tree, vdev_queue_offset_compare,
127 sizeof (zio_t), offsetof(struct zio, io_offset_node));
128
129 avl_create(&vq->vq_pending_tree, vdev_queue_offset_compare,
130 sizeof (zio_t), offsetof(struct zio, io_offset_node));
131
132 /*
133 * A list of buffers which can be used for aggregate I/O, this
134 * avoids the need to allocate them on demand when memory is low.
135 */
136 list_create(&vq->vq_io_list, sizeof (vdev_io_t),
137 offsetof(vdev_io_t, vi_node));
138
139 for (i = 0; i < zfs_vdev_max_pending; i++)
140 list_insert_tail(&vq->vq_io_list, zio_vdev_alloc());
141 }
142
143 void
144 vdev_queue_fini(vdev_t *vd)
145 {
146 vdev_queue_t *vq = &vd->vdev_queue;
147 vdev_io_t *vi;
148
149 avl_destroy(&vq->vq_deadline_tree);
150 avl_destroy(&vq->vq_read_tree);
151 avl_destroy(&vq->vq_write_tree);
152 avl_destroy(&vq->vq_pending_tree);
153
154 while ((vi = list_head(&vq->vq_io_list)) != NULL) {
155 list_remove(&vq->vq_io_list, vi);
156 zio_vdev_free(vi);
157 }
158
159 list_destroy(&vq->vq_io_list);
160
161 mutex_destroy(&vq->vq_lock);
162 }
163
164 static void
165 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
166 {
167 avl_add(&vq->vq_deadline_tree, zio);
168 avl_add(zio->io_vdev_tree, zio);
169 }
170
171 static void
172 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
173 {
174 avl_remove(&vq->vq_deadline_tree, zio);
175 avl_remove(zio->io_vdev_tree, zio);
176 }
177
178 static void
179 vdev_queue_agg_io_done(zio_t *aio)
180 {
181 vdev_queue_t *vq = &aio->io_vd->vdev_queue;
182 vdev_io_t *vi = aio->io_data;
183 zio_t *pio;
184
185 while ((pio = zio_walk_parents(aio)) != NULL)
186 if (aio->io_type == ZIO_TYPE_READ)
187 bcopy((char *)aio->io_data + (pio->io_offset -
188 aio->io_offset), pio->io_data, pio->io_size);
189
190 mutex_enter(&vq->vq_lock);
191 list_insert_tail(&vq->vq_io_list, vi);
192 mutex_exit(&vq->vq_lock);
193 }
194
195 /*
196 * Compute the range spanned by two i/os, which is the endpoint of the last
197 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
198 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
199 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
200 */
201 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
202 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
203
204 static zio_t *
205 vdev_queue_io_to_issue(vdev_queue_t *vq, uint64_t pending_limit)
206 {
207 zio_t *fio, *lio, *aio, *dio, *nio, *mio;
208 avl_tree_t *t;
209 vdev_io_t *vi;
210 int flags;
211 uint64_t maxspan = MIN(zfs_vdev_aggregation_limit, SPA_MAXBLOCKSIZE);
212 uint64_t maxgap;
213 int stretch;
214
215 again:
216 ASSERT(MUTEX_HELD(&vq->vq_lock));
217
218 if (avl_numnodes(&vq->vq_pending_tree) >= pending_limit ||
219 avl_numnodes(&vq->vq_deadline_tree) == 0)
220 return (NULL);
221
222 fio = lio = avl_first(&vq->vq_deadline_tree);
223
224 t = fio->io_vdev_tree;
225 flags = fio->io_flags & ZIO_FLAG_AGG_INHERIT;
226 maxgap = (t == &vq->vq_read_tree) ? zfs_vdev_read_gap_limit : 0;
227
228 vi = list_head(&vq->vq_io_list);
229 if (vi == NULL) {
230 vi = zio_vdev_alloc();
231 list_insert_head(&vq->vq_io_list, vi);
232 }
233
234 if (!(flags & ZIO_FLAG_DONT_AGGREGATE)) {
235 /*
236 * We can aggregate I/Os that are sufficiently adjacent and of
237 * the same flavor, as expressed by the AGG_INHERIT flags.
238 * The latter requirement is necessary so that certain
239 * attributes of the I/O, such as whether it's a normal I/O
240 * or a scrub/resilver, can be preserved in the aggregate.
241 * We can include optional I/Os, but don't allow them
242 * to begin a range as they add no benefit in that situation.
243 */
244
245 /*
246 * We keep track of the last non-optional I/O.
247 */
248 mio = (fio->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : fio;
249
250 /*
251 * Walk backwards through sufficiently contiguous I/Os
252 * recording the last non-option I/O.
253 */
254 while ((dio = AVL_PREV(t, fio)) != NULL &&
255 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
256 IO_SPAN(dio, lio) <= maxspan &&
257 IO_GAP(dio, fio) <= maxgap) {
258 fio = dio;
259 if (mio == NULL && !(fio->io_flags & ZIO_FLAG_OPTIONAL))
260 mio = fio;
261 }
262
263 /*
264 * Skip any initial optional I/Os.
265 */
266 while ((fio->io_flags & ZIO_FLAG_OPTIONAL) && fio != lio) {
267 fio = AVL_NEXT(t, fio);
268 ASSERT(fio != NULL);
269 }
270
271 /*
272 * Walk forward through sufficiently contiguous I/Os.
273 */
274 while ((dio = AVL_NEXT(t, lio)) != NULL &&
275 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
276 IO_SPAN(fio, dio) <= maxspan &&
277 IO_GAP(lio, dio) <= maxgap) {
278 lio = dio;
279 if (!(lio->io_flags & ZIO_FLAG_OPTIONAL))
280 mio = lio;
281 }
282
283 /*
284 * Now that we've established the range of the I/O aggregation
285 * we must decide what to do with trailing optional I/Os.
286 * For reads, there's nothing to do. While we are unable to
287 * aggregate further, it's possible that a trailing optional
288 * I/O would allow the underlying device to aggregate with
289 * subsequent I/Os. We must therefore determine if the next
290 * non-optional I/O is close enough to make aggregation
291 * worthwhile.
292 */
293 stretch = B_FALSE;
294 if (t != &vq->vq_read_tree && mio != NULL) {
295 nio = lio;
296 while ((dio = AVL_NEXT(t, nio)) != NULL &&
297 IO_GAP(nio, dio) == 0 &&
298 IO_GAP(mio, dio) <= zfs_vdev_write_gap_limit) {
299 nio = dio;
300 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
301 stretch = B_TRUE;
302 break;
303 }
304 }
305 }
306
307 if (stretch) {
308 /* This may be a no-op. */
309 VERIFY((dio = AVL_NEXT(t, lio)) != NULL);
310 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
311 } else {
312 while (lio != mio && lio != fio) {
313 ASSERT(lio->io_flags & ZIO_FLAG_OPTIONAL);
314 lio = AVL_PREV(t, lio);
315 ASSERT(lio != NULL);
316 }
317 }
318 }
319
320 if (fio != lio) {
321 uint64_t size = IO_SPAN(fio, lio);
322 ASSERT(size <= maxspan);
323 ASSERT(vi != NULL);
324
325 aio = zio_vdev_delegated_io(fio->io_vd, fio->io_offset,
326 vi, size, fio->io_type, ZIO_PRIORITY_AGG,
327 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
328 vdev_queue_agg_io_done, NULL);
329 aio->io_timestamp = fio->io_timestamp;
330
331 nio = fio;
332 do {
333 dio = nio;
334 nio = AVL_NEXT(t, dio);
335 ASSERT(dio->io_type == aio->io_type);
336 ASSERT(dio->io_vdev_tree == t);
337
338 if (dio->io_flags & ZIO_FLAG_NODATA) {
339 ASSERT(dio->io_type == ZIO_TYPE_WRITE);
340 bzero((char *)aio->io_data + (dio->io_offset -
341 aio->io_offset), dio->io_size);
342 } else if (dio->io_type == ZIO_TYPE_WRITE) {
343 bcopy(dio->io_data, (char *)aio->io_data +
344 (dio->io_offset - aio->io_offset),
345 dio->io_size);
346 }
347
348 zio_add_child(dio, aio);
349 vdev_queue_io_remove(vq, dio);
350 zio_vdev_io_bypass(dio);
351 zio_execute(dio);
352 } while (dio != lio);
353
354 avl_add(&vq->vq_pending_tree, aio);
355 list_remove(&vq->vq_io_list, vi);
356
357 return (aio);
358 }
359
360 ASSERT(fio->io_vdev_tree == t);
361 vdev_queue_io_remove(vq, fio);
362
363 /*
364 * If the I/O is or was optional and therefore has no data, we need to
365 * simply discard it. We need to drop the vdev queue's lock to avoid a
366 * deadlock that we could encounter since this I/O will complete
367 * immediately.
368 */
369 if (fio->io_flags & ZIO_FLAG_NODATA) {
370 mutex_exit(&vq->vq_lock);
371 zio_vdev_io_bypass(fio);
372 zio_execute(fio);
373 mutex_enter(&vq->vq_lock);
374 goto again;
375 }
376
377 avl_add(&vq->vq_pending_tree, fio);
378
379 return (fio);
380 }
381
382 zio_t *
383 vdev_queue_io(zio_t *zio)
384 {
385 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
386 zio_t *nio;
387
388 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
389
390 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
391 return (zio);
392
393 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
394
395 if (zio->io_type == ZIO_TYPE_READ)
396 zio->io_vdev_tree = &vq->vq_read_tree;
397 else
398 zio->io_vdev_tree = &vq->vq_write_tree;
399
400 mutex_enter(&vq->vq_lock);
401
402 zio->io_timestamp = gethrtime();
403 zio->io_deadline = (zio->io_timestamp >> zfs_vdev_time_shift) +
404 zio->io_priority;
405
406 vdev_queue_io_add(vq, zio);
407
408 nio = vdev_queue_io_to_issue(vq, zfs_vdev_min_pending);
409
410 mutex_exit(&vq->vq_lock);
411
412 if (nio == NULL)
413 return (NULL);
414
415 if (nio->io_done == vdev_queue_agg_io_done) {
416 zio_nowait(nio);
417 return (NULL);
418 }
419
420 return (nio);
421 }
422
423 void
424 vdev_queue_io_done(zio_t *zio)
425 {
426 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
427 int i;
428
429 if (zio_injection_enabled)
430 delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
431
432 mutex_enter(&vq->vq_lock);
433
434 avl_remove(&vq->vq_pending_tree, zio);
435
436 zio->io_delta = gethrtime() - zio->io_timestamp;
437 vq->vq_io_complete_ts = gethrtime();
438 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
439
440 for (i = 0; i < zfs_vdev_ramp_rate; i++) {
441 zio_t *nio = vdev_queue_io_to_issue(vq, zfs_vdev_max_pending);
442 if (nio == NULL)
443 break;
444 mutex_exit(&vq->vq_lock);
445 if (nio->io_done == vdev_queue_agg_io_done) {
446 zio_nowait(nio);
447 } else {
448 zio_vdev_io_reissue(nio);
449 zio_execute(nio);
450 }
451 mutex_enter(&vq->vq_lock);
452 }
453
454 mutex_exit(&vq->vq_lock);
455 }
456
457 #if defined(_KERNEL) && defined(HAVE_SPL)
458 module_param(zfs_vdev_max_pending, int, 0644);
459 MODULE_PARM_DESC(zfs_vdev_max_pending, "Max pending per-vdev I/Os");
460
461 module_param(zfs_vdev_min_pending, int, 0644);
462 MODULE_PARM_DESC(zfs_vdev_min_pending, "Min pending per-vdev I/Os");
463
464 module_param(zfs_vdev_aggregation_limit, int, 0644);
465 MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
466
467 module_param(zfs_vdev_time_shift, int, 0644);
468 MODULE_PARM_DESC(zfs_vdev_time_shift, "Deadline time shift for vdev I/O");
469
470 module_param(zfs_vdev_ramp_rate, int, 0644);
471 MODULE_PARM_DESC(zfs_vdev_ramp_rate, "Exponential I/O issue ramp-up rate");
472
473 module_param(zfs_vdev_read_gap_limit, int, 0644);
474 MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
475
476 module_param(zfs_vdev_write_gap_limit, int, 0644);
477 MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
478 #endif