STATS: Dynamically mapped shared memory segment

Move from using a hash to a vector with offsets into shared memory.
Limit exposure of VPP data structures and include files to
external stats library and applications.

Change-Id: Ic06129f12d10cf4c4946a86d9bc734eacff2c7da
Signed-off-by: Ole Troan <ot@cisco.com>
diff --git a/src/vlib/CMakeLists.txt b/src/vlib/CMakeLists.txt
index c3dfba9..b187f98 100644
--- a/src/vlib/CMakeLists.txt
+++ b/src/vlib/CMakeLists.txt
@@ -66,6 +66,7 @@
   cli_funcs.h
   cli.h
   counter.h
+  counter_types.h
   defs.h
   error_funcs.h
   error.h
diff --git a/src/vlib/counter.c b/src/vlib/counter.c
index 6afa73e..5c3350f 100644
--- a/src/vlib/counter.c
+++ b/src/vlib/counter.c
@@ -99,7 +99,7 @@
     vec_validate_aligned (cm->counters[i], index, CLIB_CACHE_LINE_BYTES);
 
   vlib_stats_pop_heap (cm, oldheap,
-		       3 /* STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE */ );
+		       2 /* STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE */ );
 }
 
 void
@@ -114,7 +114,7 @@
     vec_validate_aligned (cm->counters[i], index, CLIB_CACHE_LINE_BYTES);
 
   vlib_stats_pop_heap (cm, oldheap,
-		       4 /*STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED */ );
+		       3 /*STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED */ );
 }
 
 u32
diff --git a/src/vlib/counter.h b/src/vlib/counter.h
index fe5279a..3aacc9b 100644
--- a/src/vlib/counter.h
+++ b/src/vlib/counter.h
@@ -40,6 +40,8 @@
 #ifndef included_vlib_counter_h
 #define included_vlib_counter_h
 
+#include <vlib/counter_types.h>
+
 /** \file
 
     Optimized thread-safe counters.
@@ -50,9 +52,6 @@
     The idea is to drastically eliminate atomic operations.
 */
 
-/** 64bit counters */
-typedef u64 counter_t;
-
 /** A collection of simple counters */
 
 typedef struct
@@ -135,14 +134,6 @@
     }
 }
 
-/** Combined counter to hold both packets and byte differences.
- */
-typedef struct
-{
-  counter_t packets;			/**< packet counter */
-  counter_t bytes;			/**< byte counter  */
-} vlib_counter_t;
-
 /** Add two combined counters, results in the first counter
     @param [in,out] a - (vlib_counter_t *) dst counter
     @param b - (vlib_counter_t *) src counter
diff --git a/src/vlib/counter_types.h b/src/vlib/counter_types.h
new file mode 100644
index 0000000..d6ce27f
--- /dev/null
+++ b/src/vlib/counter_types.h
@@ -0,0 +1,32 @@
+/*
+ * 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_vlib_counter_types_h
+#define included_vlib_counter_types_h
+
+#include <stdint.h>
+
+/** 64bit counters */
+typedef uint64_t counter_t;
+
+/** Combined counter to hold both packets and byte differences.
+ */
+typedef struct
+{
+  counter_t packets;			/**< packet counter */
+  counter_t bytes;			/**< byte counter  */
+} vlib_counter_t;
+
+#endif
diff --git a/src/vlib/error.c b/src/vlib/error.c
index aa53324..9fc1e70 100644
--- a/src/vlib/error.c
+++ b/src/vlib/error.c
@@ -140,9 +140,10 @@
 };
 /* *INDENT-ON* */
 
-void vlib_stats_register_error_index (u8 *, u64) __attribute__ ((weak));
+void vlib_stats_register_error_index (u8 *, u64 *, u64)
+  __attribute__ ((weak));
 void
-vlib_stats_register_error_index (u8 * notused, u64 notused2)
+vlib_stats_register_error_index (u8 * notused, u64 * notused2, u64 notused3)
 {
 };
 
@@ -210,7 +211,8 @@
       {
 	error_name = format (0, "/err/%v/%s%c", n->name, error_strings[i], 0);
 	/* Note: error_name consumed by the following call */
-	vlib_stats_register_error_index (error_name, n->error_heap_index + i);
+	vlib_stats_register_error_index (error_name, em->counters,
+					 n->error_heap_index + i);
       }
   }
 
diff --git a/src/vpp-api/CMakeLists.txt b/src/vpp-api/CMakeLists.txt
index 872e9cc..0f2510d 100644
--- a/src/vpp-api/CMakeLists.txt
+++ b/src/vpp-api/CMakeLists.txt
@@ -20,8 +20,7 @@
   client/client.c
   client/stat_client.c
   client/libvppapiclient.map
-
-  LINK_LIBRARIES vppinfra vlibmemoryclient svm pthread m rt
+  LINK_LIBRARIES vppinfra vlibmemoryclient pthread
 )
 add_dependencies(vppapiclient vpp_version_h api_headers)
 
diff --git a/src/vpp-api/client/libvppapiclient.map b/src/vpp-api/client/libvppapiclient.map
index f51b924..00a26fb 100644
--- a/src/vpp-api/client/libvppapiclient.map
+++ b/src/vpp-api/client/libvppapiclient.map
@@ -15,8 +15,6 @@
 	api_main;
 	stat_segment_connect;
 	stat_segment_disconnect;
-	stat_segment_register;
-	stat_segment_collect;
 	stat_segment_ls;
 	stat_segment_dump;
 	stat_segment_data_free;
diff --git a/src/vpp-api/client/stat_client.c b/src/vpp-api/client/stat_client.c
index ad7078e..b5aab74 100644
--- a/src/vpp-api/client/stat_client.c
+++ b/src/vpp-api/client/stat_client.c
@@ -17,72 +17,122 @@
  *------------------------------------------------------------------
  */
 
-#include <vlib/vlib.h>
-#include <vppinfra/socket.h>
-#include <svm/ssvm.h>
-#include <vpp/stats/stats.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdbool.h>
+#include <sys/stat.h>
 #include <regex.h>
+#include <assert.h>
+#include <vppinfra/vec.h>
+#include <vppinfra/lock.h>
 #include "stat_client.h"
+#include <stdatomic.h>
 
 typedef struct
 {
-  u64 current_epoch;
-  volatile int segment_ready;
-  ssvm_private_t stat_segment;	/* mapped stats segment object */
-  ssvm_shared_header_t *shared_header;
-  clib_spinlock_t *stat_segment_lockp;	/* Spinlock for the stats segment */
-  uword *counter_vector_by_name;
-  u64 *error_base;
+  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;
 
 stat_client_main_t stat_client_main;
 
+static int
+recv_fd (int sock)
+{
+  struct msghdr msg = { 0 };
+  struct cmsghdr *cmsg;
+  int fd = -1;
+  char iobuf[1];
+  struct iovec io = {.iov_base = iobuf,.iov_len = sizeof (iobuf) };
+  union
+  {
+    char buf[CMSG_SPACE (sizeof (fd))];
+    struct cmsghdr align;
+  } u;
+  msg.msg_iov = &io;
+  msg.msg_iovlen = 1;
+  msg.msg_control = u.buf;
+  msg.msg_controllen = sizeof (u.buf);
+
+  ssize_t size;
+  if ((size = recvmsg (sock, &msg, 0)) < 0)
+    {
+      perror ("recvmsg failed");
+      return -1;
+    }
+  cmsg = CMSG_FIRSTHDR (&msg);
+  if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
+    {
+      memmove (&fd, CMSG_DATA (cmsg), sizeof (fd));
+    }
+  return fd;
+}
+
+static stat_segment_directory_entry_t *
+get_stat_vector (void)
+{
+  stat_client_main_t *sm = &stat_client_main;
+  ASSERT (sm->shared_header);
+  return stat_segment_pointer (sm->shared_header,
+			       sm->shared_header->directory_offset);
+}
+
 int
 stat_segment_connect (char *socket_name)
 {
   stat_client_main_t *sm = &stat_client_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  clib_socket_t s = { 0 };
-  clib_error_t *err;
-  int fd = -1, retval;
+  int mfd = -1;
+  int sock;
 
   memset (sm, 0, sizeof (*sm));
-  s.config = socket_name;
-  s.flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;
-  err = clib_socket_init (&s);
-  if (err)
+  if ((sock = socket (AF_UNIX, SOCK_SEQPACKET, 0)) < 0)
     {
-      clib_error_report (err);
-      return -1;
-    }
-  err = clib_socket_recvmsg (&s, 0, 0, &fd, 1);
-  if (err)
-    {
-      clib_error_report (err);
-      return -1;
-    }
-  clib_socket_close (&s);
-
-  memset (ssvmp, 0, sizeof (*ssvmp));
-  ssvmp->fd = fd;
-
-  /* Note: this closes memfd.fd */
-  retval = ssvm_slave_init_memfd (ssvmp);
-  if (retval)
-    {
-      fprintf (stderr, "WARNING: segment map returned %d\n", retval);
+      perror ("Couldn't open socket");
       return -1;
     }
 
-  ASSERT (ssvmp && ssvmp->sh);
+  struct sockaddr_un un = { 0 };
+  un.sun_family = AF_UNIX;
+  strncpy ((char *) un.sun_path, socket_name, sizeof (un.sun_path) - 1);
+  if (connect (sock, (struct sockaddr *) &un, sizeof (struct sockaddr_un)) <
+      0)
+    {
+      perror ("connect");
+      return -1;
+    }
 
-  /* Pick up the segment lock from the shared memory header */
-  sm->shared_header = ssvmp->sh;
-  sm->stat_segment_lockp = (clib_spinlock_t *) (sm->shared_header->opaque[0]);
-  sm->segment_ready = 1;
+  if ((mfd = recv_fd (sock)) < 0)
+    {
+      fprintf (stderr, "Receiving file descriptor failed\n");
+      return -1;
+    }
+  close (sock);
 
-  sm->counter_vector_by_name =
-    (uword *) sm->shared_header->opaque[STAT_SEGMENT_OPAQUE_DIR];
+  /* mmap shared memory segment. */
+  void *memaddr;
+  struct stat st = { 0 };
+
+  if (fstat (mfd, &st) == -1)
+    {
+      perror ("mmap");
+      return -1;
+    }
+  if ((memaddr =
+       mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, mfd, 0)) == MAP_FAILED)
+    {
+      perror ("mmap");
+      return -1;
+    }
+
+  sm->memory_size = st.st_size;
+  sm->shared_header = memaddr;
+  sm->directory_vector =
+    stat_segment_pointer (memaddr, sm->shared_header->directory_offset);
 
   return 0;
 }
