[v3,1/2] bitmap: add create bitmap with all bits set

Message ID 1586522787-403236-2-git-send-email-suanmingm@mellanox.com (mailing list archive)
State Superseded, archived
Headers
Series bitmap: add create bitmap with all bits set |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-testing success Testing PASS
ci/Intel-compilation success Compilation OK

Commit Message

Suanming Mou April 10, 2020, 12:46 p.m. UTC
  Currently, in the case to use bitmap as resource allocator, after
bitmap creation, all the bitmap bits should be set to indicate the
bit available. Every time when allocate one bit, search for the set
bits and clear it to make it in use.

Add a new rte_bitmap_init_with_all_set() function to have a quick
fill up the bitmap bits.

Comparing with the case create the bitmap as empty and set the bitmap
one by one, the new function costs less cycles.

Signed-off-by: Suanming Mou <suanmingm@mellanox.com>
---
 lib/librte_eal/include/rte_bitmap.h | 78 +++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
  

Comments

Cristian Dumitrescu April 10, 2020, 4:04 p.m. UTC | #1
> -----Original Message-----
> From: Suanming Mou <suanmingm@mellanox.com>
> Sent: Friday, April 10, 2020 1:46 PM
> To: Dumitrescu, Cristian <cristian.dumitrescu@intel.com>; dev@dpdk.org
> Cc: amo@semihalf.com
> Subject: [PATCH v3 1/2] bitmap: add create bitmap with all bits set
> 
> Currently, in the case to use bitmap as resource allocator, after
> bitmap creation, all the bitmap bits should be set to indicate the
> bit available. Every time when allocate one bit, search for the set
> bits and clear it to make it in use.
> 
> Add a new rte_bitmap_init_with_all_set() function to have a quick
> fill up the bitmap bits.
> 
> Comparing with the case create the bitmap as empty and set the bitmap
> one by one, the new function costs less cycles.
> 
> Signed-off-by: Suanming Mou <suanmingm@mellanox.com>
> ---
>  lib/librte_eal/include/rte_bitmap.h | 78
> +++++++++++++++++++++++++++++++++++++
>  1 file changed, 78 insertions(+)
> 

Acked-by: Cristian Dumitrescu <cristian.dumitrescu@intel.com>
  

Patch

diff --git a/lib/librte_eal/include/rte_bitmap.h b/lib/librte_eal/include/rte_bitmap.h
index 6b846f2..e3b806e 100644
--- a/lib/librte_eal/include/rte_bitmap.h
+++ b/lib/librte_eal/include/rte_bitmap.h
@@ -483,6 +483,84 @@  struct rte_bitmap {
 	return 0;
 }
 
+/**
+ * Bitmap clear slab overhead bits.
+ *
+ * @param slabs
+ *   Slab arrary.
+ * @param slab_size
+ *   Number of 64-bit slabs in the slabs array.
+ * @param pos
+ *   The start bit position in the slabs to be cleared.
+ */
+static inline void
+__rte_bitmap_clear_slab_overhead_bits(uint64_t *slabs, uint32_t slab_size,
+				      uint32_t pos)
+{
+	uint32_t i;
+	uint32_t index = pos / RTE_BITMAP_SLAB_BIT_SIZE;
+	uint32_t offset = pos & RTE_BITMAP_SLAB_BIT_MASK;
+
+	if (offset) {
+		for (i = offset; i < RTE_BITMAP_SLAB_BIT_SIZE; i++)
+			slabs[index] &= ~(1llu << i);
+		index++;
+	}
+	if (index < slab_size)
+		memset(&slabs[index], 0, sizeof(slabs[0]) *
+		       (slab_size - index));
+}
+
+
+/**
+ * Bitmap initialization with all bits set
+ *
+ * @param n_bits
+ *   Number of pre-allocated bits in array2.
+ * @param mem
+ *   Base address of array1 and array2.
+ * @param mem_size
+ *   Minimum expected size of bitmap.
+ * @return
+ *   Handle to bitmap instance.
+ */
+static inline struct rte_bitmap *
+rte_bitmap_init_with_all_set(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
+{
+	struct rte_bitmap *bmp;
+	uint32_t array1_byte_offset, array1_slabs;
+	uint32_t array2_byte_offset, array2_slabs;
+	uint32_t size;
+
+	/* Check input arguments */
+	if (!n_bits || !mem || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK))
+		return NULL;
+
+	size = __rte_bitmap_get_memory_footprint(n_bits,
+		&array1_byte_offset, &array1_slabs,
+		&array2_byte_offset, &array2_slabs);
+	if (size < mem_size)
+		return NULL;
+
+	/* Setup bitmap */
+	bmp = (struct rte_bitmap *) mem;
+	bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
+	bmp->array1_size = array1_slabs;
+	bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
+	bmp->array2_size = array2_slabs;
+
+	__rte_bitmap_scan_init(bmp);
+
+	memset(bmp->array1, 0xff, bmp->array1_size * sizeof(bmp->array1[0]));
+	memset(bmp->array2, 0xff, bmp->array2_size * sizeof(bmp->array2[0]));
+	/* Clear overhead bits. */
+	__rte_bitmap_clear_slab_overhead_bits(bmp->array1, bmp->array1_size,
+			bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2);
+	__rte_bitmap_clear_slab_overhead_bits(bmp->array2, bmp->array2_size,
+			n_bits);
+	return bmp;
+}
+
 #ifdef __cplusplus
 }
 #endif