dmu_zfetch.c revision 288571
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 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 38/* 39 * I'm against tune-ables, but these should probably exist as tweakable globals 40 * until we can get this working the way we want it to. 41 */ 42 43int zfs_prefetch_disable = 0; 44 45/* max # of streams per zfetch */ 46uint32_t zfetch_max_streams = 8; 47/* min time before stream reclaim */ 48uint32_t zfetch_min_sec_reap = 2; 49/* max number of blocks to fetch at a time */ 50uint32_t zfetch_block_cap = 256; 51/* number of bytes in a array_read at which we stop prefetching (1Mb) */ 52uint64_t zfetch_array_rd_sz = 1024 * 1024; 53 54SYSCTL_DECL(_vfs_zfs); 55SYSCTL_INT(_vfs_zfs, OID_AUTO, prefetch_disable, CTLFLAG_RW, 56 &zfs_prefetch_disable, 0, "Disable prefetch"); 57SYSCTL_NODE(_vfs_zfs, OID_AUTO, zfetch, CTLFLAG_RW, 0, "ZFS ZFETCH"); 58TUNABLE_INT("vfs.zfs.zfetch.max_streams", &zfetch_max_streams); 59SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, max_streams, CTLFLAG_RW, 60 &zfetch_max_streams, 0, "Max # of streams per zfetch"); 61TUNABLE_INT("vfs.zfs.zfetch.min_sec_reap", &zfetch_min_sec_reap); 62SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, min_sec_reap, CTLFLAG_RWTUN, 63 &zfetch_min_sec_reap, 0, "Min time before stream reclaim"); 64TUNABLE_INT("vfs.zfs.zfetch.block_cap", &zfetch_block_cap); 65SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, block_cap, CTLFLAG_RWTUN, 66 &zfetch_block_cap, 0, "Max number of blocks to fetch at a time"); 67TUNABLE_QUAD("vfs.zfs.zfetch.array_rd_sz", &zfetch_array_rd_sz); 68SYSCTL_UQUAD(_vfs_zfs_zfetch, OID_AUTO, array_rd_sz, CTLFLAG_RWTUN, 69 &zfetch_array_rd_sz, 0, 70 "Number of bytes in a array_read at which we stop prefetching"); 71 72/* forward decls for static routines */ 73static boolean_t dmu_zfetch_colinear(zfetch_t *, zstream_t *); 74static void dmu_zfetch_dofetch(zfetch_t *, zstream_t *); 75static uint64_t dmu_zfetch_fetch(dnode_t *, uint64_t, uint64_t); 76static uint64_t dmu_zfetch_fetchsz(dnode_t *, uint64_t, uint64_t); 77static boolean_t dmu_zfetch_find(zfetch_t *, zstream_t *, int); 78static int dmu_zfetch_stream_insert(zfetch_t *, zstream_t *); 79static zstream_t *dmu_zfetch_stream_reclaim(zfetch_t *); 80static void dmu_zfetch_stream_remove(zfetch_t *, zstream_t *); 81static int dmu_zfetch_streams_equal(zstream_t *, zstream_t *); 82 83typedef struct zfetch_stats { 84 kstat_named_t zfetchstat_hits; 85 kstat_named_t zfetchstat_misses; 86 kstat_named_t zfetchstat_colinear_hits; 87 kstat_named_t zfetchstat_colinear_misses; 88 kstat_named_t zfetchstat_stride_hits; 89 kstat_named_t zfetchstat_stride_misses; 90 kstat_named_t zfetchstat_reclaim_successes; 91 kstat_named_t zfetchstat_reclaim_failures; 92 kstat_named_t zfetchstat_stream_resets; 93 kstat_named_t zfetchstat_stream_noresets; 94 kstat_named_t zfetchstat_bogus_streams; 95} zfetch_stats_t; 96 97static zfetch_stats_t zfetch_stats = { 98 { "hits", KSTAT_DATA_UINT64 }, 99 { "misses", KSTAT_DATA_UINT64 }, 100 { "colinear_hits", KSTAT_DATA_UINT64 }, 101 { "colinear_misses", KSTAT_DATA_UINT64 }, 102 { "stride_hits", KSTAT_DATA_UINT64 }, 103 { "stride_misses", KSTAT_DATA_UINT64 }, 104 { "reclaim_successes", KSTAT_DATA_UINT64 }, 105 { "reclaim_failures", KSTAT_DATA_UINT64 }, 106 { "streams_resets", KSTAT_DATA_UINT64 }, 107 { "streams_noresets", KSTAT_DATA_UINT64 }, 108 { "bogus_streams", KSTAT_DATA_UINT64 }, 109}; 110 111#define ZFETCHSTAT_INCR(stat, val) \ 112 atomic_add_64(&zfetch_stats.stat.value.ui64, (val)); 113 114#define ZFETCHSTAT_BUMP(stat) ZFETCHSTAT_INCR(stat, 1); 115 116kstat_t *zfetch_ksp; 117 118/* 119 * Given a zfetch structure and a zstream structure, determine whether the 120 * blocks to be read are part of a co-linear pair of existing prefetch 121 * streams. If a set is found, coalesce the streams, removing one, and 122 * configure the prefetch so it looks for a strided access pattern. 123 * 124 * In other words: if we find two sequential access streams that are 125 * the same length and distance N appart, and this read is N from the 126 * last stream, then we are probably in a strided access pattern. So 127 * combine the two sequential streams into a single strided stream. 128 * 129 * Returns whether co-linear streams were found. 130 */ 131static boolean_t 132dmu_zfetch_colinear(zfetch_t *zf, zstream_t *zh) 133{ 134 zstream_t *z_walk; 135 zstream_t *z_comp; 136 137 if (! rw_tryenter(&zf->zf_rwlock, RW_WRITER)) 138 return (0); 139 140 if (zh == NULL) { 141 rw_exit(&zf->zf_rwlock); 142 return (0); 143 } 144 145 for (z_walk = list_head(&zf->zf_stream); z_walk; 146 z_walk = list_next(&zf->zf_stream, z_walk)) { 147 for (z_comp = list_next(&zf->zf_stream, z_walk); z_comp; 148 z_comp = list_next(&zf->zf_stream, z_comp)) { 149 int64_t diff; 150 151 if (z_walk->zst_len != z_walk->zst_stride || 152 z_comp->zst_len != z_comp->zst_stride) { 153 continue; 154 } 155 156 diff = z_comp->zst_offset - z_walk->zst_offset; 157 if (z_comp->zst_offset + diff == zh->zst_offset) { 158 z_walk->zst_offset = zh->zst_offset; 159 z_walk->zst_direction = diff < 0 ? -1 : 1; 160 z_walk->zst_stride = 161 diff * z_walk->zst_direction; 162 z_walk->zst_ph_offset = 163 zh->zst_offset + z_walk->zst_stride; 164 dmu_zfetch_stream_remove(zf, z_comp); 165 mutex_destroy(&z_comp->zst_lock); 166 kmem_free(z_comp, sizeof (zstream_t)); 167 168 dmu_zfetch_dofetch(zf, z_walk); 169 170 rw_exit(&zf->zf_rwlock); 171 return (1); 172 } 173 174 diff = z_walk->zst_offset - z_comp->zst_offset; 175 if (z_walk->zst_offset + diff == zh->zst_offset) { 176 z_walk->zst_offset = zh->zst_offset; 177 z_walk->zst_direction = diff < 0 ? -1 : 1; 178 z_walk->zst_stride = 179 diff * z_walk->zst_direction; 180 z_walk->zst_ph_offset = 181 zh->zst_offset + z_walk->zst_stride; 182 dmu_zfetch_stream_remove(zf, z_comp); 183 mutex_destroy(&z_comp->zst_lock); 184 kmem_free(z_comp, sizeof (zstream_t)); 185 186 dmu_zfetch_dofetch(zf, z_walk); 187 188 rw_exit(&zf->zf_rwlock); 189 return (1); 190 } 191 } 192 } 193 194 rw_exit(&zf->zf_rwlock); 195 return (0); 196} 197 198/* 199 * Given a zstream_t, determine the bounds of the prefetch. Then call the 200 * routine that actually prefetches the individual blocks. 201 */ 202static void 203dmu_zfetch_dofetch(zfetch_t *zf, zstream_t *zs) 204{ 205 uint64_t prefetch_tail; 206 uint64_t prefetch_limit; 207 uint64_t prefetch_ofst; 208 uint64_t prefetch_len; 209 uint64_t blocks_fetched; 210 211 zs->zst_stride = MAX((int64_t)zs->zst_stride, zs->zst_len); 212 zs->zst_cap = MIN(zfetch_block_cap, 2 * zs->zst_cap); 213 214 prefetch_tail = MAX((int64_t)zs->zst_ph_offset, 215 (int64_t)(zs->zst_offset + zs->zst_stride)); 216 /* 217 * XXX: use a faster division method? 218 */ 219 prefetch_limit = zs->zst_offset + zs->zst_len + 220 (zs->zst_cap * zs->zst_stride) / zs->zst_len; 221 222 while (prefetch_tail < prefetch_limit) { 223 prefetch_ofst = zs->zst_offset + zs->zst_direction * 224 (prefetch_tail - zs->zst_offset); 225 226 prefetch_len = zs->zst_len; 227 228 /* 229 * Don't prefetch beyond the end of the file, if working 230 * backwards. 231 */ 232 if ((zs->zst_direction == ZFETCH_BACKWARD) && 233 (prefetch_ofst > prefetch_tail)) { 234 prefetch_len += prefetch_ofst; 235 prefetch_ofst = 0; 236 } 237 238 /* don't prefetch more than we're supposed to */ 239 if (prefetch_len > zs->zst_len) 240 break; 241 242 blocks_fetched = dmu_zfetch_fetch(zf->zf_dnode, 243 prefetch_ofst, zs->zst_len); 244 245 prefetch_tail += zs->zst_stride; 246 /* stop if we've run out of stuff to prefetch */ 247 if (blocks_fetched < zs->zst_len) 248 break; 249 } 250 zs->zst_ph_offset = prefetch_tail; 251 zs->zst_last = ddi_get_lbolt(); 252} 253 254void 255zfetch_init(void) 256{ 257 258 zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc", 259 KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t), 260 KSTAT_FLAG_VIRTUAL); 261 262 if (zfetch_ksp != NULL) { 263 zfetch_ksp->ks_data = &zfetch_stats; 264 kstat_install(zfetch_ksp); 265 } 266} 267 268void 269zfetch_fini(void) 270{ 271 if (zfetch_ksp != NULL) { 272 kstat_delete(zfetch_ksp); 273 zfetch_ksp = NULL; 274 } 275} 276 277/* 278 * This takes a pointer to a zfetch structure and a dnode. It performs the 279 * necessary setup for the zfetch structure, grokking data from the 280 * associated dnode. 281 */ 282void 283dmu_zfetch_init(zfetch_t *zf, dnode_t *dno) 284{ 285 if (zf == NULL) { 286 return; 287 } 288 289 zf->zf_dnode = dno; 290 zf->zf_stream_cnt = 0; 291 zf->zf_alloc_fail = 0; 292 293 list_create(&zf->zf_stream, sizeof (zstream_t), 294 offsetof(zstream_t, zst_node)); 295 296 rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL); 297} 298 299/* 300 * This function computes the actual size, in blocks, that can be prefetched, 301 * and fetches it. 302 */ 303static uint64_t 304dmu_zfetch_fetch(dnode_t *dn, uint64_t blkid, uint64_t nblks) 305{ 306 uint64_t fetchsz; 307 uint64_t i; 308 309 fetchsz = dmu_zfetch_fetchsz(dn, blkid, nblks); 310 311 for (i = 0; i < fetchsz; i++) { 312 dbuf_prefetch(dn, 0, blkid + i, ZIO_PRIORITY_ASYNC_READ, 313 ARC_FLAG_PREFETCH); 314 } 315 316 return (fetchsz); 317} 318 319/* 320 * this function returns the number of blocks that would be prefetched, based 321 * upon the supplied dnode, blockid, and nblks. This is used so that we can 322 * update streams in place, and then prefetch with their old value after the 323 * fact. This way, we can delay the prefetch, but subsequent accesses to the 324 * stream won't result in the same data being prefetched multiple times. 325 */ 326static uint64_t 327dmu_zfetch_fetchsz(dnode_t *dn, uint64_t blkid, uint64_t nblks) 328{ 329 uint64_t fetchsz; 330 331 if (blkid > dn->dn_maxblkid) { 332 return (0); 333 } 334 335 /* compute fetch size */ 336 if (blkid + nblks + 1 > dn->dn_maxblkid) { 337 fetchsz = (dn->dn_maxblkid - blkid) + 1; 338 ASSERT(blkid + fetchsz - 1 <= dn->dn_maxblkid); 339 } else { 340 fetchsz = nblks; 341 } 342 343 344 return (fetchsz); 345} 346 347/* 348 * given a zfetch and a zstream structure, see if there is an associated zstream 349 * for this block read. If so, it starts a prefetch for the stream it 350 * located and returns true, otherwise it returns false 351 */ 352static boolean_t 353dmu_zfetch_find(zfetch_t *zf, zstream_t *zh, int prefetched) 354{ 355 zstream_t *zs; 356 int64_t diff; 357 int reset = !prefetched; 358 int rc = 0; 359 360 if (zh == NULL) 361 return (0); 362 363 /* 364 * XXX: This locking strategy is a bit coarse; however, it's impact has 365 * yet to be tested. If this turns out to be an issue, it can be 366 * modified in a number of different ways. 367 */ 368 369 rw_enter(&zf->zf_rwlock, RW_READER); 370top: 371 372 for (zs = list_head(&zf->zf_stream); zs; 373 zs = list_next(&zf->zf_stream, zs)) { 374 375 /* 376 * XXX - should this be an assert? 377 */ 378 if (zs->zst_len == 0) { 379 /* bogus stream */ 380 ZFETCHSTAT_BUMP(zfetchstat_bogus_streams); 381 continue; 382 } 383 384 /* 385 * We hit this case when we are in a strided prefetch stream: 386 * we will read "len" blocks before "striding". 387 */ 388 if (zh->zst_offset >= zs->zst_offset && 389 zh->zst_offset < zs->zst_offset + zs->zst_len) { 390 if (prefetched) { 391 /* already fetched */ 392 ZFETCHSTAT_BUMP(zfetchstat_stride_hits); 393 rc = 1; 394 goto out; 395 } else { 396 ZFETCHSTAT_BUMP(zfetchstat_stride_misses); 397 } 398 } 399 400 /* 401 * This is the forward sequential read case: we increment 402 * len by one each time we hit here, so we will enter this 403 * case on every read. 404 */ 405 if (zh->zst_offset == zs->zst_offset + zs->zst_len) { 406 407 reset = !prefetched && zs->zst_len > 1; 408 409 if (mutex_tryenter(&zs->zst_lock) == 0) { 410 rc = 1; 411 goto out; 412 } 413 414 if (zh->zst_offset != zs->zst_offset + zs->zst_len) { 415 mutex_exit(&zs->zst_lock); 416 goto top; 417 } 418 zs->zst_len += zh->zst_len; 419 diff = zs->zst_len - zfetch_block_cap; 420 if (diff > 0) { 421 zs->zst_offset += diff; 422 zs->zst_len = zs->zst_len > diff ? 423 zs->zst_len - diff : 0; 424 } 425 zs->zst_direction = ZFETCH_FORWARD; 426 427 break; 428 429 /* 430 * Same as above, but reading backwards through the file. 431 */ 432 } else if (zh->zst_offset == zs->zst_offset - zh->zst_len) { 433 /* backwards sequential access */ 434 435 reset = !prefetched && zs->zst_len > 1; 436 437 if (mutex_tryenter(&zs->zst_lock) == 0) { 438 rc = 1; 439 goto out; 440 } 441 442 if (zh->zst_offset != zs->zst_offset - zh->zst_len) { 443 mutex_exit(&zs->zst_lock); 444 goto top; 445 } 446 447 zs->zst_offset = zs->zst_offset > zh->zst_len ? 448 zs->zst_offset - zh->zst_len : 0; 449 zs->zst_ph_offset = zs->zst_ph_offset > zh->zst_len ? 450 zs->zst_ph_offset - zh->zst_len : 0; 451 zs->zst_len += zh->zst_len; 452 453 diff = zs->zst_len - zfetch_block_cap; 454 if (diff > 0) { 455 zs->zst_ph_offset = zs->zst_ph_offset > diff ? 456 zs->zst_ph_offset - diff : 0; 457 zs->zst_len = zs->zst_len > diff ? 458 zs->zst_len - diff : zs->zst_len; 459 } 460 zs->zst_direction = ZFETCH_BACKWARD; 461 462 break; 463 464 } else if ((zh->zst_offset - zs->zst_offset - zs->zst_stride < 465 zs->zst_len) && (zs->zst_len != zs->zst_stride)) { 466 /* strided forward access */ 467 468 if (mutex_tryenter(&zs->zst_lock) == 0) { 469 rc = 1; 470 goto out; 471 } 472 473 if ((zh->zst_offset - zs->zst_offset - zs->zst_stride >= 474 zs->zst_len) || (zs->zst_len == zs->zst_stride)) { 475 mutex_exit(&zs->zst_lock); 476 goto top; 477 } 478 479 zs->zst_offset += zs->zst_stride; 480 zs->zst_direction = ZFETCH_FORWARD; 481 482 break; 483 484 } else if ((zh->zst_offset - zs->zst_offset + zs->zst_stride < 485 zs->zst_len) && (zs->zst_len != zs->zst_stride)) { 486 /* strided reverse access */ 487 488 if (mutex_tryenter(&zs->zst_lock) == 0) { 489 rc = 1; 490 goto out; 491 } 492 493 if ((zh->zst_offset - zs->zst_offset + zs->zst_stride >= 494 zs->zst_len) || (zs->zst_len == zs->zst_stride)) { 495 mutex_exit(&zs->zst_lock); 496 goto top; 497 } 498 499 zs->zst_offset = zs->zst_offset > zs->zst_stride ? 500 zs->zst_offset - zs->zst_stride : 0; 501 zs->zst_ph_offset = (zs->zst_ph_offset > 502 (2 * zs->zst_stride)) ? 503 (zs->zst_ph_offset - (2 * zs->zst_stride)) : 0; 504 zs->zst_direction = ZFETCH_BACKWARD; 505 506 break; 507 } 508 } 509 510 if (zs) { 511 if (reset) { 512 zstream_t *remove = zs; 513 514 ZFETCHSTAT_BUMP(zfetchstat_stream_resets); 515 rc = 0; 516 mutex_exit(&zs->zst_lock); 517 rw_exit(&zf->zf_rwlock); 518 rw_enter(&zf->zf_rwlock, RW_WRITER); 519 /* 520 * Relocate the stream, in case someone removes 521 * it while we were acquiring the WRITER lock. 522 */ 523 for (zs = list_head(&zf->zf_stream); zs; 524 zs = list_next(&zf->zf_stream, zs)) { 525 if (zs == remove) { 526 dmu_zfetch_stream_remove(zf, zs); 527 mutex_destroy(&zs->zst_lock); 528 kmem_free(zs, sizeof (zstream_t)); 529 break; 530 } 531 } 532 } else { 533 ZFETCHSTAT_BUMP(zfetchstat_stream_noresets); 534 rc = 1; 535 dmu_zfetch_dofetch(zf, zs); 536 mutex_exit(&zs->zst_lock); 537 } 538 } 539out: 540 rw_exit(&zf->zf_rwlock); 541 return (rc); 542} 543 544/* 545 * Clean-up state associated with a zfetch structure. This frees allocated 546 * structure members, empties the zf_stream tree, and generally makes things 547 * nice. This doesn't free the zfetch_t itself, that's left to the caller. 548 */ 549void 550dmu_zfetch_rele(zfetch_t *zf) 551{ 552 zstream_t *zs; 553 zstream_t *zs_next; 554 555 ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock)); 556 557 for (zs = list_head(&zf->zf_stream); zs; zs = zs_next) { 558 zs_next = list_next(&zf->zf_stream, zs); 559 560 list_remove(&zf->zf_stream, zs); 561 mutex_destroy(&zs->zst_lock); 562 kmem_free(zs, sizeof (zstream_t)); 563 } 564 list_destroy(&zf->zf_stream); 565 rw_destroy(&zf->zf_rwlock); 566 567 zf->zf_dnode = NULL; 568} 569 570/* 571 * Given a zfetch and zstream structure, insert the zstream structure into the 572 * AVL tree contained within the zfetch structure. Peform the appropriate 573 * book-keeping. It is possible that another thread has inserted a stream which 574 * matches one that we are about to insert, so we must be sure to check for this 575 * case. If one is found, return failure, and let the caller cleanup the 576 * duplicates. 577 */ 578static int 579dmu_zfetch_stream_insert(zfetch_t *zf, zstream_t *zs) 580{ 581 zstream_t *zs_walk; 582 zstream_t *zs_next; 583 584 ASSERT(RW_WRITE_HELD(&zf->zf_rwlock)); 585 586 for (zs_walk = list_head(&zf->zf_stream); zs_walk; zs_walk = zs_next) { 587 zs_next = list_next(&zf->zf_stream, zs_walk); 588 589 if (dmu_zfetch_streams_equal(zs_walk, zs)) { 590 return (0); 591 } 592 } 593 594 list_insert_head(&zf->zf_stream, zs); 595 zf->zf_stream_cnt++; 596 return (1); 597} 598 599 600/* 601 * Walk the list of zstreams in the given zfetch, find an old one (by time), and 602 * reclaim it for use by the caller. 603 */ 604static zstream_t * 605dmu_zfetch_stream_reclaim(zfetch_t *zf) 606{ 607 zstream_t *zs; 608 clock_t ticks; 609 610 ticks = zfetch_min_sec_reap * hz; 611 if (! rw_tryenter(&zf->zf_rwlock, RW_WRITER)) 612 return (0); 613 614 for (zs = list_head(&zf->zf_stream); zs; 615 zs = list_next(&zf->zf_stream, zs)) { 616 617 if (ddi_get_lbolt() - zs->zst_last > ticks) 618 break; 619 } 620 621 if (zs) { 622 dmu_zfetch_stream_remove(zf, zs); 623 mutex_destroy(&zs->zst_lock); 624 bzero(zs, sizeof (zstream_t)); 625 } else { 626 zf->zf_alloc_fail++; 627 } 628 rw_exit(&zf->zf_rwlock); 629 630 return (zs); 631} 632 633/* 634 * Given a zfetch and zstream structure, remove the zstream structure from its 635 * container in the zfetch structure. Perform the appropriate book-keeping. 636 */ 637static void 638dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs) 639{ 640 ASSERT(RW_WRITE_HELD(&zf->zf_rwlock)); 641 642 list_remove(&zf->zf_stream, zs); 643 zf->zf_stream_cnt--; 644} 645 646static int 647dmu_zfetch_streams_equal(zstream_t *zs1, zstream_t *zs2) 648{ 649 if (zs1->zst_offset != zs2->zst_offset) 650 return (0); 651 652 if (zs1->zst_len != zs2->zst_len) 653 return (0); 654 655 if (zs1->zst_stride != zs2->zst_stride) 656 return (0); 657 658 if (zs1->zst_ph_offset != zs2->zst_ph_offset) 659 return (0); 660 661 if (zs1->zst_cap != zs2->zst_cap) 662 return (0); 663 664 if (zs1->zst_direction != zs2->zst_direction) 665 return (0); 666 667 return (1); 668} 669 670/* 671 * This is the prefetch entry point. It calls all of the other dmu_zfetch 672 * routines to create, delete, find, or operate upon prefetch streams. 673 */ 674void 675dmu_zfetch(zfetch_t *zf, uint64_t offset, uint64_t size, int prefetched) 676{ 677 zstream_t zst; 678 zstream_t *newstream; 679 boolean_t fetched; 680 int inserted; 681 unsigned int blkshft; 682 uint64_t blksz; 683 684 if (zfs_prefetch_disable) 685 return; 686 687 /* files that aren't ln2 blocksz are only one block -- nothing to do */ 688 if (!zf->zf_dnode->dn_datablkshift) 689 return; 690 691 /* convert offset and size, into blockid and nblocks */ 692 blkshft = zf->zf_dnode->dn_datablkshift; 693 blksz = (1 << blkshft); 694 695 bzero(&zst, sizeof (zstream_t)); 696 zst.zst_offset = offset >> blkshft; 697 zst.zst_len = (P2ROUNDUP(offset + size, blksz) - 698 P2ALIGN(offset, blksz)) >> blkshft; 699 700 fetched = dmu_zfetch_find(zf, &zst, prefetched); 701 if (fetched) { 702 ZFETCHSTAT_BUMP(zfetchstat_hits); 703 } else { 704 ZFETCHSTAT_BUMP(zfetchstat_misses); 705 fetched = dmu_zfetch_colinear(zf, &zst); 706 if (fetched) { 707 ZFETCHSTAT_BUMP(zfetchstat_colinear_hits); 708 } else { 709 ZFETCHSTAT_BUMP(zfetchstat_colinear_misses); 710 } 711 } 712 713 if (!fetched) { 714 newstream = dmu_zfetch_stream_reclaim(zf); 715 716 /* 717 * we still couldn't find a stream, drop the lock, and allocate 718 * one if possible. Otherwise, give up and go home. 719 */ 720 if (newstream) { 721 ZFETCHSTAT_BUMP(zfetchstat_reclaim_successes); 722 } else { 723 uint64_t maxblocks; 724 uint32_t max_streams; 725 uint32_t cur_streams; 726 727 ZFETCHSTAT_BUMP(zfetchstat_reclaim_failures); 728 cur_streams = zf->zf_stream_cnt; 729 maxblocks = zf->zf_dnode->dn_maxblkid; 730 731 max_streams = MIN(zfetch_max_streams, 732 (maxblocks / zfetch_block_cap)); 733 if (max_streams == 0) { 734 max_streams++; 735 } 736 737 if (cur_streams >= max_streams) { 738 return; 739 } 740 newstream = kmem_zalloc(sizeof (zstream_t), KM_SLEEP); 741 } 742 743 newstream->zst_offset = zst.zst_offset; 744 newstream->zst_len = zst.zst_len; 745 newstream->zst_stride = zst.zst_len; 746 newstream->zst_ph_offset = zst.zst_len + zst.zst_offset; 747 newstream->zst_cap = zst.zst_len; 748 newstream->zst_direction = ZFETCH_FORWARD; 749 newstream->zst_last = ddi_get_lbolt(); 750 751 mutex_init(&newstream->zst_lock, NULL, MUTEX_DEFAULT, NULL); 752 753 rw_enter(&zf->zf_rwlock, RW_WRITER); 754 inserted = dmu_zfetch_stream_insert(zf, newstream); 755 rw_exit(&zf->zf_rwlock); 756 757 if (!inserted) { 758 mutex_destroy(&newstream->zst_lock); 759 kmem_free(newstream, sizeof (zstream_t)); 760 } 761 } 762} 763