stats: refactor header files 02/21302/3
authorDave Barach <dave@barachs.net>
Wed, 14 Aug 2019 13:35:41 +0000 (09:35 -0400)
committerNeale Ranns <nranns@cisco.com>
Thu, 15 Aug 2019 10:14:52 +0000 (10:14 +0000)
Performant stat segment scraping involves caching the results of
stat_segment_ls (...) and directly fishing counter data from the
shared-memory segment.

To do that, we need to publish several things previously hidden,
declared in stat_client.c:

o stat_client_main_t typedef
o stat_segment_access_t typedef
o stat_segment_access_start inline function
o stat_segment_access_end inline function

Type: refactor

Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: I3175e3d1f1fd8ea816336a584565179d1972115c

src/vpp-api/client/stat_client.c
src/vpp-api/client/stat_client.h
src/vpp/CMakeLists.txt
src/vpp/stats/stat_segment.h
src/vpp/stats/stat_segment_shared.h [new file with mode: 0644]

index 0eaec15..9a7a0e5 100644 (file)
 #include <assert.h>
 #include <vppinfra/vec.h>
 #include <vppinfra/lock.h>
-#include "stat_client.h"
 #include <stdatomic.h>
 #include <vpp/stats/stat_segment.h>
-
-struct stat_client_main_t
-{
-  uint64_t current_epoch;
-  stat_segment_shared_header_t *shared_header;
-  stat_segment_directory_entry_t *directory_vector;
-  ssize_t memory_size;
-};
+#include <vpp-api/client/stat_client.h>
 
 stat_client_main_t stat_client_main;
 
@@ -169,34 +161,6 @@ stat_segment_disconnect_r (stat_client_main_t * sm)
   return;
 }
 
-typedef struct
-{
-  uint64_t epoch;
-} stat_segment_access_t;
-
-static void
-stat_segment_access_start (stat_segment_access_t * sa,
-                          stat_client_main_t * sm)
-{
-  stat_segment_shared_header_t *shared_header = sm->shared_header;
-  sa->epoch = shared_header->epoch;
-  while (shared_header->in_progress != 0)
-    ;
-  sm->directory_vector = stat_segment_pointer (sm->shared_header,
-                                              sm->
-                                              shared_header->directory_offset);
-}
-
-static bool
-stat_segment_access_end (stat_segment_access_t * sa, stat_client_main_t * sm)
-{
-  stat_segment_shared_header_t *shared_header = sm->shared_header;
-
-  if (shared_header->epoch != sa->epoch || shared_header->in_progress)
-    return false;
-  return true;
-}
-
 void
 stat_segment_disconnect (void)
 {
index 10b54c8..97a21dd 100644 (file)
 #include <stdint.h>
 #include <unistd.h>
 #include <vlib/counter_types.h>
-
-typedef enum
-{
-  STAT_DIR_TYPE_ILLEGAL = 0,
-  STAT_DIR_TYPE_SCALAR_INDEX,
-  STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE,
-  STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED,
-  STAT_DIR_TYPE_ERROR_INDEX,
-  STAT_DIR_TYPE_NAME_VECTOR,
-} stat_directory_type_t;
+#include <stdbool.h>
+#include <vpp/stats/stat_segment_shared.h>
 
 /* Default socket to exchange segment fd */
 /* TODO: Get from runtime directory */
 #define STAT_SEGMENT_SOCKET_FILE "/run/vpp/stats.sock"
 #define STAT_SEGMENT_SOCKET_FILENAME "stats.sock"
 
-typedef struct stat_client_main_t stat_client_main_t;
-
 typedef struct
 {
   char *name;
@@ -55,6 +45,16 @@ typedef struct
   };
 } stat_segment_data_t;
 
+typedef struct
+{
+  uint64_t current_epoch;
+  stat_segment_shared_header_t *shared_header;
+  stat_segment_directory_entry_t *directory_vector;
+  ssize_t memory_size;
+} stat_client_main_t;
+
+extern stat_client_main_t stat_client_main;
+
 stat_client_main_t *stat_client_get (void);
 void stat_client_free (stat_client_main_t * sm);
 int stat_segment_connect_r (const char *socket_name, stat_client_main_t * sm);
@@ -83,6 +83,35 @@ char *stat_segment_index_to_name (uint32_t index);
 uint64_t stat_segment_version (void);
 uint64_t stat_segment_version_r (stat_client_main_t * sm);
 
