Home | History | Annotate | Line # | Download | only in uvm
uvm_readahead.c revision 1.1.2.2
      1 /*	$NetBSD: uvm_readahead.c,v 1.1.2.2 2005/11/15 05:24:48 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.2 2005/11/15 05:24:48 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 0
     85 	printf("%s: uobj=%p, off=%" PRIu64 ", endoff=%" PRIu64 "\n",
     86 	    __func__, uobj, off, endoff);
     87 #endif
     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 0
    101 		printf("%s: off=%" PRIu64 ", bytelen=%zu\n",
    102 		    __func__, off, bytelen);
    103 #endif
    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 1
    112 			if (error != EINVAL) {
    113 				printf("%s: error=%d\n", __func__, error);
    114 			}
    115 #endif
    116 			break;
    117 		}
    118 		donebytes = orignpages << PAGE_SHIFT;
    119 		off += donebytes;
    120 		if (orignpages != npages) {
    121 #if 1
    122 			printf("%s: orignpages=%d, npages=%d\n",
    123 			    __func__, orignpages, npages);
    124 #endif
    125 			/* XXX */
    126 		}
    127 	}
    128 
    129 	return off;
    130 }
    131 
    132 /* ------------------------------------------------------------ */
    133 
    134 struct uvm_ractx *
    135 uvm_ra_allocctx(int advice)
    136 {
    137 	struct uvm_ractx *ra;
    138 
    139 	KASSERT(advice == POSIX_FADV_NORMAL ||
    140 	    advice == POSIX_FADV_SEQUENTIAL ||
    141 	    advice == POSIX_FADV_RANDOM);
    142 
    143 	ra = ra_allocctx();
    144 	if (ra != NULL) {
    145 		ra->ra_flags = 0;
    146 		ra->ra_winstart = 0;
    147 		ra->ra_advice = advice;
    148 	}
    149 
    150 	return ra;
    151 }
    152 
    153 void
    154 uvm_ra_freectx(struct uvm_ractx *ra)
    155 {
    156 
    157 	KASSERT(ra != NULL);
    158 	ra_freectx(ra);
    159 }
    160 
    161 void
    162 uvm_ra_request(struct uvm_ractx *ra, struct uvm_object *uobj,
    163     off_t reqoff, size_t reqsize)
    164 {
    165 
    166 	if (ra == NULL) {
    167 		return;
    168 	}
    169 
    170 	switch (ra->ra_advice) {
    171 	case POSIX_FADV_NORMAL:
    172 		break;
    173 
    174 	case POSIX_FADV_RANDOM:
    175 		return;
    176 
    177 	case POSIX_FADV_SEQUENTIAL:
    178 		if (reqoff <= ra->ra_winstart) {
    179 			ra->ra_next = reqoff;
    180 		}
    181 		ra->ra_winsize = RA_WINSIZE_SEQENTIAL;
    182 		goto do_readahead;
    183 
    184 	default:
    185 #if defined(DIAGNOSTIC)
    186 		panic("%s: unknown advice %d", __func__, ra->ra_advice);
    187 #endif /* defined(DIAGNOSTIC) */
    188 		break;
    189 	}
    190 
    191 	if ((ra->ra_flags & RA_VALID) == 0) {
    192 initialize:
    193 		ra->ra_winstart = ra->ra_next = reqoff + reqsize;
    194 		ra->ra_winsize = RA_WINSIZE_INIT;
    195 		ra->ra_flags |= RA_VALID;
    196 		return;
    197 	}
    198 
    199 	if (reqoff < ra->ra_winstart ||
    200 	    ra->ra_winstart + ra->ra_winsize < reqoff) {
    201 
    202 		/*
    203 		 * miss
    204 		 */
    205 
    206 		goto initialize;
    207 	}
    208 
    209 	/*
    210 	 * hit
    211 	 */
    212 
    213 do_readahead:
    214 	if (reqoff > ra->ra_next) {
    215 		ra->ra_next = reqoff;
    216 	}
    217 
    218 	if (reqoff + ra->ra_winsize > ra->ra_next) {
    219 		off_t raoff = MAX(reqoff, ra->ra_next);
    220 		size_t rasize = reqoff + ra->ra_winsize - ra->ra_next;
    221 
    222 		if (rasize >= RA_MINSIZE) {
    223 			ra->ra_next = ra_startio(uobj, raoff, rasize);
    224 		}
    225 	}
    226 
    227 	/*
    228 	 * update window
    229 	 */
    230 
    231 	ra->ra_winstart = reqoff + reqsize;
    232 	ra->ra_winsize = MIN(RA_WINSIZE_MAX, ra->ra_winsize + reqsize);
    233 }
    234