1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef __INCLUDE_RTE_BITMAP_H__
6 #define __INCLUDE_RTE_BITMAP_H__
16 * The bitmap component provides a mechanism to manage large arrays of bits
17 * through bit get/set/clear and bit array scan operations.
19 * The bitmap scan operation is optimized for 64-bit CPUs using 64/128 byte cache
20 * lines. The bitmap is hierarchically organized using two arrays (array1 and
21 * array2), with each bit in array1 being associated with a full cache line
22 * (512/1024 bits) of bitmap bits, which are stored in array2: the bit in array1
23 * is set only when there is at least one bit set within its associated array2
24 * bits, otherwise the bit in array1 is cleared. The read and write operations
25 * for array1 and array2 are always done in slabs of 64 bits.
27 * This bitmap is not thread safe. For lock free operation on a specific bitmap
28 * instance, a single writer thread performing bit set/clear operations is
29 * allowed, only the writer thread can do bitmap scan operations, while there
30 * can be several reader threads performing bit get operations in parallel with
31 * the writer thread. When the use of locking primitives is acceptable, the
32 * serialization of the bit set/clear and bitmap scan operations needs to be
33 * enforced by the caller, while the bit get operation does not require locking
39 #include <rte_common.h>
40 #include <rte_config.h>
41 #include <rte_debug.h>
42 #include <rte_memory.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_prefetch.h>
47 #define RTE_BITMAP_SLAB_BIT_SIZE 64
48 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
49 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
52 #define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
53 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
54 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
56 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
57 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
58 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
60 /** Bitmap data structure */
62 /* Context for array1 and array2 */
63 uint64_t *array1; /**< Bitmap array1 */
64 uint64_t *array2; /**< Bitmap array2 */
65 uint32_t array1_size; /**< Number of 64-bit slabs in array1 that are actually used */
66 uint32_t array2_size; /**< Number of 64-bit slabs in array2 */
68 /* Context for the "scan next" operation */
69 uint32_t index1; /**< Bitmap scan: Index of current array1 slab */
70 uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
71 uint32_t index2; /**< Bitmap scan: Index of current array2 slab */
72 uint32_t go2; /**< Bitmap scan: Go/stop condition for current array2 cache line */
74 /* Storage space for array1 and array2 */
79 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
81 bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
84 static inline uint64_t
85 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
87 return (~1llu) << bmp->offset1;
91 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
93 bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
96 static inline uint32_t
97 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
98 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
99 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
101 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
102 uint32_t n_cache_lines_array2;
103 uint32_t n_bytes_total;
105 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
106 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
107 n_slabs_array1 = rte_align32pow2(n_slabs_array1);
108 n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
109 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
110 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
112 if (array1_byte_offset) {
113 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
116 *array1_slabs = n_slabs_array1;
118 if (array2_byte_offset) {
119 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
122 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
125 return n_bytes_total;
129 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
131 bmp->index1 = bmp->array1_size - 1;
132 bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
133 __rte_bitmap_index2_set(bmp);
134 bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
140 * Bitmap memory footprint calculation
143 * Number of bits in the bitmap
145 * Bitmap memory footprint measured in bytes on success, 0 on error
147 static inline uint32_t
148 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
149 /* Check input arguments */
154 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
158 * Bitmap initialization
161 * Number of pre-allocated bits in array2.
163 * Base address of array1 and array2.
165 * Minimum expected size of bitmap.
167 * Handle to bitmap instance.
169 static inline struct rte_bitmap *
170 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
172 struct rte_bitmap *bmp;
173 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
176 /* Check input arguments */
181 if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
185 size = __rte_bitmap_get_memory_footprint(n_bits,
186 &array1_byte_offset, &array1_slabs,
187 &array2_byte_offset, &array2_slabs);
192 memset(mem, 0, size);
193 bmp = (struct rte_bitmap *) mem;
195 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
196 bmp->array1_size = array1_slabs;
197 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
198 bmp->array2_size = array2_slabs;
200 __rte_bitmap_scan_init(bmp);
207 * @b EXPERIMENTAL: this API may change without prior notice.
209 * Bitmap clear slab overhead bits.
214 * Number of 64-bit slabs in the slabs array.
216 * The start bit position in the slabs to be cleared.
220 __rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size,
224 uint32_t index = pos / RTE_BITMAP_SLAB_BIT_SIZE;
225 uint32_t offset = pos & RTE_BITMAP_SLAB_BIT_MASK;
228 for (i = offset; i < RTE_BITMAP_SLAB_BIT_SIZE; i++)
229 slabs[index] &= ~(1llu << i);
232 if (index < slab_size)
233 memset(&slabs[index], 0, sizeof(slabs[0]) *
234 (slab_size - index));
239 * @b EXPERIMENTAL: this API may change without prior notice.
241 * Bitmap initialization with all bits set
244 * Number of pre-allocated bits in array2.
246 * Base address of array1 and array2.
248 * Minimum expected size of bitmap.
250 * Handle to bitmap instance.
253 static inline struct rte_bitmap *
254 rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
256 struct rte_bitmap *bmp;
257 uint32_t array1_byte_offset, array1_slabs;
258 uint32_t array2_byte_offset, array2_slabs;
261 /* Check input arguments */
262 if (!n_bits || !mem || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK))
265 size = __rte_bitmap_get_memory_footprint(n_bits,
266 &array1_byte_offset, &array1_slabs,
267 &array2_byte_offset, &array2_slabs);
272 bmp = (struct rte_bitmap *) mem;
273 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
274 bmp->array1_size = array1_slabs;
275 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
276 bmp->array2_size = array2_slabs;
278 __rte_bitmap_scan_init(bmp);
280 memset(bmp->array1, 0xff, bmp->array1_size * sizeof(bmp->array1[0]));
281 memset(bmp->array2, 0xff, bmp->array2_size * sizeof(bmp->array2[0]));
282 /* Clear overhead bits. */
283 __rte_bitmap_clear_slab_overhead_bits(bmp->array1, bmp->array1_size,
284 bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2);
285 __rte_bitmap_clear_slab_overhead_bits(bmp->array2, bmp->array2_size,
294 * Handle to bitmap instance
296 * 0 upon success, error code otherwise
299 rte_bitmap_free(struct rte_bitmap *bmp)
301 /* Check input arguments */
313 * Handle to bitmap instance
316 rte_bitmap_reset(struct rte_bitmap *bmp)
318 memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
319 memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
320 __rte_bitmap_scan_init(bmp);
324 * Bitmap location prefetch into CPU L1 cache
327 * Handle to bitmap instance
331 * 0 upon success, error code otherwise
334 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
339 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
340 slab2 = bmp->array2 + index2;
341 rte_prefetch0((void *) slab2);
348 * Handle to bitmap instance
352 * 0 when bit is cleared, non-zero when bit is set
354 static inline uint64_t
355 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
358 uint32_t index2, offset2;
360 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
361 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
362 slab2 = bmp->array2 + index2;
363 return (*slab2) & (1llu << offset2);
370 * Handle to bitmap instance
375 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
377 uint64_t *slab1, *slab2;
378 uint32_t index1, index2, offset1, offset2;
380 /* Set bit in array2 slab and set bit in array1 slab */
381 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
382 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
383 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
384 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
385 slab2 = bmp->array2 + index2;
386 slab1 = bmp->array1 + index1;
388 *slab2 |= 1llu << offset2;
389 *slab1 |= 1llu << offset1;
396 * Handle to bitmap instance
398 * Bit position identifying the array2 slab
400 * Value to be assigned to the 64-bit slab in array2
403 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
405 uint64_t *slab1, *slab2;
406 uint32_t index1, index2, offset1;
408 /* Set bits in array2 slab and set bit in array1 slab */
409 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
410 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
411 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
412 slab2 = bmp->array2 + index2;
413 slab1 = bmp->array1 + index1;
416 *slab1 |= 1llu << offset1;
419 #if RTE_BITMAP_CL_SLAB_SIZE == 8
420 static inline uint64_t
421 __rte_bitmap_line_not_empty(uint64_t *slab2)
423 uint64_t v1, v2, v3, v4;
425 v1 = slab2[0] | slab2[1];
426 v2 = slab2[2] | slab2[3];
427 v3 = slab2[4] | slab2[5];
428 v4 = slab2[6] | slab2[7];
435 #elif RTE_BITMAP_CL_SLAB_SIZE == 16
436 static inline uint64_t
437 __rte_bitmap_line_not_empty(uint64_t *slab2)
439 uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
441 v1 = slab2[0] | slab2[1];
442 v2 = slab2[2] | slab2[3];
443 v3 = slab2[4] | slab2[5];
444 v4 = slab2[6] | slab2[7];
445 v5 = slab2[8] | slab2[9];
446 v6 = slab2[10] | slab2[11];
447 v7 = slab2[12] | slab2[13];
448 v8 = slab2[14] | slab2[15];
454 return v1 | v3 | v5 | v7;
457 #endif /* RTE_BITMAP_CL_SLAB_SIZE */
463 * Handle to bitmap instance
468 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
470 uint64_t *slab1, *slab2;
471 uint32_t index1, index2, offset1, offset2;
473 /* Clear bit in array2 slab */
474 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
475 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
476 slab2 = bmp->array2 + index2;
478 /* Return if array2 slab is not all-zeros */
479 *slab2 &= ~(1llu << offset2);
484 /* Check the entire cache line of array2 for all-zeros */
485 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
486 slab2 = bmp->array2 + index2;
487 if (__rte_bitmap_line_not_empty(slab2)) {
491 /* The array2 cache line is all-zeros, so clear bit in array1 slab */
492 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
493 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
494 slab1 = bmp->array1 + index1;
495 *slab1 &= ~(1llu << offset1);
501 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
506 /* Check current array1 slab */
507 value1 = bmp->array1[bmp->index1];
508 value1 &= __rte_bitmap_mask1_get(bmp);
510 if (rte_bsf64_safe(value1, &bmp->offset1))
513 __rte_bitmap_index1_inc(bmp);
516 /* Look for another array1 slab */
517 for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
518 value1 = bmp->array1[bmp->index1];
520 if (rte_bsf64_safe(value1, &bmp->offset1))
528 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
530 __rte_bitmap_index2_set(bmp);
532 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
536 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
540 slab2 = bmp->array2 + bmp->index2;
541 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
543 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
548 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
557 * Bitmap scan (with automatic wrap-around)
560 * Handle to bitmap instance
562 * When function call returns 1, pos contains the position of the next set
563 * bit, otherwise not modified
565 * When function call returns 1, slab contains the value of the entire 64-bit
566 * slab where the bit indicated by pos is located. Slabs are always 64-bit
567 * aligned, so the position of the first bit of the slab (this bit is not
568 * necessarily set) is pos / 64. Once a slab has been returned by the bitmap
569 * scan operation, the internal pointers of the bitmap are updated to point
570 * after this slab, so the same slab will not be returned again if it
571 * contains more than one bit which is set. When function call returns 0,
572 * slab is not modified.
574 * 0 if there is no bit set in the bitmap, 1 otherwise
577 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
579 /* Return data from current array2 line if available */
580 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
584 /* Look for non-empty array2 line */
585 if (__rte_bitmap_scan_search(bmp)) {
586 __rte_bitmap_scan_read_init(bmp);
587 __rte_bitmap_scan_read(bmp, pos, slab);
599 #endif /* __INCLUDE_RTE_BITMAP_H__ */