blob: b75967a0f025f4013917092529d4b2e1dae9ec82 [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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020013/*! Number of rate counter intervals */
Harald Welte7b45d602010-05-13 11:35:30 +020014#define RATE_CTR_INTV_NUM 4
15
Neels Hofmeyr87e45502017-06-20 00:17:59 +020016/*! Rate counter interval */
Harald Welte7b45d602010-05-13 11:35:30 +020017enum rate_ctr_intv {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020018 RATE_CTR_INTV_SEC, /*!< last second */
19 RATE_CTR_INTV_MIN, /*!< last minute */
20 RATE_CTR_INTV_HOUR, /*!< last hour */
21 RATE_CTR_INTV_DAY, /*!< last day */
Harald Welte7b45d602010-05-13 11:35:30 +020022};
23
Neels Hofmeyr87e45502017-06-20 00:17:59 +020024/*! data we keep for each of the intervals */
Harald Welte7b45d602010-05-13 11:35:30 +020025struct rate_ctr_per_intv {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 uint64_t last; /*!< counter value in last interval */
27 uint64_t rate; /*!< counter rate */
Harald Welte7b45d602010-05-13 11:35:30 +020028};
29
Neels Hofmeyr87e45502017-06-20 00:17:59 +020030/*! data we keep for each actual value */
Harald Welte7b45d602010-05-13 11:35:30 +020031struct rate_ctr {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020032 uint64_t current; /*!< current value */
33 uint64_t previous; /*!< previous value, used for delta */
34 /*! per-interval data */
Harald Welte7b45d602010-05-13 11:35:30 +020035 struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM];
36};
37
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038/*! rate counter description */
Harald Welte7b45d602010-05-13 11:35:30 +020039struct rate_ctr_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020040 const char *name; /*!< name of the counter */
41 const char *description;/*!< description of the counter */
Harald Welte7b45d602010-05-13 11:35:30 +020042};
43
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044/*! description of a rate counter group */
Harald Welte7b45d602010-05-13 11:35:30 +020045struct rate_ctr_group_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046 /*! 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;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048 /*! The human-readable description of the group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080049 const char *group_description;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 /*! The class to which this group belongs */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010051 int class_id;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 /*! The number of counters in this group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080053 const unsigned int num_ctr;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054 /*! Pointer to array of counter names */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080055 const struct rate_ctr_desc *ctr_desc;
Harald Welte7b45d602010-05-13 11:35:30 +020056};
57
Neels Hofmeyr87e45502017-06-20 00:17:59 +020058/*! One instance of a counter group class */
Harald Welte7b45d602010-05-13 11:35:30 +020059struct rate_ctr_group {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020060 /*! Linked list of all counter groups in the system */
Harald Welte7b45d602010-05-13 11:35:30 +020061 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 /*! Pointer to the counter group class */
Harald Welte7b45d602010-05-13 11:35:30 +020063 const struct rate_ctr_group_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020064 /*! The index of this ctr_group within its class */
Harald Welte087fcff2010-05-13 12:16:17 +020065 unsigned int idx;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066 /*! Actual counter structures 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}
78
Harald Welte7b45d602010-05-13 11:35:30 +020079void rate_ctr_group_free(struct rate_ctr_group *grp);
80
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081/*! Increment the counter by \a inc
Harald Welte2d2e2cc2016-04-25 12:11:20 +020082 * \param ctr \ref rate_ctr to increment
83 * \param inc quantity to increment \a ctr by */
Harald Welte7b45d602010-05-13 11:35:30 +020084void rate_ctr_add(struct rate_ctr *ctr, int inc);
85
Neels Hofmeyr87e45502017-06-20 00:17:59 +020086/*! Increment the counter by 1
Harald Welte2d2e2cc2016-04-25 12:11:20 +020087 * \param ctr \ref rate_ctr to increment */
Harald Welte7b45d602010-05-13 11:35:30 +020088static inline void rate_ctr_inc(struct rate_ctr *ctr)
89{
90 rate_ctr_add(ctr, 1);
91}
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! Return the counter difference since the last call to this function */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +020094int64_t rate_ctr_difference(struct rate_ctr *ctr);
95
Harald Welte7b45d602010-05-13 11:35:30 +020096int rate_ctr_init(void *tall_ctx);
97
Daniel Willmann2d42dde2011-04-08 10:46:18 +020098struct 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 +020099const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name);
Harald Welte9327c6d2011-08-17 16:06:06 +0200100
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200101typedef int (*rate_ctr_handler_t)(
102 struct rate_ctr_group *, struct rate_ctr *,
103 const struct rate_ctr_desc *, void *);
104typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *);
105
106
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200107/*! Iterate over all counters
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200108 * \param[in] handle_item Call-back function, aborts if rc < 0
109 * \param[in] data Private data handed through to \a handle_counter
110 */
111int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
112 rate_ctr_handler_t handle_counter, void *data);
113
114int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data);
115
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200116/*! @} */