@@ -91,146 +141,72 @@
 stat_segment_disconnect (void)
 {
   stat_client_main_t *sm = &stat_client_main;
-  ssvm_delete_memfd (&sm->stat_segment);
+  munmap (sm->shared_header, sm->memory_size);
+
   return;
 }
 
-/*
- * The application needs to register which counters it is interested
- * in.
- */
-stat_segment_cached_pointer_t *
-stat_segment_register (u8 * stats[])
-{
-  int i;
-  uword *p;
-  stat_client_main_t *sm = &stat_client_main;
-  stat_segment_cached_pointer_t *cp, *cached_pointer_vec = 0;
-
-  for (i = 0; i < vec_len (stats); i++)
-    {
-      p = hash_get_mem (sm->counter_vector_by_name, stats[i]);
-      if (p == 0)
-	{
-	  fprintf (stderr, "WARN: %s not in directory!\n", stats[i]);
-	  continue;
-	}
-      vec_add2 (cached_pointer_vec, cp, 1);
-      cp->name = strdup ((char *) stats[i]);	// Point to p->key instead?
-    }
-  return cached_pointer_vec;
-}
-
-static u64 *
-get_error_base (u32 thread_index)
-{
-  u64 *error_base = 0;
-  uword *p;
-  stat_client_main_t *sm = &stat_client_main;
-  stat_segment_directory_entry_t *ep;
-
-  /* Special case /err/0/counter_vector */
-  p = hash_get_mem (sm->counter_vector_by_name,
-		    format (0, "/err/%d/counter_vector", thread_index));
-  if (p)
-    {
-      ep = (stat_segment_directory_entry_t *) (p[0]);
-      error_base = ep->value;
-    }
-  return error_base;
-}
-
-f64
+double
 stat_segment_heartbeat (void)
 {
-  f64 *heartbeat = 0;
-  uword *p;
   stat_client_main_t *sm = &stat_client_main;
-  stat_segment_directory_entry_t *ep;
-
-  /* Special case /err/0/counter_vector */
-  p = hash_get_mem (sm->counter_vector_by_name,
-		    format (0, "/sys/heartbeat%c", 0));
-  if (p)
-    {
-      ep = (stat_segment_directory_entry_t *) (p[0]);
-      heartbeat = ep->value;
-    }
-  return *heartbeat;
-}
-
-static void
-maybe_update_cached_pointers (stat_segment_cached_pointer_t * cached_pointers)
-{
-  stat_client_main_t *sm = &stat_client_main;
-  stat_segment_cached_pointer_t *cp;
-  uword *p;
-  int i;
-
-  /* Cached pointers OK? */
-  if (sm->current_epoch ==
-      (u64) sm->shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH])
-    return;
-
-  /* Special case /err/0/counter_vector */
-  sm->error_base = get_error_base (0);
-
-  /* Nope, fix them... */
-  for (i = 0; i < vec_len (cached_pointers); i++)
-    {
-      cp = &cached_pointers[i];
-
-      p = hash_get_mem (sm->counter_vector_by_name, cp->name);
-      if (p == 0)
-	{
-	  fprintf (stderr, "WARN: %s not in directory!\n", cp->name);
-	  continue;
-	}
-      cp->ep = (stat_segment_directory_entry_t *) (p[0]);
-    }
-
-  /* And remember that we did... */
-  sm->current_epoch =
-    (u64) sm->shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH];
+  stat_segment_directory_entry_t *vec = get_stat_vector ();
+  double *hb = stat_segment_pointer (sm->shared_header, vec[4].offset);
+  return *hb;
 }
 
 stat_segment_data_t
-copy_data (stat_segment_directory_entry_t * ep, u64 * error_base, char *name)
+copy_data (stat_segment_directory_entry_t * ep)
 {
+  stat_client_main_t *sm = &stat_client_main;
   stat_segment_data_t result = { 0 };
-  u32 error_index;
   int i;
   vlib_counter_t **combined_c;	/* Combined counter */
   counter_t **simple_c;		/* Simple counter */
+  counter_t *error_base;
+  uint64_t *offset_vector;
+
+  assert (sm->shared_header);
+
   result.type = ep->type;
-  result.name = name;
+  result.name = strdup (ep->name);
   switch (ep->type)
     {
-    case STAT_DIR_TYPE_SCALAR_POINTER:
-      result.scalar_value = *(f64 *) ep->value;
-      break;
-
-    case STAT_DIR_TYPE_VECTOR_POINTER:
-      result.vector_pointer = ep->value;
+    case STAT_DIR_TYPE_SCALAR_INDEX:
+      result.scalar_value = ep->value;
       break;
 
     case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
-      simple_c = ep->value;
+      simple_c = stat_segment_pointer (sm->shared_header, ep->offset);
       result.simple_counter_vec = vec_dup (simple_c);
+      offset_vector =
+	stat_segment_pointer (sm->shared_header, ep->offset_vector);
       for (i = 0; i < vec_len (simple_c); i++)
-	result.simple_counter_vec[i] = vec_dup (simple_c[i]);
+	{
+	  counter_t *cb =
+	    stat_segment_pointer (sm->shared_header, offset_vector[i]);
+	  result.simple_counter_vec[i] = vec_dup (cb);
+	}
       break;
 
     case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
-      combined_c = ep->value;
+      combined_c = stat_segment_pointer (sm->shared_header, ep->offset);
       result.combined_counter_vec = vec_dup (combined_c);
+      offset_vector =
+	stat_segment_pointer (sm->shared_header, ep->offset_vector);
       for (i = 0; i < vec_len (combined_c); i++)
-	result.combined_counter_vec[i] = vec_dup (combined_c[i]);
+	{
+	  vlib_counter_t *cb =
+	    stat_segment_pointer (sm->shared_header, offset_vector[i]);
+	  result.combined_counter_vec[i] = vec_dup (cb);
+	}
       break;
 
     case STAT_DIR_TYPE_ERROR_INDEX:
-      error_index = (uintptr_t) ep->value;
-      result.error_value = error_base[error_index];
+      error_base =
+	stat_segment_pointer (sm->shared_header,
+			      sm->shared_header->error_offset);
+      result.error_value = error_base[ep->index];
       break;
 
     default:
@@ -239,32 +215,6 @@
   return result;
 }
 
-stat_segment_data_t *
-stat_segment_collect (stat_segment_cached_pointer_t * cached_pointers)
-{
-  stat_client_main_t *sm = &stat_client_main;
-  stat_segment_data_t *res = 0;
-  int i;
-
-  /* Grab the stats segment lock */
-  clib_spinlock_lock (sm->stat_segment_lockp);
-
-  /* see if we need to update cached pointers */
-  maybe_update_cached_pointers (cached_pointers);
-
-  for (i = 0; i < vec_len (cached_pointers); i++)
-    {
-      vec_add1 (res,
-		copy_data (cached_pointers[i].ep, sm->error_base,
-			   cached_pointers[i].name));
-    }
-
-  /* Drop the lock */
-  clib_spinlock_unlock (sm->stat_segment_lockp);
-
-  return res;
-}
-
 void
 stat_segment_data_free (stat_segment_data_t * res)
 {
@@ -286,19 +236,48 @@
 	default:
 	  ;
 	}
+      free (res[i].name);
     }
   vec_free (res);
 }
 
-u8 **
-stat_segment_ls (u8 ** patterns)
+
+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_client_main;
-  hash_pair_t *p;
-  u8 **dir = 0;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+  sa->epoch = shared_header->epoch;
+  while (shared_header->in_progress != 0)
+    ;
+}
+
+static bool
+stat_segment_access_end (stat_segment_access_t * sa)
+{
+  stat_client_main_t *sm = &stat_client_main;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+
+  if (shared_header->epoch != sa->epoch || shared_header->in_progress)
+    return false;
+  return true;
+}
+
+uint32_t *
+stat_segment_ls (uint8_t ** patterns)
+{
+  stat_client_main_t *sm = &stat_client_main;
+  stat_segment_access_t sa;
+
+  uint32_t *dir = 0;
   regex_t regex[vec_len (patterns)];
 
-  int i;
+  int i, j;
   for (i = 0; i < vec_len (patterns); i++)
     {
       int rv = regcomp (&regex[i], (char *) patterns[i], 0);
@@ -309,58 +288,67 @@
 	}
     }
 
