uvm_readahead.c revision 1.1.2.3 1 /* $NetBSD: uvm_readahead.c,v 1.1.2.3 2005/11/15 11:28:39 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: uvm_readahead.c,v 1.1.2.3 2005/11/15 11:28:39 yamt Exp $");
31
32 #include <sys/param.h>
33 #include <sys/pool.h>
34 #include <sys/fcntl.h> /* POSIX_FADV_* */
35
36 #include <uvm/uvm.h>
37 #include <uvm/uvm_readahead.h>
38
39 struct uvm_ractx {
40 int ra_flags;
41 #define RA_VALID 1
42 int ra_advice;
43 off_t ra_winstart;
44 size_t ra_winsize;
45 off_t ra_next;
46 };
47
48 /*
49 * XXX tune
50 * XXX should consider the amount of memory in the system
51 */
52
53 #define RA_WINSIZE_INIT MAXPHYS
54 #define RA_WINSIZE_MAX (MAXPHYS * 8)
55 #define RA_WINSIZE_SEQENTIAL RA_WINSIZE_MAX
56 #define RA_MINSIZE (MAXPHYS * 2)
57
58 static off_t ra_startio(struct uvm_object *, off_t, size_t);
59 static struct uvm_ractx *ra_allocctx(void);
60 static void ra_freectx(struct uvm_ractx *);
61
62 POOL_INIT(ractx_pool, sizeof(struct uvm_ractx), 0, 0, 0, "ractx",
63 &pool_allocator_nointr);
64
65 static struct uvm_ractx *
66 ra_allocctx(void)
67 {
68
69 return pool_get(&ractx_pool, PR_NOWAIT);
70 }
71
72 static void
73 ra_freectx(struct uvm_ractx *ra)
74 {
75
76 pool_put(&ractx_pool, ra);
77 }
78
79 static off_t
80 ra_startio(struct uvm_object *uobj, off_t off, size_t sz)
81 {
82 const off_t endoff = off + sz;
83
84 #if defined(READAHEAD_DEBUG)
85 printf("%s: uobj=%p, off=%" PRIu64 ", endoff=%" PRIu64 "\n",
86 __func__, uobj, off, endoff);
87 #endif /* defined(READAHEAD_DEBUG) */
88 off = trunc_page(off);
89 while (off < endoff) {
90 const size_t chunksize = MAXPHYS;
91 int error;
92 size_t donebytes;
93 int npages;
94 int orignpages;
95 size_t bytelen;
96
97 KASSERT((chunksize & (chunksize - 1)) == 0);
98 KASSERT((off & PAGE_MASK) == 0);
99 bytelen = ((off + chunksize) & -(off_t)chunksize) - off;
100 #if defined(READAHEAD_DEBUG)
101 printf("%s: off=%" PRIu64 ", bytelen=%zu\n",
102 __func__, off, bytelen);
103 #endif /* defined(READAHEAD_DEBUG) */
104 KASSERT((bytelen & PAGE_MASK) == 0);
105 npages = orignpages = bytelen >> PAGE_SHIFT;
106 KASSERT(npages != 0);
107 simple_lock(&uobj->vmobjlock);
108 error = (*uobj->pgops->pgo_get)(uobj, off, NULL,
109 &npages, 0, VM_PROT_READ, 0, 0);
110 if (error) {
111 #if defined(READAHEAD_DEBUG)
112 if (error != EINVAL) {
113 printf("%s: error=%d\n", __func__, error);
114 }
115 #endif /* defined(READAHEAD_DEBUG) */
116 break;
117 }
118 KASSERT(orignpages != npages);
119 donebytes = orignpages << PAGE_SHIFT;
120 off += donebytes;
121 }
122
123 return off;
124 }
125
126 /* ------------------------------------------------------------ */
127
128 struct uvm_ractx *
129 uvm_ra_allocctx(int advice)
130 {
131 struct uvm_ractx *ra;
132
133 KASSERT(advice == POSIX_FADV_NORMAL ||
134 advice == POSIX_FADV_SEQUENTIAL ||
135 advice == POSIX_FADV_RANDOM);
136
137 ra = ra_allocctx();
138 if (ra != NULL) {
139 ra->ra_flags = 0;
140 ra->ra_winstart = 0;
141 ra->ra_advice = advice;
142 }
143
144 return ra;
145 }
146
147 void
148 uvm_ra_freectx(struct uvm_ractx *ra)
149 {
150
151 KASSERT(ra != NULL);
152 ra_freectx(ra);
153 }
154
155 void
156 uvm_ra_request(struct uvm_ractx *ra, struct uvm_object *uobj,
157 off_t reqoff, size_t reqsize)
158 {
159
160 if (ra == NULL) {
161 return;
162 }
163
164 switch (ra->ra_advice) {
165 case POSIX_FADV_NORMAL:
166 break;
167
168 case POSIX_FADV_RANDOM:
169 return;
170
171 case POSIX_FADV_SEQUENTIAL:
172 if (reqoff <= ra->ra_winstart) {
173 ra->ra_next = reqoff;
174 }
175 ra->ra_winsize = RA_WINSIZE_SEQENTIAL;
176 goto do_readahead;
177
178 default:
179 #if defined(DIAGNOSTIC)
180 panic("%s: unknown advice %d", __func__, ra->ra_advice);
181 #endif /* defined(DIAGNOSTIC) */
182 break;
183 }
184
185 if ((ra->ra_flags & RA_VALID) == 0) {
186 initialize:
187 ra->ra_winstart = ra->ra_next = reqoff + reqsize;
188 ra->ra_winsize = RA_WINSIZE_INIT;
189 ra->ra_flags |= RA_VALID;
190 return;
191 }
192
193 if (reqoff < ra->ra_winstart ||
194 ra->ra_winstart + ra->ra_winsize < reqoff) {
195
196 /*
197 * miss
198 */
199
200 goto initialize;
201 }
202
203 /*
204 * hit
205 */
206
207 do_readahead:
208 if (reqoff > ra->ra_next) {
209 ra->ra_next = reqoff;
210 }
211
212 if (reqoff + ra->ra_winsize > ra->ra_next) {
213 off_t raoff = MAX(reqoff, ra->ra_next);
214 size_t rasize = reqoff + ra->ra_winsize - ra->ra_next;
215
216 if (rasize >= RA_MINSIZE) {
217 ra->ra_next = ra_startio(uobj, raoff, rasize);
218 }
219 }
220
221 /*
222 * update window
223 */
224
225 ra->ra_winstart = reqoff + reqsize;
226 ra->ra_winsize = MIN(RA_WINSIZE_MAX, ra->ra_winsize + reqsize);
227 }
228