DPDK 22.11.5
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_compat.h>
40#include <rte_common.h>
41#include <rte_config.h>
42#include <rte_debug.h>
43#include <rte_memory.h>
45#include <rte_prefetch.h>
46
47/* Slab */
48#define RTE_BITMAP_SLAB_BIT_SIZE 64
49#define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
50#define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
51
52/* Cache line (CL) */
53#define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
54#define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
55#define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
56
57#define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
58#define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
59#define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
60
62struct rte_bitmap {
63 /* Context for array1 and array2 */
64 uint64_t *array1;
65 uint64_t *array2;
66 uint32_t array1_size;
67 uint32_t array2_size;
69 /* Context for the "scan next" operation */
70 uint32_t index1;
71 uint32_t offset1;
72 uint32_t index2;
73 uint32_t go2;
75 /* Storage space for array1 and array2 */
76 uint8_t memory[];
77};
78
79static inline void
80__rte_bitmap_index1_inc(struct rte_bitmap *bmp)
81{
82 bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
83}
84
85static inline uint64_t
86__rte_bitmap_mask1_get(struct rte_bitmap *bmp)
87{
88 return (~1llu) << bmp->offset1;
89}
90
91static inline void
92__rte_bitmap_index2_set(struct rte_bitmap *bmp)
93{
94 bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
95}
96
97static inline uint32_t
98__rte_bitmap_get_memory_footprint(uint32_t n_bits,
99 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
100 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
101{
102 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
103 uint32_t n_cache_lines_array2;
104 uint32_t n_bytes_total;
105
106 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
107 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
108 n_slabs_array1 = rte_align32pow2(n_slabs_array1);
109 n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
110 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
111 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
112
113 if (array1_byte_offset) {
114 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
115 }
116 if (array1_slabs) {
117 *array1_slabs = n_slabs_array1;
118 }
119 if (array2_byte_offset) {
120 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
121 }
122 if (array2_slabs) {
123 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
124 }
125
126 return n_bytes_total;
127}
128
129static inline void
130__rte_bitmap_scan_init(struct rte_bitmap *bmp)
131{
132 bmp->index1 = bmp->array1_size - 1;
133 bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
134 __rte_bitmap_index2_set(bmp);
135 bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
136
137 bmp->go2 = 0;
138}
139
148static inline uint32_t
150 /* Check input arguments */
151 if (n_bits == 0) {
152 return 0;
153 }
154
155 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
156}
157
170static inline struct rte_bitmap *
171rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
172{
173 struct rte_bitmap *bmp;
174 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
175 uint32_t size;
176
177 /* Check input arguments */
178 if (n_bits == 0) {
179 return NULL;
180 }
181
182 if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
183 return NULL;
184 }
185
186 size = __rte_bitmap_get_memory_footprint(n_bits,
187 &array1_byte_offset, &array1_slabs,
188 &array2_byte_offset, &array2_slabs);
189 if (size > mem_size)
190 return NULL;
191
192 /* Setup bitmap */
193 memset(mem, 0, size);
194 bmp = (struct rte_bitmap *) mem;
195
196 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
197 bmp->array1_size = array1_slabs;
198 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
199 bmp->array2_size = array2_slabs;
200
201 __rte_bitmap_scan_init(bmp);
202
203 return bmp;
204}
205
219__rte_experimental
220static inline void
221__rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size,
222 uint32_t pos)
223{
224 uint32_t i;
225 uint32_t index = pos / RTE_BITMAP_SLAB_BIT_SIZE;
226 uint32_t offset = pos & RTE_BITMAP_SLAB_BIT_MASK;
227
228 if (offset) {
229 for (i = offset; i < RTE_BITMAP_SLAB_BIT_SIZE; i++)
230 slabs[index] &= ~(1llu << i);
231 index++;
232 }
233 if (index < slab_size)
234 memset(&slabs[index], 0, sizeof(slabs[0]) *
235 (slab_size - index));
236}
237
253__rte_experimental
254static inline struct rte_bitmap *
255rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
256{
257 struct rte_bitmap *bmp;
258 uint32_t array1_byte_offset, array1_slabs;
259 uint32_t array2_byte_offset, array2_slabs;
260 uint32_t size;
261
262 /* Check input arguments */
263 if (!n_bits || !mem || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK))
264 return NULL;
265
266 size = __rte_bitmap_get_memory_footprint(n_bits,
267 &array1_byte_offset, &array1_slabs,
268 &array2_byte_offset, &array2_slabs);
269 if (size < mem_size)
270 return NULL;
271
272 /* Setup bitmap */
273 bmp = (struct rte_bitmap *) mem;
274 bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
275 bmp->array1_size = array1_slabs;
276 bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
277 bmp->array2_size = array2_slabs;
278
279 __rte_bitmap_scan_init(bmp);
280
281 memset(bmp->array1, 0xff, bmp->array1_size * sizeof(bmp->array1[0]));
282 memset(bmp->array2, 0xff, bmp->array2_size * sizeof(bmp->array2[0]));
283 /* Clear overhead bits. */
285 bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2);
287 n_bits);
288 return bmp;
289}
290
299static inline int
301{
302 /* Check input arguments */
303 if (bmp == NULL) {
304 return -1;
305 }
306
307 return 0;
308}
309
316static inline void
318{
319 memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
320 memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
321 __rte_bitmap_scan_init(bmp);
322}
323
332static inline void
333rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
334{
335 uint64_t *slab2;
336 uint32_t index2;
337
338 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
339 slab2 = bmp->array2 + index2;
340 rte_prefetch0((void *) slab2);
341}
342
353static inline uint64_t
354rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
355{
356 uint64_t *slab2;
357 uint32_t index2, offset2;
358
359 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
360 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
361 slab2 = bmp->array2 + index2;
362 return (*slab2) & (1llu << offset2);
363}
364
373static inline void
374rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
375{
376 uint64_t *slab1, *slab2;
377 uint32_t index1, index2, offset1, offset2;
378
379 /* Set bit in array2 slab and set bit in array1 slab */
380 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
381 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
382 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
383 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
384 slab2 = bmp->array2 + index2;
385 slab1 = bmp->array1 + index1;
386
387 *slab2 |= 1llu << offset2;
388 *slab1 |= 1llu << offset1;
389}
390
401static inline void
402rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
403{
404 uint64_t *slab1, *slab2;
405 uint32_t index1, index2, offset1;
406
407 /* Set bits in array2 slab and set bit in array1 slab */
408 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
409 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
410 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
411 slab2 = bmp->array2 + index2;
412 slab1 = bmp->array1 + index1;
413
414 *slab2 |= slab;
415 *slab1 |= 1llu << offset1;
416}
417
418#if RTE_BITMAP_CL_SLAB_SIZE == 8
419static inline uint64_t
420__rte_bitmap_line_not_empty(uint64_t *slab2)
421{
422 uint64_t v1, v2, v3, v4;
423
424 v1 = slab2[0] | slab2[1];
425 v2 = slab2[2] | slab2[3];
426 v3 = slab2[4] | slab2[5];
427 v4 = slab2[6] | slab2[7];
428 v1 |= v2;
429 v3 |= v4;
430
431 return v1 | v3;
432}
433
434#elif RTE_BITMAP_CL_SLAB_SIZE == 16
435static inline uint64_t
436__rte_bitmap_line_not_empty(uint64_t *slab2)
437{
438 uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
439
440 v1 = slab2[0] | slab2[1];
441 v2 = slab2[2] | slab2[3];
442 v3 = slab2[4] | slab2[5];
443 v4 = slab2[6] | slab2[7];
444 v5 = slab2[8] | slab2[9];
445 v6 = slab2[10] | slab2[11];
446 v7 = slab2[12] | slab2[13];
447 v8 = slab2[14] | slab2[15];
448 v1 |= v2;
449 v3 |= v4;
450 v5 |= v6;
451 v7 |= v8;
452
453 return v1 | v3 | v5 | v7;
454}
455
456#endif /* RTE_BITMAP_CL_SLAB_SIZE */
457
466static inline void
467rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
468{
469 uint64_t *slab1, *slab2;
470 uint32_t index1, index2, offset1, offset2;
471
472 /* Clear bit in array2 slab */
473 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
474 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
475 slab2 = bmp->array2 + index2;
476
477 /* Return if array2 slab is not all-zeros */
478 *slab2 &= ~(1llu << offset2);
479 if (*slab2){
480 return;
481 }
482
483 /* Check the entire cache line of array2 for all-zeros */
484 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
485 slab2 = bmp->array2 + index2;
486 if (__rte_bitmap_line_not_empty(slab2)) {
487 return;
488 }
489
490 /* The array2 cache line is all-zeros, so clear bit in array1 slab */
491 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
492 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
493 slab1 = bmp->array1 + index1;
494 *slab1 &= ~(1llu << offset1);
495
496 return;
497}
498
499static inline int
500__rte_bitmap_scan_search(struct rte_bitmap *bmp)
501{
502 uint64_t value1;
503 uint32_t i;
504
505 /* Check current array1 slab */
506 value1 = bmp->array1[bmp->index1];
507 value1 &= __rte_bitmap_mask1_get(bmp);
508
509 if (rte_bsf64_safe(value1, &bmp->offset1))
510 return 1;
511
512 __rte_bitmap_index1_inc(bmp);
513 bmp->offset1 = 0;
514
515 /* Look for another array1 slab */
516 for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
517 value1 = bmp->array1[bmp->index1];
518
519 if (rte_bsf64_safe(value1, &bmp->offset1))
520 return 1;
521 }
522
523 return 0;
524}
525
526static inline void
527__rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
528{
529 __rte_bitmap_index2_set(bmp);
530 bmp->go2 = 1;
531 rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
532}
533
534static inline int
535__rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
536{
537 uint64_t *slab2;
538
539 slab2 = bmp->array2 + bmp->index2;
540 for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
541 if (*slab2) {
542 *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
543 *slab = *slab2;
544
545 bmp->index2 ++;
546 slab2 ++;
547 bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
548 return 1;
549 }
550 }
551
552 return 0;
553}
554
575static inline int
576rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
577{
578 /* Return data from current array2 line if available */
579 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
580 return 1;
581 }
582
583 /* Look for non-empty array2 line */
584 if (__rte_bitmap_scan_search(bmp)) {
585 __rte_bitmap_scan_read_init(bmp);
586 __rte_bitmap_scan_read(bmp, pos, slab);
587 return 1;
588 }
589
590 /* Empty bitmap */
591 return 0;
592}
593
594#ifdef __cplusplus
595}
596#endif
597
598#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:221
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:333
static int rte_bitmap_free(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:300
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:467
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:354
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
Definition: rte_bitmap.h:149
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
Definition: rte_bitmap.h:402
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
Definition: rte_bitmap.h:576
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:171
static void rte_bitmap_reset(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:317
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:255
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:374
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:548
#define RTE_CACHE_LINE_MASK
Definition: rte_common.h:422
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:743
static void rte_prefetch1(const volatile void *p)
static void rte_prefetch0(const volatile void *p)
uint32_t array2_size
Definition: rte_bitmap.h:67
uint32_t index2
Definition: rte_bitmap.h:72
uint32_t array1_size
Definition: rte_bitmap.h:66
uint64_t * array1
Definition: rte_bitmap.h:64
uint32_t index1
Definition: rte_bitmap.h:70
uint32_t go2
Definition: rte_bitmap.h:73
uint64_t * array2
Definition: rte_bitmap.h:65
uint32_t offset1
Definition: rte_bitmap.h:71