-  clib_spinlock_lock (sm->stat_segment_lockp);
+  stat_segment_access_start (&sa);
 
-  /* *INDENT-OFF* */
-  hash_foreach_pair (p, sm->counter_vector_by_name,
-  ({
-    for (i = 0; i < vec_len(patterns); i++) {
-      int rv = regexec(&regex[i], (char *)p->key, 0, NULL, 0);
-      if (rv == 0) {
-	vec_add1 (dir, (u8 *)p->key);
-	break;
-      }
+  stat_segment_directory_entry_t *counter_vec = get_stat_vector ();
+  for (j = 0; j < vec_len (counter_vec); j++)
+    {
+      for (i = 0; i < vec_len (patterns); i++)
+	{
+	  int rv = regexec (&regex[i], counter_vec[j].name, 0, NULL, 0);
+	  if (rv == 0)
+	    {
+	      vec_add1 (dir, j);
+	      break;
+	    }
+	}
+      if (vec_len (patterns) == 0)
+	vec_add1 (dir, j);
     }
-    if (vec_len(patterns) == 0)
-      vec_add1 (dir, (u8 *)p->key);
-  }));
-  /* *INDENT-ON* */
-
-  clib_spinlock_unlock (sm->stat_segment_lockp);
 
   for (i = 0; i < vec_len (patterns); i++)
     regfree (&regex[i]);
 
+  if (!stat_segment_access_end (&sa))
+    {
+      /* Failed, clean up */
+      vec_free (dir);
+      return 0;
+
+    }
+
+  /* Update last version */
+  sm->current_epoch = sa.epoch;
   return dir;
 }
 
 stat_segment_data_t *
-stat_segment_dump (u8 * stats[])
+stat_segment_dump (uint32_t * stats)
 {
   int i;
-  uword *p;
   stat_client_main_t *sm = &stat_client_main;
   stat_segment_directory_entry_t *ep;
   stat_segment_data_t *res = 0;
+  stat_segment_access_t sa;
 
-  clib_spinlock_lock (sm->stat_segment_lockp);
+  /* Has directory been update? */
+  if (sm->shared_header->epoch != sm->current_epoch)
+    return 0;
 
-  sm->error_base = get_error_base (0);
+  stat_segment_access_start (&sa);
   for (i = 0; i < vec_len (stats); i++)
     {
-      p = hash_get_mem (sm->counter_vector_by_name, stats[i]);
-      if (p == 0)
-	{
-	  fprintf (stderr, "WARN: %s not in directory!\n", stats[i]);
-	  continue;
-	}
       /* Collect counter */
-      ep = (stat_segment_directory_entry_t *) (p[0]);
-      vec_add1 (res, copy_data (ep, sm->error_base, (char *) stats[i]));
+      ep = vec_elt_at_index (sm->directory_vector, stats[i]);
+      vec_add1 (res, copy_data (ep));
     }
-  clib_spinlock_unlock (sm->stat_segment_lockp);
 
-  return res;
+  if (stat_segment_access_end (&sa))
+    return res;
+
+  fprintf (stderr, "Epoch changed while reading, invalid results\n");
+  // TODO increase counter
+  return 0;
 }
 
 /* Wrapper for accessing vectors from other languages */
@@ -380,6 +368,36 @@
   return string_vector;
 }
 
