Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 2 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 3 | /*! \file statistics.h |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 4 | * Common routines regarding statistics */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 5 | |
| 6 | /*! structure representing a single counter */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 7 | struct osmo_counter { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 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 */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 13 | }; |
| 14 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 15 | /*! Decrement counter */ |
Alexander Couzens | e65315f | 2016-08-22 21:42:57 +0200 | [diff] [blame] | 16 | static inline void osmo_counter_dec(struct osmo_counter *ctr) |
| 17 | { |
| 18 | ctr->value--; |
| 19 | } |
| 20 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 21 | /*! Increment counter */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 22 | static inline void osmo_counter_inc(struct osmo_counter *ctr) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 23 | { |
| 24 | ctr->value++; |
| 25 | } |
| 26 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 27 | /*! Get current value of counter */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 28 | static inline unsigned long osmo_counter_get(struct osmo_counter *ctr) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 29 | { |
| 30 | return ctr->value; |
| 31 | } |
| 32 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 33 | /*! Reset current value of counter to 0 */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 34 | static inline void osmo_counter_reset(struct osmo_counter *ctr) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | { |
| 36 | ctr->value = 0; |
| 37 | } |
| 38 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 39 | /*! Allocate a new counter */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 40 | struct osmo_counter *osmo_counter_alloc(const char *name); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 41 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 42 | /*! Free the specified counter |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 43 | * \param[in] ctr Counter |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 44 | */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 45 | void osmo_counter_free(struct osmo_counter *ctr); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 46 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 47 | /*! Iterate over all counters |
Jacob Erlbeck | e5b0fe2 | 2015-10-19 15:00:59 +0200 | [diff] [blame] | 48 | * \param[in] handle_counter Call-back function, aborts if rc < 0 |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 49 | * \param[in] data Private dtata handed through to \a handle_counter |
| 50 | */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 51 | int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), void *data); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 53 | /*! Resolve counter by human-readable name |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 54 | * \param[in] name human-readable name of counter |
| 55 | * \returns pointer to counter (\ref osmo_counter) or NULL otherwise |
| 56 | */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 57 | struct osmo_counter *osmo_counter_get_by_name(const char *name); |
Jacob Erlbeck | 80db4ec | 2015-10-26 14:39:08 +0100 | [diff] [blame] | 58 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 59 | /*! Return the counter difference since the last call to this function */ |
Jacob Erlbeck | 80db4ec | 2015-10-26 14:39:08 +0100 | [diff] [blame] | 60 | int osmo_counter_difference(struct osmo_counter *ctr); |