Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | /*! \file counter.h |
| 4 | * Common routines regarding counter handling */ |
| 5 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 6 | /*! Structure representing a single counter */ |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 7 | struct 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 Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 15 | /*! Decrement given counter by one |
| 16 | * \param[in] ctr Counter that's to be decremented */ |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 17 | static inline void osmo_counter_dec(struct osmo_counter *ctr) |
| 18 | { |
| 19 | ctr->value--; |
| 20 | } |
| 21 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 22 | /*! Increment counter by one. |
| 23 | * \param[in] Counter that's to be incremented */ |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 24 | static inline void osmo_counter_inc(struct osmo_counter *ctr) |
| 25 | { |
| 26 | ctr->value++; |
| 27 | } |
| 28 | |
| 29 | /*! Get current value of counter */ |
| 30 | static 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 */ |
| 36 | static inline void osmo_counter_reset(struct osmo_counter *ctr) |
| 37 | { |
| 38 | ctr->value = 0; |
| 39 | } |
| 40 | |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 41 | struct osmo_counter *osmo_counter_alloc(const char *name); |
| 42 | |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 43 | void osmo_counter_free(struct osmo_counter *ctr); |
| 44 | |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 45 | int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), void *data); |
| 46 | |
Alexander Couzens | 18ba26c | 2017-12-05 16:06:27 +0100 | [diff] [blame] | 47 | int osmo_counters_count(); |
| 48 | |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 49 | struct osmo_counter *osmo_counter_get_by_name(const char *name); |
| 50 | |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 51 | int osmo_counter_difference(struct osmo_counter *ctr); |