+stat_segment_data_t *
+stat_segment_dump_entry (uint32_t index)
+{
+  stat_client_main_t *sm = &stat_client_main;
+  stat_segment_directory_entry_t *ep;
+  stat_segment_data_t *res = 0;
+  stat_segment_access_t sa;
+
+  stat_segment_access_start (&sa);
+
+  /* Collect counter */
+  ep = vec_elt_at_index (sm->directory_vector, index);
+  vec_add1 (res, copy_data (ep));
+
+  if (stat_segment_access_end (&sa))
+    return res;
+  return 0;
+}
+
+char *
+stat_segment_index_to_name (uint32_t index)
+{
+  char *name;
+  stat_segment_directory_entry_t *counter_vec = get_stat_vector ();
+  stat_segment_directory_entry_t *ep;
+  ep = vec_elt_at_index (counter_vec, index);
+  name = strdup (ep->name);
+  return name;
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vpp-api/client/stat_client.h b/src/vpp-api/client/stat_client.h
index 73e91b1..c1a0ecf 100644
--- a/src/vpp-api/client/stat_client.h
+++ b/src/vpp-api/client/stat_client.h
@@ -17,8 +17,9 @@
 #ifndef included_stat_client_h
 #define included_stat_client_h
 
-#include <vlib/vlib.h>
-#include <vpp/stats/stats.h>
+#include <stdint.h>
+#include <vpp/stats/stat_segment.h>
+#include <vlib/counter_types.h>
 
 typedef struct
 {
@@ -26,31 +27,25 @@
   stat_directory_type_t type;
   union
   {
-    f64 scalar_value;
-    u64 error_value;
-    u64 *vector_pointer;
+    double scalar_value;
+    uint64_t error_value;
     counter_t **simple_counter_vec;
     vlib_counter_t **combined_counter_vec;
   };
 } stat_segment_data_t;
 
-typedef struct
-{
-  char *name;
-  stat_segment_directory_entry_t *ep;
-} stat_segment_cached_pointer_t;
-
 int stat_segment_connect (char *socket_name);
 void stat_segment_disconnect (void);
-u8 **stat_segment_ls (u8 ** pattern);
-stat_segment_data_t *stat_segment_dump (u8 ** counter_vec);
-stat_segment_cached_pointer_t *stat_segment_register (u8 ** counter_vec);
-/* Collects registered counters */
-stat_segment_data_t *stat_segment_collect (stat_segment_cached_pointer_t *);
-void stat_segment_data_free (stat_segment_data_t * res);
-f64 stat_segment_heartbeat (void);
-u8 **stat_segment_string_vector (u8 ** string_vector, char *string);
+uint8_t **stat_segment_string_vector (uint8_t ** string_vector, char *string);
 int stat_segment_vec_len (void *vec);
+uint32_t *stat_segment_ls (uint8_t ** pattern);
+stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
+stat_segment_data_t *stat_segment_dump_entry (uint32_t index);
+void stat_segment_data_free (stat_segment_data_t * res);
+
+double stat_segment_heartbeat (void);
+
+char *stat_segment_index_to_name (uint32_t index);
 
 #endif /* included_stat_client_h */
 
diff --git a/src/vpp-api/python/vpp_papi/vpp_stats.py b/src/vpp-api/python/vpp_papi/vpp_stats.py
index b84a0e5..9bdb79f 100644
--- a/src/vpp-api/python/vpp_papi/vpp_stats.py
+++ b/src/vpp-api/python/vpp_papi/vpp_stats.py
@@ -13,44 +13,42 @@
 
 typedef enum {
   STAT_DIR_TYPE_ILLEGAL = 0,
-  STAT_DIR_TYPE_SCALAR_POINTER,
-  STAT_DIR_TYPE_VECTOR_POINTER,
+  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_SERIALIZED_NODES,
 } stat_directory_type_t;
 
-typedef struct {
-  stat_directory_type_t type;
-  void *value;
-} stat_segment_directory_entry_t;
-
-typedef struct {
-  char *name;
+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;
+
+typedef struct
+{
+  char *name;
+  stat_directory_type_t type;
+  union
+  {
     double scalar_value;
     uint64_t error_value;
-    uint64_t *vector_pointer;
     counter_t **simple_counter_vec;
     vlib_counter_t **combined_counter_vec;
   };
 } stat_segment_data_t;
 
-typedef struct {
-  char *name;
-  stat_segment_directory_entry_t *ep;
-} stat_segment_cached_pointer_t;
-
 int stat_segment_connect (char *socket_name);
 void stat_segment_disconnect (void);
 
-uint8_t  **stat_segment_ls (uint8_t **pattern);
-stat_segment_data_t *stat_segment_dump (uint8_t ** counter_vec);
-/* Collects registered counters */
-stat_segment_cached_pointer_t *stat_segment_register (uint8_t ** counter_vec);
-stat_segment_data_t *stat_segment_collect (stat_segment_cached_pointer_t *);
+uint32_t *stat_segment_ls (uint8_t ** pattern);
+stat_segment_data_t *stat_segment_dump (uint32_t * counter_vec);
 void stat_segment_data_free (stat_segment_data_t * res);
 double stat_segment_heartbeat (void);
 int stat_segment_vec_len(void *vec);
@@ -100,11 +98,15 @@
 
 
 def stat_entry_to_python(api, e):
+    # Scalar index
+    if e.type == 1:
+        return e.scalar_value
+        return None
+    if e.type == 2:
+        return simple_counter_vec_list(api, e.simple_counter_vec)
     if e.type == 3:
-        return simple_counter_vec_list(e.simple_counter_vec)
-    if e.type == 4:
         return combined_counter_vec_list(api, e.combined_counter_vec)
-    if e.type == 5:
+    if e.type == 4:
         return e.error_value
     return None
 
@@ -129,12 +131,12 @@
         for i in range(rv_len):
             n = ffi.string(rv[i].name)
             e = stat_entry_to_python(self.api, rv[i])
-            if e:
-                stats[n] = e
+            stats[n] = e
         return stats
 
-    def dump_str(self, counters_str):
-        return self.dump(make_string_vector(self.api, counters_str))
+    def get_counter(self, name):
+        dir = self.ls(name)
+        return self.dump(dir).values()[0]
 
     def disconnect(self):
         self.api.stat_segment_disconnect()
@@ -153,9 +155,3 @@
         for k in sorted(error_counters):
             s += '{:<60}{:>10}\n'.format(k, error_counters[k])
         return s
-
-    def register(self):
-        raise NotImplemented
-
-    def collect(self):
-        raise NotImplemented
diff --git a/src/vpp/CMakeLists.txt b/src/vpp/CMakeLists.txt
index feb75cf..999b280 100644
--- a/src/vpp/CMakeLists.txt
+++ b/src/vpp/CMakeLists.txt
@@ -113,9 +113,8 @@
 
 add_vpp_executable(vpp_get_stats
   SOURCES app/vpp_get_stats.c
-  LINK_LIBRARIES vppapiclient vppinfra svm vlibmemoryclient
+  LINK_LIBRARIES vppapiclient vppinfra
   DEPENDS api_headers
-  NO_INSTALL
 )
 
 add_vpp_executable(vpp_prometheus_export
diff --git a/src/vpp/app/vpp_get_stats.c b/src/vpp/app/vpp_get_stats.c
index 908e675..c1a5acb 100644
--- a/src/vpp/app/vpp_get_stats.c
+++ b/src/vpp/app/vpp_get_stats.c
@@ -22,12 +22,17 @@
 #include <vpp/stats/stats.h>
 
 static int
-stat_poll_loop (stat_segment_cached_pointer_t * cp)
+stat_poll_loop (u8 ** patterns)
 {
   struct timespec ts, tsrem;
   stat_segment_data_t *res;
   int i, j, k, lost_connection = 0;
   f64 heartbeat, prev_heartbeat = 0;
+  u32 *stats = stat_segment_ls (patterns);
+  if (!stats)
+    {
+      return -1;
+    }
 
   printf ("\033[2J");		/*  clear the screen  */
   while (1)
@@ -49,7 +54,12 @@
 	}
 
       printf ("\033[H");	/* Cursor top left corner */
-      res = stat_segment_collect (cp);
+      res = stat_segment_dump (stats);
+      if (!res)
+	{
+	  stats = stat_segment_ls (patterns);
+	  continue;
+	}
       for (i = 0; i < vec_len (res); i++)
 	{
 	  switch (res[i].type)
@@ -57,24 +67,24 @@
 	    case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
 	      for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
 		for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
-		  fformat (stdout, "[%d]: %lld packets %s\n",
+		  fformat (stdout, "[%d]: %llu packets %s\n",
 			   j, res[i].simple_counter_vec[k][j], res[i].name);
 	      break;
 
 	    case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
 	      for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
 		for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
-		  fformat (stdout, "[%d]: %lld packets, %lld bytes %s\n",
+		  fformat (stdout, "[%d]: %llu packets, %llu bytes %s\n",
 			   j, res[i].combined_counter_vec[k][j].packets,
 			   res[i].combined_counter_vec[k][j].bytes,
 			   res[i].name);
 	      break;
 
 	    case STAT_DIR_TYPE_ERROR_INDEX:
-	      fformat (stdout, "%lld %s\n", res[i].error_value, res[i].name);
+	      fformat (stdout, "%llu %s\n", res[i].error_value, res[i].name);
 	      break;
 
-	    case STAT_DIR_TYPE_SCALAR_POINTER:
+	    case STAT_DIR_TYPE_SCALAR_INDEX:
 	      fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
 	      break;
 
@@ -99,6 +109,7 @@
   STAT_CLIENT_CMD_LS,
   STAT_CLIENT_CMD_POLL,
   STAT_CLIENT_CMD_DUMP,
+  STAT_CLIENT_CMD_TIGHTPOLL,
 };
 
 int
@@ -108,10 +119,9 @@
   u8 *stat_segment_name, *pattern = 0, **patterns = 0;
   int rv;
   enum stat_client_cmd_e cmd = STAT_CLIENT_CMD_UNKNOWN;
-  void *heap_base;
 
-  heap_base = clib_mem_vm_map ((void *) 0x10000000ULL, 128 << 20);
-  clib_mem_init (heap_base, 128 << 20);
+  /* Create a heap of 64MB */
+  clib_mem_init (0, 64 << 20);
 
   unformat_init_command_line (a, argv);
 
@@ -133,6 +143,10 @@
 	{
 	  cmd = STAT_CLIENT_CMD_POLL;
 	}
+      else if (unformat (a, "tightpoll"))
+	{
+	  cmd = STAT_CLIENT_CMD_TIGHTPOLL;
+	}
       else if (unformat (a, "%s", &pattern))
 	{
 	  vec_add1 (patterns, pattern);
@@ -154,10 +168,9 @@
       exit (1);
     }
 
-  u8 **dir;
+  u32 *dir;
   int i, j, k;
   stat_segment_data_t *res;
-  stat_segment_cached_pointer_t *cp;
 
   dir = stat_segment_ls (patterns);
 
@@ -167,7 +180,9 @@
       /* List all counters */
       for (i = 0; i < vec_len (dir); i++)
 	{
-	  printf ("%s\n", (char *) dir[i]);
+	  char *n = stat_segment_index_to_name (dir[i]);
+	  printf ("%s\n", n);
+	  free (n);
 	}
       break;
 
@@ -180,7 +195,7 @@
 	    case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
 	      for (k = 0; k < vec_len (res[i].simple_counter_vec) - 1; k++)
 		for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
-		  fformat (stdout, "[%d @ %d]: %lld packets %s\n",
+		  fformat (stdout, "[%d @ %d]: %llu packets %s\n",
 			   j, k, res[i].simple_counter_vec[k][j],
 			   res[i].name);
 	      break;
@@ -188,19 +203,18 @@
 	    case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
 	      for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
 		for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
-		  fformat (stdout, "[%d @ %d]: %lld packets, %lld bytes %s\n",
+		  fformat (stdout, "[%d @ %d]: %llu packets, %llu bytes %s\n",
 			   j, k, res[i].combined_counter_vec[k][j].packets,
 			   res[i].combined_counter_vec[k][j].bytes,
 			   res[i].name);
 	      break;
 
 	    case STAT_DIR_TYPE_ERROR_INDEX:
-	      fformat (stdout, "%lld %s\n", res[i].error_value, dir[i]);
+	      fformat (stdout, "%llu %s\n", res[i].error_value, res[i].name);
 	      break;
 
-	    case STAT_DIR_TYPE_SCALAR_POINTER:
-	      fformat (stdout, "%.2f %s\n", dir[i], res[i].scalar_value,
-		       res[i].name);
+	    case STAT_DIR_TYPE_SCALAR_INDEX:
+	      fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
 	      break;
 
 	    default:
@@ -211,19 +225,27 @@
       break;
 
     case STAT_CLIENT_CMD_POLL:
-      cp = stat_segment_register (dir);
-      if (!cp)
-	{
-	  fformat (stderr,
-		   "Couldn't register required counters with stat segment\n");
-	  exit (1);
-	}
-      stat_poll_loop (cp);
+      stat_poll_loop (patterns);
       /* We can only exist the pool loop if we lost connection to VPP */
       stat_segment_disconnect ();
       goto reconnect;
       break;
 
+    case STAT_CLIENT_CMD_TIGHTPOLL:
+      while (1)
+	{
+	  res = stat_segment_dump (dir);
+	  if (res == 0)
+	    {
+	      /* Refresh */
+	      vec_free (dir);
+	      dir = stat_segment_ls (patterns);
+	      continue;
+	    }
+	  stat_segment_data_free (res);
+	}
+      break;
+
     default:
       fformat (stderr,
 	       "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
diff --git a/src/vpp/app/vpp_prometheus_export.c b/src/vpp/app/vpp_prometheus_export.c
index 4fd749a..65e0147 100644
--- a/src/vpp/app/vpp_prometheus_export.c
+++ b/src/vpp/app/vpp_prometheus_export.c
@@ -30,7 +30,6 @@
 #include <sys/socket.h>
 #include <vpp-api/client/stat_client.h>
 #include <vlib/vlib.h>
-#include <vpp/stats/stats.h>
 #include <ctype.h>
 
 /* https://github.com/prometheus/prometheus/wiki/Default-port-allocations */
@@ -50,12 +49,22 @@
 }
 
 static void
-dump_metrics (FILE * stream, stat_segment_cached_pointer_t * cp)
+dump_metrics (FILE * stream, u8 ** patterns)
 {
   stat_segment_data_t *res;
   int i, j, k;
+  static u32 *stats = 0;
 
-  res = stat_segment_collect (cp);
+retry:
+  res = stat_segment_dump (stats);
+  if (res == 0)
+    {				/* Memory layout has changed */
+      if (stats)
+	vec_free (stats);
+      stats = stat_segment_ls (patterns);
+      goto retry;
+    }
+
   for (i = 0; i < vec_len (res); i++)
     {
       switch (res[i].type)
@@ -93,7 +102,7 @@
 		   prom_string (res[i].name), res[i].error_value);
 	  break;
 
-	case STAT_DIR_TYPE_SCALAR_POINTER:
+	case STAT_DIR_TYPE_SCALAR_INDEX:
 	  fformat (stream, "# TYPE %s counter\n", prom_string (res[i].name));
 	  fformat (stream, "%s %.2f\n", prom_string (res[i].name),
 		   res[i].scalar_value);
@@ -113,7 +122,7 @@
 #define NOT_FOUND_ERROR "<html><head><title>Document not found</title></head><body><h1>404 - Document not found</h1></body></html>"
 
 static void
-http_handler (FILE * stream, stat_segment_cached_pointer_t * cp)
+http_handler (FILE * stream, u8 ** patterns)
 {
   char status[80] = { 0 };
   if (fgets (status, sizeof (status) - 1, stream) == 0)
@@ -165,7 +174,7 @@
       return;
     }
   fputs ("HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n", stream);
-  dump_metrics (stream, cp);
+  dump_metrics (stream, patterns);
 }
 
 static int
@@ -223,10 +232,9 @@
   unformat_input_t _argv, *a = &_argv;
   u8 *stat_segment_name, *pattern = 0, **patterns = 0;
   int rv;
-  void *heap_base;
 
-  heap_base = clib_mem_vm_map ((void *) 0x10000000ULL, 128 << 20);
-  clib_mem_init (heap_base, 128 << 20);
+  /* Allocating 32MB heap */
+  clib_mem_init (0, 32 << 20);
 
   unformat_init_command_line (a, argv);
 
@@ -257,18 +265,6 @@
       exit (1);
     }
 
-  u8 **dir;
-  stat_segment_cached_pointer_t *cp;
-
-  dir = stat_segment_ls (patterns);
-  cp = stat_segment_register (dir);
-  if (!cp)
-    {
-      fformat (stderr,
-	       "Couldn't register required counters with stat segment\n");
-      exit (1);
-    }
-
   int fd = start_listen (SERVER_PORT);
   if (fd < 0)
     {
@@ -291,8 +287,8 @@
 	  if (inet_ntop
 	      (AF_INET6, &clientaddr.sin6_addr, address, sizeof (address)))
 	    {
-	      printf ("Client address is [%s]:%d\n", address,
-		      ntohs (clientaddr.sin6_port));
+	      fprintf (stderr, "Client address is [%s]:%d\n", address,
+		       ntohs (clientaddr.sin6_port));
 	    }
 	}
 
@@ -304,7 +300,7 @@
 	  continue;
 	}
       /* Single reader at the moment */
-      http_handler (stream, cp);
+      http_handler (stream, patterns);
       fclose (stream);
     }
 
diff --git a/src/vpp/stats/stat_segment.c b/src/vpp/stats/stat_segment.c
index dcaeb50..7bf6624 100644
--- a/src/vpp/stats/stat_segment.c
+++ b/src/vpp/stats/stat_segment.c
@@ -12,47 +12,64 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <vpp/stats/stats.h>
 
+#include <vppinfra/mem.h>
+#include <vpp/stats/stats.h>
+#undef HAVE_MEMFD_CREATE
+#include <vppinfra/linux/syscall.h>
+
+/*
+ *  Used only by VPP writers
+ */
 void
 vlib_stat_segment_lock (void)
 {
   stats_main_t *sm = &stats_main;
-  vlib_main_t *vm = vlib_get_main ();
-  f64 deadman;
-
-  /* 3ms is WAY long enough to be reasonably sure something is wrong */
-  deadman = vlib_time_now (vm) + 3e-3;
-
-  while (__sync_lock_test_and_set (&((*sm->stat_segment_lockp)->lock), 1))
-    {
-      if (vlib_time_now (vm) >= deadman)
-	{
-	  clib_warning ("BUG: stat segment lock held too long...");
-	  break;
-	}
-    }
+  clib_spinlock_lock (sm->stat_segment_lockp);
+  sm->shared_header->in_progress = 1;
 }
 
 void
 vlib_stat_segment_unlock (void)
 {
   stats_main_t *sm = &stats_main;
+  sm->shared_header->epoch++;
+  sm->shared_header->in_progress = 0;
   clib_spinlock_unlock (sm->stat_segment_lockp);
 }
 
+/*
+ * Change heap to the stats shared memory segment
+ */
 void *
 vlib_stats_push_heap (void)
 {
   stats_main_t *sm = &stats_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  ssvm_shared_header_t *shared_header;
 
-  ASSERT (ssvmp && ssvmp->sh);
+  ASSERT (sm && sm->shared_header);
+  return clib_mem_set_heap (sm->heap);
+}
 
-  shared_header = ssvmp->sh;
+/* Name to vector index hash */
+static u32
+lookup_or_create_hash_index (void *oldheap, char *name, u32 next_vector_index)
+{
+  stats_main_t *sm = &stats_main;
+  u32 index;
+  hash_pair_t *hp;
 
-  return ssvm_push_heap (shared_header);
+  hp = hash_get_pair (sm->directory_vector_by_name, name);
+  if (!hp)
+    {
+      hash_set (sm->directory_vector_by_name, name, next_vector_index);
+      index = next_vector_index;
+    }
+  else
+    {
+      index = hp->value[0];
+    }
+
+  return index;
 }
 
 void
@@ -60,250 +77,216 @@
 {
   vlib_simple_counter_main_t *cm = (vlib_simple_counter_main_t *) cm_arg;
   stats_main_t *sm = &stats_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  ssvm_shared_header_t *shared_header;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
   char *stat_segment_name;
-  stat_segment_directory_entry_t *ep;
-  uword *p;
-
-  ASSERT (ssvmp && ssvmp->sh);
-
-  shared_header = ssvmp->sh;
+  stat_segment_directory_entry_t e = { 0 };
 
   /* Not all counters have names / hash-table entries */
-  if (cm->name || cm->stat_segment_name)
+  if (!cm->name && !cm->stat_segment_name)
     {
-      hash_pair_t *hp;
-      u8 *name_copy;
-
-      stat_segment_name = cm->stat_segment_name ?
-	cm->stat_segment_name : cm->name;
-
-      vlib_stat_segment_lock ();
-
-      /* Update hash table. The name must be copied into the segment */
-      hp = hash_get_pair (sm->counter_vector_by_name, stat_segment_name);
-      if (hp)
-	{
-	  name_copy = (u8 *) hp->key;
-	  ep = (stat_segment_directory_entry_t *) (hp->value[0]);
-	  hash_unset_mem (sm->counter_vector_by_name, stat_segment_name);
-	  vec_free (name_copy);
-	  clib_mem_free (ep);
-	}
-      name_copy = format (0, "%s%c", stat_segment_name, 0);
-      ep = clib_mem_alloc (sizeof (*ep));
-      ep->type = type;
-      ep->value = cm->counters;
-      hash_set_mem (sm->counter_vector_by_name, name_copy, ep);
-
-      /* Reset the client hash table pointer, since it WILL change! */
-      shared_header->opaque[STAT_SEGMENT_OPAQUE_DIR]
-	= sm->counter_vector_by_name;
-
-      /* Warn clients to refresh any pointers they might be holding */
-      shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] = (void *)
-	((u64) shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] + 1);
-      vlib_stat_segment_unlock ();
+      clib_mem_set_heap (oldheap);
+      return;
     }
-  ssvm_pop_heap (oldheap);
+
+  ASSERT (shared_header);
+
+  vlib_stat_segment_lock ();
+
+  /* Lookup hash-table is on the main heap */
+  stat_segment_name =
+    cm->stat_segment_name ? cm->stat_segment_name : cm->name;
+  u32 next_vector_index = vec_len (sm->directory_vector);
+  clib_mem_set_heap (oldheap);	/* Exit stats segment */
+  u32 vector_index = lookup_or_create_hash_index (oldheap, stat_segment_name,
+						  next_vector_index);
+  /* Back to stats segment */
+  clib_mem_set_heap (sm->heap);	/* Re-enter stat segment */
+
+
+  /* Update the vector */
+  if (vector_index == next_vector_index)
+    {				/* New */
+      strncpy (e.name, stat_segment_name, 128 - 1);
+      e.type = type;
+      vec_add1 (sm->directory_vector, e);
+      vector_index++;
+    }
+
+  stat_segment_directory_entry_t *ep = &sm->directory_vector[vector_index];
+  ep->offset = stat_segment_offset (shared_header, cm->counters);	/* Vector of threads of vectors of counters */
+  u64 *offset_vector =
+    ep->offset_vector ? stat_segment_pointer (shared_header,
+					      ep->offset_vector) : 0;
+
+  /* Update the 2nd dimension offset vector */
+  int i;
+  vec_validate (offset_vector, vec_len (cm->counters) - 1);
+  for (i = 0; i < vec_len (cm->counters); i++)
+    offset_vector[i] = stat_segment_offset (shared_header, cm->counters[i]);
+  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
+  sm->directory_vector[vector_index].offset =
+    stat_segment_offset (shared_header, cm->counters);
+
+  /* Reset the client hash table pointer, since it WILL change! */
+  shared_header->directory_offset =
+    stat_segment_offset (shared_header, sm->directory_vector);
+
+  vlib_stat_segment_unlock ();
+  clib_mem_set_heap (oldheap);
 }
 
 void
-vlib_stats_register_error_index (u8 * name, u64 index)
+vlib_stats_register_error_index (u8 * name, u64 * em_vec, u64 index)
 {
   stats_main_t *sm = &stats_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  ssvm_shared_header_t *shared_header;
-  stat_segment_directory_entry_t *ep;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+  stat_segment_directory_entry_t e;
   hash_pair_t *hp;
-  u8 *name_copy;
-  uword *p;
 
-  ASSERT (ssvmp && ssvmp->sh);
-
-  shared_header = ssvmp->sh;
+  ASSERT (shared_header);
 
   vlib_stat_segment_lock ();
-  /* Update hash table. The name must be copied into the segment */
-  hp = hash_get_pair (sm->counter_vector_by_name, name);
-  if (hp)
-    {
-      name_copy = (u8 *) hp->key;
-      ep = (stat_segment_directory_entry_t *) (hp->value[0]);
-      hash_unset_mem (sm->counter_vector_by_name, name);
-      vec_free (name_copy);
-      clib_mem_free (ep);
-    }
 
-  ep = clib_mem_alloc (sizeof (*ep));
-  ep->type = STAT_DIR_TYPE_ERROR_INDEX;
-  ep->value = (void *) index;
-
-  hash_set_mem (sm->counter_vector_by_name, name, ep);
-
-  /* Reset the client hash table pointer, since it WILL change! */
-  shared_header->opaque[STAT_SEGMENT_OPAQUE_DIR] = sm->counter_vector_by_name;
+  memcpy (e.name, name, vec_len (name));
+  e.name[vec_len (name)] = '\0';
+  e.type = STAT_DIR_TYPE_ERROR_INDEX;
+  e.offset = index;
+  vec_add1 (sm->directory_vector, e);
 
   /* Warn clients to refresh any pointers they might be holding */
-  shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] = (void *)
-    ((u64) shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] + 1);
+  shared_header->directory_offset =
+    stat_segment_offset (shared_header, sm->directory_vector);
+
   vlib_stat_segment_unlock ();
 }
 
