vppinfra: use vm memory allocator for numa mapping

Type: refactor

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I13b239cd572ae6dfaec07019d3d9b7c0ed3edcfa
diff --git a/src/vlib/main.c b/src/vlib/main.c
index 28c8c78..94218d3 100644
--- a/src/vlib/main.c
+++ b/src/vlib/main.c
@@ -1717,6 +1717,7 @@
 
   vm->cpu_id = clib_get_current_cpu_id ();
   vm->numa_node = clib_get_current_numa_node ();
+  os_set_numa_index (vm->numa_node);
 
   /* Start all processes. */
   if (is_main)
diff --git a/src/vlib/threads.c b/src/vlib/threads.c
index 8a06887..d9d7810 100644
--- a/src/vlib/threads.c
+++ b/src/vlib/threads.c
@@ -589,9 +589,8 @@
   p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, cpu_id, 0);
   clib_sysfs_read ((char *) p, "%d", &core_id);
   vec_reset_length (p);
-  p =
-    format (p, "%s%u/topology/physical_package_id%c", sys_cpu_path, cpu_id,
-	    0);
+  p = format (p, "%s%u/topology/physical_package_id%c", sys_cpu_path,
+	      cpu_id, 0);
   clib_sysfs_read ((char *) p, "%d", &numa_id);
   vec_free (p);
 
@@ -608,7 +607,6 @@
 
   w->cpu_id = cpu_id;
   vlib_get_thread_core_numa (w, cpu_id);
-  os_set_numa_index (w->numa_id);
 
   /* Set up NUMA-bound heap if indicated */
   if (clib_per_numa_mheaps[w->numa_id] == 0)
@@ -617,7 +615,7 @@
       if (tm->numa_heap_size)
 	{
 	  numa_heap = clib_mem_init_thread_safe_numa
-	    (0 /* DIY */ , tm->numa_heap_size);
+	    (0 /* DIY */ , tm->numa_heap_size, w->numa_id);
 	  clib_per_numa_mheaps[w->numa_id] = numa_heap;
 	}
       else
diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt
index 60e6eef..1c234cc 100644
--- a/src/vppinfra/CMakeLists.txt
+++ b/src/vppinfra/CMakeLists.txt
@@ -24,13 +24,6 @@
   set(DLMALLOC 0)
 endif(VPP_USE_DLMALLOC)
 
-find_library(NUMA numa)
-if (NUMA)
-  set(NUMA_LIBRARY_FOUND 1)
-else(NUMA)
-  set(NUMA_LIBRARY_FOUND 0)
-endif()
-
 configure_file(
   ${CMAKE_SOURCE_DIR}/vppinfra/config.h.in
   ${CMAKE_BINARY_DIR}/vppinfra/config.h
@@ -217,7 +210,7 @@
 
 add_vpp_library(vppinfra
   SOURCES ${VPPINFRA_SRCS}
-  LINK_LIBRARIES m ${NUMA}
+  LINK_LIBRARIES m
   INSTALL_HEADERS ${VPPINFRA_HEADERS}
   COMPONENT libvppinfra
 )
diff --git a/src/vppinfra/config.h.in b/src/vppinfra/config.h.in
index b236663..a7a22a6 100644
--- a/src/vppinfra/config.h.in
+++ b/src/vppinfra/config.h.in
@@ -21,7 +21,6 @@
 #endif
 
 #define USE_DLMALLOC @DLMALLOC@
-#define HAVE_NUMA_LIBRARY @NUMA_LIBRARY_FOUND@
 
 #define CLIB_TARGET_TRIPLET "@CMAKE_C_COMPILER_TARGET@"
 #endif
diff --git a/src/vppinfra/mem.h b/src/vppinfra/mem.h
index c00c78a..0367c4a 100644
--- a/src/vppinfra/mem.h
+++ b/src/vppinfra/mem.h
@@ -308,7 +308,8 @@
 
 void *clib_mem_init (void *heap, uword size);
 void *clib_mem_init_thread_safe (void *memory, uword memory_size);
-void *clib_mem_init_thread_safe_numa (void *memory, uword memory_size);
+void *clib_mem_init_thread_safe_numa (void *memory, uword memory_size,
+				      u8 numa);
 
 void clib_mem_exit (void);
 
diff --git a/src/vppinfra/mem_dlmalloc.c b/src/vppinfra/mem_dlmalloc.c
index 38226e2..d62bb74 100644
--- a/src/vppinfra/mem_dlmalloc.c
+++ b/src/vppinfra/mem_dlmalloc.c
@@ -243,47 +243,26 @@
 }
 
 void *
-clib_mem_init_thread_safe_numa (void *memory, uword memory_size)
+clib_mem_init_thread_safe_numa (void *memory, uword memory_size, u8 numa)
 {
+  clib_mem_vm_alloc_t alloc = { 0 };
+  clib_error_t *err;
   void *heap;
-  unsigned long this_numa;
 
-  heap =
-    clib_mem_init_internal (memory, memory_size,
-			    0 /* do NOT clib_mem_set_heap */ );
+  alloc.size = memory_size;
+  alloc.flags = CLIB_MEM_VM_F_NUMA_FORCE;
+  alloc.numa_node = numa;
+  if ((err = clib_mem_vm_ext_alloc (&alloc)))
+    {
+      clib_error_report (err);
+      return 0;
+    }
+
+  heap = clib_mem_init_internal (memory, memory_size,
+				 0 /* do NOT clib_mem_set_heap */ );
 
   ASSERT (heap);
 
-  this_numa = os_get_numa_index ();
-
-#if HAVE_NUMA_LIBRARY > 0
-  unsigned long nodemask = 1 << this_numa;
-  void *page_base;
-  unsigned long page_mask;
-  long rv;
-
-  /*
-   * Bind the heap to the current thread's NUMA node.
-   * heap is not naturally page-aligned, so fix it.
-   */
-
-  page_mask = ~(clib_mem_get_page_size () - 1);
-  page_base = (void *) (((unsigned long) heap) & page_mask);
-
-  clib_warning ("Bind heap at %llx size %llx to NUMA numa %d",
-		page_base, memory_size, this_numa);
-
-  rv = mbind (page_base, memory_size, MPOL_BIND /* mode */ ,
-	      &nodemask /* nodemask */ ,
-	      BITS (nodemask) /* max node number */ ,
-	      MPOL_MF_MOVE /* flags */ );
-
-  if (rv < 0)
-    clib_unix_warning ("mbind");
-#else
-  clib_warning ("mbind unavailable, can't bind to numa %d", this_numa);
-#endif
-
   return heap;
 }