stats: add apis to delete simple/combined counters 20/26620/3
authorOle Troan <ot@cisco.com>
Tue, 21 Apr 2020 15:54:41 +0000 (17:54 +0200)
committerNeale Ranns <nranns@cisco.com>
Tue, 28 Apr 2020 09:39:47 +0000 (09:39 +0000)
vlib_free_simple_counter()
vlib_free_combined_counter()

Frees the name and two dimensional vector from the stats segment.

Type: fix
Signed-off-by: Ole Troan <ot@cisco.com>
Change-Id: If1becf7d09520ba41a3d59e2df94958ecfcf6948

src/vlib/counter.c
src/vlib/counter.h
src/vlib/stat_weak_inlines.h
src/vpp/stats/stat_segment.c

index 8573b79..edba375 100644 (file)
@@ -90,6 +90,20 @@ vlib_validate_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
                       2 /* STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE */ );
 }
 
+void
+vlib_free_simple_counter (vlib_simple_counter_main_t * cm)
+{
+  int i;
+
+  vlib_stats_delete_cm (cm);
+
+  void *oldheap = vlib_stats_push_heap (cm->counters);
+  for (i = 0; i < vec_len (cm->counters); i++)
+    vec_free (cm->counters[i]);
+  vec_free (cm->counters);
+  clib_mem_set_heap (oldheap);
+}
+
 void
 vlib_validate_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
 {
@@ -105,6 +119,20 @@ vlib_validate_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
                       3 /*STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED */ );
 }
 
+void
+vlib_free_combined_counter (vlib_combined_counter_main_t * cm)
+{
+  int i;
+
+  vlib_stats_delete_cm (cm);
+
+  void *oldheap = vlib_stats_push_heap (cm->counters);
+  for (i = 0; i < vec_len (cm->counters); i++)
+    vec_free (cm->counters[i]);
+  vec_free (cm->counters);
+  clib_mem_set_heap (oldheap);
+}
+
 u32
 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t * cm)
 {
index 292e137..7c90947 100644 (file)
@@ -304,6 +304,8 @@ vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
 
 void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
                                   u32 index);
+void vlib_free_simple_counter (vlib_simple_counter_main_t * cm);
+
 /** validate a combined counter
     @param cm - (vlib_combined_counter_main_t *) pointer to the counter
     collection
@@ -312,6 +314,7 @@ void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
 
 void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
                                     u32 index);
+void vlib_free_combined_counter (vlib_combined_counter_main_t * cm);
 
 /** Obtain the number of simple or combined counters allocated.
     A macro which reduces to to vec_len(cm->maxi), the answer in either
index d288a04..a1311e8 100644 (file)
@@ -57,5 +57,10 @@ void
 vlib_stat_segment_unlock (void)
 {
 }
+void vlib_stats_delete_cm (void *) __attribute__ ((weak));
+void
+vlib_stats_delete_cm (void *notused)
+{
+}
 
 #endif
index 0e3fa41..2152e1e 100644 (file)
@@ -148,6 +148,44 @@ vlib_stats_delete_counter (u32 index, void *oldheap)
   e->type = STAT_DIR_TYPE_EMPTY;
 }
 
+/*
+ * Called from main heap
+ */
+void
+vlib_stats_delete_cm (void *cm_arg)
+{
+  vlib_simple_counter_main_t *cm = (vlib_simple_counter_main_t *) cm_arg;
+  stat_segment_main_t *sm = &stat_segment_main;
+  stat_segment_directory_entry_t *e;
+  stat_segment_shared_header_t *shared_header = sm->shared_header;
+
+  /* Not all counters have names / hash-table entries */
+  if (!cm->name && !cm->stat_segment_name)
+    {
+      return;
+    }
+  vlib_stat_segment_lock ();
+
+  /* Lookup hash-table is on the main heap */
+  char *stat_segment_name =
+    cm->stat_segment_name ? cm->stat_segment_name : cm->name;
+  u32 index = lookup_hash_index ((u8 *) stat_segment_name);
+
+  e = &sm->directory_vector[index];
+  hash_unset (sm->directory_vector_by_name, &e->name);
+
+  u64 *offset_vector = stat_segment_pointer (shared_header, e->offset_vector);
+
+  void *oldheap = clib_mem_set_heap (sm->heap);        /* Enter stats segment */
+  vec_free (offset_vector);
+  clib_mem_set_heap (oldheap); /* Exit stats segment */
+
+  memset (e, 0, sizeof (*e));
+  e->type = STAT_DIR_TYPE_EMPTY;
+
+  vlib_stat_segment_unlock ();
+}
+
 void
 vlib_stats_pop_heap (void *cm_arg, void *oldheap, u32 cindex,
                     stat_directory_type_t type)
@@ -172,6 +210,7 @@ vlib_stats_pop_heap (void *cm_arg, void *oldheap, u32 cindex,
   /* Lookup hash-table is on the main heap */
   stat_segment_name =
     cm->stat_segment_name ? cm->stat_segment_name : cm->name;
+
   clib_mem_set_heap (oldheap); /* Exit stats segment */
   u32 vector_index = lookup_hash_index ((u8 *) stat_segment_name);
   /* Back to stats segment */