uvm_readahead.c revision 1.8.12.2 1 /* $NetBSD: uvm_readahead.c,v 1.8.12.2 2012/10/09 20:07:28 bouyer Exp $ */
2
3 /*-
4 * Copyright (c)2003, 2005, 2009 YAMAMOTO Takashi,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * uvm_object read-ahead
31 *
32 * TODO:
33 * - tune.
34 * - handle multiple streams.
35 * - find a better way to deal with PGO_LOCKED pager requests.
36 * (currently just ignored)
37 * - consider the amount of memory in the system.
38 * - consider the speed of the underlying device.
39 * - consider filesystem block size / block layout.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: uvm_readahead.c,v 1.8.12.2 2012/10/09 20:07:28 bouyer Exp $");
44
45 #include <sys/param.h>
46 #include <sys/pool.h>
47
48 #include <uvm/uvm.h>
49 #include <uvm/uvm_readahead.h>
50
51 #if defined(READAHEAD_DEBUG)
52 #define DPRINTF(a) printf a
53 #else /* defined(READAHEAD_DEBUG) */
54 #define DPRINTF(a) /* nothing */
55 #endif /* defined(READAHEAD_DEBUG) */
56
57 #if defined(sun2) || defined(sun3)
58 /* XXX: on sun2 and sun3 MAXPHYS is 0xe000 */
59 #undef MAXPHYS
60 #define MAXPHYS 0x8000 /* XXX */
61 #endif
62
63 static off_t ra_startio(struct uvm_object *, off_t, size_t, size_t);
64 static struct uvm_ractx *ra_allocctx(void);
65 static void ra_freectx(struct uvm_ractx *);
66
67 static struct pool_cache ractx_cache;
68
69 /*
70 * uvm_ra_init: initialize readahead module.
71 */
72
73 void
74 uvm_ra_init(void)
75 {
76
77 pool_cache_bootstrap(&ractx_cache, sizeof(struct uvm_ractx), 0, 0, 0,
78 "ractx", NULL, IPL_NONE, NULL, NULL, NULL);
79 }
80
81 static struct uvm_ractx *
82 ra_allocctx(void)
83 {
84
85 return pool_cache_get(&ractx_cache, PR_NOWAIT);
86 }
87
88 static void
89 ra_freectx(struct uvm_ractx *ra)
90 {
91
92 pool_cache_put(&ractx_cache, ra);
93 }
94
95 /*
96 * ra_startio: start i/o for read-ahead.
97 *
98 * => start i/o for each RA_IOCHUNK sized chunk.
99 * => return offset to which we started i/o.
100 *
101 * => If the next layer up has given us less than IOCHUNK, assume
102 * it knew best (don't always perform minimal readahead).
103 */
104
105 static off_t
106 ra_startio(struct uvm_object *uobj, off_t off, size_t sz, size_t chunksz)
107 {
108 const off_t endoff = off + sz;
109
110 DPRINTF(("%s: uobj=%p, off=%" PRIu64 ", endoff=%" PRIu64 "\n",
111 __func__, uobj, off, endoff));
112
113 KASSERT((off & (PAGE_SIZE - 1)) == 0);
114 KASSERT((chunksz & (PAGE_SIZE - 1)) == 0);
115 while (off < endoff) {
116 const size_t chunksize = MIN(chunksz, round_page(sz));
117 int error;
118 size_t donebytes;
119 int npages;
120 int orignpages;
121
122 if ((chunksize & (PAGE_SIZE - 1)) != 0) {
123 panic("bad chunksize %d, iochunk %d, request size %d",
124 (int)chunksize, (int)chunksz, (int)sz);
125 }
126 /* KASSERT((chunksize & (PAGE_SIZE - 1)) == 0); */
127 KASSERT((off & PAGE_MASK) == 0);
128 npages = orignpages = chunksize >> PAGE_SHIFT;
129 KASSERT(npages != 0);
130
131 /*
132 * use UVM_ADV_RANDOM to avoid recursion.
133 */
134
135 mutex_enter(uobj->vmobjlock);
136 error = (*uobj->pgops->pgo_get)(uobj, off, NULL,
137 &npages, 0, VM_PROT_READ, UVM_ADV_RANDOM, 0);
138 DPRINTF(("%s: off=%" PRIu64 ", chunksize=%zu -> %d\n",
139 __func__, off, chunksize, error));
140 if (error != 0 && error != EBUSY) {
141 if (error != EINVAL) { /* maybe past EOF */
142 DPRINTF(("%s: error=%d\n", __func__, error));
143 }
144 break;
145 }
146 KASSERT(orignpages == npages);
147 donebytes = orignpages << PAGE_SHIFT;
148 off += donebytes;
149 }
150
151 return off;
152 }
153
154 /* ------------------------------------------------------------ */
155
156 /*
157 * uvm_ra_allocctx: allocate a context.
158 */
159
160 struct uvm_ractx *
161 uvm_ra_allocctx(void)
162 {
163 struct uvm_ractx *ra;
164
165 ra = ra_allocctx();
166 if (ra != NULL) {
167 ra->ra_flags = 0;
168 ra->ra_iochunk = MAXPHYS;
169 }
170
171 return ra;
172 }
173
174 /*
175 * uvm_ra_freectx: free a context.
176 */
177
178 void
179 uvm_ra_freectx(struct uvm_ractx *ra)
180 {
181
182 KASSERT(ra != NULL);
183 ra_freectx(ra);
184 }
185
186 /*
187 * uvm_ra_request: update a read-ahead context and start i/o if appropriate.
188 *
189 * => called when [reqoff, reqoff+reqsize) is requested.
190 * => object must be locked by caller, will return locked.
191 */
192
193 void
194 uvm_ra_request(struct uvm_ractx *ra, int advice, struct uvm_object *uobj,
195 off_t reqoff, size_t reqsize)
196 {
197
198 KASSERT(mutex_owned(uobj->vmobjlock));
199
200 KASSERT((reqoff & (PAGE_SIZE - 1)) == 0);
201 KASSERT((reqsize & (PAGE_SIZE - 1)) == 0);
202
203 if (ra == NULL || advice == UVM_ADV_RANDOM) {
204 return;
205 }
206
207 if (advice == UVM_ADV_SEQUENTIAL) {
208
209 /*
210 * always do read-ahead with a large window.
211 */
212
213 if ((ra->ra_flags & UVM_RA_VALID) == 0) {
214 ra->ra_winstart = ra->ra_next = 0;
215 ra->ra_flags |= UVM_RA_VALID;
216 }
217 if (reqoff < ra->ra_winstart) {
218 ra->ra_next = reqoff;
219 }
220 ra->ra_winsize = UVM_RA_WINSIZE_SEQUENTIAL;
221 goto do_readahead;
222 }
223
224 /*
225 * a request with UVM_ADV_NORMAL hint. (ie. no hint)
226 *
227 * we keep a sliding window in order to determine:
228 * - if the previous read-ahead was successful or not.
229 * - how many bytes to read-ahead.
230 */
231
232 /*
233 * if it's the first request for this context,
234 * initialize context and return.
235 */
236
237 if ((ra->ra_flags & UVM_RA_VALID) == 0) {
238 initialize:
239 ra->ra_winstart = ra->ra_next = reqoff + reqsize;
240 ra->ra_winsize = UVM_RA_WINSIZE_INIT;
241 ra->ra_flags |= UVM_RA_VALID;
242 goto done;
243 }
244
245 /*
246 * if it isn't in our window,
247 * initialize context and return.
248 * (read-ahead miss)
249 */
250
251 if (reqoff < ra->ra_winstart ||
252 ra->ra_winstart + ra->ra_winsize < reqoff) {
253
254 /*
255 * ... unless we seem to be reading the same chunk repeatedly.
256 *
257 * XXX should have some margin?
258 */
259
260 if (reqoff + reqsize == ra->ra_winstart) {
261 DPRINTF(("%s: %p: same block: off=%" PRIu64
262 ", size=%zd, winstart=%" PRIu64 "\n",
263 __func__, ra, reqoff, reqsize, ra->ra_winstart));
264 goto done;
265 }
266 goto initialize;
267 }
268
269 /*
270 * it's in our window. (read-ahead hit)
271 * - start read-ahead i/o if appropriate.
272 * - advance and enlarge window.
273 */
274
275 do_readahead:
276
277 /*
278 * don't bother to read-ahead behind current request.
279 */
280
281 if (reqoff > ra->ra_next) {
282 ra->ra_next = reqoff;
283 }
284
285 /*
286 * try to make [reqoff, reqoff+ra_winsize) in-core.
287 * note that [reqoff, ra_next) is considered already done.
288 */
289
290 if (reqoff + ra->ra_winsize > ra->ra_next) {
291 off_t raoff = MAX(reqoff, ra->ra_next);
292 size_t rasize = reqoff + ra->ra_winsize - ra->ra_next;
293
294 #if defined(DIAGNOSTIC)
295 if (rasize > UVM_RA_WINSIZE_MAX) {
296 printf("%s: corrupted context", __func__);
297 rasize = UVM_RA_WINSIZE_MAX;
298 }
299 #endif /* defined(DIAGNOSTIC) */
300
301 /*
302 * issue read-ahead only if we can start big enough i/o.
303 * otherwise we end up with a stream of small i/o.
304 */
305
306 if (rasize >= UVM_RA_MINSIZE) {
307 off_t next;
308
309 mutex_exit(uobj->vmobjlock);
310 next = ra_startio(uobj, raoff, rasize, ra->ra_iochunk);
311 mutex_enter(uobj->vmobjlock);
312 ra->ra_next = next;
313 }
314 }
315
316 /*
317 * update window.
318 *
319 * enlarge window by reqsize, so that it grows in a predictable manner
320 * regardless of the size of each read(2).
321 */
322
323 ra->ra_winstart = reqoff + reqsize;
324 ra->ra_winsize = MIN(UVM_RA_WINSIZE_MAX, ra->ra_winsize + reqsize);
325
326 done:;
327 }
328
329 int
330 uvm_readahead(struct uvm_object *uobj, off_t off, off_t size,
331 struct uvm_ractx *ra)
332 {
333
334 /*
335 * don't allow too much read-ahead.
336 */
337 if (size > UVM_RA_WINSIZE_MAX) {
338 size = UVM_RA_WINSIZE_MAX;
339 }
340 ra_startio(uobj, off, size, ra->ra_iochunk);
341 return 0;
342 }
343