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