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