xref: /NextBSD/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu_zfetch.c (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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, 2015 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 number of bytes in an array_read in which we allow prefetching (1MB) */
53 uint64_t	zfetch_array_rd_sz = 1024 * 1024;
54 
55 SYSCTL_DECL(_vfs_zfs);
56 SYSCTL_INT(_vfs_zfs, OID_AUTO, prefetch_disable, CTLFLAG_RW,
57     &zfs_prefetch_disable, 0, "Disable prefetch");
58 SYSCTL_NODE(_vfs_zfs, OID_AUTO, zfetch, CTLFLAG_RW, 0, "ZFS ZFETCH");
59 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, max_streams, CTLFLAG_RWTUN,
60     &zfetch_max_streams, 0, "Max # of streams per zfetch");
61 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, min_sec_reap, CTLFLAG_RWTUN,
62     &zfetch_min_sec_reap, 0, "Min time before stream reclaim");
63 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, max_distance, CTLFLAG_RWTUN,
64     &zfetch_max_distance, 0, "Max bytes to prefetch per stream");
65 SYSCTL_UQUAD(_vfs_zfs_zfetch, OID_AUTO, array_rd_sz, CTLFLAG_RWTUN,
66     &zfetch_array_rd_sz, 0,
67     "Number of bytes in a array_read at which we stop prefetching");
68 
69 typedef struct zfetch_stats {
70 	kstat_named_t zfetchstat_hits;
71 	kstat_named_t zfetchstat_misses;
72 	kstat_named_t zfetchstat_max_streams;
73 } zfetch_stats_t;
74 
75 static zfetch_stats_t zfetch_stats = {
76 	{ "hits",			KSTAT_DATA_UINT64 },
77 	{ "misses",			KSTAT_DATA_UINT64 },
78 	{ "max_streams",		KSTAT_DATA_UINT64 },
79 };
80 
81 #define	ZFETCHSTAT_BUMP(stat) \
82 	atomic_inc_64(&zfetch_stats.stat.value.ui64);
83 
84 kstat_t		*zfetch_ksp;
85 
86 void
zfetch_init(void)87 zfetch_init(void)
88 {
89 	zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
90 	    KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
91 	    KSTAT_FLAG_VIRTUAL);
92 
93 	if (zfetch_ksp != NULL) {
94 		zfetch_ksp->ks_data = &zfetch_stats;
95 		kstat_install(zfetch_ksp);
96 	}
97 }
98 
99 void
zfetch_fini(void)100 zfetch_fini(void)
101 {
102 	if (zfetch_ksp != NULL) {
103 		kstat_delete(zfetch_ksp);
104 		zfetch_ksp = NULL;
105 	}
106 }
107 
108 /*
109  * This takes a pointer to a zfetch structure and a dnode.  It performs the
110  * necessary setup for the zfetch structure, grokking data from the
111  * associated dnode.
112  */
113 void
dmu_zfetch_init(zfetch_t * zf,dnode_t * dno)114 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
115 {
116 	if (zf == NULL)
117 		return;
118 
119 	zf->zf_dnode = dno;
120 
121 	list_create(&zf->zf_stream, sizeof (zstream_t),
122 	    offsetof(zstream_t, zs_node));
123 
124 	rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL);
125 }
126 
127 static void
dmu_zfetch_stream_remove(zfetch_t * zf,zstream_t * zs)128 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
129 {
130 	ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
131 	list_remove(&zf->zf_stream, zs);
132 	mutex_destroy(&zs->zs_lock);
133 	kmem_free(zs, sizeof (*zs));
134 }
135 
136 /*
137  * Clean-up state associated with a zfetch structure (e.g. destroy the
138  * streams).  This doesn't free the zfetch_t itself, that's left to the caller.
139  */
140 void
dmu_zfetch_fini(zfetch_t * zf)141 dmu_zfetch_fini(zfetch_t *zf)
142 {
143 	zstream_t *zs;
144 
145 	ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
146 
147 	rw_enter(&zf->zf_rwlock, RW_WRITER);
148 	while ((zs = list_head(&zf->zf_stream)) != NULL)
149 		dmu_zfetch_stream_remove(zf, zs);
150 	rw_exit(&zf->zf_rwlock);
151 	list_destroy(&zf->zf_stream);
152 	rw_destroy(&zf->zf_rwlock);
153 
154 	zf->zf_dnode = NULL;
155 }
156 
157 /*
158  * If there aren't too many streams already, create a new stream.
159  * The "blkid" argument is the next block that we expect this stream to access.
160  * While we're here, clean up old streams (which haven't been
161  * accessed for at least zfetch_min_sec_reap seconds).
162  */
163 static void
dmu_zfetch_stream_create(zfetch_t * zf,uint64_t blkid)164 dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)
165 {
166 	zstream_t *zs_next;
167 	int numstreams = 0;
168 
169 	ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
170 
171 	/*
172 	 * Clean up old streams.
173 	 */
174 	for (zstream_t *zs = list_head(&zf->zf_stream);
175 	    zs != NULL; zs = zs_next) {
176 		zs_next = list_next(&zf->zf_stream, zs);
177 		if (((gethrtime() - zs->zs_atime) / NANOSEC) >
178 		    zfetch_min_sec_reap)
179 			dmu_zfetch_stream_remove(zf, zs);
180 		else
181 			numstreams++;
182 	}
183 
184 	/*
185 	 * The maximum number of streams is normally zfetch_max_streams,
186 	 * but for small files we lower it such that it's at least possible
187 	 * for all the streams to be non-overlapping.
188 	 *
189 	 * If we are already at the maximum number of streams for this file,
190 	 * even after removing old streams, then don't create this stream.
191 	 */
192 	uint32_t max_streams = MAX(1, MIN(zfetch_max_streams,
193 	    zf->zf_dnode->dn_maxblkid * zf->zf_dnode->dn_datablksz /
194 	    zfetch_max_distance));
195 	if (numstreams >= max_streams) {
196 		ZFETCHSTAT_BUMP(zfetchstat_max_streams);
197 		return;
198 	}
199 
200 	zstream_t *zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);
201 	zs->zs_blkid = blkid;
202 	zs->zs_pf_blkid = blkid;
203 	zs->zs_atime = gethrtime();
204 	mutex_init(&zs->zs_lock, NULL, MUTEX_DEFAULT, NULL);
205 
206 	list_insert_head(&zf->zf_stream, zs);
207 }
208 
209 /*
210  * This is the prefetch entry point.  It calls all of the other dmu_zfetch
211  * routines to create, delete, find, or operate upon prefetch streams.
212  */
213 void
dmu_zfetch(zfetch_t * zf,uint64_t blkid,uint64_t nblks)214 dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks)
215 {
216 	zstream_t *zs;
217 
218 	if (zfs_prefetch_disable)
219 		return;
220 
221 	/*
222 	 * As a fast path for small (single-block) files, ignore access
223 	 * to the first block.
224 	 */
225 	if (blkid == 0)
226 		return;
227 
228 	rw_enter(&zf->zf_rwlock, RW_READER);
229 
230 	for (zs = list_head(&zf->zf_stream); zs != NULL;
231 	    zs = list_next(&zf->zf_stream, zs)) {
232 		if (blkid == zs->zs_blkid) {
233 			mutex_enter(&zs->zs_lock);
234 			/*
235 			 * zs_blkid could have changed before we
236 			 * acquired zs_lock; re-check them here.
237 			 */
238 			if (blkid != zs->zs_blkid) {
239 				mutex_exit(&zs->zs_lock);
240 				continue;
241 			}
242 			break;
243 		}
244 	}
245 
246 	if (zs == NULL) {
247 		/*
248 		 * This access is not part of any existing stream.  Create
249 		 * a new stream for it.
250 		 */
251 		ZFETCHSTAT_BUMP(zfetchstat_misses);
252 		if (rw_tryupgrade(&zf->zf_rwlock))
253 			dmu_zfetch_stream_create(zf, blkid + nblks);
254 		rw_exit(&zf->zf_rwlock);
255 		return;
256 	}
257 
258 	/*
259 	 * This access was to a block that we issued a prefetch for on
260 	 * behalf of this stream. Issue further prefetches for this stream.
261 	 *
262 	 * Normally, we start prefetching where we stopped
263 	 * prefetching last (zs_pf_blkid).  But when we get our first
264 	 * hit on this stream, zs_pf_blkid == zs_blkid, we don't
265 	 * want to prefetch to block we just accessed.  In this case,
266 	 * start just after the block we just accessed.
267 	 */
268 	int64_t pf_start = MAX(zs->zs_pf_blkid, blkid + nblks);
269 
270 	/*
271 	 * Double our amount of prefetched data, but don't let the
272 	 * prefetch get further ahead than zfetch_max_distance.
273 	 */
274 	int pf_nblks =
275 	    MIN((int64_t)zs->zs_pf_blkid - zs->zs_blkid + nblks,
276 	    zs->zs_blkid + nblks +
277 	    (zfetch_max_distance >> zf->zf_dnode->dn_datablkshift) - pf_start);
278 
279 	zs->zs_pf_blkid = pf_start + pf_nblks;
280 	zs->zs_atime = gethrtime();
281 	zs->zs_blkid = blkid + nblks;
282 
283 	/*
284 	 * dbuf_prefetch() issues the prefetch i/o
285 	 * asynchronously, but it may need to wait for an
286 	 * indirect block to be read from disk.  Therefore
287 	 * we do not want to hold any locks while we call it.
288 	 */
289 	mutex_exit(&zs->zs_lock);
290 	rw_exit(&zf->zf_rwlock);
291 	for (int i = 0; i < pf_nblks; i++) {
292 		dbuf_prefetch(zf->zf_dnode, 0, pf_start + i,
293 		    ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH);
294 	}
295 	ZFETCHSTAT_BUMP(zfetchstat_hits);
296 }
297