Home | History | Annotate | Line # | Download | only in uvm
uvm_readahead.c revision 1.1.2.8
      1 /*	$NetBSD: uvm_readahead.c,v 1.1.2.8 2005/11/17 08:05:01 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.8 2005/11/17 08:05:01 yamt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/pool.h>
     34 
     35 #include <uvm/uvm.h>
     36 #include <uvm/uvm_readahead.h>
     37 
     38 #if defined(READAHEAD_DEBUG)
     39 #define	DPRINTF(a)	printf a
     40 #else /* defined(READAHEAD_DEBUG) */
     41 #define	DPRINTF(a)	/* nothing */
     42 #endif /* defined(READAHEAD_DEBUG) */
     43 
     44 /*
     45  * uvm_ractx: read-ahead context.
     46  */
     47 
     48 struct uvm_ractx {
     49 	int ra_flags;
     50 #define	RA_VALID	1
     51 	int ra_advice;		/* hint from posix_fadvise; UVM_ADV_* */
     52 	off_t ra_winstart;	/* window start offset */
     53 	size_t ra_winsize;	/* window size */
     54 	off_t ra_next;		/* next offset to read-ahead */
     55 };
     56 
     57 /*
     58  * XXX tune
     59  * XXX should consider the amount of memory in the system.
     60  * XXX should consider the speed of the underlying device.
     61  */
     62 
     63 #define	RA_WINSIZE_INIT	MAXPHYS			/* initial window size */
     64 #define	RA_WINSIZE_MAX	(MAXPHYS * 8)		/* max window size */
     65 #define	RA_WINSIZE_SEQENTIAL	RA_WINSIZE_MAX	/* fixed window size used for
     66 						   SEQUENTIAL hint */
     67 #define	RA_MINSIZE	(MAXPHYS * 2)		/* min size to start i/o */
     68 #define	RA_IOCHUNK	MAXPHYS			/* read-ahead i/o chunk size */
     69 
     70 static off_t ra_startio(struct uvm_object *, off_t, size_t);
     71 static struct uvm_ractx *ra_allocctx(void);
     72 static void ra_freectx(struct uvm_ractx *);
     73 
     74 POOL_INIT(ractx_pool, sizeof(struct uvm_ractx), 0, 0, 0, "ractx",
     75     &pool_allocator_nointr);
     76 
     77 static struct uvm_ractx *
     78 ra_allocctx(void)
     79 {
     80 
     81 	return pool_get(&ractx_pool, PR_NOWAIT);
     82 }
     83 
     84 static void
     85 ra_freectx(struct uvm_ractx *ra)
     86 {
     87 
     88 	pool_put(&ractx_pool, ra);
     89 }
     90 
     91 /*
     92  * ra_startio: start i/o for read-ahead.
     93  *
     94  * => start i/o for each RA_IOCHUNK sized chunk.
     95  * => return offset to which we started i/o.
     96  */
     97 
     98 static off_t
     99 ra_startio(struct uvm_object *uobj, off_t off, size_t sz)
    100 {
    101 	const off_t endoff = off + sz;
    102 
    103 	DPRINTF(("%s: uobj=%p, off=%" PRIu64 ", endoff=%" PRIu64 "\n",
    104 	    __func__, uobj, off, endoff));
    105 	off = trunc_page(off);
    106 	while (off < endoff) {
    107 		const size_t chunksize = RA_IOCHUNK;
    108 		int error;
    109 		size_t donebytes;
    110 		int npages;
    111 		int orignpages;
    112 		size_t bytelen;
    113 
    114 		KASSERT((chunksize & (chunksize - 1)) == 0);
    115 		KASSERT((off & PAGE_MASK) == 0);
    116 		bytelen = ((off + chunksize) & -(off_t)chunksize) - off;
    117 		DPRINTF(("%s: off=%" PRIu64 ", bytelen=%zu\n",
    118 		    __func__, off, bytelen));
    119 		KASSERT((bytelen & PAGE_MASK) == 0);
    120 		npages = orignpages = bytelen >> PAGE_SHIFT;
    121 		KASSERT(npages != 0);
    122 		simple_lock(&uobj->vmobjlock);
    123 		error = (*uobj->pgops->pgo_get)(uobj, off, NULL,
    124 		    &npages, 0, VM_PROT_READ, 0, 0);
    125 		if (error) {
    126 			if (error != EINVAL) { /* maybe past EOF */
    127 				DPRINTF(("%s: error=%d\n", __func__, error));
    128 			}
    129 			break;
    130 		}
    131 		KASSERT(orignpages == npages);
    132 		donebytes = orignpages << PAGE_SHIFT;
    133 		off += donebytes;
    134 	}
    135 
    136 	return off;
    137 }
    138 
    139 /* ------------------------------------------------------------ */
    140 
    141 struct uvm_ractx *
    142 uvm_ra_allocctx(int advice)
    143 {
    144 	struct uvm_ractx *ra;
    145 
    146 	KASSERT(advice == UVM_ADV_NORMAL ||
    147 	    advice == UVM_ADV_RANDOM ||
    148 	    advice == UVM_ADV_SEQUENTIAL);
    149 
    150 	ra = ra_allocctx();
    151 	if (ra != NULL) {
    152 		ra->ra_flags = 0;
    153 		ra->ra_winstart = 0;
    154 		ra->ra_advice = advice;
    155 	}
    156 
    157 	return ra;
    158 }
    159 
    160 void
    161 uvm_ra_freectx(struct uvm_ractx *ra)
    162 {
    163 
    164 	KASSERT(ra != NULL);
    165 	ra_freectx(ra);
    166 }
    167 
    168 /*
    169  * uvm_ra_request: start i/o for read-ahead if appropriate.
    170  *
    171  * => called by filesystems when [reqoff, reqoff+reqsize) is requested.
    172  */
    173 
    174 void
    175 uvm_ra_request(struct uvm_ractx *ra, struct uvm_object *uobj,
    176     off_t reqoff, size_t reqsize)
    177 {
    178 
    179 	if (ra == NULL) {
    180 		return;
    181 	}
    182 
    183 	switch (ra->ra_advice) {
    184 	case UVM_ADV_NORMAL:
    185 		break;
    186 
    187 	case UVM_ADV_RANDOM:
    188 
    189 		/*
    190 		 * no read-ahead.
    191 		 */
    192 
    193 		return;
    194 
    195 	case UVM_ADV_SEQUENTIAL:
    196 
    197 		/*
    198 		 * always do read-ahead with a large window.
    199 		 */
    200 
    201 		if (reqoff <= ra->ra_winstart) {
    202 			ra->ra_next = reqoff;
    203 		}
    204 		ra->ra_winsize = RA_WINSIZE_SEQENTIAL;
    205 		goto do_readahead;
    206 
    207 	default:
    208 #if defined(DIAGNOSTIC)
    209 		panic("%s: unknown advice %d", __func__, ra->ra_advice);
    210 #endif /* defined(DIAGNOSTIC) */
    211 		break;
    212 	}
    213 
    214 	/*
    215 	 * a request with NORMAL hint.  (ie. no hint)
    216 	 *
    217 	 * we keep a sliding window in order to determine:
    218 	 *	- if the previous read-ahead was successful or not.
    219 	 *	- how many bytes to read-ahead.
    220 	 */
    221 
    222 	/*
    223 	 * if it's the first request for this context,
    224 	 * initialize context and return.
    225 	 */
    226 
    227 	if ((ra->ra_flags & RA_VALID) == 0) {
    228 initialize:
    229 		ra->ra_winstart = ra->ra_next = reqoff + reqsize;
    230 		ra->ra_winsize = RA_WINSIZE_INIT;
    231 		ra->ra_flags |= RA_VALID;
    232 		return;
    233 	}
    234 
    235 	/*
    236 	 * if it isn't in our window,
    237 	 * initialize context and return.
    238 	 * (read-ahead miss)
    239 	 */
    240 
    241 	if (reqoff < ra->ra_winstart ||
    242 	    ra->ra_winstart + ra->ra_winsize < reqoff) {
    243 		goto initialize;
    244 	}
    245 
    246 	/*
    247 	 * it's in our window. (read-ahead hit)
    248 	 *	- start read-ahead i/o if appropriate.
    249 	 *	- advance and enlarge window.
    250 	 */
    251 
    252 do_readahead:
    253 
    254 	/*
    255 	 * don't bother to read-ahead behind current request.
    256 	 */
    257 
    258 	if (reqoff > ra->ra_next) {
    259 		ra->ra_next = reqoff;
    260 	}
    261 
    262 	/*
    263 	 * try to make [reqoff, reqoff+ra_winsize) in-core.
    264 	 * note that [reqoff, ra_next) is considered already done.
    265 	 */
    266 
    267 	if (reqoff + ra->ra_winsize > ra->ra_next) {
    268 		off_t raoff = MAX(reqoff, ra->ra_next);
    269 		size_t rasize = reqoff + ra->ra_winsize - ra->ra_next;
    270 
    271 		/*
    272 		 * issue read-ahead only if we can start big enough i/o.
    273 		 * otherwise we end up with a stream of small i/o.
    274 		 */
    275 
    276 		if (rasize >= RA_MINSIZE) {
    277 			ra->ra_next = ra_startio(uobj, raoff, rasize);
    278 		}
    279 	}
    280 
    281 	/*
    282 	 * update window.
    283 	 *
    284 	 * enlarge window by reqsize, so that it grows in a predictable manner
    285 	 * regardless of the size of each read(2).
    286 	 */
    287 
    288 	ra->ra_winstart = reqoff + reqsize;
    289 	ra->ra_winsize = MIN(RA_WINSIZE_MAX, ra->ra_winsize + reqsize);
    290 }
    291