-void
-vlib_stats_pop_heap2 (u64 * counter_vector, u32 thread_index, void *oldheap)
+static void
+stat_validate_counter_vector (stat_segment_directory_entry_t * ep, u32 max)
 {
   stats_main_t *sm = &stats_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  ssvm_shared_header_t *shared_header;
-  stat_segment_directory_entry_t *ep;
-  hash_pair_t *hp;
-  u8 *error_vector_name;
-  u8 *name_copy;
-  uword *p;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+  counter_t **counters = 0;
+  vlib_thread_main_t *tm = vlib_get_thread_main ();
+  int i;
+  u64 *offset_vector = 0;
 
-  ASSERT (ssvmp && ssvmp->sh);
+  vec_validate_aligned (counters, tm->n_vlib_mains - 1,
+			CLIB_CACHE_LINE_BYTES);
+  for (i = 0; i < tm->n_vlib_mains; i++)
+    {
+      vec_validate_aligned (counters[i], max, CLIB_CACHE_LINE_BYTES);
+      vec_add1 (offset_vector,
+		stat_segment_offset (shared_header, counters[i]));
+    }
+  ep->offset = stat_segment_offset (shared_header, counters);
+  ep->offset_vector = stat_segment_offset (shared_header, offset_vector);
+}
 
-  shared_header = ssvmp->sh;
+void
+vlib_stats_pop_heap2 (u64 * error_vector, u32 thread_index, void *oldheap)
+{
+  stats_main_t *sm = &stats_main;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+
+  ASSERT (shared_header);
 
   vlib_stat_segment_lock ();
-  error_vector_name = format (0, "/err/%d/counter_vector%c", thread_index, 0);
-
-  /* Update hash table. The name must be copied into the segment */
-  hp = hash_get_pair (sm->counter_vector_by_name, error_vector_name);
-  if (hp)
-    {
-      name_copy = (u8 *) hp->key;
-      ep = (stat_segment_directory_entry_t *) (hp->value[0]);
-      hash_unset_mem (sm->counter_vector_by_name, error_vector_name);
-      vec_free (name_copy);
-      clib_mem_free (ep);
-    }
-
-  ep = clib_mem_alloc (sizeof (*ep));
-  ep->type = STAT_DIR_TYPE_VECTOR_POINTER;
-  ep->value = counter_vector;
-
-  hash_set_mem (sm->counter_vector_by_name, error_vector_name, ep);
 
   /* Reset the client hash table pointer, since it WILL change! */
-  shared_header->opaque[STAT_SEGMENT_OPAQUE_DIR] = sm->counter_vector_by_name;
+  shared_header->error_offset =
+    stat_segment_offset (shared_header, error_vector);
+  shared_header->directory_offset =
+    stat_segment_offset (shared_header, sm->directory_vector);
 
-  /* Warn clients to refresh any pointers they might be holding */
-  shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] = (void *)
-    ((u64) shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] + 1);
   vlib_stat_segment_unlock ();
