stats: enable/disable segments polls

Add an enable/disable field to stats segments poll. The is used by the
perfmon plugin to only poll for stats on an active bundle.

Type: improvement

Signed-off-by: Ray Kinsella <mdr@ashroe.eu>
Change-Id: I6404a7db50d8e922afea5da8ec15fdabe3d97157
diff --git a/src/vpp/stats/stat_segment.c b/src/vpp/stats/stat_segment.c
index 1ca9f5b..cebd5e3 100644
--- a/src/vpp/stats/stat_segment.c
+++ b/src/vpp/stats/stat_segment.c
@@ -780,9 +780,10 @@
   /* *INDENT-OFF* */
   stat_segment_gauges_pool_t *g;
   pool_foreach (g, sm->gauges)
-   {
-    g->fn(&sm->directory_vector[g->directory_index], g->caller_index);
-  }
+    {
+      if (g->enabled)
+	g->fn (&sm->directory_vector[g->directory_index], g->caller_index);
+    }
   /* *INDENT-ON* */
 
   /* Heartbeat, so clients detect we're still here */
@@ -880,9 +881,9 @@
  * Add a data provider (via callback) for a given stats entry.
  * TODO: Add support for per-provider interval.
  */
-void
+uword
 stat_segment_poll_add (u32 vector_index, stat_segment_update_fn update_fn,
-		       u32 caller_index, u32 interval)
+		       u32 caller_index, u32 interval, u8 enabled)
 {
   stat_segment_main_t *sm = &stat_segment_main;
   stat_segment_gauges_pool_t *gauge;
@@ -891,8 +892,23 @@
   gauge->fn = update_fn;
   gauge->caller_index = caller_index;
   gauge->directory_index = vector_index;
+  gauge->enabled = enabled;
 
-  return;
+  return pool_elts (sm->gauges) - 1;
+}
+
+/*
+ * Enable (or disable) a callback for a stats entry
+ */
+void
+stat_segment_poll_state (uword index, u8 enabled)
+{
+  stat_segment_main_t *sm = &stat_segment_main;
+  stat_segment_gauges_pool_t *gauge = pool_elt_at_index (sm->gauges, index);
+
+  ASSERT (gauge);
+
+  gauge->enabled = enabled;
 }
 
 /*
@@ -900,20 +916,14 @@
  * Deprecated, replace with stat_segment_new_entry + stat_segment_pool_add
  */
 clib_error_t *
-stat_segment_register_gauge (u8 * name, stat_segment_update_fn update_fn,
+stat_segment_register_gauge (u8 *name, stat_segment_update_fn update_fn,
 			     u32 caller_index)
 {
-  stat_segment_main_t *sm = &stat_segment_main;
-  stat_segment_gauges_pool_t *gauge;
-
   u32 vector_index = stat_segment_new_entry (name, STAT_DIR_TYPE_SCALAR_INDEX);
   if (vector_index == ~0) /* Already registered */
     return clib_error_return (0, "%v is already registered", name);
 
-  pool_get (sm->gauges, gauge);
-  gauge->fn = update_fn;
-  gauge->caller_index = caller_index;
-  gauge->directory_index = vector_index;
+  stat_segment_poll_add (vector_index, update_fn, caller_index, -1, true);
 
   return NULL;
 }
diff --git a/src/vpp/stats/stat_segment.h b/src/vpp/stats/stat_segment.h
index 2858003..42c3007 100644
--- a/src/vpp/stats/stat_segment.h
+++ b/src/vpp/stats/stat_segment.h
@@ -68,6 +68,7 @@
   u32 directory_index;
   stat_segment_update_fn fn;
   u32 caller_index;
+  u8 enabled;
 } stat_segment_gauges_pool_t;
 
 typedef struct
@@ -108,8 +109,10 @@
 clib_error_t *
 stat_segment_deregister_state_counter(u32 index);
 void stat_segment_set_state_counter (u32 index, u64 value);
-void stat_segment_poll_add (u32 vector_index, stat_segment_update_fn update_fn,
-			    u32 caller_index, u32 interval);
+uword stat_segment_poll_add (u32 vector_index,
+			     stat_segment_update_fn update_fn,
+			     u32 caller_index, u32 interval, u8 enabled);
+void stat_segment_poll_state (uword index, u8 enabled);
 
 counter_t **stat_validate_counter_vector3 (counter_t **counters, u32 max1,
 					   u32 max2);
diff --git a/src/vpp/stats/stat_segment_provider.c b/src/vpp/stats/stat_segment_provider.c
index 30e03be..f62a74f 100644
--- a/src/vpp/stats/stat_segment_provider.c
+++ b/src/vpp/stats/stat_segment_provider.c
@@ -117,7 +117,7 @@
   vec_free (s_free);
 
   stat_segment_poll_add (mem_vector_index, stat_provider_mem_usage_update_fn,
-			 heap_index, 10);
+			 heap_index, 10, true);
 }
 
 static void
@@ -180,7 +180,7 @@
   if (i == ~0)
     ASSERT (0);
   vec_free (s);
-  stat_segment_poll_add (i, stat_provider_vector_rate_update_fn, ~0, 10);
+  stat_segment_poll_add (i, stat_provider_vector_rate_update_fn, ~0, 10, true);
 
   s = format (0, "/sys/vector_rate_per_worker%c", 0);
   i = stat_segment_new_entry (s, STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE);
@@ -188,7 +188,7 @@
     ASSERT (0);
   vec_free (s);
   stat_segment_poll_add (i, stat_provider_vector_rate_per_thread_update_fn, ~0,
-			 10);
+			 10, true);
 
   stat_segment_main_t *sm = &stat_segment_main;
   vlib_stat_segment_lock ();