Home | History | Annotate | Line # | Download | only in kern
bufq_readprio.c revision 1.8
      1 /*	$NetBSD: bufq_readprio.c,v 1.8 2007/04/04 02:30:43 wrstuden Exp $	*/
      2 /*	NetBSD: subr_disk.c,v 1.61 2004/09/25 03:30:44 thorpej Exp 	*/
      3 
      4 /*-
      5  * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  * NASA Ames Research Center.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the NetBSD
     23  *	Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * Copyright (c) 1982, 1986, 1988, 1993
     43  *	The Regents of the University of California.  All rights reserved.
     44  * (c) UNIX System Laboratories, Inc.
     45  * All or some portions of this file are derived from material licensed
     46  * to the University of California by American Telephone and Telegraph
     47  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     48  * the permission of UNIX System Laboratories, Inc.
     49  *
     50  * Redistribution and use in source and binary forms, with or without
     51  * modification, are permitted provided that the following conditions
     52  * are met:
     53  * 1. Redistributions of source code must retain the above copyright
     54  *    notice, this list of conditions and the following disclaimer.
     55  * 2. Redistributions in binary form must reproduce the above copyright
     56  *    notice, this list of conditions and the following disclaimer in the
     57  *    documentation and/or other materials provided with the distribution.
     58  * 3. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     75  */
     76 
     77 #include <sys/cdefs.h>
     78 __KERNEL_RCSID(0, "$NetBSD: bufq_readprio.c,v 1.8 2007/04/04 02:30:43 wrstuden Exp $");
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/buf.h>
     83 #include <sys/bufq.h>
     84 #include <sys/bufq_impl.h>
     85 #include <sys/malloc.h>
     86 
     87 /*
     88  * Seek sort for disks.
     89  *
     90  * There are two queues.  The first queue holds read requests; the second
     91  * holds write requests.  The read queue is first-come first-served; the
     92  * write queue is sorted in ascendening block order.
     93  * The read queue is processed first.  After PRIO_READ_BURST consecutive
     94  * read requests with non-empty write queue PRIO_WRITE_REQ requests from
     95  * the write queue will be processed.
     96  */
     97 
     98 #define PRIO_READ_BURST		48
     99 #define PRIO_WRITE_REQ		16
    100 
    101 struct bufq_prio {
    102 	TAILQ_HEAD(, buf) bq_read, bq_write; /* actual list of buffers */
    103 	struct buf *bq_write_next;	/* next request in bq_write */
    104 	struct buf *bq_next;		/* current request */
    105 	int bq_read_burst;		/* # of consecutive reads */
    106 };
    107 
    108 static void bufq_readprio_init(struct bufq_state *);
    109 static void bufq_prio_put(struct bufq_state *, struct buf *);
    110 static struct buf *bufq_prio_get(struct bufq_state *, int);
    111 
    112 BUFQ_DEFINE(readprio, 30, bufq_readprio_init);
    113 
    114 static void
    115 bufq_prio_put(struct bufq_state *bufq, struct buf *bp)
    116 {
    117 	struct bufq_prio *prio = bufq->bq_private;
    118 	struct buf *bq;
    119 	int sortby;
    120 
    121 	sortby = bufq->bq_flags & BUFQ_SORT_MASK;
    122 
    123 	/*
    124 	 * If it's a read request append it to the list.
    125 	 */
    126 	if ((bp->b_flags & B_READ) == B_READ) {
    127 		TAILQ_INSERT_TAIL(&prio->bq_read, bp, b_actq);
    128 		return;
    129 	}
    130 
    131 	bq = TAILQ_FIRST(&prio->bq_write);
    132 
    133 	/*
    134 	 * If the write list is empty, simply append it to the list.
    135 	 */
    136 	if (bq == NULL) {
    137 		TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
    138 		prio->bq_write_next = bp;
    139 		return;
    140 	}
    141 
    142 	/*
    143 	 * If we lie after the next request, insert after this request.
    144 	 */
    145 	if (buf_inorder(prio->bq_write_next, bp, sortby))
    146 		bq = prio->bq_write_next;
    147 
    148 	/*
    149 	 * Search for the first request at a larger block number.
    150 	 * We go before this request if it exists.
    151 	 */
    152 	while (bq != NULL && buf_inorder(bq, bp, sortby))
    153 		bq = TAILQ_NEXT(bq, b_actq);
    154 
    155 	if (bq != NULL)
    156 		TAILQ_INSERT_BEFORE(bq, bp, b_actq);
    157 	else
    158 		TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
    159 }
    160 
    161 static struct buf *
    162 bufq_prio_get(struct bufq_state *bufq, int remove)
    163 {
    164 	struct bufq_prio *prio = bufq->bq_private;
    165 	struct buf *bp;
    166 
    167 	/*
    168 	 * If no current request, get next from the lists.
    169 	 */
    170 	if (prio->bq_next == NULL) {
    171 		/*
    172 		 * If at least one list is empty, select the other.
    173 		 */
    174 		if (TAILQ_FIRST(&prio->bq_read) == NULL) {
    175 			prio->bq_next = prio->bq_write_next;
    176 			prio->bq_read_burst = 0;
    177 		} else if (prio->bq_write_next == NULL) {
    178 			bp = prio->bq_next = TAILQ_FIRST(&prio->bq_read);
    179 			prio->bq_read_burst = 0;
    180 			KASSERT((bp == NULL)
    181 					|| ((bp->b_flags & B_READ) == B_READ));
    182 		} else {
    183 			/*
    184 			 * Both list have requests.  Select the read list up
    185 			 * to PRIO_READ_BURST times, then select the write
    186 			 * list PRIO_WRITE_REQ times.
    187 			 */
    188 			if (prio->bq_read_burst++ < PRIO_READ_BURST)
    189 				prio->bq_next = TAILQ_FIRST(&prio->bq_read);
    190 			else if (prio->bq_read_burst <
    191 			    PRIO_READ_BURST + PRIO_WRITE_REQ)
    192 				prio->bq_next = prio->bq_write_next;
    193 			else {
    194 				prio->bq_next = TAILQ_FIRST(&prio->bq_read);
    195 				prio->bq_read_burst = 0;
    196 			}
    197 		}
    198 	}
    199 
    200 	bp = prio->bq_next;
    201 
    202 	if (bp != NULL && remove) {
    203 		if ((bp->b_flags & B_READ) == B_READ)
    204 			TAILQ_REMOVE(&prio->bq_read, bp, b_actq);
    205 		else {
    206 			/*
    207 			 * Advance the write pointer before removing
    208 			 * bp since it is actually prio->bq_write_next.
    209 			 */
    210 			prio->bq_write_next =
    211 			    TAILQ_NEXT(prio->bq_write_next, b_actq);
    212 			TAILQ_REMOVE(&prio->bq_write, bp, b_actq);
    213 			if (prio->bq_write_next == NULL)
    214 				prio->bq_write_next =
    215 				    TAILQ_FIRST(&prio->bq_write);
    216 		}
    217 
    218 		prio->bq_next = NULL;
    219 	}
    220 
    221 	return (bp);
    222 }
    223 
    224 static void
    225 bufq_readprio_init(struct bufq_state *bufq)
    226 {
    227 	struct bufq_prio *prio;
    228 
    229 	bufq->bq_get = bufq_prio_get;
    230 	bufq->bq_put = bufq_prio_put;
    231 	bufq->bq_private = malloc(sizeof(struct bufq_prio), M_DEVBUF, M_ZERO);
    232 	prio = (struct bufq_prio *)bufq->bq_private;
    233 	TAILQ_INIT(&prio->bq_read);
    234 	TAILQ_INIT(&prio->bq_write);
    235 }
    236 
    237