xref: /freebsd/sys/contrib/openzfs/module/zfs/dmu_zfetch.c (revision 6132212808e8dccedc9e5d85fea4390c2f38059a)
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 } zfetch_stats_t;
63 
64 static zfetch_stats_t zfetch_stats = {
65 	{ "hits",			KSTAT_DATA_UINT64 },
66 	{ "misses",			KSTAT_DATA_UINT64 },
67 	{ "max_streams",		KSTAT_DATA_UINT64 },
68 };
69 
70 #define	ZFETCHSTAT_BUMP(stat) \
71 	atomic_inc_64(&zfetch_stats.stat.value.ui64);
72 
73 kstat_t		*zfetch_ksp;
74 
75 void
76 zfetch_init(void)
77 {
78 	zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
79 	    KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
80 	    KSTAT_FLAG_VIRTUAL);
81 
82 	if (zfetch_ksp != NULL) {
83 		zfetch_ksp->ks_data = &zfetch_stats;
84 		kstat_install(zfetch_ksp);
85 	}
86 }
87 
88 void
89 zfetch_fini(void)
90 {
91 	if (zfetch_ksp != NULL) {
92 		kstat_delete(zfetch_ksp);
93 		zfetch_ksp = NULL;
94 	}
95 }
96 
97 /*
98  * This takes a pointer to a zfetch structure and a dnode.  It performs the
99  * necessary setup for the zfetch structure, grokking data from the
100  * associated dnode.
101  */
102 void
103 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
104 {
105 	if (zf == NULL)
106 		return;
107 
108 	zf->zf_dnode = dno;
109 
110 	list_create(&zf->zf_stream, sizeof (zstream_t),
111 	    offsetof(zstream_t, zs_node));
112 
113 	mutex_init(&zf->zf_lock, NULL, MUTEX_DEFAULT, NULL);
114 }
115 
116 static void
117 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
118 {
119 	ASSERT(MUTEX_HELD(&zf->zf_lock));
120 	list_remove(&zf->zf_stream, zs);
121 	mutex_destroy(&zs->zs_lock);
122 	kmem_free(zs, sizeof (*zs));
123 }
124 
125 /*
126  * Clean-up state associated with a zfetch structure (e.g. destroy the
127  * streams).  This doesn't free the zfetch_t itself, that's left to the caller.
128  */
129 void
130 dmu_zfetch_fini(zfetch_t *zf)
131 {
132 	zstream_t *zs;
133 
134 	mutex_enter(&zf->zf_lock);
135 	while ((zs = list_head(&zf->zf_stream)) != NULL)
136 		dmu_zfetch_stream_remove(zf, zs);
137 	mutex_exit(&zf->zf_lock);
138 	list_destroy(&zf->zf_stream);
139 	mutex_destroy(&zf->zf_lock);
140 
141 	zf->zf_dnode = NULL;
142 }
143 
144 /*
145  * If there aren't too many streams already, create a new stream.
146  * The "blkid" argument is the next block that we expect this stream to access.
147  * While we're here, clean up old streams (which haven't been
148  * accessed for at least zfetch_min_sec_reap seconds).
149  */
150 static void
151 dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)
152 {
153 	zstream_t *zs_next;
154 	int numstreams = 0;
155 
156 	ASSERT(MUTEX_HELD(&zf->zf_lock));
157 
158 	/*
159 	 * Clean up old streams.
160 	 */
161 	for (zstream_t *zs = list_head(&zf->zf_stream);
162 	    zs != NULL; zs = zs_next) {
163 		zs_next = list_next(&zf->zf_stream, zs);
164 		if (((gethrtime() - zs->zs_atime) / NANOSEC) >
165 		    zfetch_min_sec_reap)
166 			dmu_zfetch_stream_remove(zf, zs);
167 		else
168 			numstreams++;
169 	}
170 
171 	/*
172 	 * The maximum number of streams is normally zfetch_max_streams,
173 	 * but for small files we lower it such that it's at least possible
174 	 * for all the streams to be non-overlapping.
175 	 *
176 	 * If we are already at the maximum number of streams for this file,
177 	 * even after removing old streams, then don't create this stream.
178 	 */
179 	uint32_t max_streams = MAX(1, MIN(zfetch_max_streams,
180 	    zf->zf_dnode->dn_maxblkid * zf->zf_dnode->dn_datablksz /
181 	    zfetch_max_distance));
182 	if (numstreams >= max_streams) {
183 		ZFETCHSTAT_BUMP(zfetchstat_max_streams);
184 		return;
185 	}
186 
187 	zstream_t *zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);
188 	zs->zs_blkid = blkid;
189 	zs->zs_pf_blkid = blkid;
190 	zs->zs_ipf_blkid = blkid;
191 	zs->zs_atime = gethrtime();
192 	mutex_init(&zs->zs_lock, NULL, MUTEX_DEFAULT, NULL);
193 
194 	list_insert_head(&zf->zf_stream, zs);
195 }
196 
197 /*
198  * This is the predictive prefetch entry point.  It associates dnode access
199  * specified with blkid and nblks arguments with prefetch stream, predicts
200  * further accesses based on that stats and initiates speculative prefetch.
201  * fetch_data argument specifies whether actual data blocks should be fetched:
202  *   FALSE -- prefetch only indirect blocks for predicted data blocks;
203  *   TRUE -- prefetch predicted data blocks plus following indirect blocks.
204  */
205 void
206 dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks, boolean_t fetch_data,
207     boolean_t have_lock)
208 {
209 	zstream_t *zs;
210 	int64_t pf_start, ipf_start, ipf_istart, ipf_iend;
211 	int64_t pf_ahead_blks, max_blks;
212 	int epbs, max_dist_blks, pf_nblks, ipf_nblks;
213 	uint64_t end_of_access_blkid;
214 	end_of_access_blkid = blkid + nblks;
215 	spa_t *spa = zf->zf_dnode->dn_objset->os_spa;
216 
217 	if (zfs_prefetch_disable)
218 		return;
219 	/*
220 	 * If we haven't yet loaded the indirect vdevs' mappings, we
221 	 * can only read from blocks that we carefully ensure are on
222 	 * concrete vdevs (or previously-loaded indirect vdevs).  So we
223 	 * can't allow the predictive prefetcher to attempt reads of other
224 	 * blocks (e.g. of the MOS's dnode object).
225 	 */
226 	if (!spa_indirect_vdevs_loaded(spa))
227 		return;
228 
229 	/*
230 	 * As a fast path for small (single-block) files, ignore access
231 	 * to the first block.
232 	 */
233 	if (blkid == 0)
234 		return;
235 
236 	if (!have_lock)
237 		rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER);
238 	mutex_enter(&zf->zf_lock);
239 
240 	/*
241 	 * Find matching prefetch stream.  Depending on whether the accesses
242 	 * are block-aligned, first block of the new access may either follow
243 	 * the last block of the previous access, or be equal to it.
244 	 */
245 	for (zs = list_head(&zf->zf_stream); zs != NULL;
246 	    zs = list_next(&zf->zf_stream, zs)) {
247 		if (blkid == zs->zs_blkid || blkid + 1 == zs->zs_blkid) {
248 			mutex_enter(&zs->zs_lock);
249 			/*
250 			 * zs_blkid could have changed before we
251 			 * acquired zs_lock; re-check them here.
252 			 */
253 			if (blkid == zs->zs_blkid) {
254 				break;
255 			} else if (blkid + 1 == zs->zs_blkid) {
256 				blkid++;
257 				nblks--;
258 				if (nblks == 0) {
259 					/* Already prefetched this before. */
260 					mutex_exit(&zs->zs_lock);
261 					mutex_exit(&zf->zf_lock);
262 					if (!have_lock) {
263 						rw_exit(&zf->zf_dnode->
264 						    dn_struct_rwlock);
265 					}
266 					return;
267 				}
268 				break;
269 			}
270 			mutex_exit(&zs->zs_lock);
271 		}
272 	}
273 
274 	if (zs == NULL) {
275 		/*
276 		 * This access is not part of any existing stream.  Create
277 		 * a new stream for it.
278 		 */
279 		ZFETCHSTAT_BUMP(zfetchstat_misses);
280 
281 		dmu_zfetch_stream_create(zf, end_of_access_blkid);
282 		mutex_exit(&zf->zf_lock);
283 		if (!have_lock)
284 			rw_exit(&zf->zf_dnode->dn_struct_rwlock);
285 		return;
286 	}
287 
288 	/*
289 	 * This access was to a block that we issued a prefetch for on
290 	 * behalf of this stream. Issue further prefetches for this stream.
291 	 *
292 	 * Normally, we start prefetching where we stopped
293 	 * prefetching last (zs_pf_blkid).  But when we get our first
294 	 * hit on this stream, zs_pf_blkid == zs_blkid, we don't
295 	 * want to prefetch the block we just accessed.  In this case,
296 	 * start just after the block we just accessed.
297 	 */
298 	pf_start = MAX(zs->zs_pf_blkid, end_of_access_blkid);
299 
300 	/*
301 	 * Double our amount of prefetched data, but don't let the
302 	 * prefetch get further ahead than zfetch_max_distance.
303 	 */
304 	if (fetch_data) {
305 		max_dist_blks =
306 		    zfetch_max_distance >> zf->zf_dnode->dn_datablkshift;
307 		/*
308 		 * Previously, we were (zs_pf_blkid - blkid) ahead.  We
309 		 * want to now be double that, so read that amount again,
310 		 * plus the amount we are catching up by (i.e. the amount
311 		 * read just now).
312 		 */
313 		pf_ahead_blks = zs->zs_pf_blkid - blkid + nblks;
314 		max_blks = max_dist_blks - (pf_start - end_of_access_blkid);
315 		pf_nblks = MIN(pf_ahead_blks, max_blks);
316 	} else {
317 		pf_nblks = 0;
318 	}
319 
320 	zs->zs_pf_blkid = pf_start + pf_nblks;
321 
322 	/*
323 	 * Do the same for indirects, starting from where we stopped last,
324 	 * or where we will stop reading data blocks (and the indirects
325 	 * that point to them).
326 	 */
327 	ipf_start = MAX(zs->zs_ipf_blkid, zs->zs_pf_blkid);
328 	max_dist_blks = zfetch_max_idistance >> zf->zf_dnode->dn_datablkshift;
329 	/*
330 	 * We want to double our distance ahead of the data prefetch
331 	 * (or reader, if we are not prefetching data).  Previously, we
332 	 * were (zs_ipf_blkid - blkid) ahead.  To double that, we read
333 	 * that amount again, plus the amount we are catching up by
334 	 * (i.e. the amount read now + the amount of data prefetched now).
335 	 */
336 	pf_ahead_blks = zs->zs_ipf_blkid - blkid + nblks + pf_nblks;
337 	max_blks = max_dist_blks - (ipf_start - end_of_access_blkid);
338 	ipf_nblks = MIN(pf_ahead_blks, max_blks);
339 	zs->zs_ipf_blkid = ipf_start + ipf_nblks;
340 
341 	epbs = zf->zf_dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
342 	ipf_istart = P2ROUNDUP(ipf_start, 1 << epbs) >> epbs;
343 	ipf_iend = P2ROUNDUP(zs->zs_ipf_blkid, 1 << epbs) >> epbs;
344 
345 	zs->zs_atime = gethrtime();
346 	zs->zs_blkid = end_of_access_blkid;
347 	mutex_exit(&zs->zs_lock);
348 	mutex_exit(&zf->zf_lock);
349 
350 	/*
351 	 * dbuf_prefetch() is asynchronous (even when it needs to read
352 	 * indirect blocks), but we still prefer to drop our locks before
353 	 * calling it to reduce the time we hold them.
354 	 */
355 
356 	for (int i = 0; i < pf_nblks; i++) {
357 		dbuf_prefetch(zf->zf_dnode, 0, pf_start + i,
358 		    ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH);
359 	}
360 	for (int64_t iblk = ipf_istart; iblk < ipf_iend; iblk++) {
361 		dbuf_prefetch(zf->zf_dnode, 1, iblk,
362 		    ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH);
363 	}
364 	if (!have_lock)
365 		rw_exit(&zf->zf_dnode->dn_struct_rwlock);
366 	ZFETCHSTAT_BUMP(zfetchstat_hits);
367 }
368 
369 /* BEGIN CSTYLED */
370 ZFS_MODULE_PARAM(zfs_prefetch, zfs_prefetch_, disable, INT, ZMOD_RW,
371 	"Disable all ZFS prefetching");
372 
373 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_streams, UINT, ZMOD_RW,
374 	"Max number of streams per zfetch");
375 
376 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, min_sec_reap, UINT, ZMOD_RW,
377 	"Min time before stream reclaim");
378 
379 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_distance, UINT, ZMOD_RW,
380 	"Max bytes to prefetch per stream (default 8MB)");
381 
382 ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, array_rd_sz, ULONG, ZMOD_RW,
383 	"Number of bytes in a array_read");
384 /* END CSTYLED */
385