From patchwork Thu Jul 21 04:46:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 114066 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 48A09A00C2; Thu, 21 Jul 2022 06:53:47 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 26C0840A87; Thu, 21 Jul 2022 06:53:47 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id E8AC240A7A for ; Thu, 21 Jul 2022 06:53:44 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LpKt65qBSzlVpK; Thu, 21 Jul 2022 12:51:58 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Thu, 21 Jul 2022 12:53:41 +0800 From: Chengwen Feng To: CC: Subject: [RFC] memarea: introduce memory area library Date: Thu, 21 Jul 2022 12:46:48 +0800 Message-ID: <20220721044648.6817-1-fengchengwen@huawei.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The memarea library is an allocator of variable-size object. It is a collection of allocated objects that can be efficiently alloc or free all at once, the main feature are as follows: a) it facilitate alloc and free of memory with low overhead. b) it provides refcnt feature which could be useful in some scenes. c) it supports MT-safe as long as it's specified at creation time. d) it's memory source could comes from: d.1) system API: malloc in C library. d.2) user provided address: it can be from the rte_malloc API series or extended memory as long as it is available. d.3) user provided memarea: it can be from another memarea. Note: a) the memarea is oriented towards the application layer, which could provides 'region-based memory management' [1] function. b) the eal library also provide memory zone/heap management, but these are tied to huge pages management. [1] https://en.wikipedia.org/wiki/Region-based_memory_management Signed-off-by: Chengwen Feng --- lib/memarea/meson.build | 10 ++ lib/memarea/rte_memarea.c | 52 ++++++++++ lib/memarea/rte_memarea.h | 205 ++++++++++++++++++++++++++++++++++++++ lib/memarea/version.map | 16 +++ lib/meson.build | 1 + 5 files changed, 284 insertions(+) create mode 100644 lib/memarea/meson.build create mode 100644 lib/memarea/rte_memarea.c create mode 100644 lib/memarea/rte_memarea.h create mode 100644 lib/memarea/version.map diff --git a/lib/memarea/meson.build b/lib/memarea/meson.build new file mode 100644 index 0000000000..d5c2c442f5 --- /dev/null +++ b/lib/memarea/meson.build @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2022 HiSilicon Limited + +sources = files( + 'rte_memarea.c', +) +headers = files( + 'rte_memarea.h', +) +deps += [] diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c new file mode 100644 index 0000000000..2989b56c60 --- /dev/null +++ b/lib/memarea/rte_memarea.c @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 202 HiSilicon Limited + */ + +#include +#include + +struct rte_memarea * +rte_memarea_create(const struct rte_memarea_param *init) +{ + RTE_SET_USED(init); + return NULL; +} + +int +rte_memarea_destroy(struct rte_memarea *ma) +{ + RTE_SET_USED(ma); + return 0; +} + +void * +rte_memarea_alloc(struct rte_memarea *ma, uint32_t size) +{ + RTE_SET_USED(ma); + RTE_SET_USED(size); + return NULL; +} + +void +rte_memarea_free(struct rte_memarea *ma, void *ptr) +{ + RTE_SET_USED(ma); + RTE_SET_USED(ptr); +} + +int +rte_memarea_refcnt_update(struct rte_memarea *ma, void *ptr, int16_t value) +{ + RTE_SET_USED(ma); + RTE_SET_USED(ptr); + RTE_SET_USED(value); + return 0; +} + +int +rte_memarea_dump(struct rte_memarea *ma, FILE *f) +{ + RTE_SET_USED(ma); + RTE_SET_USED(f); + return 0; +} diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h new file mode 100644 index 0000000000..dfbe845595 --- /dev/null +++ b/lib/memarea/rte_memarea.h @@ -0,0 +1,205 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2022 HiSilicon Limited + */ + +#ifndef RTE_MEMAREA_H +#define RTE_MEMAREA_H + +/** + * @file + * RTE Memarea. + * + * A memory area is an allocator of variable-size object. It is identified + * by its name. + * + * The memarea is a collection of allocated objects that can be efficiently + * alloc or free all at once, the main feature are as follows: + * a) it facilitate alloc and free of memory with low overhead. + * b) it provides refcnt feature which could be useful in some scenes. + * c) it supports MT-safe as long as it's specified at creation time. + * d) it's memory source could comes from: + * d.1) system API: malloc in C library. + * d.2) user provided address: it can be from the rte_malloc API series + * or extended memory as long as it is available. + * d.3) user provided memarea: it can be from another memarea. So we can + * build the following memory management structure: + * memarea-1 + * | + * v + * ------------------------ + * | | | + * v v v + * memarea-2 memarea-3 obj + * + */ + +#include +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define RTE_MEMAREA_NAMESIZE 64 + +/** + * Memarea memory source. + */ +enum rte_memarea_source { + /** Memory source comes from sys api (e.g. malloc) */ + RTE_MEMAREA_SOURCE_SYSAPI, + /** Memory source comes from user-provided address */ + RTE_MEMAREA_SOURCE_USER_ADDR, + /** Memory source comes from user-provided memarea */ + RTE_MEMAREA_SOURCE_USER_MEMAREA, +}; + +struct rte_memarea; + +struct rte_memarea_param { + char name[RTE_MEMAREA_NAMESIZE]; /**< Name of memarea */ + enum rte_memarea_source source; /**< Memory source of memarea */ + uint64_t size; /**< Size (byte) of memarea */ + uint32_t align; /**< Align of allocated object */ + /** Indicates whether the memarea should be MT-safe */ + bool mt_safe; + /** Indicates whether the memarea is visible to multiple process. + * If the memory source is RTE_MEMAREA_SOURCE_USER_ADDR, this filed + * depends on user settings and must be set. + * If the memory source is RTE_MEMAREA_SOURCE_SYSAPI or + * RTE_MEMAREA_SOURCE_USER_MEMAREA, this filed does not need to be set. + */ + bool mp_visible; + /** User provided address, this field is valid only when source + * is set to RTE_MEMAREA_SOURCE_USER_ADDR. + */ + void *user_addr; + /** User provided memarea, this field is valid only when source + * is set to RTE_MEMAREA_SOURCE_MEMAREA. + */ + struct rte_memarea *user_memarea; +}; + +struct rte_memarea { + void *private_data; /**< private management data pointer*/ + struct rte_memarea_param init; +}; + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Create memarea. + * + * Create one new memarea. + * + * @param init + * The init parameter of memarea. + * + * @return + * Non-NULL on success. Otherwise NULL is returned. + */ +__rte_experimental +struct rte_memarea *rte_memarea_create(const struct rte_memarea_param *init); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Destroy memarea. + * + * Destroy the memarea. + * + * @param ma + * The pointer of memarea. + * + * @return + * 0 on success. Otherwise negative value is returned. + */ +__rte_experimental +int rte_memarea_destroy(struct rte_memarea *ma); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Allocate memory from memarea. + * + * Allocate one memory region from the memarea. + * + * @param ma + * The pointer of memarea. + * @param size + * The memory size to be allocated. + * + * @return + * Non-NULL on success. Otherwise NULL is returned. + */ +__rte_experimental +void *rte_memarea_alloc(struct rte_memarea *ma, uint32_t size); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Free memory to memarea. + * + * Free one memory region to the memarea. + * + * @param ma + * The pointer of memarea. + * @param ptr + * The pointer of memory region which need be freed. + */ +__rte_experimental +void rte_memarea_free(struct rte_memarea *ma, void *ptr); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Update memory's refcnt. + * + * Update one memory region's refcnt. + * + * @param ma + * The pointer of memarea. + * @param ptr + * The pointer of memory region which need be updated refcnt. + * @param value + * The value which need be updated. + * Note: it could be negative. + * + * @return + * 0 on success. Otherwise negative value is returned. + */ +__rte_experimental +int rte_memarea_refcnt_update(struct rte_memarea *ma, void *ptr, int16_t value); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Dump memarea. + * + * Dump one memarea, + * + * @param ma + * The pointer of memarea. + * @param f + * The file to write the output to. + * + * @return + * 0 on success. Otherwise negative value is returned. + */ +__rte_experimental +int rte_memarea_dump(struct rte_memarea *ma, FILE *f); + +#ifdef __cplusplus +} +#endif + +#endif /* RTE_MEMAREA_H */ diff --git a/lib/memarea/version.map b/lib/memarea/version.map new file mode 100644 index 0000000000..6d75f9f7c4 --- /dev/null +++ b/lib/memarea/version.map @@ -0,0 +1,16 @@ +EXPERIMENTAL { + global: + + rte_memarea_alloc; + rte_memarea_create; + rte_memarea_destroy; + rte_memarea_dump; + rte_memarea_free; + rte_memarea_refcnt_update; + + local: *; +}; + +INTERNAL { + local: *; +}; diff --git a/lib/meson.build b/lib/meson.build index c648f7d800..521a25d6c0 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -42,6 +42,7 @@ libraries = [ 'kni', 'latencystats', 'lpm', + 'memarea', 'member', 'pcapng', 'power', From patchwork Sat Oct 8 11:33:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 117690 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 032FEA0542; Sat, 8 Oct 2022 13:39:26 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8C6FE4021F; Sat, 8 Oct 2022 13:39:26 +0200 (CEST) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by mails.dpdk.org (Postfix) with ESMTP id 3D13140146 for ; Sat, 8 Oct 2022 13:39:25 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Ml36v5bZ6zkXwJ; Sat, 8 Oct 2022 19:36:55 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 8 Oct 2022 19:39:23 +0800 From: Chengwen Feng To: , , , , , CC: , , Subject: [PATCH v6 02/10] test/memarea: support memarea test Date: Sat, 8 Oct 2022 11:33:35 +0000 Message-ID: <20221008113343.30577-3-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221008113343.30577-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20221008113343.30577-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch supports memarea test about API rte_memarea_create and rte_memarea_destroy. Signed-off-by: Chengwen Feng --- MAINTAINERS | 1 + app/test/meson.build | 2 + app/test/test_memarea.c | 168 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 app/test/test_memarea.c diff --git a/MAINTAINERS b/MAINTAINERS index 6c1cb637b0..7adeef31bf 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1566,6 +1566,7 @@ Memarea - EXPERIMENTAL M: Chengwen Feng F: lib/memarea F: doc/guides/prog_guide/memarea_lib.rst +F: app/test/test_memarea* Membership - EXPERIMENTAL M: Yipeng Wang diff --git a/app/test/meson.build b/app/test/meson.build index d5cad72116..778de8d65d 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -84,6 +84,7 @@ test_sources = files( 'test_malloc.c', 'test_malloc_perf.c', 'test_mbuf.c', + 'test_memarea.c', 'test_member.c', 'test_member_perf.c', 'test_memcpy.c', @@ -199,6 +200,7 @@ fast_tests = [ ['malloc_autotest', false, true], ['mbuf_autotest', false, true], ['mcslock_autotest', false, true], + ['memarea_autotest', true, true], ['memcpy_autotest', true, true], ['memory_autotest', false, true], ['mempool_autotest', false, true], diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c new file mode 100644 index 0000000000..6b6612dc82 --- /dev/null +++ b/app/test/test_memarea.c @@ -0,0 +1,168 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2022 HiSilicon Limited + */ + +#include +#include + +#include "test.h" + +#ifdef RTE_EXEC_ENV_WINDOWS +static int +test_memarea(void) +{ + printf("memarea not supported on Windows, skipping test\n"); + return TEST_SKIPPED; +} + +#else + +#include +#include + +#define MEMAREA_TEST_DEFAULT_SIZE 0x1000 + +#define MEMAREA_TEST_API_RUN(test_func) \ + do { \ + int ret = test_func(); \ + if (ret < 0) { \ + printf("%s Failed\n", #test_func); \ + fails++; \ + } else { \ + printf("%s Passed\n", #test_func); \ + } \ + } while (0) + +static int fails; + +static void +test_memarea_prepare(void) +{ + fails = 0; +} + +static int +test_memarea_retcode(void) +{ + return fails > 0 ? -1 : 0; +} + +static void +test_memarea_init_def_param(struct rte_memarea_param *init) +{ + memset(init, 0, sizeof(struct rte_memarea_param)); + sprintf(init->name, "%s", "test-memarea"); + init->source = RTE_MEMAREA_SOURCE_LIBC; + init->total_sz = MEMAREA_TEST_DEFAULT_SIZE; + init->mt_safe = 1; +} + +static int +test_memarea_create_bad_param(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + int i; + + /* test for NULL */ + ma = rte_memarea_create(NULL); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for invalid name */ + memset(&init, 0, sizeof(init)); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + memset(&init.name, 1, sizeof(init.name)); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for invalid source */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_MEMAREA + 1; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for total_sz */ + test_memarea_init_def_param(&init); + init.total_sz = 0; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for user address NULL */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for user address align invalid */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER; + for (i = 1; i < RTE_CACHE_LINE_SIZE; i++) { + init.user_addr = (void *)((uintptr_t)i); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + } + + /* test for user memarea NULL */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_MEMAREA; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + /* test for alg invalid */ + test_memarea_init_def_param(&init); + init.alg = RTE_MEMAREA_ALG_DEFAULT + 1; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma == NULL, "Expected NULL"); + + return 0; +} + +static int +test_memarea_create_destroy(void) +{ + uint8_t user_buffer[MEMAREA_TEST_DEFAULT_SIZE + RTE_CACHE_LINE_SIZE]; + struct rte_memarea_param init; + struct rte_memarea *ma; + + /* test for create with HEAP */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_HEAP; + init.numa_socket = SOCKET_ID_ANY; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + rte_memarea_destroy(ma); + + /* test for create with LIBC */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_LIBC; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + rte_memarea_destroy(ma); + + /* test for create with user-provided */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER; + init.user_addr = (void *)(((uintptr_t)user_buffer + RTE_CACHE_LINE_SIZE) & + ~(RTE_CACHE_LINE_SIZE - 1)); + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + rte_memarea_destroy(ma); + + return 0; +} + +static int +test_memarea(void) +{ + test_memarea_prepare(); + + MEMAREA_TEST_API_RUN(test_memarea_create_bad_param); + MEMAREA_TEST_API_RUN(test_memarea_create_destroy); + + return test_memarea_retcode(); +} + +#endif /* !RTE_EXEC_ENV_WINDOWS */ + +REGISTER_TEST_COMMAND(memarea_autotest, test_memarea); From patchwork Sat Sep 24 07:49:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116801 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 13CE1A0542; Sat, 24 Sep 2022 09:56:25 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D3CDF42BDC; Sat, 24 Sep 2022 09:56:14 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 5509E400D5 for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MZLqK0PZZzpVTC; Sat, 24 Sep 2022 15:53:17 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:08 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 03/10] memarea: support alloc/free/update-refcnt API Date: Sat, 24 Sep 2022 07:49:44 +0000 Message-ID: <20220924074951.31814-4-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch supports rte_memarea_alloc()/rte_memarea_free()/ rte_memarea_update_refcnt() API. Signed-off-by: Chengwen Feng --- doc/guides/prog_guide/memarea_lib.rst | 10 ++ lib/memarea/memarea_private.h | 3 + lib/memarea/rte_memarea.c | 143 ++++++++++++++++++++++++++ lib/memarea/rte_memarea.h | 56 ++++++++++ lib/memarea/version.map | 3 + 5 files changed, 215 insertions(+) diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst index b96dad15f6..41bc0a90cd 100644 --- a/doc/guides/prog_guide/memarea_lib.rst +++ b/doc/guides/prog_guide/memarea_lib.rst @@ -33,6 +33,16 @@ returns the pointer to the created memarea or ``NULL`` if the creation failed. The ``rte_memarea_destroy()`` function is used to destroy a memarea. +The ``rte_memarea_alloc()`` function is used to alloc one memory object from +the memarea. + +The ``rte_memarea_free()`` function is used to free one memory object which +allocated by ``rte_memarea_alloc()``. + +The ``rte_memarea_update_refcnt()`` function is used to update the memory +object's reference count, if the count reaches zero, the memory object will +be freed to memarea. + Reference --------- diff --git a/lib/memarea/memarea_private.h b/lib/memarea/memarea_private.h index c76392d3e6..98406879b9 100644 --- a/lib/memarea/memarea_private.h +++ b/lib/memarea/memarea_private.h @@ -25,6 +25,9 @@ struct rte_memarea { void *area_addr; struct memarea_elem_list elem_list; struct memarea_elem_list free_list; + + uint64_t alloc_fails; + uint64_t refcnt_check_fails; } __rte_cache_aligned; #endif /* MEMAREA_PRIVATE_H */ diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c index 868da7661d..a072f07f20 100644 --- a/lib/memarea/rte_memarea.c +++ b/lib/memarea/rte_memarea.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -94,6 +95,8 @@ memarea_alloc_area(const struct rte_memarea_param *init) ptr = memarea_alloc_from_system_api(init->total_sz); else if (init->source == RTE_MEMAREA_SOURCE_USER_ADDR) ptr = init->user_addr; + else if (init->source == RTE_MEMAREA_SOURCE_USER_MEMAREA) + ptr = rte_memarea_alloc(init->user_memarea, init->total_sz, 0); if (ptr == NULL) RTE_LOG(ERR, MEMAREA, "memarea: %s alloc memory area fail!\n", init->name); @@ -145,6 +148,8 @@ memarea_free_area(struct rte_memarea *ma) rte_free(ma->area_addr); else if (ma->init.source == RTE_MEMAREA_SOURCE_SYSTEM_API) free(ma->area_addr); + else if (ma->init.source == RTE_MEMAREA_SOURCE_USER_MEMAREA) + rte_memarea_free(ma->init.user_memarea, ma->area_addr); } void @@ -155,3 +160,141 @@ rte_memarea_destroy(struct rte_memarea *ma) memarea_free_area(ma); rte_free(ma); } + +static inline void +memarea_lock(struct rte_memarea *ma) +{ + if (ma->init.mt_safe) + rte_spinlock_lock(&ma->lock); +} + +static inline void +memarea_unlock(struct rte_memarea *ma) +{ + if (ma->init.mt_safe) + rte_spinlock_unlock(&ma->lock); +} + +#define memarea_roundup(val, align) ((((val) + ((align) - 1)) / (align)) * (align)) + +static inline bool +memarea_whether_add_node(size_t free_size, size_t need_size) +{ + size_t align_size = memarea_roundup(need_size, RTE_CACHE_LINE_SIZE); + return free_size > align_size && (free_size - align_size) > sizeof(struct memarea_elem); +} + +static inline void +memarea_add_node(struct rte_memarea *ma, struct memarea_elem *elem, size_t need_size) +{ + size_t align_size = memarea_roundup(need_size, RTE_CACHE_LINE_SIZE); + struct memarea_elem *new_elem; + new_elem = (struct memarea_elem *)((uintptr_t)elem + sizeof(struct memarea_elem) + + align_size); + new_elem->size = elem->size - align_size - sizeof(struct memarea_elem); + new_elem->cookie = MEMAREA_FREE_ELEM_COOKIE; + new_elem->refcnt = 0; + TAILQ_INSERT_AFTER(&ma->elem_list, elem, new_elem, elem_node); + TAILQ_INSERT_AFTER(&ma->free_list, elem, new_elem, free_node); + elem->size = align_size; +} + +void * +rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie) +{ + struct memarea_elem *elem; + void *ptr = NULL; + + if (unlikely(ma == NULL || size == 0)) + return NULL; + + memarea_lock(ma); + TAILQ_FOREACH(elem, &ma->free_list, free_node) { + if (elem->size < size) + continue; + if (memarea_whether_add_node(elem->size, size)) + memarea_add_node(ma, elem, size); + elem->cookie = cookie; + elem->refcnt = 1; + TAILQ_REMOVE(&ma->free_list, elem, free_node); + ptr = (void *)((uintptr_t)elem + sizeof(struct memarea_elem)); + break; + } + if (unlikely(ptr == NULL)) + ma->alloc_fails++; + memarea_unlock(ma); + + return ptr; +} + +void +rte_memarea_free(struct rte_memarea *ma, void *ptr) +{ + rte_memarea_update_refcnt(ma, ptr, -1); +} + +static inline void +memarea_merge_node(struct rte_memarea *ma, struct memarea_elem *curr, + struct memarea_elem *next, bool del_next_from_free, + bool add_curr_to_free) +{ + curr->size += next->size + sizeof(struct memarea_elem); + next->size = 0; + next->cookie = 0; + TAILQ_REMOVE(&ma->elem_list, next, elem_node); + if (del_next_from_free) + TAILQ_REMOVE(&ma->free_list, next, free_node); + if (add_curr_to_free) { + curr->cookie = MEMAREA_FREE_ELEM_COOKIE; + TAILQ_INSERT_TAIL(&ma->free_list, curr, free_node); + } +} + +static inline void +memarea_free_elem(struct rte_memarea *ma, struct memarea_elem *elem) +{ + struct memarea_elem *prev, *next; + bool merged = false; + prev = TAILQ_PREV(elem, memarea_elem_list, elem_node); + next = TAILQ_NEXT(elem, elem_node); + if (prev != NULL && prev->refcnt == 0) { + memarea_merge_node(ma, prev, elem, false, false); + elem = prev; + merged = true; + } + if (next != NULL && next->refcnt == 0) { + memarea_merge_node(ma, elem, next, true, !merged); + merged = true; + } + if (!merged) { + elem->cookie = MEMAREA_FREE_ELEM_COOKIE; + TAILQ_INSERT_TAIL(&ma->free_list, elem, free_node); + } +} + +void +rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value) +{ + struct memarea_elem *elem = (struct memarea_elem *)((uintptr_t)ptr - + sizeof(struct memarea_elem)); + + if (unlikely(ma == NULL || ptr == NULL)) + return; + + memarea_lock(ma); + if (unlikely(elem->refcnt <= 0 || elem->refcnt + value < 0)) { + RTE_LOG(ERR, MEMAREA, + "memarea: %s cookie: 0x%x curr refcnt: %d update refcnt: %d check fail!\n", + ma->init.name, elem->cookie, elem->refcnt, value); + ma->refcnt_check_fails++; + if (elem->refcnt > 0) + elem->refcnt += value; + memarea_unlock(ma); + return; + } + + elem->refcnt += value; + if (elem->refcnt == 0) + memarea_free_elem(ma, elem); + memarea_unlock(ma); +} diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h index 543cda4cac..10e0d6ad5a 100644 --- a/lib/memarea/rte_memarea.h +++ b/lib/memarea/rte_memarea.h @@ -138,6 +138,62 @@ struct rte_memarea *rte_memarea_create(const struct rte_memarea_param *init); __rte_experimental void rte_memarea_destroy(struct rte_memarea *ma); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Allocate memory from memarea. + * + * Allocate one memory object from the memarea. + * + * @param ma + * The pointer of memarea. + * @param size + * The memory size to be allocated. + * @param cookie + * User-provided footprint which could used to debug memory leak. + * + * @return + * Non-NULL on success. Otherwise NULL is returned. + */ +__rte_experimental +void *rte_memarea_alloc(struct rte_memarea *ma, size_t size, uint32_t cookie); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Free memory to memarea. + * + * Free one memory object to the memarea. + * + * @param ma + * The pointer of memarea. + * @param ptr + * The pointer of memory object which need be freed. + */ +__rte_experimental +void rte_memarea_free(struct rte_memarea *ma, void *ptr); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Update memory's refcnt. + * + * Update one memory object's refcnt. + * When refcnt is updated to be zero, the memory object is freed. + * + * @param ma + * The pointer of memarea. + * @param ptr + * The pointer of memory object which need be updated refcnt. + * @param value + * The value which need be updated. + */ +__rte_experimental +void rte_memarea_update_refcnt(struct rte_memarea *ma, void *ptr, int16_t value); + #ifdef __cplusplus } #endif diff --git a/lib/memarea/version.map b/lib/memarea/version.map index f36a04d7cf..a0026fc5f9 100644 --- a/lib/memarea/version.map +++ b/lib/memarea/version.map @@ -1,8 +1,11 @@ EXPERIMENTAL { global: + rte_memarea_alloc; rte_memarea_create; rte_memarea_destroy; + rte_memarea_free; + rte_memarea_update_refcnt; local: *; }; From patchwork Sat Sep 24 07:49:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116802 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C5B28A0542; Sat, 24 Sep 2022 09:56:30 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id BBBE442BE9; Sat, 24 Sep 2022 09:56:15 +0200 (CEST) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mails.dpdk.org (Postfix) with ESMTP id 59B6A4021F for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.56]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MZLn86L27zHtg7; Sat, 24 Sep 2022 15:51:24 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:09 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 04/10] test/memarea: support alloc/free/update-refcnt test Date: Sat, 24 Sep 2022 07:49:45 +0000 Message-ID: <20220924074951.31814-5-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch supports rte_memarea_alloc()/rte_memarea_free()/ rte_memarea_update_refcnt() test. Signed-off-by: Chengwen Feng --- app/test/test_memarea.c | 150 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 149 insertions(+), 1 deletion(-) diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c index 7c3d78652d..0a54ede4c1 100644 --- a/app/test/test_memarea.c +++ b/app/test/test_memarea.c @@ -41,6 +41,12 @@ test_memarea_init_def_param(struct rte_memarea_param *init) init->mt_safe = 1; } +static void +test_memarea_fill_allocated_region(void *ptr, size_t size) +{ + memset(ptr, 0xff, size); +} + static int test_memarea_create_bad_param(void) { @@ -106,8 +112,8 @@ static int test_memarea_create_destroy(void) { uint8_t user_buffer[MEMAREA_TEST_DEFAULT_SIZE + RTE_CACHE_LINE_SIZE]; + struct rte_memarea *ma, *user_ma; struct rte_memarea_param init; - struct rte_memarea *ma; /* test for create with RTE memory */ test_memarea_init_def_param(&init); @@ -133,6 +139,145 @@ test_memarea_create_destroy(void) RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); rte_memarea_destroy(ma); + /* test for create with user-memarea */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + user_ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(user_ma != NULL, "Expected Non-NULL"); + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_USER_MEMAREA; + init.total_sz = init.total_sz >> 1; + init.user_memarea = user_ma; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + rte_memarea_destroy(ma); + rte_memarea_destroy(user_ma); + + return 0; +} + +static int +test_memarea_alloc_fail(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + void *ptr[2]; + + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + + /* test alloc fail with big size */ + ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE, 0); + RTE_TEST_ASSERT(ptr[0] == NULL, "Expected NULL"); + + /* test alloc fail because no memory */ + ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE - RTE_CACHE_LINE_SIZE, 0); + RTE_TEST_ASSERT(ptr[0] != NULL, "Expected Non-NULL"); + ptr[1] = rte_memarea_alloc(ma, 1, 0); + RTE_TEST_ASSERT(ptr[1] == NULL, "Expected NULL"); + rte_memarea_free(ma, ptr[0]); + + /* test alloc fail when second fail */ + ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0); + RTE_TEST_ASSERT(ptr[0] != NULL, "Expected Non-NULL"); + test_memarea_fill_allocated_region(ptr[0], MEMAREA_TEST_DEFAULT_SIZE >> 1); + ptr[1] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0); + RTE_TEST_ASSERT(ptr[1] == NULL, "Expected NULL"); + rte_memarea_free(ma, ptr[0]); + ptr[1] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0); + RTE_TEST_ASSERT(ptr[1] != NULL, "Expected Non-NULL"); + test_memarea_fill_allocated_region(ptr[1], MEMAREA_TEST_DEFAULT_SIZE >> 1); + rte_memarea_free(ma, ptr[1]); + + rte_memarea_destroy(ma); + + return 0; +} + +static int +test_memarea_free_fail(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + void *ptr; + + /* prepare env */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + + /* test invalid parameters with update-refcnt */ + rte_memarea_update_refcnt(NULL, (void *)(uintptr_t)1, 0); + rte_memarea_update_refcnt(ma, NULL, 0); + rte_memarea_update_refcnt(NULL, NULL, 0); + + /* test free with refcnt fail */ + ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0); + RTE_TEST_ASSERT(ptr != NULL, "Expected Non-NULL"); + test_memarea_fill_allocated_region(ptr, MEMAREA_TEST_DEFAULT_SIZE >> 1); + rte_memarea_free(ma, ptr); + rte_memarea_free(ma, ptr); + + /* test update refcnt with fail */ + ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0); + RTE_TEST_ASSERT(ptr != NULL, "Expected Non-NULL"); + test_memarea_fill_allocated_region(ptr, MEMAREA_TEST_DEFAULT_SIZE >> 1); + rte_memarea_update_refcnt(ma, ptr, -2); + ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0); + RTE_TEST_ASSERT(ptr == NULL, "Expected NULL"); + + rte_memarea_destroy(ma); + + return 0; +} + +static int +test_memarea_alloc_free(void) +{ +#define ALLOC_MAX_NUM 8 + struct rte_memarea_param init; + struct rte_memarea *ma; + void *ptr[ALLOC_MAX_NUM]; + int i; + + /* prepare env */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + memset(ptr, 0, sizeof(ptr)); + + /* test random alloc and free */ + for (i = 0; i < ALLOC_MAX_NUM; i++) + ptr[i] = rte_memarea_alloc(ma, 1, 0); + + /* test merge left */ + rte_memarea_free(ma, ptr[0]); + rte_memarea_free(ma, ptr[1]); + + /* test merge right */ + rte_memarea_free(ma, ptr[7]); + rte_memarea_free(ma, ptr[6]); + + /* test merge left and right */ + rte_memarea_free(ma, ptr[3]); + rte_memarea_free(ma, ptr[2]); + + /* test merge remains */ + rte_memarea_free(ma, ptr[4]); + rte_memarea_free(ma, ptr[5]); + + /* test free NULL */ + rte_memarea_free(ma, ptr[6]); + + rte_memarea_destroy(ma); + return 0; } @@ -141,6 +286,9 @@ test_memarea(void) { MEMAREA_TEST_API_RUN(test_memarea_create_bad_param); MEMAREA_TEST_API_RUN(test_memarea_create_destroy); + MEMAREA_TEST_API_RUN(test_memarea_alloc_fail); + MEMAREA_TEST_API_RUN(test_memarea_free_fail); + MEMAREA_TEST_API_RUN(test_memarea_alloc_free); return 0; } From patchwork Tue Jul 18 02:40:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 129607 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9360242EA1; Tue, 18 Jul 2023 04:48:22 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0B57042B8C; Tue, 18 Jul 2023 04:48:22 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 60AF340223 for ; Tue, 18 Jul 2023 04:48:20 +0200 (CEST) Received: from dggpeml100024.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4R4jwn208ZztRXp; Tue, 18 Jul 2023 10:45:13 +0800 (CST) Received: from localhost.localdomain (10.50.163.32) by dggpeml100024.china.huawei.com (7.185.36.115) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Tue, 18 Jul 2023 10:48:18 +0800 From: Chengwen Feng To: , CC: , , , , , , Subject: [PATCH v17 5/6] memarea: support dump API Date: Tue, 18 Jul 2023 02:40:05 +0000 Message-ID: <20230718024006.2154-6-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230718024006.2154-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20230718024006.2154-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.50.163.32] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml100024.china.huawei.com (7.185.36.115) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch supports rte_memarea_dump() API which could be used for debug. Signed-off-by: Chengwen Feng Reviewed-by: Dongdong Liu Acked-by: Morten Brørup Acked-by: Anatoly Burakov --- doc/guides/prog_guide/memarea_lib.rst | 3 + lib/memarea/rte_memarea.c | 100 ++++++++++++++++++++++++++ lib/memarea/rte_memarea.h | 21 ++++++ lib/memarea/version.map | 1 + 4 files changed, 125 insertions(+) diff --git a/doc/guides/prog_guide/memarea_lib.rst b/doc/guides/prog_guide/memarea_lib.rst index 157baf3c7e..ef22294664 100644 --- a/doc/guides/prog_guide/memarea_lib.rst +++ b/doc/guides/prog_guide/memarea_lib.rst @@ -39,6 +39,9 @@ the memarea. The ``rte_memarea_free()`` function is used to free one memory object which allocated by ``rte_memarea_alloc()``. ++The ``rte_memarea_dump()`` function is used to dump the internal information ++of a memarea. + Debug Mode ---------- diff --git a/lib/memarea/rte_memarea.c b/lib/memarea/rte_memarea.c index 0c538b54ba..03e196d1b3 100644 --- a/lib/memarea/rte_memarea.c +++ b/lib/memarea/rte_memarea.c @@ -365,3 +365,103 @@ rte_memarea_free(struct rte_memarea *ma, void *ptr) memarea_unlock(ma); } + +static const char * +memarea_source_name(enum rte_memarea_source source) +{ + if (source == RTE_MEMAREA_SOURCE_HEAP) + return "heap"; + else if (source == RTE_MEMAREA_SOURCE_LIBC) + return "libc"; + else if (source == RTE_MEMAREA_SOURCE_MEMAREA) + return "memarea"; + else + return "unknown"; +} + +static const char * +memarea_alg_name(enum rte_memarea_algorithm alg) +{ + if (alg == RTE_MEMAREA_ALGORITHM_NEXTFIT) + return "nextfit"; + else + return "unknown"; +} + +static void +memarea_dump_objects_brief(struct rte_memarea *ma, FILE *f) +{ + uint32_t total_objs = 0, total_avail_objs = 0; + struct memarea_objhdr *hdr; + size_t total_avail_sz = 0; + + TAILQ_FOREACH(hdr, &ma->obj_list, obj_next) { + if (hdr == ma->guard_hdr) + break; + memarea_check_cookie(ma, hdr, COOKIE_EXPECT_STATUS_VALID); + total_objs++; + if (!MEMAREA_OBJECT_IS_ALLOCATED(hdr)) { + total_avail_objs++; + total_avail_sz += MEMAREA_OBJECT_GET_SIZE(hdr); + } + } + fprintf(f, " total-objects: %u\n", total_objs); + fprintf(f, " total-avail-objects: %u\n", total_avail_objs); + fprintf(f, " total-avail-objects-size: 0x%zx\n", total_avail_sz); +} + +static void +memarea_dump_objects_detail(struct rte_memarea *ma, FILE *f) +{ + struct memarea_objhdr *hdr; + size_t offset; + void *ptr; + + fprintf(f, " objects:\n"); + TAILQ_FOREACH(hdr, &ma->obj_list, obj_next) { + if (hdr == ma->guard_hdr) + break; + memarea_check_cookie(ma, hdr, COOKIE_EXPECT_STATUS_VALID); + ptr = RTE_PTR_ADD(hdr, sizeof(struct memarea_objhdr)); + offset = RTE_PTR_DIFF(ptr, ma->area_base); +#ifdef RTE_LIBRTE_MEMAREA_DEBUG + fprintf(f, " %p off: 0x%zx size: 0x%zx %s\n", + ptr, offset, MEMAREA_OBJECT_GET_SIZE(hdr), + MEMAREA_OBJECT_IS_ALLOCATED(hdr) ? "allocated" : ""); +#else + fprintf(f, " off: 0x%zx size: 0x%zx %s\n", + offset, MEMAREA_OBJECT_GET_SIZE(hdr), + MEMAREA_OBJECT_IS_ALLOCATED(hdr) ? "allocated" : ""); +#endif + } +} + +int +rte_memarea_dump(struct rte_memarea *ma, FILE *f, bool dump_all) +{ + if (ma == NULL || f == NULL) { + rte_errno = EINVAL; + return -1; + } + + memarea_lock(ma); + fprintf(f, "memarea name: %s\n", ma->init.name); + fprintf(f, " source: %s\n", memarea_source_name(ma->init.source)); + if (ma->init.source == RTE_MEMAREA_SOURCE_HEAP) + fprintf(f, " heap-numa-socket: %d\n", ma->init.heap.socket_id); + else if (ma->init.source == RTE_MEMAREA_SOURCE_MEMAREA) + fprintf(f, " source-memarea: %s\n", ma->init.ma.src->init.name); + fprintf(f, " algorithm: %s\n", memarea_alg_name(ma->init.alg)); + fprintf(f, " total-size: 0x%zx\n", ma->init.total_sz); + fprintf(f, " mt-safe: %s\n", ma->init.mt_safe ? "yes" : "no"); +#ifdef RTE_LIBRTE_MEMAREA_DEBUG + fprintf(f, " area-base: %p\n", ma->area_base); + fprintf(f, " guard-header: %p\n", ma->guard_hdr); +#endif + memarea_dump_objects_brief(ma, f); + if (dump_all) + memarea_dump_objects_detail(ma, f); + memarea_unlock(ma); + + return 0; +} diff --git a/lib/memarea/rte_memarea.h b/lib/memarea/rte_memarea.h index bb1bd5bae5..fa57f6c455 100644 --- a/lib/memarea/rte_memarea.h +++ b/lib/memarea/rte_memarea.h @@ -180,6 +180,27 @@ void *rte_memarea_alloc(struct rte_memarea *ma, size_t size); __rte_experimental void rte_memarea_free(struct rte_memarea *ma, void *ptr); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Dump memarea. + * + * Dump one memarea. + * + * @param ma + * The pointer of memarea. + * @param f + * The file to write the output to. + * @param dump_all + * Indicate whether to dump the allocated and free memory objects information. + * + * @return + * 0 on success. Otherwise negative value is returned (the rte_errno is set). + */ +__rte_experimental +int rte_memarea_dump(struct rte_memarea *ma, FILE *f, bool dump_all); + #ifdef __cplusplus } #endif diff --git a/lib/memarea/version.map b/lib/memarea/version.map index effbd0b488..9513d91e0b 100644 --- a/lib/memarea/version.map +++ b/lib/memarea/version.map @@ -4,6 +4,7 @@ EXPERIMENTAL { rte_memarea_alloc; rte_memarea_create; rte_memarea_destroy; + rte_memarea_dump; rte_memarea_free; local: *; From patchwork Sat Sep 24 07:49:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116799 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id A1571A0542; Sat, 24 Sep 2022 09:56:13 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4EF384021F; Sat, 24 Sep 2022 09:56:13 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 506B3400D4 for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MZLqK3RxnzpVTZ; Sat, 24 Sep 2022 15:53:17 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:09 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 06/10] test/memarea: support dump test Date: Sat, 24 Sep 2022 07:49:47 +0000 Message-ID: <20220924074951.31814-7-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch supports rte_memarea_dump() test. Signed-off-by: Chengwen Feng --- app/test/test_memarea.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c index 0a54ede4c1..ab360f0265 100644 --- a/app/test/test_memarea.c +++ b/app/test/test_memarea.c @@ -274,7 +274,39 @@ test_memarea_alloc_free(void) rte_memarea_free(ma, ptr[5]); /* test free NULL */ - rte_memarea_free(ma, ptr[6]); + rte_memarea_free(ma, NULL); + + rte_memarea_destroy(ma); + + return 0; +} + +static int +test_memarea_dump(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + int ret; + + /* prepare env */ + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + + /* test for invalid parameters */ + ret = rte_memarea_dump(NULL, stderr, false); + RTE_TEST_ASSERT(ret == -EINVAL, "Expected EINVAL"); + ret = rte_memarea_dump(ma, NULL, false); + RTE_TEST_ASSERT(ret == -EINVAL, "Expected EINVAL"); + + /* test for dump */ + (void)rte_memarea_alloc(ma, 1, 0); + (void)rte_memarea_alloc(ma, 1, 0); + (void)rte_memarea_alloc(ma, 1, 0); + ret = rte_memarea_dump(ma, stderr, true); + RTE_TEST_ASSERT(ret == 0, "Expected ZERO"); rte_memarea_destroy(ma); @@ -289,6 +321,7 @@ test_memarea(void) MEMAREA_TEST_API_RUN(test_memarea_alloc_fail); MEMAREA_TEST_API_RUN(test_memarea_free_fail); MEMAREA_TEST_API_RUN(test_memarea_alloc_free); + MEMAREA_TEST_API_RUN(test_memarea_dump); return 0; } From patchwork Sat Sep 24 07:49:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fengchengwen X-Patchwork-Id: 116800 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 37D5EA0542; Sat, 24 Sep 2022 09:56:20 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0D06142BD0; Sat, 24 Sep 2022 09:56:14 +0200 (CEST) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mails.dpdk.org (Postfix) with ESMTP id 5C671406A2 for ; Sat, 24 Sep 2022 09:56:11 +0200 (CEST) Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MZLp159hdzWgy3; Sat, 24 Sep 2022 15:52:09 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 24 Sep 2022 15:56:09 +0800 From: Chengwen Feng To: CC: , , , Subject: [PATCH v3 08/10] test/memarea: support backup memory test Date: Sat, 24 Sep 2022 07:49:49 +0000 Message-ID: <20220924074951.31814-9-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220924074951.31814-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20220924074951.31814-1-fengchengwen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.165.24] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500024.china.huawei.com (7.185.36.10) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch supports backup memory mechanism test. Signed-off-by: Chengwen Feng --- app/test/test_memarea.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c index ab360f0265..ec3475c354 100644 --- a/app/test/test_memarea.c +++ b/app/test/test_memarea.c @@ -313,6 +313,46 @@ test_memarea_dump(void) return 0; } +static int +test_memarea_backup(void) +{ + struct rte_memarea *ma, *bak_ma; + struct rte_memarea_param init; + void *ptr; + + /* prepare env */ + test_memarea_init_def_param(&init); + strcat(init.name, "_backup"); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + bak_ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(bak_ma != NULL, "Expected Non-NULL"); + test_memarea_init_def_param(&init); + init.source = RTE_MEMAREA_SOURCE_SYSTEM_API; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE >> 2; + init.bak_memarea = bak_ma; + ma = rte_memarea_create(&init); + RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL"); + + /* test for backup */ + ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 3, 0); + RTE_TEST_ASSERT(ptr != NULL, "Expected Non-NULL"); + ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0); + RTE_TEST_ASSERT(ptr != NULL, "Expected Non-NULL"); + (void)rte_memarea_dump(ma, stderr, true); + (void)rte_memarea_dump(bak_ma, stderr, true); + rte_memarea_free(ma, ptr); + ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE, 0); + RTE_TEST_ASSERT(ptr == NULL, "Expected NULL"); + (void)rte_memarea_dump(ma, stderr, true); + (void)rte_memarea_dump(bak_ma, stderr, true); + + rte_memarea_destroy(ma); + rte_memarea_destroy(bak_ma); + + return 0; +} + static int test_memarea(void) { @@ -322,6 +362,7 @@ test_memarea(void) MEMAREA_TEST_API_RUN(test_memarea_free_fail); MEMAREA_TEST_API_RUN(test_memarea_alloc_free); MEMAREA_TEST_API_RUN(test_memarea_dump); + MEMAREA_TEST_API_RUN(test_memarea_backup); return 0; }