move hexdump() from logging.c to utils.c
[osmocom-bb.git] / include / osmocore / rate_ctr.h
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 to the name of all counters in this group */
37         const char *group_name_prefix;
38         /* The human-readable description of the group */
39         const char *group_description;
40         /* The number of counters in this group */
41         const unsigned int num_ctr;
42         /* Pointer to array of counter names */
43         const 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 index of this ctr_group within its class */
53         unsigned int idx;
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 */