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