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