vlib: add decrement counter method 43/28843/9
authorSergey Matov <sergey.matov@travelping.com>
Tue, 15 Sep 2020 09:40:55 +0000 (13:40 +0400)
committerDave Barach <openvpp@barachs.net>
Fri, 18 Sep 2020 15:23:01 +0000 (15:23 +0000)
Currently there is no way to decrease simple counter. To extend
counters API new method allows to decrement counter by given value.
This might be useful in implementation of statistics stored in stats
segment.

Type: improvement

Change-Id: I7c08c62bffa6a2d50e9e2cf884f7b2d48538f34b
Signed-off-by: Sergey Matov <sergey.matov@travelping.com>
Signed-off-by: Dave Barach <dave@barachs.net>
src/vlib/counter.h

index 8a5aed4..04ab837 100644 (file)
@@ -84,6 +84,25 @@ vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
   my_counters[index] += increment;
 }
 
+/** Decrement a simple counter
+    @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
+    @param thread_index - (u32) the current cpu index
+    @param index - (u32) index of the counter to increment
+    @param increment - (u64) quantitiy remove from the counter value
+*/
+always_inline void
+vlib_decrement_simple_counter (vlib_simple_counter_main_t * cm,
+                              u32 thread_index, u32 index, u64 decrement)
+{
+  counter_t *my_counters;
+
+  my_counters = cm->counters[thread_index];
+
+  ASSERT (my_counters[index] >= decrement);
+
+  my_counters[index] -= decrement;
+}
+
 /** Set a simple counter
     @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
     @param thread_index - (u32) the current cpu index