blob: 0a3eb2ceef19670708de82219c7d4005bf6783fd [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;
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +020064 /*! Optional string-based identifier to be used instead of index at report time */
65 char *name;
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +020066 /*! Actual counter structures below. Don't access it directly, use APIs below! */
Harald Welte7b45d602010-05-13 11:35:30 +020067 struct rate_ctr ctr[0];
68};
69
Harald Welte7b45d602010-05-13 11:35:30 +020070struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
71 const struct rate_ctr_group_desc *desc,
72 unsigned int idx);
73
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010074static inline void rate_ctr_group_upd_idx(struct rate_ctr_group *grp, unsigned int idx)
75{
76 grp->idx = idx;
77}
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +020078void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name);
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010079
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +020080struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx);
81
Harald Welte7b45d602010-05-13 11:35:30 +020082void rate_ctr_group_free(struct rate_ctr_group *grp);
83
Neels Hofmeyr87e45502017-06-20 00:17:59 +020084/*! Increment the counter by \a inc
Harald Welte2d2e2cc2016-04-25 12:11:20 +020085 * \param ctr \ref rate_ctr to increment
86 * \param inc quantity to increment \a ctr by */
Harald Welte7b45d602010-05-13 11:35:30 +020087void rate_ctr_add(struct rate_ctr *ctr, int inc);
88
Harald Welte89692982023-03-23 21:59:03 +010089/*! Increment the counter by \a inc
90 * \param ctrg \ref rate_ctr_group of counter
91 * \param idx index into \a ctrg counter group
92 * \param inc quantity to increment \a ctr by */
93static inline void rate_ctr_add2(struct rate_ctr_group *ctrg, unsigned int idx, int inc)
94{
95 rate_ctr_add(rate_ctr_group_get_ctr(ctrg, idx), inc);
96}
97
Neels Hofmeyr87e45502017-06-20 00:17:59 +020098/*! Increment the counter by 1
Harald Welte2d2e2cc2016-04-25 12:11:20 +020099 * \param ctr \ref rate_ctr to increment */
Harald Welte7b45d602010-05-13 11:35:30 +0200100static inline void rate_ctr_inc(struct rate_ctr *ctr)
101{
102 rate_ctr_add(ctr, 1);
103}
104
Harald Welte175a4ae2018-02-24 14:43:34 +0100105/*! Increment the counter by 1
106 * \param ctrg \ref rate_ctr_group of counter
107 * \param idx index into \a ctrg counter group */
108static inline void rate_ctr_inc2(struct rate_ctr_group *ctrg, unsigned int idx)
109{
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +0200110 rate_ctr_inc(rate_ctr_group_get_ctr(ctrg, idx));
Harald Welte175a4ae2018-02-24 14:43:34 +0100111}
112
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! Return the counter difference since the last call to this function */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200115int64_t rate_ctr_difference(struct rate_ctr *ctr);
116
Harald Welte7b45d602010-05-13 11:35:30 +0200117int rate_ctr_init(void *tall_ctx);
118
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200119struct 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 +0200120const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name);
Harald Welte9327c6d2011-08-17 16:06:06 +0200121
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200122typedef int (*rate_ctr_handler_t)(
123 struct rate_ctr_group *, struct rate_ctr *,
124 const struct rate_ctr_desc *, void *);
125typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *);
126
127
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200128int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
129 rate_ctr_handler_t handle_counter, void *data);
130
131int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data);
132
Daniel Willmann26a95392020-07-14 18:04:18 +0200133void rate_ctr_reset(struct rate_ctr *ctr);
134void rate_ctr_group_reset(struct rate_ctr_group *ctrg);
135
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200136/*! @} */