DPDK  16.04.0
rte_bitmap.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef __INCLUDE_RTE_BITMAP_H__
35 #define __INCLUDE_RTE_BITMAP_H__
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
67 #include <rte_common.h>
68 #include <rte_debug.h>
69 #include <rte_memory.h>
70 #include <rte_branch_prediction.h>
71 #include <rte_prefetch.h>
72 
73 #ifndef RTE_BITMAP_OPTIMIZATIONS
74 #define RTE_BITMAP_OPTIMIZATIONS 1
75 #endif
76 
77 /* Slab */
78 #define RTE_BITMAP_SLAB_BIT_SIZE 64
79 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
80 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
81 
82 /* Cache line (CL) */
83 #define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
84 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 (RTE_CACHE_LINE_SIZE_LOG2 + 3)
85 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
86 
87 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
88 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
89 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
90 
92 struct rte_bitmap {
93  /* Context for array1 and array2 */
94  uint64_t *array1;
95  uint64_t *array2;
96  uint32_t array1_size;
97  uint32_t array2_size;
99  /* Context for the "scan next" operation */
100  uint32_t index1;
101  uint32_t offset1;
102  uint32_t index2;
103  uint32_t go2;
105  /* Storage space for array1 and array2 */
106  uint8_t memory[0];
107 };
108 
109 static inline void
110 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
111 {
112  bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
113 }
114 
115 static inline uint64_t
116 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
117 {
118  return (~1lu) << bmp->offset1;
119 }
120 
121 static inline void
122 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
123 {
124  bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
125 }
126 
127 #if RTE_BITMAP_OPTIMIZATIONS
128 
129 static inline int
130 rte_bsf64(uint64_t slab, uint32_t *pos)
131 {
132  if (likely(slab == 0)) {
133  return 0;
134  }
135 
136  *pos = __builtin_ctzll(slab);
137  return 1;
138 }
139 
140 #else
141 
142 static inline int
143 rte_bsf64(uint64_t slab, uint32_t *pos)
144 {
145  uint64_t mask;
146  uint32_t i;
147 
148  if (likely(slab == 0)) {
149  return 0;
150  }
151 
152  for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
153  if (unlikely(slab & mask)) {
154  *pos = i;
155  return 1;
156  }
157  }
158 
159  return 0;
160 }
161 
162 #endif
163 
164 static inline uint32_t
165 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
166  uint32_t *array1_byte_offset, uint32_t *array1_slabs,
167  uint32_t *array2_byte_offset, uint32_t *array2_slabs)
168 {
169  uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
170  uint32_t n_cache_lines_array2;
171  uint32_t n_bytes_total;
172 
173  n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
174  n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
175  n_slabs_array1 = rte_align32pow2(n_slabs_array1);
176  n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
177  n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
178  n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
179 
180  if (array1_byte_offset) {
181  *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
182  }
183  if (array1_slabs) {
184  *array1_slabs = n_slabs_array1;
185  }
186  if (array2_byte_offset) {
187  *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
188  }
189  if (array2_slabs) {
190  *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
191  }
192 
193  return n_bytes_total;
194 }
195 
196 static inline void
197 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
198 {
199  bmp->index1 = bmp->array1_size - 1;
200  bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
201  __rte_bitmap_index2_set(bmp);
202  bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
203 
204  bmp->go2 = 0;
205 }
206 
215 static inline uint32_t
217  /* Check input arguments */
218  if (n_bits == 0) {
219  return 0;
220  }
221 
222  return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
223 }
224 
237 static inline struct rte_bitmap *
238 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
239 {
240  struct rte_bitmap *bmp;
241  uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
242  uint32_t size;
243 
244  /* Check input arguments */
245  if (n_bits == 0) {
246  return NULL;
247  }
248 
249  if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
250  return NULL;
251  }
252 
253  size = __rte_bitmap_get_memory_footprint(n_bits,
254  &array1_byte_offset, &array1_slabs,
255  &array2_byte_offset, &array2_slabs);
256  if (size < mem_size) {
257  return NULL;
258  }
259 
260  /* Setup bitmap */
261  memset(mem, 0, size);
262  bmp = (struct rte_bitmap *) mem;
263 
264  bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
265  bmp->array1_size = array1_slabs;
266  bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
267  bmp->array2_size = array2_slabs;
268 
269  __rte_bitmap_scan_init(bmp);
270 
271  return bmp;
272 }
273 
282 static inline int
284 {
285  /* Check input arguments */
286  if (bmp == NULL) {
287  return -1;
288  }
289 
290  return 0;
291 }
292 
299 static inline void
301 {
302  memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
303  memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
304  __rte_bitmap_scan_init(bmp);
305 }
306 
317 static inline void
318 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
319 {
320  uint64_t *slab2;
321  uint32_t index2;
322 
323  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
324  slab2 = bmp->array2 + index2;
325  rte_prefetch0((void *) slab2);
326 }
327 
338 static inline uint64_t
339 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
340 {
341  uint64_t *slab2;
342  uint32_t index2, offset2;
343 
344  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
345  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
346  slab2 = bmp->array2 + index2;
347  return (*slab2) & (1lu << offset2);
348 }
349 
358 static inline void
359 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
360 {
361  uint64_t *slab1, *slab2;
362  uint32_t index1, index2, offset1, offset2;
363 
364  /* Set bit in array2 slab and set bit in array1 slab */
365  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
366  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
367  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
368  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
369  slab2 = bmp->array2 + index2;
370  slab1 = bmp->array1 + index1;
371 
372  *slab2 |= 1lu << offset2;
373  *slab1 |= 1lu << offset1;
374 }
375 
386 static inline void
387 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
388 {
389  uint64_t *slab1, *slab2;
390  uint32_t index1, index2, offset1;
391 
392  /* Set bits in array2 slab and set bit in array1 slab */
393  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
394  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
395  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
396  slab2 = bmp->array2 + index2;
397  slab1 = bmp->array1 + index1;
398 
399  *slab2 |= slab;
400  *slab1 |= 1lu << offset1;
401 }
402 
403 static inline uint64_t
404 __rte_bitmap_line_not_empty(uint64_t *slab2)
405 {
406  uint64_t v1, v2, v3, v4;
407 
408  v1 = slab2[0] | slab2[1];
409  v2 = slab2[2] | slab2[3];
410  v3 = slab2[4] | slab2[5];
411  v4 = slab2[6] | slab2[7];
412  v1 |= v2;
413  v3 |= v4;
414 
415  return v1 | v3;
416 }
417 
426 static inline void
427 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
428 {
429  uint64_t *slab1, *slab2;
430  uint32_t index1, index2, offset1, offset2;
431 
432  /* Clear bit in array2 slab */
433  index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
434  offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
435  slab2 = bmp->array2 + index2;
436 
437  /* Return if array2 slab is not all-zeros */
438  *slab2 &= ~(1lu << offset2);
439  if (*slab2){
440  return;
441  }
442 
443  /* Check the entire cache line of array2 for all-zeros */
444  index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
445  slab2 = bmp->array2 + index2;
446  if (__rte_bitmap_line_not_empty(slab2)) {
447  return;
448  }
449 
450  /* The array2 cache line is all-zeros, so clear bit in array1 slab */
451  index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
452  offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
453  slab1 = bmp->array1 + index1;
454  *slab1 &= ~(1lu << offset1);
455 
456  return;
457 }
458 
459 static inline int
460 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
461 {
462  uint64_t value1;
463  uint32_t i;
464 
465  /* Check current array1 slab */
466  value1 = bmp->array1[bmp->index1];
467  value1 &= __rte_bitmap_mask1_get(bmp);
468 
469  if (rte_bsf64(value1, &bmp->offset1)) {
470  return 1;
471  }
472 
473  __rte_bitmap_index1_inc(bmp);
474  bmp->offset1 = 0;
475 
476  /* Look for another array1 slab */
477  for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
478  value1 = bmp->array1[bmp->index1];
479 
480  if (rte_bsf64(value1, &bmp->offset1)) {
481  return 1;
482  }
483  }
484 
485  return 0;
486 }
487 
488 static inline void
489 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
490 {
491  __rte_bitmap_index2_set(bmp);
492  bmp->go2 = 1;
493  rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
494 }
495 
496 static inline int
497 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
498 {
499  uint64_t *slab2;
500 
501  slab2 = bmp->array2 + bmp->index2;
502  for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
503  if (*slab2) {
504  *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
505  *slab = *slab2;
506 
507  bmp->index2 ++;
508  slab2 ++;
509  bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
510  return 1;
511  }
512  }
513 
514  return 0;
515 }
516 
537 static inline int
538 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
539 {
540  /* Return data from current array2 line if available */
541  if (__rte_bitmap_scan_read(bmp, pos, slab)) {
542  return 1;
543  }
544 
545  /* Look for non-empty array2 line */
546  if (__rte_bitmap_scan_search(bmp)) {
547  __rte_bitmap_scan_read_init(bmp);
548  __rte_bitmap_scan_read(bmp, pos, slab);
549  return 1;
550  }
551 
552  /* Empty bitmap */
553  return 0;
554 }
555 
556 #ifdef __cplusplus
557 }
558 #endif
559 
560 #endif /* __INCLUDE_RTE_BITMAP_H__ */