perfmon: collect data on selected thread(s)

Add missing pre-input node runtime fork and refork code.
unix-epoll-input runs on all threads; each instance needs its own
runtime stats.

Change-Id: I16b02e42d0c95f863161176c4bb9f9917bef809d
Signed-off-by: Dave Barach <dave@barachs.net>
diff --git a/src/plugins/perfmon/perfmon.c b/src/plugins/perfmon/perfmon.c
index 3595557..7f621a1 100644
--- a/src/plugins/perfmon/perfmon.c
+++ b/src/plugins/perfmon/perfmon.c
@@ -301,11 +301,15 @@
 		    unformat_input_t * input, vlib_cli_command_t * cmd)
 {
   perfmon_main_t *pm = &perfmon_main;
+  vlib_thread_main_t *vtm = vlib_get_thread_main ();
+  int num_threads = 1 + vtm->n_threads;
   unformat_input_t _line_input, *line_input = &_line_input;
   perfmon_event_config_t ec;
   f64 delay;
   u32 timeout_seconds;
   u32 deadman;
+  int last_set;
+  clib_error_t *error;
 
   vec_reset_length (pm->single_events_to_collect);
   vec_reset_length (pm->paired_events_to_collect);
@@ -315,6 +319,8 @@
   if (!unformat_user (input, unformat_line_input, line_input))
     return clib_error_return (0, "counter names required...");
 
+  clib_bitmap_zero (pm->thread_bitmap);
+
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
       if (unformat (line_input, "timeout %u", &timeout_seconds))
@@ -343,6 +349,12 @@
 	  ec.pe_config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
 	  vec_add1 (pm->paired_events_to_collect, ec);
 	}
+      else if (unformat (line_input, "threads %U",
+			 unformat_bitmap_list, &pm->thread_bitmap))
+	;
+      else if (unformat (line_input, "thread %U",
+			 unformat_bitmap_list, &pm->thread_bitmap))
+	;
       else if (unformat (line_input, "%U", unformat_processor_event, pm, &ec))
 	{
 	  vec_add1 (pm->single_events_to_collect, ec);
@@ -358,10 +370,20 @@
       foreach_perfmon_event
 #undef _
 	else
-	return clib_error_return (0, "unknown input '%U'",
-				  format_unformat_error, line_input);
+	{
+	  error = clib_error_return (0, "unknown input '%U'",
+				     format_unformat_error, line_input);
+	  unformat_free (line_input);
+	  return error;
+	}
     }
 
+  unformat_free (line_input);
+
+  last_set = clib_bitmap_last_set (pm->thread_bitmap);
+  if (last_set != ~0 && last_set >= num_threads)
+    return clib_error_return (0, "thread %d does not exist", last_set);
+
   /* Stick paired events at the front of the (unified) list */
   if (vec_len (pm->paired_events_to_collect) > 0)
     {
@@ -410,7 +432,7 @@
 VLIB_CLI_COMMAND (set_pmc_command, static) =
 {
   .path = "set pmc",
-  .short_help = "set pmc c1 [..., use \"show pmc events\"]",
+  .short_help = "set pmc [threads n,n1-n2] c1... [see \"show pmc events\"]",
   .function = set_pmc_command_fn,
   .is_mp_safe = 1,
 };
diff --git a/src/plugins/perfmon/perfmon.h b/src/plugins/perfmon/perfmon.h
index 9663dae..9c4c34e 100644
--- a/src/plugins/perfmon/perfmon.h
+++ b/src/plugins/perfmon/perfmon.h
@@ -121,6 +121,9 @@
   /* Current perf_event file descriptors, per thread */
   int **pm_fds;
 
+  /* thread bitmap */
+  uword *thread_bitmap;
+
   /* Logging */
   vlib_log_class_t log_class;
 
diff --git a/src/plugins/perfmon/perfmon_periodic.c b/src/plugins/perfmon/perfmon_periodic.c
index ccf3e9e..0811439 100644
--- a/src/plugins/perfmon/perfmon_periodic.c
+++ b/src/plugins/perfmon/perfmon_periodic.c
@@ -21,6 +21,7 @@
 #include <asm/unistd.h>
 #include <sys/ioctl.h>
 
+/* "not in glibc" */
 static long
 perf_event_open (struct perf_event_attr *hw_event, pid_t pid, int cpu,
 		 int group_fd, unsigned long flags)
@@ -114,6 +115,7 @@
   u32 my_thread_index = vm->thread_index;
   u32 index;
   int i, limit = 1;
+  int cpu;
 
   if ((pm->current_event + 1) < vec_len (pm->single_events_to_collect))
     limit = 2;
@@ -140,7 +142,9 @@
 	  pe.exclude_hv = 1;
 	}
 
