doc: add Meson coding style to contributors guide
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
index 28f39e2..7c21aa9 100644 (file)
 #include <rte_memory.h>
 #include <rte_memzone.h>
 #include <rte_eal.h>
-#include <rte_eal_memconfig.h>
 #include <rte_per_lcore.h>
 #include <rte_errno.h>
 #include <rte_string_fns.h>
 #include <rte_common.h>
+#include <rte_eal_trace.h>
 
 #include "malloc_heap.h"
 #include "malloc_elem.h"
 #include "eal_private.h"
+#include "eal_memcfg.h"
 
 static inline const struct rte_memzone *
 memzone_lookup_thread_unsafe(const char *name)
@@ -52,38 +53,6 @@ memzone_lookup_thread_unsafe(const char *name)
        return NULL;
 }
 
-
-/* This function will return the greatest free block if a heap has been
- * specified. If no heap has been specified, it will return the heap and
- * length of the greatest free block available in all heaps */
-static size_t
-find_heap_max_free_elem(int *s, unsigned align)
-{
-       struct rte_mem_config *mcfg;
-       struct rte_malloc_socket_stats stats;
-       int i, socket = *s;
-       size_t len = 0;
-
-       /* get pointer to global configuration */
-       mcfg = rte_eal_get_configuration()->mem_config;
-
-       for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
-               if ((socket != SOCKET_ID_ANY) && (socket != i))
-                       continue;
-
-               malloc_heap_get_stats(&mcfg->malloc_heaps[i], &stats);
-               if (stats.greatest_free_size > len) {
-                       len = stats.greatest_free_size;
-                       *s = i;
-               }
-       }
-
-       if (len < MALLOC_ELEM_OVERHEAD + align)
-               return 0;
-
-       return len - MALLOC_ELEM_OVERHEAD - align;
-}
-
 static const struct rte_memzone *
 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                int socket_id, unsigned int flags, unsigned int align,
@@ -92,6 +61,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
        struct rte_memzone *mz;
        struct rte_mem_config *mcfg;
        struct rte_fbarray *arr;
+       void *mz_addr;
        size_t requested_len;
        int mz_idx;
        bool contig;
@@ -102,7 +72,9 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
 
        /* no more room in config */
        if (arr->count >= arr->len) {
-               RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
+               RTE_LOG(ERR, EAL,
+               "%s(): Number of requested memzone segments exceeds RTE_MAX_MEMZONE\n",
+                       __func__);
                rte_errno = ENOSPC;
                return NULL;
        }
@@ -140,8 +112,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                return NULL;
        }
 
-       len += RTE_CACHE_LINE_MASK;
-       len &= ~((size_t) RTE_CACHE_LINE_MASK);
+       len = RTE_ALIGN_CEIL(len, RTE_CACHE_LINE_SIZE);
 
        /* save minimal requested  length */
        requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
@@ -152,40 +123,33 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                return NULL;
        }
 
-       if ((socket_id != SOCKET_ID_ANY) &&
-           (socket_id >= RTE_MAX_NUMA_NODES || socket_id < 0)) {
+       if ((socket_id != SOCKET_ID_ANY) && socket_id < 0) {
                rte_errno = EINVAL;
                return NULL;
        }
 
-       if (!rte_eal_has_hugepages())
+       /* only set socket to SOCKET_ID_ANY if we aren't allocating for an
+        * external heap.
+        */
+       if (!rte_eal_has_hugepages() && socket_id < RTE_MAX_NUMA_NODES)
                socket_id = SOCKET_ID_ANY;
 
        contig = (flags & RTE_MEMZONE_IOVA_CONTIG) != 0;
        /* malloc only cares about size flags, remove contig flag from flags */
        flags &= ~RTE_MEMZONE_IOVA_CONTIG;
 
-       if (len == 0) {
-               /* len == 0 is only allowed for non-contiguous zones */
-               if (contig) {
-                       RTE_LOG(DEBUG, EAL, "Reserving zero-length contiguous memzones is not supported\n");
-                       rte_errno = EINVAL;
-                       return NULL;
-               }
-               if (bound != 0)
+       if (len == 0 && bound == 0) {
+               /* no size constraints were placed, so use malloc elem len */
+               requested_len = 0;
+               mz_addr = malloc_heap_alloc_biggest(NULL, socket_id, flags,
+                               align, contig);
+       } else {
+               if (len == 0)
                        requested_len = bound;
-               else {
-                       requested_len = find_heap_max_free_elem(&socket_id, align);
-                       if (requested_len == 0) {
-                               rte_errno = ENOMEM;
-                               return NULL;
-                       }
-               }
+               /* allocate memory on heap */
+               mz_addr = malloc_heap_alloc(NULL, requested_len, socket_id,
+                               flags, align, bound, contig);
        }
-
-       /* allocate memory on heap */
-       void *mz_addr = malloc_heap_alloc(NULL, requested_len, socket_id, flags,
-                       align, bound, contig);
        if (mz_addr == NULL) {
                rte_errno = ENOMEM;
                return NULL;
@@ -210,10 +174,12 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
                return NULL;
        }
 
-       snprintf(mz->name, sizeof(mz->name), "%s", name);
+       strlcpy(mz->name, name, sizeof(mz->name));
        mz->iova = rte_malloc_virt2iova(mz_addr);
        mz->addr = mz_addr;
-       mz->len = (requested_len == 0 ? elem->size : requested_len);
+       mz->len = requested_len == 0 ?
+                       elem->size - elem->pad - MALLOC_ELEM_OVERHEAD :
+                       requested_len;
        mz->hugepage_sz = elem->msl->page_sz;
        mz->socket_id = elem->msl->socket_id;
        mz->flags = 0;
@@ -236,6 +202,9 @@ rte_memzone_reserve_thread_safe(const char *name, size_t len, int socket_id,
        mz = memzone_reserve_aligned_thread_unsafe(
                name, len, socket_id, flags, align, bound);
 
+       rte_eal_trace_memzone_reserve(name, len, socket_id, flags, align,
+               bound, mz);
+
        rte_rwlock_write_unlock(&mcfg->mlock);
 
        return mz;
@@ -281,6 +250,7 @@ rte_memzone_reserve(const char *name, size_t len, int socket_id,
 int
 rte_memzone_free(const struct rte_memzone *mz)
 {
+       char name[RTE_MEMZONE_NAMESIZE];
        struct rte_mem_config *mcfg;
        struct rte_fbarray *arr;
        struct rte_memzone *found_mz;
@@ -291,6 +261,7 @@ rte_memzone_free(const struct rte_memzone *mz)
        if (mz == NULL)
                return -EINVAL;
 
+       rte_strlcpy(name, mz->name, RTE_MEMZONE_NAMESIZE);
        mcfg = rte_eal_get_configuration()->mem_config;
        arr = &mcfg->memzones;
 
@@ -315,6 +286,7 @@ rte_memzone_free(const struct rte_memzone *mz)
        if (addr != NULL)
                rte_free(addr);
 
+       rte_eal_trace_memzone_free(name, addr, ret);
        return ret;
 }
 
@@ -335,6 +307,7 @@ rte_memzone_lookup(const char *name)
 
        rte_rwlock_read_unlock(&mcfg->mlock);
 
+       rte_eal_trace_memzone_lookup(name, memzone);
        return memzone;
 }
 
@@ -402,6 +375,7 @@ int
 rte_eal_memzone_init(void)
 {
        struct rte_mem_config *mcfg;
+       int ret = 0;
 
        /* get pointer to global configuration */
        mcfg = rte_eal_get_configuration()->mem_config;
@@ -412,17 +386,16 @@ rte_eal_memzone_init(void)
                        rte_fbarray_init(&mcfg->memzones, "memzone",
                        RTE_MAX_MEMZONE, sizeof(struct rte_memzone))) {
                RTE_LOG(ERR, EAL, "Cannot allocate memzone list\n");
-               return -1;
+               ret = -1;
        } else if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
                        rte_fbarray_attach(&mcfg->memzones)) {
                RTE_LOG(ERR, EAL, "Cannot attach to memzone list\n");
-               rte_rwlock_write_unlock(&mcfg->mlock);
-               return -1;
+               ret = -1;
        }
 
        rte_rwlock_write_unlock(&mcfg->mlock);
 
-       return 0;
+       return ret;
 }
 
 /* Walk all reserved memory zones */