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