-      fd = perf_event_open (&pe, 0, -1, -1, 0);
+      cpu = vm->cpu_index;
+
+      fd = perf_event_open (&pe, 0, cpu, -1, 0);
       if (fd == -1)
 	{
 	  clib_unix_warning ("event open: type %d config %d", c->pe_type,
@@ -237,25 +241,38 @@
 start_event (perfmon_main_t * pm, f64 now, uword event_data)
 {
   int i;
+  int last_set;
+  int all = 0;
   pm->current_event = 0;
+
   if (vec_len (pm->single_events_to_collect) == 0)
     {
       pm->state = PERFMON_STATE_OFF;
       return;
     }
+
+  last_set = clib_bitmap_last_set (pm->thread_bitmap);
+  all = (last_set == ~0);
+
   pm->state = PERFMON_STATE_RUNNING;
   clear_counters (pm);
 
-  /* Start collection on this thread */
-  enable_current_events (pm);
+  /* Start collection on thread 0? */
+  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
+    {
+      /* Start collection on this thread */
+      enable_current_events (pm);
+    }
 
   /* And also on worker threads */
   for (i = 1; i < vec_len (vlib_mains); i++)
     {
       if (vlib_mains[i] == 0)
 	continue;
-      vlib_mains[i]->worker_thread_main_loop_callback = (void *)
-	worker_thread_start_event;
+
+      if (all || clib_bitmap_get (pm->thread_bitmap, i))
+	vlib_mains[i]->worker_thread_main_loop_callback = (void *)
+	  worker_thread_start_event;
     }
 }
 
@@ -397,23 +414,46 @@
 }
 
 static void
-handle_timeout (perfmon_main_t * pm, f64 now)
+handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now)
 {
   int i;
-  disable_events (pm);
+  int last_set, all;
+
+  last_set = clib_bitmap_last_set (pm->thread_bitmap);
+  all = (last_set == ~0);
+
+  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
+    disable_events (pm);
 
   /* And also on worker threads */
   for (i = 1; i < vec_len (vlib_mains); i++)
     {
       if (vlib_mains[i] == 0)
 	continue;
-      vlib_mains[i]->worker_thread_main_loop_callback = (void *)
-	worker_thread_stop_event;
+      if (all || clib_bitmap_get (pm->thread_bitmap, i))
+	vlib_mains[i]->worker_thread_main_loop_callback = (void *)
+	  worker_thread_stop_event;
     }
 
-  /* Short delay to make sure workers have stopped collection */
+  /* Make sure workers have stopped collection */
   if (i > 1)
-    vlib_process_suspend (pm->vlib_main, 1e-3);
+    {
+      f64 deadman = vlib_time_now (vm) + 1.0;
+
+      for (i = 1; i < vec_len (vlib_mains); i++)
+	{
+	  /* Has the worker actually stopped collecting data? */
+	  while (vlib_mains[i]->worker_thread_main_loop_callback)
+	    {
+	      if (vlib_time_now (vm) > deadman)
+		{
+		  clib_warning ("Thread %d deadman timeout!", i);
+		  break;
+		}
+	      vlib_process_suspend (pm->vlib_main, 1e-3);
+	    }
+	}
+    }
   scrape_and_clear_counters (pm);
   pm->current_event += pm->n_active;
   if (pm->current_event >= vec_len (pm->single_events_to_collect))
@@ -422,15 +462,18 @@
       pm->state = PERFMON_STATE_OFF;
       return;
     }
-  enable_current_events (pm);
+
+  if (all || clib_bitmap_get (pm->thread_bitmap, 0))
+    enable_current_events (pm);
 
   /* And also on worker threads */
   for (i = 1; i < vec_len (vlib_mains); i++)
     {
       if (vlib_mains[i] == 0)
 	continue;
-      vlib_mains[i]->worker_thread_main_loop_callback = (void *)
-	worker_thread_start_event;
+      if (all || clib_bitmap_get (pm->thread_bitmap, i))
+	vlib_mains[i]->worker_thread_main_loop_callback = (void *)
+	  worker_thread_start_event;
     }
 }
 
@@ -464,7 +507,7 @@
 
 	  /* Handle timeout */
 	case ~0:
-	  handle_timeout (pm, now);
+	  handle_timeout (vm, pm, now);
 	  break;
 
 	default: