From patchwork Tue Jan 17 18:17:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 91706 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp598124qgi; Tue, 17 Jan 2017 09:19:04 -0800 (PST) X-Received: by 10.36.129.2 with SMTP id q2mr20321321itd.26.1484673544318; Tue, 17 Jan 2017 09:19:04 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id b20si22511026ioe.107.2017.01.17.09.19.03; Tue, 17 Jan 2017 09:19:04 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 59FA760ED6; Tue, 17 Jan 2017 17:19:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 23696609D9; Tue, 17 Jan 2017 17:18:50 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 4F690609F8; Tue, 17 Jan 2017 17:18:43 +0000 (UTC) Received: from mail-lf0-f41.google.com (mail-lf0-f41.google.com [209.85.215.41]) by lists.linaro.org (Postfix) with ESMTPS id A770D609D9 for ; Tue, 17 Jan 2017 17:18:40 +0000 (UTC) Received: by mail-lf0-f41.google.com with SMTP id n124so44968257lfd.2 for ; Tue, 17 Jan 2017 09:18:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JCmxZOqj0jtWzvjs3HN8vieyV8b4vDwukfgHfRW/8vM=; b=P4eLTjTCIFczZ2ApUwec/4FxJnlpkdw8MErQq3XQoSUguKQXzUG7lJ2W9LYkQZy7mG Q++XI0pxUu+Ll9BtKrbcIGMmRb2Jgx/PYSFHiWCv9Avf8NjY9PQbWEyokRgQFR+LDxx9 66Q88GqvW2zJyAibr67r9k9VuRmayQFqhMjdm1W3XTBSQAw+SA1yyBQM7hOwFpE9KlKO bIy0myLF0a1IoQ7VYqnNtlaP/4mS6tpEk7P9ABwYdCQ3kTtX1H/mUDoMCqVR8nXCNVeX HS8E/dY74wxmsGKohEnUSRyfxs9a7vh/ZY8egxarhTUpwFbM1lqV2ICO16MjjigX+Zsl tRfA== X-Gm-Message-State: AIkVDXLohEjbMIeN+xQA2VejUU9HCCuftlr/1kj9g8Qa1XZmhH7ObVF4pjpejs54jW3cNC08vA4= X-Received: by 10.25.202.10 with SMTP id a10mr14634281lfg.23.1484673519149; Tue, 17 Jan 2017 09:18:39 -0800 (PST) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-76-66.cust.bredband2.com. [83.233.76.66]) by smtp.gmail.com with ESMTPSA id 14sm9365278lju.16.2017.01.17.09.18.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Jan 2017 09:18:38 -0800 (PST) From: Christophe Milard To: mike.holmes@linaro.org, bill.fischofer@linaro.org, yi.he@linaro.org, forrest.shi@linaro.org, maxim.uvarov@linaro.com, lng-odp@lists.linaro.org Date: Tue, 17 Jan 2017 19:17:58 +0100 Message-Id: <1484677083-12831-2-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> References: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> Subject: [lng-odp] [API-NEXT PATCHv8 1/6] linux-gen: _ishm: adding buddy and slab allocation X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" _ishm now provides functions to create/destroy pools for buddy/slab memory allocation, as well as functions to allocated/release memory from the created pools. Signed-off-by: Christophe Milard --- platform/linux-generic/Makefile.am | 2 + platform/linux-generic/_ishm.c | 14 +- platform/linux-generic/_ishmpool.c | 811 +++++++++++++++++++++ .../linux-generic/include/_ishmpool_internal.h | 56 ++ 4 files changed, 882 insertions(+), 1 deletion(-) create mode 100644 platform/linux-generic/_ishmpool.c create mode 100644 platform/linux-generic/include/_ishmpool_internal.h -- 2.7.4 diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am index 6bbe775..d615088 100644 --- a/platform/linux-generic/Makefile.am +++ b/platform/linux-generic/Makefile.am @@ -127,6 +127,7 @@ noinst_HEADERS = \ ${srcdir}/include/_fdserver_internal.h \ ${srcdir}/include/_ishm_internal.h \ ${srcdir}/include/_ishmphy_internal.h \ + ${srcdir}/include/_ishmpool_internal.h \ ${srcdir}/include/odp_align_internal.h \ ${srcdir}/include/odp_atomic_internal.h \ ${srcdir}/include/odp_buffer_inlines.h \ @@ -172,6 +173,7 @@ __LIB__libodp_linux_la_SOURCES = \ _fdserver.c \ _ishm.c \ _ishmphy.c \ + _ishmpool.c \ odp_atomic.c \ odp_barrier.c \ odp_bitmap.c \ diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_ishm.c index 23f620d..4c2578b 100644 --- a/platform/linux-generic/_ishm.c +++ b/platform/linux-generic/_ishm.c @@ -59,6 +59,7 @@ #include <_fdserver_internal.h> #include <_ishm_internal.h> #include <_ishmphy_internal.h> +#include <_ishmpool_internal.h> #include #include #include @@ -1441,8 +1442,19 @@ int _odp_ishm_init_global(void) * is performed for the main thread... Many init_global() functions * indeed assume the availability of odp_shm_reserve()...: */ - return do_odp_ishm_init_local(); + if (do_odp_ishm_init_local()) { + ODP_ERR("unable to init the main thread\n."); + goto init_glob_err4; + } + + /* get ready to create pools: */ + _odp_ishm_pool_init(); + return 0; + +init_glob_err4: + if (_odp_ishmphy_unbook_va()) + ODP_ERR("unable to unbook virtual space\n."); init_glob_err3: if (munmap(ishm_ftbl, sizeof(ishm_ftable_t)) < 0) ODP_ERR("unable to munmap main fragment table\n."); diff --git a/platform/linux-generic/_ishmpool.c b/platform/linux-generic/_ishmpool.c new file mode 100644 index 0000000..df6e49e --- /dev/null +++ b/platform/linux-generic/_ishmpool.c @@ -0,0 +1,811 @@ +/* Copyright (c) 2017, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* This file gathers the buddy and slab allocation functionality provided + * by _ishm. + * _odp_ishmpool_create() can be used to create a pool for buddy/slab + * allocation. _odp_ishmpool_create() will allocate a memory area using + * ishm_reserve() for both the control part (needed for tracking + * allocation/free...) and the user memory itself (part of which will be given + * at each ishmpool_alloc()). + * The element size provided at pool creation time determines whether + * to pool will of type buddy or slab. + * For buddy, all allocations are rounded to the nearest power of 2. + * + * The implementation of the buddy allocator is very traditional: it + * maintains N lists of free buffers. + * The control part actually contains these N queue heads, (N-M are actually + * used), the free buffers themselves being used for chaining (the chaining info + * is in the buffers: as they are "free" they should not be touched by the + * user). The control part also contains a array of bytes for remembering + * the size (actually the order) of the allocated buffers: + * There are 2^(N-M) such bytes, this number being the maximum number of + * allocated buffers (when all allocation are <= 2^M bytes) + * Buddy allocators handle fragmentation by splitting or merging blocks by 2. + * They guarantee a minimum efficiency of 50%, at worse case fragmentation. + * + * Slab implementation is even simpler, all free elements being queued in + * one single queue at init, taken from this queue when allocated and + * returned to this same queue when freed. + * + * The reason for not using malloc() is that malloc does not guarantee + * memory sharability between ODP threads (regardless of their implememtation) + * which ishm_reserve() can do. see the comments around + * _odp_ishmbud_pool_create() and ishm_reserve() for more details. + * + * This file is divided in 3 sections: the first one regroups functions + * needed by the buddy allocation. + * The second one regroups the functions needed by the slab allocator. + * The third section regroups the common functions exported externally. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include <_ishm_internal.h> +#include <_ishmpool_internal.h> +#include +#include +#include +#include +#include + +#define BUDDY_MIN_SIZE 32 /* minimal buddy allocation size */ + +typedef _odp_ishm_pool_t pool_t; /* for shorter writing */ + +/* array of ishm block index used for pools. only used for pool + * lookup by name */ +#define MAX_NB_POOL 100 +static int pool_blk_idx[MAX_NB_POOL]; + +/* section 1: functions for buddy allocation: */ + +/* free buddy blocks contains the following structure, used to link the + * free blocks together. + */ +typedef struct bblock_t { + struct bblock_t *next; + uint32_t order; +} bblock_t; + +/* value set in the 'order' table when the block is not allocated: */ +#define BBLOCK_FREE 0 + +/* compute ceil(log2(size)) */ +static uint8_t clog2(uint64_t size) +{ + uint64_t sz; + uint32_t bit; + uint8_t res; + + sz = size; /* we start by computing res = log2(sz)... */ + res = 0; + for (bit = 32; bit ; bit >>= 1) { + if (sz >= ((uint64_t)1 << bit)) { + sz >>= bit; + res += bit; + } + } + if (((uint64_t)1 << res) < size) /* ...and then ceil(x) */ + res++; + + return res; +} + +/* + * given a bblock address, and an order value, returns the address + * of the buddy bblock (the other "half") + */ +static inline bblock_t *get_bblock_buddy(pool_t *bpool, bblock_t *addr, + uint8_t order) +{ + uintptr_t b; + + b = ((uintptr_t)addr - (uintptr_t)bpool->ctrl.user_addr); + b ^= 1 << order; + return (void *)(b + (uintptr_t)bpool->ctrl.user_addr); +} + +/* + * given a buddy block address, return its number (used for busy flags): + */ +static inline uintptr_t get_bblock_nr(pool_t *bpool, void *addr) +{ + uintptr_t b; + uint8_t min_order; + + min_order = bpool->ctrl.min_order; + b = ((uintptr_t)addr - (uintptr_t)bpool->ctrl.user_addr) >> min_order; + return b; +} + +/* remove bblock from the list for bblocks of rank order. The bblock to be + * removed is really expected to be on the list: not finding it is an error */ +static inline void remove_from_list(pool_t *bpool, uint8_t order, + bblock_t *bblock) +{ + bblock_t *curr; /* current bblock (when parsing list) */ + bblock_t *prev; /* previous bblock (when parsing list) */ + + curr = bpool->ctrl.free_heads[order]; + if (!curr) + goto remove_from_list_error; + + if (curr == bblock) { + bpool->ctrl.free_heads[order] = curr->next; + return; + } + + while (curr) { + if (curr == bblock) { + prev->next = curr->next; + return; + } + prev = curr; + curr = curr->next; + } + +remove_from_list_error: + ODP_ERR("List corrupted\n"); +} + +/* + * create a buddy memory pool of given size (actually nearest power of 2), + * where allocation will never be smaller than min_alloc. + * returns a pointer to the created buddy_pool + * The allocated area contains: + * - The _odp_ishm_pool_ctrl_t structure + * - The array of ((order - min_order) of free list heads + * - The array of 'order' values, remembering sizes of allocated bblocks + * - alignment to cache line + * - The user memory + */ +static pool_t *_odp_ishmbud_pool_create(const char *pool_name, int store_idx, + uint64_t size, + uint64_t min_alloc, int flags) +{ + uint8_t order; /* pool order = ceil(log2(size)) */ + uint8_t min_order; /* pool min_order = ceil(log2(min_alloc))*/ + uint32_t max_nb_bblock; /* max number of bblock, when smallest */ + uint32_t control_sz; /* size of control area */ + uint32_t free_head_sz; /* mem area needed for list heads */ + uint32_t saved_order_sz; /* mem area to remember given sizes */ + uint64_t user_sz; /* 2^order bytes */ + uint64_t total_sz; /* total size to request */ + int blk_idx; /* as returned by _ishm_resrve() */ + pool_t *bpool; + int i; + bblock_t *first_block; + + /* a bblock_t must fit in the buffers for linked chain! */ + if (min_alloc < sizeof(bblock_t)) + min_alloc = sizeof(bblock_t); + + /* pool order is such that 2^order = size. same for min_order */ + order = clog2(size); + min_order = clog2(min_alloc); + + /* check parameters obvious wishes: */ + if (order >= 64) + return NULL; + if (order < min_order) + return NULL; + + /* at worst case, all bblocks have smallest (2^min_order) size */ + max_nb_bblock = (1 << (order - min_order)); + + /* space needed for the control area (padded to cache line size)*/ + control_sz = + ODP_CACHE_LINE_SIZE_ROUNDUP(sizeof(_odp_ishm_pool_ctrl_t)); + + /* space needed for 'order' free bblock list heads: */ + /* Note that only lists from min_order to order are really used.*/ + free_head_sz = ODP_CACHE_LINE_SIZE_ROUNDUP(sizeof(void *) * + (order + 1)); + + /* space needed for order -i.e. size- storage of alloc'd bblock:*/ + saved_order_sz = ODP_CACHE_LINE_SIZE_ROUNDUP(max_nb_bblock * + sizeof(uint8_t)); + + /* space needed for user area is 2^order bytes: */ + user_sz = 1 << order; + + total_sz = control_sz + + free_head_sz + + saved_order_sz + + user_sz; + + /* allocate required memory: */ + blk_idx = _odp_ishm_reserve(pool_name, total_sz, -1, + ODP_CACHE_LINE_SIZE, flags, 0); + if (blk_idx < 0) { + ODP_ERR("_odp_ishm_reserve failed."); + return NULL; + } + + bpool = _odp_ishm_address(blk_idx); + if (bpool == NULL) { + ODP_ERR("_odp_ishm_address failed."); + return NULL; + } + + /* store in pool array (needed for look up): */ + pool_blk_idx[store_idx] = blk_idx; + + /* remember block index, needed when pool is destroyed */ + bpool->ctrl.ishm_blk_idx = blk_idx; + + /* remember element size: 0 means unknown size, i.e. buddy alloation*/ + bpool->ctrl.element_sz = 0; + + /* prepare mutex: */ + odp_spinlock_init(&bpool->ctrl.lock); + + /* initialise pointers and things... */ + bpool->ctrl.order = order; + bpool->ctrl.min_order = min_order; + bpool->ctrl.free_heads = + (void *)((uintptr_t)bpool + control_sz); + bpool->ctrl.alloced_order = + (uint8_t *)((uintptr_t)bpool->ctrl.free_heads + free_head_sz); + bpool->ctrl.user_addr = + (void *)((uintptr_t)bpool->ctrl.alloced_order + saved_order_sz); + + /* initialize all free list to NULL, except the top biggest element:*/ + for (i = 0; i < (order - min_order); i++) + bpool->ctrl.free_heads[i] = NULL; + bpool->ctrl.free_heads[order] = bpool->ctrl.user_addr; + first_block = (bblock_t *)bpool->ctrl.user_addr; + first_block->next = NULL; + first_block->order = order; + + /* set all 'order' of allocated bblocks to free: */ + memset(bpool->ctrl.alloced_order, BBLOCK_FREE, saved_order_sz); + + return bpool; +} + +/* allocated memory from the given buddy pool */ +static void *_odp_ishmbud_alloc(pool_t *bpool, uint64_t size) +{ + uint32_t rq_order; /* requested order */ + uint32_t try_order; + bblock_t *bblock; + bblock_t *buddy; + uintptr_t nr; + + /* if size is zero or too big reject: */ + if ((!size) && (size > (1U << bpool->ctrl.order))) { + ODP_ERR("Invalid alloc size (0 or larger than whole pool)\n"); + return NULL; + } + + /* compute ceil(log2(size)), to get the requested block order: */ + rq_order = clog2(size); + + /* make sure the requested order is bigger (or same) as minimum! */ + if (rq_order < bpool->ctrl.min_order) + rq_order = bpool->ctrl.min_order; + + /* mutex from here: */ + odp_spinlock_lock(&bpool->ctrl.lock); + + /* now, start trying to allocate a bblock of rq_order. If that + * fails keep trying larger orders until pool order is reached */ + bblock = NULL; + for (try_order = rq_order; try_order <= bpool->ctrl.order; + try_order++) { + if (bpool->ctrl.free_heads[try_order]) { + /* remove from list: */ + bblock = + (bblock_t *)(bpool->ctrl.free_heads[try_order]); + bpool->ctrl.free_heads[try_order] = bblock->next; + break; + } + } + + if (!bblock) { + odp_spinlock_unlock(&bpool->ctrl.lock); + ODP_ERR("Out of memory. (Buddy pool full)\n"); + return NULL; + } + + /* OK: we got a block, but possibbly too large (if try_order>rq_order) + * return the extra halves to the pool hence splitting the bblock at + * each 'extra' order: */ + while (try_order-- > rq_order) { + /* split: */ + buddy = (bblock_t *)((uintptr_t)bblock + (1 << try_order)); + buddy->order = try_order; + /* add to list: */ + buddy->next = bpool->ctrl.free_heads[try_order]; + bpool->ctrl.free_heads[try_order] = buddy; + /* mark as free (non allocated block get size 0): */ + nr = get_bblock_nr(bpool, buddy); + bpool->ctrl.alloced_order[nr] = BBLOCK_FREE; + } + + /* remember the size if the allocated block: */ + nr = get_bblock_nr(bpool, bblock); + bpool->ctrl.alloced_order[nr] = rq_order; + + /* and return the allocated block! */ + odp_spinlock_unlock(&bpool->ctrl.lock); + return (void *)bblock; +} + +/* free a previously allocated buffer from a given buddy pool */ +static int _odp_ishmbud_free(pool_t *bpool, void *addr) +{ + uintptr_t user_start; /* start of user area */ + uintptr_t user_stop; /* stop of user area */ + uintptr_t mask; /* 2^min_order - 1 */ + bblock_t *bblock; /* bblock being freed */ + bblock_t *buddy; /* buddy bblock of bblock being freed */ + uint8_t order; /* order of block being freed */ + uintptr_t nr; /* block number */ + + /* freeing NULL is regarded as OK, though without any effect: */ + if (!addr) + return 0; + + user_start = (uintptr_t)bpool->ctrl.user_addr; + user_stop = user_start + ((uintptr_t)1 << bpool->ctrl.order); + mask = ((uintptr_t)1 << bpool->ctrl.min_order) - 1; + + /* some sanity checks: check that given address is within pool and + * that relative address has 2^min_order granularity: */ + if (((uintptr_t)addr < user_start) || + ((uintptr_t)addr > user_stop) || + (((uintptr_t)addr - user_start) & mask)) { + ODP_ERR("Invalid address to be freed\n"); + return -1; + } + + /* mutex from here: */ + odp_spinlock_lock(&bpool->ctrl.lock); + + /* collect saved block order and make sure bblock was allocated */ + bblock = (bblock_t *)addr; + nr = get_bblock_nr(bpool, bblock); + order = bpool->ctrl.alloced_order[nr]; + if (order == BBLOCK_FREE) { + ODP_ERR("Double free error\n"); + odp_spinlock_unlock(&bpool->ctrl.lock); + return -1; + } + + /* this looks like a valid free, mark at least this as free: */ + bpool->ctrl.alloced_order[nr] = BBLOCK_FREE; + + /* go up in orders, trying to merge buddies... */ + while (order < bpool->ctrl.order) { + buddy = get_bblock_buddy(bpool, bblock, order); + /*if buddy is not free: no further merge possible */ + nr = get_bblock_nr(bpool, buddy); + if (bpool->ctrl.alloced_order[nr] != BBLOCK_FREE) + break; + /*merge only bblock of same order:*/ + if (buddy->order != order) + break; + /*merge: remove buddy from free list: */ + remove_from_list(bpool, order, buddy); + /*merge: make sure we point at start of block: */ + if (bblock > buddy) + bblock = buddy; + /*merge: size of bloack has dubbled: increse order: */ + order++; + } + + /* insert the bblock into its correct free block list: */ + bblock->next = bpool->ctrl.free_heads[order]; + bpool->ctrl.free_heads[order] = bblock; + + /* remember the (possibly now merged) block order: */ + bblock->order = order; + + odp_spinlock_unlock(&bpool->ctrl.lock); + return 0; +} + +/* print buddy pool status and performs sanity checks */ +static int _odp_ishmbud_pool_status(const char *title, pool_t *bpool) +{ + uint8_t order, pool_order, pool_min_order; + uint64_t free_q_nb_bblocks[64]; + uint64_t allocated_nb_bblocks[64]; + uint64_t free_q_nb_bblocks_bytes[64]; + uint64_t allocated_nb_bblocks_bytes[64]; + uint64_t total_bytes_free; + uint64_t total_bytes_allocated; + uint64_t nr; + bblock_t *bblock; + int res = 0; + + odp_spinlock_lock(&bpool->ctrl.lock); + + pool_order = bpool->ctrl.order; + pool_min_order = bpool->ctrl.min_order; + + ODP_DBG("\n%s\n", title); + ODP_DBG("Pool Type: BUDDY\n"); + ODP_DBG("pool size: %" PRIu64 " (bytes)\n", (1UL << pool_order)); + ODP_DBG("pool order: %d\n", (int)pool_order); + ODP_DBG("pool min_order: %d\n", (int)pool_min_order); + + /* a pool wholse order is more than 64 cannot even be reached on 64 + * bit machines! */ + if (pool_order > 64) { + odp_spinlock_unlock(&bpool->ctrl.lock); + return -1; + } + + total_bytes_free = 0; + total_bytes_allocated = 0; + + /* for each queue */ + for (order = pool_min_order; order <= pool_order; order++) { + free_q_nb_bblocks[order] = 0; + free_q_nb_bblocks_bytes[order] = 0; + allocated_nb_bblocks[order] = 0; + allocated_nb_bblocks_bytes[order] = 0; + + /* get the number of buffs in the free queue for this order: */ + bblock = bpool->ctrl.free_heads[order]; + while (bblock) { + free_q_nb_bblocks[order]++; + free_q_nb_bblocks_bytes[order] += (1 << order); + bblock = bblock->next; + } + + total_bytes_free += free_q_nb_bblocks_bytes[order]; + + /* get the number of allocated buffers of this order */ + for (nr = 0; + nr < (1U << (pool_order - pool_min_order)); nr++) { + if (bpool->ctrl.alloced_order[nr] == order) + allocated_nb_bblocks[order]++; + } + + allocated_nb_bblocks_bytes[order] = + allocated_nb_bblocks[order] * (1 << order); + + total_bytes_allocated += allocated_nb_bblocks_bytes[order]; + + ODP_DBG("Order %d => Free: %" PRIu64 " buffers " + "(%" PRIu64" bytes) " + "Allocated %" PRIu64 " buffers (%" PRIu64 " bytes) " + "Total: %" PRIu64 " bytes\n", + (int)order, free_q_nb_bblocks[order], + free_q_nb_bblocks_bytes[order], + allocated_nb_bblocks[order], + allocated_nb_bblocks_bytes[order], + free_q_nb_bblocks_bytes[order] + + allocated_nb_bblocks_bytes[order]); + } + + ODP_DBG("Allocated space: %" PRIu64 " (bytes)\n", + total_bytes_allocated); + ODP_DBG("Free space: %" PRIu64 " (bytes)\n", total_bytes_free); + + if (total_bytes_free + total_bytes_allocated != (1U << pool_order)) { + ODP_DBG("Lost bytes on this pool!\n"); + res = -1; + } + + if (res) + ODP_DBG("Pool inconsistent!\n"); + + odp_spinlock_unlock(&bpool->ctrl.lock); + return res; +} + +/* section 2: functions for slab allocation: */ + +/* free slab blocks contains the following structure, used to link the + * free blocks together. + */ +typedef struct sblock_t { + struct sblock_t *next; +} sblock_t; + +/* + * create a slab memory pool of given size (rounded up to the nearest integer + * number of element, where each element has size 'elt_size'). + * returns a pointer to the created slab pool. + * The allocated area contains: + * - The _odp_ishm_pool_ctrl_t structure + * - alignment to cache line + * - The user memory + */ +static pool_t *_odp_ishmslab_pool_create(const char *pool_name, int store_idx, + uint64_t size, + uint64_t elt_size, int flags) +{ + uint32_t nb_sblock; /* number of elements in the pool */ + uint32_t control_sz; /* size of control area */ + uint64_t total_sz; /* total size to request */ + uint64_t user_sz; /* 2^order bytes */ + int blk_idx; /* as returned by _ishm_reserve() */ + pool_t *spool; + unsigned int i; + sblock_t *block; + + /* a sblock_t must fit in the buffers for linked chain! */ + if (elt_size < sizeof(bblock_t)) { + elt_size = sizeof(bblock_t); + size = size * (sizeof(bblock_t) / elt_size + + ((sizeof(bblock_t) % elt_size) ? 1 : 0)); + } + + /* nb of element fitting in the pool is just ceil(size/elt_size)*/ + nb_sblock = (size / elt_size) + ((size % elt_size) ? 1 : 0); + + /* space needed for the control area (padded to cache line size)*/ + control_sz = + ODP_CACHE_LINE_SIZE_ROUNDUP(sizeof(_odp_ishm_pool_ctrl_t)); + + /* space needed for user area is : */ + user_sz = nb_sblock * elt_size; + + total_sz = control_sz + + user_sz; + + /* allocate required memory: */ + blk_idx = _odp_ishm_reserve(pool_name, total_sz, -1, + ODP_CACHE_LINE_SIZE, flags, 0); + if (blk_idx < 0) { + ODP_ERR("_odp_ishm_reserve failed."); + return NULL; + } + + spool = _odp_ishm_address(blk_idx); + if (spool == NULL) { + ODP_ERR("_odp_ishm_address failed."); + return NULL; + } + + /* store in pool array (needed for look up): */ + pool_blk_idx[store_idx] = blk_idx; + + /* remember block index, needed when pool is destroyed */ + spool->ctrl.ishm_blk_idx = blk_idx; + + /* remember element (sblock) size and their number: */ + spool->ctrl.element_sz = elt_size; + spool->ctrl.nb_elem = nb_sblock; + + /* prepare mutex: */ + odp_spinlock_init(&spool->ctrl.lock); + + /* initialise pointers and things... */ + spool->ctrl.user_addr = + (void *)((uintptr_t)spool + control_sz); + + /* initialise the free list with the list of all elements:*/ + spool->ctrl.free_head = spool->ctrl.user_addr; + for (i = 0; i < nb_sblock - 1; i++) { + block = (sblock_t *)((uintptr_t)spool->ctrl.user_addr + + i * (uintptr_t)elt_size); + block->next = (sblock_t *)((uintptr_t)block + + (uintptr_t)elt_size); + } + block = (sblock_t *)((uintptr_t)spool->ctrl.user_addr + + (nb_sblock - 1) * (uintptr_t)elt_size); + block->next = NULL; + + return spool; +} + +/* allocated memory from the given slab pool */ +static void *_odp_ishmslab_alloc(pool_t *spool, uint64_t size) +{ + void *ret; + sblock_t *block; + + if (size > spool->ctrl.element_sz) + return NULL; + + odp_spinlock_lock(&spool->ctrl.lock); + ret = spool->ctrl.free_head; + if (!ret) { + odp_spinlock_unlock(&spool->ctrl.lock); + ODP_ERR("Out of memory. (Slab pool full)\n"); + return NULL; + } + + block = (sblock_t *)ret; + spool->ctrl.free_head = block->next; + + odp_spinlock_unlock(&spool->ctrl.lock); + return ret; +} + +/* free a previously allocated buffer from a given slab pool */ +static int _odp_ishmslab_free(pool_t *spool, void *addr) +{ + uintptr_t user_start; /* start of user area */ + uintptr_t user_stop; /* stop of user area */ + sblock_t *block; + + /* freeing NULL is regarded as OK, though without any effect: */ + if (!addr) + return 0; + + user_start = (uintptr_t)spool->ctrl.user_addr; + user_stop = user_start + spool->ctrl.element_sz * spool->ctrl.nb_elem; + + /* some sanity checks: check that given address is within pool and + * that relative address has element_sz granularity: */ + if (((uintptr_t)addr < user_start) || + ((uintptr_t)addr > user_stop) || + (((uintptr_t)addr - user_start) % spool->ctrl.element_sz)) { + ODP_ERR("Invalid address to be freed\n"); + return -1; + } + + odp_spinlock_lock(&spool->ctrl.lock); + block = (sblock_t *)addr; + block->next = (sblock_t *)spool->ctrl.free_head; + spool->ctrl.free_head = addr; + odp_spinlock_unlock(&spool->ctrl.lock); + + return 0; +} + +/* print slab pool status and performs sanity checks */ +static int _odp_ishmslab_pool_status(const char *title, pool_t *spool) +{ + sblock_t *sblock; + uint64_t nb_free_elts; /* number of free elements */ + + odp_spinlock_lock(&spool->ctrl.lock); + + ODP_DBG("\n%s\n", title); + ODP_DBG("Pool Type: FIXED SIZE\n"); + ODP_DBG("pool size: %" PRIu64 " (bytes)\n", + spool->ctrl.nb_elem * spool->ctrl.element_sz); + + /* count the number of free elements in the free list: */ + nb_free_elts = 0; + sblock = (sblock_t *)spool->ctrl.free_head; + while (sblock) { + nb_free_elts++; + sblock = sblock->next; + } + + ODP_DBG("%" PRIu64 "/%" PRIu64 " available elements.\n", + nb_free_elts, spool->ctrl.nb_elem); + + odp_spinlock_unlock(&spool->ctrl.lock); + return 0; +} + +/* section 3: common, external functions: */ + +/* create a pool: either with fixed alloc size (if max_alloc/min_alloc<2) or + * of variable block size (if max_alloc == 0) */ +pool_t *_odp_ishm_pool_create(const char *pool_name, uint64_t size, + uint64_t min_alloc, uint64_t max_alloc, int flags) +{ + int store_idx; + uint64_t real_pool_sz; + + if (min_alloc > max_alloc) { + ODP_ERR("invalid parameter: min_alloc > max_alloc"); + return NULL; + } + + /* search for a free index in pool_blk_idx for the pool */ + for (store_idx = 0; store_idx < MAX_NB_POOL; store_idx++) { + if (pool_blk_idx[store_idx] < 0) + break; + } + if (store_idx == MAX_NB_POOL) { + ODP_ERR("Max number of pool reached (MAX_NB_POOL)"); + return NULL; + } + + if ((min_alloc == 0) || ((max_alloc / min_alloc) > 2)) { + /* alloc variation is not constant enough: we go for a buddy + * allocator. The pool efficiency may go as low as 50% + * so we double the required size to make sure we can satisfy + * the user request */ + real_pool_sz = 2 * size; + return _odp_ishmbud_pool_create(pool_name, store_idx, + real_pool_sz, + BUDDY_MIN_SIZE, flags); + } else { + /* min and max are close enough so we go for constant size + * allocator: + * make sure the pool can fit the required size, even when + * only min_alloc allocation are performed: */ + real_pool_sz = ((size / min_alloc) + + ((size % min_alloc) ? 1 : 0)) + * max_alloc; + return _odp_ishmslab_pool_create(pool_name, store_idx, + real_pool_sz, + max_alloc, flags); + } +} + +/* destroy a pool. everything goes away. no operation on the pool should + * follow. */ +int _odp_ishm_pool_destroy(pool_t *pool) +{ + int store_idx; + + for (store_idx = 0; store_idx < MAX_NB_POOL; store_idx++) { + if (pool_blk_idx[store_idx] == pool->ctrl.ishm_blk_idx) { + pool_blk_idx[store_idx] = -1; + break; + } + } + + return _odp_ishm_free_by_index(pool->ctrl.ishm_blk_idx); +} + +/* allocated a buffer from a pool */ +void *_odp_ishm_pool_alloc(_odp_ishm_pool_t *pool, uint64_t size) +{ + if (!pool->ctrl.element_sz) + return _odp_ishmbud_alloc(pool, size); + else + return _odp_ishmslab_alloc(pool, size); +} + +/* free a previously allocated buffer from a pool */ +int _odp_ishm_pool_free(_odp_ishm_pool_t *pool, void *addr) +{ + if (!pool->ctrl.element_sz) + return _odp_ishmbud_free(pool, addr); + else + return _odp_ishmslab_free(pool, addr); +} + +/* Print a pool status */ +int _odp_ishm_pool_status(const char *title, _odp_ishm_pool_t *pool) +{ + if (!pool->ctrl.element_sz) + return _odp_ishmbud_pool_status(title, pool); + else + return _odp_ishmslab_pool_status(title, pool); +} + +void _odp_ishm_pool_init(void) +{ + int i; + + for (i = 0; i < MAX_NB_POOL; i++) + pool_blk_idx[i] = -1; +} + +_odp_ishm_pool_t *_odp_ishm_pool_lookup(const char *pool_name) +{ + int block_idx; + int store_idx; + + /* search for a _ishm block with the given name */ + block_idx = _odp_ishm_lookup_by_name(pool_name); + if (block_idx < 0) + return NULL; + + /* a block with that name exists: make sure it is within + * the registered pools */ + for (store_idx = 0; store_idx < MAX_NB_POOL; store_idx++) { + if (pool_blk_idx[store_idx] == block_idx) + return _odp_ishm_address(block_idx); + } + + return NULL; +} diff --git a/platform/linux-generic/include/_ishmpool_internal.h b/platform/linux-generic/include/_ishmpool_internal.h new file mode 100644 index 0000000..5c5304a --- /dev/null +++ b/platform/linux-generic/include/_ishmpool_internal.h @@ -0,0 +1,56 @@ +/* Copyright (c) 2017, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef ODP_ISHMBUDDY_INTERNAL_H_ +#define ODP_ISHMBUDDY_INTERNAL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +typedef struct _odp_ishm_pool_ctrl_t { + uint32_t element_sz; /* 0 for buddy pools, >0 for slab. */ + int ishm_blk_idx; /* the block index returned by _ishm_resrve()*/ + odp_spinlock_t lock; /* for pool access mutex */ + void *user_addr; /* user pool area ('real user pool') */ + union { + struct { /* things needed for buddy pools: */ + uint8_t order; /* pool is 2^order bytes long */ + uint8_t min_order; /*alloc won't go below 2^min_order*/ + void **free_heads; /* 'order' free list heads. */ + uint8_t *alloced_order; /* size of blocks, 0=free */ + }; + struct { /* things needed for slab pools: */ + void *free_head; /* free element list head */ + uint64_t nb_elem;/* total number of elements in pool */ + }; + }; +} _odp_ishm_pool_ctrl_t; + +typedef struct _odp_ishm_pool_t { + _odp_ishm_pool_ctrl_t ctrl; /* control part */ + uint8_t mem[1]; /* area for heads, saved alloc'd orders, data*/ +} _odp_ishm_pool_t; + +_odp_ishm_pool_t *_odp_ishm_pool_create(const char *pool_name, + uint64_t size, + uint64_t min_alloc, + uint64_t max_alloc, int flags); +int _odp_ishm_pool_destroy(_odp_ishm_pool_t *pool); +void *_odp_ishm_pool_alloc(_odp_ishm_pool_t *pool, uint64_t size); +int _odp_ishm_pool_free(_odp_ishm_pool_t *pool, void *addr); +int _odp_ishm_pool_status(const char *title, _odp_ishm_pool_t *pool); +_odp_ishm_pool_t *_odp_ishm_pool_lookup(const char *pool_name); +void _odp_ishm_pool_init(void); + +#ifdef __cplusplus +} +#endif + +#endif From patchwork Tue Jan 17 18:17:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 91709 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp599514qgi; Tue, 17 Jan 2017 09:21:59 -0800 (PST) X-Received: by 10.55.127.7 with SMTP id a7mr26639636qkd.111.1484673718980; Tue, 17 Jan 2017 09:21:58 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id 52si17033970qtv.302.2017.01.17.09.21.58; Tue, 17 Jan 2017 09:21:58 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 960CB609E3; Tue, 17 Jan 2017 17:21:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 4AB7960ECF; Tue, 17 Jan 2017 17:19:02 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 2DB85609E7; Tue, 17 Jan 2017 17:18:46 +0000 (UTC) Received: from mail-lf0-f41.google.com (mail-lf0-f41.google.com [209.85.215.41]) by lists.linaro.org (Postfix) with ESMTPS id A12FF609DC for ; Tue, 17 Jan 2017 17:18:41 +0000 (UTC) Received: by mail-lf0-f41.google.com with SMTP id z134so110250773lff.3 for ; Tue, 17 Jan 2017 09:18:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=rHSByxQFTNEnuikhA9YCorvSGfYuiai8cp7evgKqoaM=; b=G4wsaagqP6ByySGrzFF3Mmmto5gi/FcbIwDN+l3Wy2xudtN3764HJIhY7vSZATEwIR Oca7Shm7TxXVwEqnM/hljz1UU46AyvN7puUHfhyRR3QV8LsnnLtOYAKgyupjWBBBOnOc W4RA2cqGxhK5cvC3Qhi4+20aHSjIr805MIe+xVi8ngeVG4ECekWcHo1sNYmTVRTUzHPV CsCvrEXhgtrpbT9531sXLDwUs1HmJ2EC/LXHUFnRWGfjkN/I566LfucqW/HmFkMVhGIc +TcFvuABfhrZF6EdZ7hpw5bQoQJ7MO5GFfJ+i+Ct4jUHyLnK400tvo55caDcL2ijm6Jf IXnw== X-Gm-Message-State: AIkVDXLorCqiOsJVplQ/al7VWoHIfK4RDXmJLRA+JMlfEo/Me6zFZ/CHUExx8/poLGLEtb4M5gk= X-Received: by 10.25.211.7 with SMTP id k7mr14813882lfg.62.1484673520381; Tue, 17 Jan 2017 09:18:40 -0800 (PST) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-76-66.cust.bredband2.com. [83.233.76.66]) by smtp.gmail.com with ESMTPSA id 14sm9365278lju.16.2017.01.17.09.18.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Jan 2017 09:18:39 -0800 (PST) From: Christophe Milard To: mike.holmes@linaro.org, bill.fischofer@linaro.org, yi.he@linaro.org, forrest.shi@linaro.org, maxim.uvarov@linaro.com, lng-odp@lists.linaro.org Date: Tue, 17 Jan 2017 19:17:59 +0100 Message-Id: <1484677083-12831-3-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> References: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> Subject: [lng-odp] [API-NEXT PATCHv8 2/6] drv: adding odpdrv_shm_pool functions X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Adding functions to create and destroy memory pools (from which memory can be allocated and freed) are added. These functions enable the usage of small memory amount (compared to drvshm_reserve() whose granularity is the page size). The usage of this pool guatantees that allocated memory is sharable between ODP threads. (using malloc would not work when ODP threads are linux processes). Signed-off-by: Christophe Milard Reviewed-by: Petri Savolainen --- include/odp/drv/spec/shm.h | 99 ++++++++++++++++++++++ .../linux-generic/include/odp/drv/plat/shm_types.h | 3 + 2 files changed, 102 insertions(+) -- 2.7.4 diff --git a/include/odp/drv/spec/shm.h b/include/odp/drv/spec/shm.h index ef64f5d..ee8181f 100644 --- a/include/odp/drv/spec/shm.h +++ b/include/odp/drv/spec/shm.h @@ -220,6 +220,105 @@ int odpdrv_shm_print_all(const char *title); uint64_t odpdrv_shm_to_u64(odpdrv_shm_t hdl); /** + * drv shm pool parameters + * Used to communicate pool creation options. + */ +typedef struct { + /** Sum of all (simultaneous) allocs (bytes)*/ + uint64_t pool_size; + + /** Minimum alloc size user will request from pool (bytes)*/ + uint64_t min_alloc; + + /** Maximum alloc size user will request from pool (bytes)*/ + uint64_t max_alloc; +} odpdrv_shm_pool_param_t; + +/** + * @typedef odpdrv_shm_pool_t + * odpdrv shared memory pool + */ + +/** + * @def ODPDRV_SHM_POOL_INVALID + * Invalid odpdrv shared memory pool + */ + +/** + * Create a memory pool + * + * This routine is used to create a memory pool. The use of pool name is + * optional. + * Unique names are not required. However, odpdrv_shm_pool_lookup() + * returns only a single matching pool. + * + * @param pool_name Name of the pool or NULL. + * @param param Pool parameters. + * + * @return Handle of the created drv shm memory pool + * @retval ODPDRV_SHM_POOL_INVALID Pool could not be created + */ +odpdrv_shm_pool_t odpdrv_shm_pool_create(const char *pool_name, + odpdrv_shm_pool_param_t *param); + +/** + * Destroy a pool previously created by odpdrv_shm_pool_create() + * + * @param pool Handle of the pool to be destroyed + * + * @retval 0 Success + * @retval <0 Failure + * + * @note This routine destroys a previously created pool, and will destroy any + * internal shared memory objects associated with the pool. Results are + * undefined if an attempt is made to destroy a pool that contains allocated + * or otherwise active allocations. + */ +int odpdrv_shm_pool_destroy(odpdrv_shm_pool_t pool); + +/** + * Find a memory pool by name + * + * @param name Name of the pool + * + * @return Handle of the first matching pool + * @retval ODPDRV_SHM_POOL_INVALID Pool could not be found + */ +odpdrv_shm_pool_t odpdrv_shm_pool_lookup(const char *name); + +/** + * Allocate memory from a memory pool + * + * @param pool Memory pool handle + * @param size Number of bytes to allocate (bytes) + * + * @return A pointer to the allocated memory + * @retval NULL on error. + */ +void *odpdrv_shm_pool_alloc(odpdrv_shm_pool_t pool, uint64_t size); + +/** + * Free memory back to a memory pool + * + * @param pool Memory pool handle + * @param addr pointer to a previously allocated memory + * (as returned by a previous call to odpdrv_shm_pool_alloc) + */ +void odpdrv_shm_pool_free(odpdrv_shm_pool_t pool, void *addr); + +/** + * Print memory pool info + * + * @param title A string to be printed as a title (e.g. location) + * @param pool Memory pool handle + * + * @return 0 on success, negative value if pool inconsistency is detected. + * + * @note This routine writes implementation-defined information about the + * specified pool to the ODP log. The intended use is for debugging. + */ +int odpdrv_shm_pool_print(const char *title, odpdrv_shm_pool_t pool); +/** * @} */ diff --git a/platform/linux-generic/include/odp/drv/plat/shm_types.h b/platform/linux-generic/include/odp/drv/plat/shm_types.h index c48eeca..50a0837 100644 --- a/platform/linux-generic/include/odp/drv/plat/shm_types.h +++ b/platform/linux-generic/include/odp/drv/plat/shm_types.h @@ -35,6 +35,9 @@ static inline uint64_t odpdrv_shm_to_u64(odpdrv_shm_t hdl) return _odpdrv_pri(hdl); } +typedef ODPDRV_HANDLE_T(odpdrv_shm_pool_t); + +#define ODPDRV_SHM_POOL_INVALID _odpdrv_cast_scalar(odpdrv_shm_pool_t, NULL) /** * @} */ From patchwork Tue Jan 17 18:18:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 91708 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp599223qgi; Tue, 17 Jan 2017 09:21:17 -0800 (PST) X-Received: by 10.55.174.130 with SMTP id x124mr13166844qke.63.1484673677171; Tue, 17 Jan 2017 09:21:17 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id h30si17056182qta.318.2017.01.17.09.21.16; Tue, 17 Jan 2017 09:21:17 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 9A982609CE; Tue, 17 Jan 2017 17:21:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id EC04E60EC2; Tue, 17 Jan 2017 17:18:53 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id E6566609E7; Tue, 17 Jan 2017 17:18:45 +0000 (UTC) Received: from mail-lf0-f52.google.com (mail-lf0-f52.google.com [209.85.215.52]) by lists.linaro.org (Postfix) with ESMTPS id BA187609E1 for ; Tue, 17 Jan 2017 17:18:42 +0000 (UTC) Received: by mail-lf0-f52.google.com with SMTP id k86so113541571lfi.0 for ; Tue, 17 Jan 2017 09:18:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zU/Fst4OP+SAbn8Wz6rR6k2ctH2ECsmnBhlDwpc5p3k=; b=egNuteFVLsS7aYy84HXOo2HiOr/7e6qH1UQHSajnik2cwzcHa8DrXARlor+XRL+T5f 7S6KOJzIKNio2BD1J31YHbaWmu/9JgcHOt/s6XDBA4C/p8VQBouotIOUdA0CE5BiE8K6 i5dCvwQGpLSEy3Xv0Y7ZzrOwpbLm03wT6L17M1PqNknm9Dt5hdv6/w5rqXyDg0YTxW2M lYT22iLJ+lgPRL32rdQ73esFzNkXa2jPL1ZBQgFKL6xURzk1FQG6f4hJ9aVhuSO287JB 9SYnnfBg6OyZDLtovj+EFTEZ7qMKyBS+A5rDFzWoorB7+HLTzg64zHxE+upQLJUng40Y P+nw== X-Gm-Message-State: AIkVDXJUgauV9pgw3/hjRZJO69qrHafUh3aqNjj3+TK9coh/JHfFcB6OvdUAV9AD23lqsVoo+O4= X-Received: by 10.25.4.9 with SMTP id 9mr13176001lfe.45.1484673521490; Tue, 17 Jan 2017 09:18:41 -0800 (PST) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-76-66.cust.bredband2.com. [83.233.76.66]) by smtp.gmail.com with ESMTPSA id 14sm9365278lju.16.2017.01.17.09.18.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Jan 2017 09:18:40 -0800 (PST) From: Christophe Milard To: mike.holmes@linaro.org, bill.fischofer@linaro.org, yi.he@linaro.org, forrest.shi@linaro.org, maxim.uvarov@linaro.com, lng-odp@lists.linaro.org Date: Tue, 17 Jan 2017 19:18:00 +0100 Message-Id: <1484677083-12831-4-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> References: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> Subject: [lng-odp] [API-NEXT PATCHv8 3/6] linux-gen: drv: shm: adding pool allocator X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Adding functions to create memory pools and allocate / free memory from the created pools. These functions calls their _ishm conterpart, of course. Signed-off-by: Christophe Milard --- platform/linux-generic/drv_shm.c | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) -- 2.7.4 diff --git a/platform/linux-generic/drv_shm.c b/platform/linux-generic/drv_shm.c index 9b2560d..325632e 100644 --- a/platform/linux-generic/drv_shm.c +++ b/platform/linux-generic/drv_shm.c @@ -8,6 +8,7 @@ #include #include #include <_ishm_internal.h> +#include <_ishmpool_internal.h> static inline uint32_t from_handle(odpdrv_shm_t shm) { @@ -100,3 +101,46 @@ int odpdrv_shm_print_all(const char *title) { return _odp_ishm_status(title); } + +odpdrv_shm_pool_t odpdrv_shm_pool_create(const char *pool_name, + odpdrv_shm_pool_param_t *param) +{ + int flags; + + /* force unique address for all ODP threads */ + flags = _ODP_ISHM_SINGLE_VA; + return (odpdrv_shm_pool_t)_odp_ishm_pool_create(pool_name, + param->pool_size, + param->min_alloc, + param->max_alloc, + flags); +} + +int odpdrv_shm_pool_destroy(odpdrv_shm_pool_t pool) +{ + return _odp_ishm_pool_destroy((_odp_ishm_pool_t *)(void*)pool); +} + +odpdrv_shm_pool_t odpdrv_shm_pool_lookup(const char *name) +{ + return (odpdrv_shm_pool_t)_odp_ishm_pool_lookup(name); +} + +void *odpdrv_shm_pool_alloc(odpdrv_shm_pool_t pool, uint64_t size) +{ + return _odp_ishm_pool_alloc((_odp_ishm_pool_t *)(void*)pool, size); +} + +void odpdrv_shm_pool_free(odpdrv_shm_pool_t pool, void *addr) +{ + (void)_odp_ishm_pool_free((_odp_ishm_pool_t *)(void*)pool, addr); +} + +int odpdrv_shm_pool_print(const char *title, odpdrv_shm_pool_t pool) +{ + return _odp_ishm_pool_status(title, (_odp_ishm_pool_t *)(void*)pool); +} + +/** + * @} + */ From patchwork Tue Jan 17 18:18:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 91710 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp599930qgi; Tue, 17 Jan 2017 09:23:04 -0800 (PST) X-Received: by 10.36.33.135 with SMTP id e129mr10945668ita.9.1484673784907; Tue, 17 Jan 2017 09:23:04 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id 201si12908460ita.114.2017.01.17.09.23.04; Tue, 17 Jan 2017 09:23:04 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 6CDE760EC2; Tue, 17 Jan 2017 17:23:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id A321960ED6; Tue, 17 Jan 2017 17:19:09 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 19C70609E1; Tue, 17 Jan 2017 17:18:47 +0000 (UTC) Received: from mail-lf0-f48.google.com (mail-lf0-f48.google.com [209.85.215.48]) by lists.linaro.org (Postfix) with ESMTPS id F27F1609D9 for ; Tue, 17 Jan 2017 17:18:43 +0000 (UTC) Received: by mail-lf0-f48.google.com with SMTP id z134so110251820lff.3 for ; Tue, 17 Jan 2017 09:18:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=J3t1jsUbE/y0pMceSFx68iqhBLMGDFw5rGvkMk5uEi8=; b=HxHC5ltO8vJD99pkXoOBJxBo+4vyCX9rnlrpI+MV8SbiGJ2Y/xrBNxo/6aQRCcfYwO 9EYOXww+/fySbMVYQiSLh/mxCetYL1hatiIaaGMyK0Fu8ZPJsFgfLIypZICwUAXWOXvW B0ptGyCpLQ71zd9JFWAGREqckixaePci03+kJML8K7dWKFlW7wD06cB9SN0rdYdzelES 2Nm8aTeWT5G8wwvoi8DihtArzdqopsHfHEOyxExyllFc8a+82MljDk+udQEEXnOtsSGd huKALKWoJlXfweM0NEt21oQWydIzM+THsV7vVQvUBkdVq5ui2WoTg3XT7p412EORV5fm T5mg== X-Gm-Message-State: AIkVDXKFO7Eu2kLRQufogNsx98Q2285yYuiG+GheXPPW/RlOBEV6L4pIEcAaQbjS8y0tDlOIC60= X-Received: by 10.25.234.216 with SMTP id y85mr13325718lfi.35.1484673522719; Tue, 17 Jan 2017 09:18:42 -0800 (PST) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-76-66.cust.bredband2.com. [83.233.76.66]) by smtp.gmail.com with ESMTPSA id 14sm9365278lju.16.2017.01.17.09.18.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Jan 2017 09:18:42 -0800 (PST) From: Christophe Milard To: mike.holmes@linaro.org, bill.fischofer@linaro.org, yi.he@linaro.org, forrest.shi@linaro.org, maxim.uvarov@linaro.com, lng-odp@lists.linaro.org Date: Tue, 17 Jan 2017 19:18:01 +0100 Message-Id: <1484677083-12831-5-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> References: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> Subject: [lng-odp] [API-NEXT PATCHv8 4/6] test: drv: shm: adding basic buddy allocation tests X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Basic tests for odpdrv_shm_pool are added here, creating a buddy pool and performing basic alloc/free on it Signed-off-by: Christophe Milard --- .../common_plat/validation/drv/drvshmem/drvshmem.c | 92 ++++++++++++++++++++++ .../common_plat/validation/drv/drvshmem/drvshmem.h | 1 + 2 files changed, 93 insertions(+) -- 2.7.4 diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.c b/test/common_plat/validation/drv/drvshmem/drvshmem.c index 0247a03..5843573 100644 --- a/test/common_plat/validation/drv/drvshmem/drvshmem.c +++ b/test/common_plat/validation/drv/drvshmem/drvshmem.c @@ -21,6 +21,12 @@ #define STRESS_RANDOM_SZ 5 #define STRESS_ITERATION 5000 +#define POOL_NAME "test_pool" +#define POOL_SZ (1UL << 20) /* 1 MBytes */ +#define TEST_SZ 1000 +#define SZ_1K 1024 +#define BUFF_PATTERN 0xA3 + typedef enum { STRESS_FREE, /* entry is free and can be allocated */ STRESS_BUSY, /* entry is being processed: don't touch */ @@ -762,11 +768,97 @@ void drvshmem_test_stress(void) CU_ASSERT(odpdrv_shm_print_all("After stress tests") == base); } +void drvshmem_test_buddy_basic(void) +{ + odpdrv_shm_pool_param_t pool_params; + odpdrv_shm_pool_t pool, found_pool; + uint8_t *buff; + uint8_t *addrs[TEST_SZ]; + uint8_t length; + int i, j; + + /* create a pool and check that it can be looked up */ + pool_params.pool_size = POOL_SZ; + pool_params.min_alloc = 1; + pool_params.max_alloc = POOL_SZ; + pool = odpdrv_shm_pool_create(POOL_NAME, &pool_params); + found_pool = odpdrv_shm_pool_lookup(POOL_NAME); + CU_ASSERT(found_pool == pool); + + /* alloc a 1k buffer, filling its contents: */ + buff = odpdrv_shm_pool_alloc(pool, SZ_1K); + CU_ASSERT_PTR_NOT_NULL(buff); + for (i = 0; i < SZ_1K; i++) + buff[i] = BUFF_PATTERN; + odpdrv_shm_pool_print("buddy test: 1K reserved", pool); + + /* alloc as many buffer a possible on increseasing sz */ + for (i = 0; i < TEST_SZ; i++) { + length = i * 16; + addrs[i] = odpdrv_shm_pool_alloc(pool, length); + /* if alloc was success, fill buffer for later check */ + if (addrs[i]) { + for (j = 0; j < length; j++) + addrs[i][j] = (uint8_t)(length & 0xFF); + } + } + odpdrv_shm_pool_print("buddy test: after many mallocs", pool); + + /* release every 3rth buffer, checking contents: */ + for (i = 0; i < TEST_SZ; i += 3) { + /* if buffer was allocated, check the pattern in it */ + if (addrs[i]) { + length = i * 16; + for (j = 0; j < length; j++) + CU_ASSERT(addrs[i][j] == + (uint8_t)(length & 0xFF)); + } + odpdrv_shm_pool_free(pool, addrs[i]); + } + odpdrv_shm_pool_print("buddy test: after 1/3 free:", pool); + + /* realloc them:*/ + for (i = 0; i < TEST_SZ; i += 3) { + length = i * 16; + addrs[i] = odpdrv_shm_pool_alloc(pool, length); + /* if alloc was success, fill buffer for later check */ + if (addrs[i]) { + for (j = 0; j < length; j++) + addrs[i][j] = (uint8_t)(length & 0xFF); + } + } + odpdrv_shm_pool_print("buddy test: after realloc:", pool); + + /* free all (except buff), checking contents: */ + for (i = 0; i < TEST_SZ; i++) { + /* if buffer was allocated, check the pattern in it */ + if (addrs[i]) { + length = i * 16; + for (j = 0; j < length; j++) + CU_ASSERT(addrs[i][j] == + (uint8_t)(length & 0xFF)) + } + odpdrv_shm_pool_free(pool, addrs[i]); + } + odpdrv_shm_pool_print("buddy test: after all but 1K free:", pool); + + /* check contents of our initial 1K buffer: */ + for (i = 0; i < SZ_1K; i++) + CU_ASSERT((buff[i] == BUFF_PATTERN)) + odpdrv_shm_pool_free(pool, buff); + + odpdrv_shm_pool_print("buddy test: after all free", pool); + + /* destroy pool: */ + odpdrv_shm_pool_destroy(pool); +} + odp_testinfo_t drvshmem_suite[] = { ODP_TEST_INFO(drvshmem_test_basic), ODP_TEST_INFO(drvshmem_test_reserve_after_fork), ODP_TEST_INFO(drvshmem_test_singleva_after_fork), ODP_TEST_INFO(drvshmem_test_stress), + ODP_TEST_INFO(drvshmem_test_buddy_basic), ODP_TEST_INFO_NULL, }; diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.h b/test/common_plat/validation/drv/drvshmem/drvshmem.h index f4c26a1..ab45f7c 100644 --- a/test/common_plat/validation/drv/drvshmem/drvshmem.h +++ b/test/common_plat/validation/drv/drvshmem/drvshmem.h @@ -14,6 +14,7 @@ void drvshmem_test_basic(void); void drvshmem_test_reserve_after_fork(void); void drvshmem_test_singleva_after_fork(void); void drvshmem_test_stress(void); +void drvshmem_test_buddy_basic(void); /* test arrays: */ extern odp_testinfo_t drvshmem_suite[]; From patchwork Tue Jan 17 18:18:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 91711 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp600179qgi; Tue, 17 Jan 2017 09:23:39 -0800 (PST) X-Received: by 10.107.1.149 with SMTP id 143mr19444539iob.179.1484673819496; Tue, 17 Jan 2017 09:23:39 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id p72si22520723ioo.245.2017.01.17.09.23.39; Tue, 17 Jan 2017 09:23:39 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 0B029609E7; Tue, 17 Jan 2017 17:23:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id CF5EE60EDF; Tue, 17 Jan 2017 17:19:12 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 3D5EE609DC; Tue, 17 Jan 2017 17:18:48 +0000 (UTC) Received: from mail-lf0-f49.google.com (mail-lf0-f49.google.com [209.85.215.49]) by lists.linaro.org (Postfix) with ESMTPS id 4AC2D609CE for ; Tue, 17 Jan 2017 17:18:45 +0000 (UTC) Received: by mail-lf0-f49.google.com with SMTP id n124so44970128lfd.2 for ; Tue, 17 Jan 2017 09:18:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xlx6Yj2OAvgVnwXInAgt5/ucWEOEY54ktSdP5hbP8W4=; b=CQxBND8LmlUr0fkUYS8zhk6Q8Cb86hmJxzLH4nh7i0gboBJ2MRy5vk+spWfqtU0J4s O1KMGcxxgGhyQgfh+VwXNtrXG2zNZwl9ftGArYdsSpxQjhcSYLBbmcaNr+H1QrDyalF3 CXz7u4yYxO7pDOv4KqctmjK8c8yNDBydPdAltS1zAI713wyAeLxKHJAA+duObAcM4lbF kL0mjoo63bh0QbsqUO4yIAr0bw+DO0Fh21wwQUgx4nt3IdAwSuSw1lm9RGu1L84ztxOJ IPtiW8yxPamzEZDVhWg8V0468GBBAvVH5/xO+Au/0XkTcCgaO7JA7dZ8qFlcikGIQ5ZA Sraw== X-Gm-Message-State: AIkVDXIHSDtoVZz5zv1D05xK9/LhQ5WYzt05ma388O/VaPgRwnY30rks8LhOAvUxi1k26LTI07Q= X-Received: by 10.46.84.18 with SMTP id i18mr12803180ljb.63.1484673523786; Tue, 17 Jan 2017 09:18:43 -0800 (PST) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-76-66.cust.bredband2.com. [83.233.76.66]) by smtp.gmail.com with ESMTPSA id 14sm9365278lju.16.2017.01.17.09.18.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Jan 2017 09:18:43 -0800 (PST) From: Christophe Milard To: mike.holmes@linaro.org, bill.fischofer@linaro.org, yi.he@linaro.org, forrest.shi@linaro.org, maxim.uvarov@linaro.com, lng-odp@lists.linaro.org Date: Tue, 17 Jan 2017 19:18:02 +0100 Message-Id: <1484677083-12831-6-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> References: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> Subject: [lng-odp] [API-NEXT PATCHv8 5/6] test: drv: shm: adding basic fixed size allocation tests X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Basic tests for odpdrv_shm_pool are added here, creating a fixed size pool and performing basic alloc/free on it Signed-off-by: Christophe Milard --- .../common_plat/validation/drv/drvshmem/drvshmem.c | 86 ++++++++++++++++++++++ .../common_plat/validation/drv/drvshmem/drvshmem.h | 1 + 2 files changed, 87 insertions(+) -- 2.7.4 diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.c b/test/common_plat/validation/drv/drvshmem/drvshmem.c index 5843573..d4dedea 100644 --- a/test/common_plat/validation/drv/drvshmem/drvshmem.c +++ b/test/common_plat/validation/drv/drvshmem/drvshmem.c @@ -853,12 +853,98 @@ void drvshmem_test_buddy_basic(void) odpdrv_shm_pool_destroy(pool); } +void drvshmem_test_slab_basic(void) +{ + odpdrv_shm_pool_param_t pool_params; + odpdrv_shm_pool_t pool, found_pool; + uint8_t *buff; + uint8_t *addrs[TEST_SZ]; + uint16_t length; + int i, j; + + /* create a pool and check that it can be looked up */ + pool_params.pool_size = POOL_SZ; + pool_params.min_alloc = SZ_1K; /* constant size will give slab */ + pool_params.max_alloc = SZ_1K; + pool = odpdrv_shm_pool_create(POOL_NAME, &pool_params); + found_pool = odpdrv_shm_pool_lookup(POOL_NAME); + CU_ASSERT(found_pool == pool); + + /* alloc a 1k buffer, filling its contents: */ + buff = odpdrv_shm_pool_alloc(pool, SZ_1K); + CU_ASSERT_PTR_NOT_NULL(buff); + for (i = 0; i < SZ_1K; i++) + buff[i] = BUFF_PATTERN; + odpdrv_shm_pool_print("buddy test: 1K reserved", pool); + + /* alloc as many 1K buffer a possible */ + for (i = 0; i < TEST_SZ; i++) { + length = SZ_1K; + addrs[i] = odpdrv_shm_pool_alloc(pool, length); + /* if alloc was success, fill buffer for later check */ + if (addrs[i]) { + for (j = 0; j < length; j++) + addrs[i][j] = (uint8_t)(length & 0xFF); + } + } + odpdrv_shm_pool_print("slab test: after many mallocs", pool); + + /* release every 3rth buffer, checking contents: */ + for (i = 0; i < TEST_SZ; i += 3) { + /* if buffer was allocated, check the pattern in it */ + if (addrs[i]) { + length = SZ_1K; + for (j = 0; j < length; j++) + CU_ASSERT(addrs[i][j] == + (uint8_t)(length & 0xFF)); + } + odpdrv_shm_pool_free(pool, addrs[i]); + } + odpdrv_shm_pool_print("slab test: after 1/3 free:", pool); + + /* realloc them:*/ + for (i = 0; i < TEST_SZ; i += 3) { + length = SZ_1K; + addrs[i] = odpdrv_shm_pool_alloc(pool, length); + /* if alloc was success, fill buffer for later check */ + if (addrs[i]) { + for (j = 0; j < length; j++) + addrs[i][j] = (uint8_t)(length & 0xFF); + } + } + odpdrv_shm_pool_print("slab test: after realloc:", pool); + + /* free all (except buff), checking contents: */ + for (i = 0; i < TEST_SZ; i++) { + /* if buffer was allocated, check the pattern in it */ + if (addrs[i]) { + length = SZ_1K; + for (j = 0; j < length; j++) + CU_ASSERT(addrs[i][j] == + (uint8_t)(length & 0xFF)) + } + odpdrv_shm_pool_free(pool, addrs[i]); + } + odpdrv_shm_pool_print("slab test: after all but 1K free:", pool); + + /* check contents of our initial 1K buffer: */ + for (i = 0; i < SZ_1K; i++) + CU_ASSERT((buff[i] == BUFF_PATTERN)) + odpdrv_shm_pool_free(pool, buff); + + odpdrv_shm_pool_print("slab test: after all free", pool); + + /* destroy pool: */ + odpdrv_shm_pool_destroy(pool); +} + odp_testinfo_t drvshmem_suite[] = { ODP_TEST_INFO(drvshmem_test_basic), ODP_TEST_INFO(drvshmem_test_reserve_after_fork), ODP_TEST_INFO(drvshmem_test_singleva_after_fork), ODP_TEST_INFO(drvshmem_test_stress), ODP_TEST_INFO(drvshmem_test_buddy_basic), + ODP_TEST_INFO(drvshmem_test_slab_basic), ODP_TEST_INFO_NULL, }; diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.h b/test/common_plat/validation/drv/drvshmem/drvshmem.h index ab45f7c..fdc1080 100644 --- a/test/common_plat/validation/drv/drvshmem/drvshmem.h +++ b/test/common_plat/validation/drv/drvshmem/drvshmem.h @@ -15,6 +15,7 @@ void drvshmem_test_reserve_after_fork(void); void drvshmem_test_singleva_after_fork(void); void drvshmem_test_stress(void); void drvshmem_test_buddy_basic(void); +void drvshmem_test_slab_basic(void); /* test arrays: */ extern odp_testinfo_t drvshmem_suite[]; From patchwork Tue Jan 17 18:18:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 91712 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp600472qgi; Tue, 17 Jan 2017 09:24:21 -0800 (PST) X-Received: by 10.55.111.2 with SMTP id k2mr40269272qkc.138.1484673861453; Tue, 17 Jan 2017 09:24:21 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id t26si17053118qkt.266.2017.01.17.09.24.21; Tue, 17 Jan 2017 09:24:21 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 1F4EE609D9; Tue, 17 Jan 2017 17:24:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 20CA160EE1; Tue, 17 Jan 2017 17:19:24 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id CA884609D9; Tue, 17 Jan 2017 17:18:48 +0000 (UTC) Received: from mail-lf0-f48.google.com (mail-lf0-f48.google.com [209.85.215.48]) by lists.linaro.org (Postfix) with ESMTPS id 2D38C609E3 for ; Tue, 17 Jan 2017 17:18:46 +0000 (UTC) Received: by mail-lf0-f48.google.com with SMTP id k86so113543040lfi.0 for ; Tue, 17 Jan 2017 09:18:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tNqQ8XCNG7TTdJUM71yZ5raPSQNfjlihz9pnrXQ20V4=; b=Rm6jbxrDbGJwutbIvdX6s1BU69kZ2Is1t+de//fSretGTZOXHRRQhTpVTQEPCbENwf ezYT9Vuyr8Li+y1ApQvqw4IpKmpL+CWYBFHB3c8Ff1NVe2t7ozoav+ZkXLxO/zKD36kE PgBdYyqeHZuu8Uf9oJtpx5NoZP0eY8IL1Bnq7SmnTHdUZ6cd95zmlol4wCPrWRdCAaXM HNC/3XohP9qcaxXGFZmNU/0soHal7QdQLmHn1Nana5AGtKbREULNxgjtRn8M+zlQjkRP gPw5xofKtSiU7nRGPq30jH5EueiFPQBjmOFOAhefyErXexqfq4qaP58DxtObQ87eT5lS gw9w== X-Gm-Message-State: AIkVDXIbMUp1WGOs+kXe0+INd/wZshD32X6qPTo8+O9CZ1RAI0aDfTf+BDH2eMXY6VoatS7LzzM= X-Received: by 10.46.6.17 with SMTP id 17mr16435011ljg.14.1484673524869; Tue, 17 Jan 2017 09:18:44 -0800 (PST) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-76-66.cust.bredband2.com. [83.233.76.66]) by smtp.gmail.com with ESMTPSA id 14sm9365278lju.16.2017.01.17.09.18.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Jan 2017 09:18:44 -0800 (PST) From: Christophe Milard To: mike.holmes@linaro.org, bill.fischofer@linaro.org, yi.he@linaro.org, forrest.shi@linaro.org, maxim.uvarov@linaro.com, lng-odp@lists.linaro.org Date: Tue, 17 Jan 2017 19:18:03 +0100 Message-Id: <1484677083-12831-7-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> References: <1484677083-12831-1-git-send-email-christophe.milard@linaro.org> Subject: [lng-odp] [API-NEXT PATCHv8 6/6] test: drv: shm: adding buddy allocation stress tests X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Stress tests for the random size allocator (buddy allocator in linux-generic) are added here. Signed-off-by: Christophe Milard --- .../common_plat/validation/drv/drvshmem/drvshmem.c | 177 +++++++++++++++++++++ .../common_plat/validation/drv/drvshmem/drvshmem.h | 1 + 2 files changed, 178 insertions(+) -- 2.7.4 diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.c b/test/common_plat/validation/drv/drvshmem/drvshmem.c index d4dedea..0f882ae 100644 --- a/test/common_plat/validation/drv/drvshmem/drvshmem.c +++ b/test/common_plat/validation/drv/drvshmem/drvshmem.c @@ -938,6 +938,182 @@ void drvshmem_test_slab_basic(void) odpdrv_shm_pool_destroy(pool); } +/* + * thread part for the drvshmem_test_buddy_stress + */ +static int run_test_buddy_stress(void *arg ODP_UNUSED) +{ + odpdrv_shm_t shm; + odpdrv_shm_pool_t pool; + uint8_t *address; + shared_test_data_t *glob_data; + uint8_t random_bytes[STRESS_RANDOM_SZ]; + uint32_t index; + uint32_t size; + uint8_t data; + uint32_t iter; + uint32_t i; + + shm = odpdrv_shm_lookup_by_name(MEM_NAME); + glob_data = odpdrv_shm_addr(shm); + CU_ASSERT_PTR_NOT_NULL(glob_data); + + /* get the pool to test */ + pool = odpdrv_shm_pool_lookup(POOL_NAME); + + /* wait for general GO! */ + odpdrv_barrier_wait(&glob_data->test_barrier1); + /* + + * at each iteration: pick up a random index for + * glob_data->stress[index]: If the entry is free, allocated small mem + * randomly. If it is already allocated, make checks and free it: + * Note that different tread can allocate or free a given block + */ + for (iter = 0; iter < STRESS_ITERATION; iter++) { + /* get 4 random bytes from which index, size ,align, flags + * and data will be derived: + */ + odp_random_data(random_bytes, STRESS_RANDOM_SZ, 0); + index = random_bytes[0] & (STRESS_SIZE - 1); + + odp_spinlock_lock(&glob_data->stress_lock); + + switch (glob_data->stress[index].state) { + case STRESS_FREE: + /* allocated a new block for this entry */ + + glob_data->stress[index].state = STRESS_BUSY; + odp_spinlock_unlock(&glob_data->stress_lock); + + size = (random_bytes[1] + 1) << 4; /* up to 4Kb */ + data = random_bytes[2]; + + address = odpdrv_shm_pool_alloc(pool, size); + glob_data->stress[index].address = address; + if (address == NULL) { /* out of mem ? */ + odp_spinlock_lock(&glob_data->stress_lock); + glob_data->stress[index].state = STRESS_ALLOC; + odp_spinlock_unlock(&glob_data->stress_lock); + continue; + } + + glob_data->stress[index].size = size; + glob_data->stress[index].data_val = data; + + /* write some data: */ + for (i = 0; i < size; i++) + address[i] = (data++) & 0xFF; + odp_spinlock_lock(&glob_data->stress_lock); + glob_data->stress[index].state = STRESS_ALLOC; + odp_spinlock_unlock(&glob_data->stress_lock); + + break; + + case STRESS_ALLOC: + /* free the block for this entry */ + + glob_data->stress[index].state = STRESS_BUSY; + odp_spinlock_unlock(&glob_data->stress_lock); + address = glob_data->stress[index].address; + + if (shm == NULL) { /* out of mem ? */ + odp_spinlock_lock(&glob_data->stress_lock); + glob_data->stress[index].state = STRESS_FREE; + odp_spinlock_unlock(&glob_data->stress_lock); + continue; + } + + /* check that data is reachable and correct: */ + data = glob_data->stress[index].data_val; + size = glob_data->stress[index].size; + for (i = 0; i < size; i++) { + CU_ASSERT(address[i] == (data & 0xFF)); + data++; + } + + odpdrv_shm_pool_free(pool, address); + + odp_spinlock_lock(&glob_data->stress_lock); + glob_data->stress[index].state = STRESS_FREE; + odp_spinlock_unlock(&glob_data->stress_lock); + + break; + + case STRESS_BUSY: + default: + odp_spinlock_unlock(&glob_data->stress_lock); + break; + } + } + + fflush(stdout); + return CU_get_number_of_failures(); +} + +/* + * stress tests + */ +void drvshmem_test_buddy_stress(void) +{ + odpdrv_shm_pool_param_t pool_params; + odpdrv_shm_pool_t pool; + pthrd_arg thrdarg; + odpdrv_shm_t shm; + shared_test_data_t *glob_data; + odp_cpumask_t unused; + uint32_t i; + uint8_t *address; + + /* create a pool and check that it can be looked up */ + pool_params.pool_size = POOL_SZ; + pool_params.min_alloc = 0; + pool_params.max_alloc = POOL_SZ; + pool = odpdrv_shm_pool_create(POOL_NAME, &pool_params); + odpdrv_shm_pool_print("Stress test start", pool); + + shm = odpdrv_shm_reserve(MEM_NAME, sizeof(shared_test_data_t), + 0, ODPDRV_SHM_LOCK); + CU_ASSERT(ODPDRV_SHM_INVALID != shm); + glob_data = odpdrv_shm_addr(shm); + CU_ASSERT_PTR_NOT_NULL(glob_data); + + thrdarg.numthrds = odp_cpumask_default_worker(&unused, 0); + if (thrdarg.numthrds > MAX_WORKERS) + thrdarg.numthrds = MAX_WORKERS; + + glob_data->nb_threads = thrdarg.numthrds; + odpdrv_barrier_init(&glob_data->test_barrier1, thrdarg.numthrds); + odp_spinlock_init(&glob_data->stress_lock); + + /* before starting the threads, mark all entries as free: */ + for (i = 0; i < STRESS_SIZE; i++) + glob_data->stress[i].state = STRESS_FREE; + + /* create threads */ + odp_cunit_thread_create(run_test_buddy_stress, &thrdarg); + + /* wait for all thread endings: */ + CU_ASSERT(odp_cunit_thread_exit(&thrdarg) >= 0); + + odpdrv_shm_pool_print("Stress test all thread finished", pool); + + /* release left overs: */ + for (i = 0; i < STRESS_SIZE; i++) { + address = glob_data->stress[i].address; + if (glob_data->stress[i].state == STRESS_ALLOC) + odpdrv_shm_pool_free(pool, address); + } + + CU_ASSERT(0 == odpdrv_shm_free_by_name(MEM_NAME)); + + /* check that no memory is left over: */ + odpdrv_shm_pool_print("Stress test all released", pool); + + /* destroy pool: */ + odpdrv_shm_pool_destroy(pool); +} + odp_testinfo_t drvshmem_suite[] = { ODP_TEST_INFO(drvshmem_test_basic), ODP_TEST_INFO(drvshmem_test_reserve_after_fork), @@ -945,6 +1121,7 @@ odp_testinfo_t drvshmem_suite[] = { ODP_TEST_INFO(drvshmem_test_stress), ODP_TEST_INFO(drvshmem_test_buddy_basic), ODP_TEST_INFO(drvshmem_test_slab_basic), + ODP_TEST_INFO(drvshmem_test_buddy_stress), ODP_TEST_INFO_NULL, }; diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.h b/test/common_plat/validation/drv/drvshmem/drvshmem.h index fdc1080..817b3d5 100644 --- a/test/common_plat/validation/drv/drvshmem/drvshmem.h +++ b/test/common_plat/validation/drv/drvshmem/drvshmem.h @@ -16,6 +16,7 @@ void drvshmem_test_singleva_after_fork(void); void drvshmem_test_stress(void); void drvshmem_test_buddy_basic(void); void drvshmem_test_slab_basic(void); +void drvshmem_test_buddy_stress(void); /* test arrays: */ extern odp_testinfo_t drvshmem_suite[];