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