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