xref: /dpdk/lib/eal/include/rte_bitmap.h (revision 02d36ef6a9528e0f4a3403956e66bcea5fadbf8c)
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_compat.h>
40 #include <rte_common.h>
41 #include <rte_config.h>
42 #include <rte_debug.h>
43 #include <rte_memory.h>
44 #include <rte_branch_prediction.h>
45 #include <rte_prefetch.h>
46 
47 /* Slab */
48 #define RTE_BITMAP_SLAB_BIT_SIZE                 64
49 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2            6
50 #define RTE_BITMAP_SLAB_BIT_MASK                 (RTE_BITMAP_SLAB_BIT_SIZE - 1)
51 
52 /* Cache line (CL) */
53 #define RTE_BITMAP_CL_BIT_SIZE                   (RTE_CACHE_LINE_SIZE * 8)
54 #define RTE_BITMAP_CL_BIT_SIZE_LOG2              (RTE_CACHE_LINE_SIZE_LOG2 + 3)
55 #define RTE_BITMAP_CL_BIT_MASK                   (RTE_BITMAP_CL_BIT_SIZE - 1)
56 
57 #define RTE_BITMAP_CL_SLAB_SIZE                  (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
58 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2             (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
59 #define RTE_BITMAP_CL_SLAB_MASK                  (RTE_BITMAP_CL_SLAB_SIZE - 1)
60 
61 /** Bitmap data structure */
62 struct rte_bitmap {
63 	/* Context for array1 and array2 */
64 	uint64_t *array1;                        /**< Bitmap array1 */
65 	uint64_t *array2;                        /**< Bitmap array2 */
66 	uint32_t array1_size;                    /**< Number of 64-bit slabs in array1 that are actually used */
67 	uint32_t array2_size;                    /**< Number of 64-bit slabs in array2 */
68 
69 	/* Context for the "scan next" operation */
70 	uint32_t index1;  /**< Bitmap scan: Index of current array1 slab */
71 	uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
72 	uint32_t index2;  /**< Bitmap scan: Index of current array2 slab */
73 	uint32_t go2;     /**< Bitmap scan: Go/stop condition for current array2 cache line */
74 
75 	/* Storage space for array1 and array2 */
76 	uint8_t memory[];
77 };
78 
79 static inline void
80 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
81 {
82 	bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
83 }
84 
85 static inline uint64_t
86 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
87 {
88 	return (~1llu) << bmp->offset1;
89 }
90 
91 static inline void
92 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
93 {
94 	bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
95 }
96 
97 static inline uint32_t
98 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
99 	uint32_t *array1_byte_offset, uint32_t *array1_slabs,
100 	uint32_t *array2_byte_offset, uint32_t *array2_slabs)
101 {
102 	uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
103 	uint32_t n_cache_lines_array2;
104 	uint32_t n_bytes_total;
105 
106 	n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
107 	n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
108 	n_slabs_array1 = rte_align32pow2(n_slabs_array1);
109 	n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
110 	n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
111 	n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
112 
113 	if (array1_byte_offset) {
114 		*array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
115 	}
116 	if (array1_slabs) {
117 		*array1_slabs = n_slabs_array1;
118 	}
119 	if (array2_byte_offset) {
120 		*array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
121 	}
122 	if (array2_slabs) {
123 		*array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
124 	}
125 
126 	return n_bytes_total;
127 }
128 
129 static inline void
130 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
131 {
132 	bmp->index1 = bmp->array1_size - 1;
133 	bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
134 	__rte_bitmap_index2_set(bmp);
135 	bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
136 
137 	bmp->go2 = 0;
138 }
139 
140 /**
141  * Bitmap memory footprint calculation
142  *
143  * @param n_bits
144  *   Number of bits in the bitmap
145  * @return
146  *   Bitmap memory footprint measured in bytes on success, 0 on error
147  */
148 static inline uint32_t
149 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
150 	/* Check input arguments */
151 	if (n_bits == 0) {
152 		return 0;
153 	}
154 
155 	return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
156 }
157 
158 /**
159  * Bitmap initialization
160  *
161  * @param n_bits
162  *   Number of pre-allocated bits in array2.
163  * @param mem
164  *   Base address of array1 and array2.
165  * @param mem_size
166  *   Minimum expected size of bitmap.
167  * @return
168  *   Handle to bitmap instance.
169  */
170 static inline struct rte_bitmap *
171 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
172 {
173 	struct rte_bitmap *bmp;
174 	uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
175 	uint32_t size;
176 
177 	/* Check input arguments */
178 	if (n_bits == 0) {
179 		return NULL;
180 	}
181 
182 	if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
183 		return NULL;
184 	}
185 
186 	size = __rte_bitmap_get_memory_footprint(n_bits,
187 		&array1_byte_offset, &array1_slabs,
188 		&array2_byte_offset, &array2_slabs);
189 	if (size > mem_size)
190 		return NULL;
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  */
332 static inline void
333 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
334 {
335 	uint64_t *slab2;
336 	uint32_t index2;
337 
338 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
339 	slab2 = bmp->array2 + index2;
340 	rte_prefetch0((void *) slab2);
341 }
342 
343 /**
344  * Bitmap bit get
345  *
346  * @param bmp
347  *   Handle to bitmap instance
348  * @param pos
349  *   Bit position
350  * @return
351  *   0 when bit is cleared, non-zero when bit is set
352  */
353 static inline uint64_t
354 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
355 {
356 	uint64_t *slab2;
357 	uint32_t index2, offset2;
358 
359 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
360 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
361 	slab2 = bmp->array2 + index2;
362 	return (*slab2) & (1llu << offset2);
363 }
364 
365 /**
366  * Bitmap bit set
367  *
368  * @param bmp
369  *   Handle to bitmap instance
370  * @param pos
371  *   Bit position
372  */
373 static inline void
374 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
375 {
376 	uint64_t *slab1, *slab2;
377 	uint32_t index1, index2, offset1, offset2;
378 
379 	/* Set bit in array2 slab and set bit in array1 slab */
380 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
381 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
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 	slab2 = bmp->array2 + index2;
385 	slab1 = bmp->array1 + index1;
386 
387 	*slab2 |= 1llu << offset2;
388 	*slab1 |= 1llu << offset1;
389 }
390 
391 /**
392  * Bitmap slab set
393  *
394  * @param bmp
395  *   Handle to bitmap instance
396  * @param pos
397  *   Bit position identifying the array2 slab
398  * @param slab
399  *   Value to be assigned to the 64-bit slab in array2
400  */
401 static inline void
402 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
403 {
404 	uint64_t *slab1, *slab2;
405 	uint32_t index1, index2, offset1;
406 
407 	/* Set bits in array2 slab and set bit in array1 slab */
408 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
409 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
410 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
411 	slab2 = bmp->array2 + index2;
412 	slab1 = bmp->array1 + index1;
413 
414 	*slab2 |= slab;
415 	*slab1 |= 1llu << offset1;
416 }
417 
418 #if RTE_BITMAP_CL_SLAB_SIZE == 8
419 static inline uint64_t
420 __rte_bitmap_line_not_empty(uint64_t *slab2)
421 {
422 	uint64_t v1, v2, v3, v4;
423 
424 	v1 = slab2[0] | slab2[1];
425 	v2 = slab2[2] | slab2[3];
426 	v3 = slab2[4] | slab2[5];
427 	v4 = slab2[6] | slab2[7];
428 	v1 |= v2;
429 	v3 |= v4;
430 
431 	return v1 | v3;
432 }
433 
434 #elif RTE_BITMAP_CL_SLAB_SIZE == 16
435 static inline uint64_t
436 __rte_bitmap_line_not_empty(uint64_t *slab2)
437 {
438 	uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
439 
440 	v1 = slab2[0] | slab2[1];
441 	v2 = slab2[2] | slab2[3];
442 	v3 = slab2[4] | slab2[5];
443 	v4 = slab2[6] | slab2[7];
444 	v5 = slab2[8] | slab2[9];
445 	v6 = slab2[10] | slab2[11];
446 	v7 = slab2[12] | slab2[13];
447 	v8 = slab2[14] | slab2[15];
448 	v1 |= v2;
449 	v3 |= v4;
450 	v5 |= v6;
451 	v7 |= v8;
452 
453 	return v1 | v3 | v5 | v7;
454 }
455 
456 #endif /* RTE_BITMAP_CL_SLAB_SIZE */
457 
458 /**
459  * Bitmap bit clear
460  *
461  * @param bmp
462  *   Handle to bitmap instance
463  * @param pos
464  *   Bit position
465  */
466 static inline void
467 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
468 {
469 	uint64_t *slab1, *slab2;
470 	uint32_t index1, index2, offset1, offset2;
471 
472 	/* Clear bit in array2 slab */
473 	index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
474 	offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
475 	slab2 = bmp->array2 + index2;
476 
477 	/* Return if array2 slab is not all-zeros */
478 	*slab2 &= ~(1llu << offset2);
479 	if (*slab2){
480 		return;
481 	}
482 
483 	/* Check the entire cache line of array2 for all-zeros */
484 	index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
485 	slab2 = bmp->array2 + index2;
486 	if (__rte_bitmap_line_not_empty(slab2)) {
487 		return;
488 	}
489 
490 	/* The array2 cache line is all-zeros, so clear bit in array1 slab */
491 	index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
492 	offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
493 	slab1 = bmp->array1 + index1;
494 	*slab1 &= ~(1llu << offset1);
495 
496 	return;
497 }
498 
499 static inline int
500 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
501 {
502 	uint64_t value1;
503 	uint32_t i;
504 
505 	/* Check current array1 slab */
506 	value1 = bmp->array1[bmp->index1];
507 	value1 &= __rte_bitmap_mask1_get(bmp);
508 
509 	if (rte_bsf64_safe(value1, &bmp->offset1))
510 		return 1;
511 
512 	__rte_bitmap_index1_inc(bmp);
513 	bmp->offset1 = 0;
514 
515 	/* Look for another array1 slab */
516 	for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
517 		value1 = bmp->array1[bmp->index1];
518 
519 		if (rte_bsf64_safe(value1, &bmp->offset1))
520 			return 1;
521 	}
522 
523 	return 0;
524 }
525 
526 static inline void
527 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
528 {
529 	__rte_bitmap_index2_set(bmp);
530 	bmp->go2 = 1;
531 	rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
532 }
533 
534 static inline int
535 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
536 {
537 	uint64_t *slab2;
538 
539 	slab2 = bmp->array2 + bmp->index2;
540 	for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
541 		if (*slab2) {
542 			*pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
543 			*slab = *slab2;
544 
545 			bmp->index2 ++;
546 			slab2 ++;
547 			bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
548 			return 1;
549 		}
550 	}
551 
552 	return 0;
553 }
554 
555 /**
556  * Bitmap scan (with automatic wrap-around)
557  *
558  * @param bmp
559  *   Handle to bitmap instance
560  * @param pos
561  *   When function call returns 1, pos contains the position of the next set
562  *   bit, otherwise not modified
563  * @param slab
564  *   When function call returns 1, slab contains the value of the entire 64-bit
565  *   slab where the bit indicated by pos is located. Slabs are always 64-bit
566  *   aligned, so the position of the first bit of the slab (this bit is not
567  *   necessarily set) is pos / 64. Once a slab has been returned by the bitmap
568  *   scan operation, the internal pointers of the bitmap are updated to point
569  *   after this slab, so the same slab will not be returned again if it
570  *   contains more than one bit which is set. When function call returns 0,
571  *   slab is not modified.
572  * @return
573  *   0 if there is no bit set in the bitmap, 1 otherwise
574  */
575 static inline int
576 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
577 {
578 	/* Return data from current array2 line if available */
579 	if (__rte_bitmap_scan_read(bmp, pos, slab)) {
580 		return 1;
581 	}
582 
583 	/* Look for non-empty array2 line */
584 	if (__rte_bitmap_scan_search(bmp)) {
585 		__rte_bitmap_scan_read_init(bmp);
586 		__rte_bitmap_scan_read(bmp, pos, slab);
587 		return 1;
588 	}
589 
590 	/* Empty bitmap */
591 	return 0;
592 }
593 
594 #ifdef __cplusplus
595 }
596 #endif
597 
598 #endif /* __INCLUDE_RTE_BITMAP_H__ */
599