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 { |
Harald Welte | cf73478 | 2010-05-13 12:53:35 +0200 | [diff] [blame] | 36 | /* 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] | 37 | const char *group_name_prefix; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 38 | /* The human-readable description of the group */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 39 | const char *group_description; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 40 | /* The number of counters in this group */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 41 | const unsigned int num_ctr; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 42 | /* Pointer to array of counter names */ |
Holger Hans Peter Freyther | 75bd69b | 2010-05-23 21:14:32 +0800 | [diff] [blame] | 43 | const struct rate_ctr_desc *ctr_desc; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 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; |
Harald Welte | dd178b2 | 2010-05-13 12:50:44 +0200 | [diff] [blame] | 52 | /* The index of this ctr_group within its class */ |
Harald Welte | 087fcff | 2010-05-13 12:16:17 +0200 | [diff] [blame] | 53 | unsigned int idx; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 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 */ |