DPDK  19.11.14
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 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 
128 static 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 
147 static 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 
169 static inline struct rte_bitmap *
170 rte_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 
213 static inline int
215 {
216  /* Check input arguments */
217  if (bmp == NULL) {
218  return -1;
219  }
220 
221  return 0;
222 }
223 
230 static inline void
232 {
233  memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
234  memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
235  __rte_bitmap_scan_init(bmp);
236 }
237 
248 static inline void
249 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
250 {
251  uint64_t *slab2;
252  uint32_t index2;
253 
254  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
255  slab2 = bmp->array2 + index2;
256  rte_prefetch0((void *) slab2);
257 }
258 
269 static inline uint64_t
270 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
271 {
272  uint64_t *slab2;
273  uint32_t index2, offset2;
274 
275  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
276  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
277  slab2 = bmp->array2 + index2;
278  return (*slab2) & (1llu << offset2);
279 }
280 
289 static inline void
290 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
291 {
292  uint64_t *slab1, *slab2;
293  uint32_t index1, index2, offset1, offset2;
294 
295  /* Set bit in array2 slab and set bit in array1 slab */
296  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
297  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
298  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
299  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
300  slab2 = bmp->array2 + index2;
301  slab1 = bmp->array1 + index1;
302 
303  *slab2 |= 1llu << offset2;
304  *slab1 |= 1llu << offset1;
305 }
306 
317 static inline void
318 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
319 {
320  uint64_t *slab1, *slab2;
321  uint32_t index1, index2, offset1;
322 
323  /* Set bits in array2 slab and set bit in array1 slab */
324  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
325  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
326  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
327  slab2 = bmp->array2 + index2;
328  slab1 = bmp->array1 + index1;
329 
330  *slab2 |= slab;
331  *slab1 |= 1llu << offset1;
332 }
333 
334 static inline uint64_t
335 __rte_bitmap_line_not_empty(uint64_t *slab2)
336 {
337  uint64_t v1, v2, v3, v4;
338 
339  v1 = slab2[0] | slab2[1];
340  v2 = slab2[2] | slab2[3];
341  v3 = slab2[4] | slab2[5];
342  v4 = slab2[6] | slab2[7];
343  v1 |= v2;
344  v3 |= v4;
345 
346  return v1 | v3;
347 }
348 
357 static inline void
358 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
359 {
360  uint64_t *slab1, *slab2;
361  uint32_t index1, index2, offset1, offset2;
362 
363  /* Clear bit in array2 slab */
364  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
365  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
366  slab2 = bmp->array2 + index2;
367 
368  /* Return if array2 slab is not all-zeros */
369  *slab2 &= ~(1llu << offset2);
370  if (*slab2){
371  return;
372  }
373 
374  /* Check the entire cache line of array2 for all-zeros */
375  index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
376  slab2 = bmp->array2 + index2;
377  if (__rte_bitmap_line_not_empty(slab2)) {
378  return;
379  }
380 
381  /* The array2 cache line is all-zeros, so clear bit in array1 slab */
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  slab1 = bmp->array1 + index1;
385  *slab1 &= ~(1llu << offset1);
386 
387  return;
388 }
389 
390 static inline int
391 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
392 {
393  uint64_t value1;
394  uint32_t i;
395 
396  /* Check current array1 slab */
397  value1 = bmp->array1[bmp->index1];
398  value1 &= __rte_bitmap_mask1_get(bmp);
399 
400  if (rte_bsf64_safe(value1, &bmp->offset1))
401  return 1;
402 
403  __rte_bitmap_index1_inc(bmp);
404  bmp->offset1 = 0;
405 
406  /* Look for another array1 slab */
407  for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
408  value1 = bmp->array1[bmp->index1];
409 
410  if (rte_bsf64_safe(value1, &bmp->offset1))
411  return 1;
412  }
413 
414  return 0;
415 }
416 
417 static inline void
418 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
419 {
420  __rte_bitmap_index2_set(bmp);
421  bmp->go2 = 1;
422  rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
423 }
424 
425 static inline int
426 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
427 {
428  uint64_t *slab2;
429 
430  slab2 = bmp->array2 + bmp->index2;
431  for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
432  if (*slab2) {
433  *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
434  *slab = *slab2;
435 
436  bmp->index2 ++;
437  slab2 ++;
438  bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
439  return 1;
440  }
441  }
442 
443  return 0;
444 }
445 
466 static inline int
467 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
468 {
469  /* Return data from current array2 line if available */
470  if (__rte_bitmap_scan_read(bmp, pos, slab)) {
471  return 1;
472  }
473 
474  /* Look for non-empty array2 line */
475  if (__rte_bitmap_scan_search(bmp)) {
476  __rte_bitmap_scan_read_init(bmp);
477  __rte_bitmap_scan_read(bmp, pos, slab);
478  return 1;
479  }
480 
481  /* Empty bitmap */
482  return 0;
483 }
484 
485 #ifdef __cplusplus
486 }
487 #endif
488 
489 #endif /* __INCLUDE_RTE_BITMAP_H__ */
static int rte_bitmap_free(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:214
static void rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
Definition: rte_bitmap.h:318
static void rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:290
static void rte_bitmap_reset(struct rte_bitmap *bmp)
Definition: rte_bitmap.h:231
static void rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:358
static struct rte_bitmap * rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
Definition: rte_bitmap.h:170
uint32_t array1_size
Definition: rte_bitmap.h:65
uint64_t * array1
Definition: rte_bitmap.h:63
static uint32_t rte_bitmap_get_memory_footprint(uint32_t n_bits)
Definition: rte_bitmap.h:148
uint64_t * array2
Definition: rte_bitmap.h:64
static uint32_t rte_align32pow2(uint32_t x)
Definition: rte_common.h:418
static int rte_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: rte_common.h:613
static uint64_t rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:270
uint32_t index2
Definition: rte_bitmap.h:71
static void rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
Definition: rte_bitmap.h:249
uint32_t index1
Definition: rte_bitmap.h:69
static void rte_prefetch1(const volatile void *p)
#define RTE_CACHE_LINE_MASK
Definition: rte_common.h:302
static int rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
Definition: rte_bitmap.h:467
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)