misc: refactor clib_bitmap_foreach macro
[vpp.git] / src / vlib / cli.c
index 467d1f7..e1db95d 100644 (file)
@@ -39,6 +39,7 @@
 
 #include <vlib/vlib.h>
 #include <vlib/unix/unix.h>
+#include <vppinfra/callback.h>
 #include <vppinfra/cpu.h>
 #include <vppinfra/elog.h>
 #include <unistd.h>
@@ -303,7 +304,7 @@ vlib_cli_get_possible_completions (u8 * str)
    * autocomplete the next level of subcommands */
   help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
   /* *INDENT-OFF* */
-  clib_bitmap_foreach(index, match_bitmap, {
+  clib_bitmap_foreach (index, match_bitmap) {
     if (help_next_level && is_unique) {
        c = get_sub_command (vcm, c, index);
        vec_foreach (sc, c->sub_commands) {
@@ -313,7 +314,7 @@ vlib_cli_get_possible_completions (u8 * str)
     }
     sc = &c->sub_commands[index];
     vec_add1(result, (u8*) sc->name);
-  });
+  }
   /* *INDENT-ON* */
 
 done:
@@ -506,7 +507,7 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
        }
 
       (void) clib_mem_trace_enable_disable (0);
-      leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
+      leak_report = format (0, "%U", format_clib_mem_heap, 0,
                            1 /* verbose, i.e. print leaks */ );
       clib_mem_trace (0);
       vlib_cli_output (vm, "%v", leak_report);
@@ -563,10 +564,16 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
 
              if (!c->is_mp_safe)
                vlib_worker_thread_barrier_sync (vm);
+             if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
+               clib_call_callbacks (cm->perf_counter_cbs, cm,
+                                    c - cm->commands, 0 /* before */ );
 
              c->hit_counter++;
              c_error = c->function (vm, si, c);
 
+             if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
+               clib_call_callbacks (cm->perf_counter_cbs, cm,
+                                    c - cm->commands, 1 /* after */ );
              if (!c->is_mp_safe)
                vlib_worker_thread_barrier_release (vm);
 
@@ -696,6 +703,11 @@ vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
   s = va_format (0, fmt, &va);
   va_end (va);
 
+  /* some format functions might return 0
+   * e.g. show int addr */
+  if (NULL == s)
+    return;
+
   /* Terminate with \n if not present. */
   if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
     vec_add1 (s, '\n');
@@ -732,10 +744,13 @@ 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;
+  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;
   uword clib_mem_trace_enable_disable (uword enable);
   uword was_enabled;
 
@@ -750,6 +765,10 @@ show_memory_usage (vlib_main_t * vm,
        stats_segment = 1;
       else if (unformat (input, "main-heap"))
        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'",
@@ -758,16 +777,16 @@ show_memory_usage (vlib_main_t * vm,
        }
     }
 
-  if ((api_segment + stats_segment + main_heap) == 0)
+  if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
     return clib_error_return