+typedef struct
+{
+  uint64_t epoch;
+} stat_segment_access_t;
+
+static inline void
+stat_segment_access_start (stat_segment_access_t * sa,
+                          stat_client_main_t * sm)
+{
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+  sa->epoch = shared_header->epoch;
+  while (shared_header->in_progress != 0)
+    ;
+  sm->directory_vector = (stat_segment_directory_entry_t *)
+    stat_segment_pointer (sm->shared_header,
+                         sm->shared_header->directory_offset);
+}
+
+static inline bool
+stat_segment_access_end (stat_segment_access_t * sa, stat_client_main_t * sm)
+{
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+
+  if (shared_header->epoch != sa->epoch || shared_header->in_progress)
+    return false;
+  return true;
+}
+
+
 #endif /* included_stat_client_h */
 
 /*
index 401f1d0..da43b0f 100644 (file)
@@ -81,6 +81,7 @@ add_vpp_headers(vpp
   api/vpe_msg_enum.h
   api/vpe_all_api_h.h
   stats/stat_segment.h
+  stats/stat_segment_shared.h
 )
 
 ##############################################################################
index a67c59f..5c55cf9 100644 (file)
 #ifndef included_stat_segment_h
 #define included_stat_segment_h
 
-#include <stdatomic.h>
 #include <vlib/vlib.h>
 #include <vppinfra/socket.h>
-#include <vpp-api/client/stat_client.h>
+#include <vpp/stats/stat_segment_shared.h>
 
 typedef enum
 {
@@ -59,49 +58,18 @@ typedef enum
   _(MEM_STATSEG_TOTAL, SCALAR_INDEX, total, /mem/statseg)       \
   _(MEM_STATSEG_USED, SCALAR_INDEX, used, /mem/statseg)
 
-typedef struct
-{
-  stat_directory_type_t type;
-  union {
-    uint64_t offset;
-    uint64_t index;
-    uint64_t value;
-  };
-  uint64_t offset_vector;
-  char name[128]; // TODO change this to pointer to "somewhere"
-} stat_segment_directory_entry_t;
-
 /* Default stat segment 32m */
 #define STAT_SEGMENT_DEFAULT_SIZE      (32<<20)
 
 /* Shared segment memory layout version */
 #define STAT_SEGMENT_VERSION           1
 
-/*
- * Shared header first in the shared memory segment.
- */
-typedef struct
-{
-  u64 version;
-  atomic_int_fast64_t epoch;
-  atomic_int_fast64_t in_progress;
-  atomic_int_fast64_t directory_offset;
-  atomic_int_fast64_t error_offset;
-  atomic_int_fast64_t stats_offset;
-} stat_segment_shared_header_t;
-
 static inline uint64_t
 stat_segment_offset (void *start, void *data)
 {
   return (char *) data - (char *) start;
 }
 
-static inline void *
-stat_segment_pointer (void *start, uint64_t offset)
-{
-  return ((char *) start + offset);
-}
-
 typedef void (*stat_segment_update_fn)(stat_segment_directory_entry_t * e, u32 i);
 
 typedef struct {
diff --git a/src/vpp/stats/stat_segment_shared.h b/src/vpp/stats/stat_segment_shared.h
new file mode 100644 (file)
index 0000000..719cf59
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+
+#ifndef included_stat_segment_shared_h
+#define included_stat_segment_shared_h
+
+typedef enum
+{
+  STAT_DIR_TYPE_ILLEGAL = 0,
+  STAT_DIR_TYPE_SCALAR_INDEX,
+  STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE,
+  STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED,
+  STAT_DIR_TYPE_ERROR_INDEX,
+  STAT_DIR_TYPE_NAME_VECTOR,
+} stat_directory_type_t;
+
+typedef struct
+{
+  stat_directory_type_t type;
+  union {
+    uint64_t offset;
+    uint64_t index;
+    uint64_t value;
+  };
+  uint64_t offset_vector;
+  char name[128]; // TODO change this to pointer to "somewhere"
+} stat_segment_directory_entry_t;
+
+/*
+ * Shared header first in the shared memory segment.
+ */
+typedef struct
+{
+  uint64_t version;
+  volatile uint64_t epoch;
+  volatile uint64_t in_progress;
+  volatile uint64_t directory_offset;
+  volatile uint64_t error_offset;
+  volatile uint64_t stats_offset;
+} stat_segment_shared_header_t;
+
+static inline void *
+stat_segment_pointer (void *start, uint64_t offset)
+{
+  return ((char *) start + offset);
+}
+
+#endif /* included_stat_segment_shared_h */