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 | * @{ |
| 5 | */ |
| 6 | |
| 7 | /*! \file rate_ctr.h */ |
| 8 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 11 | #include <osmocom/core/linuxlist.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 12 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 13 | /*! \brief Number of rate counter intervals */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 14 | #define RATE_CTR_INTV_NUM 4 |
| 15 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 16 | /*! \brief Rate counter interval */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 17 | enum rate_ctr_intv { |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 18 | 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 Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 24 | /*! \brief data we keep for each of the intervals */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 25 | struct rate_ctr_per_intv { |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 26 | uint64_t last; /*!< \brief counter value in last interval */ |
| 27 | uint64_t rate; /*!< \brief counter rate */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 30 | /*! \brief data we keep for each actual value */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 31 | struct rate_ctr { |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 32 | uint64_t current; /*!< \brief current value */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 33 | uint64_t previous; /*!< \brief previous value, used for delta */ |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 34 | /*! \brief per-interval data */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 35 | struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM]; |
| 36 | }; |
| 37 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 38 | /*! \brief rate counter description */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 39 | struct rate_ctr_desc { |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 40 | const char *name; /*!< \brief name of the counter */ |
| 41 | const char *description;/*!< \brief description of the counter */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 44 | /*! \brief description of a rate counter group */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 45 | struct rate_ctr_group_desc { |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 46 | /*! \brief 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] | 47 | const char *group_name_prefix; |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 48 | /*! \brief The human-readable description of the group */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 49 | const char *group_description; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 50 | /*! \brief The class to which this group belongs */ |
| 51 | int class_id; |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 52 | /*! \brief The number of counters in this group */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 53 | const unsigned int num_ctr; |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 54 | /*! \brief Pointer to array of counter names */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 55 | const struct rate_ctr_desc *ctr_desc; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 58 | /*! \brief One instance of a counter group class */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 59 | struct rate_ctr_group { |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 60 | /*! \brief Linked list of all counter groups in the system */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 61 | struct llist_head list; |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 62 | /*! \brief Pointer to the counter group class */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 63 | const struct rate_ctr_group_desc *desc; |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 64 | /*! \brief The index of this ctr_group within its class */ |
Harald Welte | 087fcff | 2010-05-13 12:16:17 +0200 | [diff] [blame] | 65 | unsigned int idx; |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 66 | /*! \brief Actual counter structures 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 | } |
| 78 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 79 | void rate_ctr_group_free(struct rate_ctr_group *grp); |
| 80 | |
Neels Hofmeyr | 9e57a5a | 2015-12-21 11:20:14 +0100 | [diff] [blame] | 81 | /*! \brief Increment the counter by \a inc */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 82 | void rate_ctr_add(struct rate_ctr *ctr, int inc); |
| 83 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 84 | /*! \brief Increment the counter by 1 */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 85 | static inline void rate_ctr_inc(struct rate_ctr *ctr) |
| 86 | { |
| 87 | rate_ctr_add(ctr, 1); |
| 88 | } |
| 89 | |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 90 | /*! \brief Return the counter difference since the last call to this function */ |
| 91 | int64_t rate_ctr_difference(struct rate_ctr *ctr); |
| 92 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 93 | int rate_ctr_init(void *tall_ctx); |
| 94 | |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 95 | 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] | 96 | 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] | 97 | |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 98 | typedef int (*rate_ctr_handler_t)( |
| 99 | struct rate_ctr_group *, struct rate_ctr *, |
| 100 | const struct rate_ctr_desc *, void *); |
| 101 | typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *); |
| 102 | |
| 103 | |
| 104 | /*! \brief Iterate over all counters |
| 105 | * \param[in] handle_item Call-back function, aborts if rc < 0 |
| 106 | * \param[in] data Private data handed through to \a handle_counter |
| 107 | */ |
| 108 | int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, |
| 109 | rate_ctr_handler_t handle_counter, void *data); |
| 110 | |
| 111 | int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data); |
| 112 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 113 | /*! @} */ |