-
-
-#if BALANCE_QUEUES
-
-/* XXX move this to a separate file */
-
-/*
- * NOTE: THIS CODE HAS NOT BEEN UPDATED
- * WITH RESPECT TO THE NEW LOCKING MODEL
- */
-
-
-/*
- * Dynamic Scaling of the Buffer Queues
- */
-
-typedef long long blsize_t;
-
-blsize_t MAXNBUF; /* initialize to (sane_size / PAGE_SIZE) */
-/* Global tunable limits */
-blsize_t nbufh; /* number of buffer headers */
-blsize_t nbuflow; /* minimum number of buffer headers required */
-blsize_t nbufhigh; /* maximum number of buffer headers allowed */
-blsize_t nbuftarget; /* preferred number of buffer headers */
-
-/*
- * assertions:
- *
- * 1. 0 < nbuflow <= nbufh <= nbufhigh
- * 2. nbufhigh <= MAXNBUF
- * 3. 0 < nbuflow <= nbuftarget <= nbufhigh
- * 4. nbufh can not be set by sysctl().
- */
-
-/* Per queue tunable limits */
-
-struct bufqlim {
- blsize_t bl_nlow; /* minimum number of buffer headers required */
- blsize_t bl_num; /* number of buffer headers on the queue */
- blsize_t bl_nlhigh; /* maximum number of buffer headers allowed */
- blsize_t bl_target; /* preferred number of buffer headers */
- long bl_stale; /* Seconds after which a buffer is considered stale */
-} bufqlim[BQUEUES];
-
-/*
- * assertions:
- *
- * 1. 0 <= bl_nlow <= bl_num <= bl_nlhigh
- * 2. bl_nlhigh <= MAXNBUF
- * 3. bufqlim[BQ_META].bl_nlow != 0
- * 4. bufqlim[BQ_META].bl_nlow > (number of possible concurrent
- * file system IO operations)
- * 5. bl_num can not be set by sysctl().
- * 6. bl_nhigh <= nbufhigh
- */
-
-/*
- * Rationale:
- * ----------
- * Defining it blsize_t as long permits 2^31 buffer headers per queue.
- * Which can describe (2^31 * PAGE_SIZE) memory per queue.
- *
- * These limits are exported to by means of sysctl().
- * It was decided to define blsize_t as a 64 bit quantity.
- * This will make sure that we will not be required to change it
- * as long as we do not exceed 64 bit address space for the kernel.
- *
- * low and high numbers parameters initialized at compile time
- * and boot arguments can be used to override them. sysctl()
- * would not change the value. sysctl() can get all the values
- * but can set only target. num is the current level.
- *
- * Advantages of having a "bufqscan" thread doing the balancing are,
- * Keep enough bufs on BQ_EMPTY.
- * getnewbuf() by default will always select a buffer from the BQ_EMPTY.
- * getnewbuf() perfoms best if a buffer was found there.
- * Also this minimizes the possibility of starting IO
- * from getnewbuf(). That's a performance win, too.
- *
- * Localize complex logic [balancing as well as time aging]
- * to balancebufq().
- *
- * Simplify getnewbuf() logic by elimination of time aging code.
- */
-
-/*
- * Algorithm:
- * -----------
- * The goal of the dynamic scaling of the buffer queues to to keep
- * the size of the LRU close to bl_target. Buffers on a queue would
- * be time aged.
- *
- * There would be a thread which will be responsible for "balancing"
- * the buffer cache queues.
- *
- * The scan order would be: AGE, LRU, META, EMPTY.
- */
-
-long bufqscanwait = 0;
-
-static void bufqscan_thread();
-static int balancebufq(int q);
-static int btrimempty(int n);
-static __inline__ int initbufqscan(void);
-static __inline__ int nextbufq(int q);
-static void buqlimprt(int all);
-
-
-static __inline__ void
-bufqinc(int q)
-{
- if ((q < 0) || (q >= BQUEUES))
- return;
-
- bufqlim[q].bl_num++;
- return;
-}
-
-static __inline__ void
-bufqdec(int q)
-{
- if ((q < 0) || (q >= BQUEUES))
- return;
-
- bufqlim[q].bl_num--;
- return;
-}
-
-static void
-bufq_balance_thread_init()
-{
-
- if (bufqscanwait++ == 0) {
-
- /* Initalize globals */
- MAXNBUF = (sane_size / PAGE_SIZE);
- nbufh = nbuf;
- nbuflow = min(nbufh, 100);
- nbufhigh = min(MAXNBUF, max(nbufh, 2048));
- nbuftarget = (sane_size >> 5) / PAGE_SIZE;
- nbuftarget = max(nbuflow, nbuftarget);
- nbuftarget = min(nbufhigh, nbuftarget);
-
- /*
- * Initialize the bufqlim
- */
-
- /* LOCKED queue */
- bufqlim[BQ_LOCKED].bl_nlow = 0;
- bufqlim[BQ_LOCKED].bl_nlhigh = 32;
- bufqlim[BQ_LOCKED].bl_target = 0;
- bufqlim[BQ_LOCKED].bl_stale = 30;
-
- /* LRU queue */
- bufqlim[BQ_LRU].bl_nlow = 0;
- bufqlim[BQ_LRU].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_LRU].bl_target = nbuftarget/4;
- bufqlim[BQ_LRU].bl_stale = LRU_IS_STALE;
-
- /* AGE queue */
- bufqlim[BQ_AGE].bl_nlow = 0;
- bufqlim[BQ_AGE].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_AGE].bl_target = nbuftarget/4;
- bufqlim[BQ_AGE].bl_stale = AGE_IS_STALE;
-
- /* EMPTY queue */
- bufqlim[BQ_EMPTY].bl_nlow = 0;
- bufqlim[BQ_EMPTY].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_EMPTY].bl_target = nbuftarget/4;
- bufqlim[BQ_EMPTY].bl_stale = 600000;
-
- /* META queue */
- bufqlim[BQ_META].bl_nlow = 0;
- bufqlim[BQ_META].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_META].bl_target = nbuftarget/4;
- bufqlim[BQ_META].bl_stale = META_IS_STALE;
-
- /* LAUNDRY queue */
- bufqlim[BQ_LOCKED].bl_nlow = 0;
- bufqlim[BQ_LOCKED].bl_nlhigh = 32;
- bufqlim[BQ_LOCKED].bl_target = 0;
- bufqlim[BQ_LOCKED].bl_stale = 30;
-
- buqlimprt(1);
- }
-
- /* create worker thread */
- kernel_thread(kernel_task, bufqscan_thread);
-}
-
-/* The workloop for the buffer balancing thread */
-static void
-bufqscan_thread()
-{
- int moretodo = 0;
-
- for(;;) {
- do {
- int q; /* buffer queue to process */
-
- q = initbufqscan();
- for (; q; ) {
- moretodo |= balancebufq(q);
- q = nextbufq(q);
- }
- } while (moretodo);
-
-#if DIAGNOSTIC
- vfs_bufstats();
- buqlimprt(0);
-#endif
- (void)tsleep((void *)&bufqscanwait, PRIBIO, "bufqscanwait", 60 * hz);
- moretodo = 0;
- }
-}
-
-/* Seed for the buffer queue balancing */
-static __inline__ int
-initbufqscan()
-{
- /* Start with AGE queue */
- return (BQ_AGE);
-}
-
-/* Pick next buffer queue to balance */
-static __inline__ int
-nextbufq(int q)
-{
- int order[] = { BQ_AGE, BQ_LRU, BQ_META, BQ_EMPTY, 0 };
-
- q++;
- q %= sizeof(order);
- return (order[q]);
-}
-
-/* function to balance the buffer queues */
-static int
-balancebufq(int q)
-{
- int moretodo = 0;
- int s = splbio();
- int n, t;
-
- /* reject invalid q */
- if ((q < 0) || (q >= BQUEUES))
- goto out;
-
- /* LOCKED or LAUNDRY queue MUST not be balanced */
- if ((q == BQ_LOCKED) || (q == BQ_LAUNDRY))
- goto out;
-
- n = (bufqlim[q].bl_num - bufqlim[q].bl_target);
-
- /* If queue has less than target nothing more to do */
- if (n < 0)
- goto out;
-
- if ( n > 8 ) {
- /* Balance only a small amount (12.5%) at a time */
- n >>= 3;
- }
-
- /* EMPTY queue needs special handling */
- if (q == BQ_EMPTY) {
- moretodo |= btrimempty(n);
- goto out;
- }
-
- t = buf_timestamp():
-
- for (; n > 0; n--) {
- struct buf *bp = bufqueues[q].tqh_first;
- if (!bp)
- break;
-
- /* check if it's stale */
- if ((t - bp->b_timestamp) > bufqlim[q].bl_stale) {
- if (bcleanbuf(bp)) {
- /* buf_bawrite() issued, bp not ready */
- moretodo = 1;
- } else {
- /* release the cleaned buffer to BQ_EMPTY */
- SET(bp->b_flags, B_INVAL);
- buf_brelse(bp);
- }
- } else
- break;
- }
-
-out:
- splx(s);
- return (moretodo);
-}
-
-static int
-btrimempty(int n)
-{
- /*
- * When struct buf are allocated dynamically, this would
- * reclaim upto 'n' struct buf from the empty queue.
- */
-
- return (0);
-}
-
-static void
-buqlimprt(int all)
-{
- int i;
- static char *bname[BQUEUES] =
- { "LOCKED", "LRU", "AGE", "EMPTY", "META", "LAUNDRY" };
-
- if (all)
- for (i = 0; i < BQUEUES; i++) {
- printf("%s : ", bname[i]);
- printf("min = %ld, ", (long)bufqlim[i].bl_nlow);
- printf("cur = %ld, ", (long)bufqlim[i].bl_num);
- printf("max = %ld, ", (long)bufqlim[i].bl_nlhigh);
- printf("target = %ld, ", (long)bufqlim[i].bl_target);
- printf("stale after %ld seconds\n", bufqlim[i].bl_stale);
- }
- else
- for (i = 0; i < BQUEUES; i++) {
- printf("%s : ", bname[i]);
- printf("cur = %ld, ", (long)bufqlim[i].bl_num);
- }
-}
-
-#endif
-
-