vppinfra: support main heap with different page sizes
[vpp.git] / src / vlib / cli.c
index 2697c0a..223a3b7 100644 (file)
@@ -739,8 +739,10 @@ static clib_error_t *
 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;
+  int map = 0;
   clib_error_t *error;
   u32 index = 0;
   int i;
@@ -760,6 +762,8 @@ show_memory_usage (vlib_main_t * vm,
        main_heap = 1;
       else if (unformat (input, "numa-heaps"))
        numa_heaps = 1;
+      else if (unformat (input, "map"))
+       map = 1;
       else
        {
          error = clib_error_return (0, "unknown input `%U'",
@@ -768,9 +772,10 @@ show_memory_usage (vlib_main_t * vm,
        }
     }
 
-  if ((api_segment + stats_segment + main_heap + numa_heaps) == 0)
+  if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
     return clib_error_return
-      (0, "Need one of api-segment, stats-segment, main-heap or numa-heaps");
+      (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
+       "or map");
 
   if (api_segment)
     {
@@ -826,7 +831,7 @@ show_memory_usage (vlib_main_t * vm,
         ({
           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 +840,7 @@ show_memory_usage (vlib_main_t * vm,
                            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 +854,17 @@ show_memory_usage (vlib_main_t * vm,
        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,9 +872,44 @@ show_memory_usage (vlib_main_t * vm,
                             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);
          }
       }
+    if (map)
+      {
+       clib_mem_page_stats_t stats = { };
+       clib_mem_vm_map_hdr_t *hdr = 0;
+       u8 *s = 0;
+       int numa = -1;
+
+       s = format (s, "\n%-16s%7s%7s%7s",
+                   "StartAddr", "size", "PageSz", "Pages");
+       while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
+         s = format (s, " Numa%u", numa);
+       s = format (s, " NotMap");
+       s = format (s, " Name");
+       vlib_cli_output (vm, "%v", s);
+       vec_reset_length (s);
+
+       while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
+         {
+           clib_mem_get_page_stats ((void *) hdr->base_addr,
+                                    hdr->log2_page_sz, hdr->num_pages,
+                                    &stats);
+           s = format (s, "%016lx%7U%7U%7lu",
+                       hdr->base_addr, format_memory_size,
+                       hdr->num_pages << hdr->log2_page_sz,
+                       format_log2_page_size, hdr->log2_page_sz,
+                       hdr->num_pages);
+           while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
+             s = format (s, "%6lu", stats.per_numa[numa]);
+           s = format (s, "%7lu", stats.not_mapped);
+           s = format (s, " %s", hdr->name);
+           vlib_cli_output (vm, "%v", s);
+           vec_reset_length (s);
+         }
+       vec_free (s);
+      }
   }
   return 0;
 }
@@ -878,7 +918,7 @@ show_memory_usage (vlib_main_t * vm,
 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
   .path = "show memory",
   .short_help = "show memory [api-segment][stats-segment][verbose]\n"
-  "            [numa-heaps]",
+  "            [numa-heaps][map]",
   .function = show_memory_usage,
 };
 /* *INDENT-ON* */
@@ -921,6 +961,7 @@ enable_disable_memory_trace (vlib_main_t * vm,
                             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 +1044,15 @@ enable_disable_memory_trace (vlib_main_t * vm,
 
   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);