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