From patchwork Wed Apr 8 03:05:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suanming Mou X-Patchwork-Id: 67934 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 61F55A0597; Wed, 8 Apr 2020 05:05:57 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 437071BFC0; Wed, 8 Apr 2020 05:05:52 +0200 (CEST) Received: from git-send-mailer.rdmz.labs.mlnx (unknown [37.142.13.130]) by dpdk.org (Postfix) with ESMTP id C34AE2B86 for ; Wed, 8 Apr 2020 05:05:50 +0200 (CEST) From: Suanming Mou To: cristian.dumitrescu@intel.com Cc: dev@dpdk.org, amo@semihalf.com Date: Wed, 8 Apr 2020 11:05:44 +0800 Message-Id: <1586315145-6633-2-git-send-email-suanmingm@mellanox.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1586315145-6633-1-git-send-email-suanmingm@mellanox.com> References: <1583828479-204084-1-git-send-email-suanmingm@mellanox.com> <1586315145-6633-1-git-send-email-suanmingm@mellanox.com> Subject: [dpdk-dev] [PATCH v2 1/2] bitmap: add create bitmap with all bits set X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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 --- lib/librte_eal/common/include/rte_bitmap.h | 113 ++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 28 deletions(-) diff --git a/lib/librte_eal/common/include/rte_bitmap.h b/lib/librte_eal/common/include/rte_bitmap.h index 6b846f2..740076b 100644 --- a/lib/librte_eal/common/include/rte_bitmap.h +++ b/lib/librte_eal/common/include/rte_bitmap.h @@ -136,6 +136,40 @@ struct rte_bitmap { bmp->go2 = 0; } +static inline struct rte_bitmap * +__rte_bitmap_init(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 == 0) + return NULL; + + if ((mem == NULL) || (((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); + + return bmp; +} + /** * Bitmap memory footprint calculation * @@ -170,36 +204,12 @@ struct rte_bitmap { rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size) { struct rte_bitmap *bmp; - uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs; - uint32_t size; - /* Check input arguments */ - if (n_bits == 0) { - return NULL; - } - - if ((mem == NULL) || (((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) { + bmp = __rte_bitmap_init(n_bits, mem, mem_size); + if (!bmp) return NULL; - } - - /* Setup bitmap */ - memset(mem, 0, size); - 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, 0, bmp->array1_size * sizeof(uint64_t)); + memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t)); return bmp; } @@ -483,6 +493,53 @@ struct rte_bitmap { return 0; } +/** + * 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) +{ + uint32_t i; + uint32_t slabs, array1_bits; + struct rte_bitmap *bmp; + + bmp = __rte_bitmap_init(n_bits, mem, mem_size); + if (!bmp) + return NULL; + + array1_bits = bmp->array2_size >> RTE_BITMAP_CL_SLAB_SIZE_LOG2; + /* Fill the arry1 slab aligned bits. */ + slabs = array1_bits >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2; + memset(bmp->array1, 0xff, slabs * sizeof(bmp->array1[0])); + /* Clear the array1 left slabs. */ + memset(&bmp->array1[slabs], 0, (bmp->array1_size - slabs) * + sizeof(bmp->array1[0])); + /* Fill the array1 middle not full set slab. */ + for (i = 0; i < (array1_bits & RTE_BITMAP_SLAB_BIT_MASK); i++) + bmp->array1[slabs] |= 1llu << i; + + /* Fill the arry2 slab aligned bits. */ + slabs = n_bits >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2; + memset(bmp->array2, 0xff, slabs * sizeof(bmp->array2[0])); + /* Clear the array2 left slabs. */ + memset(&bmp->array2[slabs], 0, (bmp->array2_size - slabs) * + sizeof(bmp->array2[0])); + /* Fill the array2 middle not full set slab. */ + for (i = 0; i < (n_bits & RTE_BITMAP_SLAB_BIT_MASK); i++) + bmp->array2[slabs] |= 1llu << i; + + return bmp; +} + #ifdef __cplusplus } #endif From patchwork Wed Apr 8 03:05:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suanming Mou X-Patchwork-Id: 67935 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id D6B67A0597; Wed, 8 Apr 2020 05:06:05 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 873C61BFC6; Wed, 8 Apr 2020 05:05:54 +0200 (CEST) Received: from git-send-mailer.rdmz.labs.mlnx (unknown [37.142.13.130]) by dpdk.org (Postfix) with ESMTP id 1D5B51BFB7 for ; Wed, 8 Apr 2020 05:05:52 +0200 (CEST) From: Suanming Mou To: cristian.dumitrescu@intel.com Cc: dev@dpdk.org, amo@semihalf.com Date: Wed, 8 Apr 2020 11:05:45 +0800 Message-Id: <1586315145-6633-3-git-send-email-suanmingm@mellanox.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1586315145-6633-1-git-send-email-suanmingm@mellanox.com> References: <1583828479-204084-1-git-send-email-suanmingm@mellanox.com> <1586315145-6633-1-git-send-email-suanmingm@mellanox.com> Subject: [dpdk-dev] [PATCH v2 2/2] test/bitmap: add bitmap create with all bits set case X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add the case to verify bitmap create with all bits set works correctly. Signed-off-by: Suanming Mou --- app/test/test_bitmap.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/app/test/test_bitmap.c b/app/test/test_bitmap.c index 95c5184..a8204d3 100644 --- a/app/test/test_bitmap.c +++ b/app/test/test_bitmap.c @@ -146,7 +146,7 @@ } static int -test_bitmap(void) +test_bitmap_all_clear(void) { void *mem; uint32_t bmp_size; @@ -182,4 +182,59 @@ return TEST_SUCCESS; } +static int +test_bitmap_all_set(void) +{ + void *mem; + uint32_t i; + uint64_t slab; + uint32_t pos; + uint32_t bmp_size; + struct rte_bitmap *bmp; + + bmp_size = + rte_bitmap_get_memory_footprint(MAX_BITS); + + mem = rte_zmalloc("test_bmap", bmp_size, RTE_CACHE_LINE_SIZE); + if (mem == NULL) { + printf("Failed to allocate memory for bitmap\n"); + return TEST_FAILED; + } + + bmp = rte_bitmap_init_with_all_set(MAX_BITS, mem, bmp_size); + if (bmp == NULL) { + printf("Failed to init bitmap\n"); + return TEST_FAILED; + } + + for (i = 0; i < MAX_BITS; i++) { + pos = slab = 0; + if (!rte_bitmap_scan(bmp, &pos, &slab)) { + printf("Failed with init bitmap.\n"); + return TEST_FAILED; + } + pos += (slab ? __builtin_ctzll(slab) : 0); + rte_bitmap_clear(bmp, pos); + } + + if (rte_bitmap_scan(bmp, &pos, &slab)) { + printf("Too much bits set.\n"); + return TEST_FAILED; + } + + rte_bitmap_free(bmp); + rte_free(mem); + + return TEST_SUCCESS; + +} + +static int +test_bitmap(void) +{ + if (test_bitmap_all_clear() != TEST_SUCCESS) + return TEST_FAILED; + return test_bitmap_all_set(); +} + REGISTER_TEST_COMMAND(bitmap_test, test_bitmap);