Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 2 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 3 | /*! \defgroup rate_ctr Rate counters |
| 4 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | * \file rate_ctr.h */ |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 6 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 9 | #include <osmocom/core/linuxlist.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 10 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 11 | /*! Number of rate counter intervals */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 12 | #define RATE_CTR_INTV_NUM 4 |
| 13 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 14 | /*! Rate counter interval */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 15 | enum rate_ctr_intv { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 16 | 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 Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 22 | /*! data we keep for each of the intervals */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 23 | struct rate_ctr_per_intv { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 24 | uint64_t last; /*!< counter value in last interval */ |
| 25 | uint64_t rate; /*!< counter rate */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 28 | /*! data we keep for each actual value */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 29 | struct rate_ctr { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 30 | uint64_t current; /*!< current value */ |
| 31 | uint64_t previous; /*!< previous value, used for delta */ |
| 32 | /*! per-interval data */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 33 | struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM]; |
| 34 | }; |
| 35 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 36 | /*! rate counter description */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 37 | struct rate_ctr_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 38 | const char *name; /*!< name of the counter */ |
| 39 | const char *description;/*!< description of the counter */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 42 | /*! description of a rate counter group */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 43 | struct rate_ctr_group_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 44 | /*! The prefix to the name of all counters in this group */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 45 | const char *group_name_prefix; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! The human-readable description of the group */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 47 | const char *group_description; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 48 | /*! The class to which this group belongs */ |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 49 | int class_id; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | /*! The number of counters in this group */ |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 51 | unsigned int num_ctr; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 52 | /*! Pointer to array of counter names */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 53 | const struct rate_ctr_desc *ctr_desc; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 56 | /*! One instance of a counter group class */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 57 | struct rate_ctr_group { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 58 | /*! Linked list of all counter groups in the system */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 59 | struct llist_head list; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 60 | /*! Pointer to the counter group class */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 61 | const struct rate_ctr_group_desc *desc; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 62 | /*! The index of this ctr_group within its class */ |
Harald Welte | 087fcff | 2010-05-13 12:16:17 +0200 | [diff] [blame] | 63 | unsigned int idx; |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 64 | /*! Optional string-based identifier to be used instead of index at report time */ |
| 65 | char *name; |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 66 | /*! Actual counter structures below. Don't access it directly, use APIs below! */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 67 | struct rate_ctr ctr[0]; |
| 68 | }; |
| 69 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 70 | struct 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 Freyther | 495b0db | 2015-11-04 14:39:37 +0100 | [diff] [blame] | 74 | static inline void rate_ctr_group_upd_idx(struct rate_ctr_group *grp, unsigned int idx) |
| 75 | { |
| 76 | grp->idx = idx; |
| 77 | } |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 78 | void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name); |
Holger Hans Peter Freyther | 495b0db | 2015-11-04 14:39:37 +0100 | [diff] [blame] | 79 | |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 80 | struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx); |
| 81 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 82 | void rate_ctr_group_free(struct rate_ctr_group *grp); |
| 83 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 84 | /*! Increment the counter by \a inc |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 85 | * \param ctr \ref rate_ctr to increment |
| 86 | * \param inc quantity to increment \a ctr by */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 87 | void rate_ctr_add(struct rate_ctr *ctr, int inc); |
| 88 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 89 | /*! Increment the counter by 1 |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 90 | * \param ctr \ref rate_ctr to increment */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 91 | static inline void rate_ctr_inc(struct rate_ctr *ctr) |
| 92 | { |
| 93 | rate_ctr_add(ctr, 1); |
| 94 | } |
| 95 | |
Harald Welte | 175a4ae | 2018-02-24 14:43:34 +0100 | [diff] [blame] | 96 | /*! Increment the counter by 1 |
| 97 | * \param ctrg \ref rate_ctr_group of counter |
| 98 | * \param idx index into \a ctrg counter group */ |
| 99 | static inline void rate_ctr_inc2(struct rate_ctr_group *ctrg, unsigned int idx) |
| 100 | { |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 101 | rate_ctr_inc(rate_ctr_group_get_ctr(ctrg, idx)); |
Harald Welte | 175a4ae | 2018-02-24 14:43:34 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 105 | /*! Return the counter difference since the last call to this function */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 106 | int64_t rate_ctr_difference(struct rate_ctr *ctr); |
| 107 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 108 | int rate_ctr_init(void *tall_ctx); |
| 109 | |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 110 | struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx); |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 111 | const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name); |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 112 | |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 113 | typedef int (*rate_ctr_handler_t)( |
| 114 | struct rate_ctr_group *, struct rate_ctr *, |
| 115 | const struct rate_ctr_desc *, void *); |
| 116 | typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *); |
| 117 | |
| 118 | |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 119 | int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, |
| 120 | rate_ctr_handler_t handle_counter, void *data); |
| 121 | |
| 122 | int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data); |
| 123 | |
Daniel Willmann | 26a9539 | 2020-07-14 18:04:18 +0200 | [diff] [blame] | 124 | void rate_ctr_reset(struct rate_ctr *ctr); |
| 125 | void rate_ctr_group_reset(struct rate_ctr_group *ctrg); |
| 126 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 127 | /*! @} */ |