3f2793ba4f1623f7e1c0374c0a5ffd73295b8360
[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 /* 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         int i;
114
115         mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
116
117         avl_create(&vq->vq_deadline_tree, vdev_queue_deadline_compare,
118             sizeof (zio_t), offsetof(struct zio, io_deadline_node));
119
120         avl_create(&vq->vq_read_tree, vdev_queue_offset_compare,
121             sizeof (zio_t), offsetof(struct zio, io_offset_node));
122
123         avl_create(&vq->vq_write_tree, vdev_queue_offset_compare,
124             sizeof (zio_t), offsetof(struct zio, io_offset_node));
125
126         avl_create(&vq->vq_pending_tree, vdev_queue_offset_compare,
127             sizeof (zio_t), offsetof(struct zio, io_offset_node));
128
129         /*
130          * A list of buffers which can be used for aggregate I/O, this
131          * avoids the need to allocate them on demand when memory is low.
132          */
133         list_create(&vq->vq_io_list, sizeof (vdev_io_t),
134             offsetof(vdev_io_t, vi_node));
135
136         for (i = 0; i < zfs_vdev_max_pending; i++)
137                 list_insert_tail(&vq->vq_io_list, zio_vdev_alloc());
138 }
139
140 void
141 vdev_queue_fini(vdev_t *vd)
142 {
143         vdev_queue_t *vq = &vd->vdev_queue;
144         vdev_io_t *vi;
145
146         avl_destroy(&vq->vq_deadline_tree);
147         avl_destroy(&vq->vq_read_tree);
148         avl_destroy(&vq->vq_write_tree);
149         avl_destroy(&vq->vq_pending_tree);
150
151         while ((vi = list_head(&vq->vq_io_list)) != NULL) {
152                 list_remove(&vq->vq_io_list, vi);
153                 zio_vdev_free(vi);
154         }
155
156         list_destroy(&vq->vq_io_list);
157
158         mutex_destroy(&vq->vq_lock);
159 }
160
161 static void
162 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
163 {
164         avl_add(&vq->vq_deadline_tree, zio);
165         avl_add(zio->io_vdev_tree, zio);
166 }
167
168 static void
169 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
170 {
171         avl_remove(&vq->vq_deadline_tree, zio);
172         avl_remove(zio->io_vdev_tree, zio);
173 }
174
175 static void
176 vdev_queue_agg_io_done(zio_t *aio)
177 {
178         vdev_queue_t *vq = &aio->io_vd->vdev_queue;
179         vdev_io_t *vi = aio->io_data;
180         zio_t *pio;
181
182         while ((pio = zio_walk_parents(aio)) != NULL)
183                 if (aio->io_type == ZIO_TYPE_READ)
184                         bcopy((char *)aio->io_data + (pio->io_offset -
185                             aio->io_offset), pio->io_data, pio->io_size);
186
187         mutex_enter(&vq->vq_lock);
188         list_insert_tail(&vq->vq_io_list, vi);
189         mutex_exit(&vq->vq_lock);
190 }
191
192 /*
193  * Compute the range spanned by two i/os, which is the endpoint of the last
194  * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
195  * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
196  * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
197  */
198 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
199 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
200
201 static zio_t *
202 vdev_queue_io_to_issue(vdev_queue_t *vq, uint64_t pending_limit)
203 {
204         zio_t *fio, *lio, *aio, *dio, *nio, *mio;
205         avl_tree_t *t;
206         vdev_io_t *vi;
207         int flags;
208         uint64_t maxspan = MIN(zfs_vdev_aggregation_limit, SPA_MAXBLOCKSIZE);
209         uint64_t maxgap;
210         int stretch;
211
212 again:
213         ASSERT(MUTEX_HELD(&vq->vq_lock));
214
215         if (avl_numnodes(&vq->vq_pending_tree) >= pending_limit ||
216             avl_numnodes(&vq->vq_deadline_tree) == 0)
217                 return (NULL);
218
219         fio = lio = avl_first(&vq->vq_deadline_tree);
220
221         t = fio->io_vdev_tree;
222         flags = fio->io_flags & ZIO_FLAG_AGG_INHERIT;
223         maxgap = (t == &vq->vq_read_tree) ? zfs_vdev_read_gap_limit : 0;
224
225         vi = list_head(&vq->vq_io_list);
226         if (vi == NULL) {
227                 vi = zio_vdev_alloc();
228                 list_insert_head(&vq->vq_io_list, vi);
229         }
230
231         if (!(flags & ZIO_FLAG_DONT_AGGREGATE)) {
232                 /*
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.
240                  */
241
242                 /*
243                  * We keep track of the last non-optional I/O.
244                  */
245                 mio = (fio->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : fio;
246
247                 /*
248                  * Walk backwards through sufficiently contiguous I/Os
249                  * recording the last non-option I/O.
250                  */
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) {
255                         fio = dio;
256                         if (mio == NULL && !(fio->io_flags & ZIO_FLAG_OPTIONAL))
257                                 mio = fio;
258                 }
259
260                 /*
261                  * Skip any initial optional I/Os.
262                  */
263                 while ((fio->io_flags & ZIO_FLAG_OPTIONAL) && fio != lio) {
264                         fio = AVL_NEXT(t, fio);
265                         ASSERT(fio != NULL);
266                 }
267
268                 /*
269                  * Walk forward through sufficiently contiguous I/Os.
270                  */
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) {
275                         lio = dio;
276                         if (!(lio->io_flags & ZIO_FLAG_OPTIONAL))
277                                 mio = lio;
278                 }
279
280                 /*
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
288                  * worthwhile.
289                  */
290                 stretch = B_FALSE;
291                 if (t != &vq->vq_read_tree && mio != NULL) {
292                         nio = lio;
293                         while ((dio = AVL_NEXT(t, nio)) != NULL &&
294                             IO_GAP(nio, dio) == 0 &&
295                             IO_GAP(mio, dio) <= zfs_vdev_write_gap_limit) {
296                                 nio = dio;
297                                 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
298                                         stretch = B_TRUE;
299                                         break;
300                                 }
301                         }
302                 }
303
304                 if (stretch) {
305                         /* This may be a no-op. */
306                         VERIFY((dio = AVL_NEXT(t, lio)) != NULL);
307                         dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
308                 } else {
309                         while (lio != mio && lio != fio) {
310                                 ASSERT(lio->io_flags & ZIO_FLAG_OPTIONAL);
311                                 lio = AVL_PREV(t, lio);
312                                 ASSERT(lio != NULL);
313                         }
314                 }
315         }
316
317         if (fio != lio) {
318                 uint64_t size = IO_SPAN(fio, lio);
319                 ASSERT(size <= maxspan);
320                 ASSERT(vi != NULL);
321
322                 aio = zio_vdev_delegated_io(fio->io_vd, fio->io_offset,
323                     vi, 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                 list_remove(&vq->vq_io_list, vi);
353
354                 return (aio);
355         }
356
357         ASSERT(fio->io_vdev_tree == t);
358         vdev_queue_io_remove(vq, fio);
359
360         /*
361          * If the I/O is or was optional and therefore has no data, we need to
362          * simply discard it. We need to drop the vdev queue's lock to avoid a
363          * deadlock that we could encounter since this I/O will complete
364          * immediately.
365          */
366         if (fio->io_flags & ZIO_FLAG_NODATA) {
367                 mutex_exit(&vq->vq_lock);
368                 zio_vdev_io_bypass(fio);
369                 zio_execute(fio);
370                 mutex_enter(&vq->vq_lock);
371                 goto again;
372         }
373
374         avl_add(&vq->vq_pending_tree, fio);
375
376         return (fio);
377 }
378
379 zio_t *
380 vdev_queue_io(zio_t *zio)
381 {
382         vdev_queue_t *vq = &zio->io_vd->vdev_queue;
383         zio_t *nio;
384
385         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
386
387         if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
388                 return (zio);
389
390         zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
391
392         if (zio->io_type == ZIO_TYPE_READ)
393                 zio->io_vdev_tree = &vq->vq_read_tree;
394         else
395                 zio->io_vdev_tree = &vq->vq_write_tree;
396
397         mutex_enter(&vq->vq_lock);
398
399         zio->io_timestamp = ddi_get_lbolt64();
400         zio->io_deadline = (zio->io_timestamp >> zfs_vdev_time_shift) +
401             zio->io_priority;
402
403         vdev_queue_io_add(vq, zio);
404
405         nio = vdev_queue_io_to_issue(vq, zfs_vdev_min_pending);
406
407         mutex_exit(&vq->vq_lock);
408
409         if (nio == NULL)
410                 return (NULL);
411
412         if (nio->io_done == vdev_queue_agg_io_done) {
413                 zio_nowait(nio);
414                 return (NULL);
415         }
416
417         return (nio);
418 }
419
420 void
421 vdev_queue_io_done(zio_t *zio)
422 {
423         vdev_queue_t *vq = &zio->io_vd->vdev_queue;
424         int i;
425
426         if (zio_injection_enabled)
427                 delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
428
429         mutex_enter(&vq->vq_lock);
430
431         avl_remove(&vq->vq_pending_tree, zio);
432
433         zio->io_delta = ddi_get_lbolt64() - zio->io_timestamp;
434         vq->vq_io_complete_ts = ddi_get_lbolt64();
435         vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
436
437         for (i = 0; i < zfs_vdev_ramp_rate; i++) {
438                 zio_t *nio = vdev_queue_io_to_issue(vq, zfs_vdev_max_pending);
439                 if (nio == NULL)
440                         break;
441                 mutex_exit(&vq->vq_lock);
442                 if (nio->io_done == vdev_queue_agg_io_done) {
443                         zio_nowait(nio);
444                 } else {
445                         zio_vdev_io_reissue(nio);
446                         zio_execute(nio);
447                 }
448                 mutex_enter(&vq->vq_lock);
449         }
450
451         mutex_exit(&vq->vq_lock);
452 }
453
454 #if defined(_KERNEL) && defined(HAVE_SPL)
455 module_param(zfs_vdev_max_pending, int, 0644);
456 MODULE_PARM_DESC(zfs_vdev_max_pending, "Max pending per-vdev I/Os");
457
458 module_param(zfs_vdev_min_pending, int, 0644);
459 MODULE_PARM_DESC(zfs_vdev_min_pending, "Min pending per-vdev I/Os");
460
461 module_param(zfs_vdev_aggregation_limit, int, 0644);
462 MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
463
464 module_param(zfs_vdev_time_shift, int, 0644);
465 MODULE_PARM_DESC(zfs_vdev_time_shift, "Deadline time shift for vdev I/O");
466
467 module_param(zfs_vdev_ramp_rate, int, 0644);
468 MODULE_PARM_DESC(zfs_vdev_ramp_rate, "Exponential I/O issue ramp-up rate");
469
470 module_param(zfs_vdev_read_gap_limit, int, 0644);
471 MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
472
473 module_param(zfs_vdev_write_gap_limit, int, 0644);
474 MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
475 #endif