#ifndef included_vlib_counter_h
#define included_vlib_counter_h
+#include <vlib/counter_types.h>
+
/** \file
Optimized thread-safe counters.
The idea is to drastically eliminate atomic operations.
*/
-/** 64bit counters */
-typedef u64 counter_t;
-
/** A collection of simple counters */
typedef struct
{
counter_t **counters; /**< Per-thread u64 non-atomic counters */
- counter_t *value_at_last_serialize; /**< Values as of last serialize. */
- u32 last_incremental_serialize_index; /**< Last counter index
- serialized incrementally. */
-
char *name; /**< The counter collection's name. */
+ char *stat_segment_name; /**< Name in stat segment directory */
} vlib_simple_counter_main_t;
/** The number of counters (not the number of per-thread counters) */
/** Increment a simple counter
@param cm - (vlib_simple_counter_main_t *) simple counter main pointer
- @param cpu_index - (u32) the current cpu index
+ @param thread_index - (u32) the current cpu index
@param index - (u32) index of the counter to increment
@param increment - (u64) quantitiy to add to the counter
*/
always_inline void
vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
- u32 cpu_index, u32 index, u64 increment)
+ u32 thread_index, u32 index, u64 increment)
{
counter_t *my_counters;
- my_counters = cm->counters[cpu_index];
+ my_counters = cm->counters[thread_index];
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
+ @param index - (u32) index of the counter to increment
+ @param value - (u64) quantitiy to set to the counter
+*/
+always_inline void
+vlib_set_simple_counter (vlib_simple_counter_main_t * cm,
+ u32 thread_index, u32 index, u64 value)
+{
+ counter_t *my_counters;
+
+ my_counters = cm->counters[thread_index];
+ my_counters[index] = value;
+}
+
/** Get the value of a simple counter
Scrapes the entire set of per-thread counters. Innacurate unless
worker threads which might increment the counter are
}
}
-/** 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
typedef struct
{
vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
- vlib_counter_t *value_at_last_serialize; /**< Counter values as of last serialize. */
- u32 last_incremental_serialize_index; /**< Last counter index serialized incrementally. */
char *name; /**< The counter collection's name. */
+ char *stat_segment_name; /**< Name in stat segment directory */
} vlib_combined_counter_main_t;
/** The number of counters (not the number of per-thread counters) */
/** Increment a combined counter
@param cm - (vlib_combined_counter_main_t *) comined counter main pointer
- @param cpu_index - (u32) the current cpu index
+ @param thread_index - (u32) the current cpu index
@param index - (u32) index of the counter to increment
@param packet_increment - (u64) number of packets to add to the counter
@param byte_increment - (u64) number of bytes to add to the counter
always_inline void
vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
- u32 cpu_index,
+ u32 thread_index,
u32 index, u64 n_packets, u64 n_bytes)
{
vlib_counter_t *my_counters;
/* Use this CPU's counter array */
- my_counters = cm->counters[cpu_index];
+ my_counters = cm->counters[thread_index];
my_counters[index].packets += n_packets;
my_counters[index].bytes += n_bytes;
/** Pre-fetch a per-thread combined counter for the given object index */
always_inline void
vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
- u32 cpu_index, u32 index)
+ u32 thread_index, u32 index)
{
vlib_counter_t *cpu_counters;
/*
* This CPU's index is assumed to already be in cache
*/
- cpu_counters = cm->counters[cpu_index];
+ cpu_counters = cm->counters[thread_index];
CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
}
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
void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
u32 index);
+int vlib_validate_combined_counter_will_expand
+ (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
*/
#define vlib_counter_len(cm) vec_len((cm)->maxi)
-serialize_function_t serialize_vlib_simple_counter_main,
- unserialize_vlib_simple_counter_main;
-serialize_function_t serialize_vlib_combined_counter_main,
- unserialize_vlib_combined_counter_main;
-
#endif /* included_vlib_counter_h */
/*