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 $
64 * $DragonFly: src/sys/kern/subr_blist.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/blist.h>
74 #include <sys/malloc.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_extern.h>
79 #include <vm/vm_page.h>
83 #ifndef BLIST_NO_DEBUG
87 #define SWAPBLK_NONE ((daddr_t)-1)
89 #include <sys/types.h>
95 #define malloc(a,b,c) malloc(a)
96 #define free(a,b) free(a)
98 typedef unsigned int u_daddr_t;
100 #include <sys/blist.h>
102 void panic(const char *ctl, ...);
107 * static support functions
110 static daddr_t blst_leaf_alloc(blmeta_t *scan, daddr_t blk, int count);
111 static daddr_t blst_meta_alloc(blmeta_t *scan, daddr_t blk,
112 daddr_t count, daddr_t radix, int skip);
113 static void blst_leaf_free(blmeta_t *scan, daddr_t relblk, int count);
114 static void blst_meta_free(blmeta_t *scan, daddr_t freeBlk, daddr_t count,
115 daddr_t radix, int skip, daddr_t blk);
116 static void blst_copy(blmeta_t *scan, daddr_t blk, daddr_t radix,
117 daddr_t skip, blist_t dest, daddr_t count);
118 static daddr_t blst_radix_init(blmeta_t *scan, daddr_t radix,
119 int skip, daddr_t count);
121 static void blst_radix_print(blmeta_t *scan, daddr_t blk,
122 daddr_t radix, int skip, int tab);
126 static MALLOC_DEFINE(M_SWAP, "SWAP", "Swap space");
130 * blist_create() - create a blist capable of handling up to the specified
133 * blocks must be greater then 0
135 * The smallest blist consists of a single leaf node capable of
136 * managing BLIST_BMAP_RADIX blocks.
140 blist_create(daddr_t blocks)
147 * Calculate radix and skip field used for scanning.
149 radix = BLIST_BMAP_RADIX;
151 while (radix < blocks) {
152 radix *= BLIST_META_RADIX;
153 skip = (skip + 1) * BLIST_META_RADIX;
156 bl = malloc(sizeof(struct blist), M_SWAP, M_WAITOK);
158 bzero(bl, sizeof(*bl));
160 bl->bl_blocks = blocks;
161 bl->bl_radix = radix;
163 bl->bl_rootblks = 1 +
164 blst_radix_init(NULL, bl->bl_radix, bl->bl_skip, blocks);
165 bl->bl_root = malloc(sizeof(blmeta_t) * bl->bl_rootblks, M_SWAP, M_WAITOK);
167 #if defined(BLIST_DEBUG)
169 "BLIST representing %d blocks (%d MB of swap)"
170 ", requiring %dK of ram\n",
172 bl->bl_blocks * 4 / 1024,
173 (bl->bl_rootblks * sizeof(blmeta_t) + 1023) / 1024
175 printf("BLIST raw radix tree contains %d records\n", bl->bl_rootblks);
177 blst_radix_init(bl->bl_root, bl->bl_radix, bl->bl_skip, blocks);
183 blist_destroy(blist_t bl)
185 free(bl->bl_root, M_SWAP);
190 * blist_alloc() - reserve space in the block bitmap. Return the base
191 * of a contiguous region or SWAPBLK_NONE if space could
196 blist_alloc(blist_t bl, daddr_t count)
198 daddr_t blk = SWAPBLK_NONE;
201 if (bl->bl_radix == BLIST_BMAP_RADIX)
202 blk = blst_leaf_alloc(bl->bl_root, 0, count);
204 blk = blst_meta_alloc(bl->bl_root, 0, count, bl->bl_radix, bl->bl_skip);
205 if (blk != SWAPBLK_NONE)
206 bl->bl_free -= count;
212 * blist_free() - free up space in the block bitmap. Return the base
213 * of a contiguous region. Panic if an inconsistancy is
218 blist_free(blist_t bl, daddr_t blkno, daddr_t count)
221 if (bl->bl_radix == BLIST_BMAP_RADIX)
222 blst_leaf_free(bl->bl_root, blkno, count);
224 blst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, bl->bl_skip, 0);
225 bl->bl_free += count;
230 * blist_resize() - resize an existing radix tree to handle the
231 * specified number of blocks. This will reallocate
232 * the tree and transfer the previous bitmap to the new
233 * one. When extending the tree you can specify whether
234 * the new blocks are to left allocated or freed.
238 blist_resize(blist_t *pbl, daddr_t count, int freenew)
240 blist_t newbl = blist_create(count);
244 if (count > save->bl_blocks)
245 count = save->bl_blocks;
246 blst_copy(save->bl_root, 0, save->bl_radix, save->bl_skip, newbl, count);
249 * If resizing upwards, should we free the new space or not?
251 if (freenew && count < newbl->bl_blocks) {
252 blist_free(newbl, count, newbl->bl_blocks - count);
260 * blist_print() - dump radix tree
264 blist_print(blist_t bl)
267 blst_radix_print(bl->bl_root, 0, bl->bl_radix, bl->bl_skip, 4);
273 /************************************************************************
274 * ALLOCATION SUPPORT FUNCTIONS *
275 ************************************************************************
277 * These support functions do all the actual work. They may seem
278 * rather longish, but that's because I've commented them up. The
279 * actual code is straight forward.
284 * blist_leaf_alloc() - allocate at a leaf in the radix tree (a bitmap).
286 * This is the core of the allocator and is optimized for the 1 block
287 * and the BLIST_BMAP_RADIX block allocation cases. Other cases are
288 * somewhat slower. The 1 block allocation case is log2 and extremely
298 u_daddr_t orig = scan->u.bmu_bitmap;
302 * Optimize bitmap all-allocated case. Also, count = 1
303 * case assumes at least 1 bit is free in the bitmap, so
304 * we have to take care of this case here.
306 scan->bm_bighint = 0;
307 return(SWAPBLK_NONE);
311 * Optimized code to allocate one bit out of the bitmap
314 int j = BLIST_BMAP_RADIX/2;
317 mask = (u_daddr_t)-1 >> (BLIST_BMAP_RADIX/2);
320 if ((orig & mask) == 0) {
327 scan->u.bmu_bitmap &= ~(1 << r);
330 if (count <= BLIST_BMAP_RADIX) {
332 * non-optimized code to allocate N bits out of the bitmap.
333 * The more bits, the faster the code runs. It will run
334 * the slowest allocating 2 bits, but since there aren't any
335 * memory ops in the core loop (or shouldn't be, anyway),
336 * you probably won't notice the difference.
339 int n = BLIST_BMAP_RADIX - count;
342 mask = (u_daddr_t)-1 >> n;
344 for (j = 0; j <= n; ++j) {
345 if ((orig & mask) == mask) {
346 scan->u.bmu_bitmap &= ~mask;
353 * We couldn't allocate count in this subtree, update bighint.
355 scan->bm_bighint = count - 1;
356 return(SWAPBLK_NONE);
360 * blist_meta_alloc() - allocate at a meta in the radix tree.
362 * Attempt to allocate at a meta node. If we can't, we update
363 * bighint and return a failure. Updating bighint optimize future
364 * calls that hit this node. We have to check for our collapse cases
365 * and we have a few optimizations strewn in as well.
377 int next_skip = ((u_int)skip / BLIST_META_RADIX);
379 if (scan->u.bmu_avail == 0) {
381 * ALL-ALLOCATED special case
383 scan->bm_bighint = count;
384 return(SWAPBLK_NONE);
387 if (scan->u.bmu_avail == radix) {
388 radix /= BLIST_META_RADIX;
391 * ALL-FREE special case, initialize uninitialize
394 for (i = 1; i <= skip; i += next_skip) {
395 if (scan[i].bm_bighint == (daddr_t)-1)
397 if (next_skip == 1) {
398 scan[i].u.bmu_bitmap = (u_daddr_t)-1;
399 scan[i].bm_bighint = BLIST_BMAP_RADIX;
401 scan[i].bm_bighint = radix;
402 scan[i].u.bmu_avail = radix;
406 radix /= BLIST_META_RADIX;
409 for (i = 1; i <= skip; i += next_skip) {
410 if (count <= scan[i].bm_bighint) {
412 * count fits in object
415 if (next_skip == 1) {
416 r = blst_leaf_alloc(&scan[i], blk, count);
418 r = blst_meta_alloc(&scan[i], blk, count, radix, next_skip - 1);
420 if (r != SWAPBLK_NONE) {
421 scan->u.bmu_avail -= count;
422 if (scan->bm_bighint > scan->u.bmu_avail)
423 scan->bm_bighint = scan->u.bmu_avail;
426 } else if (scan[i].bm_bighint == (daddr_t)-1) {
431 } else if (count > radix) {
433 * count does not fit in object even if it were
436 panic("blist_meta_alloc: allocation too large");
442 * We couldn't allocate count in this subtree, update bighint.
444 if (scan->bm_bighint >= count)
445 scan->bm_bighint = count - 1;
446 return(SWAPBLK_NONE);
450 * BLST_LEAF_FREE() - free allocated block from leaf bitmap
461 * free some data in this bitmap
464 * 0000111111111110000
468 int n = blk & (BLIST_BMAP_RADIX - 1);
471 mask = ((u_daddr_t)-1 << n) &
472 ((u_daddr_t)-1 >> (BLIST_BMAP_RADIX - count - n));
474 if (scan->u.bmu_bitmap & mask)
475 panic("blst_radix_free: freeing free block");
476 scan->u.bmu_bitmap |= mask;
479 * We could probably do a better job here. We are required to make
480 * bighint at least as large as the biggest contiguous block of
481 * data. If we just shoehorn it, a little extra overhead will
482 * be incured on the next allocation (but only that one typically).
484 scan->bm_bighint = BLIST_BMAP_RADIX;
488 * BLST_META_FREE() - free allocated blocks from radix tree meta info
490 * This support routine frees a range of blocks from the bitmap.
491 * The range must be entirely enclosed by this radix node. If a
492 * meta node, we break the range down recursively to free blocks
493 * in subnodes (which means that this code can free an arbitrary
494 * range whereas the allocation code cannot allocate an arbitrary
508 int next_skip = ((u_int)skip / BLIST_META_RADIX);
511 printf("FREE (%x,%d) FROM (%x,%d)\n",
517 if (scan->u.bmu_avail == 0) {
519 * ALL-ALLOCATED special case, with possible
520 * shortcut to ALL-FREE special case.
522 scan->u.bmu_avail = count;
523 scan->bm_bighint = count;
525 if (count != radix) {
526 for (i = 1; i <= skip; i += next_skip) {
527 if (scan[i].bm_bighint == (daddr_t)-1)
529 scan[i].bm_bighint = 0;
530 if (next_skip == 1) {
531 scan[i].u.bmu_bitmap = 0;
533 scan[i].u.bmu_avail = 0;
539 scan->u.bmu_avail += count;
540 /* scan->bm_bighint = radix; */
544 * ALL-FREE special case.
547 if (scan->u.bmu_avail == radix)
549 if (scan->u.bmu_avail > radix)
550 panic("blst_meta_free: freeing already free blocks (%d) %d/%d", count, scan->u.bmu_avail, radix);
553 * Break the free down into its components
556 radix /= BLIST_META_RADIX;
558 i = (freeBlk - blk) / radix;
560 i = i * next_skip + 1;
562 while (i <= skip && blk < freeBlk + count) {
565 v = blk + radix - freeBlk;
569 if (scan->bm_bighint == (daddr_t)-1)
570 panic("blst_meta_free: freeing unexpected range");
572 if (next_skip == 1) {
573 blst_leaf_free(&scan[i], freeBlk, v);
575 blst_meta_free(&scan[i], freeBlk, v, radix, next_skip - 1, blk);
577 if (scan->bm_bighint < scan[i].bm_bighint)
578 scan->bm_bighint = scan[i].bm_bighint;
587 * BLIST_RADIX_COPY() - copy one radix tree to another
589 * Locates free space in the source tree and frees it in the destination
590 * tree. The space may not already be free in the destination.
593 static void blst_copy(
608 if (radix == BLIST_BMAP_RADIX) {
609 u_daddr_t v = scan->u.bmu_bitmap;
611 if (v == (u_daddr_t)-1) {
612 blist_free(dest, blk, count);
616 for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) {
618 blist_free(dest, blk + i, 1);
628 if (scan->u.bmu_avail == 0) {
630 * Source all allocated, leave dest allocated
634 if (scan->u.bmu_avail == radix) {
636 * Source all free, free entire dest
639 blist_free(dest, blk, count);
641 blist_free(dest, blk, radix);
646 radix /= BLIST_META_RADIX;
647 next_skip = ((u_int)skip / BLIST_META_RADIX);
649 for (i = 1; count && i <= skip; i += next_skip) {
650 if (scan[i].bm_bighint == (daddr_t)-1)
653 if (count >= radix) {
681 * BLST_RADIX_INIT() - initialize radix tree
683 * Initialize our meta structures and bitmaps and calculate the exact
684 * amount of space required to manage 'count' blocks - this space may
685 * be considerably less then the calculated radix due to the large
686 * RADIX values we use.
690 blst_radix_init(blmeta_t *scan, daddr_t radix, int skip, daddr_t count)
694 daddr_t memindex = 0;
700 if (radix == BLIST_BMAP_RADIX) {
702 scan->bm_bighint = 0;
703 scan->u.bmu_bitmap = 0;
709 * Meta node. If allocating the entire object we can special
710 * case it. However, we need to figure out how much memory
711 * is required to manage 'count' blocks, so we continue on anyway.
715 scan->bm_bighint = 0;
716 scan->u.bmu_avail = 0;
719 radix /= BLIST_META_RADIX;
720 next_skip = ((u_int)skip / BLIST_META_RADIX);
722 for (i = 1; i <= skip; i += next_skip) {
723 if (count >= radix) {
725 * Allocate the entire object
727 memindex = i + blst_radix_init(
728 ((scan) ? &scan[i] : NULL),
734 } else if (count > 0) {
736 * Allocate a partial object
738 memindex = i + blst_radix_init(
739 ((scan) ? &scan[i] : NULL),
747 * Add terminator and break out
750 scan[i].bm_bighint = (daddr_t)-1;
762 blst_radix_print(blmeta_t *scan, daddr_t blk, daddr_t radix, int skip, int tab)
768 if (radix == BLIST_BMAP_RADIX) {
770 "%*.*s(%04x,%d): bitmap %08x big=%d\n",
779 if (scan->u.bmu_avail == 0) {
781 "%*.*s(%04x,%d) ALL ALLOCATED\n",
788 if (scan->u.bmu_avail == radix) {
790 "%*.*s(%04x,%d) ALL FREE\n",
799 "%*.*s(%04x,%d): subtree (%d/%d) big=%d {\n",
807 radix /= BLIST_META_RADIX;
808 next_skip = ((u_int)skip / BLIST_META_RADIX);
811 for (i = 1; i <= skip; i += next_skip) {
812 if (scan[i].bm_bighint == (daddr_t)-1) {
814 "%*.*s(%04x,%d): Terminator\n",
843 main(int ac, char **av)
849 for (i = 1; i < ac; ++i) {
850 const char *ptr = av[i];
852 size = strtol(ptr, NULL, 0);
856 fprintf(stderr, "Bad option: %s\n", ptr - 2);
859 bl = blist_create(size);
860 blist_free(bl, 0, size);
868 printf("%d/%d/%d> ", bl->bl_free, size, bl->bl_radix);
870 if (fgets(buf, sizeof(buf), stdin) == NULL)
874 if (sscanf(buf + 1, "%d", &count) == 1) {
875 blist_resize(&bl, count, 1);
883 if (sscanf(buf + 1, "%d", &count) == 1) {
884 daddr_t blk = blist_alloc(bl, count);
885 printf(" R=%04x\n", blk);
891 if (sscanf(buf + 1, "%x %d", &da, &count) == 2) {
892 blist_free(bl, da, count);
916 panic(const char *ctl, ...)
921 vfprintf(stderr, ctl, va);
922 fprintf(stderr, "\n");