Add a command line utility to display critical vpp engine metrics
[vpp.git] / vpp / vpp-api / vpp_get_metrics.c
diff --git a/vpp/vpp-api/vpp_get_metrics.c b/vpp/vpp-api/vpp_get_metrics.c
new file mode 100644 (file)
index 0000000..e963bc6
--- /dev/null
@@ -0,0 +1,186 @@
+/* 
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <netinet/in.h>
+#include <signal.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <time.h>
+#include <fcntl.h>
+#include <string.h>
+#include <vppinfra/clib.h>
+#include <vppinfra/vec.h>
+#include <vppinfra/hash.h>
+#include <vppinfra/bitmap.h>
+#include <vppinfra/fifo.h>
+#include <vppinfra/time.h>
+#include <vppinfra/mheap.h>
+#include <vppinfra/heap.h>
+#include <vppinfra/pool.h>
+#include <vppinfra/format.h>
+#include <vlibapi/api.h>
+#include <vlibmemory/api.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+#include <vnet/api_errno.h>
+
+#include <svmdb.h>
+
+svmdb_client_t *c;
+volatile int signal_received;
+
+static void
+unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
+{
+  static int once;
+
+  if (once)
+    exit (1);
+
+  once = 1;
+  signal_received = 1;
+}
+
+static void
+setup_signal_handlers (void)
+{
+  uword i;
+  struct sigaction sa;
+
+  for (i = 1; i < 32; i++)
+    {
+      memset (&sa, 0, sizeof (sa));
+      sa.sa_sigaction = (void *) unix_signal_handler;
+      sa.sa_flags = SA_SIGINFO;
+
+      switch (i)
+       {
+         /* these signals take the default action */
+       case SIGABRT:
+       case SIGKILL:
+       case SIGSTOP:
+       case SIGUSR1:
+       case SIGUSR2:
+         continue;
+
+         /* ignore SIGPIPE, SIGCHLD */
+       case SIGPIPE:
+       case SIGCHLD:
+         sa.sa_sigaction = (void *) SIG_IGN;
+         break;
+
+         /* catch and handle all other signals */
+       default:
+         break;
+       }
+
+      if (sigaction (i, &sa, 0) < 0)
+       return clib_unix_warning (0, "sigaction %U", format_signal, i);
+    }
+}
+
+int
+main (int argc, char **argv)
+{
+  unformat_input_t input;
+  char *chroot_path = 0;
+  u8 *chroot_path_u8;
+  int interval = 0;
+  f64 *vector_ratep, *rx_ratep, *sig_error_ratep;
+  pid_t *vpp_pidp;
+
+  unformat_init_command_line (&input, argv);
+
+  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (&input, "chroot %s", &chroot_path_u8))
+       {
+         chroot_path = (char *) chroot_path_u8;
+       }
+      else if (unformat (&input, "interval %d", &interval))
+       ;
+      else
+       {
+         fformat (stderr,
+                  "usage: vpp_get_metrics [chroot <path>] [interval <nn>]\n");
+         exit (1);
+       }
+    }
+
+  setup_signal_handlers ();
+
+  c = svmdb_map_chroot (chroot_path);
+
+  vpp_pidp =
+    svmdb_local_get_variable_reference (c, SVMDB_NAMESPACE_VEC, "vpp_pid");
+  vector_ratep =
+    svmdb_local_get_variable_reference (c, SVMDB_NAMESPACE_VEC,
+                                       "vpp_vector_rate");
+  rx_ratep =
+    svmdb_local_get_variable_reference (c, SVMDB_NAMESPACE_VEC,
+                                       "vpp_input_rate");
+  sig_error_ratep =
+    svmdb_local_get_variable_reference (c, SVMDB_NAMESPACE_VEC,
+                                       "vpp_sig_error_rate");
+
+  /* 
+   * Make sure vpp is actually running. Otherwise, there's every
+   * chance that the database region will be wiped out by the
+   * process monitor script
+   */
+
+  if (vpp_pidp == 0 || vector_ratep == 0 || rx_ratep == 0
+      || sig_error_ratep == 0)
+    {
+      fformat (stdout, "vpp not running\n");
+      exit (1);
+    }
+
+  do
+    {
+      /* Once vpp exits, the svm db region will be recreated... */
+      if (*vpp_pidp == 0 || kill (*vpp_pidp, 0) < 0)
+       {
+         fformat (stdout, "vpp not running\n");
+         exit (1);
+       }
+      fformat (stdout,
+              "%d: vpp_vector_rate=%.2f, vpp_input_rate=%f, vpp_sig_error_rate=%f\n",
+              *vpp_pidp, *vector_ratep, *rx_ratep, *sig_error_ratep);
+
+      if (interval)
+       sleep (interval);
+      if (signal_received)
+       break;
+    }
+  while (interval);
+
+  svmdb_unmap (c);
+  exit (0);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */