memory: malloc now supports multi process
authorIntel <intel.com>
Wed, 19 Dec 2012 23:00:00 +0000 (00:00 +0100)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Thu, 25 Jul 2013 13:23:27 +0000 (15:23 +0200)
Signed-off-by: Intel
lib/librte_eal/common/include/rte_eal_memconfig.h
lib/librte_eal/common/include/rte_malloc_heap.h
lib/librte_eal/linuxapp/eal/eal.c
lib/librte_malloc/malloc_heap.c
lib/librte_malloc/rte_malloc.c

index adca8fd..4ce7811 100644 (file)
@@ -38,6 +38,7 @@
 #include <rte_tailq.h>
 #include <rte_memory.h>
 #include <rte_memzone.h>
+#include <rte_malloc_heap.h>
 #include <rte_rwlock.h>
 
 #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__));
 
 
index 3389f37..d5ea63e 100644 (file)
 #define _RTE_MALLOC_HEAP_H_
 
 #include <stddef.h>
+#include <rte_spinlock.h>
 
 enum heap_state {
        NOT_INITIALISED = 0,
+       INITIALISING,
        INITIALISED
 };
 
index 759f198..74a6681 100644 (file)
@@ -68,6 +68,7 @@
 #include <rte_common.h>
 #include <rte_version.h>
 #include <rte_atomic.h>
+#include <malloc_heap.h>
 
 #include "eal_private.h"
 #include "eal_thread.h"
index 9c75d26..0b35074 100644 (file)
 #include <rte_memzone.h>
 #include <rte_tailq.h>
 #include <rte_eal.h>
+#include <rte_eal_memconfig.h>
 #include <rte_launch.h>
 #include <rte_per_lcore.h>
 #include <rte_lcore.h>
 #include <rte_common.h>
 #include <rte_string_fns.h>
 #include <rte_spinlock.h>
+#include <rte_memcpy.h>
+#include <rte_atomic.h>
 
 #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);
 }
 
 /*
index 7bf633d..a1664fe 100644 (file)
 #include <rte_malloc.h>
 #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);
 }