Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 1 | #ifndef _RATE_CTR_H |
| 2 | #define _RATE_CTR_H |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | #include <osmocore/linuxlist.h> |
| 7 | |
| 8 | #define RATE_CTR_INTV_NUM 4 |
| 9 | |
| 10 | enum rate_ctr_intv { |
| 11 | RATE_CTR_INTV_SEC, |
| 12 | RATE_CTR_INTV_MIN, |
| 13 | RATE_CTR_INTV_HOUR, |
| 14 | RATE_CTR_INTV_DAY, |
| 15 | }; |
| 16 | |
| 17 | /* for each of the intervals, we keep the following values */ |
| 18 | struct rate_ctr_per_intv { |
| 19 | uint64_t last; |
| 20 | uint64_t rate; |
| 21 | }; |
| 22 | |
| 23 | /* for each actual value, we keep the following data */ |
| 24 | struct rate_ctr { |
| 25 | uint64_t current; |
| 26 | struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM]; |
| 27 | }; |
| 28 | |
| 29 | struct rate_ctr_desc { |
| 30 | const char *name; |
| 31 | const char *description; |
| 32 | }; |
| 33 | |
| 34 | /* Describe a counter group class */ |
| 35 | struct rate_ctr_group_desc { |
| 36 | /* The prefix / format string to be used for all counters */ |
| 37 | char *group_prefix_fmt; |
| 38 | /* The human-readable description of the group */ |
| 39 | char *group_description; |
| 40 | /* The number of counters in this group */ |
| 41 | unsigned int num_ctr; |
| 42 | /* Pointer to array of counter names */ |
| 43 | struct rate_ctr_desc *ctr_desc; |
| 44 | }; |
| 45 | |
| 46 | /* One instance of a counter group class */ |
| 47 | struct rate_ctr_group { |
| 48 | /* Linked list of all counter groups in the system */ |
| 49 | struct llist_head list; |
| 50 | /* Pointer to the counter group class */ |
| 51 | const struct rate_ctr_group_desc *desc; |
| 52 | /* The name prefix generated from desc->group_prefix_fmt and index */ |
| 53 | char *name_prefix; |
| 54 | /* Actual counter structures below */ |
| 55 | struct rate_ctr ctr[0]; |
| 56 | }; |
| 57 | |
| 58 | /* Allocate a new group of counters according to description */ |
| 59 | struct rate_ctr_group *rate_ctr_group_alloc(void *ctx, |
| 60 | const struct rate_ctr_group_desc *desc, |
| 61 | unsigned int idx); |
| 62 | |
| 63 | /* Free the memory for the specified group of counters */ |
| 64 | void rate_ctr_group_free(struct rate_ctr_group *grp); |
| 65 | |
| 66 | /* Add a number to the counter */ |
| 67 | void rate_ctr_add(struct rate_ctr *ctr, int inc); |
| 68 | |
| 69 | /* Increment the counter by 1 */ |
| 70 | static inline void rate_ctr_inc(struct rate_ctr *ctr) |
| 71 | { |
| 72 | rate_ctr_add(ctr, 1); |
| 73 | } |
| 74 | |
| 75 | /* Initialize the counter module */ |
| 76 | int rate_ctr_init(void *tall_ctx); |
| 77 | |
| 78 | struct vty; |
| 79 | void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, |
| 80 | struct rate_ctr_group *ctrg); |
| 81 | #endif /* RATE_CTR_H */ |