]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/vdev_queue.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / cddl / contrib / opensolaris / uts / common / fs / 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 #include <sys/zfs_context.h>
27 #include <sys/vdev_impl.h>
28 #include <sys/zio.h>
29 #include <sys/avl.h>
30
31 /*
32  * These tunables are for performance analysis.
33  */
34 /*
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
38  * max_pending).
39  */
40 int zfs_vdev_max_pending = 10;
41 int zfs_vdev_min_pending = 4;
42
43 /* deadline = pri + ddi_get_lbolt64() >> time_shift) */
44 int zfs_vdev_time_shift = 6;
45
46 /* exponential I/O issue ramp-up rate */
47 int zfs_vdev_ramp_rate = 2;
48
49 /*
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.
54  */
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;
58
59 SYSCTL_DECL(_vfs_zfs_vdev);
60 TUNABLE_INT("vfs.zfs.vdev.max_pending", &zfs_vdev_max_pending);
61 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, max_pending, CTLFLAG_RW,
62     &zfs_vdev_max_pending, 0, "Maximum I/O requests pending on each device");
63 TUNABLE_INT("vfs.zfs.vdev.min_pending", &zfs_vdev_min_pending);
64 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, min_pending, CTLFLAG_RW,
65     &zfs_vdev_min_pending, 0,
66     "Initial number of I/O requests pending to each device");
67 TUNABLE_INT("vfs.zfs.vdev.time_shift", &zfs_vdev_time_shift);
68 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, time_shift, CTLFLAG_RW,
69     &zfs_vdev_time_shift, 0, "Used for calculating I/O request deadline");
70 TUNABLE_INT("vfs.zfs.vdev.ramp_rate", &zfs_vdev_ramp_rate);
71 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, ramp_rate, CTLFLAG_RW,
72     &zfs_vdev_ramp_rate, 0, "Exponential I/O issue ramp-up rate");
73 TUNABLE_INT("vfs.zfs.vdev.aggregation_limit", &zfs_vdev_aggregation_limit);
74 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RW,
75     &zfs_vdev_aggregation_limit, 0,
76     "I/O requests are aggregated up to this size");
77 TUNABLE_INT("vfs.zfs.vdev.read_gap_limit", &zfs_vdev_read_gap_limit);
78 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RW,
79     &zfs_vdev_read_gap_limit, 0,
80     "Acceptable gap between two reads being aggregated");
81 TUNABLE_INT("vfs.zfs.vdev.write_gap_limit", &zfs_vdev_write_gap_limit);
82 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RW,
83     &zfs_vdev_write_gap_limit, 0,
84     "Acceptable gap between two writes being aggregated");
85
86 /*
87  * Virtual device vector for disk I/O scheduling.
88  */
89 int
90 vdev_queue_deadline_compare(const void *x1, const void *x2)
91 {
92         const zio_t *z1 = x1;
93         const zio_t *z2 = x2;
94
95         if (z1->io_deadline < z2->io_deadline)
96                 return (-1);
97         if (z1->io_deadline > z2->io_deadline)
98                 return (1);
99
100         if (z1->io_offset < z2->io_offset)
101                 return (-1);
102         if (z1->io_offset > z2->io_offset)
103                 return (1);
104
105         if (z1 < z2)
106                 return (-1);
107         if (z1 > z2)
108                 return (1);
109
110         return (0);
111 }
112
113 int
114 vdev_queue_offset_compare(const void *x1, const void *x2)
115 {
116         const zio_t *z1 = x1;
117         const zio_t *z2 = x2;
118
119         if (z1->io_offset < z2->io_offset)
120                 return (-1);
121         if (z1->io_offset > z2->io_offset)
122                 return (1);
123
124         if (z1 < z2)
125                 return (-1);
126         if (z1 > z2)
127                 return (1);
128
129         return (0);
130 }
131
132 void
133 vdev_queue_init(vdev_t *vd)
134 {
135         vdev_queue_t *vq = &vd->vdev_queue;
136
137         mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
138
139         avl_create(&vq->vq_deadline_tree, vdev_queue_deadline_compare,
140             sizeof (zio_t), offsetof(struct zio, io_deadline_node));
141
142         avl_create(&vq->vq_read_tree, vdev_queue_offset_compare,
143             sizeof (zio_t), offsetof(struct zio, io_offset_node));
144
145         avl_create(&vq->vq_write_tree, vdev_queue_offset_compare,
146             sizeof (zio_t), offsetof(struct zio, io_offset_node));
147
148         avl_create(&vq->vq_pending_tree, vdev_queue_offset_compare,
149             sizeof (zio_t), offsetof(struct zio, io_offset_node));
150 }
151
152 void
153 vdev_queue_fini(vdev_t *vd)
154 {
155         vdev_queue_t *vq = &vd->vdev_queue;
156
157         avl_destroy(&vq->vq_deadline_tree);
158         avl_destroy(&vq->vq_read_tree);
159         avl_destroy(&vq->vq_write_tree);
160         avl_destroy(&vq->vq_pending_tree);
161
162         mutex_destroy(&vq->vq_lock);
163 }
164
165 static void
166 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
167 {
168         avl_add(&vq->vq_deadline_tree, zio);
169         avl_add(zio->io_vdev_tree, zio);
170 }
171
172 static void
173 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
174 {
175         avl_remove(&vq->vq_deadline_tree, zio);
176         avl_remove(zio->io_vdev_tree, zio);
177 }
178
179 static void
180 vdev_queue_agg_io_done(zio_t *aio)
181 {
182         zio_t *pio;
183
184         while ((pio = zio_walk_parents(aio)) != NULL)
185                 if (aio->io_type == ZIO_TYPE_READ)
186                         bcopy((char *)aio->io_data + (pio->io_offset -
187                             aio->io_offset), pio->io_data, pio->io_size);
188
189         zio_buf_free(aio->io_data, aio->io_size);
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         int flags;
207         uint64_t maxspan = zfs_vdev_aggregation_limit;
208         uint64_t maxgap;
209         int stretch;
210
211 again:
212         ASSERT(MUTEX_HELD(&vq->vq_lock));
213
214         if (avl_numnodes(&vq->vq_pending_tree) >= pending_limit ||
215             avl_numnodes(&vq->vq_deadline_tree) == 0)
216                 return (NULL);
217
218         fio = lio = avl_first(&vq->vq_deadline_tree);
219
220         t = fio->io_vdev_tree;
221         flags = fio->io_flags & ZIO_FLAG_AGG_INHERIT;
222         maxgap = (t == &vq->vq_read_tree) ? zfs_vdev_read_gap_limit : 0;
223
224         if (!(flags & ZIO_FLAG_DONT_AGGREGATE)) {
225                 /*
226                  * We can aggregate I/Os that are sufficiently adjacent and of
227                  * the same flavor, as expressed by the AGG_INHERIT flags.
228                  * The latter requirement is necessary so that certain
229                  * attributes of the I/O, such as whether it's a normal I/O
230                  * or a scrub/resilver, can be preserved in the aggregate.
231                  * We can include optional I/Os, but don't allow them
232                  * to begin a range as they add no benefit in that situation.
233                  */
234
235                 /*
236                  * We keep track of the last non-optional I/O.
237                  */
238                 mio = (fio->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : fio;
239
240                 /*
241                  * Walk backwards through sufficiently contiguous I/Os
242                  * recording the last non-option I/O.
243                  */
244                 while ((dio = AVL_PREV(t, fio)) != NULL &&
245                     (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
246                     IO_SPAN(dio, lio) <= maxspan &&
247                     IO_GAP(dio, fio) <= maxgap) {
248                         fio = dio;
249                         if (mio == NULL && !(fio->io_flags & ZIO_FLAG_OPTIONAL))
250                                 mio = fio;
251                 }
252
253                 /*
254                  * Skip any initial optional I/Os.
255                  */
256                 while ((fio->io_flags & ZIO_FLAG_OPTIONAL) && fio != lio) {
257                         fio = AVL_NEXT(t, fio);
258                         ASSERT(fio != NULL);
259                 }
260
261                 /*
262                  * Walk forward through sufficiently contiguous I/Os.
263                  */
264                 while ((dio = AVL_NEXT(t, lio)) != NULL &&
265                     (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
266                     IO_SPAN(fio, dio) <= maxspan &&
267                     IO_GAP(lio, dio) <= maxgap) {
268                         lio = dio;
269                         if (!(lio->io_flags & ZIO_FLAG_OPTIONAL))
270                                 mio = lio;
271                 }
272
273                 /*
274                  * Now that we've established the range of the I/O aggregation
275                  * we must decide what to do with trailing optional I/Os.
276                  * For reads, there's nothing to do. While we are unable to
277                  * aggregate further, it's possible that a trailing optional
278                  * I/O would allow the underlying device to aggregate with
279                  * subsequent I/Os. We must therefore determine if the next
280                  * non-optional I/O is close enough to make aggregation
281                  * worthwhile.
282                  */
283                 stretch = B_FALSE;
284                 if (t != &vq->vq_read_tree && mio != NULL) {
285                         nio = lio;
286                         while ((dio = AVL_NEXT(t, nio)) != NULL &&
287                             IO_GAP(nio, dio) == 0 &&
288                             IO_GAP(mio, dio) <= zfs_vdev_write_gap_limit) {
289                                 nio = dio;
290                                 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
291                                         stretch = B_TRUE;
292                                         break;
293                                 }
294                         }
295                 }
296
297                 if (stretch) {
298                         /* This may be a no-op. */
299                         VERIFY((dio = AVL_NEXT(t, lio)) != NULL);
300                         dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
301                 } else {
302                         while (lio != mio && lio != fio) {
303                                 ASSERT(lio->io_flags & ZIO_FLAG_OPTIONAL);
304                                 lio = AVL_PREV(t, lio);
305                                 ASSERT(lio != NULL);
306                         }
307                 }
308         }
309
310         if (fio != lio) {
311                 uint64_t size = IO_SPAN(fio, lio);
312                 ASSERT(size <= zfs_vdev_aggregation_limit);
313
314                 aio = zio_vdev_delegated_io(fio->io_vd, fio->io_offset,
315                     zio_buf_alloc(size), size, fio->io_type, ZIO_PRIORITY_AGG,
316                     flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
317                     vdev_queue_agg_io_done, NULL);
318
319                 nio = fio;
320                 do {
321                         dio = nio;
322                         nio = AVL_NEXT(t, dio);
323                         ASSERT(dio->io_type == aio->io_type);
324                         ASSERT(dio->io_vdev_tree == t);
325
326                         if (dio->io_flags & ZIO_FLAG_NODATA) {
327                                 ASSERT(dio->io_type == ZIO_TYPE_WRITE);
328                                 bzero((char *)aio->io_data + (dio->io_offset -
329                                     aio->io_offset), dio->io_size);
330                         } else if (dio->io_type == ZIO_TYPE_WRITE) {
331                                 bcopy(dio->io_data, (char *)aio->io_data +
332                                     (dio->io_offset - aio->io_offset),
333                                     dio->io_size);
334                         }
335
336                         zio_add_child(dio, aio);
337                         vdev_queue_io_remove(vq, dio);
338                         zio_vdev_io_bypass(dio);
339                         zio_execute(dio);
340                 } while (dio != lio);
341
342                 avl_add(&vq->vq_pending_tree, aio);
343
344                 return (aio);
345         }
346
347         ASSERT(fio->io_vdev_tree == t);
348         vdev_queue_io_remove(vq, fio);
349
350         /*
351          * If the I/O is or was optional and therefore has no data, we need to
352          * simply discard it. We need to drop the vdev queue's lock to avoid a
353          * deadlock that we could encounter since this I/O will complete
354          * immediately.
355          */
356         if (fio->io_flags & ZIO_FLAG_NODATA) {
357                 mutex_exit(&vq->vq_lock);
358                 zio_vdev_io_bypass(fio);
359                 zio_execute(fio);
360                 mutex_enter(&vq->vq_lock);
361                 goto again;
362         }
363
364         avl_add(&vq->vq_pending_tree, fio);
365
366         return (fio);
367 }
368
369 zio_t *
370 vdev_queue_io(zio_t *zio)
371 {
372         vdev_queue_t *vq = &zio->io_vd->vdev_queue;
373         zio_t *nio;
374
375         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
376
377         if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
378                 return (zio);
379
380         zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
381
382         if (zio->io_type == ZIO_TYPE_READ)
383                 zio->io_vdev_tree = &vq->vq_read_tree;
384         else
385                 zio->io_vdev_tree = &vq->vq_write_tree;
386
387         mutex_enter(&vq->vq_lock);
388
389         zio->io_deadline = (ddi_get_lbolt64() >> zfs_vdev_time_shift) +
390             zio->io_priority;
391
392         vdev_queue_io_add(vq, zio);
393
394         nio = vdev_queue_io_to_issue(vq, zfs_vdev_min_pending);
395
396         mutex_exit(&vq->vq_lock);
397
398         if (nio == NULL)
399                 return (NULL);
400
401         if (nio->io_done == vdev_queue_agg_io_done) {
402                 zio_nowait(nio);
403                 return (NULL);
404         }
405
406         return (nio);
407 }
408
409 void
410 vdev_queue_io_done(zio_t *zio)
411 {
412         vdev_queue_t *vq = &zio->io_vd->vdev_queue;
413
414         mutex_enter(&vq->vq_lock);
415
416         avl_remove(&vq->vq_pending_tree, zio);
417
418         for (int i = 0; i < zfs_vdev_ramp_rate; i++) {
419                 zio_t *nio = vdev_queue_io_to_issue(vq, zfs_vdev_max_pending);
420                 if (nio == NULL)
421                         break;
422                 mutex_exit(&vq->vq_lock);
423                 if (nio->io_done == vdev_queue_agg_io_done) {
424                         zio_nowait(nio);
425                 } else {
426                         zio_vdev_io_reissue(nio);
427                         zio_execute(nio);
428                 }
429                 mutex_enter(&vq->vq_lock);
430         }
431
432         mutex_exit(&vq->vq_lock);
433 }