malloc: add function to check if socket is external
[dpdk.git] / lib / librte_eal / common / rte_malloc.c
index 47ca5a7..fa81d78 100644 (file)
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <sys/queue.h>
 
+#include <rte_errno.h>
 #include <rte_memcpy.h>
 #include <rte_memory.h>
 #include <rte_eal.h>
@@ -47,10 +48,6 @@ rte_malloc_socket(const char *type, size_t size, unsigned int align,
        if (!rte_eal_has_hugepages())
                socket_arg = SOCKET_ID_ANY;
 
-       /* Check socket parameter */
-       if (socket_arg >= RTE_MAX_NUMA_NODES)
-               return NULL;
-
        return malloc_heap_alloc(type, size, socket_arg, 0,
                        align == 0 ? 1 : align, 0, false);
 }
@@ -152,11 +149,20 @@ rte_malloc_get_socket_stats(int socket,
                struct rte_malloc_socket_stats *socket_stats)
 {
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       int heap_idx, ret = -1;
 
-       if (socket >= RTE_MAX_NUMA_NODES || socket < 0)
-               return -1;
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+
+       heap_idx = malloc_socket_to_heap_id(socket);
+       if (heap_idx < 0)
+               goto unlock;
+
+       ret = malloc_heap_get_stats(&mcfg->malloc_heaps[heap_idx],
+                       socket_stats);
+unlock:
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
 
-       return malloc_heap_get_stats(&mcfg->malloc_heaps[socket], socket_stats);
+       return ret;
 }
 
 /*
@@ -168,12 +174,75 @@ rte_malloc_dump_heaps(FILE *f)
        struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
        unsigned int idx;
 
-       for (idx = 0; idx < rte_socket_count(); idx++) {
-               unsigned int socket = rte_socket_id_by_idx(idx);
-               fprintf(f, "Heap on socket %i:\n", socket);
-               malloc_heap_dump(&mcfg->malloc_heaps[socket], f);
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+
+       for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
+               fprintf(f, "Heap id: %u\n", idx);
+               malloc_heap_dump(&mcfg->malloc_heaps[idx], f);
+       }
+
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+}
+
+int
+rte_malloc_heap_get_socket(const char *name)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       struct malloc_heap *heap = NULL;
+       unsigned int idx;
+       int ret;
+
+       if (name == NULL ||
+                       strnlen(name, RTE_HEAP_NAME_MAX_LEN) == 0 ||
+                       strnlen(name, RTE_HEAP_NAME_MAX_LEN) ==
+                               RTE_HEAP_NAME_MAX_LEN) {
+               rte_errno = EINVAL;
+               return -1;
+       }
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
+               struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
+
+               if (!strncmp(name, tmp->name, RTE_HEAP_NAME_MAX_LEN)) {
+                       heap = tmp;
+                       break;
+               }
+       }
+
+       if (heap != NULL) {
+               ret = heap->socket_id;
+       } else {
+               rte_errno = ENOENT;
+               ret = -1;
+       }
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
+
+       return ret;
+}
+
+int
+rte_malloc_heap_socket_is_external(int socket_id)
+{
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       unsigned int idx;
+       int ret = -1;
+
+       if (socket_id == SOCKET_ID_ANY)
+               return 0;
+
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+       for (idx = 0; idx < RTE_MAX_HEAPS; idx++) {
+               struct malloc_heap *tmp = &mcfg->malloc_heaps[idx];
+
+               if ((int)tmp->socket_id == socket_id) {
+                       /* external memory always has large socket ID's */
+                       ret = tmp->socket_id >= RTE_MAX_NUMA_NODES;
+                       break;
+               }
        }
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
 
+       return ret;
 }
 
 /*
@@ -182,14 +251,20 @@ rte_malloc_dump_heaps(FILE *f)
 void
 rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
 {
-       unsigned int socket;
+       struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
+       unsigned int heap_id;
        struct rte_malloc_socket_stats sock_stats;
+
+       rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
+
        /* Iterate through all initialised heaps */
-       for (socket=0; socket< RTE_MAX_NUMA_NODES; socket++) {
-               if ((rte_malloc_get_socket_stats(socket, &sock_stats) < 0))
-                       continue;
+       for (heap_id = 0; heap_id < RTE_MAX_HEAPS; heap_id++) {
+               struct malloc_heap *heap = &mcfg->malloc_heaps[heap_id];
+
+               malloc_heap_get_stats(heap, &sock_stats);
 
-               fprintf(f, "Socket:%u\n", socket);
+               fprintf(f, "Heap id:%u\n", heap_id);
+               fprintf(f, "\tHeap name:%s\n", heap->name);
                fprintf(f, "\tHeap_size:%zu,\n", sock_stats.heap_totalsz_bytes);
                fprintf(f, "\tFree_size:%zu,\n", sock_stats.heap_freesz_bytes);
                fprintf(f, "\tAlloc_size:%zu,\n", sock_stats.heap_allocsz_bytes);
@@ -198,6 +273,7 @@ rte_malloc_dump_stats(FILE *f, __rte_unused const char *type)
                fprintf(f, "\tAlloc_count:%u,\n",sock_stats.alloc_count);
                fprintf(f, "\tFree_count:%u,\n", sock_stats.free_count);
        }
+       rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
        return;
 }