1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Cavium, Inc
8 #include <rte_common.h>
9 #include <rte_bitmap.h>
10 #include <rte_malloc.h>
17 test_bitmap_scan_operations(struct rte_bitmap *bmp)
19 uint64_t slab1_magic = 0xBADC0FFEEBADF00D;
20 uint64_t slab2_magic = 0xFEEDDEADDEADF00D;
21 uint32_t pos = 0, start_pos;
22 int i, nb_clear, nb_set;
23 uint64_t out_slab = 0;
25 rte_bitmap_reset(bmp);
27 rte_bitmap_set_slab(bmp, pos, slab1_magic);
28 rte_bitmap_set_slab(bmp, pos + RTE_BITMAP_SLAB_BIT_SIZE, slab2_magic);
30 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
31 printf("Failed to get slab from bitmap.\n");
35 if (slab1_magic != out_slab) {
36 printf("Scan operation sanity failed.\n");
40 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
41 printf("Failed to get slab from bitmap.\n");
45 if (slab2_magic != out_slab) {
46 printf("Scan operation sanity failed.\n");
51 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
52 printf("Failed to get slab from bitmap.\n");
56 if (slab1_magic != out_slab) {
57 printf("Scan operation wrap around failed.\n");
61 /* Scan reset check. */
62 __rte_bitmap_scan_init(bmp);
64 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
65 printf("Failed to get slab from bitmap.\n");
69 if (slab1_magic != out_slab) {
70 printf("Scan reset operation failed.\n");
74 /* Test scan when a cline is half full */
75 rte_bitmap_reset(bmp);
76 for (i = 0; i < MAX_BITS; i++)
77 rte_bitmap_set(bmp, i);
79 nb_clear = RTE_MIN(RTE_BITMAP_CL_BIT_SIZE / 2, MAX_BITS);
80 for (i = 0; i < nb_clear; i++)
81 rte_bitmap_clear(bmp, i);
83 /* Find remaining bits set in bmp */
84 __rte_bitmap_scan_init(bmp);
86 if (rte_bitmap_scan(bmp, &pos, &out_slab) != 1) {
87 printf("Initial scan failed with half CL empty.\n");
94 nb_set += __builtin_popcountll(out_slab);
95 if (!rte_bitmap_scan(bmp, &pos, &out_slab))
97 } while (pos != start_pos);
99 if ((nb_clear + nb_set) != MAX_BITS) {
100 printf("Scan failed to find all set bits. "
101 "Expected %u, found %u.\n", MAX_BITS - nb_clear, nb_set);
109 test_bitmap_slab_set_get(struct rte_bitmap *bmp)
112 uint64_t slab_magic = 0xBADC0FFEEBADF00D;
113 uint64_t out_slab = 0;
115 rte_bitmap_reset(bmp);
116 rte_bitmap_set_slab(bmp, pos, slab_magic);
118 if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
119 printf("Failed to get slab from bitmap.\n");
124 if (slab_magic != out_slab) {
125 printf("Invalid slab in bitmap.\n");
134 test_bitmap_set_get_clear(struct rte_bitmap *bmp)
139 rte_bitmap_reset(bmp);
140 for (i = 0; i < MAX_BITS; i++)
141 rte_bitmap_set(bmp, i);
143 for (i = 0; i < MAX_BITS; i++) {
144 if (!rte_bitmap_get(bmp, i)) {
145 printf("Failed to get set bit.\n");
150 for (i = 0; i < MAX_BITS; i++)
151 rte_bitmap_clear(bmp, i);
153 for (i = 0; i < MAX_BITS; i++) {
154 if (rte_bitmap_get(bmp, i)) {
155 printf("Failed to clear set bit.\n");
160 rte_bitmap_reset(bmp);
162 /* Alternate slab set test */
163 for (i = 0; i < MAX_BITS; i++) {
164 if (i % RTE_BITMAP_SLAB_BIT_SIZE)
165 rte_bitmap_set(bmp, i);
168 for (i = 0; i < MAX_BITS; i++) {
169 val = rte_bitmap_get(bmp, i);
170 if (((i % RTE_BITMAP_SLAB_BIT_SIZE) && !val) ||
171 (!(i % RTE_BITMAP_SLAB_BIT_SIZE) && val)) {
172 printf("Failed to get set bit.\n");
181 test_bitmap_all_clear(void)
185 struct rte_bitmap *bmp;
188 rte_bitmap_get_memory_footprint(MAX_BITS);
190 mem = rte_zmalloc("test_bmap", bmp_size, RTE_CACHE_LINE_SIZE);
192 printf("Failed to allocate memory for bitmap\n");
196 bmp = rte_bitmap_init(MAX_BITS, mem, bmp_size);
198 printf("Failed to init bitmap\n");
202 if (test_bitmap_set_get_clear(bmp) < 0)
205 if (test_bitmap_slab_set_get(bmp) < 0)
208 if (test_bitmap_scan_operations(bmp) < 0)
211 rte_bitmap_free(bmp);
218 test_bitmap_all_set(void)
225 struct rte_bitmap *bmp;
228 rte_bitmap_get_memory_footprint(MAX_BITS);
230 mem = rte_zmalloc("test_bmap", bmp_size, RTE_CACHE_LINE_SIZE);
232 printf("Failed to allocate memory for bitmap\n");
236 bmp = rte_bitmap_init_with_all_set(MAX_BITS, mem, bmp_size);
238 printf("Failed to init bitmap\n");
242 for (i = 0; i < MAX_BITS; i++) {
244 if (!rte_bitmap_scan(bmp, &pos, &slab)) {
245 printf("Failed with init bitmap.\n");
248 pos += (slab ? __builtin_ctzll(slab) : 0);
249 rte_bitmap_clear(bmp, pos);
252 if (rte_bitmap_scan(bmp, &pos, &slab)) {
253 printf("Too much bits set.\n");
257 rte_bitmap_free(bmp);
267 if (test_bitmap_all_clear() != TEST_SUCCESS)
269 return test_bitmap_all_set();
272 REGISTER_TEST_COMMAND(bitmap_test, test_bitmap);