xref: /dpdk/lib/eal/include/rte_bitmap.h (revision da7e701151ea8b742d4c38ace3e4fefd1b4507fc)
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 #include <string.h>
38 #include <rte_compat.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 	/* 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 
205 /**
206  * @warning
207  * @b EXPERIMENTAL: this API may change without prior notice.
208  *
209  * Bitmap clear slab overhead bits.
210  *
211  * @param slabs
212  *   Slab array.
213  * @param slab_size
214  *   Number of 64-bit slabs in the slabs array.
215  * @param pos
216  *   The start bit position in the slabs to be cleared.
217  */
218 __rte_experimental
219 static inline void
220 __rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size,
221 				      uint32_t pos)
222 {
223 	uint32_t i;
224 	uint32_t index = pos / RTE_BITMAP_SLAB_BIT_SIZE;
225 	uint32_t offset = pos & RTE_BITMAP_SLAB_BIT_MASK;
226 
227 	if (offset) {
228 		for (i = offset; i < RTE_BITMAP_SLAB_BIT_SIZE; i++)
229 			slabs[index] &= ~(1llu << i);
230 		index++;
231 	}
232 	if (index < slab_size)
233 		memset(&slabs[index], 0, sizeof(slabs[0]) *
234 		       (slab_size - index));
235 }
236 
237 /**
238  * @warning
239  * @b EXPERIMENTAL: this API may change without prior notice.
240  *
241  * Bitmap initialization with all bits set
242  *
243  * @param n_bits
244  *   Number of pre-allocated bits in array2.
245  * @param mem
246  *   Base address of array1 and array2.
247  * @param mem_size
248  *   Minimum expected size of bitmap.
249  * @return
250  *   Handle to bitmap instance.
251  */
252 __rte_experimental
253 static inline struct rte_bitmap *
254 rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
255 {
256 	struct rte_bitmap *bmp;
257 	uint32_t array1_byte_offset, array1_slabs;
258 	uint32_t array2_byte_offset, array2_slabs;
259 	uint32_t size;
260 
261 	/* Check input arguments */
262 	if (!n_bits || !mem || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK))
263 		return NULL;
264 
265 	size = __rte_bitmap_get_memory_footprint(n_bits,
266 		&array1_byte_offset, &array1_slabs,
267 		&array2_byte_offset, &array2_slabs);
268 	if (size < mem_size)
269 		return NULL;
270 
271 	/* Setup bitmap */
272 	bmp = (struct rte_bitmap *) mem;
273 	bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
274 	bmp->array1_size = array1_slabs;
275 	bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
276 	bmp->array2_size = array2_slabs;
277 
278 	__rte_bitmap_scan_init(bmp);
279 
280 	memset(bmp->array1, 0xff, bmp->array1_size * sizeof(bmp->array1[0]));
281 	memset(bmp->array2, 0xff, bmp->array2_size * sizeof(bmp->array2[0]));
282 	/* Clear overhead bits. */
283 	__rte_bitmap_clear_slab_overhead_bits(bmp->array1, bmp->array1_size,
284 			bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2);
285 	__rte_bitmap_clear_slab_overhead_bits(bmp->array2, bmp->array2_size,
286 			n_bits);
287 	return bmp;
288 }
289 
290 /**
291  * Bitmap free
292  *
293  * @param bmp
294  *   Handle to bitmap instance
295  * @return
296  *   0 upon success, error code otherwise
297  */
298 static inline int
299 rte_bitmap_free(struct rte_bitmap *bmp)
300 {
301 	/* Check input arguments */
302 	if (bmp == NULL) {
303 		return -1;
304 	}
305 
306 	return 0;
307 }
308 
309 /**
310  * Bitmap reset
311  *
312  * @param bmp
313  *   Handle to bitmap instance
314  */
315 static inline void
316 rte_bitmap_reset(struct rte_bitmap *bmp)
317 {
318 	memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
319 	memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
320 	__rte_bitmap_scan_init(bmp);
321 }
322 
323 /**
324  * Bitmap location prefetch into CPU L1 cache
325  *
326  * @param bmp
327  *   Handle to bitmap instance
328  * @param pos
329  *   Bit position
330  */
331 static inline void
332 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
333 {
334 	uint64_t *slab2;
335 	uint32_t index2;
336 
337 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
338 	slab2 = bmp->array2 + index2;
339 	rte_prefetch0((void *) slab2);
340 }
341 
342 /**
343  * Bitmap bit get
344  *
345  * @param bmp
346  *   Handle to bitmap instance
347  * @param pos
348  *   Bit position
349  * @return
350  *   0 when bit is cleared, non-zero when bit is set
351  */
352 static inline uint64_t
353 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
354 {
355 	uint64_t *slab2;
356 	uint32_t index2, offset2;
357 
358 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
359 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
360 	slab2 = bmp->array2 + index2;
361 	return (*slab2) & (1llu << offset2);
362 }
363 
364 /**
365  * Bitmap bit set
366  *
367  * @param bmp
368  *   Handle to bitmap instance
369  * @param pos
370  *   Bit position
371  */
372 static inline void
373 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
374 {
375 	uint64_t *slab1, *slab2;
376 	uint32_t index1, index2, offset1, offset2;
377 
378 	/* Set bit in array2 slab and set bit in array1 slab */
379 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
380 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
381 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
382 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
383 	slab2 = bmp->array2 + index2;
384 	slab1 = bmp->array1 + index1;
385 
386 	*slab2 |= 1llu << offset2;
387 	*slab1 |= 1llu << offset1;
388 }
389 
390 /**
391  * Bitmap slab set
392  *
393  * @param bmp
394  *   Handle to bitmap instance
395  * @param pos
396  *   Bit position identifying the array2 slab
397  * @param slab
398  *   Value to be assigned to the 64-bit slab in array2
399  */
400 static inline void
401 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
402 {
403 	uint64_t *slab1, *slab2;
404 	uint32_t index1, index2, offset1;
405 
406 	/* Set bits in array2 slab and set bit in array1 slab */
407 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
408 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
409 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
410 	slab2 = bmp->array2 + index2;
411 	slab1 = bmp->array1 + index1;
412 
413 	*slab2 |= slab;
414 	*slab1 |= 1llu << offset1;
415 }
416 
417 #if RTE_BITMAP_CL_SLAB_SIZE == 8
418 static inline uint64_t
419 __rte_bitmap_line_not_empty(uint64_t *slab2)
420 {
421 	uint64_t v1, v2, v3, v4;
422 
423 	v1 = slab2[0] | slab2[1];
424 	v2 = slab2[2] | slab2[3];
425 	v3 = slab2[4] | slab2[5];
426 	v4 = slab2[6] | slab2[7];
427 	v1 |= v2;
428 	v3 |= v4;
429 
430 	return v1 | v3;
431 }
432 
433 #elif RTE_BITMAP_CL_SLAB_SIZE == 16
434 static inline uint64_t
435 __rte_bitmap_line_not_empty(uint64_t *slab2)
436 {
437 	uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
438 
439 	v1 = slab2[0] | slab2[1];
440 	v2 = slab2[2] | slab2[3];
441 	v3 = slab2[4] | slab2[5];
442 	v4 = slab2[6] | slab2[7];
443 	v5 = slab2[8] | slab2[9];
444 	v6 = slab2[10] | slab2[11];
445 	v7 = slab2[12] | slab2[13];
446 	v8 = slab2[14] | slab2[15];
447 	v1 |= v2;
448 	v3 |= v4;
449 	v5 |= v6;
450 	v7 |= v8;
451 
452 	return v1 | v3 | v5 | v7;
453 }
454 
455 #endif /* RTE_BITMAP_CL_SLAB_SIZE */
456 
457 /**
458  * Bitmap bit clear
459  *
460  * @param bmp
461  *   Handle to bitmap instance
462  * @param pos
463  *   Bit position
464  */
465 static inline void
466 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
467 {
468 	uint64_t *slab1, *slab2;
469 	uint32_t index1, index2, offset1, offset2;
470 
471 	/* Clear bit in array2 slab */
472 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
473 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
474 	slab2 = bmp->array2 + index2;
475 
476 	/* Return if array2 slab is not all-zeros */
477 	*slab2 &= ~(1llu << offset2);
478 	if (*slab2){
479 		return;
480 	}
481 
482 	/* Check the entire cache line of array2 for all-zeros */
483 	index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
484 	slab2 = bmp->array2 + index2;
485 	if (__rte_bitmap_line_not_empty(slab2)) {
486 		return;
487 	}
488 
489 	/* The array2 cache line is all-zeros, so clear bit in array1 slab */
490 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
491 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
492 	slab1 = bmp->array1 + index1;
493 	*slab1 &= ~(1llu << offset1);
494 
495 	return;
496 }
497 
498 static inline int
499 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
500 {
501 	uint64_t value1;
502 	uint32_t i;
503 
504 	/* Check current array1 slab */
505 	value1 = bmp->array1[bmp->index1];
506 	value1 &= __rte_bitmap_mask1_get(bmp);
507 
508 	if (rte_bsf64_safe(value1, &bmp->offset1))
509 		return 1;
510 
511 	__rte_bitmap_index1_inc(bmp);
512 	bmp->offset1 = 0;
513 
514 	/* Look for another array1 slab */
515 	for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
516 		value1 = bmp->array1[bmp->index1];
517 
518 		if (rte_bsf64_safe(value1, &bmp->offset1))
519 			return 1;
520 	}
521 
522 	return 0;
523 }
524 
525 static inline void
526 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
527 {
528 	__rte_bitmap_index2_set(bmp);
529 	bmp->go2 = 1;
530 	rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
531 }
532 
533 static inline int
534 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
535 {
536 	uint64_t *slab2;
537 
538 	slab2 = bmp->array2 + bmp->index2;
539 	for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
540 		if (*slab2) {
541 			*pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
542 			*slab = *slab2;
543 
544 			bmp->index2 ++;
545 			slab2 ++;
546 			bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
547 			return 1;
548 		}
549 	}
550 
551 	return 0;
552 }
553 
554 /**
555  * Bitmap scan (with automatic wrap-around)
556  *
557  * @param bmp
558  *   Handle to bitmap instance
559  * @param pos
560  *   When function call returns 1, pos contains the position of the next set
561  *   bit, otherwise not modified
562  * @param slab
563  *   When function call returns 1, slab contains the value of the entire 64-bit
564  *   slab where the bit indicated by pos is located. Slabs are always 64-bit
565  *   aligned, so the position of the first bit of the slab (this bit is not
566  *   necessarily set) is pos / 64. Once a slab has been returned by the bitmap
567  *   scan operation, the internal pointers of the bitmap are updated to point
568  *   after this slab, so the same slab will not be returned again if it
569  *   contains more than one bit which is set. When function call returns 0,
570  *   slab is not modified.
571  * @return
572  *   0 if there is no bit set in the bitmap, 1 otherwise
573  */
574 static inline int
575 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
576 {
577 	/* Return data from current array2 line if available */
578 	if (__rte_bitmap_scan_read(bmp, pos, slab)) {
579 		return 1;
580 	}
581 
582 	/* Look for non-empty array2 line */
583 	if (__rte_bitmap_scan_search(bmp)) {
584 		__rte_bitmap_scan_read_init(bmp);
585 		__rte_bitmap_scan_read(bmp, pos, slab);
586 		return 1;
587 	}
588 
589 	/* Empty bitmap */
590 	return 0;
591 }
592 
593 #ifdef __cplusplus
594 }
595 #endif
596 
597 #endif /* __INCLUDE_RTE_BITMAP_H__ */
598