xref: /dpdk/lib/eal/include/rte_bitmap.h (revision 99f9d799ce21ab22e922ffec8aad51d56e24d04d)
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 
12 /**
13  * @file
14  * RTE Bitmap
15  *
16  * The bitmap component provides a mechanism to manage large arrays of bits
17  * through bit get/set/clear and bit array scan operations.
18  *
19  * The bitmap scan operation is optimized for 64-bit CPUs using 64/128 byte cache
20  * lines. The bitmap is hierarchically organized using two arrays (array1 and
21  * array2), with each bit in array1 being associated with a full cache line
22  * (512/1024 bits) of bitmap bits, which are stored in array2: the bit in array1
23  * is set only when there is at least one bit set within its associated array2
24  * bits, otherwise the bit in array1 is cleared. The read and write operations
25  * for array1 and array2 are always done in slabs of 64 bits.
26  *
27  * This bitmap is not thread safe. For lock free operation on a specific bitmap
28  * instance, a single writer thread performing bit set/clear operations is
29  * allowed, only the writer thread can do bitmap scan operations, while there
30  * can be several reader threads performing bit get operations in parallel with
31  * the writer thread. When the use of locking primitives is acceptable, the
32  * serialization of the bit set/clear and bitmap scan operations needs to be
33  * enforced by the caller, while the bit get operation does not require locking
34  * the bitmap.
35  *
36  ***/
37 
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 
60 /** Bitmap data structure */
61 struct rte_bitmap {
62 	/* Context for array1 and array2 */
63 	uint64_t *array1;                        /**< Bitmap array1 */
64 	uint64_t *array2;                        /**< Bitmap array2 */
65 	uint32_t array1_size;                    /**< Number of 64-bit slabs in array1 that are actually used */
66 	uint32_t array2_size;                    /**< Number of 64-bit slabs in array2 */
67 
68 	/* Context for the "scan next" operation */
69 	uint32_t index1;  /**< Bitmap scan: Index of current array1 slab */
70 	uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
71 	uint32_t index2;  /**< Bitmap scan: Index of current array2 slab */
72 	uint32_t go2;     /**< Bitmap scan: Go/stop condition for current array2 cache line */
73 
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 
139 /**
140  * Bitmap memory footprint calculation
141  *
142  * @param n_bits
143  *   Number of bits in the bitmap
144  * @return
145  *   Bitmap memory footprint measured in bytes on success, 0 on error
146  */
147 static inline uint32_t
148 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
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 
157 /**
158  * Bitmap initialization
159  *
160  * @param n_bits
161  *   Number of pre-allocated bits in array2.
162  * @param mem
163  *   Base address of array1 and array2.
164  * @param mem_size
165  *   Minimum expected size of bitmap.
166  * @return
167  *   Handle to bitmap instance.
168  */
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 
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 
206 /**
207  * @warning
208  * @b EXPERIMENTAL: this API may change without prior notice.
209  *
210  * Bitmap clear slab overhead bits.
211  *
212  * @param slabs
213  *   Slab array.
214  * @param slab_size
215  *   Number of 64-bit slabs in the slabs array.
216  * @param pos
217  *   The start bit position in the slabs to be cleared.
218  */
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 
238 /**
239  * @warning
240  * @b EXPERIMENTAL: this API may change without prior notice.
241  *
242  * Bitmap initialization with all bits set
243  *
244  * @param n_bits
245  *   Number of pre-allocated bits in array2.
246  * @param mem
247  *   Base address of array1 and array2.
248  * @param mem_size
249  *   Minimum expected size of bitmap.
250  * @return
251  *   Handle to bitmap instance.
252  */
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. */
284 	__rte_bitmap_clear_slab_overhead_bits(bmp->array1, bmp->array1_size,
285 			bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2);
286 	__rte_bitmap_clear_slab_overhead_bits(bmp->array2, bmp->array2_size,
287 			n_bits);
288 	return bmp;
289 }
290 
291 /**
292  * Bitmap free
293  *
294  * @param bmp
295  *   Handle to bitmap instance
296  * @return
297  *   0 upon success, error code otherwise
298  */
299 static inline int
300 rte_bitmap_free(struct rte_bitmap *bmp)
301 {
302 	/* Check input arguments */
303 	if (bmp == NULL) {
304 		return -1;
305 	}
306 
307 	return 0;
308 }
309 
310 /**
311  * Bitmap reset
312  *
313  * @param bmp
314  *   Handle to bitmap instance
315  */
316 static inline void
317 rte_bitmap_reset(struct rte_bitmap *bmp)
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 
324 /**
325  * Bitmap location prefetch into CPU L1 cache
326  *
327  * @param bmp
328  *   Handle to bitmap instance
329  * @param pos
330  *   Bit position
331  * @return
332  *   0 upon success, error code otherwise
333  */
334 static inline void
335 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
336 {
337 	uint64_t *slab2;
338 	uint32_t index2;
339 
340 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
341 	slab2 = bmp->array2 + index2;
342 	rte_prefetch0((void *) slab2);
343 }
344 
345 /**
346  * Bitmap bit get
347  *
348  * @param bmp
349  *   Handle to bitmap instance
350  * @param pos
351  *   Bit position
352  * @return
353  *   0 when bit is cleared, non-zero when bit is set
354  */
355 static inline uint64_t
356 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
357 {
358 	uint64_t *slab2;
359 	uint32_t index2, offset2;
360 
361 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
362 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
363 	slab2 = bmp->array2 + index2;
364 	return (*slab2) & (1llu << offset2);
365 }
366 
367 /**
368  * Bitmap bit set
369  *
370  * @param bmp
371  *   Handle to bitmap instance
372  * @param pos
373  *   Bit position
374  */
375 static inline void
376 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
377 {
378 	uint64_t *slab1, *slab2;
379 	uint32_t index1, index2, offset1, offset2;
380 
381 	/* Set bit in array2 slab and set bit in array1 slab */
382 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
383 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
384 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
385 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
386 	slab2 = bmp->array2 + index2;
387 	slab1 = bmp->array1 + index1;
388 
389 	*slab2 |= 1llu << offset2;
390 	*slab1 |= 1llu << offset1;
391 }
392 
393 /**
394  * Bitmap slab set
395  *
396  * @param bmp
397  *   Handle to bitmap instance
398  * @param pos
399  *   Bit position identifying the array2 slab
400  * @param slab
401  *   Value to be assigned to the 64-bit slab in array2
402  */
403 static inline void
404 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
405 {
406 	uint64_t *slab1, *slab2;
407 	uint32_t index1, index2, offset1;
408 
409 	/* Set bits in array2 slab and set bit in array1 slab */
410 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
411 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
412 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
413 	slab2 = bmp->array2 + index2;
414 	slab1 = bmp->array1 + index1;
415 
416 	*slab2 |= slab;
417 	*slab1 |= 1llu << offset1;
418 }
419 
420 #if RTE_BITMAP_CL_SLAB_SIZE == 8
421 static inline uint64_t
422 __rte_bitmap_line_not_empty(uint64_t *slab2)
423 {
424 	uint64_t v1, v2, v3, v4;
425 
426 	v1 = slab2[0] | slab2[1];
427 	v2 = slab2[2] | slab2[3];
428 	v3 = slab2[4] | slab2[5];
429 	v4 = slab2[6] | slab2[7];
430 	v1 |= v2;
431 	v3 |= v4;
432 
433 	return v1 | v3;
434 }
435 
436 #elif RTE_BITMAP_CL_SLAB_SIZE == 16
437 static inline uint64_t
438 __rte_bitmap_line_not_empty(uint64_t *slab2)
439 {
440 	uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
441 
442 	v1 = slab2[0] | slab2[1];
443 	v2 = slab2[2] | slab2[3];
444 	v3 = slab2[4] | slab2[5];
445 	v4 = slab2[6] | slab2[7];
446 	v5 = slab2[8] | slab2[9];
447 	v6 = slab2[10] | slab2[11];
448 	v7 = slab2[12] | slab2[13];
449 	v8 = slab2[14] | slab2[15];
450 	v1 |= v2;
451 	v3 |= v4;
452 	v5 |= v6;
453 	v7 |= v8;
454 
455 	return v1 | v3 | v5 | v7;
456 }
457 
458 #endif /* RTE_BITMAP_CL_SLAB_SIZE */
459 
460 /**
461  * Bitmap bit clear
462  *
463  * @param bmp
464  *   Handle to bitmap instance
465  * @param pos
466  *   Bit position
467  */
468 static inline void
469 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
470 {
471 	uint64_t *slab1, *slab2;
472 	uint32_t index1, index2, offset1, offset2;
473 
474 	/* Clear bit in array2 slab */
475 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
476 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
477 	slab2 = bmp->array2 + index2;
478 
479 	/* Return if array2 slab is not all-zeros */
480 	*slab2 &= ~(1llu << offset2);
481 	if (*slab2){
482 		return;
483 	}
484 
485 	/* Check the entire cache line of array2 for all-zeros */
486 	index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
487 	slab2 = bmp->array2 + index2;
488 	if (__rte_bitmap_line_not_empty(slab2)) {
489 		return;
490 	}
491 
492 	/* The array2 cache line is all-zeros, so clear bit in array1 slab */
493 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
494 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
495 	slab1 = bmp->array1 + index1;
496 	*slab1 &= ~(1llu << offset1);
497 
498 	return;
499 }
500 
501 static inline int
502 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
503 {
504 	uint64_t value1;
505 	uint32_t i;
506 
507 	/* Check current array1 slab */
508 	value1 = bmp->array1[bmp->index1];
509 	value1 &= __rte_bitmap_mask1_get(bmp);
510 
511 	if (rte_bsf64_safe(value1, &bmp->offset1))
512 		return 1;
513 
514 	__rte_bitmap_index1_inc(bmp);
515 	bmp->offset1 = 0;
516 
517 	/* Look for another array1 slab */
518 	for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
519 		value1 = bmp->array1[bmp->index1];
520 
521 		if (rte_bsf64_safe(value1, &bmp->offset1))
522 			return 1;
523 	}
524 
525 	return 0;
526 }
527 
528 static inline void
529 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
530 {
531 	__rte_bitmap_index2_set(bmp);
532 	bmp->go2 = 1;
533 	rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
534 }
535 
536 static inline int
537 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
538 {
539 	uint64_t *slab2;
540 
541 	slab2 = bmp->array2 + bmp->index2;
542 	for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
543 		if (*slab2) {
544 			*pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
545 			*slab = *slab2;
546 
547 			bmp->index2 ++;
548 			slab2 ++;
549 			bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
550 			return 1;
551 		}
552 	}
553 
554 	return 0;
555 }
556 
557 /**
558  * Bitmap scan (with automatic wrap-around)
559  *
560  * @param bmp
561  *   Handle to bitmap instance
562  * @param pos
563  *   When function call returns 1, pos contains the position of the next set
564  *   bit, otherwise not modified
565  * @param slab
566  *   When function call returns 1, slab contains the value of the entire 64-bit
567  *   slab where the bit indicated by pos is located. Slabs are always 64-bit
568  *   aligned, so the position of the first bit of the slab (this bit is not
569  *   necessarily set) is pos / 64. Once a slab has been returned by the bitmap
570  *   scan operation, the internal pointers of the bitmap are updated to point
571  *   after this slab, so the same slab will not be returned again if it
572  *   contains more than one bit which is set. When function call returns 0,
573  *   slab is not modified.
574  * @return
575  *   0 if there is no bit set in the bitmap, 1 otherwise
576  */
577 static inline int
578 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
579 {
580 	/* Return data from current array2 line if available */
581 	if (__rte_bitmap_scan_read(bmp, pos, slab)) {
582 		return 1;
583 	}
584 
585 	/* Look for non-empty array2 line */
586 	if (__rte_bitmap_scan_search(bmp)) {
587 		__rte_bitmap_scan_read_init(bmp);
588 		__rte_bitmap_scan_read(bmp, pos, slab);
589 		return 1;
590 	}
591 
592 	/* Empty bitmap */
593 	return 0;
594 }
595 
596 #ifdef __cplusplus
597 }
598 #endif
599 
600 #endif /* __INCLUDE_RTE_BITMAP_H__ */
601