]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/vdev_cache.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_cache.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/spa.h>
28 #include <sys/vdev_impl.h>
29 #include <sys/zio.h>
30 #include <sys/kstat.h>
31
32 /*
33  * Virtual device read-ahead caching.
34  *
35  * This file implements a simple LRU read-ahead cache.  When the DMU reads
36  * a given block, it will often want other, nearby blocks soon thereafter.
37  * We take advantage of this by reading a larger disk region and caching
38  * the result.  In the best case, this can turn 128 back-to-back 512-byte
39  * reads into a single 64k read followed by 127 cache hits; this reduces
40  * latency dramatically.  In the worst case, it can turn an isolated 512-byte
41  * read into a 64k read, which doesn't affect latency all that much but is
42  * terribly wasteful of bandwidth.  A more intelligent version of the cache
43  * could keep track of access patterns and not do read-ahead unless it sees
44  * at least two temporally close I/Os to the same region.  Currently, only
45  * metadata I/O is inflated.  A futher enhancement could take advantage of
46  * more semantic information about the I/O.  And it could use something
47  * faster than an AVL tree; that was chosen solely for convenience.
48  *
49  * There are five cache operations: allocate, fill, read, write, evict.
50  *
51  * (1) Allocate.  This reserves a cache entry for the specified region.
52  *     We separate the allocate and fill operations so that multiple threads
53  *     don't generate I/O for the same cache miss.
54  *
55  * (2) Fill.  When the I/O for a cache miss completes, the fill routine
56  *     places the data in the previously allocated cache entry.
57  *
58  * (3) Read.  Read data from the cache.
59  *
60  * (4) Write.  Update cache contents after write completion.
61  *
62  * (5) Evict.  When allocating a new entry, we evict the oldest (LRU) entry
63  *     if the total cache size exceeds zfs_vdev_cache_size.
64  */
65
66 /*
67  * These tunables are for performance analysis.
68  */
69 /*
70  * All i/os smaller than zfs_vdev_cache_max will be turned into
71  * 1<<zfs_vdev_cache_bshift byte reads by the vdev_cache (aka software
72  * track buffer).  At most zfs_vdev_cache_size bytes will be kept in each
73  * vdev's vdev_cache.
74  *
75  * TODO: Note that with the current ZFS code, it turns out that the
76  * vdev cache is not helpful, and in some cases actually harmful.  It
77  * is better if we disable this.  Once some time has passed, we should
78  * actually remove this to simplify the code.  For now we just disable
79  * it by setting the zfs_vdev_cache_size to zero.  Note that Solaris 11
80  * has made these same changes.
81  */
82 int zfs_vdev_cache_max = 1<<14;                 /* 16KB */
83 int zfs_vdev_cache_size = 0;
84 int zfs_vdev_cache_bshift = 16;
85
86 #define VCBS (1 << zfs_vdev_cache_bshift)       /* 64KB */
87
88 SYSCTL_DECL(_vfs_zfs_vdev);
89 SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, cache, CTLFLAG_RW, 0, "ZFS VDEV Cache");
90 TUNABLE_INT("vfs.zfs.vdev.cache.max", &zfs_vdev_cache_max);
91 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, max, CTLFLAG_RDTUN,
92     &zfs_vdev_cache_max, 0, "Maximum I/O request size that increase read size");
93 TUNABLE_INT("vfs.zfs.vdev.cache.size", &zfs_vdev_cache_size);
94 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, size, CTLFLAG_RDTUN,
95     &zfs_vdev_cache_size, 0, "Size of VDEV cache");
96 TUNABLE_INT("vfs.zfs.vdev.cache.bshift", &zfs_vdev_cache_bshift);
97 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, bshift, CTLFLAG_RDTUN,
98     &zfs_vdev_cache_bshift, 0, "Turn too small requests into 1 << this value");
99
100 kstat_t *vdc_ksp = NULL;
101
102 typedef struct vdc_stats {
103         kstat_named_t vdc_stat_delegations;
104         kstat_named_t vdc_stat_hits;
105         kstat_named_t vdc_stat_misses;
106 } vdc_stats_t;
107
108 static vdc_stats_t vdc_stats = {
109         { "delegations",        KSTAT_DATA_UINT64 },
110         { "hits",               KSTAT_DATA_UINT64 },
111         { "misses",             KSTAT_DATA_UINT64 }
112 };
113
114 #define VDCSTAT_BUMP(stat)      atomic_add_64(&vdc_stats.stat.value.ui64, 1);
115
116 static int
117 vdev_cache_offset_compare(const void *a1, const void *a2)
118 {
119         const vdev_cache_entry_t *ve1 = a1;
120         const vdev_cache_entry_t *ve2 = a2;
121
122         if (ve1->ve_offset < ve2->ve_offset)
123                 return (-1);
124         if (ve1->ve_offset > ve2->ve_offset)
125                 return (1);
126         return (0);
127 }
128
129 static int
130 vdev_cache_lastused_compare(const void *a1, const void *a2)
131 {
132         const vdev_cache_entry_t *ve1 = a1;
133         const vdev_cache_entry_t *ve2 = a2;
134
135         if (ve1->ve_lastused < ve2->ve_lastused)
136                 return (-1);
137         if (ve1->ve_lastused > ve2->ve_lastused)
138                 return (1);
139
140         /*
141          * Among equally old entries, sort by offset to ensure uniqueness.
142          */
143         return (vdev_cache_offset_compare(a1, a2));
144 }
145
146 /*
147  * Evict the specified entry from the cache.
148  */
149 static void
150 vdev_cache_evict(vdev_cache_t *vc, vdev_cache_entry_t *ve)
151 {
152         ASSERT(MUTEX_HELD(&vc->vc_lock));
153         ASSERT(ve->ve_fill_io == NULL);
154         ASSERT(ve->ve_data != NULL);
155
156         avl_remove(&vc->vc_lastused_tree, ve);
157         avl_remove(&vc->vc_offset_tree, ve);
158         zio_buf_free(ve->ve_data, VCBS);
159         kmem_free(ve, sizeof (vdev_cache_entry_t));
160 }
161
162 /*
163  * Allocate an entry in the cache.  At the point we don't have the data,
164  * we're just creating a placeholder so that multiple threads don't all
165  * go off and read the same blocks.
166  */
167 static vdev_cache_entry_t *
168 vdev_cache_allocate(zio_t *zio)
169 {
170         vdev_cache_t *vc = &zio->io_vd->vdev_cache;
171         uint64_t offset = P2ALIGN(zio->io_offset, VCBS);
172         vdev_cache_entry_t *ve;
173
174         ASSERT(MUTEX_HELD(&vc->vc_lock));
175
176         if (zfs_vdev_cache_size == 0)
177                 return (NULL);
178
179         /*
180          * If adding a new entry would exceed the cache size,
181          * evict the oldest entry (LRU).
182          */
183         if ((avl_numnodes(&vc->vc_lastused_tree) << zfs_vdev_cache_bshift) >
184             zfs_vdev_cache_size) {
185                 ve = avl_first(&vc->vc_lastused_tree);
186                 if (ve->ve_fill_io != NULL)
187                         return (NULL);
188                 ASSERT(ve->ve_hits != 0);
189                 vdev_cache_evict(vc, ve);
190         }
191
192         ve = kmem_zalloc(sizeof (vdev_cache_entry_t), KM_SLEEP);
193         ve->ve_offset = offset;
194         ve->ve_lastused = ddi_get_lbolt();
195         ve->ve_data = zio_buf_alloc(VCBS);
196
197         avl_add(&vc->vc_offset_tree, ve);
198         avl_add(&vc->vc_lastused_tree, ve);
199
200         return (ve);
201 }
202
203 static void
204 vdev_cache_hit(vdev_cache_t *vc, vdev_cache_entry_t *ve, zio_t *zio)
205 {
206         uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
207
208         ASSERT(MUTEX_HELD(&vc->vc_lock));
209         ASSERT(ve->ve_fill_io == NULL);
210
211         if (ve->ve_lastused != ddi_get_lbolt()) {
212                 avl_remove(&vc->vc_lastused_tree, ve);
213                 ve->ve_lastused = ddi_get_lbolt();
214                 avl_add(&vc->vc_lastused_tree, ve);
215         }
216
217         ve->ve_hits++;
218         bcopy(ve->ve_data + cache_phase, zio->io_data, zio->io_size);
219 }
220
221 /*
222  * Fill a previously allocated cache entry with data.
223  */
224 static void
225 vdev_cache_fill(zio_t *fio)
226 {
227         vdev_t *vd = fio->io_vd;
228         vdev_cache_t *vc = &vd->vdev_cache;
229         vdev_cache_entry_t *ve = fio->io_private;
230         zio_t *pio;
231
232         ASSERT(fio->io_size == VCBS);
233
234         /*
235          * Add data to the cache.
236          */
237         mutex_enter(&vc->vc_lock);
238
239         ASSERT(ve->ve_fill_io == fio);
240         ASSERT(ve->ve_offset == fio->io_offset);
241         ASSERT(ve->ve_data == fio->io_data);
242
243         ve->ve_fill_io = NULL;
244
245         /*
246          * Even if this cache line was invalidated by a missed write update,
247          * any reads that were queued up before the missed update are still
248          * valid, so we can satisfy them from this line before we evict it.
249          */
250         while ((pio = zio_walk_parents(fio)) != NULL)
251                 vdev_cache_hit(vc, ve, pio);
252
253         if (fio->io_error || ve->ve_missed_update)
254                 vdev_cache_evict(vc, ve);
255
256         mutex_exit(&vc->vc_lock);
257 }
258
259 /*
260  * Read data from the cache.  Returns 0 on cache hit, errno on a miss.
261  */
262 int
263 vdev_cache_read(zio_t *zio)
264 {
265         vdev_cache_t *vc = &zio->io_vd->vdev_cache;
266         vdev_cache_entry_t *ve, ve_search;
267         uint64_t cache_offset = P2ALIGN(zio->io_offset, VCBS);
268         uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
269         zio_t *fio;
270
271         ASSERT(zio->io_type == ZIO_TYPE_READ);
272
273         if (zio->io_flags & ZIO_FLAG_DONT_CACHE)
274                 return (EINVAL);
275
276         if (zio->io_size > zfs_vdev_cache_max)
277                 return (EOVERFLOW);
278
279         /*
280          * If the I/O straddles two or more cache blocks, don't cache it.
281          */
282         if (P2BOUNDARY(zio->io_offset, zio->io_size, VCBS))
283                 return (EXDEV);
284
285         ASSERT(cache_phase + zio->io_size <= VCBS);
286
287         mutex_enter(&vc->vc_lock);
288
289         ve_search.ve_offset = cache_offset;
290         ve = avl_find(&vc->vc_offset_tree, &ve_search, NULL);
291
292         if (ve != NULL) {
293                 if (ve->ve_missed_update) {
294                         mutex_exit(&vc->vc_lock);
295                         return (ESTALE);
296                 }
297
298                 if ((fio = ve->ve_fill_io) != NULL) {
299                         zio_vdev_io_bypass(zio);
300                         zio_add_child(zio, fio);
301                         mutex_exit(&vc->vc_lock);
302                         VDCSTAT_BUMP(vdc_stat_delegations);
303                         return (0);
304                 }
305
306                 vdev_cache_hit(vc, ve, zio);
307                 zio_vdev_io_bypass(zio);
308
309                 mutex_exit(&vc->vc_lock);
310                 VDCSTAT_BUMP(vdc_stat_hits);
311                 return (0);
312         }
313
314         ve = vdev_cache_allocate(zio);
315
316         if (ve == NULL) {
317                 mutex_exit(&vc->vc_lock);
318                 return (ENOMEM);
319         }
320
321         fio = zio_vdev_delegated_io(zio->io_vd, cache_offset,
322             ve->ve_data, VCBS, ZIO_TYPE_READ, ZIO_PRIORITY_CACHE_FILL,
323             ZIO_FLAG_DONT_CACHE, vdev_cache_fill, ve);
324
325         ve->ve_fill_io = fio;
326         zio_vdev_io_bypass(zio);
327         zio_add_child(zio, fio);
328
329         mutex_exit(&vc->vc_lock);
330         zio_nowait(fio);
331         VDCSTAT_BUMP(vdc_stat_misses);
332
333         return (0);
334 }
335
336 /*
337  * Update cache contents upon write completion.
338  */
339 void
340 vdev_cache_write(zio_t *zio)
341 {
342         vdev_cache_t *vc = &zio->io_vd->vdev_cache;
343         vdev_cache_entry_t *ve, ve_search;
344         uint64_t io_start = zio->io_offset;
345         uint64_t io_end = io_start + zio->io_size;
346         uint64_t min_offset = P2ALIGN(io_start, VCBS);
347         uint64_t max_offset = P2ROUNDUP(io_end, VCBS);
348         avl_index_t where;
349
350         ASSERT(zio->io_type == ZIO_TYPE_WRITE);
351
352         mutex_enter(&vc->vc_lock);
353
354         ve_search.ve_offset = min_offset;
355         ve = avl_find(&vc->vc_offset_tree, &ve_search, &where);
356
357         if (ve == NULL)
358                 ve = avl_nearest(&vc->vc_offset_tree, where, AVL_AFTER);
359
360         while (ve != NULL && ve->ve_offset < max_offset) {
361                 uint64_t start = MAX(ve->ve_offset, io_start);
362                 uint64_t end = MIN(ve->ve_offset + VCBS, io_end);
363
364                 if (ve->ve_fill_io != NULL) {
365                         ve->ve_missed_update = 1;
366                 } else {
367                         bcopy((char *)zio->io_data + start - io_start,
368                             ve->ve_data + start - ve->ve_offset, end - start);
369                 }
370                 ve = AVL_NEXT(&vc->vc_offset_tree, ve);
371         }
372         mutex_exit(&vc->vc_lock);
373 }
374
375 void
376 vdev_cache_purge(vdev_t *vd)
377 {
378         vdev_cache_t *vc = &vd->vdev_cache;
379         vdev_cache_entry_t *ve;
380
381         mutex_enter(&vc->vc_lock);
382         while ((ve = avl_first(&vc->vc_offset_tree)) != NULL)
383                 vdev_cache_evict(vc, ve);
384         mutex_exit(&vc->vc_lock);
385 }
386
387 void
388 vdev_cache_init(vdev_t *vd)
389 {
390         vdev_cache_t *vc = &vd->vdev_cache;
391
392         mutex_init(&vc->vc_lock, NULL, MUTEX_DEFAULT, NULL);
393
394         avl_create(&vc->vc_offset_tree, vdev_cache_offset_compare,
395             sizeof (vdev_cache_entry_t),
396             offsetof(struct vdev_cache_entry, ve_offset_node));
397
398         avl_create(&vc->vc_lastused_tree, vdev_cache_lastused_compare,
399             sizeof (vdev_cache_entry_t),
400             offsetof(struct vdev_cache_entry, ve_lastused_node));
401 }
402
403 void
404 vdev_cache_fini(vdev_t *vd)
405 {
406         vdev_cache_t *vc = &vd->vdev_cache;
407
408         vdev_cache_purge(vd);
409
410         avl_destroy(&vc->vc_offset_tree);
411         avl_destroy(&vc->vc_lastused_tree);
412
413         mutex_destroy(&vc->vc_lock);
414 }
415
416 void
417 vdev_cache_stat_init(void)
418 {
419         vdc_ksp = kstat_create("zfs", 0, "vdev_cache_stats", "misc",
420             KSTAT_TYPE_NAMED, sizeof (vdc_stats) / sizeof (kstat_named_t),
421             KSTAT_FLAG_VIRTUAL);
422         if (vdc_ksp != NULL) {
423                 vdc_ksp->ks_data = &vdc_stats;
424                 kstat_install(vdc_ksp);
425         }
426 }
427
428 void
429 vdev_cache_stat_fini(void)
430 {
431         if (vdc_ksp != NULL) {
432                 kstat_delete(vdc_ksp);
433                 vdc_ksp = NULL;
434         }
435 }