DPDK  23.03.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
9 extern "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>
44 #include <rte_branch_prediction.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 
62 struct 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 
79 static inline void
80 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
81 {
82  bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
83 }
84 
85 static inline uint64_t
86 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
87 {
88  return (~1llu) << bmp->offset1;
89 }
90 
91 static 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 
97 static 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 
129 static 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 
148 static 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 
170 static inline struct rte_bitmap *
171 rte_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
220 static 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
254 static inline struct rte_bitmap *
255 rte_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 
299 static inline int
301 {
302  /* Check input arguments */
303  if (bmp == NULL) {
304  return -1;
305  }
306 
307  return 0;
308 }
309 
316 static 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 
332 static inline void
333 rte_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 
353 static inline uint64_t
354 rte_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 
373 static inline void
374 rte_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 
401 static inline void
402 rte_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
419 static 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
435 static 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 
466 static inline void
467 rte_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 
499 static 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 
526 static 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 
534 static 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 
575 static inline int
576 rte_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 int rte_bitmap_free(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:300
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
Definition: rte_bitmap.h:402
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:374
static void rte_bitmap_reset(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:317
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:467
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:171
uint32_t array1_size
Definition: rte_bitmap.h:66
uint64_t * array1
Definition: rte_bitmap.h:64
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
Definition: rte_bitmap.h:149
uint64_t * array2
Definition: rte_bitmap.h:65
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:548
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:743
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:354
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
uint32_t index2
Definition: rte_bitmap.h:72
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:333
uint32_t index1
Definition: rte_bitmap.h:70
static void rte_prefetch1(const volatile void *p)
#define RTE_CACHE_LINE_MASK
Definition: rte_common.h:422
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
Definition: rte_bitmap.h:576
uint32_t go2
Definition: rte_bitmap.h:73
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
uint32_t array2_size
Definition: rte_bitmap.h:67
uint32_t offset1
Definition: rte_bitmap.h:71
static void rte_prefetch0(const volatile void *p)