2 * Hierarchical bitmap unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qemu/hbitmap.h"
15 #define LOG_BITS_PER_LONG (BITS_PER_LONG == 32 ? 5 : 6)
17 #define L1 BITS_PER_LONG
18 #define L2 (BITS_PER_LONG * L1)
19 #define L3 (BITS_PER_LONG * L2)
21 typedef struct TestHBitmapData
{
30 /* Check that the HBitmap and the shadow bitmap contain the same data,
31 * ignoring the same "first" bits.
33 static void hbitmap_test_check(TestHBitmapData
*data
,
42 hbitmap_iter_init(&hbi
, data
->hb
, first
);
46 next
= hbitmap_iter_next(&hbi
);
52 pos
= i
>> LOG_BITS_PER_LONG
;
53 bit
= i
& (BITS_PER_LONG
- 1);
55 g_assert_cmpint(data
->bits
[pos
] & (1UL << bit
), ==, 0);
58 if (next
== data
->size
) {
62 pos
= i
>> LOG_BITS_PER_LONG
;
63 bit
= i
& (BITS_PER_LONG
- 1);
66 g_assert_cmpint(data
->bits
[pos
] & (1UL << bit
), !=, 0);
70 g_assert_cmpint(count
<< data
->granularity
, ==, hbitmap_count(data
->hb
));
74 /* This is provided instead of a test setup function so that the sizes
75 are kept in the test functions (and not in main()) */
76 static void hbitmap_test_init(TestHBitmapData
*data
,
77 uint64_t size
, int granularity
)
80 data
->hb
= hbitmap_alloc(size
, granularity
);
82 n
= DIV_ROUND_UP(size
, BITS_PER_LONG
);
86 data
->bits
= g_new0(unsigned long, n
);
88 data
->granularity
= granularity
;
90 hbitmap_test_check(data
, 0);
94 static inline size_t hbitmap_test_array_size(size_t bits
)
96 size_t n
= DIV_ROUND_UP(bits
, BITS_PER_LONG
);
100 static void hbitmap_test_truncate_impl(TestHBitmapData
*data
,
105 data
->old_size
= data
->size
;
108 if (data
->size
== data
->old_size
) {
112 n
= hbitmap_test_array_size(size
);
113 m
= hbitmap_test_array_size(data
->old_size
);
114 data
->bits
= g_realloc(data
->bits
, sizeof(unsigned long) * n
);
116 memset(&data
->bits
[m
], 0x00, sizeof(unsigned long) * (n
- m
));
119 /* If we shrink to an uneven multiple of sizeof(unsigned long),
120 * scrub the leftover memory. */
121 if (data
->size
< data
->old_size
) {
122 m
= size
% (sizeof(unsigned long) * 8);
124 unsigned long mask
= (1ULL << m
) - 1;
125 data
->bits
[n
-1] &= mask
;
129 hbitmap_truncate(data
->hb
, size
);
132 static void hbitmap_test_teardown(TestHBitmapData
*data
,
136 hbitmap_free(data
->hb
);
143 /* Set a range in the HBitmap and in the shadow "simple" bitmap.
144 * The two bitmaps are then tested against each other.
146 static void hbitmap_test_set(TestHBitmapData
*data
,
147 uint64_t first
, uint64_t count
)
149 hbitmap_set(data
->hb
, first
, count
);
150 while (count
-- != 0) {
151 size_t pos
= first
>> LOG_BITS_PER_LONG
;
152 int bit
= first
& (BITS_PER_LONG
- 1);
155 data
->bits
[pos
] |= 1UL << bit
;
158 if (data
->granularity
== 0) {
159 hbitmap_test_check(data
, 0);
163 /* Reset a range in the HBitmap and in the shadow "simple" bitmap.
165 static void hbitmap_test_reset(TestHBitmapData
*data
,
166 uint64_t first
, uint64_t count
)
168 hbitmap_reset(data
->hb
, first
, count
);
169 while (count
-- != 0) {
170 size_t pos
= first
>> LOG_BITS_PER_LONG
;
171 int bit
= first
& (BITS_PER_LONG
- 1);
174 data
->bits
[pos
] &= ~(1UL << bit
);
177 if (data
->granularity
== 0) {
178 hbitmap_test_check(data
, 0);
182 static void hbitmap_test_reset_all(TestHBitmapData
*data
)
186 hbitmap_reset_all(data
->hb
);
188 n
= DIV_ROUND_UP(data
->size
, BITS_PER_LONG
);
192 memset(data
->bits
, 0, n
* sizeof(unsigned long));
194 if (data
->granularity
== 0) {
195 hbitmap_test_check(data
, 0);
199 static void hbitmap_test_check_get(TestHBitmapData
*data
)
204 for (i
= 0; i
< data
->size
; i
++) {
205 size_t pos
= i
>> LOG_BITS_PER_LONG
;
206 int bit
= i
& (BITS_PER_LONG
- 1);
207 unsigned long val
= data
->bits
[pos
] & (1UL << bit
);
208 count
+= hbitmap_get(data
->hb
, i
);
209 g_assert_cmpint(hbitmap_get(data
->hb
, i
), ==, val
!= 0);
211 g_assert_cmpint(count
, ==, hbitmap_count(data
->hb
));
214 static void test_hbitmap_zero(TestHBitmapData
*data
,
217 hbitmap_test_init(data
, 0, 0);
220 static void test_hbitmap_unaligned(TestHBitmapData
*data
,
223 hbitmap_test_init(data
, L3
+ 23, 0);
224 hbitmap_test_set(data
, 0, 1);
225 hbitmap_test_set(data
, L3
+ 22, 1);
228 static void test_hbitmap_iter_empty(TestHBitmapData
*data
,
231 hbitmap_test_init(data
, L1
, 0);
234 static void test_hbitmap_iter_partial(TestHBitmapData
*data
,
237 hbitmap_test_init(data
, L3
, 0);
238 hbitmap_test_set(data
, 0, L3
);
239 hbitmap_test_check(data
, 1);
240 hbitmap_test_check(data
, L1
- 1);
241 hbitmap_test_check(data
, L1
);
242 hbitmap_test_check(data
, L1
* 2 - 1);
243 hbitmap_test_check(data
, L2
- 1);
244 hbitmap_test_check(data
, L2
);
245 hbitmap_test_check(data
, L2
+ 1);
246 hbitmap_test_check(data
, L2
+ L1
);
247 hbitmap_test_check(data
, L2
+ L1
* 2 - 1);
248 hbitmap_test_check(data
, L2
* 2 - 1);
249 hbitmap_test_check(data
, L2
* 2);
250 hbitmap_test_check(data
, L2
* 2 + 1);
251 hbitmap_test_check(data
, L2
* 2 + L1
);
252 hbitmap_test_check(data
, L2
* 2 + L1
* 2 - 1);
253 hbitmap_test_check(data
, L3
/ 2);
256 static void test_hbitmap_set_all(TestHBitmapData
*data
,
259 hbitmap_test_init(data
, L3
, 0);
260 hbitmap_test_set(data
, 0, L3
);
263 static void test_hbitmap_get_all(TestHBitmapData
*data
,
266 hbitmap_test_init(data
, L3
, 0);
267 hbitmap_test_set(data
, 0, L3
);
268 hbitmap_test_check_get(data
);
271 static void test_hbitmap_get_some(TestHBitmapData
*data
,
274 hbitmap_test_init(data
, 2 * L2
, 0);
275 hbitmap_test_set(data
, 10, 1);
276 hbitmap_test_check_get(data
);
277 hbitmap_test_set(data
, L1
- 1, 1);
278 hbitmap_test_check_get(data
);
279 hbitmap_test_set(data
, L1
, 1);
280 hbitmap_test_check_get(data
);
281 hbitmap_test_set(data
, L2
- 1, 1);
282 hbitmap_test_check_get(data
);
283 hbitmap_test_set(data
, L2
, 1);
284 hbitmap_test_check_get(data
);
287 static void test_hbitmap_set_one(TestHBitmapData
*data
,
290 hbitmap_test_init(data
, 2 * L2
, 0);
291 hbitmap_test_set(data
, 10, 1);
292 hbitmap_test_set(data
, L1
- 1, 1);
293 hbitmap_test_set(data
, L1
, 1);
294 hbitmap_test_set(data
, L2
- 1, 1);
295 hbitmap_test_set(data
, L2
, 1);
298 static void test_hbitmap_set_two_elem(TestHBitmapData
*data
,
301 hbitmap_test_init(data
, 2 * L2
, 0);
302 hbitmap_test_set(data
, L1
- 1, 2);
303 hbitmap_test_set(data
, L1
* 2 - 1, 4);
304 hbitmap_test_set(data
, L1
* 4, L1
+ 1);
305 hbitmap_test_set(data
, L1
* 8 - 1, L1
+ 1);
306 hbitmap_test_set(data
, L2
- 1, 2);
307 hbitmap_test_set(data
, L2
+ L1
- 1, 8);
308 hbitmap_test_set(data
, L2
+ L1
* 4, L1
+ 1);
309 hbitmap_test_set(data
, L2
+ L1
* 8 - 1, L1
+ 1);
312 static void test_hbitmap_set(TestHBitmapData
*data
,
315 hbitmap_test_init(data
, L3
* 2, 0);
316 hbitmap_test_set(data
, L1
- 1, L1
+ 2);
317 hbitmap_test_set(data
, L1
* 3 - 1, L1
+ 2);
318 hbitmap_test_set(data
, L1
* 5, L1
* 2 + 1);
319 hbitmap_test_set(data
, L1
* 8 - 1, L1
* 2 + 1);
320 hbitmap_test_set(data
, L2
- 1, L1
+ 2);
321 hbitmap_test_set(data
, L2
+ L1
* 2 - 1, L1
+ 2);
322 hbitmap_test_set(data
, L2
+ L1
* 4, L1
* 2 + 1);
323 hbitmap_test_set(data
, L2
+ L1
* 7 - 1, L1
* 2 + 1);
324 hbitmap_test_set(data
, L2
* 2 - 1, L3
* 2 - L2
* 2);
327 static void test_hbitmap_set_twice(TestHBitmapData
*data
,
330 hbitmap_test_init(data
, L1
* 3, 0);
331 hbitmap_test_set(data
, 0, L1
* 3);
332 hbitmap_test_set(data
, L1
, 1);
335 static void test_hbitmap_set_overlap(TestHBitmapData
*data
,
338 hbitmap_test_init(data
, L3
* 2, 0);
339 hbitmap_test_set(data
, L1
- 1, L1
+ 2);
340 hbitmap_test_set(data
, L1
* 2 - 1, L1
* 2 + 2);
341 hbitmap_test_set(data
, 0, L1
* 3);
342 hbitmap_test_set(data
, L1
* 8 - 1, L2
);
343 hbitmap_test_set(data
, L2
, L1
);
344 hbitmap_test_set(data
, L2
- L1
- 1, L1
* 8 + 2);
345 hbitmap_test_set(data
, L2
, L3
- L2
+ 1);
346 hbitmap_test_set(data
, L3
- L1
, L1
* 3);
347 hbitmap_test_set(data
, L3
- 1, 3);
348 hbitmap_test_set(data
, L3
- 1, L2
);
351 static void test_hbitmap_reset_empty(TestHBitmapData
*data
,
354 hbitmap_test_init(data
, L3
, 0);
355 hbitmap_test_reset(data
, 0, L3
);
358 static void test_hbitmap_reset(TestHBitmapData
*data
,
361 hbitmap_test_init(data
, L3
* 2, 0);
362 hbitmap_test_set(data
, L1
- 1, L1
+ 2);
363 hbitmap_test_reset(data
, L1
* 2 - 1, L1
* 2 + 2);
364 hbitmap_test_set(data
, 0, L1
* 3);
365 hbitmap_test_reset(data
, L1
* 8 - 1, L2
);
366 hbitmap_test_set(data
, L2
, L1
);
367 hbitmap_test_reset(data
, L2
- L1
- 1, L1
* 8 + 2);
368 hbitmap_test_set(data
, L2
, L3
- L2
+ 1);
369 hbitmap_test_reset(data
, L3
- L1
, L1
* 3);
370 hbitmap_test_set(data
, L3
- 1, 3);
371 hbitmap_test_reset(data
, L3
- 1, L2
);
372 hbitmap_test_set(data
, 0, L3
* 2);
373 hbitmap_test_reset(data
, 0, L1
);
374 hbitmap_test_reset(data
, 0, L2
);
375 hbitmap_test_reset(data
, L3
, L3
);
376 hbitmap_test_set(data
, L3
/ 2, L3
);
379 static void test_hbitmap_reset_all(TestHBitmapData
*data
,
382 hbitmap_test_init(data
, L3
* 2, 0);
383 hbitmap_test_set(data
, L1
- 1, L1
+ 2);
384 hbitmap_test_reset_all(data
);
385 hbitmap_test_set(data
, 0, L1
* 3);
386 hbitmap_test_reset_all(data
);
387 hbitmap_test_set(data
, L2
, L1
);
388 hbitmap_test_reset_all(data
);
389 hbitmap_test_set(data
, L2
, L3
- L2
+ 1);
390 hbitmap_test_reset_all(data
);
391 hbitmap_test_set(data
, L3
- 1, 3);
392 hbitmap_test_reset_all(data
);
393 hbitmap_test_set(data
, 0, L3
* 2);
394 hbitmap_test_reset_all(data
);
395 hbitmap_test_set(data
, L3
/ 2, L3
);
396 hbitmap_test_reset_all(data
);
399 static void test_hbitmap_granularity(TestHBitmapData
*data
,
402 /* Note that hbitmap_test_check has to be invoked manually in this test. */
403 hbitmap_test_init(data
, L1
, 1);
404 hbitmap_test_set(data
, 0, 1);
405 g_assert_cmpint(hbitmap_count(data
->hb
), ==, 2);
406 hbitmap_test_check(data
, 0);
407 hbitmap_test_set(data
, 2, 1);
408 g_assert_cmpint(hbitmap_count(data
->hb
), ==, 4);
409 hbitmap_test_check(data
, 0);
410 hbitmap_test_set(data
, 0, 3);
411 g_assert_cmpint(hbitmap_count(data
->hb
), ==, 4);
412 hbitmap_test_reset(data
, 0, 1);
413 g_assert_cmpint(hbitmap_count(data
->hb
), ==, 2);
416 static void test_hbitmap_iter_granularity(TestHBitmapData
*data
,
421 /* Note that hbitmap_test_check has to be invoked manually in this test. */
422 hbitmap_test_init(data
, 131072 << 7, 7);
423 hbitmap_iter_init(&hbi
, data
->hb
, 0);
424 g_assert_cmpint(hbitmap_iter_next(&hbi
), <, 0);
426 hbitmap_test_set(data
, ((L2
+ L1
+ 1) << 7) + 8, 8);
427 hbitmap_iter_init(&hbi
, data
->hb
, 0);
428 g_assert_cmpint(hbitmap_iter_next(&hbi
), ==, (L2
+ L1
+ 1) << 7);
429 g_assert_cmpint(hbitmap_iter_next(&hbi
), <, 0);
431 hbitmap_iter_init(&hbi
, data
->hb
, (L2
+ L1
+ 2) << 7);
432 g_assert_cmpint(hbitmap_iter_next(&hbi
), <, 0);
434 hbitmap_test_set(data
, (131072 << 7) - 8, 8);
435 hbitmap_iter_init(&hbi
, data
->hb
, 0);
436 g_assert_cmpint(hbitmap_iter_next(&hbi
), ==, (L2
+ L1
+ 1) << 7);
437 g_assert_cmpint(hbitmap_iter_next(&hbi
), ==, 131071 << 7);
438 g_assert_cmpint(hbitmap_iter_next(&hbi
), <, 0);
440 hbitmap_iter_init(&hbi
, data
->hb
, (L2
+ L1
+ 2) << 7);
441 g_assert_cmpint(hbitmap_iter_next(&hbi
), ==, 131071 << 7);
442 g_assert_cmpint(hbitmap_iter_next(&hbi
), <, 0);
445 static void hbitmap_test_set_boundary_bits(TestHBitmapData
*data
, ssize_t diff
)
447 size_t size
= data
->size
;
450 hbitmap_test_set(data
, 0, 1);
452 /* Last bit in new, shortened map */
453 hbitmap_test_set(data
, size
+ diff
- 1, 1);
455 /* First bit to be truncated away */
456 hbitmap_test_set(data
, size
+ diff
, 1);
459 hbitmap_test_set(data
, size
- 1, 1);
460 if (data
->granularity
== 0) {
461 hbitmap_test_check_get(data
);
465 static void hbitmap_test_check_boundary_bits(TestHBitmapData
*data
)
467 size_t size
= MIN(data
->size
, data
->old_size
);
469 if (data
->granularity
== 0) {
470 hbitmap_test_check_get(data
);
471 hbitmap_test_check(data
, 0);
473 /* If a granularity was set, note that every distinct
474 * (bit >> granularity) value that was set will increase
475 * the bit pop count by 2^granularity, not just 1.
477 * The hbitmap_test_check facility does not currently tolerate
478 * non-zero granularities, so test the boundaries and the population
481 g_assert(hbitmap_get(data
->hb
, 0));
482 g_assert(hbitmap_get(data
->hb
, size
- 1));
483 g_assert_cmpint(2 << data
->granularity
, ==, hbitmap_count(data
->hb
));
487 /* Generic truncate test. */
488 static void hbitmap_test_truncate(TestHBitmapData
*data
,
493 hbitmap_test_init(data
, size
, granularity
);
494 hbitmap_test_set_boundary_bits(data
, diff
);
495 hbitmap_test_truncate_impl(data
, size
+ diff
);
496 hbitmap_test_check_boundary_bits(data
);
499 static void test_hbitmap_truncate_nop(TestHBitmapData
*data
,
502 hbitmap_test_truncate(data
, L2
, 0, 0);
506 * Grow by an amount smaller than the granularity, without crossing
507 * a granularity alignment boundary. Effectively a NOP.
509 static void test_hbitmap_truncate_grow_negligible(TestHBitmapData
*data
,
512 size_t size
= L2
- 1;
516 hbitmap_test_truncate(data
, size
, diff
, granularity
);
520 * Shrink by an amount smaller than the granularity, without crossing
521 * a granularity alignment boundary. Effectively a NOP.
523 static void test_hbitmap_truncate_shrink_negligible(TestHBitmapData
*data
,
530 hbitmap_test_truncate(data
, size
, diff
, granularity
);
534 * Grow by an amount smaller than the granularity, but crossing over
535 * a granularity alignment boundary.
537 static void test_hbitmap_truncate_grow_tiny(TestHBitmapData
*data
,
540 size_t size
= L2
- 2;
544 hbitmap_test_truncate(data
, size
, diff
, granularity
);
548 * Shrink by an amount smaller than the granularity, but crossing over
549 * a granularity alignment boundary.
551 static void test_hbitmap_truncate_shrink_tiny(TestHBitmapData
*data
,
554 size_t size
= L2
- 1;
558 hbitmap_test_truncate(data
, size
, diff
, granularity
);
562 * Grow by an amount smaller than sizeof(long), and not crossing over
563 * a sizeof(long) alignment boundary.
565 static void test_hbitmap_truncate_grow_small(TestHBitmapData
*data
,
568 size_t size
= L2
+ 1;
569 size_t diff
= sizeof(long) / 2;
571 hbitmap_test_truncate(data
, size
, diff
, 0);
575 * Shrink by an amount smaller than sizeof(long), and not crossing over
576 * a sizeof(long) alignment boundary.
578 static void test_hbitmap_truncate_shrink_small(TestHBitmapData
*data
,
582 size_t diff
= sizeof(long) / 2;
584 hbitmap_test_truncate(data
, size
, -diff
, 0);
588 * Grow by an amount smaller than sizeof(long), while crossing over
589 * a sizeof(long) alignment boundary.
591 static void test_hbitmap_truncate_grow_medium(TestHBitmapData
*data
,
594 size_t size
= L2
- 1;
595 size_t diff
= sizeof(long) / 2;
597 hbitmap_test_truncate(data
, size
, diff
, 0);
601 * Shrink by an amount smaller than sizeof(long), while crossing over
602 * a sizeof(long) alignment boundary.
604 static void test_hbitmap_truncate_shrink_medium(TestHBitmapData
*data
,
607 size_t size
= L2
+ 1;
608 size_t diff
= sizeof(long) / 2;
610 hbitmap_test_truncate(data
, size
, -diff
, 0);
614 * Grow by an amount larger than sizeof(long).
616 static void test_hbitmap_truncate_grow_large(TestHBitmapData
*data
,
620 size_t diff
= 8 * sizeof(long);
622 hbitmap_test_truncate(data
, size
, diff
, 0);
626 * Shrink by an amount larger than sizeof(long).
628 static void test_hbitmap_truncate_shrink_large(TestHBitmapData
*data
,
632 size_t diff
= 8 * sizeof(long);
634 hbitmap_test_truncate(data
, size
, -diff
, 0);
637 static void hbitmap_test_add(const char *testpath
,
638 void (*test_func
)(TestHBitmapData
*data
, const void *user_data
))
640 g_test_add(testpath
, TestHBitmapData
, NULL
, NULL
, test_func
,
641 hbitmap_test_teardown
);
644 int main(int argc
, char **argv
)
646 g_test_init(&argc
, &argv
, NULL
);
647 hbitmap_test_add("/hbitmap/size/0", test_hbitmap_zero
);
648 hbitmap_test_add("/hbitmap/size/unaligned", test_hbitmap_unaligned
);
649 hbitmap_test_add("/hbitmap/iter/empty", test_hbitmap_iter_empty
);
650 hbitmap_test_add("/hbitmap/iter/partial", test_hbitmap_iter_partial
);
651 hbitmap_test_add("/hbitmap/iter/granularity", test_hbitmap_iter_granularity
);
652 hbitmap_test_add("/hbitmap/get/all", test_hbitmap_get_all
);
653 hbitmap_test_add("/hbitmap/get/some", test_hbitmap_get_some
);
654 hbitmap_test_add("/hbitmap/set/all", test_hbitmap_set_all
);
655 hbitmap_test_add("/hbitmap/set/one", test_hbitmap_set_one
);
656 hbitmap_test_add("/hbitmap/set/two-elem", test_hbitmap_set_two_elem
);
657 hbitmap_test_add("/hbitmap/set/general", test_hbitmap_set
);
658 hbitmap_test_add("/hbitmap/set/twice", test_hbitmap_set_twice
);
659 hbitmap_test_add("/hbitmap/set/overlap", test_hbitmap_set_overlap
);
660 hbitmap_test_add("/hbitmap/reset/empty", test_hbitmap_reset_empty
);
661 hbitmap_test_add("/hbitmap/reset/general", test_hbitmap_reset
);
662 hbitmap_test_add("/hbitmap/reset/all", test_hbitmap_reset_all
);
663 hbitmap_test_add("/hbitmap/granularity", test_hbitmap_granularity
);
665 hbitmap_test_add("/hbitmap/truncate/nop", test_hbitmap_truncate_nop
);
666 hbitmap_test_add("/hbitmap/truncate/grow/negligible",
667 test_hbitmap_truncate_grow_negligible
);
668 hbitmap_test_add("/hbitmap/truncate/shrink/negligible",
669 test_hbitmap_truncate_shrink_negligible
);
670 hbitmap_test_add("/hbitmap/truncate/grow/tiny",
671 test_hbitmap_truncate_grow_tiny
);
672 hbitmap_test_add("/hbitmap/truncate/shrink/tiny",
673 test_hbitmap_truncate_shrink_tiny
);
674 hbitmap_test_add("/hbitmap/truncate/grow/small",
675 test_hbitmap_truncate_grow_small
);
676 hbitmap_test_add("/hbitmap/truncate/shrink/small",
677 test_hbitmap_truncate_shrink_small
);
678 hbitmap_test_add("/hbitmap/truncate/grow/medium",
679 test_hbitmap_truncate_grow_medium
);
680 hbitmap_test_add("/hbitmap/truncate/shrink/medium",
681 test_hbitmap_truncate_shrink_medium
);
682 hbitmap_test_add("/hbitmap/truncate/grow/large",
683 test_hbitmap_truncate_grow_large
);
684 hbitmap_test_add("/hbitmap/truncate/shrink/large",
685 test_hbitmap_truncate_shrink_large
);