blob: 41716ccf6b51f1bdd6abc8131de91ad0cf301575 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
Harald Weltebd598e32011-08-16 23:26:52 +02003/*! \file statistics.h
4 * \brief Common routines regarding statistics */
5
6/*! structure representing a single counter */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +02007struct osmo_counter {
Harald Weltebd598e32011-08-16 23:26:52 +02008 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 Erlbeck80db4ec2015-10-26 14:39:08 +010012 unsigned long previous; /*!< \brief previous value */
Harald Welteec8b4502010-02-20 20:34:29 +010013};
14
Alexander Couzense65315f2016-08-22 21:42:57 +020015/*! \brief Decrement counter */
16static inline void osmo_counter_dec(struct osmo_counter *ctr)
17{
18 ctr->value--;
19}
20
Harald Weltebd598e32011-08-16 23:26:52 +020021/*! \brief Increment counter */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020022static inline void osmo_counter_inc(struct osmo_counter *ctr)
Harald Welteec8b4502010-02-20 20:34:29 +010023{
24 ctr->value++;
25}
26
Harald Weltebd598e32011-08-16 23:26:52 +020027/*! \brief Get current value of counter */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020028static inline unsigned long osmo_counter_get(struct osmo_counter *ctr)
Harald Welteec8b4502010-02-20 20:34:29 +010029{
30 return ctr->value;
31}
32
Harald Weltebd598e32011-08-16 23:26:52 +020033/*! \brief Reset current value of counter to 0 */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020034static inline void osmo_counter_reset(struct osmo_counter *ctr)
Harald Welteec8b4502010-02-20 20:34:29 +010035{
36 ctr->value = 0;
37}
38
Harald Weltebd598e32011-08-16 23:26:52 +020039/*! \brief Allocate a new counter */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020040struct osmo_counter *osmo_counter_alloc(const char *name);
Harald Weltebd598e32011-08-16 23:26:52 +020041
42/*! \brief Free the specified counter
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010043 * \param[in] ctr Counter
Harald Weltebd598e32011-08-16 23:26:52 +020044 */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020045void osmo_counter_free(struct osmo_counter *ctr);
Harald Welteec8b4502010-02-20 20:34:29 +010046
Jacob Erlbecke5b0fe22015-10-19 15:00:59 +020047/*! \brief Iterate over all counters
48 * \param[in] handle_counter Call-back function, aborts if rc < 0
Harald Weltebd598e32011-08-16 23:26:52 +020049 * \param[in] data Private dtata handed through to \a handle_counter
50 */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020051int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), void *data);
Harald Welteec8b4502010-02-20 20:34:29 +010052
Harald Weltebd598e32011-08-16 23:26:52 +020053/*! \brief Resolve counter by human-readable name
54 * \param[in] name human-readable name of counter
55 * \returns pointer to counter (\ref osmo_counter) or NULL otherwise
56 */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020057struct osmo_counter *osmo_counter_get_by_name(const char *name);
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +010058
59/*! \brief Return the counter difference since the last call to this function */
60int osmo_counter_difference(struct osmo_counter *ctr);