stats: Split stat_segment and stats code in preparation for deprecation. 97/14997/6
authorOle Troan <ot@cisco.com>
Wed, 26 Sep 2018 13:26:42 +0000 (15:26 +0200)
committerDave Barach <openvpp@barachs.net>
Fri, 28 Sep 2018 12:05:25 +0000 (12:05 +0000)
Split the stat_segment.c code from stats.c.
Rename stats.[ch] to prepare for removing (19.01?)
In addition stats.api can be removed.

Since the stats aggregation for the stat segment does not use the API, that
part is now done on the main thread. (Old stats aggregator is also left in place).

Change-Id: I9867429f4fc547b1a7ab7f88bc4f3625428d681b
Signed-off-by: Ole Troan <ot@cisco.com>
src/vnet/ip/ip_api.c
src/vpp/CMakeLists.txt
src/vpp/api/api.c
src/vpp/api/custom_dump.c
src/vpp/app/vpp_get_stats.c
src/vpp/stats/stat_segment.c
src/vpp/stats/stat_segment.h
src/vpp/stats/stats_to_be_deprecated.c [moved from src/vpp/stats/stats.c with 98% similarity]
src/vpp/stats/stats_to_be_deprecated.h [moved from src/vpp/stats/stats.h with 89% similarity]
test/framework.py

index bed5889..477090d 100644 (file)
@@ -20,7 +20,6 @@
 #include <vnet/vnet.h>
 #include <vlibmemory/api.h>
 
-#include <vpp/stats/stats.h>
 #include <vnet/interface.h>
 #include <vnet/api_errno.h>
 #include <vnet/ethernet/ethernet.h>
index 999b280..601bc03 100644 (file)
@@ -56,7 +56,7 @@ set(VPP_SOURCES
   app/version.c
   oam/oam.c
   oam/oam_api.c
-  stats/stats.c
+  stats/stats_to_be_deprecated.c
   stats/stat_segment.c
   api/api.c
   api/json_format.c
index f7643f4..d69b84f 100644 (file)
@@ -56,7 +56,7 @@
 #undef BIHASH_TYPE
 #undef __included_bihash_template_h__
 
-#include <vpp/stats/stats.h>
+#include <vnet/ip/format.h>
 
 #include <vpp/api/vpe_msg_enum.h>
 
index f8a8b73..baa675c 100644 (file)
@@ -40,7 +40,6 @@
 #include <vlibmemory/api.h>
 #include <vnet/lisp-cp/lisp_types.h>
 #include <vnet/qos/qos_types.h>
-#include <vpp/stats/stats.h>
 #include <vpp/oam/oam.h>
 
 #include <vnet/ethernet/ethernet.h>
index c1a5acb..1852b77 100644 (file)
@@ -19,7 +19,6 @@
 
 #include <vpp-api/client/stat_client.h>
 #include <vlib/vlib.h>
-#include <vpp/stats/stats.h>
 
 static int
 stat_poll_loop (u8 ** patterns)
index 7bf6624..37beb94 100644 (file)
  */
 
 #include <vppinfra/mem.h>
-#include <vpp/stats/stats.h>
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+#include "stat_segment.h"
+#include <vnet/vnet.h>
+#include <vnet/devices/devices.h>      /* vnet_get_aggregate_rx_packets */
 #undef HAVE_MEMFD_CREATE
 #include <vppinfra/linux/syscall.h>
 
+stat_segment_main_t stat_segment_main;
+
 /*
  *  Used only by VPP writers
  */
 void
 vlib_stat_segment_lock (void)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   clib_spinlock_lock (sm->stat_segment_lockp);
   sm->shared_header->in_progress = 1;
 }
@@ -32,7 +38,7 @@ vlib_stat_segment_lock (void)
 void
 vlib_stat_segment_unlock (void)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   sm->shared_header->epoch++;
   sm->shared_header->in_progress = 0;
   clib_spinlock_unlock (sm->stat_segment_lockp);
@@ -44,7 +50,7 @@ vlib_stat_segment_unlock (void)
 void *
 vlib_stats_push_heap (void)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
 
   ASSERT (sm && sm->shared_header);
   return clib_mem_set_heap (sm->heap);
@@ -54,7 +60,7 @@ vlib_stats_push_heap (void)
 static u32
 lookup_or_create_hash_index (void *oldheap, char *name, u32 next_vector_index)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   u32 index;
   hash_pair_t *hp;
 
