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.
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-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 bits) of bitmap bits, which are stored in array2: the bit in array1 is
52 * 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
76 #if RTE_BITMAP_OPTIMIZATIONS
77 #include <tmmintrin.h>
81 #define RTE_BITMAP_SLAB_BIT_SIZE 64
82 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
83 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
86 #define RTE_BITMAP_CL_BIT_SIZE (CACHE_LINE_SIZE * 8)
87 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 9
88 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
90 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
91 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 3
92 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
94 /** Bitmap data structure */
96 /* Context for array1 and array2 */
97 uint64_t *array1; /**< Bitmap array1 */
98 uint64_t *array2; /**< Bitmap array2 */
99 uint32_t array1_size; /**< Number of 64-bit slabs in array1 that are actually used */
100 uint32_t array2_size; /**< Number of 64-bit slabs in array2 */
102 /* Context for the "scan next" operation */
103 uint32_t index1; /**< Bitmap scan: Index of current array1 slab */
104 uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
105 uint32_t index2; /**< Bitmap scan: Index of current array2 slab */
106 uint32_t go2; /**< Bitmap scan: Go/stop condition for current array2 cache line */
108 /* Storage space for array1 and array2 */
113 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
115 bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
118 static inline uint64_t
119 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
121 return ((~1lu) << bmp->offset1);
125 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
127 bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
130 #if RTE_BITMAP_OPTIMIZATIONS
133 rte_bsf64(uint64_t slab, uint32_t *pos)
135 if (likely(slab == 0)) {
139 *pos = __builtin_ctzll(slab);
146 rte_bsf64(uint64_t slab, uint32_t *pos)
151 if (likely(slab == 0)) {
155 for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
156 if (unlikely(slab & mask)) {
167 static inline uint32_t
168 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
169 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
170 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
172 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
173 uint32_t n_cache_lines_array2;
174 uint32_t n_bytes_total;
176 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
177 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
178 n_slabs_array1 = rte_align32pow2(n_slabs_array1);
179 n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
180 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
181 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * CACHE_LINE_SIZE;
183 if (array1_byte_offset) {
184 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
187 *array1_slabs = n_slabs_array1;
189 if (array2_byte_offset) {
190 *array2_byte_offset = n_cache_lines_context_and_array1 * CACHE_LINE_SIZE;
193 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
196 return n_bytes_total;
200 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
202 bmp->index1 = bmp->array1_size - 1;
203 bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
204 __rte_bitmap_index2_set(bmp);
205 bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
211 * Bitmap memory footprint calculation
214 * Number of bits in the bitmap
216 * Bitmap memory footprint measured in bytes on success, 0 on error
218 static inline uint32_t
219 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
220 /* Check input arguments */
225 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
229 * Bitmap initialization
232 * Handle to bitmap instance
234 * Base address of pre-allocated array2
236 * Number of pre-allocated bits in array2. Must be non-zero and multiple of 512.
238 * 0 upon success, error code otherwise
240 static inline struct rte_bitmap *
241 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
243 struct rte_bitmap *bmp;
244 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
247 /* Check input arguments */
252 if ((mem == NULL) || (((uintptr_t) mem) & CACHE_LINE_MASK)) {
256 size = __rte_bitmap_get_memory_footprint(n_bits,
257 &array1_byte_offset, &array1_slabs,
258 &array2_byte_offset, &array2_slabs);
259 if (size < mem_size) {
264 memset(mem, 0, size);
265 bmp = (struct rte_bitmap *) mem;
267 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
268 bmp->array1_size = array1_slabs;
269 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
270 bmp->array2_size = array2_slabs;
272 __rte_bitmap_scan_init(bmp);
281 * Handle to bitmap instance
283 * 0 upon success, error code otherwise
286 rte_bitmap_free(struct rte_bitmap *bmp)
288 /* Check input arguments */
300 * Handle to bitmap instance
303 rte_bitmap_reset(struct rte_bitmap *bmp)
305 memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
306 memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
307 __rte_bitmap_scan_init(bmp);
311 * Bitmap location prefetch into CPU L1 cache
314 * Handle to bitmap instance
318 * 0 upon success, error code otherwise
321 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
326 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
327 slab2 = bmp->array2 + index2;
328 rte_prefetch0((void *) slab2);
335 * Handle to bitmap instance
339 * 0 when bit is cleared, non-zero when bit is set
341 static inline uint64_t
342 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
345 uint32_t index2, offset2;
347 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
348 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
349 slab2 = bmp->array2 + index2;
350 return ((*slab2) & (1lu << offset2));
357 * Handle to bitmap instance
362 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
364 uint64_t *slab1, *slab2;
365 uint32_t index1, index2, offset1, offset2;
367 /* Set bit in array2 slab and set bit in array1 slab */
368 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
369 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
370 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
371 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
372 slab2 = bmp->array2 + index2;
373 slab1 = bmp->array1 + index1;
375 *slab2 |= 1lu << offset2;
376 *slab1 |= 1lu << offset1;
383 * Handle to bitmap instance
385 * Bit position identifying the array2 slab
387 * Value to be assigned to the 64-bit slab in array2
390 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
392 uint64_t *slab1, *slab2;
393 uint32_t index1, index2, offset1;
395 /* Set bits in array2 slab and set bit in array1 slab */
396 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
397 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
398 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
399 slab2 = bmp->array2 + index2;
400 slab1 = bmp->array1 + index1;
403 *slab1 |= 1lu << offset1;
406 static inline uint64_t
407 __rte_bitmap_line_not_empty(uint64_t *slab2)
409 uint64_t v1, v2, v3, v4;
411 v1 = slab2[0] | slab2[1];
412 v2 = slab2[2] | slab2[3];
413 v3 = slab2[4] | slab2[5];
414 v4 = slab2[6] | slab2[7];
425 * Handle to bitmap instance
430 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
432 uint64_t *slab1, *slab2;
433 uint32_t index1, index2, offset1, offset2;
435 /* Clear bit in array2 slab */
436 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
437 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
438 slab2 = bmp->array2 + index2;
440 /* Return if array2 slab is not all-zeros */
441 *slab2 &= ~(1lu << offset2);
446 /* Check the entire cache line of array2 for all-zeros */
447 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
448 slab2 = bmp->array2 + index2;
449 if (__rte_bitmap_line_not_empty(slab2)) {
453 /* The array2 cache line is all-zeros, so clear bit in array1 slab */
454 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
455 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
456 slab1 = bmp->array1 + index1;
457 *slab1 &= ~(1lu << offset1);
463 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
468 /* Check current array1 slab */
469 value1 = bmp->array1[bmp->index1];
470 value1 &= __rte_bitmap_mask1_get(bmp);
472 if (rte_bsf64(value1, &bmp->offset1)) {
476 __rte_bitmap_index1_inc(bmp);
479 /* Look for another array1 slab */
480 for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
481 value1 = bmp->array1[bmp->index1];
483 if (rte_bsf64(value1, &bmp->offset1)) {
492 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
494 __rte_bitmap_index2_set(bmp);
496 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
500 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
504 slab2 = bmp->array2 + bmp->index2;
505 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
507 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
512 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
521 * Bitmap scan (with automatic wrap-around)
524 * Handle to bitmap instance
526 * When function call returns 1, pos contains the position of the next set
527 * bit, otherwise not modified
529 * When function call returns 1, slab contains the value of the entire 64-bit
530 * slab where the bit indicated by pos is located. Slabs are always 64-bit
531 * aligned, so the position of the first bit of the slab (this bit is not
532 * necessarily set) is pos / 64. Once a slab has been returned by the bitmap
533 * scan operation, the internal pointers of the bitmap are updated to point
534 * after this slab, so the same slab will not be returned again if it
535 * contains more than one bit which is set. When function call returns 0,
536 * slab is not modified.
538 * 0 if there is no bit set in the bitmap, 1 otherwise
541 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
543 /* Return data from current array2 line if available */
544 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
548 /* Look for non-empty array2 line */
549 if (__rte_bitmap_scan_search(bmp)) {
550 __rte_bitmap_scan_read_init(bmp);
551 __rte_bitmap_scan_read(bmp, pos, slab);
563 #endif /* __INCLUDE_RTE_BITMAP_H__ */