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