@@ -76,7 +82,7 @@ void
 vlib_stats_pop_heap (void *cm_arg, void *oldheap, stat_directory_type_t type)
 {
   vlib_simple_counter_main_t *cm = (vlib_simple_counter_main_t *) cm_arg;
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   stat_segment_shared_header_t *shared_header = sm->shared_header;
   char *stat_segment_name;
   stat_segment_directory_entry_t e = { 0 };
@@ -138,7 +144,7 @@ vlib_stats_pop_heap (void *cm_arg, void *oldheap, stat_directory_type_t type)
 void
 vlib_stats_register_error_index (u8 * name, u64 * em_vec, u64 index)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   stat_segment_shared_header_t *shared_header = sm->shared_header;
   stat_segment_directory_entry_t e;
   hash_pair_t *hp;
@@ -163,7 +169,7 @@ vlib_stats_register_error_index (u8 * name, u64 * em_vec, u64 index)
 static void
 stat_validate_counter_vector (stat_segment_directory_entry_t * ep, u32 max)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   stat_segment_shared_header_t *shared_header = sm->shared_header;
   counter_t **counters = 0;
   vlib_thread_main_t *tm = vlib_get_thread_main ();
@@ -185,7 +191,7 @@ stat_validate_counter_vector (stat_segment_directory_entry_t * ep, u32 max)
 void
 vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   stat_segment_shared_header_t *shared_header = sm->shared_header;
 
   ASSERT (shared_header);
@@ -205,7 +211,7 @@ vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap)
 clib_error_t *
 vlib_map_stat_segment_init (void)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   stat_segment_shared_header_t *shared_header;
   stat_segment_directory_entry_t *ep;
 
@@ -329,7 +335,7 @@ show_stat_segment_command_fn (vlib_main_t * vm,
                              unformat_input_t * input,
                              vlib_cli_command_t * cmd)
 {
-  stats_main_t *sm = &stats_main;
+  stat_segment_main_t *sm = &stat_segment_main;
   counter_t *counter;
   hash_pair_t *p;
   stat_segment_directory_entry_t *show_data, *this;
@@ -383,7 +389,7 @@ VLIB_CLI_COMMAND (show_stat_segment_command, static) =
  */
 
 static inline void
-update_node_counters (stats_main_t * sm)
+update_node_counters (stat_segment_main_t * sm)
 {
   vlib_main_t *vm = vlib_mains[0];
   vlib_main_t **stat_vms = 0;
@@ -465,14 +471,8 @@ update_node_counters (stats_main_t * sm)
     }
 }
 
-/*
- * Called by stats_thread_fn, in stats.c, which runs in a
- * separate pthread, which won't halt the parade
- * in single-forwarding-core cases.
- */
-
-void
-do_stat_segment_updates (stats_main_t * sm)
+static void
+do_stat_segment_updates (stat_segment_main_t * sm)
 {
   vlib_main_t *vm = vlib_mains[0];
   f64 vector_rate;
@@ -518,15 +518,105 @@ do_stat_segment_updates (stats_main_t * sm)
   sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
 }
 