-      (0, "Please supply one of api-segment, stats-segment or main-heap");
+      (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
+       "or map");
 
   if (api_segment)
     {
       void *oldheap = vl_msg_push_heap ();
       was_enabled = clib_mem_trace_enable_disable (0);
-      u8 *s_in_svm =
-       format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
+      u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
       vl_msg_pop_heap (oldheap);
       u8 *s = vec_dup (s_in_svm);
 
@@ -783,8 +802,7 @@ show_memory_usage (vlib_main_t * vm,
     {
       void *oldheap = vlib_stats_push_heap (0);
       was_enabled = clib_mem_trace_enable_disable (0);
-      u8 *s_in_svm =
-       format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
+      u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
       if (oldheap)
        clib_mem_set_heap (oldheap);
       u8 *s = vec_dup (s_in_svm);
@@ -801,22 +819,7 @@ show_memory_usage (vlib_main_t * vm,
       vec_free (s);
     }
 
-#if USE_DLMALLOC == 0
-  /* *INDENT-OFF* */
-  foreach_vlib_main (
-  ({
-      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
+
   {
     if (main_heap)
       {
@@ -829,18 +832,10 @@ show_memory_usage (vlib_main_t * vm,
         /* *INDENT-OFF* */
         foreach_vlib_main (
         ({
-          struct dlmallinfo 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],
+          vlib_cli_output (vm, "  %U\n", format_clib_mem_heap,
+                           mm->per_cpu_mheaps[index],
                            verbose);
           index++;
         }));
@@ -849,15 +844,75 @@ show_memory_usage (vlib_main_t * vm,
        /* Restore the trace flag */
        clib_mem_trace_enable_disable (was_enabled);
       }
+    if (numa_heaps)
+      {
+       for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
+         {
+           if (mm->per_numa_mheaps[i] == 0)
+             continue;
+           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);
+
+           vlib_cli_output (vm, "Numa %d:", i);
+           vlib_cli_output (vm, "  %U\n", format_clib_mem_heap,
+                            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%5s%7s%7s",
+                   "StartAddr", "size", "FD", "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",
+                       hdr->base_addr, format_memory_size,
+                       hdr->num_pages << hdr->log2_page_sz);
+
+           if (hdr->fd != -1)
+             s = format (s, "%5d", hdr->fd);
+           else
+             s = format (s, "%5s", " ");
+
+           s = format (s, "%7U%7lu",
+                       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);
+      }
   }
-#endif /* USE_DLMALLOC */
   return 0;
 }
 
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
   .path = "show memory",
-  .short_help = "show memory [api-segment][stats-segment][verbose]",
+  .short_help = "show memory [api-segment][stats-segment][verbose]\n"
+  "            [numa-heaps][map]",
   .function = show_memory_usage,
 };
 /* *INDENT-ON* */
@@ -900,11 +955,13 @@ 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;
   int stats_segment = 0;
   int main_heap = 0;
+  u32 numa_id = ~0;
   void *oldheap;
 
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -920,6 +977,8 @@ enable_disable_memory_trace (vlib_main_t * vm,
        stats_segment = 1;
       else if (unformat (line_input, "main-heap"))
        main_heap = 1;
+      else if (unformat (line_input, "numa-heap %d", &numa_id))
+       ;
       else
        {
          unformat_free (line_input);
@@ -928,10 +987,12 @@ enable_disable_memory_trace (vlib_main_t * vm,
     }
   unformat_free (line_input);
 
-  if ((api_segment + stats_segment + main_heap + (enable == 0)) == 0)
+  if ((api_segment + stats_segment + main_heap + (enable == 0)
+       + (numa_id != ~0)) == 0)
     {
       return clib_error_return
-       (0, "Need one of main-heap, stats-segment or api-segment");
+       (0, "Need one of main-heap, stats-segment, api-segment,\n"
+        "numa-heap <nn> or disable");
     }
 
   /* Turn off current trace, if any */
@@ -975,80 +1036,32 @@ enable_disable_memory_trace (vlib_main_t * vm,
       clib_mem_trace (main_heap);
     }
 
-  return 0;
-}
-
-/* *INDENT-OFF* */
-VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
-  .path = "memory-trace",
-  .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n",
-  .function = enable_disable_memory_trace,
-};
-/* *INDENT-ON* */
-
-
-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;
-
-  if (unformat (input, "on"))
-    {
-        /* *INDENT-OFF* */
-        foreach_vlib_main({
-          heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
-          mheap = mheap_header(heap);
-          mheap->flags |= MHEAP_FLAG_VALIDATE;
-          // Turn off small object cache because it delays detection of errors
-          mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
-        });
-        /* *INDENT-ON* */
-
-    }
-  else if (unformat (input, "off"))
+  if (numa_id != ~0)
     {
-        /* *INDENT-OFF* */
-        foreach_vlib_main({
-          heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
-          mheap = mheap_header(heap);
-          mheap->flags &= ~MHEAP_FLAG_VALIDATE;
-          mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
-        });
-        /* *INDENT-ON* */
+      if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
+       return clib_error_return (0, "Numa %d out of range", numa_id);
+      if (mm->per_numa_mheaps[numa_id] == 0)
+       return clib_error_return (0, "Numa %d heap not active", numa_id);
+
+      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 = mm->per_numa_mheaps[numa_id];
+      oldheap = clib_mem_set_heap (current_traced_heap);
+      clib_mem_trace (1);
+      clib_mem_set_heap (oldheap);
     }
-  else if (unformat (input, "now"))
-    {
-        /* *INDENT-OFF* */
-        foreach_vlib_main({
-          heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
-          mheap = mheap_header(heap);
-          mheap_validate(heap);
-        });
-        /* *INDENT-ON* */
-      vlib_cli_output (vm, "heap validation complete");
 
-    }
-  else
-    {
-      return clib_error_return (0, "unknown input `%U'",
-                               format_unformat_error, input);
-    }
 
-  return error;
-#else
-  return clib_error_return (0, "unimplemented...");
-#endif /* USE_DLMALLOC */
+  return 0;
 }
 
 /* *INDENT-OFF* */
-VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
-    .path = "test heap-validate",
-    .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
-    .function = test_heap_validate,
+VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
+  .path = "memory-trace",
+  .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
+  "                   [numa-heap <numa-id>]\n",
+  .function = enable_disable_memory_trace,
 };
 /* *INDENT-ON* */
 
@@ -1064,11 +1077,11 @@ restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
 
   /* Close all known open files */
   /* *INDENT-OFF* */
-  pool_foreach(f, fm->file_pool,
-    ({
+  pool_foreach (f, fm->file_pool)
+     {
       if (f->file_descriptor > 2)
         close(f->file_descriptor);
-    }));
+    }
   /* *INDENT-ON* */
 
   /* Exec ourself */
@@ -1217,7 +1230,7 @@ add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
                    vec_len (p->sub_rules));
       vec_add2 (p->sub_rules, sr, 1);
       sr->name = sub_name;
-      sr->rule_index = q[0];
+      sr->rule_index = sr - p->sub_rules;
       sr->command_index = child_index;
       return;
     }
@@ -1464,8 +1477,9 @@ done:
 #endif
 
 static clib_error_t *
-elog_trace_command_fn (vlib_main_t * vm,
-                      unformat_input_t * input, vlib_cli_command_t * cmd)
+event_logger_trace_command_fn (vlib_main_t * vm,
+                              unformat_input_t * input,
+                              vlib_cli_command_t * cmd)
 {
   unformat_input_t _line_input, *line_input = &_line_input;
   int enable = 1;
@@ -1554,21 +1568,20 @@ print_status:
  *
  * @cliexpar
  * @clistart
- * elog trace api cli barrier
- * elog trace api cli barrier disable
- * elog trace dispatch
- * elog trace circuit-node ethernet-input
- * elog trace
+ * event-logger trace api cli barrier
+ * event-logger trace api cli barrier disable
+ * event-logger trace dispatch
+ * event-logger trace circuit-node ethernet-input
  * @cliend
- * @cliexcmd{elog trace [api][cli][barrier][disable]}
+ * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
 ?*/
 /* *INDENT-OFF* */
-VLIB_CLI_COMMAND (elog_trace_command, static) =
+VLIB_CLI_COMMAND (event_logger_trace_command, static) =
 {
-  .path = "elog trace",
-  .short_help = "elog trace [api][cli][barrier][dispatch]\n"
+  .path = "event-logger trace",
+  .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
   "[circuit-node <name> e.g. ethernet-input][disable]",
-  .function = elog_trace_command_fn,
+  .function = event_logger_trace_command_fn,
 };
 /* *INDENT-ON* */