6281 prefetching should apply to 1MB reads
[unleashed.git] / usr / src / uts / common / fs / zfs / dmu_zfetch.c
blobf2cdf863d83c9ecb627a3b566c4c09553e4b9666
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
34 #include <sys/dmu.h>
35 #include <sys/dbuf.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 typedef struct zfetch_stats {
56 kstat_named_t zfetchstat_hits;
57 kstat_named_t zfetchstat_misses;
58 kstat_named_t zfetchstat_max_streams;
59 } zfetch_stats_t;
61 static zfetch_stats_t zfetch_stats = {
62 { "hits", KSTAT_DATA_UINT64 },
63 { "misses", KSTAT_DATA_UINT64 },
64 { "max_streams", KSTAT_DATA_UINT64 },
67 #define ZFETCHSTAT_BUMP(stat) \
68 atomic_inc_64(&zfetch_stats.stat.value.ui64);
70 kstat_t *zfetch_ksp;
72 void
73 zfetch_init(void)
75 zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
76 KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
77 KSTAT_FLAG_VIRTUAL);
79 if (zfetch_ksp != NULL) {
80 zfetch_ksp->ks_data = &zfetch_stats;
81 kstat_install(zfetch_ksp);
85 void
86 zfetch_fini(void)
88 if (zfetch_ksp != NULL) {
89 kstat_delete(zfetch_ksp);
90 zfetch_ksp = NULL;
95 * This takes a pointer to a zfetch structure and a dnode. It performs the
96 * necessary setup for the zfetch structure, grokking data from the
97 * associated dnode.
99 void
100 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
102 if (zf == NULL)
103 return;
105 zf->zf_dnode = dno;
107 list_create(&zf->zf_stream, sizeof (zstream_t),
108 offsetof(zstream_t, zs_node));
110 rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL);
113 static void
114 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
116 ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
117 list_remove(&zf->zf_stream, zs);
118 mutex_destroy(&zs->zs_lock);
119 kmem_free(zs, sizeof (*zs));
123 * Clean-up state associated with a zfetch structure (e.g. destroy the
124 * streams). This doesn't free the zfetch_t itself, that's left to the caller.
126 void
127 dmu_zfetch_fini(zfetch_t *zf)
129 zstream_t *zs;
131 ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
133 rw_enter(&zf->zf_rwlock, RW_WRITER);
134 while ((zs = list_head(&zf->zf_stream)) != NULL)
135 dmu_zfetch_stream_remove(zf, zs);
136 rw_exit(&zf->zf_rwlock);
137 list_destroy(&zf->zf_stream);
138 rw_destroy(&zf->zf_rwlock);
140 zf->zf_dnode = NULL;
144 * If there aren't too many streams already, create a new stream.
145 * The "blkid" argument is the next block that we expect this stream to access.
146 * While we're here, clean up old streams (which haven't been
147 * accessed for at least zfetch_min_sec_reap seconds).
149 static void
150 dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)
152 zstream_t *zs_next;
153 int numstreams = 0;
155 ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
158 * Clean up old streams.
160 for (zstream_t *zs = list_head(&zf->zf_stream);
161 zs != NULL; zs = zs_next) {
162 zs_next = list_next(&zf->zf_stream, zs);
163 if (((gethrtime() - zs->zs_atime) / NANOSEC) >
164 zfetch_min_sec_reap)
165 dmu_zfetch_stream_remove(zf, zs);
166 else
167 numstreams++;
171 * The maximum number of streams is normally zfetch_max_streams,
172 * but for small files we lower it such that it's at least possible
173 * for all the streams to be non-overlapping.
175 * If we are already at the maximum number of streams for this file,
176 * even after removing old streams, then don't create this stream.
178 uint32_t max_streams = MAX(1, MIN(zfetch_max_streams,
179 zf->zf_dnode->dn_maxblkid * zf->zf_dnode->dn_datablksz /
180 zfetch_max_distance));
181 if (numstreams >= max_streams) {
182 ZFETCHSTAT_BUMP(zfetchstat_max_streams);
183 return;
186 zstream_t *zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);
187 zs->zs_blkid = blkid;
188 zs->zs_pf_blkid = blkid;
189 zs->zs_atime = gethrtime();
190 mutex_init(&zs->zs_lock, NULL, MUTEX_DEFAULT, NULL);
192 list_insert_head(&zf->zf_stream, zs);
196 * This is the prefetch entry point. It calls all of the other dmu_zfetch
197 * routines to create, delete, find, or operate upon prefetch streams.
199 void
200 dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks)
202 zstream_t *zs;
204 if (zfs_prefetch_disable)
205 return;
208 * As a fast path for small (single-block) files, ignore access
209 * to the first block.
211 if (blkid == 0)
212 return;
214 rw_enter(&zf->zf_rwlock, RW_READER);
216 for (zs = list_head(&zf->zf_stream); zs != NULL;
217 zs = list_next(&zf->zf_stream, zs)) {
218 if (blkid == zs->zs_blkid) {
219 mutex_enter(&zs->zs_lock);
221 * zs_blkid could have changed before we
222 * acquired zs_lock; re-check them here.
224 if (blkid != zs->zs_blkid) {
225 mutex_exit(&zs->zs_lock);
226 continue;
228 break;
232 if (zs == NULL) {
234 * This access is not part of any existing stream. Create
235 * a new stream for it.
237 ZFETCHSTAT_BUMP(zfetchstat_misses);
238 if (rw_tryupgrade(&zf->zf_rwlock))
239 dmu_zfetch_stream_create(zf, blkid + nblks);
240 rw_exit(&zf->zf_rwlock);
241 return;
245 * This access was to a block that we issued a prefetch for on
246 * behalf of this stream. Issue further prefetches for this stream.
248 * Normally, we start prefetching where we stopped
249 * prefetching last (zs_pf_blkid). But when we get our first
250 * hit on this stream, zs_pf_blkid == zs_blkid, we don't
251 * want to prefetch to block we just accessed. In this case,
252 * start just after the block we just accessed.
254 int64_t pf_start = MAX(zs->zs_pf_blkid, blkid + nblks);
257 * Double our amount of prefetched data, but don't let the
258 * prefetch get further ahead than zfetch_max_distance.
260 int pf_nblks =
261 MIN((int64_t)zs->zs_pf_blkid - zs->zs_blkid + nblks,
262 zs->zs_blkid + nblks +
263 (zfetch_max_distance >> zf->zf_dnode->dn_datablkshift) - pf_start);
265 zs->zs_pf_blkid = pf_start + pf_nblks;
266 zs->zs_atime = gethrtime();
267 zs->zs_blkid = blkid + nblks;
270 * dbuf_prefetch() issues the prefetch i/o
271 * asynchronously, but it may need to wait for an
272 * indirect block to be read from disk. Therefore
273 * we do not want to hold any locks while we call it.
275 mutex_exit(&zs->zs_lock);
276 rw_exit(&zf->zf_rwlock);
277 for (int i = 0; i < pf_nblks; i++) {
278 dbuf_prefetch(zf->zf_dnode, 0, pf_start + i,
279 ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH);
281 ZFETCHSTAT_BUMP(zfetchstat_hits);