DPDK 21.11.0
rte_bitmap.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5#ifndef __INCLUDE_RTE_BITMAP_H__
6#define __INCLUDE_RTE_BITMAP_H__
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
38#include <string.h>
39#include <rte_common.h>
40#include <rte_config.h>
41#include <rte_debug.h>
42#include <rte_memory.h>
44#include <rte_prefetch.h>
45
46/* Slab */
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)
50
51/* Cache line (CL) */
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)
55
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)
59
61struct rte_bitmap {
62 /* Context for array1 and array2 */
63 uint64_t *array1;
64 uint64_t *array2;
65 uint32_t array1_size;
66 uint32_t array2_size;
68 /* Context for the "scan next" operation */
69 uint32_t index1;
70 uint32_t offset1;
71 uint32_t index2;
72 uint32_t go2;
74 /* Storage space for array1 and array2 */
75 uint8_t memory[];
76};
77
78static inline void
79__rte_bitmap_index1_inc(struct rte_bitmap *bmp)
80{
81 bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
82}
83
84static inline uint64_t
85__rte_bitmap_mask1_get(struct rte_bitmap *bmp)
86{
87 return (~1llu) << bmp->offset1;
88}
89
90static inline void
91__rte_bitmap_index2_set(struct rte_bitmap *bmp)
92{
93 bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
94}
95
96static 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)
100{
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;
104
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;
111
112 if (array1_byte_offset) {
113 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
114 }
115 if (array1_slabs) {
116 *array1_slabs = n_slabs_array1;
117 }
118 if (array2_byte_offset) {
119 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
120 }
121 if (array2_slabs) {
122 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
123 }
124
125 return n_bytes_total;
126}
127
128static inline void
129__rte_bitmap_scan_init(struct rte_bitmap *bmp)
130{
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;
135
136 bmp->go2 = 0;
137}
138
147static inline uint32_t
149 /* Check input arguments */
150 if (n_bits == 0) {
151 return 0;
152 }
153
154 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
155}
156
169static inline struct rte_bitmap *
170rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
171{
172 struct rte_bitmap *bmp;
173 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
174 uint32_t size;
175
176 /* Check input arguments */
177 if (n_bits == 0) {
178 return NULL;
179 }
180
181 if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
182 return NULL;
183 }
184
185 size = __rte_bitmap_get_memory_footprint(n_bits,
186 &array1_byte_offset, &array1_slabs,
187 &array2_byte_offset, &array2_slabs);
188 if (size > mem_size)
189 return NULL;
190
191 /* Setup bitmap */
192 memset(mem, 0, size);
193 bmp = (struct rte_bitmap *) mem;
194
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;
199
200 __rte_bitmap_scan_init(bmp);
201
202 return bmp;
203}
204
218__rte_experimental
219static inline void
220__rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size,
221 uint32_t pos)
222{
223 uint32_t i;
224 uint32_t index = pos / RTE_BITMAP_SLAB_BIT_SIZE;
225 uint32_t offset = pos & RTE_BITMAP_SLAB_BIT_MASK;
226
227 if (offset) {
228 for (i = offset; i < RTE_BITMAP_SLAB_BIT_SIZE; i++)
229 slabs[index] &= ~(1llu << i);
230 index++;
231 }
232 if (index < slab_size)
233 memset(&slabs[index], 0, sizeof(slabs[0]) *
234 (slab_size - index));
235}
236
252__rte_experimental
253static inline struct rte_bitmap *
254rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
255{
256 struct rte_bitmap *bmp;
257 uint32_t array1_byte_offset, array1_slabs;
258 uint32_t array2_byte_offset, array2_slabs;
259 uint32_t size;
260
261 /* Check input arguments */
262 if (!n_bits || !mem || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK))
263 return NULL;
264
265 size = __rte_bitmap_get_memory_footprint(n_bits,
266 &array1_byte_offset, &array1_slabs,
267 &array2_byte_offset, &array2_slabs);
268 if (size < mem_size)
269 return NULL;
270
271 /* Setup bitmap */
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;
277
278 __rte_bitmap_scan_init(bmp);
279
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. */
284 bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2);
286 n_bits);
287 return bmp;
288}
289
298static inline int
300{
301 /* Check input arguments */
302 if (bmp == NULL) {
303 return -1;
304 }
305
306 return 0;
307}
308
315static inline void
317{
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);
321}
322
333static inline void
334rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
335{
336 uint64_t *slab2;
337 uint32_t index2;
338
339 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
340 slab2 = bmp->array2 + index2;
341 rte_prefetch0((void *) slab2);
342}
343
354static inline uint64_t
355rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
356{
357 uint64_t *slab2;
358 uint32_t index2, offset2;
359
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);
364}
365
374static inline void
375rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
376{
377 uint64_t *slab1, *slab2;
378 uint32_t index1, index2, offset1, offset2;
379
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;
387
388 *slab2 |= 1llu << offset2;
389 *slab1 |= 1llu << offset1;
390}
391
402static inline void
403rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
404{
405 uint64_t *slab1, *slab2;
406 uint32_t index1, index2, offset1;
407
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;
414
415 *slab2 |= slab;
416 *slab1 |= 1llu << offset1;
417}
418
419#if RTE_BITMAP_CL_SLAB_SIZE == 8
420static inline uint64_t
421__rte_bitmap_line_not_empty(uint64_t *slab2)
422{
423 uint64_t v1, v2, v3, v4;
424
425 v1 = slab2[0] | slab2[1];
426 v2 = slab2[2] | slab2[3];
427 v3 = slab2[4] | slab2[5];
428 v4 = slab2[6] | slab2[7];
429 v1 |= v2;
430 v3 |= v4;
431
432 return v1 | v3;
433}
434
435#elif RTE_BITMAP_CL_SLAB_SIZE == 16
436static inline uint64_t
437__rte_bitmap_line_not_empty(uint64_t *slab2)
438{
439 uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
440
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];
449 v1 |= v2;
450 v3 |= v4;
451 v5 |= v6;
452 v7 |= v8;
453
454 return v1 | v3 | v5 | v7;
455}
456
457#endif /* RTE_BITMAP_CL_SLAB_SIZE */
458
467static inline void
468rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
469{
470 uint64_t *slab1, *slab2;
471 uint32_t index1, index2, offset1, offset2;
472
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;
477
478 /* Return if array2 slab is not all-zeros */
479 *slab2 &= ~(1llu << offset2);
480 if (*slab2){
481 return;
482 }
483
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)) {
488 return;
489 }
490
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);
496
497 return;
498}
499
500static inline int
501__rte_bitmap_scan_search(struct rte_bitmap *bmp)
502{
503 uint64_t value1;
504 uint32_t i;
505
506 /* Check current array1 slab */
507 value1 = bmp->array1[bmp->index1];
508 value1 &= __rte_bitmap_mask1_get(bmp);
509
510 if (rte_bsf64_safe(value1, &bmp->offset1))
511 return 1;
512
513 __rte_bitmap_index1_inc(bmp);
514 bmp->offset1 = 0;
515
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];
519
520 if (rte_bsf64_safe(value1, &bmp->offset1))
521 return 1;
522 }
523
524 return 0;
525}
526
527static inline void
528__rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
529{
530 __rte_bitmap_index2_set(bmp);
531 bmp->go2 = 1;
532 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
533}
534
535static inline int
536__rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
537{
538 uint64_t *slab2;
539
540 slab2 = bmp->array2 + bmp->index2;
541 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
542 if (*slab2) {
543 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
544 *slab = *slab2;
545
546 bmp->index2 ++;
547 slab2 ++;
548 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
549 return 1;
550 }
551 }
552
553 return 0;
554}
555
576static inline int
577rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
578{
579 /* Return data from current array2 line if available */
580 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
581 return 1;
582 }
583
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);
588 return 1;
589 }
590
591 /* Empty bitmap */
592 return 0;
593}
594
595#ifdef __cplusplus
596}
597#endif
598
599#endif /* __INCLUDE_RTE_BITMAP_H__ */
static __rte_experimental void __rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size, uint32_t pos)
Definition: rte_bitmap.h:220
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:334
static int rte_bitmap_free(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:299
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:468
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:355
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
Definition: rte_bitmap.h:148
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
Definition: rte_bitmap.h:403
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
Definition: rte_bitmap.h:577
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:170
static void rte_bitmap_reset(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:316
static __rte_experimental struct rte_bitmap * rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:254
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:375
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:510
#define RTE_CACHE_LINE_MASK
Definition: rte_common.h:382
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:705
static void rte_prefetch1(const volatile void *p)
static void rte_prefetch0(const volatile void *p)
uint32_t array2_size
Definition: rte_bitmap.h:66
uint32_t index2
Definition: rte_bitmap.h:71
uint32_t array1_size
Definition: rte_bitmap.h:65
uint64_t * array1
Definition: rte_bitmap.h:63
uint32_t index1
Definition: rte_bitmap.h:69
uint32_t go2
Definition: rte_bitmap.h:72
uint64_t * array2
Definition: rte_bitmap.h:64
uint32_t offset1
Definition: rte_bitmap.h:70