blob: 5ebb5fc0ba8df354410a1f542fd1e1ee77ec3389 [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
Neels Hofmeyr87e45502017-06-20 00:17:59 +02004 * Common routines regarding statistics */
Harald Weltebd598e32011-08-16 23:26:52 +02005
6/*! structure representing a single counter */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +02007struct osmo_counter {
Neels Hofmeyr87e45502017-06-20 00:17:59 +02008 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 Welteec8b4502010-02-20 20:34:29 +010013};
14
Neels Hofmeyr87e45502017-06-20 00:17:59 +020015/*! Decrement counter */
Alexander Couzense65315f2016-08-22 21:42:57 +020016static inline void osmo_counter_dec(struct osmo_counter *ctr)
17{
18 ctr->value--;
19}
20
Neels Hofmeyr87e45502017-06-20 00:17:59 +020021/*! 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027/*! 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020033/*! 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020039/*! 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042/*! 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047/*! Iterate over all counters
Jacob Erlbecke5b0fe22015-10-19 15:00:59 +020048 * \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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053/*! Resolve counter by human-readable name
Harald Weltebd598e32011-08-16 23:26:52 +020054 * \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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059/*! Return the counter difference since the last call to this function */
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +010060int osmo_counter_difference(struct osmo_counter *ctr);