blob: 865467ce1df903b3c8518ffbee73a2195dd8a7bd [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 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02005 * \file rate_ctr.h */
Harald Welte9327c6d2011-08-17 16:06:06 +02006
Harald Welte7b45d602010-05-13 11:35:30 +02007#include <stdint.h>
8
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01009#include <osmocom/core/linuxlist.h>
Harald Welte7b45d602010-05-13 11:35:30 +020010
Neels Hofmeyr87e45502017-06-20 00:17:59 +020011/*! Number of rate counter intervals */
Harald Welte7b45d602010-05-13 11:35:30 +020012#define RATE_CTR_INTV_NUM 4
13
Neels Hofmeyr87e45502017-06-20 00:17:59 +020014/*! Rate counter interval */
Harald Welte7b45d602010-05-13 11:35:30 +020015enum rate_ctr_intv {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020016 RATE_CTR_INTV_SEC, /*!< last second */
17 RATE_CTR_INTV_MIN, /*!< last minute */
18 RATE_CTR_INTV_HOUR, /*!< last hour */
19 RATE_CTR_INTV_DAY, /*!< last day */
Harald Welte7b45d602010-05-13 11:35:30 +020020};
21
Neels Hofmeyr87e45502017-06-20 00:17:59 +020022/*! data we keep for each of the intervals */
Harald Welte7b45d602010-05-13 11:35:30 +020023struct rate_ctr_per_intv {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020024 uint64_t last; /*!< counter value in last interval */
25 uint64_t rate; /*!< counter rate */
Harald Welte7b45d602010-05-13 11:35:30 +020026};
27
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028/*! data we keep for each actual value */
Harald Welte7b45d602010-05-13 11:35:30 +020029struct rate_ctr {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020030 uint64_t current; /*!< current value */
31 uint64_t previous; /*!< previous value, used for delta */
32 /*! per-interval data */
Harald Welte7b45d602010-05-13 11:35:30 +020033 struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM];
34};
35
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036/*! rate counter description */
Harald Welte7b45d602010-05-13 11:35:30 +020037struct rate_ctr_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038 const char *name; /*!< name of the counter */
39 const char *description;/*!< description of the counter */
Harald Welte7b45d602010-05-13 11:35:30 +020040};
41
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042/*! description of a rate counter group */
Harald Welte7b45d602010-05-13 11:35:30 +020043struct rate_ctr_group_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044 /*! The prefix to the name of all counters in this group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080045 const char *group_name_prefix;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046 /*! The human-readable description of the group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080047 const char *group_description;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048 /*! The class to which this group belongs */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010049 int class_id;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 /*! The number of counters in this group */
Harald Welteae510dc2017-10-03 17:46:14 +080051 unsigned int num_ctr;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 /*! 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020056/*! One instance of a counter group class */
Harald Welte7b45d602010-05-13 11:35:30 +020057struct rate_ctr_group {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020058 /*! Linked list of all counter groups in the system */
Harald Welte7b45d602010-05-13 11:35:30 +020059 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020060 /*! Pointer to the counter group class */
Harald Welte7b45d602010-05-13 11:35:30 +020061 const struct rate_ctr_group_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 /*! The index of this ctr_group within its class */
Harald Welte087fcff2010-05-13 12:16:17 +020063 unsigned int idx;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020064 /*! 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
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010072static inline void rate_ctr_group_upd_idx(struct rate_ctr_group *grp, unsigned int idx)
73{
74 grp->idx = idx;
75}
76
Harald Welte7b45d602010-05-13 11:35:30 +020077void rate_ctr_group_free(struct rate_ctr_group *grp);
78
Neels Hofmeyr87e45502017-06-20 00:17:59 +020079/*! Increment the counter by \a inc
Harald Welte2d2e2cc2016-04-25 12:11:20 +020080 * \param ctr \ref rate_ctr to increment
81 * \param inc quantity to increment \a ctr by */
Harald Welte7b45d602010-05-13 11:35:30 +020082void rate_ctr_add(struct rate_ctr *ctr, int inc);
83
Neels Hofmeyr87e45502017-06-20 00:17:59 +020084/*! Increment the counter by 1
Harald Welte2d2e2cc2016-04-25 12:11:20 +020085 * \param ctr \ref rate_ctr to increment */
Harald Welte7b45d602010-05-13 11:35:30 +020086static inline void rate_ctr_inc(struct rate_ctr *ctr)
87{
88 rate_ctr_add(ctr, 1);
89}
90
Harald Welte175a4ae2018-02-24 14:43:34 +010091/*! Increment the counter by 1
92 * \param ctrg \ref rate_ctr_group of counter
93 * \param idx index into \a ctrg counter group */
94static inline void rate_ctr_inc2(struct rate_ctr_group *ctrg, unsigned int idx)
95{
96 rate_ctr_inc(&ctrg->ctr[idx]);
97}
98
99
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100/*! Return the counter difference since the last call to this function */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200101int64_t rate_ctr_difference(struct rate_ctr *ctr);
102
Harald Welte7b45d602010-05-13 11:35:30 +0200103int rate_ctr_init(void *tall_ctx);
104
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200105struct 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 +0200106const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name);
Harald Welte9327c6d2011-08-17 16:06:06 +0200107
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200108typedef int (*rate_ctr_handler_t)(
109 struct rate_ctr_group *, struct rate_ctr *,
110 const struct rate_ctr_desc *, void *);
111typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *);
112
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! Iterate over all counters
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200115 * \param[in] handle_item Call-back function, aborts if rc < 0
116 * \param[in] data Private data handed through to \a handle_counter
117 */
118int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
119 rate_ctr_handler_t handle_counter, void *data);
120
121int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data);
122
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200123/*! @} */