From: Intel Date: Wed, 19 Dec 2012 23:00:00 +0000 (+0100) Subject: memory: malloc now supports multi process X-Git-Tag: spdx-start~11382 X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=766b12e5384857599b35015b54a47a4ad675e818;p=dpdk.git memory: malloc now supports multi process Signed-off-by: Intel --- diff --git a/lib/librte_eal/common/include/rte_eal_memconfig.h b/lib/librte_eal/common/include/rte_eal_memconfig.h index adca8fd060..4ce7811dd7 100644 --- a/lib/librte_eal/common/include/rte_eal_memconfig.h +++ b/lib/librte_eal/common/include/rte_eal_memconfig.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #ifdef __cplusplus @@ -86,6 +87,9 @@ struct rte_mem_config { struct rte_memseg free_memseg[RTE_MAX_MEMSEG]; struct rte_tailq_head tailq_head[RTE_MAX_TAILQ]; /**< Tailqs for objects */ + + /* Heaps of Malloc per socket */ + struct malloc_heap malloc_heaps[RTE_MAX_NUMA_NODES]; } __attribute__((__packed__)); diff --git a/lib/librte_eal/common/include/rte_malloc_heap.h b/lib/librte_eal/common/include/rte_malloc_heap.h index 3389f370ab..d5ea63ee04 100644 --- a/lib/librte_eal/common/include/rte_malloc_heap.h +++ b/lib/librte_eal/common/include/rte_malloc_heap.h @@ -36,9 +36,11 @@ #define _RTE_MALLOC_HEAP_H_ #include +#include enum heap_state { NOT_INITIALISED = 0, + INITIALISING, INITIALISED }; diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c index 759f198f7c..74a6681a36 100644 --- a/lib/librte_eal/linuxapp/eal/eal.c +++ b/lib/librte_eal/linuxapp/eal/eal.c @@ -68,6 +68,7 @@ #include #include #include +#include #include "eal_private.h" #include "eal_thread.h" diff --git a/lib/librte_malloc/malloc_heap.c b/lib/librte_malloc/malloc_heap.c index 9c75d26453..0b350748bb 100644 --- a/lib/librte_malloc/malloc_heap.c +++ b/lib/librte_malloc/malloc_heap.c @@ -43,12 +43,15 @@ #include #include #include +#include #include #include #include #include #include #include +#include +#include #include "malloc_elem.h" #include "malloc_heap.h" @@ -114,16 +117,25 @@ malloc_heap_add_memzone(struct malloc_heap *heap, size_t size, unsigned align) static void malloc_heap_init(struct malloc_heap *heap) { - static rte_spinlock_t init_lock = RTE_SPINLOCK_INITIALIZER; - rte_spinlock_lock(&init_lock); - if (!heap->initialised) { - heap->free_head = NULL; - heap->mz_count = 0; - heap->numa_socket = malloc_get_numa_socket(); - rte_spinlock_init(&heap->lock); - heap->initialised = INITIALISED; + struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config; + + rte_eal_mcfg_wait_complete(mcfg); + while (heap->initialised != INITIALISED) { + if (rte_atomic32_cmpset( + (volatile uint32_t*)&heap->initialised, + NOT_INITIALISED, INITIALISING)) { + + heap->free_head = NULL; + heap->mz_count = 0; + /* + * Find NUMA socket of heap that is being initialised, so that + * malloc_heaps[n].numa_socket == n + */ + heap->numa_socket = heap - mcfg->malloc_heaps; + rte_spinlock_init(&heap->lock); + heap->initialised = INITIALISED; + } } - rte_spinlock_unlock(&init_lock); } /* diff --git a/lib/librte_malloc/rte_malloc.c b/lib/librte_malloc/rte_malloc.c index 7bf633d201..a1664fecfc 100644 --- a/lib/librte_malloc/rte_malloc.c +++ b/lib/librte_malloc/rte_malloc.c @@ -54,10 +54,8 @@ #include #include "malloc_elem.h" #include "malloc_heap.h" +#include "malloc_heap.c" -static struct malloc_heap malloc_heap[RTE_MAX_NUMA_NODES] = { - { .initialised = NOT_INITIALISED } -}; /* Free the memory space back to heap */ void rte_free(void *addr) @@ -73,6 +71,8 @@ void rte_free(void *addr) void * rte_malloc_socket(const char *type, size_t size, unsigned align, int socket) { + struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config; + /* return NULL if size is 0 or alignment is not power-of-2 */ if (size == 0 || !rte_is_power_of_2(align)) return NULL; @@ -84,7 +84,7 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket) if (socket >= RTE_MAX_NUMA_NODES) return NULL; - return malloc_heap_alloc(&malloc_heaps[socket], type, + return malloc_heap_alloc(&mcfg->malloc_heaps[socket], type, size, align == 0 ? 1 : align); }