DPDK  18.11.11
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_common.h>
40 #include <rte_config.h>
41 #include <rte_debug.h>
42 #include <rte_memory.h>
43 #include <rte_branch_prediction.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 
61 struct 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 
78 static inline void
79 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
80 {
81  bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
82 }
83 
84 static inline uint64_t
85 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
86 {
87  return (~1llu) << bmp->offset1;
88 }
89 
90 static 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 
96 static inline int __rte_deprecated
97 rte_bsf64(uint64_t slab, uint32_t *pos)
98 {
99  return rte_bsf64_safe(slab, pos);
100 }
101 
102 static inline uint32_t
103 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
104  uint32_t *array1_byte_offset, uint32_t *array1_slabs,
105  uint32_t *array2_byte_offset, uint32_t *array2_slabs)
106 {
107  uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
108  uint32_t n_cache_lines_array2;
109  uint32_t n_bytes_total;
110 
111  n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
112  n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
113  n_slabs_array1 = rte_align32pow2(n_slabs_array1);
114  n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
115  n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
116  n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
117 
118  if (array1_byte_offset) {
119  *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
120  }
121  if (array1_slabs) {
122  *array1_slabs = n_slabs_array1;
123  }
124  if (array2_byte_offset) {
125  *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
126  }
127  if (array2_slabs) {
128  *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
129  }
130 
131  return n_bytes_total;
132 }
133 
134 static inline void
135 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
136 {
137  bmp->index1 = bmp->array1_size - 1;
138  bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
139  __rte_bitmap_index2_set(bmp);
140  bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
141 
142  bmp->go2 = 0;
143 }
144 
153 static inline uint32_t
155  /* Check input arguments */
156  if (n_bits == 0) {
157  return 0;
158  }
159 
160  return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
161 }
162 
175 static inline struct rte_bitmap *
176 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
177 {
178  struct rte_bitmap *bmp;
179  uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
180  uint32_t size;
181 
182  /* Check input arguments */
183  if (n_bits == 0) {
184  return NULL;
185  }
186 
187  if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
188  return NULL;
189  }
190 
191  size = __rte_bitmap_get_memory_footprint(n_bits,
192  &array1_byte_offset, &array1_slabs,
193  &array2_byte_offset, &array2_slabs);
194  if (size < mem_size) {
195  return NULL;
196  }
197 
198  /* Setup bitmap */
199  memset(mem, 0, size);
200  bmp = (struct rte_bitmap *) mem;
201 
202  bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
203  bmp->array1_size = array1_slabs;
204  bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
205  bmp->array2_size = array2_slabs;
206 
207  __rte_bitmap_scan_init(bmp);
208 
209  return bmp;
210 }
211 
220 static inline int
222 {
223  /* Check input arguments */
224  if (bmp == NULL) {
225  return -1;
226  }
227 
228  return 0;
229 }
230 
237 static inline void
239 {
240  memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
241  memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
242  __rte_bitmap_scan_init(bmp);
243 }
244 
255 static inline void
256 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
257 {
258  uint64_t *slab2;
259  uint32_t index2;
260 
261  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
262  slab2 = bmp->array2 + index2;
263  rte_prefetch0((void *) slab2);
264 }
265 
276 static inline uint64_t
277 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
278 {
279  uint64_t *slab2;
280  uint32_t index2, offset2;
281 
282  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
283  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
284  slab2 = bmp->array2 + index2;
285  return (*slab2) & (1llu << offset2);
286 }
287 
296 static inline void
297 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
298 {
299  uint64_t *slab1, *slab2;
300  uint32_t index1, index2, offset1, offset2;
301 
302  /* Set bit in array2 slab and set bit in array1 slab */
303  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
304  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
305  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
306  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
307  slab2 = bmp->array2 + index2;
308  slab1 = bmp->array1 + index1;
309 
310  *slab2 |= 1llu << offset2;
311  *slab1 |= 1llu << offset1;
312 }
313 
324 static inline void
325 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
326 {
327  uint64_t *slab1, *slab2;
328  uint32_t index1, index2, offset1;
329 
330  /* Set bits in array2 slab and set bit in array1 slab */
331  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
332  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
333  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
334  slab2 = bmp->array2 + index2;
335  slab1 = bmp->array1 + index1;
336 
337  *slab2 |= slab;
338  *slab1 |= 1llu << offset1;
339 }
340 
341 static inline uint64_t
342 __rte_bitmap_line_not_empty(uint64_t *slab2)
343 {
344  uint64_t v1, v2, v3, v4;
345 
346  v1 = slab2[0] | slab2[1];
347  v2 = slab2[2] | slab2[3];
348  v3 = slab2[4] | slab2[5];
349  v4 = slab2[6] | slab2[7];
350  v1 |= v2;
351  v3 |= v4;
352 
353  return v1 | v3;
354 }
355 
364 static inline void
365 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
366 {
367  uint64_t *slab1, *slab2;
368  uint32_t index1, index2, offset1, offset2;
369 
370  /* Clear bit in array2 slab */
371  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
372  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
373  slab2 = bmp->array2 + index2;
374 
375  /* Return if array2 slab is not all-zeros */
376  *slab2 &= ~(1llu << offset2);
377  if (*slab2){
378  return;
379  }
380 
381  /* Check the entire cache line of array2 for all-zeros */
382  index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
383  slab2 = bmp->array2 + index2;
384  if (__rte_bitmap_line_not_empty(slab2)) {
385  return;
386  }
387 
388  /* The array2 cache line is all-zeros, so clear bit in array1 slab */
389  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
390  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
391  slab1 = bmp->array1 + index1;
392  *slab1 &= ~(1llu << offset1);
393 
394  return;
395 }
396 
397 static inline int
398 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
399 {
400  uint64_t value1;
401  uint32_t i;
402 
403  /* Check current array1 slab */
404  value1 = bmp->array1[bmp->index1];
405  value1 &= __rte_bitmap_mask1_get(bmp);
406 
407  if (rte_bsf64_safe(value1, &bmp->offset1))
408  return 1;
409 
410  __rte_bitmap_index1_inc(bmp);
411  bmp->offset1 = 0;
412 
413  /* Look for another array1 slab */
414  for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
415  value1 = bmp->array1[bmp->index1];
416 
417  if (rte_bsf64_safe(value1, &bmp->offset1))
418  return 1;
419  }
420 
421  return 0;
422 }
423 
424 static inline void
425 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
426 {
427  __rte_bitmap_index2_set(bmp);
428  bmp->go2 = 1;
429  rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
430 }
431 
432 static inline int
433 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
434 {
435  uint64_t *slab2;
436 
437  slab2 = bmp->array2 + bmp->index2;
438  for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
439  if (*slab2) {
440  *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
441  *slab = *slab2;
442 
443  bmp->index2 ++;
444  slab2 ++;
445  bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
446  return 1;
447  }
448  }
449 
450  return 0;
451 }
452 
473 static inline int
474 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
475 {
476  /* Return data from current array2 line if available */
477  if (__rte_bitmap_scan_read(bmp, pos, slab)) {
478  return 1;
479  }
480 
481  /* Look for non-empty array2 line */
482  if (__rte_bitmap_scan_search(bmp)) {
483  __rte_bitmap_scan_read_init(bmp);
484  __rte_bitmap_scan_read(bmp, pos, slab);
485  return 1;
486  }
487 
488  /* Empty bitmap */
489  return 0;
490 }
491 
492 #ifdef __cplusplus
493 }
494 #endif
495 
496 #endif /* __INCLUDE_RTE_BITMAP_H__ */
static int rte_bitmap_free(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:221
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
Definition: rte_bitmap.h:325
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:297
static void rte_bitmap_reset(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:238
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:365
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:176
uint32_t array1_size
Definition: rte_bitmap.h:65
#define RTE_CACHE_LINE_MASK
Definition: rte_memory.h:44
uint64_t * array1
Definition: rte_bitmap.h:63
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
Definition: rte_bitmap.h:154
uint64_t * array2
Definition: rte_bitmap.h:64
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:349
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:500
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:277
uint32_t index2
Definition: rte_bitmap.h:71
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:256
uint32_t index1
Definition: rte_bitmap.h:69
static void rte_prefetch1(const volatile void *p)
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
Definition: rte_bitmap.h:474
uint32_t go2
Definition: rte_bitmap.h:72
uint32_t array2_size
Definition: rte_bitmap.h:66
uint32_t offset1
Definition: rte_bitmap.h:70
static void rte_prefetch0(const volatile void *p)