Home | History | Annotate | Line # | Download | only in kern
bufq_priocscan.c revision 1.1
      1 /*	$NetBSD: bufq_priocscan.c,v 1.1 2004/10/14 05:12:28 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2004 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: bufq_priocscan.c,v 1.1 2004/10/14 05:12:28 yamt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/buf.h>
     35 #include <sys/malloc.h>
     36 
     37 /*
     38  * Cyclical scan (CSCAN)
     39  */
     40 TAILQ_HEAD(bqhead, buf);
     41 struct cscan_queue {
     42 	struct bqhead cq_head[2];	/* actual lists of buffers */
     43 	int cq_idx;			/* current list index */
     44 	int cq_lastcylinder;		/* b_cylinder of the last request */
     45 	daddr_t cq_lastrawblkno;	/* b_rawblkno of the last request */
     46 };
     47 
     48 static int __inline cscan_empty(const struct cscan_queue *);
     49 static void cscan_put(struct cscan_queue *, struct buf *, int);
     50 static struct buf *cscan_get(struct cscan_queue *, int);
     51 static void cscan_init(struct cscan_queue *);
     52 
     53 static __inline int
     54 cscan_empty(const struct cscan_queue *q)
     55 {
     56 
     57 	return TAILQ_EMPTY(&q->cq_head[0]) && TAILQ_EMPTY(&q->cq_head[1]);
     58 }
     59 
     60 static void
     61 cscan_put(struct cscan_queue *q, struct buf *bp, int sortby)
     62 {
     63 	struct buf tmp;
     64 	struct buf *it;
     65 	struct bqhead *bqh;
     66 	int idx;
     67 
     68 	tmp.b_cylinder = q->cq_lastcylinder;
     69 	tmp.b_rawblkno = q->cq_lastrawblkno;
     70 
     71 	if (buf_inorder(bp, &tmp, sortby))
     72 		idx = 1 - q->cq_idx;
     73 	else
     74 		idx = q->cq_idx;
     75 
     76 	bqh = &q->cq_head[idx];
     77 
     78 	TAILQ_FOREACH(it, bqh, b_actq)
     79 		if (buf_inorder(bp, it, sortby))
     80 			break;
     81 
     82 	if (it != NULL)
     83 		TAILQ_INSERT_BEFORE(it, bp, b_actq);
     84 	else
     85 		TAILQ_INSERT_TAIL(bqh, bp, b_actq);
     86 }
     87 
     88 static struct buf *
     89 cscan_get(struct cscan_queue *q, int remove)
     90 {
     91 	int idx = q->cq_idx;
     92 	struct bqhead *bqh;
     93 	struct buf *bp;
     94 
     95 	bqh = &q->cq_head[idx];
     96 	bp = TAILQ_FIRST(bqh);
     97 
     98 	if (bp == NULL) {
     99 		/* switch queue */
    100 		idx = 1 - idx;
    101 		bqh = &q->cq_head[idx];
    102 		bp = TAILQ_FIRST(bqh);
    103 	}
    104 
    105 	KDASSERT((bp != NULL && !cscan_empty(q)) ||
    106 	         (bp == NULL && cscan_empty(q)));
    107 
    108 	if (bp != NULL && remove) {
    109 		q->cq_idx = idx;
    110 		TAILQ_REMOVE(bqh, bp, b_actq);
    111 
    112 		q->cq_lastcylinder = bp->b_cylinder;
    113 		q->cq_lastrawblkno =
    114 		    bp->b_rawblkno + (bp->b_bcount >> DEV_BSHIFT);
    115 	}
    116 
    117 	return (bp);
    118 }
    119 
    120 static void
    121 cscan_init(struct cscan_queue *q)
    122 {
    123 
    124 	TAILQ_INIT(&q->cq_head[0]);
    125 	TAILQ_INIT(&q->cq_head[1]);
    126 }
    127 
    128 
    129 /*
    130  * Per-prioritiy CSCAN.
    131  *
    132  * XXX probably we should have a way to raise
    133  * priority of the on-queue requests.
    134  */
    135 #define	PRIOCSCAN_NQUEUE	3
    136 
    137 struct priocscan_queue {
    138 	struct cscan_queue q_queue;
    139 	int q_burst;
    140 };
    141 
    142 struct bufq_priocscan {
    143 	struct priocscan_queue bq_queue[PRIOCSCAN_NQUEUE];
    144 
    145 #if 0
    146 	/*
    147 	 * XXX using "global" head position can reduce positioning time
    148 	 * when switching between queues.
    149 	 * although it might affect against fairness.
    150 	 */
    151 	daddr_t bq_lastrawblkno;
    152 	int bq_lastcylinder;
    153 #endif
    154 };
    155 
    156 /*
    157  * how many requests to serve when having pending requests on other queues.
    158  *
    159  * XXX tune
    160  */
    161 const int priocscan_burst[] = {
    162 	64, 16, 4
    163 };
    164 
    165 static void bufq_priocscan_put(struct bufq_state *, struct buf *);
    166 static struct buf *bufq_priocscan_get(struct bufq_state *, int);
    167 static __inline struct cscan_queue *bufq_priocscan_selectqueue(
    168     struct bufq_priocscan *, const struct buf *);
    169 
    170 static __inline struct cscan_queue *
    171 bufq_priocscan_selectqueue(struct bufq_priocscan *q, const struct buf *bp)
    172 {
    173 	static const int priocscan_priomap[] = {
    174 		[BPRIO_TIMENONCRITICAL] = 2,
    175 		[BPRIO_TIMELIMITED] = 1,
    176 		[BPRIO_TIMECRITICAL] = 0
    177 	};
    178 
    179 	return &q->bq_queue[priocscan_priomap[BIO_GETPRIO(bp)]].q_queue;
    180 }
    181 
    182 static void
    183 bufq_priocscan_put(struct bufq_state *bufq, struct buf *bp)
    184 {
    185 	struct bufq_priocscan *q = bufq->bq_private;
    186 	struct cscan_queue *cq;
    187 	const int sortby = bufq->bq_flags & BUFQ_SORT_MASK;
    188 
    189 	cq = bufq_priocscan_selectqueue(q, bp);
    190 	cscan_put(cq, bp, sortby);
    191 }
    192 
    193 static struct buf *
    194 bufq_priocscan_get(struct bufq_state *bufq, int remove)
    195 {
    196 	struct bufq_priocscan *q = bufq->bq_private;
    197 	struct priocscan_queue *pq, *npq;
    198 	struct priocscan_queue *first; /* first non-empty queue */
    199 	const struct priocscan_queue *epq;
    200 	const struct cscan_queue *cq;
    201 	struct buf *bp;
    202 	boolean_t single; /* true if there's only one non-empty queue */
    203 
    204 	pq = &q->bq_queue[0];
    205 	epq = pq + PRIOCSCAN_NQUEUE;
    206 	for (; pq < epq; pq++) {
    207 		cq = &pq->q_queue;
    208 		if (!cscan_empty(cq))
    209 			break;
    210 	}
    211 	if (pq == epq) {
    212 		/* there's no requests */
    213 		return NULL;
    214 	}
    215 
    216 	first = pq;
    217 	single = TRUE;
    218 	for (npq = first + 1; npq < epq; npq++) {
    219 		cq = &npq->q_queue;
    220 		if (!cscan_empty(cq)) {
    221 			single = FALSE;
    222 			if (pq->q_burst > 0)
    223 				break;
    224 			pq = npq;
    225 		}
    226 	}
    227 	if (single) {
    228 		/*
    229 		 * there's only a non-empty queue.  just serve it.
    230 		 */
    231 		pq = first;
    232 	} else if (pq->q_burst > 0) {
    233 		/*
    234 		 * XXX account only by number of requests.  is it good enough?
    235 		 */
    236 		pq->q_burst--;
    237 	} else {
    238 		/*
    239 		 * no queue was selected due to burst counts
    240 		 */
    241 		int i;
    242 #ifdef DEBUG
    243 		for (i = 0; i < PRIOCSCAN_NQUEUE; i++) {
    244 			pq = &q->bq_queue[i];
    245 			cq = &pq->q_queue;
    246 			if (!cscan_empty(cq) && pq->q_burst)
    247 				panic("%s: inconsist", __func__);
    248 		}
    249 #endif /* DEBUG */
    250 
    251 		/*
    252 		 * reset burst counts
    253 		 */
    254 		for (i = 0; i < PRIOCSCAN_NQUEUE; i++) {
    255 			pq = &q->bq_queue[i];
    256 			pq->q_burst = priocscan_burst[i];
    257 		}
    258 
    259 		/*
    260 		 * serve first non-empty queue.
    261 		 */
    262 		pq = first;
    263 	}
    264 
    265 	KDASSERT(!cscan_empty(&pq->q_queue));
    266 	bp = cscan_get(&pq->q_queue, remove);
    267 	KDASSERT(bp != NULL);
    268 	KDASSERT(&pq->q_queue == bufq_priocscan_selectqueue(q, bp));
    269 
    270 	return bp;
    271 }
    272 
    273 void
    274 bufq_priocscan_init(struct bufq_state *bufq)
    275 {
    276 	struct bufq_priocscan *q;
    277 	int i;
    278 
    279 	bufq->bq_get = bufq_priocscan_get;
    280 	bufq->bq_put = bufq_priocscan_put;
    281 	bufq->bq_private = malloc(sizeof(struct bufq_priocscan),
    282 	    M_DEVBUF, M_ZERO);
    283 
    284 	q = bufq->bq_private;
    285 	for (i = 0; i < PRIOCSCAN_NQUEUE; i++) {
    286 		struct cscan_queue *cq = &q->bq_queue[i].q_queue;
    287 
    288 		cscan_init(cq);
    289 	}
    290 }
    291