-  ssvm_pop_heap (oldheap);
+  clib_mem_set_heap (oldheap);
 }
 
 clib_error_t *
 vlib_map_stat_segment_init (void)
 {
   stats_main_t *sm = &stats_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  ssvm_shared_header_t *shared_header;
+  stat_segment_shared_header_t *shared_header;
   stat_segment_directory_entry_t *ep;
+
   f64 *scalar_data;
   u8 *name;
   void *oldheap;
   u32 *lock;
   int rv;
-  u64 memory_size;
+  ssize_t memory_size;
+
+
+  int mfd;
+  char *mem_name = "stat_segment_test";
+  void *memaddr;
 
   memory_size = sm->memory_size;
   if (memory_size == 0)
     memory_size = STAT_SEGMENT_DEFAULT_SIZE;
 
-  ssvmp->ssvm_size = memory_size;
-  ssvmp->i_am_master = 1;
-  ssvmp->my_pid = getpid ();
-  ssvmp->name = format (0, "/stats%c", 0);
-  ssvmp->requested_va = 0;
+  /* Create shared memory segment */
+  if ((mfd = memfd_create (mem_name, 0)) < 0)
+    return clib_error_return (0, "stat segment memfd_create failure");
 
-  rv = ssvm_master_init (ssvmp, SSVM_SEGMENT_MEMFD);
+  /* Set size */
+  if ((ftruncate (mfd, memory_size)) == -1)
+    return clib_error_return (0, "stat segment ftruncate failure");
 
-  if (rv)
-    return clib_error_return (0, "stat segment ssvm init failure");
-  shared_header = ssvmp->sh;
+  if ((memaddr =
+       mmap (NULL, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd,
+	     0)) == MAP_FAILED)
+    return clib_error_return (0, "stat segment mmap failure");
 
-  oldheap = ssvm_push_heap (shared_header);
+  void *heap;
+#if USE_DLMALLOC == 0
+  heap = mheap_alloc_with_flags (((u8 *) memaddr) + getpagesize (),
+				 memory_size - getpagesize (),
+				 MHEAP_FLAG_DISABLE_VM |
+				 MHEAP_FLAG_THREAD_SAFE);
+#else
+  heap =
+    create_mspace_with_base (((u8 *) memaddr) + getpagesize (),
+			     memory_size - getpagesize (), 1 /* locked */ );
+  mspace_disable_expand (heap);
+#endif
+
+  sm->heap = heap;
+  sm->memfd = mfd;
+
+  sm->directory_vector_by_name = hash_create_string (0, sizeof (uword));
+  sm->shared_header = shared_header = memaddr;
+  sm->stat_segment_lockp = clib_mem_alloc (sizeof (clib_spinlock_t));
+  clib_spinlock_init (sm->stat_segment_lockp);
+
+  oldheap = clib_mem_set_heap (sm->heap);
 
   /* Set up the name to counter-vector hash table */
-  sm->counter_vector_by_name = hash_create_string (0, sizeof (uword));
+  sm->directory_vector = 0;
 
-  sm->stat_segment_lockp = clib_mem_alloc (sizeof (clib_spinlock_t));
+  shared_header->epoch = 1;
 
-  /* Save the hash table address in the shared segment, for clients */
-  clib_spinlock_init (sm->stat_segment_lockp);
-  shared_header->opaque[STAT_SEGMENT_OPAQUE_LOCK] = sm->stat_segment_lockp;
-  shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] = (void *) 1;
+  /* Scalar stats and node counters */
+  vec_validate (sm->directory_vector, STAT_COUNTERS - 1);
+#define _(E,t,n,p)							\
+  strcpy(sm->directory_vector[STAT_COUNTER_##E].name,  "/sys" #p "/" #n); \
+  sm->directory_vector[STAT_COUNTER_##E].type = STAT_DIR_TYPE_##t;
+  foreach_stat_segment_counter_name
+#undef _
+    /* Save the vector offset in the shared segment, for clients */
+    shared_header->directory_offset =
+    stat_segment_offset (shared_header, sm->directory_vector);
 
-  /* Set up a few scalar stats */
-
-  scalar_data = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
-					CLIB_CACHE_LINE_BYTES);
-  sm->vector_rate_ptr = (scalar_data + 0);
-  sm->input_rate_ptr = (scalar_data + 1);
-  sm->last_runtime_ptr = (scalar_data + 2);
-  sm->last_runtime_stats_clear_ptr = (scalar_data + 3);
-  sm->heartbeat_ptr = (scalar_data + 4);
-
-  name = format (0, "/sys/vector_rate%c", 0);
-  ep = clib_mem_alloc (sizeof (*ep));
-  ep->type = STAT_DIR_TYPE_SCALAR_POINTER;
-  ep->value = sm->vector_rate_ptr;
-
-  hash_set_mem (sm->counter_vector_by_name, name, ep);
-
-  name = format (0, "/sys/input_rate%c", 0);
-  ep = clib_mem_alloc (sizeof (*ep));
-  ep->type = STAT_DIR_TYPE_SCALAR_POINTER;
-  ep->value = sm->input_rate_ptr;
-
-  hash_set_mem (sm->counter_vector_by_name, name, ep);
-
-  name = format (0, "/sys/last_update%c", 0);
-  ep = clib_mem_alloc (sizeof (*ep));
-  ep->type = STAT_DIR_TYPE_SCALAR_POINTER;
-  ep->value = sm->last_runtime_ptr;
-
-  hash_set_mem (sm->counter_vector_by_name, name, ep);
-
-  name = format (0, "/sys/last_stats_clear%c", 0);
-  ep = clib_mem_alloc (sizeof (*ep));
-  ep->type = STAT_DIR_TYPE_SCALAR_POINTER;
-  ep->value = sm->last_runtime_stats_clear_ptr;
-
-  hash_set_mem (sm->counter_vector_by_name, name, ep);
-
-  name = format (0, "/sys/heartbeat%c", 0);
-  ep = clib_mem_alloc (sizeof (*ep));
-  ep->type = STAT_DIR_TYPE_SCALAR_POINTER;
-  ep->value = sm->heartbeat_ptr;
-
-  hash_set_mem (sm->counter_vector_by_name, name, ep);
-
-
-  /* Publish the hash table */
-  shared_header->opaque[STAT_SEGMENT_OPAQUE_DIR] = sm->counter_vector_by_name;
-
-  ssvm_pop_heap (oldheap);
+  clib_mem_set_heap (oldheap);
 
   return 0;
 }
 
-typedef struct
-{
-  u8 *name;
-  stat_segment_directory_entry_t *dir_entry;
-} show_stat_segment_t;
-
 static int
 name_sort_cmp (void *a1, void *a2)
 {
-  show_stat_segment_t *n1 = a1;
-  show_stat_segment_t *n2 = a2;
+  stat_segment_directory_entry_t *n1 = a1;
+  stat_segment_directory_entry_t *n2 = a2;
 
   return strcmp ((char *) n1->name, (char *) n2->name);
 }
@@ -316,30 +299,21 @@
   char *type_name;
   char *format_string;
 
-  format_string = "%-10s %20llx";
+  format_string = "%-74s %-10s %10lld";
 
   switch (ep->type)
     {
-    case STAT_DIR_TYPE_SCALAR_POINTER:
+    case STAT_DIR_TYPE_SCALAR_INDEX:
       type_name = "ScalarPtr";
       break;
 
-    case STAT_DIR_TYPE_VECTOR_POINTER:
-      type_name = "VectorPtr";
-      break;
-
     case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
     case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
       type_name = "CMainPtr";
       break;
 
-    case STAT_DIR_TYPE_SERIALIZED_NODES:
-      type_name = "SerNodesPtr";
-      break;
-
     case STAT_DIR_TYPE_ERROR_INDEX:
       type_name = "ErrIndex";
-      format_string = "%-10s %20lld";
       break;
 
     default:
@@ -347,7 +321,7 @@
       break;
     }
 
-  return format (s, format_string, type_name, ep->value);
+  return format (s, format_string, ep->name, type_name, ep->offset);
 }
 
 static clib_error_t *