+/*
+ * Accept connection on the socket and exchange the fd for the shared
+ * memory segment.
+ */
+static clib_error_t *
+stats_socket_accept_ready (clib_file_t * uf)
+{
+  stat_segment_main_t *sm = &stat_segment_main;
+  clib_error_t *err;
+  clib_socket_t client = { 0 };
+
+  err = clib_socket_accept (sm->socket, &client);
+  if (err)
+    {
+      clib_error_report (err);
+      return err;
+    }
+
+  /* Send the fd across and close */
+  err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
+  if (err)
+    clib_error_report (err);
+  clib_socket_close (&client);
+
+  return 0;
+}
+
+static void
+stats_segment_socket_init (void)
+{
+  stat_segment_main_t *sm = &stat_segment_main;
+  clib_error_t *error;
+  clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
+
+  s->config = (char *) sm->socket_name;
+  s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
+    CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
+  if ((error = clib_socket_init (s)))
+    {
+      clib_error_report (error);
+      return;
+    }
+
+  clib_file_t template = { 0 };
+  template.read_function = stats_socket_accept_ready;
+  template.file_descriptor = s->fd;
+  template.description =
+    format (0, "stats segment listener %s", STAT_SEGMENT_SOCKET_FILE);
+  clib_file_add (&file_main, &template);
+
+  sm->socket = s;
+}
+
+static uword
+stat_segment_collector_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
+                               vlib_frame_t * f)
+{
+  stat_segment_main_t *sm = &stat_segment_main;
+
+  /* Wait for Godot... */
+  f64 sleep_duration = 10;
+
+  while (1)
+    {
+      do_stat_segment_updates (sm);
+      vlib_process_suspend (vm, sleep_duration);
+    }
+  return 0;                    /* or not */
+}
+
+static clib_error_t *
+statseg_init (vlib_main_t * vm)
+{
+  stat_segment_main_t *sm = &stat_segment_main;
+  clib_error_t *error;
+
+  if ((error = vlib_call_init_function (vm, unix_input_init)))
+    return error;
+
+  if (sm->socket_name)
+    stats_segment_socket_init ();
+
+  return 0;
+}
+
 static clib_error_t *
 statseg_config (vlib_main_t * vm, unformat_input_t * input)
 {
-  stats_main_t *sm = &stats_main;
-  uword ms;
+  stat_segment_main_t *sm = &stat_segment_main;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "size %U", unformat_memory_size, &sm->memory_size))
+      if (unformat (input, "socket-name %s", &sm->socket_name))
+       ;
+      else if (unformat (input, "default"))
+       sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
+      else
+       if (unformat
+           (input, "size %U", unformat_memory_size, &sm->memory_size))
        ;
       else if (unformat (input, "per-node-counters on"))
        sm->node_counters_enabled = 1;
@@ -540,8 +630,17 @@ statseg_config (vlib_main_t * vm, unformat_input_t * input)
   return 0;
 }
 
+VLIB_INIT_FUNCTION (statseg_init);
 VLIB_EARLY_CONFIG_FUNCTION (statseg_config, "statseg");
 
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (stat_segment_collector, static) =
+{
+.function = stat_segment_collector_process,.name =
+    "statseg-collector-process",.type = VLIB_NODE_TYPE_PROCESS,};
+
+/* *INDENT-ON* */
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
index 0efd45a..21c7c3b 100644 (file)
@@ -17,6 +17,8 @@
 #define included_stat_segment_h
 
 #include <stdatomic.h>
+#include <vlib/vlib.h>
+#include <vppinfra/socket.h>
 
 /* Default socket to exchange segment fd */
 #define STAT_SEGMENT_SOCKET_FILE "/run/vpp/stats.sock"
@@ -94,4 +96,23 @@ stat_segment_pointer (void *start, uint64_t offset)
   return ((char *) start + offset);
 }
 
+typedef struct
+{
+  /* statistics segment */
+  uword *directory_vector_by_name;
+  stat_segment_directory_entry_t *directory_vector;
+  clib_spinlock_t *stat_segment_lockp;
+  clib_socket_t *socket;
+  u8 *socket_name;
+  ssize_t memory_size;
+  u8 node_counters_enabled;
+  void *heap;
+  stat_segment_shared_header_t *shared_header; /* pointer to shared memory segment */
+  int memfd;
+
+  u64 last_input_packets;
+} stat_segment_main_t;
+
+extern stat_segment_main_t stat_segment_main;
+
 #endif
similarity index 98%
rename from src/vpp/stats/stats.c
rename to src/vpp/stats/stats_to_be_deprecated.c
index 25b8c18..94d68c8 100644 (file)
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <vpp/stats/stats.h>
+#include "stats_to_be_deprecated.h"
 #include <signal.h>
 #include <vnet/fib/ip4_fib.h>
 #include <vnet/fib/fib_entry.h>
@@ -2338,60 +2338,6 @@ stats_set_poller_delay (u32 poller_delay_sec)
     }
 }
 
