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 https://opensource.org/licenses/CDDL-1.0. 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) 2013, 2017 by Delphix. All rights reserved. 28 */ 29 30 #include <sys/zfs_context.h> 31 #include <sys/arc_impl.h> 32 #include <sys/dnode.h> 33 #include <sys/dmu_objset.h> 34 #include <sys/dmu_zfetch.h> 35 #include <sys/dmu.h> 36 #include <sys/dbuf.h> 37 #include <sys/kstat.h> 38 #include <sys/wmsum.h> 39 40 /* 41 * This tunable disables predictive prefetch. Note that it leaves "prescient" 42 * prefetch (e.g. prefetch for zfs send) intact. Unlike predictive prefetch, 43 * prescient prefetch never issues i/os that end up not being needed, 44 * so it can't hurt performance. 45 */ 46 47 static int zfs_prefetch_disable = B_FALSE; 48 49 /* max # of streams per zfetch */ 50 static unsigned int zfetch_max_streams = 8; 51 /* min time before stream reclaim */ 52 static unsigned int zfetch_min_sec_reap = 1; 53 /* max time before stream delete */ 54 static unsigned int zfetch_max_sec_reap = 2; 55 /* min bytes to prefetch per stream (default 4MB) */ 56 static unsigned int zfetch_min_distance = 4 * 1024 * 1024; 57 /* max bytes to prefetch per stream (default 64MB) */ 58 unsigned int zfetch_max_distance = 64 * 1024 * 1024; 59 /* max bytes to prefetch indirects for per stream (default 64MB) */ 60 unsigned int zfetch_max_idistance = 64 * 1024 * 1024; 61 /* max number of bytes in an array_read in which we allow prefetching (1MB) */ 62 uint64_t zfetch_array_rd_sz = 1024 * 1024; 63 64 typedef struct zfetch_stats { 65 kstat_named_t zfetchstat_hits; 66 kstat_named_t zfetchstat_misses; 67 kstat_named_t zfetchstat_max_streams; 68 kstat_named_t zfetchstat_io_issued; 69 kstat_named_t zfetchstat_io_active; 70 } zfetch_stats_t; 71 72 static zfetch_stats_t zfetch_stats = { 73 { "hits", KSTAT_DATA_UINT64 }, 74 { "misses", KSTAT_DATA_UINT64 }, 75 { "max_streams", KSTAT_DATA_UINT64 }, 76 { "io_issued", KSTAT_DATA_UINT64 }, 77 { "io_active", KSTAT_DATA_UINT64 }, 78 }; 79 80 struct { 81 wmsum_t zfetchstat_hits; 82 wmsum_t zfetchstat_misses; 83 wmsum_t zfetchstat_max_streams; 84 wmsum_t zfetchstat_io_issued; 85 aggsum_t zfetchstat_io_active; 86 } zfetch_sums; 87 88 #define ZFETCHSTAT_BUMP(stat) \ 89 wmsum_add(&zfetch_sums.stat, 1) 90 #define ZFETCHSTAT_ADD(stat, val) \ 91 wmsum_add(&zfetch_sums.stat, val) 92 93 94 static kstat_t *zfetch_ksp; 95 96 static int 97 zfetch_kstats_update(kstat_t *ksp, int rw) 98 { 99 zfetch_stats_t *zs = ksp->ks_data; 100 101 if (rw == KSTAT_WRITE) 102 return (EACCES); 103 zs->zfetchstat_hits.value.ui64 = 104 wmsum_value(&zfetch_sums.zfetchstat_hits); 105 zs->zfetchstat_misses.value.ui64 = 106 wmsum_value(&zfetch_sums.zfetchstat_misses); 107 zs->zfetchstat_max_streams.value.ui64 = 108 wmsum_value(&zfetch_sums.zfetchstat_max_streams); 109 zs->zfetchstat_io_issued.value.ui64 = 110 wmsum_value(&zfetch_sums.zfetchstat_io_issued); 111 zs->zfetchstat_io_active.value.ui64 = 112 aggsum_value(&zfetch_sums.zfetchstat_io_active); 113 return (0); 114 } 115 116 void 117 zfetch_init(void) 118 { 119 wmsum_init(&zfetch_sums.zfetchstat_hits, 0); 120 wmsum_init(&zfetch_sums.zfetchstat_misses, 0); 121 wmsum_init(&zfetch_sums.zfetchstat_max_streams, 0); 122 wmsum_init(&zfetch_sums.zfetchstat_io_issued, 0); 123 aggsum_init(&zfetch_sums.zfetchstat_io_active, 0); 124 125 zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc", 126 KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t), 127 KSTAT_FLAG_VIRTUAL); 128 129 if (zfetch_ksp != NULL) { 130 zfetch_ksp->ks_data = &zfetch_stats; 131 zfetch_ksp->ks_update = zfetch_kstats_update; 132 kstat_install(zfetch_ksp); 133 } 134 } 135 136 void 137 zfetch_fini(void) 138 { 139 if (zfetch_ksp != NULL) { 140 kstat_delete(zfetch_ksp); 141 zfetch_ksp = NULL; 142 } 143 144 wmsum_fini(&zfetch_sums.zfetchstat_hits); 145 wmsum_fini(&zfetch_sums.zfetchstat_misses); 146 wmsum_fini(&zfetch_sums.zfetchstat_max_streams); 147 wmsum_fini(&zfetch_sums.zfetchstat_io_issued); 148 ASSERT0(aggsum_value(&zfetch_sums.zfetchstat_io_active)); 149 aggsum_fini(&zfetch_sums.zfetchstat_io_active); 150 } 151 152 /* 153 * This takes a pointer to a zfetch structure and a dnode. It performs the 154 * necessary setup for the zfetch structure, grokking data from the 155 * associated dnode. 156 */ 157 void 158 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno) 159 { 160 if (zf == NULL) 161 return; 162 zf->zf_dnode = dno; 163 zf->zf_numstreams = 0; 164 165 list_create(&zf->zf_stream, sizeof (zstream_t), 166 offsetof(zstream_t, zs_node)); 167 168 mutex_init(&zf->zf_lock, NULL, MUTEX_DEFAULT, NULL); 169 } 170 171 static void 172 dmu_zfetch_stream_fini(zstream_t *zs) 173 { 174 ASSERT(!list_link_active(&zs->zs_node)); 175 zfs_refcount_destroy(&zs->zs_callers); 176 zfs_refcount_destroy(&zs->zs_refs); 177 kmem_free(zs, sizeof (*zs)); 178 } 179 180 static void 181 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs) 182 { 183 ASSERT(MUTEX_HELD(&zf->zf_lock)); 184 list_remove(&zf->zf_stream, zs); 185 zf->zf_numstreams--; 186 membar_producer(); 187 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0) 188 dmu_zfetch_stream_fini(zs); 189 } 190 191 /* 192 * Clean-up state associated with a zfetch structure (e.g. destroy the 193 * streams). This doesn't free the zfetch_t itself, that's left to the caller. 194 */ 195 void 196 dmu_zfetch_fini(zfetch_t *zf) 197 { 198 zstream_t *zs; 199 200 mutex_enter(&zf->zf_lock); 201 while ((zs = list_head(&zf->zf_stream)) != NULL) 202 dmu_zfetch_stream_remove(zf, zs); 203 mutex_exit(&zf->zf_lock); 204 list_destroy(&zf->zf_stream); 205 mutex_destroy(&zf->zf_lock); 206 207 zf->zf_dnode = NULL; 208 } 209 210 /* 211 * If there aren't too many active streams already, create one more. 212 * In process delete/reuse all streams without hits for zfetch_max_sec_reap. 213 * If needed, reuse oldest stream without hits for zfetch_min_sec_reap or ever. 214 * The "blkid" argument is the next block that we expect this stream to access. 215 */ 216 static void 217 dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid) 218 { 219 zstream_t *zs, *zs_next, *zs_old = NULL; 220 hrtime_t now = gethrtime(), t; 221 222 ASSERT(MUTEX_HELD(&zf->zf_lock)); 223 224 /* 225 * Delete too old streams, reusing the first found one. 226 */ 227 t = now - SEC2NSEC(zfetch_max_sec_reap); 228 for (zs = list_head(&zf->zf_stream); zs != NULL; zs = zs_next) { 229 zs_next = list_next(&zf->zf_stream, zs); 230 /* 231 * Skip if still active. 1 -- zf_stream reference. 232 */ 233 if (zfs_refcount_count(&zs->zs_refs) != 1) 234 continue; 235 if (zs->zs_atime > t) 236 continue; 237 if (zs_old) 238 dmu_zfetch_stream_remove(zf, zs); 239 else 240 zs_old = zs; 241 } 242 if (zs_old) { 243 zs = zs_old; 244 goto reuse; 245 } 246 247 /* 248 * The maximum number of streams is normally zfetch_max_streams, 249 * but for small files we lower it such that it's at least possible 250 * for all the streams to be non-overlapping. 251 */ 252 uint32_t max_streams = MAX(1, MIN(zfetch_max_streams, 253 zf->zf_dnode->dn_maxblkid * zf->zf_dnode->dn_datablksz / 254 zfetch_max_distance)); 255 if (zf->zf_numstreams >= max_streams) { 256 t = now - SEC2NSEC(zfetch_min_sec_reap); 257 for (zs = list_head(&zf->zf_stream); zs != NULL; 258 zs = list_next(&zf->zf_stream, zs)) { 259 if (zfs_refcount_count(&zs->zs_refs) != 1) 260 continue; 261 if (zs->zs_atime > t) 262 continue; 263 if (zs_old == NULL || zs->zs_atime < zs_old->zs_atime) 264 zs_old = zs; 265 } 266 if (zs_old) { 267 zs = zs_old; 268 goto reuse; 269 } 270 ZFETCHSTAT_BUMP(zfetchstat_max_streams); 271 return; 272 } 273 274 zs = kmem_zalloc(sizeof (*zs), KM_SLEEP); 275 zs->zs_fetch = zf; 276 zfs_refcount_create(&zs->zs_callers); 277 zfs_refcount_create(&zs->zs_refs); 278 /* One reference for zf_stream. */ 279 zfs_refcount_add(&zs->zs_refs, NULL); 280 zf->zf_numstreams++; 281 list_insert_head(&zf->zf_stream, zs); 282 283 reuse: 284 zs->zs_blkid = blkid; 285 zs->zs_pf_dist = 0; 286 zs->zs_pf_start = blkid; 287 zs->zs_pf_end = blkid; 288 zs->zs_ipf_dist = 0; 289 zs->zs_ipf_start = blkid; 290 zs->zs_ipf_end = blkid; 291 /* Allow immediate stream reuse until first hit. */ 292 zs->zs_atime = now - SEC2NSEC(zfetch_min_sec_reap); 293 zs->zs_missed = B_FALSE; 294 zs->zs_more = B_FALSE; 295 } 296 297 static void 298 dmu_zfetch_done(void *arg, uint64_t level, uint64_t blkid, boolean_t io_issued) 299 { 300 zstream_t *zs = arg; 301 302 if (io_issued && level == 0 && blkid < zs->zs_blkid) 303 zs->zs_more = B_TRUE; 304 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0) 305 dmu_zfetch_stream_fini(zs); 306 aggsum_add(&zfetch_sums.zfetchstat_io_active, -1); 307 } 308 309 /* 310 * This is the predictive prefetch entry point. dmu_zfetch_prepare() 311 * associates dnode access specified with blkid and nblks arguments with 312 * prefetch stream, predicts further accesses based on that stats and returns 313 * the stream pointer on success. That pointer must later be passed to 314 * dmu_zfetch_run() to initiate the speculative prefetch for the stream and 315 * release it. dmu_zfetch() is a wrapper for simple cases when window between 316 * prediction and prefetch initiation is not needed. 317 * fetch_data argument specifies whether actual data blocks should be fetched: 318 * FALSE -- prefetch only indirect blocks for predicted data blocks; 319 * TRUE -- prefetch predicted data blocks plus following indirect blocks. 320 */ 321 zstream_t * 322 dmu_zfetch_prepare(zfetch_t *zf, uint64_t blkid, uint64_t nblks, 323 boolean_t fetch_data, boolean_t have_lock) 324 { 325 zstream_t *zs; 326 spa_t *spa = zf->zf_dnode->dn_objset->os_spa; 327 328 if (zfs_prefetch_disable) 329 return (NULL); 330 /* 331 * If we haven't yet loaded the indirect vdevs' mappings, we 332 * can only read from blocks that we carefully ensure are on 333 * concrete vdevs (or previously-loaded indirect vdevs). So we 334 * can't allow the predictive prefetcher to attempt reads of other 335 * blocks (e.g. of the MOS's dnode object). 336 */ 337 if (!spa_indirect_vdevs_loaded(spa)) 338 return (NULL); 339 340 /* 341 * As a fast path for small (single-block) files, ignore access 342 * to the first block. 343 */ 344 if (!have_lock && blkid == 0) 345 return (NULL); 346 347 if (!have_lock) 348 rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER); 349 350 /* 351 * A fast path for small files for which no prefetch will 352 * happen. 353 */ 354 uint64_t maxblkid = zf->zf_dnode->dn_maxblkid; 355 if (maxblkid < 2) { 356 if (!have_lock) 357 rw_exit(&zf->zf_dnode->dn_struct_rwlock); 358 return (NULL); 359 } 360 mutex_enter(&zf->zf_lock); 361 362 /* 363 * Find matching prefetch stream. Depending on whether the accesses 364 * are block-aligned, first block of the new access may either follow 365 * the last block of the previous access, or be equal to it. 366 */ 367 for (zs = list_head(&zf->zf_stream); zs != NULL; 368 zs = list_next(&zf->zf_stream, zs)) { 369 if (blkid == zs->zs_blkid) { 370 break; 371 } else if (blkid + 1 == zs->zs_blkid) { 372 blkid++; 373 nblks--; 374 break; 375 } 376 } 377 378 /* 379 * If the file is ending, remove the matching stream if found. 380 * If not found then it is too late to create a new one now. 381 */ 382 uint64_t end_of_access_blkid = blkid + nblks; 383 if (end_of_access_blkid >= maxblkid) { 384 if (zs != NULL) 385 dmu_zfetch_stream_remove(zf, zs); 386 mutex_exit(&zf->zf_lock); 387 if (!have_lock) 388 rw_exit(&zf->zf_dnode->dn_struct_rwlock); 389 return (NULL); 390 } 391 392 /* Exit if we already prefetched this block before. */ 393 if (nblks == 0) { 394 mutex_exit(&zf->zf_lock); 395 if (!have_lock) 396 rw_exit(&zf->zf_dnode->dn_struct_rwlock); 397 return (NULL); 398 } 399 400 if (zs == NULL) { 401 /* 402 * This access is not part of any existing stream. Create 403 * a new stream for it. 404 */ 405 dmu_zfetch_stream_create(zf, end_of_access_blkid); 406 mutex_exit(&zf->zf_lock); 407 if (!have_lock) 408 rw_exit(&zf->zf_dnode->dn_struct_rwlock); 409 ZFETCHSTAT_BUMP(zfetchstat_misses); 410 return (NULL); 411 } 412 413 /* 414 * This access was to a block that we issued a prefetch for on 415 * behalf of this stream. Calculate further prefetch distances. 416 * 417 * Start prefetch from the demand access size (nblks). Double the 418 * distance every access up to zfetch_min_distance. After that only 419 * if needed increase the distance by 1/8 up to zfetch_max_distance. 420 * 421 * Don't double the distance beyond single block if we have more 422 * than ~6% of ARC held by active prefetches. It should help with 423 * getting out of RAM on some badly mispredicted read patterns. 424 */ 425 unsigned int dbs = zf->zf_dnode->dn_datablkshift; 426 unsigned int nbytes = nblks << dbs; 427 unsigned int pf_nblks; 428 if (fetch_data) { 429 if (unlikely(zs->zs_pf_dist < nbytes)) 430 zs->zs_pf_dist = nbytes; 431 else if (zs->zs_pf_dist < zfetch_min_distance && 432 (zs->zs_pf_dist < (1 << dbs) || 433 aggsum_compare(&zfetch_sums.zfetchstat_io_active, 434 arc_c_max >> (4 + dbs)) < 0)) 435 zs->zs_pf_dist *= 2; 436 else if (zs->zs_more) 437 zs->zs_pf_dist += zs->zs_pf_dist / 8; 438 zs->zs_more = B_FALSE; 439 if (zs->zs_pf_dist > zfetch_max_distance) 440 zs->zs_pf_dist = zfetch_max_distance; 441 pf_nblks = zs->zs_pf_dist >> dbs; 442 } else { 443 pf_nblks = 0; 444 } 445 if (zs->zs_pf_start < end_of_access_blkid) 446 zs->zs_pf_start = end_of_access_blkid; 447 if (zs->zs_pf_end < end_of_access_blkid + pf_nblks) 448 zs->zs_pf_end = end_of_access_blkid + pf_nblks; 449 450 /* 451 * Do the same for indirects, starting where we will stop reading 452 * data blocks (and the indirects that point to them). 453 */ 454 if (unlikely(zs->zs_ipf_dist < nbytes)) 455 zs->zs_ipf_dist = nbytes; 456 else 457 zs->zs_ipf_dist *= 2; 458 if (zs->zs_ipf_dist > zfetch_max_idistance) 459 zs->zs_ipf_dist = zfetch_max_idistance; 460 pf_nblks = zs->zs_ipf_dist >> dbs; 461 if (zs->zs_ipf_start < zs->zs_pf_end) 462 zs->zs_ipf_start = zs->zs_pf_end; 463 if (zs->zs_ipf_end < zs->zs_pf_end + pf_nblks) 464 zs->zs_ipf_end = zs->zs_pf_end + pf_nblks; 465 466 zs->zs_blkid = end_of_access_blkid; 467 /* Protect the stream from reclamation. */ 468 zs->zs_atime = gethrtime(); 469 zfs_refcount_add(&zs->zs_refs, NULL); 470 /* Count concurrent callers. */ 471 zfs_refcount_add(&zs->zs_callers, NULL); 472 mutex_exit(&zf->zf_lock); 473 474 if (!have_lock) 475 rw_exit(&zf->zf_dnode->dn_struct_rwlock); 476 477 ZFETCHSTAT_BUMP(zfetchstat_hits); 478 return (zs); 479 } 480 481 void 482 dmu_zfetch_run(zstream_t *zs, boolean_t missed, boolean_t have_lock) 483 { 484 zfetch_t *zf = zs->zs_fetch; 485 int64_t pf_start, pf_end, ipf_start, ipf_end; 486 int epbs, issued; 487 488 if (missed) 489 zs->zs_missed = missed; 490 491 /* 492 * Postpone the prefetch if there are more concurrent callers. 493 * It happens when multiple requests are waiting for the same 494 * indirect block. The last one will run the prefetch for all. 495 */ 496 if (zfs_refcount_remove(&zs->zs_callers, NULL) != 0) { 497 /* Drop reference taken in dmu_zfetch_prepare(). */ 498 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0) 499 dmu_zfetch_stream_fini(zs); 500 return; 501 } 502 503 mutex_enter(&zf->zf_lock); 504 if (zs->zs_missed) { 505 pf_start = zs->zs_pf_start; 506 pf_end = zs->zs_pf_start = zs->zs_pf_end; 507 } else { 508 pf_start = pf_end = 0; 509 } 510 ipf_start = zs->zs_ipf_start; 511 ipf_end = zs->zs_ipf_start = zs->zs_ipf_end; 512 mutex_exit(&zf->zf_lock); 513 ASSERT3S(pf_start, <=, pf_end); 514 ASSERT3S(ipf_start, <=, ipf_end); 515 516 epbs = zf->zf_dnode->dn_indblkshift - SPA_BLKPTRSHIFT; 517 ipf_start = P2ROUNDUP(ipf_start, 1 << epbs) >> epbs; 518 ipf_end = P2ROUNDUP(ipf_end, 1 << epbs) >> epbs; 519 ASSERT3S(ipf_start, <=, ipf_end); 520 issued = pf_end - pf_start + ipf_end - ipf_start; 521 if (issued > 1) { 522 /* More references on top of taken in dmu_zfetch_prepare(). */ 523 zfs_refcount_add_few(&zs->zs_refs, issued - 1, NULL); 524 } else if (issued == 0) { 525 /* Some other thread has done our work, so drop the ref. */ 526 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0) 527 dmu_zfetch_stream_fini(zs); 528 return; 529 } 530 aggsum_add(&zfetch_sums.zfetchstat_io_active, issued); 531 532 if (!have_lock) 533 rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER); 534 535 issued = 0; 536 for (int64_t blk = pf_start; blk < pf_end; blk++) { 537 issued += dbuf_prefetch_impl(zf->zf_dnode, 0, blk, 538 ZIO_PRIORITY_ASYNC_READ, 0, dmu_zfetch_done, zs); 539 } 540 for (int64_t iblk = ipf_start; iblk < ipf_end; iblk++) { 541 issued += dbuf_prefetch_impl(zf->zf_dnode, 1, iblk, 542 ZIO_PRIORITY_ASYNC_READ, 0, dmu_zfetch_done, zs); 543 } 544 545 if (!have_lock) 546 rw_exit(&zf->zf_dnode->dn_struct_rwlock); 547 548 if (issued) 549 ZFETCHSTAT_ADD(zfetchstat_io_issued, issued); 550 } 551 552 void 553 dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks, boolean_t fetch_data, 554 boolean_t missed, boolean_t have_lock) 555 { 556 zstream_t *zs; 557 558 zs = dmu_zfetch_prepare(zf, blkid, nblks, fetch_data, have_lock); 559 if (zs) 560 dmu_zfetch_run(zs, missed, have_lock); 561 } 562 563 ZFS_MODULE_PARAM(zfs_prefetch, zfs_prefetch_, disable, INT, ZMOD_RW, 564 "Disable all ZFS prefetching"); 565 566 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_streams, UINT, ZMOD_RW, 567 "Max number of streams per zfetch"); 568 569 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, min_sec_reap, UINT, ZMOD_RW, 570 "Min time before stream reclaim"); 571 572 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_sec_reap, UINT, ZMOD_RW, 573 "Max time before stream delete"); 574 575 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, min_distance, UINT, ZMOD_RW, 576 "Min bytes to prefetch per stream"); 577 578 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_distance, UINT, ZMOD_RW, 579 "Max bytes to prefetch per stream"); 580 581 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_idistance, UINT, ZMOD_RW, 582 "Max bytes to prefetch indirects for per stream"); 583 584 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, array_rd_sz, U64, ZMOD_RW, 585 "Number of bytes in a array_read"); 586