blob: 259f1edf6cd13b33fad2bbf3f3adcf604efb43f7 [file] [log] [blame]
Harald Welte216338c2017-10-15 19:46:19 +02001#pragma once
2
3/*! \file counter.h
4 * Common routines regarding counter handling */
5
Harald Welte17bbaa32017-10-15 20:02:07 +02006/*! Structure representing a single counter */
Harald Welte216338c2017-10-15 19:46:19 +02007struct osmo_counter {
8 struct llist_head list; /*!< internal list head */
9 const char *name; /*!< human-readable name */
10 const char *description; /*!< humn-readable description */
11 unsigned long value; /*!< current value */
12 unsigned long previous; /*!< previous value */
13};
14
Harald Welte17bbaa32017-10-15 20:02:07 +020015/*! Decrement given counter by one
16 * \param[in] ctr Counter that's to be decremented */
Harald Welte216338c2017-10-15 19:46:19 +020017static inline void osmo_counter_dec(struct osmo_counter *ctr)
18{
19 ctr->value--;
20}
21
Harald Welte17bbaa32017-10-15 20:02:07 +020022/*! Increment counter by one.
23 * \param[in] Counter that's to be incremented */
Harald Welte216338c2017-10-15 19:46:19 +020024static inline void osmo_counter_inc(struct osmo_counter *ctr)
25{
26 ctr->value++;
27}
28
29/*! Get current value of counter */
30static inline unsigned long osmo_counter_get(struct osmo_counter *ctr)
31{
32 return ctr->value;
33}
34
35/*! Reset current value of counter to 0 */
36static inline void osmo_counter_reset(struct osmo_counter *ctr)
37{
38 ctr->value = 0;
39}
40
Harald Welte216338c2017-10-15 19:46:19 +020041struct osmo_counter *osmo_counter_alloc(const char *name);
42
Harald Welte216338c2017-10-15 19:46:19 +020043void osmo_counter_free(struct osmo_counter *ctr);
44
Harald Welte216338c2017-10-15 19:46:19 +020045int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), void *data);
46
Alexander Couzens18ba26c2017-12-05 16:06:27 +010047int osmo_counters_count();
48
Harald Welte216338c2017-10-15 19:46:19 +020049struct osmo_counter *osmo_counter_get_by_name(const char *name);
50
Harald Welte216338c2017-10-15 19:46:19 +020051int osmo_counter_difference(struct osmo_counter *ctr);