misc: add callback hooks and refactor pmc

Callbacks for monitoring and performance measurement:
- Add new callback list type, with context
- Add callbacks for API, CLI, and barrier sync
- Modify node dispatch callback to pass plugin-specific context
- Modify perfmon plugin to keep PMC samples local to the plugin
- Include process nodes in dispatch callback
- Pass dispatch function return value to callback

Type: refactor

Signed-off-by: Tom Seidenberg <tseidenb@cisco.com>
Change-Id: I28b06c58490611e08d76ff5b01b2347ba2109b22
diff --git a/src/vlibapi/api_common.h b/src/vlibapi/api_common.h
index 86b1c5a..915ddab 100644
--- a/src/vlibapi/api_common.h
+++ b/src/vlibapi/api_common.h
@@ -224,7 +224,7 @@
 } api_version_t;
 
 /** API main structure, used by both vpp and binary API clients */
-typedef struct
+typedef struct api_main_t
 {
   /** Message handler vector  */
   void (**msg_handlers) (void *);
@@ -374,6 +374,12 @@
   elog_main_t *elog_main;
   int elog_trace_api_messages;
 
+  /** performance counter callback **/
+  void (**perf_counter_cbs)
+    (struct api_main_t *, u32 id, int before_or_after);
+  void (**perf_counter_cbs_tmp)
+    (struct api_main_t *, u32 id, int before_or_after);
+
 } api_main_t;
 
 extern __thread api_main_t *my_api_main;
diff --git a/src/vlibapi/api_shared.c b/src/vlibapi/api_shared.c
index caad6e5..5e715d6 100644
--- a/src/vlibapi/api_shared.c
+++ b/src/vlibapi/api_shared.c
@@ -30,6 +30,7 @@
 #include <vlib/unix/unix.h>
 #include <vlibapi/api.h>
 #include <vppinfra/elog.h>
+#include <vppinfra/callback.h>
 
 /* *INDENT-OFF* */
 api_main_t api_global_main =
@@ -493,7 +494,15 @@
 	      (*endian_fp) (the_msg);
 	    }
 
+	  if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
+	    clib_call_callbacks (am->perf_counter_cbs, am, id,
+				 0 /* before */ );
+
 	  (*am->msg_handlers[id]) (the_msg);
+
+	  if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
+	    clib_call_callbacks (am->perf_counter_cbs, am, id,
+				 1 /* after */ );
 	  if (!am->is_mp_safe[id])
 	    vl_msg_api_barrier_release ();
 	}
@@ -620,8 +629,13 @@
 	  endian_fp = am->msg_endian_handlers[id];
 	  (*endian_fp) (the_msg);
 	}
+      if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
+	clib_call_callbacks (am->perf_counter_cbs, am, id, 0 /* before */ );
 
       (*handler) (the_msg, vm, node);
+
+      if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
+	clib_call_callbacks (am->perf_counter_cbs, am, id, 1 /* after */ );
       if (is_private)
 	{
 	  am->vlib_rp = old_vlib_rp;