Add config option to use dlmalloc instead of mheap
[vpp.git] / src / vlib / cli.c
index a85fa93..820fdef 100644 (file)
@@ -38,6 +38,7 @@
  */
 
 #include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
 #include <vppinfra/cpu.h>
 #include <unistd.h>
 #include <ctype.h>
@@ -706,7 +707,7 @@ static clib_error_t *
 show_memory_usage (vlib_main_t * vm,
                   unformat_input_t * input, vlib_cli_command_t * cmd)
 {
-  int verbose = 0, api_segment = 0;
+  int verbose __attribute__ ((unused)) = 0, api_segment = 0;
   clib_error_t *error;
   u32 index = 0;
 
@@ -741,14 +742,55 @@ show_memory_usage (vlib_main_t * vm,
       vec_free (s);
     }
 
+#if USE_DLMALLOC == 0
   /* *INDENT-OFF* */
   foreach_vlib_main (
   ({
-      vlib_cli_output (vm, "Thread %d %v\n", index, vlib_worker_threads[index].name);
-      vlib_cli_output (vm, "%U\n", format_mheap, clib_per_cpu_mheaps[index], verbose);
+      mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
+      vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
+                      vlib_worker_threads[index].name);
+      vlib_cli_output (vm, "  %U\n", format_page_map, pointer_to_uword (h) -
+                      h->vm_alloc_offset_from_header,
+                      h->vm_alloc_size);
+      vlib_cli_output (vm, "  %U\n", format_mheap, clib_per_cpu_mheaps[index],
+                       verbose);
       index++;
   }));
   /* *INDENT-ON* */
+#else
+  {
+    uword clib_mem_trace_enable_disable (uword enable);
+    uword was_enabled;
+
+    /*
+     * Note: the foreach_vlib_main cause allocator traffic,
+     * so shut off tracing before we go there...
+     */
+    was_enabled = clib_mem_trace_enable_disable (0);
+
+    /* *INDENT-OFF* */
+    foreach_vlib_main (
+    ({
+      struct mallinfo mi;
+      void *mspace;
+      mspace = clib_per_cpu_mheaps[index];
+
+      mi = mspace_mallinfo (mspace);
+      vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
+                      vlib_worker_threads[index].name);
+      vlib_cli_output (vm, "  %U\n", format_page_map,
+                       pointer_to_uword (mspace_least_addr(mspace)),
+                       mi.arena);
+      vlib_cli_output (vm, "  %U\n", format_mheap, clib_per_cpu_mheaps[index],
+                       verbose);
+      index++;
+    }));
+    /* *INDENT-ON* */
+
+    /* Restore the trace flag */
+    clib_mem_trace_enable_disable (was_enabled);
+  }
+#endif /* USE_DLMALLOC */
   return 0;
 }
 
@@ -810,7 +852,7 @@ enable_disable_memory_trace (vlib_main_t * vm,
 
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
-      if (!unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
+      if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
        ;
       else if (unformat (line_input, "api-segment"))
        api_segment = 1;
@@ -844,6 +886,7 @@ static clib_error_t *
 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
                    vlib_cli_command_t * cmd)
 {
+#if USE_DLMALLOC == 0
   clib_error_t *error = 0;
   void *heap;
   mheap_t *mheap;
@@ -891,6 +934,9 @@ test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
     }
 
   return error;
+#else
+  return clib_error_return (0, "unimplemented...");
+#endif /* USE_DLMALLOC */
 }
 
 /* *INDENT-OFF* */
@@ -905,9 +951,23 @@ static clib_error_t *
 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
                vlib_cli_command_t * cmd)
 {
-  char *newenviron[] = { NULL };
+  clib_file_main_t *fm = &file_main;
+  clib_file_t *f;
+
+  /* environ(7) does not indicate a header for this */
+  extern char **environ;
+
+  /* Close all known open files */
+  /* *INDENT-OFF* */
+  pool_foreach(f, fm->file_pool,
+    ({
+      if (f->file_descriptor > 2)
+        close(f->file_descriptor);
+    }));
+  /* *INDENT-ON* */
 
-  execve (vm->name, (char **) vm->argv, newenviron);
+  /* Exec ourself */
+  execve (vm->name, (char **) vm->argv, environ);
 
   return 0;
 }