4 * Copyright(c) 2010-2013 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.
35 #ifndef __INCLUDE_RTE_BITMAP_H__
36 #define __INCLUDE_RTE_BITMAP_H__
46 * The bitmap component provides a mechanism to manage large arrays of bits
47 * through bit get/set/clear and bit array scan operations.
49 * The bitmap scan operation is optimized for 64-bit CPUs using 64-byte cache
50 * lines. The bitmap is hierarchically organized using two arrays (array1 and
51 * array2), with each bit in array1 being associated with a full cache line
52 * (512 bits) of bitmap bits, which are stored in array2: the bit in array1 is
53 * set only when there is at least one bit set within its associated array2
54 * bits, otherwise the bit in array1 is cleared. The read and write operations
55 * for array1 and array2 are always done in slabs of 64 bits.
57 * This bitmap is not thread safe. For lock free operation on a specific bitmap
58 * instance, a single writer thread performing bit set/clear operations is
59 * allowed, only the writer thread can do bitmap scan operations, while there
60 * can be several reader threads performing bit get operations in parallel with
61 * the writer thread. When the use of locking primitives is acceptable, the
62 * serialization of the bit set/clear and bitmap scan operations needs to be
63 * enforced by the caller, while the bit get operation does not require locking
68 #include <rte_common.h>
69 #include <rte_debug.h>
70 #include <rte_memory.h>
71 #include <rte_branch_prediction.h>
72 #include <rte_prefetch.h>
74 #ifndef RTE_BITMAP_OPTIMIZATIONS
75 #define RTE_BITMAP_OPTIMIZATIONS 1
77 #if RTE_BITMAP_OPTIMIZATIONS
78 #include <tmmintrin.h>
82 #define RTE_BITMAP_SLAB_BIT_SIZE 64
83 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
84 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
87 #define RTE_BITMAP_CL_BIT_SIZE (CACHE_LINE_SIZE * 8)
88 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 9
89 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
91 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
92 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 3
93 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
95 /** Bitmap data structure */
97 /* Context for array1 and array2 */
98 uint64_t *array1; /**< Bitmap array1 */
99 uint64_t *array2; /**< Bitmap array2 */
100 uint32_t array1_size; /**< Number of 64-bit slabs in array1 that are actually used */
101 uint32_t array2_size; /**< Number of 64-bit slabs in array2 */
103 /* Context for the "scan next" operation */
104 uint32_t index1; /**< Bitmap scan: Index of current array1 slab */
105 uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
106 uint32_t index2; /**< Bitmap scan: Index of current array2 slab */
107 uint32_t go2; /**< Bitmap scan: Go/stop condition for current array2 cache line */
109 /* Storage space for array1 and array2 */
114 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
116 bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
119 static inline uint64_t
120 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
122 return ((~1lu) << bmp->offset1);
126 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
128 bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
131 #if RTE_BITMAP_OPTIMIZATIONS
134 rte_bsf64(uint64_t slab, uint32_t *pos)
136 if (likely(slab == 0)) {
140 *pos = __builtin_ctzll(slab);
147 rte_bsf64(uint64_t slab, uint32_t *pos)
152 if (likely(slab == 0)) {
156 for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
157 if (unlikely(slab & mask)) {
168 static inline uint32_t
169 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
170 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
171 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
173 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
174 uint32_t n_cache_lines_array2;
175 uint32_t n_bytes_total;
177 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
178 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
179 n_slabs_array1 = rte_align32pow2(n_slabs_array1);
180 n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
181 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
182 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * CACHE_LINE_SIZE;
184 if (array1_byte_offset) {
185 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
188 *array1_slabs = n_slabs_array1;
190 if (array2_byte_offset) {
191 *array2_byte_offset = n_cache_lines_context_and_array1 * CACHE_LINE_SIZE;
194 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
197 return n_bytes_total;
201 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
203 bmp->index1 = bmp->array1_size - 1;
204 bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
205 __rte_bitmap_index2_set(bmp);
206 bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
212 * Bitmap memory footprint calculation
215 * Number of bits in the bitmap
217 * Bitmap memory footprint measured in bytes on success, 0 on error
219 static inline uint32_t
220 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
221 /* Check input arguments */
226 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
230 * Bitmap initialization
233 * Handle to bitmap instance
235 * Base address of pre-allocated array2
237 * Number of pre-allocated bits in array2. Must be non-zero and multiple of 512.
239 * 0 upon success, error code otherwise
241 static inline struct rte_bitmap *
242 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
244 struct rte_bitmap *bmp;
245 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
248 /* Check input arguments */
253 if ((mem == NULL) || (((uintptr_t) mem) & CACHE_LINE_MASK)) {
257 size = __rte_bitmap_get_memory_footprint(n_bits,
258 &array1_byte_offset, &array1_slabs,
259 &array2_byte_offset, &array2_slabs);
260 if (size < mem_size) {
265 memset(mem, 0, size);
266 bmp = (struct rte_bitmap *) mem;
268 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
269 bmp->array1_size = array1_slabs;
270 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
271 bmp->array2_size = array2_slabs;
273 __rte_bitmap_scan_init(bmp);
282 * Handle to bitmap instance
284 * 0 upon success, error code otherwise
287 rte_bitmap_free(struct rte_bitmap *bmp)
289 /* Check input arguments */
301 * Handle to bitmap instance
304 rte_bitmap_reset(struct rte_bitmap *bmp)
306 memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
307 memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
308 __rte_bitmap_scan_init(bmp);
312 * Bitmap location prefetch into CPU L1 cache
315 * Handle to bitmap instance
319 * 0 upon success, error code otherwise
322 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
327 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
328 slab2 = bmp->array2 + index2;
329 rte_prefetch0((void *) slab2);
336 * Handle to bitmap instance
340 * 0 when bit is cleared, non-zero when bit is set
342 static inline uint64_t
343 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
346 uint32_t index2, offset2;
348 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
349 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
350 slab2 = bmp->array2 + index2;
351 return ((*slab2) & (1lu << offset2));
358 * Handle to bitmap instance
363 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
365 uint64_t *slab1, *slab2;
366 uint32_t index1, index2, offset1, offset2;
368 /* Set bit in array2 slab and set bit in array1 slab */
369 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
370 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
371 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
372 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
373 slab2 = bmp->array2 + index2;
374 slab1 = bmp->array1 + index1;
376 *slab2 |= 1lu << offset2;
377 *slab1 |= 1lu << offset1;
384 * Handle to bitmap instance
386 * Bit position identifying the array2 slab
388 * Value to be assigned to the 64-bit slab in array2
391 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
393 uint64_t *slab1, *slab2;
394 uint32_t index1, index2, offset1;
396 /* Set bits in array2 slab and set bit in array1 slab */
397 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
398 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
399 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
400 slab2 = bmp->array2 + index2;
401 slab1 = bmp->array1 + index1;
404 *slab1 |= 1lu << offset1;
407 static inline uint64_t
408 __rte_bitmap_line_not_empty(uint64_t *slab2)
410 uint64_t v1, v2, v3, v4;
412 v1 = slab2[0] | slab2[1];
413 v2 = slab2[2] | slab2[3];
414 v3 = slab2[4] | slab2[5];
415 v4 = slab2[6] | slab2[7];
426 * Handle to bitmap instance
431 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
433 uint64_t *slab1, *slab2;
434 uint32_t index1, index2, offset1, offset2;
436 /* Clear bit in array2 slab */
437 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
438 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
439 slab2 = bmp->array2 + index2;
441 /* Return if array2 slab is not all-zeros */
442 *slab2 &= ~(1lu << offset2);
447 /* Check the entire cache line of array2 for all-zeros */
448 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
449 slab2 = bmp->array2 + index2;
450 if (__rte_bitmap_line_not_empty(slab2)) {
454 /* The array2 cache line is all-zeros, so clear bit in array1 slab */
455 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
456 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
457 slab1 = bmp->array1 + index1;
458 *slab1 &= ~(1lu << offset1);
464 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
469 /* Check current array1 slab */
470 value1 = bmp->array1[bmp->index1];
471 value1 &= __rte_bitmap_mask1_get(bmp);
473 if (rte_bsf64(value1, &bmp->offset1)) {
477 __rte_bitmap_index1_inc(bmp);
480 /* Look for another array1 slab */
481 for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
482 value1 = bmp->array1[bmp->index1];
484 if (rte_bsf64(value1, &bmp->offset1)) {
493 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
495 __rte_bitmap_index2_set(bmp);
497 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
501 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
505 slab2 = bmp->array2 + bmp->index2;
506 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
508 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
513 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
522 * Bitmap scan (with automatic wrap-around)
525 * Handle to bitmap instance
527 * When function call returns 1, pos contains the position of the next set
528 * bit, otherwise not modified
530 * When function call returns 1, slab contains the value of the entire 64-bit
531 * slab where the bit indicated by pos is located. Slabs are always 64-bit
532 * aligned, so the position of the first bit of the slab (this bit is not
533 * necessarily set) is pos / 64. Once a slab has been returned by the bitmap
534 * scan operation, the internal pointers of the bitmap are updated to point
535 * after this slab, so the same slab will not be returned again if it
536 * contains more than one bit which is set. When function call returns 0,
537 * slab is not modified.
539 * 0 if there is no bit set in the bitmap, 1 otherwise
542 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
544 /* Return data from current array2 line if available */
545 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
549 /* Look for non-empty array2 line */
550 if (__rte_bitmap_scan_search(bmp)) {
551 __rte_bitmap_scan_read_init(bmp);
552 __rte_bitmap_scan_read(bmp, pos, slab);
564 #endif /* __INCLUDE_RTE_BITMAP_H__ */