3 * BLIST.C - Bitmap allocator/deallocator, using a radix tree with hinting
5 * (c)Copyright 1998, Matthew Dillon. Terms for use and redistribution
6 * are covered by the BSD Copyright as found in /usr/src/COPYRIGHT.
8 * This module implements a general bitmap allocator/deallocator. The
9 * allocator eats around 2 bits per 'block'. The module does not
10 * try to interpret the meaning of a 'block' other then to return
11 * SWAPBLK_NONE on an allocation failure.
13 * A radix tree is used to maintain the bitmap. Two radix constants are
14 * involved: One for the bitmaps contained in the leaf nodes (typically
15 * 32), and one for the meta nodes (typically 16). Both meta and leaf
16 * nodes have a hint field. This field gives us a hint as to the largest
17 * free contiguous range of blocks under the node. It may contain a
18 * value that is too high, but will never contain a value that is too
19 * low. When the radix tree is searched, allocation failures in subtrees
22 * The radix tree also implements two collapsed states for meta nodes:
23 * the ALL-ALLOCATED state and the ALL-FREE state. If a meta node is
24 * in either of these two states, all information contained underneath
25 * the node is considered stale. These states are used to optimize
26 * allocation and freeing operations.
28 * The hinting greatly increases code efficiency for allocations while
29 * the general radix structure optimizes both allocations and frees. The
30 * radix tree should be able to operate well no matter how much
31 * fragmentation there is and no matter how large a bitmap is used.
33 * Unlike the rlist code, the blist code wires all necessary memory at
34 * creation time. Neither allocations nor frees require interaction with
35 * the memory subsystem. In contrast, the rlist code may allocate memory
36 * on an rlist_free() call. The non-blocking features of the blist code
37 * are used to great advantage in the swap code (vm/nswap_pager.c). The
38 * rlist code uses a little less overall memory then the blist code (but
39 * due to swap interleaving not all that much less), but the blist code
40 * scales much, much better.
42 * LAYOUT: The radix tree is layed out recursively using a
43 * linear array. Each meta node is immediately followed (layed out
44 * sequentially in memory) by BLIST_META_RADIX lower level nodes. This
45 * is a recursive structure but one that can be easily scanned through
46 * a very simple 'skip' calculation. In order to support large radixes,
47 * portions of the tree may reside outside our memory allocation. We
48 * handle this with an early-termination optimization (when bighint is
49 * set to -1) on the scan. The memory allocation is only large enough
50 * to cover the number of blocks requested at creation time even if it
51 * must be encompassed in larger root-node radix.
53 * NOTE: the allocator cannot currently allocate more then
54 * BLIST_BMAP_RADIX blocks per call. It will panic with 'allocation too
55 * large' if you try. This is an area that could use improvement. The
56 * radix is large enough that this restriction does not effect the swap
57 * system, though. Currently only the allocation code is effected by
58 * this algorithmic unfeature. The freeing code can handle arbitrary
61 * This code can be compiled stand-alone for debugging.
63 * $FreeBSD: src/sys/kern/subr_blist.c,v 1.5.2.2 2003/01/12 09:23:12 dillon Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/blist.h>
73 #include <sys/malloc.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_page.h>
82 #ifndef BLIST_NO_DEBUG
86 #define SWAPBLK_NONE ((daddr_t)-1)
88 #include <sys/types.h>
94 #define malloc(a,b,c) malloc(a)
95 #define free(a,b) free(a)
97 typedef unsigned int u_daddr_t;
99 #include <sys/blist.h>
101 void panic(const char *ctl, ...);
106 * static support functions
109 static daddr_t blst_leaf_alloc(blmeta_t *scan, daddr_t blk, int count);
110 static daddr_t blst_meta_alloc(blmeta_t *scan, daddr_t blk,
111 daddr_t count, daddr_t radix, int skip);
112 static void blst_leaf_free(blmeta_t *scan, daddr_t relblk, int count);
113 static void blst_meta_free(blmeta_t *scan, daddr_t freeBlk, daddr_t count,
114 daddr_t radix, int skip, daddr_t blk);
115 static void blst_copy(blmeta_t *scan, daddr_t blk, daddr_t radix,
116 daddr_t skip, blist_t dest, daddr_t count);
117 static daddr_t blst_radix_init(blmeta_t *scan, daddr_t radix,
118 int skip, daddr_t count);
120 static void blst_radix_print(blmeta_t *scan, daddr_t blk,
121 daddr_t radix, int skip, int tab);
125 static MALLOC_DEFINE(M_SWAP, "SWAP", "Swap space");
129 * blist_create() - create a blist capable of handling up to the specified
132 * blocks must be greater then 0
134 * The smallest blist consists of a single leaf node capable of
135 * managing BLIST_BMAP_RADIX blocks.
139 blist_create(daddr_t blocks)
146 * Calculate radix and skip field used for scanning.
148 radix = BLIST_BMAP_RADIX;
150 while (radix < blocks) {
151 radix *= BLIST_META_RADIX;
152 skip = (skip + 1) * BLIST_META_RADIX;
155 bl = malloc(sizeof(struct blist), M_SWAP, M_WAITOK);
157 bzero(bl, sizeof(*bl));
159 bl->bl_blocks = blocks;
160 bl->bl_radix = radix;
162 bl->bl_rootblks = 1 +
163 blst_radix_init(NULL, bl->bl_radix, bl->bl_skip, blocks);
164 bl->bl_root = malloc(sizeof(blmeta_t) * bl->bl_rootblks, M_SWAP, M_WAITOK);
166 #if defined(BLIST_DEBUG)
168 "BLIST representing %d blocks (%d MB of swap)"
169 ", requiring %dK of ram\n",
171 bl->bl_blocks * 4 / 1024,
172 (bl->bl_rootblks * sizeof(blmeta_t) + 1023) / 1024
174 printf("BLIST raw radix tree contains %d records\n", bl->bl_rootblks);
176 blst_radix_init(bl->bl_root, bl->bl_radix, bl->bl_skip, blocks);
182 blist_destroy(blist_t bl)
184 free(bl->bl_root, M_SWAP);
189 * blist_alloc() - reserve space in the block bitmap. Return the base
190 * of a contiguous region or SWAPBLK_NONE if space could
195 blist_alloc(blist_t bl, daddr_t count)
197 daddr_t blk = SWAPBLK_NONE;
200 if (bl->bl_radix == BLIST_BMAP_RADIX)
201 blk = blst_leaf_alloc(bl->bl_root, 0, count);
203 blk = blst_meta_alloc(bl->bl_root, 0, count, bl->bl_radix, bl->bl_skip);
204 if (blk != SWAPBLK_NONE)
205 bl->bl_free -= count;
211 * blist_free() - free up space in the block bitmap. Return the base
212 * of a contiguous region. Panic if an inconsistancy is
217 blist_free(blist_t bl, daddr_t blkno, daddr_t count)
220 if (bl->bl_radix == BLIST_BMAP_RADIX)
221 blst_leaf_free(bl->bl_root, blkno, count);
223 blst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, bl->bl_skip, 0);
224 bl->bl_free += count;
229 * blist_resize() - resize an existing radix tree to handle the
230 * specified number of blocks. This will reallocate
231 * the tree and transfer the previous bitmap to the new
232 * one. When extending the tree you can specify whether
233 * the new blocks are to left allocated or freed.
237 blist_resize(blist_t *pbl, daddr_t count, int freenew)
239 blist_t newbl = blist_create(count);
243 if (count > save->bl_blocks)
244 count = save->bl_blocks;
245 blst_copy(save->bl_root, 0, save->bl_radix, save->bl_skip, newbl, count);
248 * If resizing upwards, should we free the new space or not?
250 if (freenew && count < newbl->bl_blocks) {
251 blist_free(newbl, count, newbl->bl_blocks - count);
259 * blist_print() - dump radix tree
263 blist_print(blist_t bl)
266 blst_radix_print(bl->bl_root, 0, bl->bl_radix, bl->bl_skip, 4);
272 /************************************************************************
273 * ALLOCATION SUPPORT FUNCTIONS *
274 ************************************************************************
276 * These support functions do all the actual work. They may seem
277 * rather longish, but that's because I've commented them up. The
278 * actual code is straight forward.
283 * blist_leaf_alloc() - allocate at a leaf in the radix tree (a bitmap).
285 * This is the core of the allocator and is optimized for the 1 block
286 * and the BLIST_BMAP_RADIX block allocation cases. Other cases are
287 * somewhat slower. The 1 block allocation case is log2 and extremely
297 u_daddr_t orig = scan->u.bmu_bitmap;
301 * Optimize bitmap all-allocated case. Also, count = 1
302 * case assumes at least 1 bit is free in the bitmap, so
303 * we have to take care of this case here.
305 scan->bm_bighint = 0;
306 return(SWAPBLK_NONE);
310 * Optimized code to allocate one bit out of the bitmap
313 int j = BLIST_BMAP_RADIX/2;
316 mask = (u_daddr_t)-1 >> (BLIST_BMAP_RADIX/2);
319 if ((orig & mask) == 0) {
326 scan->u.bmu_bitmap &= ~(1 << r);
329 if (count <= BLIST_BMAP_RADIX) {
331 * non-optimized code to allocate N bits out of the bitmap.
332 * The more bits, the faster the code runs. It will run
333 * the slowest allocating 2 bits, but since there aren't any
334 * memory ops in the core loop (or shouldn't be, anyway),
335 * you probably won't notice the difference.
338 int n = BLIST_BMAP_RADIX - count;
341 mask = (u_daddr_t)-1 >> n;
343 for (j = 0; j <= n; ++j) {
344 if ((orig & mask) == mask) {
345 scan->u.bmu_bitmap &= ~mask;
352 * We couldn't allocate count in this subtree, update bighint.
354 scan->bm_bighint = count - 1;
355 return(SWAPBLK_NONE);
359 * blist_meta_alloc() - allocate at a meta in the radix tree.
361 * Attempt to allocate at a meta node. If we can't, we update
362 * bighint and return a failure. Updating bighint optimize future
363 * calls that hit this node. We have to check for our collapse cases
364 * and we have a few optimizations strewn in as well.
376 int next_skip = ((u_int)skip / BLIST_META_RADIX);
378 if (scan->u.bmu_avail == 0) {
380 * ALL-ALLOCATED special case
382 scan->bm_bighint = count;
383 return(SWAPBLK_NONE);
386 if (scan->u.bmu_avail == radix) {
387 radix /= BLIST_META_RADIX;
390 * ALL-FREE special case, initialize uninitialize
393 for (i = 1; i <= skip; i += next_skip) {
394 if (scan[i].bm_bighint == (daddr_t)-1)
396 if (next_skip == 1) {
397 scan[i].u.bmu_bitmap = (u_daddr_t)-1;
398 scan[i].bm_bighint = BLIST_BMAP_RADIX;
400 scan[i].bm_bighint = radix;
401 scan[i].u.bmu_avail = radix;
405 radix /= BLIST_META_RADIX;
408 for (i = 1; i <= skip; i += next_skip) {
409 if (count <= scan[i].bm_bighint) {
411 * count fits in object
414 if (next_skip == 1) {
415 r = blst_leaf_alloc(&scan[i], blk, count);
417 r = blst_meta_alloc(&scan[i], blk, count, radix, next_skip - 1);
419 if (r != SWAPBLK_NONE) {
420 scan->u.bmu_avail -= count;
421 if (scan->bm_bighint > scan->u.bmu_avail)
422 scan->bm_bighint = scan->u.bmu_avail;
425 } else if (scan[i].bm_bighint == (daddr_t)-1) {
430 } else if (count > radix) {
432 * count does not fit in object even if it were
435 panic("blist_meta_alloc: allocation too large");
441 * We couldn't allocate count in this subtree, update bighint.
443 if (scan->bm_bighint >= count)
444 scan->bm_bighint = count - 1;
445 return(SWAPBLK_NONE);
449 * BLST_LEAF_FREE() - free allocated block from leaf bitmap
460 * free some data in this bitmap
463 * 0000111111111110000
467 int n = blk & (BLIST_BMAP_RADIX - 1);
470 mask = ((u_daddr_t)-1 << n) &
471 ((u_daddr_t)-1 >> (BLIST_BMAP_RADIX - count - n));
473 if (scan->u.bmu_bitmap & mask)
474 panic("blst_radix_free: freeing free block");
475 scan->u.bmu_bitmap |= mask;
478 * We could probably do a better job here. We are required to make
479 * bighint at least as large as the biggest contiguous block of
480 * data. If we just shoehorn it, a little extra overhead will
481 * be incured on the next allocation (but only that one typically).
483 scan->bm_bighint = BLIST_BMAP_RADIX;
487 * BLST_META_FREE() - free allocated blocks from radix tree meta info
489 * This support routine frees a range of blocks from the bitmap.
490 * The range must be entirely enclosed by this radix node. If a
491 * meta node, we break the range down recursively to free blocks
492 * in subnodes (which means that this code can free an arbitrary
493 * range whereas the allocation code cannot allocate an arbitrary
507 int next_skip = ((u_int)skip / BLIST_META_RADIX);
510 printf("FREE (%x,%d) FROM (%x,%d)\n",
516 if (scan->u.bmu_avail == 0) {
518 * ALL-ALLOCATED special case, with possible
519 * shortcut to ALL-FREE special case.
521 scan->u.bmu_avail = count;
522 scan->bm_bighint = count;
524 if (count != radix) {
525 for (i = 1; i <= skip; i += next_skip) {
526 if (scan[i].bm_bighint == (daddr_t)-1)
528 scan[i].bm_bighint = 0;
529 if (next_skip == 1) {
530 scan[i].u.bmu_bitmap = 0;
532 scan[i].u.bmu_avail = 0;
538 scan->u.bmu_avail += count;
539 /* scan->bm_bighint = radix; */
543 * ALL-FREE special case.
546 if (scan->u.bmu_avail == radix)
548 if (scan->u.bmu_avail > radix)
549 panic("blst_meta_free: freeing already free blocks (%d) %d/%d", count, scan->u.bmu_avail, radix);
552 * Break the free down into its components
555 radix /= BLIST_META_RADIX;
557 i = (freeBlk - blk) / radix;
559 i = i * next_skip + 1;
561 while (i <= skip && blk < freeBlk + count) {
564 v = blk + radix - freeBlk;
568 if (scan->bm_bighint == (daddr_t)-1)
569 panic("blst_meta_free: freeing unexpected range");
571 if (next_skip == 1) {
572 blst_leaf_free(&scan[i], freeBlk, v);
574 blst_meta_free(&scan[i], freeBlk, v, radix, next_skip - 1, blk);
576 if (scan->bm_bighint < scan[i].bm_bighint)
577 scan->bm_bighint = scan[i].bm_bighint;
586 * BLIST_RADIX_COPY() - copy one radix tree to another
588 * Locates free space in the source tree and frees it in the destination
589 * tree. The space may not already be free in the destination.
592 static void blst_copy(
607 if (radix == BLIST_BMAP_RADIX) {
608 u_daddr_t v = scan->u.bmu_bitmap;
610 if (v == (u_daddr_t)-1) {
611 blist_free(dest, blk, count);
615 for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) {
617 blist_free(dest, blk + i, 1);
627 if (scan->u.bmu_avail == 0) {
629 * Source all allocated, leave dest allocated
633 if (scan->u.bmu_avail == radix) {
635 * Source all free, free entire dest
638 blist_free(dest, blk, count);
640 blist_free(dest, blk, radix);
645 radix /= BLIST_META_RADIX;
646 next_skip = ((u_int)skip / BLIST_META_RADIX);
648 for (i = 1; count && i <= skip; i += next_skip) {
649 if (scan[i].bm_bighint == (daddr_t)-1)
652 if (count >= radix) {
680 * BLST_RADIX_INIT() - initialize radix tree
682 * Initialize our meta structures and bitmaps and calculate the exact
683 * amount of space required to manage 'count' blocks - this space may
684 * be considerably less then the calculated radix due to the large
685 * RADIX values we use.
689 blst_radix_init(blmeta_t *scan, daddr_t radix, int skip, daddr_t count)
693 daddr_t memindex = 0;
699 if (radix == BLIST_BMAP_RADIX) {
701 scan->bm_bighint = 0;
702 scan->u.bmu_bitmap = 0;
708 * Meta node. If allocating the entire object we can special
709 * case it. However, we need to figure out how much memory
710 * is required to manage 'count' blocks, so we continue on anyway.
714 scan->bm_bighint = 0;
715 scan->u.bmu_avail = 0;
718 radix /= BLIST_META_RADIX;
719 next_skip = ((u_int)skip / BLIST_META_RADIX);
721 for (i = 1; i <= skip; i += next_skip) {
722 if (count >= radix) {
724 * Allocate the entire object
726 memindex = i + blst_radix_init(
727 ((scan) ? &scan[i] : NULL),
733 } else if (count > 0) {
735 * Allocate a partial object
737 memindex = i + blst_radix_init(
738 ((scan) ? &scan[i] : NULL),
746 * Add terminator and break out
749 scan[i].bm_bighint = (daddr_t)-1;
761 blst_radix_print(blmeta_t *scan, daddr_t blk, daddr_t radix, int skip, int tab)
767 if (radix == BLIST_BMAP_RADIX) {
769 "%*.*s(%04x,%d): bitmap %08x big=%d\n",
778 if (scan->u.bmu_avail == 0) {
780 "%*.*s(%04x,%d) ALL ALLOCATED\n",
787 if (scan->u.bmu_avail == radix) {
789 "%*.*s(%04x,%d) ALL FREE\n",
798 "%*.*s(%04x,%d): subtree (%d/%d) big=%d {\n",
806 radix /= BLIST_META_RADIX;
807 next_skip = ((u_int)skip / BLIST_META_RADIX);
810 for (i = 1; i <= skip; i += next_skip) {
811 if (scan[i].bm_bighint == (daddr_t)-1) {
813 "%*.*s(%04x,%d): Terminator\n",
842 main(int ac, char **av)
848 for (i = 1; i < ac; ++i) {
849 const char *ptr = av[i];
851 size = strtol(ptr, NULL, 0);
855 fprintf(stderr, "Bad option: %s\n", ptr - 2);
858 bl = blist_create(size);
859 blist_free(bl, 0, size);
867 printf("%d/%d/%d> ", bl->bl_free, size, bl->bl_radix);
869 if (fgets(buf, sizeof(buf), stdin) == NULL)
873 if (sscanf(buf + 1, "%d", &count) == 1) {
874 blist_resize(&bl, count, 1);
882 if (sscanf(buf + 1, "%d", &count) == 1) {
883 daddr_t blk = blist_alloc(bl, count);
884 printf(" R=%04x\n", blk);
890 if (sscanf(buf + 1, "%x %d", &da, &count) == 2) {
891 blist_free(bl, da, count);
915 panic(const char *ctl, ...)
920 vfprintf(stderr, ctl, va);
921 fprintf(stderr, "\n");