From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id E3668C30E for ; Tue, 16 Feb 2016 15:48:53 +0100 (CET) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP; 16 Feb 2016 06:48:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,455,1449561600"; d="scan'208";a="48730927" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga004.fm.intel.com with ESMTP; 16 Feb 2016 06:48:51 -0800 Received: from sivswdev02.ir.intel.com (sivswdev02.ir.intel.com [10.237.217.46]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id u1GEmoBT011235; Tue, 16 Feb 2016 14:48:50 GMT Received: from sivswdev02.ir.intel.com (localhost [127.0.0.1]) by sivswdev02.ir.intel.com with ESMTP id u1GEmoVK004274; Tue, 16 Feb 2016 14:48:50 GMT Received: (from dhunt5@localhost) by sivswdev02.ir.intel.com with id u1GEmoxI004269; Tue, 16 Feb 2016 14:48:50 GMT From: David Hunt To: dev@dpdk.org Date: Tue, 16 Feb 2016 14:48:12 +0000 Message-Id: <1455634095-4183-4-git-send-email-david.hunt@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1455634095-4183-1-git-send-email-david.hunt@intel.com> References: <1453829155-1366-1-git-send-email-david.hunt@intel.com> <1455634095-4183-1-git-send-email-david.hunt@intel.com> Subject: [dpdk-dev] [PATCH 3/6] mempool: adds a simple ring-based mempool handler using mallocs for objects X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Feb 2016 14:48:54 -0000 Signed-off-by: David Hunt --- lib/librte_mempool/Makefile | 1 + lib/librte_mempool/custom_mempool.c | 146 ++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 lib/librte_mempool/custom_mempool.c diff --git a/lib/librte_mempool/Makefile b/lib/librte_mempool/Makefile index d795b48..4f72546 100644 --- a/lib/librte_mempool/Makefile +++ b/lib/librte_mempool/Makefile @@ -44,6 +44,7 @@ LIBABIVER := 1 SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) += rte_mempool.c SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) += rte_mempool_default.c SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) += rte_mempool_stack.c +SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) += custom_mempool.c ifeq ($(CONFIG_RTE_LIBRTE_XEN_DOM0),y) SRCS-$(CONFIG_RTE_LIBRTE_MEMPOOL) += rte_dom0_mempool.c endif diff --git a/lib/librte_mempool/custom_mempool.c b/lib/librte_mempool/custom_mempool.c new file mode 100644 index 0000000..5c85203 --- /dev/null +++ b/lib/librte_mempool/custom_mempool.c @@ -0,0 +1,146 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "rte_mempool_internal.h" + +/* + * Mempool + * ======= + * + * Basic tests: done on one core with and without cache: + * + * - Get one object, put one object + * - Get two objects, put two objects + * - Get all objects, test that their content is not modified and + * put them back in the pool. + */ + +#define TIME_S 5 +#define MEMPOOL_ELT_SIZE 2048 +#define MAX_KEEP 128 +#define MEMPOOL_SIZE 8192 + +/* + * Simple example of custom mempool structure. Holds pointers to all the + * elements which are simply malloc'd in this example. + */ +struct custom_mempool { + struct rte_ring *r; /* Ring to manage elements */ + void *elements[MEMPOOL_SIZE]; /* Element pointers */ +}; + +/* + * Loop though all the element pointers and allocate a chunk of memory, then + * insert that memory into the ring. + */ +static void * +custom_mempool_alloc(struct rte_mempool *mp, + const char *name, unsigned n, + __attribute__((unused)) int socket_id, + __attribute__((unused)) unsigned flags) + +{ + static struct custom_mempool *cm; + uint32_t *objnum; + unsigned int i; + + cm = malloc(sizeof(struct custom_mempool)); + + /* Create the ring so we can enqueue/dequeue */ + cm->r = rte_ring_create(name, + rte_align32pow2(n+1), 0, 0); + if (cm->r == NULL) + return NULL; + + /* + * Loop around the elements an allocate the required memory + * and place them in the ring. + * Not worried about alignment or performance for this example. + * Also, set the first 32-bits to be the element number so we + * can check later on. + */ + for (i = 0; i < n; i++) { + cm->elements[i] = malloc(mp->elt_size); + memset(cm->elements[i], 0, mp->elt_size); + objnum = (uint32_t *)cm->elements[i]; + *objnum = i; + rte_ring_sp_enqueue_bulk(cm->r, &(cm->elements[i]), 1); + } + + return cm; +} + +static int +custom_mempool_put(void *p, void * const *obj_table, unsigned n) +{ + struct custom_mempool *cm = (struct custom_mempool *)p; + + return rte_ring_mp_enqueue_bulk(cm->r, obj_table, n); +} + +static int +custom_mempool_get(void *p, void **obj_table, unsigned n) +{ + struct custom_mempool *cm = (struct custom_mempool *)p; + + return rte_ring_mc_dequeue_bulk(cm->r, obj_table, n); +} + +static unsigned +custom_mempool_get_count(void *p) +{ + struct custom_mempool *cm = (struct custom_mempool *)p; + + return rte_ring_count(cm->r); +} + +static struct rte_mempool_handler mempool_handler_custom = { + .name = "custom_handler", + .alloc = custom_mempool_alloc, + .put = custom_mempool_put, + .get = custom_mempool_get, + .get_count = custom_mempool_get_count, +}; + +REGISTER_MEMPOOL_HANDLER(mempool_handler_custom); -- 2.5.0