-/*
- * Accept connection on the socket and exchange the fd for the shared
- * memory segment.
- */
-static clib_error_t *
-stats_socket_accept_ready (clib_file_t * uf)
-{
-  stats_main_t *sm = &stats_main;
-  clib_error_t *err;
-  clib_socket_t client = { 0 };
-
-  err = clib_socket_accept (sm->socket, &client);
-  if (err)
-    {
-      clib_error_report (err);
-      return err;
-    }
-
-  /* Send the fd across and close */
-  err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
-  if (err)
-    clib_error_report (err);
-  clib_socket_close (&client);
-
-  return 0;
-}
-
-static void
-stats_segment_socket_init (void)
-{
-  stats_main_t *sm = &stats_main;
-  clib_error_t *error;
-  clib_socket_t *s = clib_mem_alloc (sizeof (clib_socket_t));
-
-  s->config = (char *) sm->socket_name;
-  s->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
-    CLIB_SOCKET_F_ALLOW_GROUP_WRITE | CLIB_SOCKET_F_PASSCRED;
-  if ((error = clib_socket_init (s)))
-    {
-      clib_error_report (error);
-      return;
-    }
-
-  clib_file_t template = { 0 };
-  clib_file_main_t *fm = &file_main;
-  template.read_function = stats_socket_accept_ready;
-  template.file_descriptor = s->fd;
-  template.description =
-    format (0, "stats segment listener %s", STAT_SEGMENT_SOCKET_FILE);
-  clib_file_add (fm, &template);
-
-  sm->socket = s;
-}
-
 static clib_error_t *
 stats_config (vlib_main_t * vm, unformat_input_t * input)
 {
@@ -2400,11 +2346,7 @@ stats_config (vlib_main_t * vm, unformat_input_t * input)
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "socket-name %s", &sm->socket_name))
-       ;
-      else if (unformat (input, "default"))
-       sm->socket_name = format (0, "%s", STAT_SEGMENT_SOCKET_FILE);
-      else if (unformat (input, "interval %u", &sec))
+      if (unformat (input, "interval %u", &sec))
        {
          int rv = stats_set_poller_delay (sec);
          if (rv)
@@ -2421,9 +2363,6 @@ stats_config (vlib_main_t * vm, unformat_input_t * input)
        }
     }
 
-  if (sm->socket_name)
-    stats_segment_socket_init ();
-
   return 0;
 }
 
@@ -2482,9 +2421,6 @@ stats_thread_fn (void *arg)
       ip46_fib_stats_delay (sm, sm->stats_poll_interval_in_seconds,
                            0 /* nsec */ );
 
-      /* Always update stats segment data */
-      do_stat_segment_updates (sm);
-
       if (!(sm->enable_poller))
        continue;
 
similarity index 89%
rename from src/vpp/stats/stats.h
rename to src/vpp/stats/stats_to_be_deprecated.h
index f21451d..9259527 100644 (file)
@@ -26,7 +26,6 @@
 #include <vlib/unix/unix.h>
 #include <vlibmemory/api.h>
 #include <vlibapi/api_helper_macros.h>
-#include <vpp/stats/stat_segment.h>
 
 typedef struct
 {
@@ -156,20 +155,6 @@ typedef struct
   vpe_client_stats_registration_t **regs_tmp;
   vpe_client_registration_t **clients_tmp;
 
-  /* statistics segment */
-  uword *directory_vector_by_name;
-  stat_segment_directory_entry_t *directory_vector;
-  clib_spinlock_t *stat_segment_lockp;
-  clib_socket_t *socket;
-  u8 *socket_name;
-  ssize_t memory_size;
-  u8 node_counters_enabled;
-  void *heap;
-  stat_segment_shared_header_t *shared_header; /* pointer to shared memory segment */
-  int memfd;
-
-  u64 last_input_packets;
-
   /* convenience */
   vlib_main_t *vlib_main;
   vnet_main_t *vnet_main;
@@ -179,8 +164,6 @@ typedef struct
 
 extern stats_main_t stats_main;
 
-void do_stat_segment_updates (stats_main_t * sm);
-
 #endif /* __included_stats_h__ */
 
 /*
index 671d4e9..cd15aec 100644 (file)
@@ -287,7 +287,7 @@ class VppTestCase(unittest.TestCase):
                            coredump_size, "}", "api-trace", "{", "on", "}",
                            "api-segment", "{", "prefix", cls.shm_prefix, "}",
                            "cpu", "{", "main-core", str(cpu_core_number), "}",
-                           "stats", "{", "socket-name",
+                           "statseg", "{", "socket-name",
                            cls.tempdir + "/stats.sock", "}",
                            "plugins", "{", "plugin", "dpdk_plugin.so", "{",
                            "disable", "}", "plugin", "unittest_plugin.so",