]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/dtrace/blist.c
2 * BLIST.C - Bitmap allocator/deallocator, using a radix tree with hinting
4 * (c)Copyright 1998, Matthew Dillon. Terms for use and redistribution
5 * are covered by the BSD Copyright as found in /usr/src/COPYRIGHT.
7 * This module implements a general bitmap allocator/deallocator. The
8 * allocator eats around 2 bits per 'block'. The module does not
9 * try to interpret the meaning of a 'block' other then to return
10 * SWAPBLK_NONE on an allocation failure.
12 * A radix tree is used to maintain the bitmap. Two radix constants are
13 * involved: One for the bitmaps contained in the leaf nodes (typically
14 * 32), and one for the meta nodes (typically 16). Both meta and leaf
15 * nodes have a hint field. This field gives us a hint as to the largest
16 * free contiguous range of blocks under the node. It may contain a
17 * value that is too high, but will never contain a value that is too
18 * low. When the radix tree is searched, allocation failures in subtrees
21 * The radix tree also implements two collapsed states for meta nodes:
22 * the ALL-ALLOCATED state and the ALL-FREE state. If a meta node is
23 * in either of these two states, all information contained underneath
24 * the node is considered stale. These states are used to optimize
25 * allocation and freeing operations.
27 * The hinting greatly increases code efficiency for allocations while
28 * the general radix structure optimizes both allocations and frees. The
29 * radix tree should be able to operate well no matter how much
30 * fragmentation there is and no matter how large a bitmap is used.
32 * Unlike the rlist code, the blist code wires all necessary memory at
33 * creation time. Neither allocations nor frees require interaction with
34 * the memory subsystem. In contrast, the rlist code may allocate memory
35 * on an rlist_free() call. The non-blocking features of the blist code
36 * are used to great advantage in the swap code (vm/nswap_pager.c). The
37 * rlist code uses a little less overall memory then the blist code (but
38 * due to swap interleaving not all that much less), but the blist code
39 * scales much, much better.
41 * LAYOUT: The radix tree is layed out recursively using a
42 * linear array. Each meta node is immediately followed (layed out
43 * sequentially in memory) by BLIST_META_RADIX lower level nodes. This
44 * is a recursive structure but one that can be easily scanned through
45 * a very simple 'skip' calculation. In order to support large radixes,
46 * portions of the tree may reside outside our memory allocation. We
47 * handle this with an early-termination optimization (when bighint is
48 * set to -1) on the scan. The memory allocation is only large enough
49 * to cover the number of blocks requested at creation time even if it
50 * must be encompassed in larger root-node radix.
52 * NOTE: the allocator cannot currently allocate more then
53 * BLIST_BMAP_RADIX blocks per call. It will panic with 'allocation too
54 * large' if you try. This is an area that could use improvement. The
55 * radix is large enough that this restriction does not effect the swap
56 * system, though. Currently only the allocation code is effected by
57 * this algorithmic unfeature. The freeing code can handle arbitrary
60 * This code can be compiled stand-alone for debugging.
62 * $FreeBSD: src/sys/kern/subr_blist.c,v 1.5.2.1 2000/03/17 10:47:29 ps Exp $
65 typedef unsigned int u_daddr_t
;
67 #include <sys/param.h>
68 #include <sys/systm.h>
70 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #define SWAPBLK_NONE ((daddr_t)-1)
75 #define malloc _MALLOC
80 * static support functions
83 static daddr_t
blst_leaf_alloc(blmeta_t
*scan
, daddr_t blk
, int count
);
84 static daddr_t
blst_meta_alloc(blmeta_t
*scan
, daddr_t blk
,
85 daddr_t count
, daddr_t radix
, int skip
);
86 static void blst_leaf_free(blmeta_t
*scan
, daddr_t relblk
, int count
);
87 static void blst_meta_free(blmeta_t
*scan
, daddr_t freeBlk
, daddr_t count
,
88 daddr_t radix
, int skip
, daddr_t blk
);
89 static void blst_copy(blmeta_t
*scan
, daddr_t blk
, daddr_t radix
,
90 daddr_t skip
, blist_t dest
, daddr_t count
);
91 static daddr_t
blst_radix_init(blmeta_t
*scan
, daddr_t radix
,
92 int skip
, daddr_t count
);
95 * blist_create() - create a blist capable of handling up to the specified
98 * blocks must be greater then 0
100 * The smallest blist consists of a single leaf node capable of
101 * managing BLIST_BMAP_RADIX blocks.
105 blist_create(daddr_t blocks
)
112 * Calculate radix and skip field used for scanning.
114 radix
= BLIST_BMAP_RADIX
;
116 while (radix
< blocks
) {
117 radix
<<= BLIST_META_RADIX_SHIFT
;
118 skip
= (skip
+ 1) << BLIST_META_RADIX_SHIFT
;
121 bl
= malloc(sizeof(struct blist
), M_SWAP
, M_WAITOK
);
123 bzero(bl
, sizeof(*bl
));
125 bl
->bl_blocks
= blocks
;
126 bl
->bl_radix
= radix
;
128 bl
->bl_rootblks
= 1 +
129 blst_radix_init(NULL
, bl
->bl_radix
, bl
->bl_skip
, blocks
);
130 bl
->bl_root
= malloc(sizeof(blmeta_t
) * bl
->bl_rootblks
, M_SWAP
, M_WAITOK
);
132 #if defined(BLIST_DEBUG)
134 "BLIST representing %d blocks (%d MB of swap)"
135 ", requiring %dK of ram\n",
137 bl
->bl_blocks
* 4 / 1024,
138 (bl
->bl_rootblks
* sizeof(blmeta_t
) + 1023) / 1024
140 printf("BLIST raw radix tree contains %d records\n", bl
->bl_rootblks
);
142 blst_radix_init(bl
->bl_root
, bl
->bl_radix
, bl
->bl_skip
, blocks
);
148 blist_destroy(blist_t bl
)
150 free(bl
->bl_root
, M_SWAP
);
155 * blist_alloc() - reserve space in the block bitmap. Return the base
156 * of a contiguous region or SWAPBLK_NONE if space could
161 blist_alloc(blist_t bl
, daddr_t count
)
163 daddr_t blk
= SWAPBLK_NONE
;
166 if (bl
->bl_radix
== BLIST_BMAP_RADIX
) {
167 blk
= blst_leaf_alloc(bl
->bl_root
, 0, count
);
169 blk
= blst_meta_alloc(bl
->bl_root
, 0, count
,
170 bl
->bl_radix
, bl
->bl_skip
);
172 if (blk
!= SWAPBLK_NONE
) {
173 bl
->bl_free
-= count
;
180 * blist_free() - free up space in the block bitmap. Return the base
181 * of a contiguous region. Panic if an inconsistancy is
186 blist_free(blist_t bl
, daddr_t blkno
, daddr_t count
)
189 if (bl
->bl_radix
== BLIST_BMAP_RADIX
) {
190 blst_leaf_free(bl
->bl_root
, blkno
, count
);
192 blst_meta_free(bl
->bl_root
, blkno
, count
,
193 bl
->bl_radix
, bl
->bl_skip
, 0);
195 bl
->bl_free
+= count
;
200 * blist_resize() - resize an existing radix tree to handle the
201 * specified number of blocks. This will reallocate
202 * the tree and transfer the previous bitmap to the new
203 * one. When extending the tree you can specify whether
204 * the new blocks are to left allocated or freed.
208 blist_resize(blist_t
*pbl
, daddr_t count
, int freenew
)
210 blist_t newbl
= blist_create(count
);
214 if (count
> save
->bl_blocks
) {
215 count
= save
->bl_blocks
;
217 blst_copy(save
->bl_root
, 0, save
->bl_radix
, save
->bl_skip
, newbl
, count
);
220 * If resizing upwards, should we free the new space or not?
222 if (freenew
&& count
< newbl
->bl_blocks
) {
223 blist_free(newbl
, count
, newbl
->bl_blocks
- count
);
231 * blist_print() - dump radix tree
235 blist_print(blist_t bl
)
238 blst_radix_print(bl
->bl_root
, 0, bl
->bl_radix
, bl
->bl_skip
, 4);
244 /************************************************************************
245 * ALLOCATION SUPPORT FUNCTIONS *
246 ************************************************************************
248 * These support functions do all the actual work. They may seem
249 * rather longish, but that's because I've commented them up. The
250 * actual code is straight forward.
255 * blist_leaf_alloc() - allocate at a leaf in the radix tree (a bitmap).
257 * This is the core of the allocator and is optimized for the 1 block
258 * and the BLIST_BMAP_RADIX block allocation cases. Other cases are
259 * somewhat slower. The 1 block allocation case is log2 and extremely
264 blst_leaf_alloc(blmeta_t
*scan
, daddr_t blk
, int count
)
266 u_daddr_t orig
= scan
->u
.bmu_bitmap
;
270 * Optimize bitmap all-allocated case. Also, count = 1
271 * case assumes at least 1 bit is free in the bitmap, so
272 * we have to take care of this case here.
274 scan
->bm_bighint
= 0;
279 * Optimized code to allocate one bit out of the bitmap
282 int j
= BLIST_BMAP_RADIX
/ 2;
285 mask
= (u_daddr_t
)-1 >> (BLIST_BMAP_RADIX
/ 2);
288 if ((orig
& mask
) == 0) {
295 scan
->u
.bmu_bitmap
&= ~(1 << r
);
298 #if !defined(__APPLE__)
299 if (count
<= BLIST_BMAP_RADIX
) {
301 if (count
<= (int)BLIST_BMAP_RADIX
) {
302 #endif /* __APPLE__ */
304 * non-optimized code to allocate N bits out of the bitmap.
305 * The more bits, the faster the code runs. It will run
306 * the slowest allocating 2 bits, but since there aren't any
307 * memory ops in the core loop (or shouldn't be, anyway),
308 * you probably won't notice the difference.
311 int n
= BLIST_BMAP_RADIX
- count
;
314 mask
= (u_daddr_t
)-1 >> n
;
316 for (j
= 0; j
<= n
; ++j
) {
317 if ((orig
& mask
) == mask
) {
318 scan
->u
.bmu_bitmap
&= ~mask
;
325 * We couldn't allocate count in this subtree, update bighint.
327 scan
->bm_bighint
= count
- 1;
332 * blist_meta_alloc() - allocate at a meta in the radix tree.
334 * Attempt to allocate at a meta node. If we can't, we update
335 * bighint and return a failure. Updating bighint optimize future
336 * calls that hit this node. We have to check for our collapse cases
337 * and we have a few optimizations strewn in as well.
341 blst_meta_alloc(blmeta_t
*scan
, daddr_t blk
, daddr_t count
, daddr_t radix
,
345 int next_skip
= (skip
>> BLIST_META_RADIX_SHIFT
);
347 if (scan
->u
.bmu_avail
== 0) {
349 * ALL-ALLOCATED special case
351 scan
->bm_bighint
= count
;
355 if (scan
->u
.bmu_avail
== radix
) {
356 radix
>>= BLIST_META_RADIX_SHIFT
;
359 * ALL-FREE special case, initialize uninitialize
362 for (i
= 1; i
<= skip
; i
+= next_skip
) {
363 if (scan
[i
].bm_bighint
== (daddr_t
)-1) {
366 if (next_skip
== 1) {
367 scan
[i
].u
.bmu_bitmap
= (u_daddr_t
)-1;
368 scan
[i
].bm_bighint
= BLIST_BMAP_RADIX
;
370 scan
[i
].bm_bighint
= radix
;
371 scan
[i
].u
.bmu_avail
= radix
;
375 radix
>>= BLIST_META_RADIX_SHIFT
;
378 for (i
= 1; i
<= skip
; i
+= next_skip
) {
379 if (count
<= scan
[i
].bm_bighint
) {
381 * count fits in object
384 if (next_skip
== 1) {
385 r
= blst_leaf_alloc(&scan
[i
], blk
, count
);
387 r
= blst_meta_alloc(&scan
[i
], blk
, count
,
388 radix
, next_skip
- 1);
390 if (r
!= SWAPBLK_NONE
) {
391 scan
->u
.bmu_avail
-= count
;
392 if (scan
->bm_bighint
> scan
->u
.bmu_avail
) {
393 scan
->bm_bighint
= scan
->u
.bmu_avail
;
397 } else if (scan
[i
].bm_bighint
== (daddr_t
)-1) {
402 } else if (count
> radix
) {
404 * count does not fit in object even if it were
407 panic("blist_meta_alloc: allocation too large");
413 * We couldn't allocate count in this subtree, update bighint.
415 if (scan
->bm_bighint
>= count
) {
416 scan
->bm_bighint
= count
- 1;
422 * BLST_LEAF_FREE() - free allocated block from leaf bitmap
427 blst_leaf_free(blmeta_t
*scan
, daddr_t blk
, int count
)
430 * free some data in this bitmap
433 * 0000111111111110000
437 int n
= blk
& (BLIST_BMAP_RADIX
- 1);
440 mask
= ((u_daddr_t
)-1 << n
) &
441 ((u_daddr_t
)-1 >> (BLIST_BMAP_RADIX
- count
- n
));
443 if (scan
->u
.bmu_bitmap
& mask
) {
444 panic("blst_radix_free: freeing free block");
446 scan
->u
.bmu_bitmap
|= mask
;
449 * We could probably do a better job here. We are required to make
450 * bighint at least as large as the biggest contiguous block of
451 * data. If we just shoehorn it, a little extra overhead will
452 * be incured on the next allocation (but only that one typically).
454 scan
->bm_bighint
= BLIST_BMAP_RADIX
;
458 * BLST_META_FREE() - free allocated blocks from radix tree meta info
460 * This support routine frees a range of blocks from the bitmap.
461 * The range must be entirely enclosed by this radix node. If a
462 * meta node, we break the range down recursively to free blocks
463 * in subnodes (which means that this code can free an arbitrary
464 * range whereas the allocation code cannot allocate an arbitrary
469 blst_meta_free(blmeta_t
*scan
, daddr_t freeBlk
, daddr_t count
, daddr_t radix
,
470 int skip
, daddr_t blk
)
473 int next_skip
= (skip
>> BLIST_META_RADIX_SHIFT
);
476 printf("FREE (%x,%d) FROM (%x,%d)\n",
482 if (scan
->u
.bmu_avail
== 0) {
484 * ALL-ALLOCATED special case, with possible
485 * shortcut to ALL-FREE special case.
487 scan
->u
.bmu_avail
= count
;
488 scan
->bm_bighint
= count
;
490 if (count
!= radix
) {
491 for (i
= 1; i
<= skip
; i
+= next_skip
) {
492 if (scan
[i
].bm_bighint
== (daddr_t
)-1) {
495 scan
[i
].bm_bighint
= 0;
496 if (next_skip
== 1) {
497 scan
[i
].u
.bmu_bitmap
= 0;
499 scan
[i
].u
.bmu_avail
= 0;
505 scan
->u
.bmu_avail
+= count
;
506 /* scan->bm_bighint = radix; */
510 * ALL-FREE special case.
513 if (scan
->u
.bmu_avail
== radix
) {
516 if (scan
->u
.bmu_avail
> radix
) {
517 panic("blst_meta_free: freeing already free blocks (%d) %d/%d", count
, scan
->u
.bmu_avail
, radix
);
521 * Break the free down into its components
524 radix
>>= BLIST_META_RADIX_SHIFT
;
526 i
= (freeBlk
- blk
) / radix
;
528 i
= i
* next_skip
+ 1;
530 while (i
<= skip
&& blk
< freeBlk
+ count
) {
533 v
= blk
+ radix
- freeBlk
;
538 if (scan
->bm_bighint
== (daddr_t
)-1) {
539 panic("blst_meta_free: freeing unexpected range");
542 if (next_skip
== 1) {
543 blst_leaf_free(&scan
[i
], freeBlk
, v
);
545 blst_meta_free(&scan
[i
], freeBlk
, v
, radix
,
548 if (scan
->bm_bighint
< scan
[i
].bm_bighint
) {
549 scan
->bm_bighint
= scan
[i
].bm_bighint
;
559 * BLIST_RADIX_COPY() - copy one radix tree to another
561 * Locates free space in the source tree and frees it in the destination
562 * tree. The space may not already be free in the destination.
566 blst_copy(blmeta_t
*scan
, daddr_t blk
, daddr_t radix
,
567 daddr_t skip
, blist_t dest
, daddr_t count
)
576 if (radix
== BLIST_BMAP_RADIX
) {
577 u_daddr_t v
= scan
->u
.bmu_bitmap
;
579 if (v
== (u_daddr_t
)-1) {
580 blist_free(dest
, blk
, count
);
582 #if !defined(__APPLE__)
585 for (i
= 0; i
< BLIST_BMAP_RADIX
&& i
< count
; ++i
) {
587 blist_free(dest
, blk
+ i
, 1);
591 int j
; /* Avoid shadow warnings */
593 for (j
= 0; j
< (int)BLIST_BMAP_RADIX
&& j
< count
; ++j
) {
595 blist_free(dest
, blk
+ j
, 1);
598 #endif /* __APPLE__ */
608 * Source all allocated, leave dest allocated
610 if (scan
->u
.bmu_avail
== 0) {
613 if (scan
->u
.bmu_avail
== radix
) {
615 * Source all free, free entire dest
618 blist_free(dest
, blk
, count
);
620 blist_free(dest
, blk
, radix
);
625 radix
>>= BLIST_META_RADIX_SHIFT
;
626 next_skip
= (skip
>> BLIST_META_RADIX_SHIFT
);
628 for (i
= 1; count
&& i
<= skip
; i
+= next_skip
) {
629 if (scan
[i
].bm_bighint
== (daddr_t
)-1) {
633 if (count
>= radix
) {
661 * BLST_RADIX_INIT() - initialize radix tree
663 * Initialize our meta structures and bitmaps and calculate the exact
664 * amount of space required to manage 'count' blocks - this space may
665 * be considerably less then the calculated radix due to the large
666 * RADIX values we use.
670 blst_radix_init(blmeta_t
*scan
, daddr_t radix
, int skip
, daddr_t count
)
674 daddr_t memindex
= 0;
680 if (radix
== BLIST_BMAP_RADIX
) {
682 scan
->bm_bighint
= 0;
683 scan
->u
.bmu_bitmap
= 0;
689 * Meta node. If allocating the entire object we can special
690 * case it. However, we need to figure out how much memory
691 * is required to manage 'count' blocks, so we continue on anyway.
695 scan
->bm_bighint
= 0;
696 scan
->u
.bmu_avail
= 0;
699 radix
>>= BLIST_META_RADIX_SHIFT
;
700 next_skip
= (skip
>> BLIST_META_RADIX_SHIFT
);
702 for (i
= 1; i
<= skip
; i
+= next_skip
) {
703 if (count
>= radix
) {
705 * Allocate the entire object
707 memindex
= i
+ blst_radix_init(
708 ((scan
) ? &scan
[i
] : NULL
),
714 } else if (count
> 0) {
716 * Allocate a partial object
718 memindex
= i
+ blst_radix_init(
719 ((scan
) ? &scan
[i
] : NULL
),
727 * Add terminator and break out
730 scan
[i
].bm_bighint
= (daddr_t
)-1;
744 blst_radix_print(blmeta_t
*scan
, daddr_t blk
, daddr_t radix
, int skip
, int tab
)
750 if (radix
== BLIST_BMAP_RADIX
) {
752 "%*.*s(%04x,%d): bitmap %08x big=%d\n",
761 if (scan
->u
.bmu_avail
== 0) {
763 "%*.*s(%04x,%d) ALL ALLOCATED\n",
770 if (scan
->u
.bmu_avail
== radix
) {
772 "%*.*s(%04x,%d) ALL FREE\n",
781 "%*.*s(%04x,%d): subtree (%d/%d) big=%d {\n",
789 radix
>>= BLIST_META_RADIX_SHIFT
;
790 next_skip
= (skip
>> BLIST_META_RADIX_SHIFT
);
793 for (i
= 1; i
<= skip
; i
+= next_skip
) {
794 if (scan
[i
].bm_bighint
== (daddr_t
)-1) {
796 "%*.*s(%04x,%d): Terminator\n",
825 main(int ac
, char **av
)
831 for (i
= 1; i
< ac
; ++i
) {
832 const char *ptr
= av
[i
];
834 size
= strtol(ptr
, NULL
, 0);
838 fprintf(stderr
, "Bad option: %s\n", ptr
- 2);
841 bl
= blist_create(size
);
842 blist_free(bl
, 0, size
);
850 printf("%d/%d/%d> ", bl
->bl_free
, size
, bl
->bl_radix
);
852 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
857 if (sscanf(buf
+ 1, "%d", &count
) == 1) {
858 blist_resize(&bl
, count
, 1);
866 if (sscanf(buf
+ 1, "%d", &count
) == 1) {
867 daddr_t blk
= blist_alloc(bl
, count
);
868 printf(" R=%04x\n", blk
);
874 if (sscanf(buf
+ 1, "%x %d", &da
, &count
) == 2) {
875 blist_free(bl
, da
, count
);
899 panic(const char *ctl
, ...)
904 vfprintf(stderr
, ctl
, va
);
905 fprintf(stderr
, "\n");