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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
30 #include <sys/zfs_context.h>
31 #include <sys/dnode.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_zfetch.h>
36 #include <sys/kstat.h>
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.
44 boolean_t zfs_prefetch_disable = B_FALSE;
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;
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 TUNABLE_INT("vfs.zfs.zfetch.max_streams", &zfetch_max_streams);
60 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, max_streams, CTLFLAG_RW,
61 &zfetch_max_streams, 0, "Max # of streams per zfetch");
62 TUNABLE_INT("vfs.zfs.zfetch.min_sec_reap", &zfetch_min_sec_reap);
63 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, min_sec_reap, CTLFLAG_RWTUN,
64 &zfetch_min_sec_reap, 0, "Min time before stream reclaim");
65 TUNABLE_INT("vfs.zfs.zfetch.max_distance", &zfetch_max_distance);
66 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, max_distance, CTLFLAG_RWTUN,
67 &zfetch_max_distance, 0, "Max bytes to prefetch per stream");
68 TUNABLE_QUAD("vfs.zfs.zfetch.array_rd_sz", &zfetch_array_rd_sz);
69 SYSCTL_UQUAD(_vfs_zfs_zfetch, OID_AUTO, array_rd_sz, CTLFLAG_RWTUN,
70 &zfetch_array_rd_sz, 0,
71 "Number of bytes in a array_read at which we stop prefetching");
73 typedef struct zfetch_stats {
74 kstat_named_t zfetchstat_hits;
75 kstat_named_t zfetchstat_misses;
76 kstat_named_t zfetchstat_max_streams;
79 static zfetch_stats_t zfetch_stats = {
80 { "hits", KSTAT_DATA_UINT64 },
81 { "misses", KSTAT_DATA_UINT64 },
82 { "max_streams", KSTAT_DATA_UINT64 },
85 #define ZFETCHSTAT_BUMP(stat) \
86 atomic_inc_64(&zfetch_stats.stat.value.ui64);
93 zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
94 KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
97 if (zfetch_ksp != NULL) {
98 zfetch_ksp->ks_data = &zfetch_stats;
99 kstat_install(zfetch_ksp);
106 if (zfetch_ksp != NULL) {
107 kstat_delete(zfetch_ksp);
113 * This takes a pointer to a zfetch structure and a dnode. It performs the
114 * necessary setup for the zfetch structure, grokking data from the
118 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
125 list_create(&zf->zf_stream, sizeof (zstream_t),
126 offsetof(zstream_t, zs_node));
128 rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL);
132 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
134 ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
135 list_remove(&zf->zf_stream, zs);
136 mutex_destroy(&zs->zs_lock);
137 kmem_free(zs, sizeof (*zs));
141 * Clean-up state associated with a zfetch structure (e.g. destroy the
142 * streams). This doesn't free the zfetch_t itself, that's left to the caller.
145 dmu_zfetch_fini(zfetch_t *zf)
149 ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
151 rw_enter(&zf->zf_rwlock, RW_WRITER);
152 while ((zs = list_head(&zf->zf_stream)) != NULL)
153 dmu_zfetch_stream_remove(zf, zs);
154 rw_exit(&zf->zf_rwlock);
155 list_destroy(&zf->zf_stream);
156 rw_destroy(&zf->zf_rwlock);
162 * If there aren't too many streams already, create a new stream.
163 * The "blkid" argument is the next block that we expect this stream to access.
164 * While we're here, clean up old streams (which haven't been
165 * accessed for at least zfetch_min_sec_reap seconds).
168 dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)
173 ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
176 * Clean up old streams.
178 for (zstream_t *zs = list_head(&zf->zf_stream);
179 zs != NULL; zs = zs_next) {
180 zs_next = list_next(&zf->zf_stream, zs);
181 if (((gethrtime() - zs->zs_atime) / NANOSEC) >
183 dmu_zfetch_stream_remove(zf, zs);
189 * The maximum number of streams is normally zfetch_max_streams,
190 * but for small files we lower it such that it's at least possible
191 * for all the streams to be non-overlapping.
193 * If we are already at the maximum number of streams for this file,
194 * even after removing old streams, then don't create this stream.
196 uint32_t max_streams = MAX(1, MIN(zfetch_max_streams,
197 zf->zf_dnode->dn_maxblkid * zf->zf_dnode->dn_datablksz /
198 zfetch_max_distance));
199 if (numstreams >= max_streams) {
200 ZFETCHSTAT_BUMP(zfetchstat_max_streams);
204 zstream_t *zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);
205 zs->zs_blkid = blkid;
206 zs->zs_pf_blkid = blkid;
207 zs->zs_atime = gethrtime();
208 mutex_init(&zs->zs_lock, NULL, MUTEX_DEFAULT, NULL);
210 list_insert_head(&zf->zf_stream, zs);
214 * This is the prefetch entry point. It calls all of the other dmu_zfetch
215 * routines to create, delete, find, or operate upon prefetch streams.
218 dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks)
222 if (zfs_prefetch_disable)
226 * As a fast path for small (single-block) files, ignore access
227 * to the first block.
232 rw_enter(&zf->zf_rwlock, RW_READER);
234 for (zs = list_head(&zf->zf_stream); zs != NULL;
235 zs = list_next(&zf->zf_stream, zs)) {
236 if (blkid == zs->zs_blkid) {
237 mutex_enter(&zs->zs_lock);
239 * zs_blkid could have changed before we
240 * acquired zs_lock; re-check them here.
242 if (blkid != zs->zs_blkid) {
243 mutex_exit(&zs->zs_lock);
252 * This access is not part of any existing stream. Create
253 * a new stream for it.
255 ZFETCHSTAT_BUMP(zfetchstat_misses);
256 if (rw_tryupgrade(&zf->zf_rwlock))
257 dmu_zfetch_stream_create(zf, blkid + nblks);
258 rw_exit(&zf->zf_rwlock);
263 * This access was to a block that we issued a prefetch for on
264 * behalf of this stream. Issue further prefetches for this stream.
266 * Normally, we start prefetching where we stopped
267 * prefetching last (zs_pf_blkid). But when we get our first
268 * hit on this stream, zs_pf_blkid == zs_blkid, we don't
269 * want to prefetch to block we just accessed. In this case,
270 * start just after the block we just accessed.
272 int64_t pf_start = MAX(zs->zs_pf_blkid, blkid + nblks);
275 * Double our amount of prefetched data, but don't let the
276 * prefetch get further ahead than zfetch_max_distance.
279 MIN((int64_t)zs->zs_pf_blkid - zs->zs_blkid + nblks,
280 zs->zs_blkid + nblks +
281 (zfetch_max_distance >> zf->zf_dnode->dn_datablkshift) - pf_start);
283 zs->zs_pf_blkid = pf_start + pf_nblks;
284 zs->zs_atime = gethrtime();
285 zs->zs_blkid = blkid + nblks;
288 * dbuf_prefetch() issues the prefetch i/o
289 * asynchronously, but it may need to wait for an
290 * indirect block to be read from disk. Therefore
291 * we do not want to hold any locks while we call it.
293 mutex_exit(&zs->zs_lock);
294 rw_exit(&zf->zf_rwlock);
295 for (int i = 0; i < pf_nblks; i++) {
296 dbuf_prefetch(zf->zf_dnode, 0, pf_start + i,
297 ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH);
299 ZFETCHSTAT_BUMP(zfetchstat_hits);