vppinfra: introduce clib_mem_main

To hold more data later...

Type: improvement
Change-Id: I4006d22dcacd788988c4907f2c263fd4e4a9d398
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlib/cli.c b/src/vlib/cli.c
index 2697c0a..2bdc98c 100644
--- a/src/vlib/cli.c
+++ b/src/vlib/cli.c
@@ -739,6 +739,7 @@
 show_memory_usage (vlib_main_t * vm,
 		   unformat_input_t * input, vlib_cli_command_t * cmd)
 {
+  clib_mem_main_t *mm = &clib_mem_main;
   int verbose __attribute__ ((unused)) = 0;
   int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
   clib_error_t *error;
@@ -826,7 +827,7 @@
         ({
           struct dlmallinfo mi;
           void *mspace;
-          mspace = clib_per_cpu_mheaps[index];
+          mspace = mm->per_cpu_mheaps[index];
 
           mi = mspace_mallinfo (mspace);
           vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
@@ -835,7 +836,7 @@
                            pointer_to_uword (mspace_least_addr(mspace)),
                            mi.arena);
           vlib_cli_output (vm, "  %U\n", format_mheap,
-                           clib_per_cpu_mheaps[index],
+                           mm->per_cpu_mheaps[index],
                            verbose);
           index++;
         }));
@@ -849,17 +850,17 @@
 	struct dlmallinfo mi;
 	void *mspace;
 
-	for (i = 0; i < ARRAY_LEN (clib_per_numa_mheaps); i++)
+	for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
 	  {
-	    if (clib_per_numa_mheaps[i] == 0)
+	    if (mm->per_numa_mheaps[i] == 0)
 	      continue;
-	    if (clib_per_numa_mheaps[i] == clib_per_cpu_mheaps[i])
+	    if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
 	      {
 		vlib_cli_output (vm, "Numa %d uses the main heap...", i);
 		continue;
 	      }
 	    was_enabled = clib_mem_trace_enable_disable (0);
-	    mspace = clib_per_numa_mheaps[i];
+	    mspace = mm->per_numa_mheaps[i];
 
 	    mi = mspace_mallinfo (mspace);
 	    vlib_cli_output (vm, "Numa %d:", i);
@@ -867,7 +868,7 @@
 			     pointer_to_uword (mspace_least_addr (mspace)),
 			     mi.arena);
 	    vlib_cli_output (vm, "  %U\n", format_mheap,
-			     clib_per_numa_mheaps[index], verbose);
+			     mm->per_numa_mheaps[index], verbose);
 	  }
       }
   }
@@ -921,6 +922,7 @@
 			     unformat_input_t * input,
 			     vlib_cli_command_t * cmd)
 {
+  clib_mem_main_t *mm = &clib_mem_main;
   unformat_input_t _line_input, *line_input = &_line_input;
   int enable = 1;
   int api_segment = 0;
@@ -1003,15 +1005,15 @@
 
   if (numa_id != ~0)
     {
-      if (numa_id >= ARRAY_LEN (clib_per_numa_mheaps))
+      if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
 	return clib_error_return (0, "Numa %d out of range", numa_id);
-      if (clib_per_numa_mheaps[numa_id] == 0)
+      if (mm->per_numa_mheaps[numa_id] == 0)
 	return clib_error_return (0, "Numa %d heap not active", numa_id);
 
-      if (clib_per_numa_mheaps[numa_id] == clib_mem_get_heap ())
+      if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
 	return clib_error_return (0, "Numa %d uses the main heap...",
 				  numa_id);
-      current_traced_heap = clib_per_numa_mheaps[numa_id];
+      current_traced_heap = mm->per_numa_mheaps[numa_id];
       oldheap = clib_mem_set_heap (current_traced_heap);
       clib_mem_trace (1);
       clib_mem_set_heap (oldheap);
diff --git a/src/vlib/threads.c b/src/vlib/threads.c
index d5096c8..c2ac4a1 100644
--- a/src/vlib/threads.c
+++ b/src/vlib/threads.c
@@ -626,6 +626,7 @@
 static clib_error_t *
 vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned cpu_id)
 {
+  clib_mem_main_t *mm = &clib_mem_main;
   vlib_thread_main_t *tm = &vlib_thread_main;
   void *(*fp_arg) (void *) = fp;
   void *numa_heap;
@@ -634,19 +635,19 @@
   vlib_get_thread_core_numa (w, cpu_id);
 
   /* Set up NUMA-bound heap if indicated */
-  if (clib_per_numa_mheaps[w->numa_id] == 0)
+  if (mm->per_numa_mheaps[w->numa_id] == 0)
     {
       /* If the user requested a NUMA heap, create it... */
       if (tm->numa_heap_size)
 	{
 	  numa_heap = clib_mem_init_thread_safe_numa
 	    (0 /* DIY */ , tm->numa_heap_size, w->numa_id);
-	  clib_per_numa_mheaps[w->numa_id] = numa_heap;
+	  mm->per_numa_mheaps[w->numa_id] = numa_heap;
 	}
       else
 	{
 	  /* Or, use the main heap */
-	  clib_per_numa_mheaps[w->numa_id] = w->thread_mheap;
+	  mm->per_numa_mheaps[w->numa_id] = w->thread_mheap;
 	}
     }
 
diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt
index 8648275..9e8bc3c 100644
--- a/src/vppinfra/CMakeLists.txt
+++ b/src/vppinfra/CMakeLists.txt
@@ -55,6 +55,7 @@
   longjmp.S
   macros.c
   maplog.c
+  mem.c
   mem_dlmalloc.c
   mhash.c
   mpcap.c
diff --git a/src/vppinfra/mem.c b/src/vppinfra/mem.c
new file mode 100644
index 0000000..3477e5f
--- /dev/null
+++ b/src/vppinfra/mem.c
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <vppinfra/clib.h>
+#include <vppinfra/mem.h>
+#include <vppinfra/time.h>
+#include <vppinfra/format.h>
+#include <vppinfra/clib_error.h>
+
+clib_mem_main_t clib_mem_main;
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vppinfra/mem.h b/src/vppinfra/mem.h
index 31c5fd8..9909726 100644
--- a/src/vppinfra/mem.h
+++ b/src/vppinfra/mem.h
@@ -71,42 +71,49 @@
   CLIB_MEM_PAGE_SZ_16G = 34,
 } clib_mem_page_sz_t;
 
+typedef struct
+{
+  /* per CPU heaps */
+  void *per_cpu_mheaps[CLIB_MAX_MHEAPS];
+
+  /* per NUMA heaps */
+  void *per_numa_mheaps[CLIB_MAX_NUMAS];
+} clib_mem_main_t;
+
+extern clib_mem_main_t clib_mem_main;
+
 /* Unspecified NUMA socket */
 #define VEC_NUMA_UNSPECIFIED (0xFF)
 
-/* Per CPU heaps. */
-extern void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
-extern void *clib_per_numa_mheaps[CLIB_MAX_NUMAS];
-
 always_inline void *
 clib_mem_get_per_cpu_heap (void)
 {
   int cpu = os_get_thread_index ();
-  return clib_per_cpu_mheaps[cpu];
+  return clib_mem_main.per_cpu_mheaps[cpu];
 }
 
 always_inline void *
 clib_mem_set_per_cpu_heap (u8 * new_heap)
 {
   int cpu = os_get_thread_index ();
-  void *old = clib_per_cpu_mheaps[cpu];
-  clib_per_cpu_mheaps[cpu] = new_heap;
+  void *old = clib_mem_main.per_cpu_mheaps[cpu];
+  clib_mem_main.per_cpu_mheaps[cpu] = new_heap;
   return old;
 }
 
 always_inline void *
 clib_mem_get_per_numa_heap (u32 numa_id)
 {
-  ASSERT (numa_id < ARRAY_LEN (clib_per_numa_mheaps));
-  return clib_per_numa_mheaps[numa_id];
+  ASSERT (numa_id < ARRAY_LEN (clib_mem_main.per_numa_mheaps));
+  return clib_mem_main.per_numa_mheaps[numa_id];
 }
 
 always_inline void *
 clib_mem_set_per_numa_heap (u8 * new_heap)
 {
   int numa = os_get_numa_index ();
-  void *old = clib_per_numa_mheaps[numa];
-  clib_per_numa_mheaps[numa] = new_heap;
+  void *old = clib_mem_main.per_numa_mheaps[numa];
+  clib_mem_main.per_numa_mheaps[numa] = new_heap;
   return old;
 }
 
@@ -121,9 +128,9 @@
   int i;
   if (__os_thread_index != 0)
     return;
-  for (i = 0; i < ARRAY_LEN (clib_per_cpu_mheaps); i++)
-    if (clib_atomic_bool_cmp_and_swap (&clib_per_cpu_mheaps[i],
-				       0, clib_per_cpu_mheaps[0]))
+  for (i = 0; i < ARRAY_LEN (clib_mem_main.per_cpu_mheaps); i++)
+    if (clib_atomic_bool_cmp_and_swap (&clib_mem_main.per_cpu_mheaps[i],
+				       0, clib_mem_main.per_cpu_mheaps[0]))
       {
 	os_set_thread_index (i);
 	break;
@@ -154,7 +161,7 @@
     }
 
   cpu = os_get_thread_index ();
-  heap = clib_per_cpu_mheaps[cpu];
+  heap = clib_mem_main.per_cpu_mheaps[cpu];
 
   p = mspace_get_aligned (heap, size, align, align_offset);
 
diff --git a/src/vppinfra/mem_dlmalloc.c b/src/vppinfra/mem_dlmalloc.c
index 1b0dbb2..2cd924a 100644
--- a/src/vppinfra/mem_dlmalloc.c
+++ b/src/vppinfra/mem_dlmalloc.c
@@ -21,9 +21,6 @@
 #include <vppinfra/elf_clib.h>
 #include <vppinfra/sanitizer.h>
 
-void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
-void *clib_per_numa_mheaps[CLIB_MAX_NUMAS];
-
 typedef struct
 {
   /* Address of callers: outer first, inner last. */