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 int __rte_deprecated
97 rte_bsf64(uint64_t slab, uint32_t *pos)
99 return rte_bsf64_safe(slab, pos);
102 static inline uint32_t
103 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
104 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
105 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
107 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
108 uint32_t n_cache_lines_array2;
109 uint32_t n_bytes_total;
111 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
112 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
113 n_slabs_array1 = rte_align32pow2(n_slabs_array1);
114 n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
115 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
116 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
118 if (array1_byte_offset) {
119 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
122 *array1_slabs = n_slabs_array1;
124 if (array2_byte_offset) {
125 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
128 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
131 return n_bytes_total;
135 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
137 bmp->index1 = bmp->array1_size - 1;
138 bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
139 __rte_bitmap_index2_set(bmp);
140 bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
146 * Bitmap memory footprint calculation
149 * Number of bits in the bitmap
151 * Bitmap memory footprint measured in bytes on success, 0 on error
153 static inline uint32_t
154 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
155 /* Check input arguments */
160 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
164 * Bitmap initialization
167 * Number of pre-allocated bits in array2.
169 * Base address of array1 and array2.
171 * Minimum expected size of bitmap.
173 * Handle to bitmap instance.
175 static inline struct rte_bitmap *
176 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
178 struct rte_bitmap *bmp;
179 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
182 /* Check input arguments */
187 if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
191 size = __rte_bitmap_get_memory_footprint(n_bits,
192 &array1_byte_offset, &array1_slabs,
193 &array2_byte_offset, &array2_slabs);
194 if (size < mem_size) {
199 memset(mem, 0, size);
200 bmp = (struct rte_bitmap *) mem;
202 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
203 bmp->array1_size = array1_slabs;
204 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
205 bmp->array2_size = array2_slabs;
207 __rte_bitmap_scan_init(bmp);
216 * Handle to bitmap instance
218 * 0 upon success, error code otherwise
221 rte_bitmap_free(struct rte_bitmap *bmp)
223 /* Check input arguments */
235 * Handle to bitmap instance
238 rte_bitmap_reset(struct rte_bitmap *bmp)
240 memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
241 memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
242 __rte_bitmap_scan_init(bmp);
246 * Bitmap location prefetch into CPU L1 cache
249 * Handle to bitmap instance
253 * 0 upon success, error code otherwise
256 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
261 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
262 slab2 = bmp->array2 + index2;
263 rte_prefetch0((void *) slab2);
270 * Handle to bitmap instance
274 * 0 when bit is cleared, non-zero when bit is set
276 static inline uint64_t
277 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
280 uint32_t index2, offset2;
282 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
283 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
284 slab2 = bmp->array2 + index2;
285 return (*slab2) & (1llu << offset2);
292 * Handle to bitmap instance
297 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
299 uint64_t *slab1, *slab2;
300 uint32_t index1, index2, offset1, offset2;
302 /* Set bit in array2 slab and set bit in array1 slab */
303 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
304 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
305 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
306 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
307 slab2 = bmp->array2 + index2;
308 slab1 = bmp->array1 + index1;
310 *slab2 |= 1llu << offset2;
311 *slab1 |= 1llu << offset1;
318 * Handle to bitmap instance
320 * Bit position identifying the array2 slab
322 * Value to be assigned to the 64-bit slab in array2
325 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
327 uint64_t *slab1, *slab2;
328 uint32_t index1, index2, offset1;
330 /* Set bits in array2 slab and set bit in array1 slab */
331 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
332 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
333 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
334 slab2 = bmp->array2 + index2;
335 slab1 = bmp->array1 + index1;
338 *slab1 |= 1llu << offset1;
341 static inline uint64_t
342 __rte_bitmap_line_not_empty(uint64_t *slab2)
344 uint64_t v1, v2, v3, v4;
346 v1 = slab2[0] | slab2[1];
347 v2 = slab2[2] | slab2[3];
348 v3 = slab2[4] | slab2[5];
349 v4 = slab2[6] | slab2[7];
360 * Handle to bitmap instance
365 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
367 uint64_t *slab1, *slab2;
368 uint32_t index1, index2, offset1, offset2;
370 /* Clear bit in array2 slab */
371 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
372 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
373 slab2 = bmp->array2 + index2;
375 /* Return if array2 slab is not all-zeros */
376 *slab2 &= ~(1llu << offset2);
381 /* Check the entire cache line of array2 for all-zeros */
382 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
383 slab2 = bmp->array2 + index2;
384 if (__rte_bitmap_line_not_empty(slab2)) {
388 /* The array2 cache line is all-zeros, so clear bit in array1 slab */
389 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
390 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
391 slab1 = bmp->array1 + index1;
392 *slab1 &= ~(1llu << offset1);
398 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
403 /* Check current array1 slab */
404 value1 = bmp->array1[bmp->index1];
405 value1 &= __rte_bitmap_mask1_get(bmp);
407 if (rte_bsf64_safe(value1, &bmp->offset1))
410 __rte_bitmap_index1_inc(bmp);
413 /* Look for another array1 slab */
414 for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
415 value1 = bmp->array1[bmp->index1];
417 if (rte_bsf64_safe(value1, &bmp->offset1))
425 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
427 __rte_bitmap_index2_set(bmp);
429 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
433 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
437 slab2 = bmp->array2 + bmp->index2;
438 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
440 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
445 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
454 * Bitmap scan (with automatic wrap-around)
457 * Handle to bitmap instance
459 * When function call returns 1, pos contains the position of the next set
460 * bit, otherwise not modified
462 * When function call returns 1, slab contains the value of the entire 64-bit
463 * slab where the bit indicated by pos is located. Slabs are always 64-bit
464 * aligned, so the position of the first bit of the slab (this bit is not
465 * necessarily set) is pos / 64. Once a slab has been returned by the bitmap
466 * scan operation, the internal pointers of the bitmap are updated to point
467 * after this slab, so the same slab will not be returned again if it
468 * contains more than one bit which is set. When function call returns 0,
469 * slab is not modified.
471 * 0 if there is no bit set in the bitmap, 1 otherwise
474 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
476 /* Return data from current array2 line if available */
477 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
481 /* Look for non-empty array2 line */
482 if (__rte_bitmap_scan_search(bmp)) {
483 __rte_bitmap_scan_read_init(bmp);
484 __rte_bitmap_scan_read(bmp, pos, slab);
496 #endif /* __INCLUDE_RTE_BITMAP_H__ */