@@ -356,13 +330,10 @@
 			      vlib_cli_command_t * cmd)
 {
   stats_main_t *sm = &stats_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  ssvm_shared_header_t *shared_header;
   counter_t *counter;
   hash_pair_t *p;
-  show_stat_segment_t *show_data = 0;
-  show_stat_segment_t *this;
-  int i;
+  stat_segment_directory_entry_t *show_data, *this;
+  int i, j;
 
   int verbose = 0;
   u8 *s;
@@ -370,40 +341,25 @@
   if (unformat (input, "verbose"))
     verbose = 1;
 
+  /* Lock even as reader, as this command doesn't handle epoch changes */
   vlib_stat_segment_lock ();
-
-  /* *INDENT-OFF* */
-  hash_foreach_pair (p, sm->counter_vector_by_name,
-  ({
-    vec_add2 (show_data, this, 1);
-
-    this->name = (u8 *) (p->key);
-    this->dir_entry = (stat_segment_directory_entry_t *)(p->value[0]);
-  }));
-  /* *INDENT-ON* */
-
+  show_data = vec_dup (sm->directory_vector);
   vlib_stat_segment_unlock ();
 
   vec_sort_with_function (show_data, name_sort_cmp);
 
-  vlib_cli_output (vm, "%-60s %10s %20s", "Name", "Type", "Value");
+  vlib_cli_output (vm, "%-74s %10s %10s", "Name", "Type", "Value");
 
   for (i = 0; i < vec_len (show_data); i++)
     {
-      this = vec_elt_at_index (show_data, i);
-
-      vlib_cli_output (vm, "%-60s %31U",
-		       this->name, format_stat_dir_entry, this->dir_entry);
+      vlib_cli_output (vm, "%-100U", format_stat_dir_entry,
+		       vec_elt_at_index (show_data, i));
     }
 
   if (verbose)
     {
-      ASSERT (ssvmp && ssvmp->sh);
-
-      shared_header = ssvmp->sh;
-
-      vlib_cli_output (vm, "%U", format_mheap,
-		       shared_header->heap, 0 /* verbose */ );
+      ASSERT (sm->heap);
+      vlib_cli_output (vm, "%U", format_mheap, sm->heap, 0 /* verbose */ );
     }
 
   return 0;
@@ -418,71 +374,95 @@
 };
 /* *INDENT-ON* */
 
+/*
+ * Node performance counters:
+ * total_calls [threads][node-index]
+ * total_vectors
+ * total_calls
+ * total suspends
+ */
+
 static inline void
-update_serialized_nodes (stats_main_t * sm)
+update_node_counters (stats_main_t * sm)
 {
-  int i;
   vlib_main_t *vm = vlib_mains[0];
-  ssvm_private_t *ssvmp = &sm->stat_segment;
-  ssvm_shared_header_t *shared_header;
-  void *oldheap;
-  stat_segment_directory_entry_t *ep;
-  hash_pair_t *hp;
-  u8 *name_copy;
+  vlib_main_t **stat_vms = 0;
+  vlib_node_t ***node_dups = 0;
+  int i, j;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+  static u32 no_max_nodes = 0;
 
-  ASSERT (ssvmp && ssvmp->sh);
-
-  vec_reset_length (sm->serialized_nodes);
-
-  shared_header = ssvmp->sh;
-
-  oldheap = ssvm_push_heap (shared_header);
-
-  vlib_stat_segment_lock ();
   vlib_node_get_nodes (0 /* vm, for barrier sync */ ,
 		       (u32) ~ 0 /* all threads */ ,
 		       1 /* include stats */ ,
 		       0 /* barrier sync */ ,
-		       &sm->node_dups, &sm->stat_vms);
+		       &node_dups, &stat_vms);
 
-  sm->serialized_nodes = vlib_node_serialize (vm, sm->node_dups,
-					      sm->serialized_nodes,
-					      0 /* include nexts */ ,
-					      1 /* include stats */ );
+  u32 l = vec_len (node_dups[0]);
 
-  hp = hash_get_pair (sm->counter_vector_by_name, "serialized_nodes");
-  if (hp)
+  /*
+   * Extend performance nodes if necessary
+   */
+  if (l > no_max_nodes)
     {
-      name_copy = (u8 *) hp->key;
-      ep = (stat_segment_directory_entry_t *) (hp->value[0]);
+      void *oldheap = clib_mem_set_heap (sm->heap);
+      vlib_stat_segment_lock ();
 
-      if (ep->value != sm->serialized_nodes)
+      stat_validate_counter_vector (&sm->directory_vector
+				    [STAT_COUNTER_NODE_CLOCKS], l);
+      stat_validate_counter_vector (&sm->directory_vector
+				    [STAT_COUNTER_NODE_VECTORS], l);
+      stat_validate_counter_vector (&sm->directory_vector
+				    [STAT_COUNTER_NODE_CALLS], l);
+      stat_validate_counter_vector (&sm->directory_vector
+				    [STAT_COUNTER_NODE_SUSPENDS], l);
+
+      vlib_stat_segment_unlock ();
+      clib_mem_set_heap (oldheap);
+      no_max_nodes = l;
+    }
+
+  for (j = 0; j < vec_len (node_dups); j++)
+    {
+      vlib_node_t **nodes = node_dups[j];
+      u32 l = vec_len (nodes);
+
+      for (i = 0; i < vec_len (nodes); i++)
 	{
-	  ep->value = sm->serialized_nodes;
-	  /* Warn clients to refresh any pointers they might be holding */
-	  shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] = (void *)
-	    ((u64) shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] + 1);
+	  counter_t **counters;
+	  counter_t *c;
+	  vlib_node_t *n = nodes[i];
+
+	  counters =
+	    stat_segment_pointer (shared_header,
+				  sm->directory_vector
+				  [STAT_COUNTER_NODE_CLOCKS].offset);
+	  c = counters[j];
+	  c[n->index] = n->stats_total.clocks - n->stats_last_clear.clocks;
+
+	  counters =
+	    stat_segment_pointer (shared_header,
+				  sm->directory_vector
+				  [STAT_COUNTER_NODE_VECTORS].offset);
+	  c = counters[j];
+	  c[n->index] = n->stats_total.vectors - n->stats_last_clear.vectors;
+
+	  counters =
+	    stat_segment_pointer (shared_header,
+				  sm->directory_vector
+				  [STAT_COUNTER_NODE_CALLS].offset);
+	  c = counters[j];
+	  c[n->index] = n->stats_total.calls - n->stats_last_clear.calls;
+
+	  counters =
+	    stat_segment_pointer (shared_header,
+				  sm->directory_vector
+				  [STAT_COUNTER_NODE_SUSPENDS].offset);
+	  c = counters[j];
+	  c[n->index] =
+	    n->stats_total.suspends - n->stats_last_clear.suspends;
 	}
     }
