Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys bring in blist from freebsd as-is.



details:   https://anonhg.NetBSD.org/src/rev/2010bcf18cea
branches:  trunk
changeset: 580042:2010bcf18cea
user:      yamt <yamt%NetBSD.org@localhost>
date:      Wed Apr 06 11:32:06 2005 +0000

description:
bring in blist from freebsd as-is.

diffstat:

 sys/kern/subr_blist.c |  1098 +++++++++++++++++++++++++++++++++++++++++++++++++
 sys/sys/blist.h       |   103 ++++
 2 files changed, 1201 insertions(+), 0 deletions(-)

diffs (truncated from 1209 to 300 lines):

diff -r e00550d32708 -r 2010bcf18cea sys/kern/subr_blist.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/kern/subr_blist.c     Wed Apr 06 11:32:06 2005 +0000
@@ -0,0 +1,1098 @@
+/*-
+ * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+/*
+ * BLIST.C -   Bitmap allocator/deallocator, using a radix tree with hinting
+ *
+ *     This module implements a general bitmap allocator/deallocator.  The
+ *     allocator eats around 2 bits per 'block'.  The module does not 
+ *     try to interpret the meaning of a 'block' other then to return 
+ *     SWAPBLK_NONE on an allocation failure.
+ *
+ *     A radix tree is used to maintain the bitmap.  Two radix constants are
+ *     involved:  One for the bitmaps contained in the leaf nodes (typically
+ *     32), and one for the meta nodes (typically 16).  Both meta and leaf
+ *     nodes have a hint field.  This field gives us a hint as to the largest
+ *     free contiguous range of blocks under the node.  It may contain a
+ *     value that is too high, but will never contain a value that is too 
+ *     low.  When the radix tree is searched, allocation failures in subtrees
+ *     update the hint. 
+ *
+ *     The radix tree also implements two collapsed states for meta nodes:
+ *     the ALL-ALLOCATED state and the ALL-FREE state.  If a meta node is
+ *     in either of these two states, all information contained underneath
+ *     the node is considered stale.  These states are used to optimize
+ *     allocation and freeing operations.
+ *
+ *     The hinting greatly increases code efficiency for allocations while
+ *     the general radix structure optimizes both allocations and frees.  The
+ *     radix tree should be able to operate well no matter how much 
+ *     fragmentation there is and no matter how large a bitmap is used.
+ *
+ *     Unlike the rlist code, the blist code wires all necessary memory at
+ *     creation time.  Neither allocations nor frees require interaction with
+ *     the memory subsystem.  In contrast, the rlist code may allocate memory 
+ *     on an rlist_free() call.  The non-blocking features of the blist code
+ *     are used to great advantage in the swap code (vm/nswap_pager.c).  The
+ *     rlist code uses a little less overall memory then the blist code (but
+ *     due to swap interleaving not all that much less), but the blist code 
+ *     scales much, much better.
+ *
+ *     LAYOUT: The radix tree is layed out recursively using a
+ *     linear array.  Each meta node is immediately followed (layed out
+ *     sequentially in memory) by BLIST_META_RADIX lower level nodes.  This
+ *     is a recursive structure but one that can be easily scanned through
+ *     a very simple 'skip' calculation.  In order to support large radixes, 
+ *     portions of the tree may reside outside our memory allocation.  We 
+ *     handle this with an early-termination optimization (when bighint is 
+ *     set to -1) on the scan.  The memory allocation is only large enough 
+ *     to cover the number of blocks requested at creation time even if it
+ *     must be encompassed in larger root-node radix.
+ *
+ *     NOTE: the allocator cannot currently allocate more then 
+ *     BLIST_BMAP_RADIX blocks per call.  It will panic with 'allocation too 
+ *     large' if you try.  This is an area that could use improvement.  The 
+ *     radix is large enough that this restriction does not effect the swap 
+ *     system, though.  Currently only the allocation code is effected by
+ *     this algorithmic unfeature.  The freeing code can handle arbitrary
+ *     ranges.
+ *
+ *     This code can be compiled stand-alone for debugging.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: src/sys/kern/subr_blist.c,v 1.17 2004/06/04 04:03:25 alc Exp $");
+
+#ifdef _KERNEL
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/lock.h>
+#include <sys/kernel.h>
+#include <sys/blist.h>
+#include <sys/malloc.h>
+#include <sys/proc.h>
+#include <sys/mutex.h> 
+
+#else
+
+#ifndef BLIST_NO_DEBUG
+#define BLIST_DEBUG
+#endif
+
+#define SWAPBLK_NONE ((daddr_t)-1)
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#define malloc(a,b,c)  calloc(a, 1)
+#define free(a,b)      free(a)
+
+typedef unsigned int u_daddr_t;
+
+#include <sys/blist.h>
+
+void panic(const char *ctl, ...);
+
+#endif
+
+/*
+ * static support functions
+ */
+
+static daddr_t blst_leaf_alloc(blmeta_t *scan, daddr_t blk, int count);
+static daddr_t blst_meta_alloc(blmeta_t *scan, daddr_t blk, 
+                               daddr_t count, daddr_t radix, int skip);
+static void blst_leaf_free(blmeta_t *scan, daddr_t relblk, int count);
+static void blst_meta_free(blmeta_t *scan, daddr_t freeBlk, daddr_t count, 
+                                       daddr_t radix, int skip, daddr_t blk);
+static void blst_copy(blmeta_t *scan, daddr_t blk, daddr_t radix, 
+                               daddr_t skip, blist_t dest, daddr_t count);
+static int blst_leaf_fill(blmeta_t *scan, daddr_t blk, int count);
+static int blst_meta_fill(blmeta_t *scan, daddr_t allocBlk, daddr_t count,
+                               daddr_t radix, int skip, daddr_t blk);
+static daddr_t blst_radix_init(blmeta_t *scan, daddr_t radix, 
+                                               int skip, daddr_t count);
+#ifndef _KERNEL
+static void    blst_radix_print(blmeta_t *scan, daddr_t blk, 
+                                       daddr_t radix, int skip, int tab);
+#endif
+
+#ifdef _KERNEL
+static MALLOC_DEFINE(M_SWAP, "SWAP", "Swap space");
+#endif
+
+/*
+ * blist_create() - create a blist capable of handling up to the specified
+ *                 number of blocks
+ *
+ *     blocks must be greater then 0
+ *
+ *     The smallest blist consists of a single leaf node capable of 
+ *     managing BLIST_BMAP_RADIX blocks.
+ */
+
+blist_t 
+blist_create(daddr_t blocks)
+{
+       blist_t bl;
+       int radix;
+       int skip = 0;
+
+       /*
+        * Calculate radix and skip field used for scanning.
+        */
+       radix = BLIST_BMAP_RADIX;
+
+       while (radix < blocks) {
+               radix *= BLIST_META_RADIX;
+               skip = (skip + 1) * BLIST_META_RADIX;
+       }
+
+       bl = malloc(sizeof(struct blist), M_SWAP, M_WAITOK | M_ZERO);
+
+       bl->bl_blocks = blocks;
+       bl->bl_radix = radix;
+       bl->bl_skip = skip;
+       bl->bl_rootblks = 1 +
+           blst_radix_init(NULL, bl->bl_radix, bl->bl_skip, blocks);
+       bl->bl_root = malloc(sizeof(blmeta_t) * bl->bl_rootblks, M_SWAP, M_WAITOK);
+
+#if defined(BLIST_DEBUG)
+       printf(
+               "BLIST representing %lld blocks (%lld MB of swap)"
+               ", requiring %lldK of ram\n",
+               (long long)bl->bl_blocks,
+               (long long)bl->bl_blocks * 4 / 1024,
+               (long long)(bl->bl_rootblks * sizeof(blmeta_t) + 1023) / 1024
+       );
+       printf("BLIST raw radix tree contains %lld records\n",
+           (long long)bl->bl_rootblks);
+#endif
+       blst_radix_init(bl->bl_root, bl->bl_radix, bl->bl_skip, blocks);
+
+       return(bl);
+}
+
+void 
+blist_destroy(blist_t bl)
+{
+       free(bl->bl_root, M_SWAP);
+       free(bl, M_SWAP);
+}
+
+/*
+ * blist_alloc() - reserve space in the block bitmap.  Return the base
+ *                  of a contiguous region or SWAPBLK_NONE if space could
+ *                  not be allocated.
+ */
+
+daddr_t 
+blist_alloc(blist_t bl, daddr_t count)
+{
+       daddr_t blk = SWAPBLK_NONE;
+
+       if (bl) {
+               if (bl->bl_radix == BLIST_BMAP_RADIX)
+                       blk = blst_leaf_alloc(bl->bl_root, 0, count);
+               else
+                       blk = blst_meta_alloc(bl->bl_root, 0, count, bl->bl_radix, bl->bl_skip);
+               if (blk != SWAPBLK_NONE)
+                       bl->bl_free -= count;
+       }
+       return(blk);
+}
+
+/*
+ * blist_free() -      free up space in the block bitmap.  Return the base
+ *                     of a contiguous region.  Panic if an inconsistancy is
+ *                     found.
+ */
+
+void 
+blist_free(blist_t bl, daddr_t blkno, daddr_t count)
+{
+       if (bl) {
+               if (bl->bl_radix == BLIST_BMAP_RADIX)
+                       blst_leaf_free(bl->bl_root, blkno, count);
+               else
+                       blst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, bl->bl_skip, 0);
+               bl->bl_free += count;
+       }
+}
+
+/*
+ * blist_fill() -      mark a region in the block bitmap as off-limits
+ *                     to the allocator (i.e. allocate it), ignoring any
+ *                     existing allocations.  Return the number of blocks
+ *                     actually filled that were free before the call.
+ */
+
+int
+blist_fill(blist_t bl, daddr_t blkno, daddr_t count)
+{
+       int filled;
+
+       if (bl) {
+               if (bl->bl_radix == BLIST_BMAP_RADIX)
+                       filled = blst_leaf_fill(bl->bl_root, blkno, count);
+               else
+                       filled = blst_meta_fill(bl->bl_root, blkno, count,
+                           bl->bl_radix, bl->bl_skip, 0);
+               bl->bl_free -= filled;
+               return filled;
+       } else
+               return 0;
+}
+
+/*
+ * blist_resize() -    resize an existing radix tree to handle the
+ *                     specified number of blocks.  This will reallocate
+ *                     the tree and transfer the previous bitmap to the new
+ *                     one.  When extending the tree you can specify whether
+ *                     the new blocks are to left allocated or freed.
+ */
+
+void
+blist_resize(blist_t *pbl, daddr_t count, int freenew)
+{
+    blist_t newbl = blist_create(count);
+    blist_t save = *pbl;
+
+    *pbl = newbl;
+    if (count > save->bl_blocks)
+           count = save->bl_blocks;
+    blst_copy(save->bl_root, 0, save->bl_radix, save->bl_skip, newbl, count);
+
+    /*
+     * If resizing upwards, should we free the new space or not?
+     */
+    if (freenew && count < newbl->bl_blocks) {



Home | Main Index | Thread Index | Old Index