4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef __INCLUDE_RTE_BITMAP_H__
35 #define __INCLUDE_RTE_BITMAP_H__
45 * The bitmap component provides a mechanism to manage large arrays of bits
46 * through bit get/set/clear and bit array scan operations.
48 * The bitmap scan operation is optimized for 64-bit CPUs using 64/128 byte cache
49 * lines. The bitmap is hierarchically organized using two arrays (array1 and
50 * array2), with each bit in array1 being associated with a full cache line
51 * (512/1024 bits) of bitmap bits, which are stored in array2: the bit in array1
52 * is set only when there is at least one bit set within its associated array2
53 * bits, otherwise the bit in array1 is cleared. The read and write operations
54 * for array1 and array2 are always done in slabs of 64 bits.
56 * This bitmap is not thread safe. For lock free operation on a specific bitmap
57 * instance, a single writer thread performing bit set/clear operations is
58 * allowed, only the writer thread can do bitmap scan operations, while there
59 * can be several reader threads performing bit get operations in parallel with
60 * the writer thread. When the use of locking primitives is acceptable, the
61 * serialization of the bit set/clear and bitmap scan operations needs to be
62 * enforced by the caller, while the bit get operation does not require locking
67 #include <rte_common.h>
68 #include <rte_debug.h>
69 #include <rte_memory.h>
70 #include <rte_branch_prediction.h>
71 #include <rte_prefetch.h>
73 #ifndef RTE_BITMAP_OPTIMIZATIONS
74 #define RTE_BITMAP_OPTIMIZATIONS 1
78 #define RTE_BITMAP_SLAB_BIT_SIZE 64
79 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
80 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
83 #define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
84 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
85 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
87 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
88 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
89 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
91 /** Bitmap data structure */
93 /* Context for array1 and array2 */
94 uint64_t *array1; /**< Bitmap array1 */
95 uint64_t *array2; /**< Bitmap array2 */
96 uint32_t array1_size; /**< Number of 64-bit slabs in array1 that are actually used */
97 uint32_t array2_size; /**< Number of 64-bit slabs in array2 */
99 /* Context for the "scan next" operation */
100 uint32_t index1; /**< Bitmap scan: Index of current array1 slab */
101 uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
102 uint32_t index2; /**< Bitmap scan: Index of current array2 slab */
103 uint32_t go2; /**< Bitmap scan: Go/stop condition for current array2 cache line */
105 /* Storage space for array1 and array2 */
110 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
112 bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
115 static inline uint64_t
116 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
118 return (~1lu) << bmp->offset1;
122 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
124 bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
127 #if RTE_BITMAP_OPTIMIZATIONS
130 rte_bsf64(uint64_t slab, uint32_t *pos)
132 if (likely(slab == 0)) {
136 *pos = __builtin_ctzll(slab);
143 rte_bsf64(uint64_t slab, uint32_t *pos)
148 if (likely(slab == 0)) {
152 for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
153 if (unlikely(slab & mask)) {
164 static inline uint32_t
165 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
166 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
167 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
169 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
170 uint32_t n_cache_lines_array2;
171 uint32_t n_bytes_total;
173 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
174 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
175 n_slabs_array1 = rte_align32pow2(n_slabs_array1);
176 n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
177 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
178 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
180 if (array1_byte_offset) {
181 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
184 *array1_slabs = n_slabs_array1;
186 if (array2_byte_offset) {
187 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
190 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
193 return n_bytes_total;
197 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
199 bmp->index1 = bmp->array1_size - 1;
200 bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
201 __rte_bitmap_index2_set(bmp);
202 bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
208 * Bitmap memory footprint calculation
211 * Number of bits in the bitmap
213 * Bitmap memory footprint measured in bytes on success, 0 on error
215 static inline uint32_t
216 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
217 /* Check input arguments */
222 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
226 * Bitmap initialization
229 * Minimum expected size of bitmap.
231 * Base address of array1 and array2.
233 * Number of pre-allocated bits in array2. Must be non-zero and multiple of 512.
235 * Handle to bitmap instance.
237 static inline struct rte_bitmap *
238 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
240 struct rte_bitmap *bmp;
241 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
244 /* Check input arguments */
249 if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
253 size = __rte_bitmap_get_memory_footprint(n_bits,
254 &array1_byte_offset, &array1_slabs,
255 &array2_byte_offset, &array2_slabs);
256 if (size < mem_size) {
261 memset(mem, 0, size);
262 bmp = (struct rte_bitmap *) mem;
264 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
265 bmp->array1_size = array1_slabs;
266 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
267 bmp->array2_size = array2_slabs;
269 __rte_bitmap_scan_init(bmp);
278 * Handle to bitmap instance
280 * 0 upon success, error code otherwise
283 rte_bitmap_free(struct rte_bitmap *bmp)
285 /* Check input arguments */
297 * Handle to bitmap instance
300 rte_bitmap_reset(struct rte_bitmap *bmp)
302 memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
303 memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
304 __rte_bitmap_scan_init(bmp);
308 * Bitmap location prefetch into CPU L1 cache
311 * Handle to bitmap instance
315 * 0 upon success, error code otherwise
318 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
323 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
324 slab2 = bmp->array2 + index2;
325 rte_prefetch0((void *) slab2);
332 * Handle to bitmap instance
336 * 0 when bit is cleared, non-zero when bit is set
338 static inline uint64_t
339 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
342 uint32_t index2, offset2;
344 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
345 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
346 slab2 = bmp->array2 + index2;
347 return (*slab2) & (1lu << offset2);
354 * Handle to bitmap instance
359 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
361 uint64_t *slab1, *slab2;
362 uint32_t index1, index2, offset1, offset2;
364 /* Set bit in array2 slab and set bit in array1 slab */
365 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
366 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
367 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
368 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
369 slab2 = bmp->array2 + index2;
370 slab1 = bmp->array1 + index1;
372 *slab2 |= 1lu << offset2;
373 *slab1 |= 1lu << offset1;
380 * Handle to bitmap instance
382 * Bit position identifying the array2 slab
384 * Value to be assigned to the 64-bit slab in array2
387 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
389 uint64_t *slab1, *slab2;
390 uint32_t index1, index2, offset1;
392 /* Set bits in array2 slab and set bit in array1 slab */
393 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
394 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
395 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
396 slab2 = bmp->array2 + index2;
397 slab1 = bmp->array1 + index1;
400 *slab1 |= 1lu << offset1;
403 static inline uint64_t
404 __rte_bitmap_line_not_empty(uint64_t *slab2)
406 uint64_t v1, v2, v3, v4;
408 v1 = slab2[0] | slab2[1];
409 v2 = slab2[2] | slab2[3];
410 v3 = slab2[4] | slab2[5];
411 v4 = slab2[6] | slab2[7];
422 * Handle to bitmap instance
427 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
429 uint64_t *slab1, *slab2;
430 uint32_t index1, index2, offset1, offset2;
432 /* Clear bit in array2 slab */
433 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
434 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
435 slab2 = bmp->array2 + index2;
437 /* Return if array2 slab is not all-zeros */
438 *slab2 &= ~(1lu << offset2);
443 /* Check the entire cache line of array2 for all-zeros */
444 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
445 slab2 = bmp->array2 + index2;
446 if (__rte_bitmap_line_not_empty(slab2)) {
450 /* The array2 cache line is all-zeros, so clear bit in array1 slab */
451 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
452 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
453 slab1 = bmp->array1 + index1;
454 *slab1 &= ~(1lu << offset1);
460 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
465 /* Check current array1 slab */
466 value1 = bmp->array1[bmp->index1];
467 value1 &= __rte_bitmap_mask1_get(bmp);
469 if (rte_bsf64(value1, &bmp->offset1)) {
473 __rte_bitmap_index1_inc(bmp);
476 /* Look for another array1 slab */
477 for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
478 value1 = bmp->array1[bmp->index1];
480 if (rte_bsf64(value1, &bmp->offset1)) {
489 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
491 __rte_bitmap_index2_set(bmp);
493 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
497 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
501 slab2 = bmp->array2 + bmp->index2;
502 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
504 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
509 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
518 * Bitmap scan (with automatic wrap-around)
521 * Handle to bitmap instance
523 * When function call returns 1, pos contains the position of the next set
524 * bit, otherwise not modified
526 * When function call returns 1, slab contains the value of the entire 64-bit
527 * slab where the bit indicated by pos is located. Slabs are always 64-bit
528 * aligned, so the position of the first bit of the slab (this bit is not
529 * necessarily set) is pos / 64. Once a slab has been returned by the bitmap
530 * scan operation, the internal pointers of the bitmap are updated to point
531 * after this slab, so the same slab will not be returned again if it
532 * contains more than one bit which is set. When function call returns 0,
533 * slab is not modified.
535 * 0 if there is no bit set in the bitmap, 1 otherwise
538 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
540 /* Return data from current array2 line if available */
541 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
545 /* Look for non-empty array2 line */
546 if (__rte_bitmap_scan_search(bmp)) {
547 __rte_bitmap_scan_read_init(bmp);
548 __rte_bitmap_scan_read(bmp, pos, slab);
560 #endif /* __INCLUDE_RTE_BITMAP_H__ */