uvm_readahead.c revision 1.1.2.14 1 /* $NetBSD: uvm_readahead.c,v 1.1.2.14 2005/11/22 07:19:51 yamt Exp $ */
2
3 /*-
4 * Copyright (c)2003, 2005 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.1.2.14 2005/11/22 07:19:51 yamt 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 /*
58 * uvm_ractx: read-ahead context.
59 */
60
61 struct uvm_ractx {
62 int ra_flags;
63 #define RA_VALID 1
64 off_t ra_winstart; /* window start offset */
65 size_t ra_winsize; /* window size */
66 off_t ra_next; /* next offset to read-ahead */
67 };
68
69 #define RA_WINSIZE_INIT MAXPHYS /* initial window size */
70 #define RA_WINSIZE_MAX (MAXPHYS * 8) /* max window size */
71 #define RA_WINSIZE_SEQENTIAL RA_WINSIZE_MAX /* fixed window size used for
72 SEQUENTIAL hint */
73 #define RA_MINSIZE (MAXPHYS * 2) /* min size to start i/o */
74 #define RA_IOCHUNK MAXPHYS /* read-ahead i/o chunk size */
75
76 static off_t ra_startio(struct uvm_object *, off_t, size_t);
77 static struct uvm_ractx *ra_allocctx(void);
78 static void ra_freectx(struct uvm_ractx *);
79
80 POOL_INIT(ractx_pool, sizeof(struct uvm_ractx), 0, 0, 0, "ractx",
81 &pool_allocator_nointr);
82
83 static struct uvm_ractx *
84 ra_allocctx(void)
85 {
86
87 return pool_get(&ractx_pool, PR_NOWAIT);
88 }
89
90 static void
91 ra_freectx(struct uvm_ractx *ra)
92 {
93
94 pool_put(&ractx_pool, ra);
95 }
96
97 /*
98 * ra_startio: start i/o for read-ahead.
99 *
100 * => start i/o for each RA_IOCHUNK sized chunk.
101 * => return offset to which we started i/o.
102 */
103
104 static off_t
105 ra_startio(struct uvm_object *uobj, off_t off, size_t sz)
106 {
107 const off_t endoff = off + sz;
108
109 DPRINTF(("%s: uobj=%p, off=%" PRIu64 ", endoff=%" PRIu64 "\n",
110 __func__, uobj, off, endoff));
111 off = trunc_page(off);
112 while (off < endoff) {
113 const size_t chunksize = RA_IOCHUNK;
114 int error;
115 size_t donebytes;
116 int npages;
117 int orignpages;
118 size_t bytelen;
119
120 KASSERT((chunksize & (chunksize - 1)) == 0);
121 KASSERT((off & PAGE_MASK) == 0);
122 bytelen = ((off + chunksize) & -(off_t)chunksize) - off;
123 KASSERT((bytelen & PAGE_MASK) == 0);
124 npages = orignpages = bytelen >> PAGE_SHIFT;
125 KASSERT(npages != 0);
126
127 /*
128 * use UVM_ADV_RANDOM to avoid recursion.
129 */
130
131 simple_lock(&uobj->vmobjlock);
132 error = (*uobj->pgops->pgo_get)(uobj, off, NULL,
133 &npages, 0, VM_PROT_READ, UVM_ADV_RANDOM, 0);
134 DPRINTF(("%s: off=%" PRIu64 ", bytelen=%zu -> %d\n",
135 __func__, off, bytelen, error));
136 if (error != 0 && error != EBUSY) {
137 if (error != EINVAL) { /* maybe past EOF */
138 DPRINTF(("%s: error=%d\n", __func__, error));
139 }
140 break;
141 }
142 KASSERT(orignpages == npages);
143 donebytes = orignpages << PAGE_SHIFT;
144 off += donebytes;
145 }
146
147 return off;
148 }
149
150 /* ------------------------------------------------------------ */
151
152 struct uvm_ractx *
153 uvm_ra_allocctx(void)
154 {
155 struct uvm_ractx *ra;
156
157 ra = ra_allocctx();
158 if (ra != NULL) {
159 ra->ra_flags = 0;
160 }
161
162 return ra;
163 }
164
165 void
166 uvm_ra_freectx(struct uvm_ractx *ra)
167 {
168
169 KASSERT(ra != NULL);
170 ra_freectx(ra);
171 }
172
173 /*
174 * uvm_ra_request: start i/o for read-ahead if appropriate.
175 *
176 * => called by filesystems when [reqoff, reqoff+reqsize) is requested.
177 */
178
179 void
180 uvm_ra_request(struct uvm_ractx *ra, int advice, struct uvm_object *uobj,
181 off_t reqoff, size_t reqsize)
182 {
183
184 if (ra == NULL || advice == UVM_ADV_RANDOM) {
185 return;
186 }
187
188 /*
189 * XXX needs locking? maybe.
190 * but the worst effect is merely a bad read-ahead.
191 */
192
193 if (advice == UVM_ADV_SEQUENTIAL) {
194
195 /*
196 * always do read-ahead with a large window.
197 */
198
199 if ((ra->ra_flags & RA_VALID) == 0) {
200 ra->ra_winstart = ra->ra_next = 0;
201 ra->ra_flags |= RA_VALID;
202 }
203 if (reqoff < ra->ra_winstart) {
204 ra->ra_next = reqoff;
205 }
206 ra->ra_winsize = RA_WINSIZE_SEQENTIAL;
207 goto do_readahead;
208 }
209
210 /*
211 * a request with UVM_ADV_NORMAL hint. (ie. no hint)
212 *
213 * we keep a sliding window in order to determine:
214 * - if the previous read-ahead was successful or not.
215 * - how many bytes to read-ahead.
216 */
217
218 /*
219 * if it's the first request for this context,
220 * initialize context and return.
221 */
222
223 if ((ra->ra_flags & RA_VALID) == 0) {
224 initialize:
225 ra->ra_winstart = ra->ra_next = reqoff + reqsize;
226 ra->ra_winsize = RA_WINSIZE_INIT;
227 ra->ra_flags |= RA_VALID;
228 goto done;
229 }
230
231 /*
232 * if it isn't in our window,
233 * initialize context and return.
234 * (read-ahead miss)
235 */
236
237 if (reqoff < ra->ra_winstart ||
238 ra->ra_winstart + ra->ra_winsize < reqoff) {
239
240 /*
241 * ... unless we seem to be reading the same chunk repeatedly.
242 */
243
244 if (reqoff + reqsize == ra->ra_winstart) {
245 DPRINTF(("%s: %p: same block: off=%" PRIu64
246 ", size=%zd, winstart=%" PRIu64 "\n",
247 __func__, ra, reqoff, reqsize, ra->ra_winstart));
248 goto done;
249 }
250 goto initialize;
251 }
252
253 /*
254 * it's in our window. (read-ahead hit)
255 * - start read-ahead i/o if appropriate.
256 * - advance and enlarge window.
257 */
258
259 do_readahead:
260
261 /*
262 * don't bother to read-ahead behind current request.
263 */
264
265 if (reqoff > ra->ra_next) {
266 ra->ra_next = reqoff;
267 }
268
269 /*
270 * try to make [reqoff, reqoff+ra_winsize) in-core.
271 * note that [reqoff, ra_next) is considered already done.
272 */
273
274 if (reqoff + ra->ra_winsize > ra->ra_next) {
275 off_t raoff = MAX(reqoff, ra->ra_next);
276 size_t rasize = reqoff + ra->ra_winsize - ra->ra_next;
277
278 #if defined(DIAGNOSTIC)
279 if (rasize > RA_WINSIZE_MAX) {
280
281 /*
282 * shouldn't happen as far as we're protected by
283 * kernel_lock.
284 */
285
286 printf("%s: corrupted context", __func__);
287 rasize = RA_WINSIZE_MAX;
288 }
289 #endif /* defined(DIAGNOSTIC) */
290
291 /*
292 * issue read-ahead only if we can start big enough i/o.
293 * otherwise we end up with a stream of small i/o.
294 */
295
296 if (rasize >= RA_MINSIZE) {
297 ra->ra_next = ra_startio(uobj, raoff, rasize);
298 }
299 }
300
301 /*
302 * update window.
303 *
304 * enlarge window by reqsize, so that it grows in a predictable manner
305 * regardless of the size of each read(2).
306 */
307
308 ra->ra_winstart = reqoff + reqsize;
309 ra->ra_winsize = MIN(RA_WINSIZE_MAX, ra->ra_winsize + reqsize);
310
311 done:;
312 }
313