blob: f3c03de34c125972940d6d363113f70ac29594de [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welte7b45d602010-05-13 11:35:30 +02002
Harald Welte9327c6d2011-08-17 16:06:06 +02003/*! \defgroup rate_ctr Rate counters
4 * @{
5 */
6
7/*! \file rate_ctr.h */
8
Harald Welte7b45d602010-05-13 11:35:30 +02009#include <stdint.h>
10
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010011#include <osmocom/core/linuxlist.h>
Harald Welte7b45d602010-05-13 11:35:30 +020012
Harald Welte9327c6d2011-08-17 16:06:06 +020013/*! \brief Number of rate counter intervals */
Harald Welte7b45d602010-05-13 11:35:30 +020014#define RATE_CTR_INTV_NUM 4
15
Harald Welte9327c6d2011-08-17 16:06:06 +020016/*! \brief Rate counter interval */
Harald Welte7b45d602010-05-13 11:35:30 +020017enum rate_ctr_intv {
Harald Welte9327c6d2011-08-17 16:06:06 +020018 RATE_CTR_INTV_SEC, /*!< \brief last second */
19 RATE_CTR_INTV_MIN, /*!< \brief last minute */
20 RATE_CTR_INTV_HOUR, /*!< \brief last hour */
21 RATE_CTR_INTV_DAY, /*!< \brief last day */
Harald Welte7b45d602010-05-13 11:35:30 +020022};
23
Harald Welte9327c6d2011-08-17 16:06:06 +020024/*! \brief data we keep for each of the intervals */
Harald Welte7b45d602010-05-13 11:35:30 +020025struct rate_ctr_per_intv {
Harald Welte9327c6d2011-08-17 16:06:06 +020026 uint64_t last; /*!< \brief counter value in last interval */
27 uint64_t rate; /*!< \brief counter rate */
Harald Welte7b45d602010-05-13 11:35:30 +020028};
29
Harald Welte9327c6d2011-08-17 16:06:06 +020030/*! \brief data we keep for each actual value */
Harald Welte7b45d602010-05-13 11:35:30 +020031struct rate_ctr {
Harald Welte9327c6d2011-08-17 16:06:06 +020032 uint64_t current; /*!< \brief current value */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +020033 uint64_t previous; /*!< \brief previous value, used for delta */
Harald Welte9327c6d2011-08-17 16:06:06 +020034 /*! \brief per-interval data */
Harald Welte7b45d602010-05-13 11:35:30 +020035 struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM];
36};
37
Harald Welte9327c6d2011-08-17 16:06:06 +020038/*! \brief rate counter description */
Harald Welte7b45d602010-05-13 11:35:30 +020039struct rate_ctr_desc {
Harald Welte9327c6d2011-08-17 16:06:06 +020040 const char *name; /*!< \brief name of the counter */
41 const char *description;/*!< \brief description of the counter */
Harald Welte7b45d602010-05-13 11:35:30 +020042};
43
Harald Welte9327c6d2011-08-17 16:06:06 +020044/*! \brief description of a rate counter group */
Harald Welte7b45d602010-05-13 11:35:30 +020045struct rate_ctr_group_desc {
Harald Welte9327c6d2011-08-17 16:06:06 +020046 /*! \brief The prefix to the name of all counters in this group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080047 const char *group_name_prefix;
Harald Welte9327c6d2011-08-17 16:06:06 +020048 /*! \brief The human-readable description of the group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080049 const char *group_description;
Harald Welte9327c6d2011-08-17 16:06:06 +020050 /*! \brief The number of counters in this group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080051 const unsigned int num_ctr;
Harald Welte9327c6d2011-08-17 16:06:06 +020052 /*! \brief Pointer to array of counter names */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080053 const struct rate_ctr_desc *ctr_desc;
Harald Welte7b45d602010-05-13 11:35:30 +020054};
55
Harald Welte9327c6d2011-08-17 16:06:06 +020056/*! \brief One instance of a counter group class */
Harald Welte7b45d602010-05-13 11:35:30 +020057struct rate_ctr_group {
Harald Welte9327c6d2011-08-17 16:06:06 +020058 /*! \brief Linked list of all counter groups in the system */
Harald Welte7b45d602010-05-13 11:35:30 +020059 struct llist_head list;
Harald Welte9327c6d2011-08-17 16:06:06 +020060 /*! \brief Pointer to the counter group class */
Harald Welte7b45d602010-05-13 11:35:30 +020061 const struct rate_ctr_group_desc *desc;
Harald Welte9327c6d2011-08-17 16:06:06 +020062 /*! \brief The index of this ctr_group within its class */
Harald Welte087fcff2010-05-13 12:16:17 +020063 unsigned int idx;
Harald Welte9327c6d2011-08-17 16:06:06 +020064 /*! \brief Actual counter structures below */
Harald Welte7b45d602010-05-13 11:35:30 +020065 struct rate_ctr ctr[0];
66};
67
Harald Welte7b45d602010-05-13 11:35:30 +020068struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
69 const struct rate_ctr_group_desc *desc,
70 unsigned int idx);
71
Harald Welte7b45d602010-05-13 11:35:30 +020072void rate_ctr_group_free(struct rate_ctr_group *grp);
73
Harald Welte7b45d602010-05-13 11:35:30 +020074void rate_ctr_add(struct rate_ctr *ctr, int inc);
75
Harald Welte9327c6d2011-08-17 16:06:06 +020076/*! \brief Increment the counter by 1 */
Harald Welte7b45d602010-05-13 11:35:30 +020077static inline void rate_ctr_inc(struct rate_ctr *ctr)
78{
79 rate_ctr_add(ctr, 1);
80}
81
Jacob Erlbeck423c1e52015-10-19 13:45:42 +020082/*! \brief Return the counter difference since the last call to this function */
83int64_t rate_ctr_difference(struct rate_ctr *ctr);
84
Harald Welte7b45d602010-05-13 11:35:30 +020085int rate_ctr_init(void *tall_ctx);
86
Daniel Willmann2d42dde2011-04-08 10:46:18 +020087struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx);
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +020088const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name);
Harald Welte9327c6d2011-08-17 16:06:06 +020089
Jacob Erlbeck423c1e52015-10-19 13:45:42 +020090typedef int (*rate_ctr_handler_t)(
91 struct rate_ctr_group *, struct rate_ctr *,
92 const struct rate_ctr_desc *, void *);
93typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *);
94
95
96/*! \brief Iterate over all counters
97 * \param[in] handle_item Call-back function, aborts if rc < 0
98 * \param[in] data Private data handed through to \a handle_counter
99 */
100int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
101 rate_ctr_handler_t handle_counter, void *data);
102
103int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data);
104
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200105/*! @} */