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