1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
29 */
30
31 #include <sys/zfs_context.h>
32 #include <sys/arc_impl.h>
33 #include <sys/dnode.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dmu_zfetch.h>
36 #include <sys/dmu.h>
37 #include <sys/dbuf.h>
38 #include <sys/kstat.h>
39 #include <sys/wmsum.h>
40
41 /*
42 * This tunable disables predictive prefetch. Note that it leaves "prescient"
43 * prefetch (e.g. prefetch for zfs send) intact. Unlike predictive prefetch,
44 * prescient prefetch never issues i/os that end up not being needed,
45 * so it can't hurt performance.
46 */
47
48 static int zfs_prefetch_disable = B_FALSE;
49
50 /* max # of streams per zfetch */
51 static unsigned int zfetch_max_streams = 8;
52 /* min time before stream reclaim */
53 static unsigned int zfetch_min_sec_reap = 1;
54 /* max time before stream delete */
55 static unsigned int zfetch_max_sec_reap = 2;
56 #ifdef _ILP32
57 /* min bytes to prefetch per stream (default 2MB) */
58 static unsigned int zfetch_min_distance = 2 * 1024 * 1024;
59 /* max bytes to prefetch per stream (default 8MB) */
60 unsigned int zfetch_max_distance = 8 * 1024 * 1024;
61 #else
62 /* min bytes to prefetch per stream (default 4MB) */
63 static unsigned int zfetch_min_distance = 4 * 1024 * 1024;
64 /* max bytes to prefetch per stream (default 64MB) */
65 unsigned int zfetch_max_distance = 64 * 1024 * 1024;
66 #endif
67 /* max bytes to prefetch indirects for per stream (default 128MB) */
68 unsigned int zfetch_max_idistance = 128 * 1024 * 1024;
69 /* max request reorder distance within a stream (default 16MB) */
70 unsigned int zfetch_max_reorder = 16 * 1024 * 1024;
71 /* Max log2 fraction of holes in a stream */
72 unsigned int zfetch_hole_shift = 2;
73
74 typedef struct zfetch_stats {
75 kstat_named_t zfetchstat_hits;
76 kstat_named_t zfetchstat_future;
77 kstat_named_t zfetchstat_stride;
78 kstat_named_t zfetchstat_past;
79 kstat_named_t zfetchstat_misses;
80 kstat_named_t zfetchstat_max_streams;
81 kstat_named_t zfetchstat_io_issued;
82 kstat_named_t zfetchstat_io_active;
83 } zfetch_stats_t;
84
85 static zfetch_stats_t zfetch_stats = {
86 { "hits", KSTAT_DATA_UINT64 },
87 { "future", KSTAT_DATA_UINT64 },
88 { "stride", KSTAT_DATA_UINT64 },
89 { "past", KSTAT_DATA_UINT64 },
90 { "misses", KSTAT_DATA_UINT64 },
91 { "max_streams", KSTAT_DATA_UINT64 },
92 { "io_issued", KSTAT_DATA_UINT64 },
93 { "io_active", KSTAT_DATA_UINT64 },
94 };
95
96 struct {
97 wmsum_t zfetchstat_hits;
98 wmsum_t zfetchstat_future;
99 wmsum_t zfetchstat_stride;
100 wmsum_t zfetchstat_past;
101 wmsum_t zfetchstat_misses;
102 wmsum_t zfetchstat_max_streams;
103 wmsum_t zfetchstat_io_issued;
104 aggsum_t zfetchstat_io_active;
105 } zfetch_sums;
106
107 #define ZFETCHSTAT_BUMP(stat) \
108 wmsum_add(&zfetch_sums.stat, 1)
109 #define ZFETCHSTAT_ADD(stat, val) \
110 wmsum_add(&zfetch_sums.stat, val)
111
112
113 static kstat_t *zfetch_ksp;
114
115 static int
zfetch_kstats_update(kstat_t * ksp,int rw)116 zfetch_kstats_update(kstat_t *ksp, int rw)
117 {
118 zfetch_stats_t *zs = ksp->ks_data;
119
120 if (rw == KSTAT_WRITE)
121 return (EACCES);
122 zs->zfetchstat_hits.value.ui64 =
123 wmsum_value(&zfetch_sums.zfetchstat_hits);
124 zs->zfetchstat_future.value.ui64 =
125 wmsum_value(&zfetch_sums.zfetchstat_future);
126 zs->zfetchstat_stride.value.ui64 =
127 wmsum_value(&zfetch_sums.zfetchstat_stride);
128 zs->zfetchstat_past.value.ui64 =
129 wmsum_value(&zfetch_sums.zfetchstat_past);
130 zs->zfetchstat_misses.value.ui64 =
131 wmsum_value(&zfetch_sums.zfetchstat_misses);
132 zs->zfetchstat_max_streams.value.ui64 =
133 wmsum_value(&zfetch_sums.zfetchstat_max_streams);
134 zs->zfetchstat_io_issued.value.ui64 =
135 wmsum_value(&zfetch_sums.zfetchstat_io_issued);
136 zs->zfetchstat_io_active.value.ui64 =
137 aggsum_value(&zfetch_sums.zfetchstat_io_active);
138 return (0);
139 }
140
141 void
zfetch_init(void)142 zfetch_init(void)
143 {
144 wmsum_init(&zfetch_sums.zfetchstat_hits, 0);
145 wmsum_init(&zfetch_sums.zfetchstat_future, 0);
146 wmsum_init(&zfetch_sums.zfetchstat_stride, 0);
147 wmsum_init(&zfetch_sums.zfetchstat_past, 0);
148 wmsum_init(&zfetch_sums.zfetchstat_misses, 0);
149 wmsum_init(&zfetch_sums.zfetchstat_max_streams, 0);
150 wmsum_init(&zfetch_sums.zfetchstat_io_issued, 0);
151 aggsum_init(&zfetch_sums.zfetchstat_io_active, 0);
152
153 zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
154 KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
155 KSTAT_FLAG_VIRTUAL);
156
157 if (zfetch_ksp != NULL) {
158 zfetch_ksp->ks_data = &zfetch_stats;
159 zfetch_ksp->ks_update = zfetch_kstats_update;
160 kstat_install(zfetch_ksp);
161 }
162 }
163
164 void
zfetch_fini(void)165 zfetch_fini(void)
166 {
167 if (zfetch_ksp != NULL) {
168 kstat_delete(zfetch_ksp);
169 zfetch_ksp = NULL;
170 }
171
172 wmsum_fini(&zfetch_sums.zfetchstat_hits);
173 wmsum_fini(&zfetch_sums.zfetchstat_future);
174 wmsum_fini(&zfetch_sums.zfetchstat_stride);
175 wmsum_fini(&zfetch_sums.zfetchstat_past);
176 wmsum_fini(&zfetch_sums.zfetchstat_misses);
177 wmsum_fini(&zfetch_sums.zfetchstat_max_streams);
178 wmsum_fini(&zfetch_sums.zfetchstat_io_issued);
179 ASSERT0(aggsum_value(&zfetch_sums.zfetchstat_io_active));
180 aggsum_fini(&zfetch_sums.zfetchstat_io_active);
181 }
182
183 /*
184 * This takes a pointer to a zfetch structure and a dnode. It performs the
185 * necessary setup for the zfetch structure, grokking data from the
186 * associated dnode.
187 */
188 void
dmu_zfetch_init(zfetch_t * zf,dnode_t * dno)189 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
190 {
191 if (zf == NULL)
192 return;
193 zf->zf_dnode = dno;
194 zf->zf_numstreams = 0;
195
196 list_create(&zf->zf_stream, sizeof (zstream_t),
197 offsetof(zstream_t, zs_node));
198
199 mutex_init(&zf->zf_lock, NULL, MUTEX_DEFAULT, NULL);
200 }
201
202 static void
dmu_zfetch_stream_fini(zstream_t * zs)203 dmu_zfetch_stream_fini(zstream_t *zs)
204 {
205 ASSERT(!list_link_active(&zs->zs_node));
206 zfs_refcount_destroy(&zs->zs_callers);
207 zfs_refcount_destroy(&zs->zs_refs);
208 kmem_free(zs, sizeof (*zs));
209 }
210
211 static void
dmu_zfetch_stream_remove(zfetch_t * zf,zstream_t * zs)212 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
213 {
214 ASSERT(MUTEX_HELD(&zf->zf_lock));
215 list_remove(&zf->zf_stream, zs);
216 zf->zf_numstreams--;
217 membar_producer();
218 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)
219 dmu_zfetch_stream_fini(zs);
220 }
221
222 /*
223 * Clean-up state associated with a zfetch structure (e.g. destroy the
224 * streams). This doesn't free the zfetch_t itself, that's left to the caller.
225 */
226 void
dmu_zfetch_fini(zfetch_t * zf)227 dmu_zfetch_fini(zfetch_t *zf)
228 {
229 zstream_t *zs;
230
231 mutex_enter(&zf->zf_lock);
232 while ((zs = list_head(&zf->zf_stream)) != NULL)
233 dmu_zfetch_stream_remove(zf, zs);
234 mutex_exit(&zf->zf_lock);
235 list_destroy(&zf->zf_stream);
236 mutex_destroy(&zf->zf_lock);
237
238 zf->zf_dnode = NULL;
239 }
240
241 /*
242 * If there aren't too many active streams already, create one more.
243 * In process delete/reuse all streams without hits for zfetch_max_sec_reap.
244 * If needed, reuse oldest stream without hits for zfetch_min_sec_reap or ever.
245 * The "blkid" argument is the next block that we expect this stream to access.
246 */
247 static void
dmu_zfetch_stream_create(zfetch_t * zf,uint64_t blkid)248 dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)
249 {
250 zstream_t *zs, *zs_next, *zs_old = NULL;
251 uint_t now = gethrestime_sec(), t;
252
253 ASSERT(MUTEX_HELD(&zf->zf_lock));
254
255 /*
256 * Delete too old streams, reusing the first found one.
257 */
258 t = now - zfetch_max_sec_reap;
259 for (zs = list_head(&zf->zf_stream); zs != NULL; zs = zs_next) {
260 zs_next = list_next(&zf->zf_stream, zs);
261 /*
262 * Skip if still active. 1 -- zf_stream reference.
263 */
264 if ((int)(zs->zs_atime - t) >= 0)
265 continue;
266 if (zfs_refcount_count(&zs->zs_refs) != 1)
267 continue;
268 if (zs_old)
269 dmu_zfetch_stream_remove(zf, zs);
270 else
271 zs_old = zs;
272 }
273 if (zs_old) {
274 zs = zs_old;
275 list_remove(&zf->zf_stream, zs);
276 goto reuse;
277 }
278
279 /*
280 * The maximum number of streams is normally zfetch_max_streams,
281 * but for small files we lower it such that it's at least possible
282 * for all the streams to be non-overlapping.
283 */
284 uint32_t max_streams = MAX(1, MIN(zfetch_max_streams,
285 (zf->zf_dnode->dn_maxblkid << zf->zf_dnode->dn_datablkshift) /
286 zfetch_max_distance));
287 if (zf->zf_numstreams >= max_streams) {
288 t = now - zfetch_min_sec_reap;
289 for (zs = list_head(&zf->zf_stream); zs != NULL;
290 zs = list_next(&zf->zf_stream, zs)) {
291 if ((int)(zs->zs_atime - t) >= 0)
292 continue;
293 if (zfs_refcount_count(&zs->zs_refs) != 1)
294 continue;
295 if (zs_old == NULL ||
296 (int)(zs_old->zs_atime - zs->zs_atime) >= 0)
297 zs_old = zs;
298 }
299 if (zs_old) {
300 zs = zs_old;
301 list_remove(&zf->zf_stream, zs);
302 goto reuse;
303 }
304 ZFETCHSTAT_BUMP(zfetchstat_max_streams);
305 return;
306 }
307
308 zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);
309 zfs_refcount_create(&zs->zs_callers);
310 zfs_refcount_create(&zs->zs_refs);
311 /* One reference for zf_stream. */
312 zfs_refcount_add(&zs->zs_refs, NULL);
313 zf->zf_numstreams++;
314
315 reuse:
316 list_insert_head(&zf->zf_stream, zs);
317 zs->zs_blkid = blkid;
318 /* Allow immediate stream reuse until first hit. */
319 zs->zs_atime = now - zfetch_min_sec_reap;
320 memset(zs->zs_ranges, 0, sizeof (zs->zs_ranges));
321 zs->zs_pf_dist = 0;
322 zs->zs_ipf_dist = 0;
323 zs->zs_pf_start = blkid;
324 zs->zs_pf_end = blkid;
325 zs->zs_ipf_start = blkid;
326 zs->zs_ipf_end = blkid;
327 zs->zs_missed = B_FALSE;
328 zs->zs_more = B_FALSE;
329 }
330
331 static void
dmu_zfetch_done(void * arg,uint64_t level,uint64_t blkid,boolean_t io_issued)332 dmu_zfetch_done(void *arg, uint64_t level, uint64_t blkid, boolean_t io_issued)
333 {
334 zstream_t *zs = arg;
335
336 if (io_issued && level == 0 && blkid < zs->zs_blkid)
337 zs->zs_more = B_TRUE;
338 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)
339 dmu_zfetch_stream_fini(zs);
340 aggsum_add(&zfetch_sums.zfetchstat_io_active, -1);
341 }
342
343 /*
344 * Process stream hit access for nblks blocks starting at zs_blkid. Return
345 * number of blocks to proceed for after aggregation with future ranges.
346 */
347 static uint64_t
dmu_zfetch_hit(zstream_t * zs,uint64_t nblks)348 dmu_zfetch_hit(zstream_t *zs, uint64_t nblks)
349 {
350 uint_t i, j;
351
352 /* Optimize sequential accesses (no future ranges). */
353 if (zs->zs_ranges[0].start == 0)
354 goto done;
355
356 /* Look for intersections with further ranges. */
357 for (i = 0; i < ZFETCH_RANGES; i++) {
358 zsrange_t *r = &zs->zs_ranges[i];
359 if (r->start == 0 || r->start > nblks)
360 break;
361 if (r->end >= nblks) {
362 nblks = r->end;
363 i++;
364 break;
365 }
366 }
367
368 /* Delete all found intersecting ranges, updates remaining. */
369 for (j = 0; i < ZFETCH_RANGES; i++, j++) {
370 if (zs->zs_ranges[i].start == 0)
371 break;
372 ASSERT3U(zs->zs_ranges[i].start, >, nblks);
373 ASSERT3U(zs->zs_ranges[i].end, >, nblks);
374 zs->zs_ranges[j].start = zs->zs_ranges[i].start - nblks;
375 zs->zs_ranges[j].end = zs->zs_ranges[i].end - nblks;
376 }
377 if (j < ZFETCH_RANGES) {
378 zs->zs_ranges[j].start = 0;
379 zs->zs_ranges[j].end = 0;
380 }
381
382 done:
383 zs->zs_blkid += nblks;
384 return (nblks);
385 }
386
387 /*
388 * Process future stream access for nblks blocks starting at blkid. Return
389 * number of blocks to proceed for if future ranges reach fill threshold.
390 */
391 static uint64_t
dmu_zfetch_future(zstream_t * zs,uint64_t blkid,uint64_t nblks)392 dmu_zfetch_future(zstream_t *zs, uint64_t blkid, uint64_t nblks)
393 {
394 ASSERT3U(blkid, >, zs->zs_blkid);
395 blkid -= zs->zs_blkid;
396 ASSERT3U(blkid + nblks, <=, UINT16_MAX);
397
398 /* Search for first and last intersection or insert point. */
399 uint_t f = ZFETCH_RANGES, l = 0, i;
400 for (i = 0; i < ZFETCH_RANGES; i++) {
401 zsrange_t *r = &zs->zs_ranges[i];
402 if (r->start == 0 || r->start > blkid + nblks)
403 break;
404 if (r->end < blkid)
405 continue;
406 if (f > i)
407 f = i;
408 if (l < i)
409 l = i;
410 }
411 if (f <= l) {
412 /* Got some intersecting range, expand it if needed. */
413 if (zs->zs_ranges[f].start > blkid)
414 zs->zs_ranges[f].start = blkid;
415 zs->zs_ranges[f].end = MAX(zs->zs_ranges[l].end, blkid + nblks);
416 if (f < l) {
417 /* Got more than one intersection, remove others. */
418 for (f++, l++; l < ZFETCH_RANGES; f++, l++) {
419 zs->zs_ranges[f].start = zs->zs_ranges[l].start;
420 zs->zs_ranges[f].end = zs->zs_ranges[l].end;
421 }
422 zs->zs_ranges[f].start = 0;
423 zs->zs_ranges[f].end = 0;
424 }
425 } else if (i < ZFETCH_RANGES) {
426 /* Got no intersecting ranges, insert new one. */
427 for (l = ZFETCH_RANGES - 1; l > i; l--) {
428 zs->zs_ranges[l].start = zs->zs_ranges[l - 1].start;
429 zs->zs_ranges[l].end = zs->zs_ranges[l - 1].end;
430 }
431 zs->zs_ranges[i].start = blkid;
432 zs->zs_ranges[i].end = blkid + nblks;
433 } else {
434 /* No space left to insert. Drop the range. */
435 return (0);
436 }
437
438 /* Check if with the new access addition we reached fill threshold. */
439 if (zfetch_hole_shift >= 16)
440 return (0);
441 uint_t hole = 0;
442 for (i = f = l = 0; i < ZFETCH_RANGES; i++) {
443 zsrange_t *r = &zs->zs_ranges[i];
444 if (r->start == 0)
445 break;
446 hole += r->start - f;
447 f = r->end;
448 if (hole <= r->end >> zfetch_hole_shift)
449 l = r->end;
450 }
451 if (l > 0)
452 return (dmu_zfetch_hit(zs, l));
453
454 return (0);
455 }
456
457 /*
458 * Prime a zfetch stream at blkid, so that the first demand access triggered
459 * enough prefetch without ramp-up to sequentially read up to end_blkid.
460 */
461 boolean_t
dmu_zfetch_prime(zfetch_t * zf,uint64_t blkid,uint64_t end_blkid)462 dmu_zfetch_prime(zfetch_t *zf, uint64_t blkid, uint64_t end_blkid)
463 {
464 zstream_t *zs;
465 dnode_t *dn = zf->zf_dnode;
466 spa_t *spa = dn->dn_objset->os_spa;
467
468 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
469 if (zfs_prefetch_disable ||
470 dn->dn_objset->os_prefetch == ZFS_PREFETCH_NONE)
471 return (B_FALSE);
472
473 if (!spa_indirect_vdevs_loaded(spa))
474 return (B_FALSE);
475
476 uint64_t maxblkid = dn->dn_maxblkid;
477 unsigned int dbs = dn->dn_datablkshift;
478
479 if (blkid >= maxblkid)
480 return (B_FALSE);
481 if (end_blkid > maxblkid + 1)
482 end_blkid = maxblkid + 1;
483
484 mutex_enter(&zf->zf_lock);
485
486 /* Skip if a nearby stream already covers this range. */
487 uint_t max_near = zfetch_max_reorder >> dbs;
488 for (zs = list_head(&zf->zf_stream); zs != NULL;
489 zs = list_next(&zf->zf_stream, zs)) {
490 uint64_t diff = (blkid >= zs->zs_blkid) ?
491 (blkid - zs->zs_blkid) : (zs->zs_blkid - blkid);
492 if (diff <= max_near) {
493 mutex_exit(&zf->zf_lock);
494 return (B_FALSE);
495 }
496 }
497
498 dmu_zfetch_stream_create(zf, blkid);
499 zs = list_head(&zf->zf_stream);
500 ASSERT(zs != NULL);
501 ASSERT3U(zs->zs_blkid, ==, blkid);
502
503 /* dmu_zfetch_prepare() will double the distances, so take a half. */
504 unsigned int nbytes = ((end_blkid - blkid) << dbs) / 2;
505 zs->zs_pf_dist = MIN(nbytes, zfetch_min_distance);
506 zs->zs_ipf_dist = MIN(nbytes, zfetch_max_idistance);
507
508 mutex_exit(&zf->zf_lock);
509 return (B_TRUE);
510 }
511
512 /*
513 * This is the predictive prefetch entry point. dmu_zfetch_prepare()
514 * associates dnode access specified with blkid and nblks arguments with
515 * prefetch stream, predicts further accesses based on that stats and returns
516 * the stream pointer on success. That pointer must later be passed to
517 * dmu_zfetch_run() to initiate the speculative prefetch for the stream and
518 * release it. dmu_zfetch() is a wrapper for simple cases when window between
519 * prediction and prefetch initiation is not needed.
520 * fetch_data argument specifies whether actual data blocks should be fetched:
521 * FALSE -- prefetch only indirect blocks for predicted data blocks;
522 * TRUE -- prefetch predicted data blocks plus following indirect blocks.
523 */
524 zstream_t *
dmu_zfetch_prepare(zfetch_t * zf,uint64_t blkid,uint64_t nblks,boolean_t fetch_data,boolean_t have_lock)525 dmu_zfetch_prepare(zfetch_t *zf, uint64_t blkid, uint64_t nblks,
526 boolean_t fetch_data, boolean_t have_lock)
527 {
528 zstream_t *zs;
529 spa_t *spa = zf->zf_dnode->dn_objset->os_spa;
530 zfs_prefetch_type_t os_prefetch = zf->zf_dnode->dn_objset->os_prefetch;
531 int64_t ipf_start, ipf_end;
532
533 if (zfs_prefetch_disable || os_prefetch == ZFS_PREFETCH_NONE)
534 return (NULL);
535
536 if (os_prefetch == ZFS_PREFETCH_METADATA)
537 fetch_data = B_FALSE;
538
539 /*
540 * If we haven't yet loaded the indirect vdevs' mappings, we
541 * can only read from blocks that we carefully ensure are on
542 * concrete vdevs (or previously-loaded indirect vdevs). So we
543 * can't allow the predictive prefetcher to attempt reads of other
544 * blocks (e.g. of the MOS's dnode object).
545 */
546 if (!spa_indirect_vdevs_loaded(spa))
547 return (NULL);
548
549 /*
550 * As a fast path for small (single-block) files, ignore access
551 * to the first block, unless some streams exist, since a prime
552 * may be waiting.
553 */
554 if (!have_lock && blkid == 0 && zf->zf_numstreams == 0)
555 return (NULL);
556
557 if (!have_lock)
558 rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER);
559
560 /*
561 * A fast path for small files for which no prefetch will happen,
562 * unless streams exist, since a prime may be waiting.
563 */
564 uint64_t maxblkid = zf->zf_dnode->dn_maxblkid;
565 if (maxblkid < 2 && (maxblkid == 0 || zf->zf_numstreams == 0)) {
566 if (!have_lock)
567 rw_exit(&zf->zf_dnode->dn_struct_rwlock);
568 return (NULL);
569 }
570 mutex_enter(&zf->zf_lock);
571
572 /*
573 * Find perfect prefetch stream. Depending on whether the accesses
574 * are block-aligned, first block of the new access may either follow
575 * the last block of the previous access, or be equal to it.
576 */
577 unsigned int dbs = zf->zf_dnode->dn_datablkshift;
578 uint64_t end_blkid = blkid + nblks;
579 for (zs = list_head(&zf->zf_stream); zs != NULL;
580 zs = list_next(&zf->zf_stream, zs)) {
581 if (blkid == zs->zs_blkid) {
582 goto hit;
583 } else if (blkid + 1 == zs->zs_blkid) {
584 blkid++;
585 nblks--;
586 goto hit;
587 }
588 }
589
590 /*
591 * Find close enough prefetch stream. Access crossing stream position
592 * is a hit in its new part. Access ahead of stream position considered
593 * a hit for metadata prefetch, since we do not care about fill percent,
594 * or stored for future otherwise. Access behind stream position is
595 * silently ignored, since we already skipped it reaching fill percent.
596 */
597 uint_t max_reorder = MIN((zfetch_max_reorder >> dbs) + 1, UINT16_MAX);
598 uint_t t = gethrestime_sec() - zfetch_max_sec_reap;
599 for (zs = list_head(&zf->zf_stream); zs != NULL;
600 zs = list_next(&zf->zf_stream, zs)) {
601 if (blkid > zs->zs_blkid) {
602 if (end_blkid <= zs->zs_blkid + max_reorder) {
603 if (!fetch_data) {
604 nblks = dmu_zfetch_hit(zs,
605 end_blkid - zs->zs_blkid);
606 ZFETCHSTAT_BUMP(zfetchstat_stride);
607 goto future;
608 }
609 nblks = dmu_zfetch_future(zs, blkid, nblks);
610 if (nblks > 0)
611 ZFETCHSTAT_BUMP(zfetchstat_stride);
612 else
613 ZFETCHSTAT_BUMP(zfetchstat_future);
614 goto future;
615 }
616 } else if (end_blkid >= zs->zs_blkid) {
617 nblks -= zs->zs_blkid - blkid;
618 blkid += zs->zs_blkid - blkid;
619 goto hit;
620 } else if (end_blkid + max_reorder > zs->zs_blkid &&
621 (int)(zs->zs_atime - t) >= 0) {
622 ZFETCHSTAT_BUMP(zfetchstat_past);
623 zs->zs_atime = gethrestime_sec();
624 goto out;
625 }
626 }
627
628 /*
629 * This access is not part of any existing stream. Create a new
630 * stream for it unless we are at the end of file.
631 */
632 ASSERT0P(zs);
633 if (end_blkid < maxblkid)
634 dmu_zfetch_stream_create(zf, end_blkid);
635 mutex_exit(&zf->zf_lock);
636 ZFETCHSTAT_BUMP(zfetchstat_misses);
637 ipf_start = 0;
638 goto prescient;
639
640 hit:
641 nblks = dmu_zfetch_hit(zs, nblks);
642 ZFETCHSTAT_BUMP(zfetchstat_hits);
643
644 future:
645 zs->zs_atime = gethrestime_sec();
646
647 /* Exit if we already prefetched for this position before. */
648 if (nblks == 0 && zs->zs_ipf_end > end_blkid)
649 goto out;
650
651 /* If the file is ending, remove the stream. */
652 end_blkid = zs->zs_blkid;
653 if (end_blkid >= maxblkid) {
654 dmu_zfetch_stream_remove(zf, zs);
655 out:
656 mutex_exit(&zf->zf_lock);
657 if (!have_lock)
658 rw_exit(&zf->zf_dnode->dn_struct_rwlock);
659 return (NULL);
660 }
661
662 /*
663 * This access was to a block that we issued a prefetch for on
664 * behalf of this stream. Calculate further prefetch distances.
665 *
666 * Start prefetch from the demand access size (nblks). Double the
667 * distance every access up to zfetch_min_distance. After that only
668 * if needed increase the distance by 1/8 up to zfetch_max_distance.
669 *
670 * Don't double the distance beyond single block if we have more
671 * than ~6% of ARC held by active prefetches. It should help with
672 * getting out of RAM on some badly mispredicted read patterns.
673 */
674 unsigned int nbytes = nblks << dbs;
675 unsigned int pf_nblks;
676 if (fetch_data) {
677 if (unlikely(zs->zs_pf_dist < nbytes))
678 zs->zs_pf_dist = nbytes;
679 else if (zs->zs_pf_dist < zfetch_min_distance &&
680 (zs->zs_pf_dist < (1 << dbs) ||
681 aggsum_compare(&zfetch_sums.zfetchstat_io_active,
682 arc_c_max >> (4 + dbs)) < 0))
683 zs->zs_pf_dist *= 2;
684 else if (zs->zs_more)
685 zs->zs_pf_dist += zs->zs_pf_dist / 8;
686 zs->zs_more = B_FALSE;
687 if (zs->zs_pf_dist > zfetch_max_distance)
688 zs->zs_pf_dist = zfetch_max_distance;
689 pf_nblks = zs->zs_pf_dist >> dbs;
690 } else {
691 pf_nblks = 0;
692 }
693 if (zs->zs_pf_start < end_blkid)
694 zs->zs_pf_start = end_blkid;
695 if (zs->zs_pf_end < end_blkid + pf_nblks)
696 zs->zs_pf_end = end_blkid + pf_nblks;
697
698 /*
699 * Do the same for indirects, starting where we will stop reading
700 * data blocks (and the indirects that point to them).
701 */
702 nbytes = MAX(nbytes, (1 << dbs));
703 if (unlikely(zs->zs_ipf_dist < nbytes))
704 zs->zs_ipf_dist = nbytes;
705 else
706 zs->zs_ipf_dist *= 2;
707 if (zs->zs_ipf_dist > zfetch_max_idistance)
708 zs->zs_ipf_dist = zfetch_max_idistance;
709 pf_nblks = zs->zs_ipf_dist >> dbs;
710 if (zs->zs_ipf_start < zs->zs_pf_end)
711 zs->zs_ipf_start = zs->zs_pf_end;
712 ipf_start = zs->zs_ipf_end;
713 if (zs->zs_ipf_end < zs->zs_pf_end + pf_nblks)
714 zs->zs_ipf_end = zs->zs_pf_end + pf_nblks;
715
716 zfs_refcount_add(&zs->zs_refs, NULL);
717 /* Count concurrent callers. */
718 zfs_refcount_add(&zs->zs_callers, NULL);
719 mutex_exit(&zf->zf_lock);
720
721 prescient:
722 /*
723 * Prefetch the following indirect blocks for this access to reduce
724 * dbuf_hold() sync read delays in dmu_buf_hold_array_by_dnode().
725 * This covers the gap during the first couple accesses when we can
726 * not predict the future yet, but know what is needed right now.
727 * This should be very rare for reads/writes to need more than one
728 * indirect, but more useful for cloning due to much bigger accesses.
729 */
730 ipf_start = MAX(ipf_start, blkid + 1);
731 int epbs = zf->zf_dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
732 ipf_start = P2ROUNDUP(ipf_start, 1 << epbs) >> epbs;
733 ipf_end = P2ROUNDUP(end_blkid, 1 << epbs) >> epbs;
734
735 int issued = 0;
736 for (int64_t iblk = ipf_start; iblk < ipf_end; iblk++) {
737 issued += dbuf_prefetch(zf->zf_dnode, 1, iblk,
738 ZIO_PRIORITY_SYNC_READ, ARC_FLAG_PRESCIENT_PREFETCH);
739 }
740
741 if (!have_lock)
742 rw_exit(&zf->zf_dnode->dn_struct_rwlock);
743 if (issued)
744 ZFETCHSTAT_ADD(zfetchstat_io_issued, issued);
745 return (zs);
746 }
747
748 void
dmu_zfetch_run(zfetch_t * zf,zstream_t * zs,boolean_t missed,boolean_t have_lock,boolean_t uncached)749 dmu_zfetch_run(zfetch_t *zf, zstream_t *zs, boolean_t missed,
750 boolean_t have_lock, boolean_t uncached)
751 {
752 int64_t pf_start, pf_end, ipf_start, ipf_end;
753 int epbs, issued;
754
755 if (missed)
756 zs->zs_missed = missed;
757
758 /*
759 * Postpone the prefetch if there are more concurrent callers.
760 * It happens when multiple requests are waiting for the same
761 * indirect block. The last one will run the prefetch for all.
762 */
763 if (zfs_refcount_remove(&zs->zs_callers, NULL) != 0) {
764 /* Drop reference taken in dmu_zfetch_prepare(). */
765 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)
766 dmu_zfetch_stream_fini(zs);
767 return;
768 }
769
770 mutex_enter(&zf->zf_lock);
771 if (zs->zs_missed) {
772 pf_start = zs->zs_pf_start;
773 pf_end = zs->zs_pf_start = zs->zs_pf_end;
774 } else {
775 pf_start = pf_end = 0;
776 }
777 ipf_start = zs->zs_ipf_start;
778 ipf_end = zs->zs_ipf_start = zs->zs_ipf_end;
779 mutex_exit(&zf->zf_lock);
780 ASSERT3S(pf_start, <=, pf_end);
781 ASSERT3S(ipf_start, <=, ipf_end);
782
783 epbs = zf->zf_dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
784 ipf_start = P2ROUNDUP(ipf_start, 1 << epbs) >> epbs;
785 ipf_end = P2ROUNDUP(ipf_end, 1 << epbs) >> epbs;
786 ASSERT3S(ipf_start, <=, ipf_end);
787 issued = pf_end - pf_start + ipf_end - ipf_start;
788 if (issued > 1) {
789 /* More references on top of taken in dmu_zfetch_prepare(). */
790 zfs_refcount_add_few(&zs->zs_refs, issued - 1, NULL);
791 } else if (issued == 0) {
792 /* Some other thread has done our work, so drop the ref. */
793 if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)
794 dmu_zfetch_stream_fini(zs);
795 return;
796 }
797 aggsum_add(&zfetch_sums.zfetchstat_io_active, issued);
798
799 if (!have_lock)
800 rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER);
801
802 issued = 0;
803 for (int64_t blk = pf_start; blk < pf_end; blk++) {
804 issued += dbuf_prefetch_impl(zf->zf_dnode, 0, blk,
805 ZIO_PRIORITY_ASYNC_READ, uncached ?
806 ARC_FLAG_UNCACHED : 0, dmu_zfetch_done, zs);
807 }
808 for (int64_t iblk = ipf_start; iblk < ipf_end; iblk++) {
809 issued += dbuf_prefetch_impl(zf->zf_dnode, 1, iblk,
810 ZIO_PRIORITY_ASYNC_READ, 0, dmu_zfetch_done, zs);
811 }
812
813 if (!have_lock)
814 rw_exit(&zf->zf_dnode->dn_struct_rwlock);
815
816 if (issued)
817 ZFETCHSTAT_ADD(zfetchstat_io_issued, issued);
818 }
819
820 void
dmu_zfetch(zfetch_t * zf,uint64_t blkid,uint64_t nblks,boolean_t fetch_data,boolean_t missed,boolean_t have_lock,boolean_t uncached)821 dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks, boolean_t fetch_data,
822 boolean_t missed, boolean_t have_lock, boolean_t uncached)
823 {
824 zstream_t *zs;
825
826 zs = dmu_zfetch_prepare(zf, blkid, nblks, fetch_data, have_lock);
827 if (zs)
828 dmu_zfetch_run(zf, zs, missed, have_lock, uncached);
829 }
830
831 ZFS_MODULE_PARAM(zfs_prefetch, zfs_prefetch_, disable, INT, ZMOD_RW,
832 "Disable all ZFS prefetching");
833
834 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_streams, UINT, ZMOD_RW,
835 "Max number of streams per zfetch");
836
837 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, min_sec_reap, UINT, ZMOD_RW,
838 "Min time before stream reclaim");
839
840 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_sec_reap, UINT, ZMOD_RW,
841 "Max time before stream delete");
842
843 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, min_distance, UINT, ZMOD_RW,
844 "Min bytes to prefetch per stream");
845
846 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_distance, UINT, ZMOD_RW,
847 "Max bytes to prefetch per stream");
848
849 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_idistance, UINT, ZMOD_RW,
850 "Max bytes to prefetch indirects for per stream");
851
852 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_reorder, UINT, ZMOD_RW,
853 "Max request reorder distance within a stream");
854
855 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, hole_shift, UINT, ZMOD_RW,
856 "Max log2 fraction of holes in a stream");
857