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