-  else
-    {
-      name_copy = format (0, "%s%c", "serialized_nodes", 0);
-      ep = clib_mem_alloc (sizeof (*ep));
-      ep->type = STAT_DIR_TYPE_SERIALIZED_NODES;
-      ep->value = sm->serialized_nodes;
-      hash_set_mem (sm->counter_vector_by_name, name_copy, ep);
-
-      /* Reset the client hash table pointer */
-      shared_header->opaque[STAT_SEGMENT_OPAQUE_DIR]
-	= sm->counter_vector_by_name;
-
-      /* Warn clients to refresh any pointers they might be holding */
-      shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] = (void *)
-	((u64) shared_header->opaque[STAT_SEGMENT_OPAQUE_EPOCH] + 1);
-    }
-
-  vlib_stat_segment_unlock ();
-  ssvm_pop_heap (oldheap);
 }
 
 /*
@@ -515,25 +495,27 @@
     }
   vector_rate /= (f64) (i - start);
 
-  *sm->vector_rate_ptr = vector_rate / ((f64) (vec_len (vlib_mains) - start));
+  sm->directory_vector[STAT_COUNTER_VECTOR_RATE].value =
+    vector_rate / ((f64) (vec_len (vlib_mains) - start));
 
   /*
    * Compute the aggregate input rate
    */
   now = vlib_time_now (vm);
-  dt = now - sm->last_runtime_ptr[0];
+  dt = now - sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value;
   input_packets = vnet_get_aggregate_rx_packets ();
-  *sm->input_rate_ptr = (f64) (input_packets - sm->last_input_packets) / dt;
-  sm->last_runtime_ptr[0] = now;
+  sm->directory_vector[STAT_COUNTER_INPUT_RATE].value =
+    (f64) (input_packets - sm->last_input_packets) / dt;
+  sm->directory_vector[STAT_COUNTER_LAST_UPDATE].value = now;
   sm->last_input_packets = input_packets;
-  sm->last_runtime_stats_clear_ptr[0] =
+  sm->directory_vector[STAT_COUNTER_LAST_STATS_CLEAR].value =
     vm->node_main.time_last_runtime_stats_clear;
 
-  if (sm->serialize_nodes)
-    update_serialized_nodes (sm);
+  if (sm->node_counters_enabled)
+    update_node_counters (sm);
 
   /* Heartbeat, so clients detect we're still here */
-  (*sm->heartbeat_ptr)++;
+  sm->directory_vector[STAT_COUNTER_HEARTBEAT].value++;
 }
 
 static clib_error_t *
@@ -546,10 +528,10 @@
     {
       if (unformat (input, "size %U", unformat_memory_size, &sm->memory_size))
 	;
-      else if (unformat (input, "serialize-nodes on"))
-	sm->serialize_nodes = 1;
-      else if (unformat (input, "serialize-nodes off"))
-	sm->serialize_nodes = 0;
+      else if (unformat (input, "per-node-counters on"))
+	sm->node_counters_enabled = 1;
+      else if (unformat (input, "per-node-counters off"))
+	sm->node_counters_enabled = 0;
       else
 	return clib_error_return (0, "unknown input `%U'",
 				  format_unformat_error, input);
diff --git a/src/vpp/stats/stat_segment.h b/src/vpp/stats/stat_segment.h
new file mode 100644
index 0000000..0efd45a
--- /dev/null
+++ b/src/vpp/stats/stat_segment.h
@@ -0,0 +1,97 @@
+/*
+ * 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_h
+#define included_stat_segment_h
+
+#include <stdatomic.h>
+
+/* Default socket to exchange segment fd */
+#define STAT_SEGMENT_SOCKET_FILE "/run/vpp/stats.sock"
+
+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_directory_type_t;
+
+typedef enum
+{
+ STAT_COUNTER_VECTOR_RATE = 0,
+ STAT_COUNTER_INPUT_RATE,
+ STAT_COUNTER_LAST_UPDATE,
+ STAT_COUNTER_LAST_STATS_CLEAR,
+ STAT_COUNTER_HEARTBEAT,
+ STAT_COUNTER_NODE_CLOCKS,
+ STAT_COUNTER_NODE_VECTORS,
+ STAT_COUNTER_NODE_CALLS,
+ STAT_COUNTER_NODE_SUSPENDS,
+ STAT_COUNTERS
+} stat_segment_counter_t;
+
+#define foreach_stat_segment_counter_name			\
+  _(VECTOR_RATE, SCALAR_INDEX, vector_rate,)			\
+  _(INPUT_RATE, SCALAR_INDEX, input_rate,)			\
+  _(LAST_UPDATE, SCALAR_INDEX, last_update,)			\
+  _(LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear,)		\
+  _(HEARTBEAT, SCALAR_INDEX, heartbeat,)			\
+  _(NODE_CLOCKS, COUNTER_VECTOR_SIMPLE, clocks, /node)		\
+  _(NODE_VECTORS, COUNTER_VECTOR_SIMPLE, vectors, /node)	\
+  _(NODE_CALLS, COUNTER_VECTOR_SIMPLE, calls, /node)		\
+  _(NODE_SUSPENDS, COUNTER_VECTOR_SIMPLE, suspends, /node)
+
+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 header first in the shared memory segment.
+ */
+typedef struct
+{
+  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);
+}
+
+#endif
diff --git a/src/vpp/stats/stats.c b/src/vpp/stats/stats.c
index d075374..5eea25e 100644
--- a/src/vpp/stats/stats.c
+++ b/src/vpp/stats/stats.c
@@ -2348,7 +2348,6 @@
 stats_socket_accept_ready (clib_file_t * uf)
 {
   stats_main_t *sm = &stats_main;
-  ssvm_private_t *ssvmp = &sm->stat_segment;
   clib_error_t *err;
   clib_socket_t client = { 0 };
 
@@ -2360,7 +2359,7 @@
     }
 
   /* Send the fd across and close */
-  err = clib_socket_sendmsg (&client, 0, 0, &ssvmp->fd, 1);
+  err = clib_socket_sendmsg (&client, 0, 0, &sm->memfd, 1);
   if (err)
     clib_error_report (err);
   clib_socket_close (&client);
diff --git a/src/vpp/stats/stats.h b/src/vpp/stats/stats.h
index 911706c..f21451d 100644
--- a/src/vpp/stats/stats.h
+++ b/src/vpp/stats/stats.h
@@ -26,11 +26,7 @@
 #include <vlib/unix/unix.h>
 #include <vlibmemory/api.h>
 #include <vlibapi/api_helper_macros.h>
-#include <svm/queue.h>
-#include <svm/ssvm.h>
-
-/* Default socket to exchange segment fd */
-#define STAT_SEGMENT_SOCKET_FILE "/run/vpp/stats.sock"
+#include <vpp/stats/stat_segment.h>
 
 typedef struct
 {
@@ -99,7 +95,6 @@
 
 } vpe_client_stats_registration_t;
 
-
 typedef struct
 {
   void *mheap;
@@ -162,33 +157,19 @@
   vpe_client_registration_t **clients_tmp;
 
   /* statistics segment */
-  ssvm_private_t stat_segment;
-  uword *counter_vector_by_name;
+  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;
-  uword memory_size;
-  u8 serialize_nodes;
+  ssize_t memory_size;
+  u8 node_counters_enabled;
+  void *heap;
+  stat_segment_shared_header_t *shared_header;	/* pointer to shared memory segment */
+  int memfd;
 
-  /* Pointers to scalar stats maintained by the stat thread */
-  f64 *input_rate_ptr;
-  f64 *last_runtime_ptr;
-  f64 *last_runtime_stats_clear_ptr;
-  f64 *vector_rate_ptr;
-  f64 *heartbeat_ptr;
   u64 last_input_packets;
 
-  /* Pointers to vector stats maintained by the stat thread */
-  u8 *serialized_nodes;
-  vlib_main_t **stat_vms;
-  vlib_node_t ***node_dups;
-
-  f64 *vectors_per_node;
-  f64 *vector_rate_in;
-  f64 *vector_rate_out;
-  f64 *vector_rate_drop;
-  f64 *vector_rate_punt;
-
   /* convenience */
   vlib_main_t *vlib_main;
   vnet_main_t *vnet_main;
@@ -198,30 +179,6 @@
 
 extern stats_main_t stats_main;
 
-/* Default stat segment 32m */
-#define STAT_SEGMENT_DEFAULT_SIZE	(32<<20)
-
-#define STAT_SEGMENT_OPAQUE_LOCK	0
-#define STAT_SEGMENT_OPAQUE_DIR		1
-#define STAT_SEGMENT_OPAQUE_EPOCH	2
-
-typedef enum
-{
-  STAT_DIR_TYPE_ILLEGAL = 0,
-  STAT_DIR_TYPE_SCALAR_POINTER,
-  STAT_DIR_TYPE_VECTOR_POINTER,
-  STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE,
-  STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED,
-  STAT_DIR_TYPE_ERROR_INDEX,
-  STAT_DIR_TYPE_SERIALIZED_NODES,
-} stat_directory_type_t;
-
-typedef struct
-{
-  stat_directory_type_t type;
-  void *value;
-} stat_segment_directory_entry_t;
-
 void do_stat_segment_updates (stats_main_t * sm);
 
 #endif /* __included_stats_h__ */