Home | History | Annotate | Line # | Download | only in uvm
uvm_readahead.c revision 1.1.2.6
      1 /*	$NetBSD: uvm_readahead.c,v 1.1.2.6 2005/11/17 04:28:10 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.6 2005/11/17 04:28:10 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 #if defined(READAHEAD_DEBUG)
     40 #define	DPRINTF(a)	printf a
     41 #else /* defined(READAHEAD_DEBUG) */
     42 #define	DPRINTF(a)	/* nothing */
     43 #endif /* defined(READAHEAD_DEBUG) */
     44 
     45 /*
     46  * uvm_ractx: read-ahead context.
     47  */
     48 
     49 struct uvm_ractx {
     50 	int ra_flags;
     51 #define	RA_VALID	1
     52 	int ra_advice;		/* hint from posix_fadvise */
     53 	off_t ra_winstart;	/* window start offset */
     54 	size_t ra_winsize;	/* window size */
     55 	off_t ra_next;		/* next offset to read-ahead */
     56 };
     57 
     58 /*
     59  * XXX tune
     60  * XXX should consider the amount of memory in the system.
     61  * XXX should consider the speed of the underlying device.
     62  */
     63 
     64 #define	RA_WINSIZE_INIT	MAXPHYS			/* initial window size */
     65 #define	RA_WINSIZE_MAX	(MAXPHYS * 8)		/* max window size */
     66 #define	RA_WINSIZE_SEQENTIAL	RA_WINSIZE_MAX	/* fixed window size used for
     67 						   SEQUENTIAL hint */
     68 #define	RA_MINSIZE	(MAXPHYS * 2)		/* min size to start i/o */
     69 #define	RA_IOCHUNK	MAXPHYS			/* read-ahead i/o chunk size */
     70 
     71 static off_t ra_startio(struct uvm_object *, off_t, size_t);
     72 static struct uvm_ractx *ra_allocctx(void);
     73 static void ra_freectx(struct uvm_ractx *);
     74 
     75 POOL_INIT(ractx_pool, sizeof(struct uvm_ractx), 0, 0, 0, "ractx",
     76     &pool_allocator_nointr);
     77 
     78 static struct uvm_ractx *
     79 ra_allocctx(void)
     80 {
     81 
     82 	return pool_get(&ractx_pool, PR_NOWAIT);
     83 }
     84 
     85 static void
     86 ra_freectx(struct uvm_ractx *ra)
     87 {
     88 
     89 	pool_put(&ractx_pool, ra);
     90 }
     91 
     92 /*
     93  * ra_startio: start i/o for read-ahead.
     94  *
     95  * => start i/o for each RA_IOCHUNK sized chunk.
     96  * => return offset to which we started i/o.
     97  */
     98 
     99 static off_t
    100 ra_startio(struct uvm_object *uobj, off_t off, size_t sz)
    101 {
    102 	const off_t endoff = off + sz;
    103 
    104 	DPRINTF(("%s: uobj=%p, off=%" PRIu64 ", endoff=%" PRIu64 "\n",
    105 	    __func__, uobj, off, endoff));
    106 	off = trunc_page(off);
    107 	while (off < endoff) {
    108 		const size_t chunksize = RA_IOCHUNK;
    109 		int error;
    110 		size_t donebytes;
    111 		int npages;
    112 		int orignpages;
    113 		size_t bytelen;
    114 
    115 		KASSERT((chunksize & (chunksize - 1)) == 0);
    116 		KASSERT((off & PAGE_MASK) == 0);
    117 		bytelen = ((off + chunksize) & -(off_t)chunksize) - off;
    118 		DPRINTF(("%s: off=%" PRIu64 ", bytelen=%zu\n",
    119 		    __func__, off, bytelen));
    120 		KASSERT((bytelen & PAGE_MASK) == 0);
    121 		npages = orignpages = bytelen >> PAGE_SHIFT;
    122 		KASSERT(npages != 0);
    123 		simple_lock(&uobj->vmobjlock);
    124 		error = (*uobj->pgops->pgo_get)(uobj, off, NULL,
    125 		    &npages, 0, VM_PROT_READ, 0, 0);
    126 		if (error) {
    127 			if (error != EINVAL) { /* maybe past EOF */
    128 				DPRINTF(("%s: error=%d\n", __func__, error));
    129 			}
    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