blob: dba573d96b30ecd9c29421bd26eaf20ffc082be7 [file] [log] [blame]
Harald Welte7b45d602010-05-13 11:35:30 +02001#ifndef _RATE_CTR_H
2#define _RATE_CTR_H
3
4#include <stdint.h>
5
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01006#include <osmocom/core/linuxlist.h>
Harald Welte7b45d602010-05-13 11:35:30 +02007
8#define RATE_CTR_INTV_NUM 4
9
10enum 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 */
18struct rate_ctr_per_intv {
19 uint64_t last;
20 uint64_t rate;
21};
22
23/* for each actual value, we keep the following data */
24struct rate_ctr {
25 uint64_t current;
26 struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM];
27};
28
29struct rate_ctr_desc {
30 const char *name;
31 const char *description;
32};
33
34/* Describe a counter group class */
35struct rate_ctr_group_desc {
Harald Weltecf734782010-05-13 12:53:35 +020036 /* The prefix to the name of all counters in this group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080037 const char *group_name_prefix;
Harald Welte7b45d602010-05-13 11:35:30 +020038 /* The human-readable description of the group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080039 const char *group_description;
Harald Welte7b45d602010-05-13 11:35:30 +020040 /* The number of counters in this group */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080041 const unsigned int num_ctr;
Harald Welte7b45d602010-05-13 11:35:30 +020042 /* Pointer to array of counter names */
Holger Hans Peter Freyther75bd69b2010-05-23 21:14:32 +080043 const struct rate_ctr_desc *ctr_desc;
Harald Welte7b45d602010-05-13 11:35:30 +020044};
45
46/* One instance of a counter group class */
47struct 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 Weltedd178b22010-05-13 12:50:44 +020052 /* The index of this ctr_group within its class */
Harald Welte087fcff2010-05-13 12:16:17 +020053 unsigned int idx;
Harald Welte7b45d602010-05-13 11:35:30 +020054 /* Actual counter structures below */
55 struct rate_ctr ctr[0];
56};
57
58/* Allocate a new group of counters according to description */
59struct 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 */
64void rate_ctr_group_free(struct rate_ctr_group *grp);
65
66/* Add a number to the counter */
67void rate_ctr_add(struct rate_ctr *ctr, int inc);
68
69/* Increment the counter by 1 */
70static inline void rate_ctr_inc(struct rate_ctr *ctr)
71{
72 rate_ctr_add(ctr, 1);
73}
74
75/* Initialize the counter module */
76int rate_ctr_init(void *tall_ctx);
77
78struct vty;
79void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
80 struct rate_ctr_group *ctrg);
81#endif /* RATE_CTR_H */