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