Home | History | Annotate | Line # | Download | only in kern
bufq_priocscan.c revision 1.10.40.1
      1  1.10.40.1     yamt /*	$NetBSD: bufq_priocscan.c,v 1.10.40.1 2008/05/18 12:35:06 yamt Exp $	*/
      2        1.1     yamt 
      3        1.1     yamt /*-
      4        1.1     yamt  * Copyright (c)2004 YAMAMOTO Takashi,
      5        1.1     yamt  * All rights reserved.
      6        1.1     yamt  *
      7        1.1     yamt  * Redistribution and use in source and binary forms, with or without
      8        1.1     yamt  * modification, are permitted provided that the following conditions
      9        1.1     yamt  * are met:
     10        1.1     yamt  * 1. Redistributions of source code must retain the above copyright
     11        1.1     yamt  *    notice, this list of conditions and the following disclaimer.
     12        1.1     yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1     yamt  *    notice, this list of conditions and the following disclaimer in the
     14        1.1     yamt  *    documentation and/or other materials provided with the distribution.
     15        1.1     yamt  *
     16        1.1     yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17        1.1     yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18        1.1     yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19        1.1     yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20        1.1     yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21        1.1     yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22        1.1     yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23        1.1     yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24        1.1     yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1     yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1     yamt  * SUCH DAMAGE.
     27        1.1     yamt  */
     28        1.1     yamt 
     29        1.1     yamt #include <sys/cdefs.h>
     30  1.10.40.1     yamt __KERNEL_RCSID(0, "$NetBSD: bufq_priocscan.c,v 1.10.40.1 2008/05/18 12:35:06 yamt Exp $");
     31        1.1     yamt 
     32        1.1     yamt #include <sys/param.h>
     33        1.1     yamt #include <sys/systm.h>
     34        1.1     yamt #include <sys/buf.h>
     35        1.2     yamt #include <sys/bufq.h>
     36        1.5     yamt #include <sys/bufq_impl.h>
     37        1.1     yamt #include <sys/malloc.h>
     38        1.1     yamt 
     39        1.1     yamt /*
     40        1.1     yamt  * Cyclical scan (CSCAN)
     41        1.1     yamt  */
     42        1.1     yamt TAILQ_HEAD(bqhead, buf);
     43        1.1     yamt struct cscan_queue {
     44        1.1     yamt 	struct bqhead cq_head[2];	/* actual lists of buffers */
     45        1.1     yamt 	int cq_idx;			/* current list index */
     46        1.1     yamt 	int cq_lastcylinder;		/* b_cylinder of the last request */
     47        1.1     yamt 	daddr_t cq_lastrawblkno;	/* b_rawblkno of the last request */
     48        1.1     yamt };
     49        1.1     yamt 
     50        1.8     yamt static inline int cscan_empty(const struct cscan_queue *);
     51        1.1     yamt static void cscan_put(struct cscan_queue *, struct buf *, int);
     52        1.1     yamt static struct buf *cscan_get(struct cscan_queue *, int);
     53        1.1     yamt static void cscan_init(struct cscan_queue *);
     54        1.1     yamt 
     55        1.7    perry static inline int
     56        1.1     yamt cscan_empty(const struct cscan_queue *q)
     57        1.1     yamt {
     58        1.1     yamt 
     59        1.1     yamt 	return TAILQ_EMPTY(&q->cq_head[0]) && TAILQ_EMPTY(&q->cq_head[1]);
     60        1.1     yamt }
     61        1.1     yamt 
     62        1.1     yamt static void
     63        1.1     yamt cscan_put(struct cscan_queue *q, struct buf *bp, int sortby)
     64        1.1     yamt {
     65        1.1     yamt 	struct buf tmp;
     66        1.1     yamt 	struct buf *it;
     67        1.1     yamt 	struct bqhead *bqh;
     68        1.1     yamt 	int idx;
     69        1.1     yamt 
     70        1.1     yamt 	tmp.b_cylinder = q->cq_lastcylinder;
     71        1.1     yamt 	tmp.b_rawblkno = q->cq_lastrawblkno;
     72        1.1     yamt 
     73        1.1     yamt 	if (buf_inorder(bp, &tmp, sortby))
     74        1.1     yamt 		idx = 1 - q->cq_idx;
     75        1.1     yamt 	else
     76        1.1     yamt 		idx = q->cq_idx;
     77        1.1     yamt 
     78        1.1     yamt 	bqh = &q->cq_head[idx];
     79        1.1     yamt 
     80        1.1     yamt 	TAILQ_FOREACH(it, bqh, b_actq)
     81        1.1     yamt 		if (buf_inorder(bp, it, sortby))
     82        1.1     yamt 			break;
     83        1.1     yamt 
     84        1.1     yamt 	if (it != NULL)
     85        1.1     yamt 		TAILQ_INSERT_BEFORE(it, bp, b_actq);
     86        1.1     yamt 	else
     87        1.1     yamt 		TAILQ_INSERT_TAIL(bqh, bp, b_actq);
     88        1.1     yamt }
     89        1.1     yamt 
     90        1.1     yamt static struct buf *
     91        1.1     yamt cscan_get(struct cscan_queue *q, int remove)
     92        1.1     yamt {
     93        1.1     yamt 	int idx = q->cq_idx;
     94        1.1     yamt 	struct bqhead *bqh;
     95        1.1     yamt 	struct buf *bp;
     96        1.1     yamt 
     97        1.1     yamt 	bqh = &q->cq_head[idx];
     98        1.1     yamt 	bp = TAILQ_FIRST(bqh);
     99        1.1     yamt 
    100        1.1     yamt 	if (bp == NULL) {
    101        1.1     yamt 		/* switch queue */
    102        1.1     yamt 		idx = 1 - idx;
    103        1.1     yamt 		bqh = &q->cq_head[idx];
    104        1.1     yamt 		bp = TAILQ_FIRST(bqh);
    105        1.1     yamt 	}
    106        1.1     yamt 
    107        1.1     yamt 	KDASSERT((bp != NULL && !cscan_empty(q)) ||
    108        1.1     yamt 	         (bp == NULL && cscan_empty(q)));
    109        1.1     yamt 
    110        1.1     yamt 	if (bp != NULL && remove) {
    111        1.1     yamt 		q->cq_idx = idx;
    112        1.1     yamt 		TAILQ_REMOVE(bqh, bp, b_actq);
    113        1.1     yamt 
    114        1.1     yamt 		q->cq_lastcylinder = bp->b_cylinder;
    115        1.1     yamt 		q->cq_lastrawblkno =
    116        1.1     yamt 		    bp->b_rawblkno + (bp->b_bcount >> DEV_BSHIFT);
    117        1.1     yamt 	}
    118        1.1     yamt 
    119        1.1     yamt 	return (bp);
    120        1.1     yamt }
    121        1.1     yamt 
    122        1.1     yamt static void
    123        1.1     yamt cscan_init(struct cscan_queue *q)
    124        1.1     yamt {
    125        1.1     yamt 
    126        1.1     yamt 	TAILQ_INIT(&q->cq_head[0]);
    127        1.1     yamt 	TAILQ_INIT(&q->cq_head[1]);
    128        1.1     yamt }
    129        1.1     yamt 
    130        1.1     yamt 
    131        1.1     yamt /*
    132        1.1     yamt  * Per-prioritiy CSCAN.
    133        1.1     yamt  *
    134        1.1     yamt  * XXX probably we should have a way to raise
    135        1.1     yamt  * priority of the on-queue requests.
    136        1.1     yamt  */
    137        1.1     yamt #define	PRIOCSCAN_NQUEUE	3
    138        1.1     yamt 
    139        1.1     yamt struct priocscan_queue {
    140        1.1     yamt 	struct cscan_queue q_queue;
    141        1.1     yamt 	int q_burst;
    142        1.1     yamt };
    143        1.1     yamt 
    144        1.1     yamt struct bufq_priocscan {
    145        1.1     yamt 	struct priocscan_queue bq_queue[PRIOCSCAN_NQUEUE];
    146        1.1     yamt 
    147        1.1     yamt #if 0
    148        1.1     yamt 	/*
    149        1.1     yamt 	 * XXX using "global" head position can reduce positioning time
    150        1.1     yamt 	 * when switching between queues.
    151        1.1     yamt 	 * although it might affect against fairness.
    152        1.1     yamt 	 */
    153        1.1     yamt 	daddr_t bq_lastrawblkno;
    154        1.1     yamt 	int bq_lastcylinder;
    155        1.1     yamt #endif
    156        1.1     yamt };
    157        1.1     yamt 
    158        1.1     yamt /*
    159        1.1     yamt  * how many requests to serve when having pending requests on other queues.
    160        1.1     yamt  *
    161        1.1     yamt  * XXX tune
    162  1.10.40.1     yamt  * be careful: while making these values larger likely
    163  1.10.40.1     yamt  * increases the total throughput, it can also increase latencies
    164  1.10.40.1     yamt  * for some workloads.
    165        1.1     yamt  */
    166        1.1     yamt const int priocscan_burst[] = {
    167        1.1     yamt 	64, 16, 4
    168        1.1     yamt };
    169        1.1     yamt 
    170        1.3     yamt static void bufq_priocscan_init(struct bufq_state *);
    171        1.1     yamt static void bufq_priocscan_put(struct bufq_state *, struct buf *);
    172        1.1     yamt static struct buf *bufq_priocscan_get(struct bufq_state *, int);
    173        1.3     yamt 
    174        1.5     yamt BUFQ_DEFINE(priocscan, 40, bufq_priocscan_init);
    175        1.3     yamt 
    176        1.7    perry static inline struct cscan_queue *bufq_priocscan_selectqueue(
    177        1.1     yamt     struct bufq_priocscan *, const struct buf *);
    178        1.1     yamt 
    179        1.7    perry static inline struct cscan_queue *
    180        1.1     yamt bufq_priocscan_selectqueue(struct bufq_priocscan *q, const struct buf *bp)
    181        1.1     yamt {
    182        1.1     yamt 	static const int priocscan_priomap[] = {
    183        1.1     yamt 		[BPRIO_TIMENONCRITICAL] = 2,
    184        1.1     yamt 		[BPRIO_TIMELIMITED] = 1,
    185        1.1     yamt 		[BPRIO_TIMECRITICAL] = 0
    186        1.1     yamt 	};
    187        1.1     yamt 
    188        1.1     yamt 	return &q->bq_queue[priocscan_priomap[BIO_GETPRIO(bp)]].q_queue;
    189        1.1     yamt }
    190        1.1     yamt 
    191        1.1     yamt static void
    192        1.1     yamt bufq_priocscan_put(struct bufq_state *bufq, struct buf *bp)
    193        1.1     yamt {
    194        1.1     yamt 	struct bufq_priocscan *q = bufq->bq_private;
    195        1.1     yamt 	struct cscan_queue *cq;
    196        1.1     yamt 	const int sortby = bufq->bq_flags & BUFQ_SORT_MASK;
    197        1.1     yamt 
    198        1.1     yamt 	cq = bufq_priocscan_selectqueue(q, bp);
    199        1.1     yamt 	cscan_put(cq, bp, sortby);
    200        1.1     yamt }
    201        1.1     yamt 
    202        1.1     yamt static struct buf *
    203        1.1     yamt bufq_priocscan_get(struct bufq_state *bufq, int remove)
    204        1.1     yamt {
    205        1.1     yamt 	struct bufq_priocscan *q = bufq->bq_private;
    206        1.1     yamt 	struct priocscan_queue *pq, *npq;
    207        1.1     yamt 	struct priocscan_queue *first; /* first non-empty queue */
    208        1.1     yamt 	const struct priocscan_queue *epq;
    209        1.1     yamt 	const struct cscan_queue *cq;
    210        1.1     yamt 	struct buf *bp;
    211        1.9  thorpej 	bool single; /* true if there's only one non-empty queue */
    212        1.1     yamt 
    213        1.1     yamt 	pq = &q->bq_queue[0];
    214        1.1     yamt 	epq = pq + PRIOCSCAN_NQUEUE;
    215        1.1     yamt 	for (; pq < epq; pq++) {
    216        1.1     yamt 		cq = &pq->q_queue;
    217        1.1     yamt 		if (!cscan_empty(cq))
    218        1.1     yamt 			break;
    219        1.1     yamt 	}
    220        1.1     yamt 	if (pq == epq) {
    221        1.1     yamt 		/* there's no requests */
    222        1.1     yamt 		return NULL;
    223        1.1     yamt 	}
    224        1.1     yamt 
    225        1.1     yamt 	first = pq;
    226       1.10  thorpej 	single = true;
    227        1.1     yamt 	for (npq = first + 1; npq < epq; npq++) {
    228        1.1     yamt 		cq = &npq->q_queue;
    229        1.1     yamt 		if (!cscan_empty(cq)) {
    230       1.10  thorpej 			single = false;
    231        1.1     yamt 			if (pq->q_burst > 0)
    232        1.1     yamt 				break;
    233        1.1     yamt 			pq = npq;
    234        1.1     yamt 		}
    235        1.1     yamt 	}
    236        1.1     yamt 	if (single) {
    237        1.1     yamt 		/*
    238        1.1     yamt 		 * there's only a non-empty queue.  just serve it.
    239        1.1     yamt 		 */
    240        1.1     yamt 		pq = first;
    241        1.1     yamt 	} else if (pq->q_burst > 0) {
    242        1.1     yamt 		/*
    243        1.1     yamt 		 * XXX account only by number of requests.  is it good enough?
    244        1.1     yamt 		 */
    245        1.4     yamt 		if (remove) {
    246        1.4     yamt 			pq->q_burst--;
    247        1.4     yamt 		}
    248        1.1     yamt 	} else {
    249        1.1     yamt 		/*
    250        1.1     yamt 		 * no queue was selected due to burst counts
    251        1.1     yamt 		 */
    252        1.1     yamt 		int i;
    253        1.1     yamt #ifdef DEBUG
    254        1.1     yamt 		for (i = 0; i < PRIOCSCAN_NQUEUE; i++) {
    255        1.1     yamt 			pq = &q->bq_queue[i];
    256        1.1     yamt 			cq = &pq->q_queue;
    257        1.1     yamt 			if (!cscan_empty(cq) && pq->q_burst)
    258        1.1     yamt 				panic("%s: inconsist", __func__);
    259        1.1     yamt 		}
    260        1.1     yamt #endif /* DEBUG */
    261        1.1     yamt 
    262        1.1     yamt 		/*
    263        1.1     yamt 		 * reset burst counts
    264        1.1     yamt 		 */
    265        1.4     yamt 		if (remove) {
    266        1.4     yamt 			for (i = 0; i < PRIOCSCAN_NQUEUE; i++) {
    267        1.4     yamt 				pq = &q->bq_queue[i];
    268        1.4     yamt 				pq->q_burst = priocscan_burst[i];
    269        1.4     yamt 			}
    270        1.1     yamt 		}
    271        1.1     yamt 
    272        1.1     yamt 		/*
    273        1.1     yamt 		 * serve first non-empty queue.
    274        1.1     yamt 		 */
    275        1.1     yamt 		pq = first;
    276        1.1     yamt 	}
    277        1.1     yamt 
    278        1.1     yamt 	KDASSERT(!cscan_empty(&pq->q_queue));
    279        1.1     yamt 	bp = cscan_get(&pq->q_queue, remove);
    280        1.1     yamt 	KDASSERT(bp != NULL);
    281        1.1     yamt 	KDASSERT(&pq->q_queue == bufq_priocscan_selectqueue(q, bp));
    282        1.1     yamt 
    283        1.1     yamt 	return bp;
    284        1.1     yamt }
    285        1.1     yamt 
    286  1.10.40.1     yamt static struct buf *
    287  1.10.40.1     yamt bufq_priocscan_cancel(struct bufq_state *bufq, struct buf *buf)
    288  1.10.40.1     yamt {
    289  1.10.40.1     yamt 	struct cscan_queue *q = bufq->bq_private;
    290  1.10.40.1     yamt 	struct bqhead *bqh;
    291  1.10.40.1     yamt 	struct buf *bq;
    292  1.10.40.1     yamt 	int idx;
    293  1.10.40.1     yamt 
    294  1.10.40.1     yamt 	for (idx = 0; idx < 2; idx++) {
    295  1.10.40.1     yamt 		bqh = &q->cq_head[idx];
    296  1.10.40.1     yamt 		bq = TAILQ_FIRST(bqh);
    297  1.10.40.1     yamt 		while (bq) {
    298  1.10.40.1     yamt 			if (bq == buf) {
    299  1.10.40.1     yamt 				TAILQ_REMOVE(bqh, bq, b_actq);
    300  1.10.40.1     yamt 				return buf;
    301  1.10.40.1     yamt 			}
    302  1.10.40.1     yamt 			bq = TAILQ_NEXT(bq, b_actq);
    303  1.10.40.1     yamt 		}
    304  1.10.40.1     yamt 	}
    305  1.10.40.1     yamt 	return NULL;
    306  1.10.40.1     yamt }
    307  1.10.40.1     yamt 
    308        1.3     yamt static void
    309        1.1     yamt bufq_priocscan_init(struct bufq_state *bufq)
    310        1.1     yamt {
    311        1.1     yamt 	struct bufq_priocscan *q;
    312        1.1     yamt 	int i;
    313        1.1     yamt 
    314        1.1     yamt 	bufq->bq_get = bufq_priocscan_get;
    315        1.1     yamt 	bufq->bq_put = bufq_priocscan_put;
    316  1.10.40.1     yamt 	bufq->bq_cancel = bufq_priocscan_cancel;
    317        1.1     yamt 	bufq->bq_private = malloc(sizeof(struct bufq_priocscan),
    318        1.1     yamt 	    M_DEVBUF, M_ZERO);
    319        1.1     yamt 
    320        1.1     yamt 	q = bufq->bq_private;
    321        1.1     yamt 	for (i = 0; i < PRIOCSCAN_NQUEUE; i++) {
    322        1.1     yamt 		struct cscan_queue *cq = &q->bq_queue[i].q_queue;
    323        1.1     yamt 
    324        1.1     yamt 		cscan_init(cq);
    325        1.1     yamt 	}
    326        1.1     yamt }
    327