libosmocore 1.13.1.1-afb4 Osmocom core library

rate_ctr.h
Go to the documentation of this file.
1#pragma once
2
7#include <stdint.h>
8
9#include <osmocom/core/linuxlist.h>
10
12#define RATE_CTR_INTV_NUM 4
13
15 enum rate_ctr_intv {
16 RATE_CTR_INTV_SEC,
17 RATE_CTR_INTV_MIN,
18 RATE_CTR_INTV_HOUR,
19 RATE_CTR_INTV_DAY,
20};
21
23 struct rate_ctr_per_intv {
24 uint64_t last;
25 uint64_t rate;
26};
27
29 struct rate_ctr {
30 uint64_t current;
31 uint64_t previous;
33 struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM];
34};
35
37 struct rate_ctr_desc {
38 const char *name;
39 const char *description;
40};
41
43 struct rate_ctr_group_desc {
45 const char *group_name_prefix;
47 const char *group_description;
49 int class_id;
51 unsigned int num_ctr;
53 const struct rate_ctr_desc *ctr_desc;
54};
55
57 struct rate_ctr_group {
59 struct llist_head list;
61 const struct rate_ctr_group_desc *desc;
63 unsigned int idx;
65 char *name;
67 struct rate_ctr ctr[0];
68};
69
70 struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
71 const struct rate_ctr_group_desc *desc,
72 unsigned int idx);
73
74 static inline void rate_ctr_group_upd_idx(struct rate_ctr_group *grp, unsigned int idx)
75{
76 grp->idx = idx;
77}
78 void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name);
79
80 struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx);
81
82 void rate_ctr_group_free(struct rate_ctr_group *grp);
83
87 void rate_ctr_add(struct rate_ctr *ctr, int inc);
88
93 static inline void rate_ctr_add2(struct rate_ctr_group *ctrg, unsigned int idx, int inc)
94{
95 rate_ctr_add(rate_ctr_group_get_ctr(ctrg, idx), inc);
96}
97
100 static inline void rate_ctr_inc(struct rate_ctr *ctr)
101{
102 rate_ctr_add(ctr, 1);
103}
104
108 static inline void rate_ctr_inc2(struct rate_ctr_group *ctrg, unsigned int idx)
109{
110 rate_ctr_inc(rate_ctr_group_get_ctr(ctrg, idx));
111}
112
113
115int64_t rate_ctr_difference(struct rate_ctr *ctr);
116
117 int rate_ctr_init(void *tall_ctx);
118
119 struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx);
120 const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name);
121
122 typedef int (*rate_ctr_handler_t)(
123 struct rate_ctr_group *, struct rate_ctr *,
124 const struct rate_ctr_desc *, void *);
125 typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *);
126
127
128 int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
129 rate_ctr_handler_t handle_counter, void *data);
130
131 int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data);
132
133 void rate_ctr_reset(struct rate_ctr *ctr);
134 void rate_ctr_group_reset(struct rate_ctr_group *ctrg);
135
name
const char * name
data
uint8_t data[0]
rate_ctr_add2
static void rate_ctr_add2(struct rate_ctr_group *ctrg, unsigned int idx, int inc)
Increment the counter by inc.
Definition: rate_ctr.h:93
rate_ctr_reset
void rate_ctr_reset(struct rate_ctr *ctr)
Reset a rate counter back to zero.
Definition: rate_ctr.c:482
rate_ctr_group_alloc
struct rate_ctr_group * rate_ctr_group_alloc(void *ctx, const struct rate_ctr_group_desc *desc, unsigned int idx)
Allocate a new group of counters according to description.
Definition: rate_ctr.c:213
rate_ctr_get_by_name
const struct rate_ctr * rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name)
Search for counter based on group + name.
Definition: rate_ctr.c:419
rate_ctr_inc2
static void rate_ctr_inc2(struct rate_ctr_group *ctrg, unsigned int idx)
Increment the counter by 1.
Definition: rate_ctr.h:108
rate_ctr_intv
rate_ctr_intv
Rate counter interval.
Definition: rate_ctr.h:15
rate_ctr_group_free
void rate_ctr_group_free(struct rate_ctr_group *grp)
Free the memory for the specified group of counters.
Definition: rate_ctr.c:256
rate_ctr_group_get_ctr
struct rate_ctr * rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx)
Get rate counter from group, identified by index idx.
Definition: rate_ctr.c:271
rate_ctr_group_reset
void rate_ctr_group_reset(struct rate_ctr_group *ctrg)
Reset all counters in a group.
Definition: rate_ctr.c:490
rate_ctr_group_set_name
void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name)
Set a name for the group of counters be used instead of index value at report time.
Definition: rate_ctr.c:281
rate_ctr_group_handler_t
int(* rate_ctr_group_handler_t)(struct rate_ctr_group *, void *)
Definition: rate_ctr.h:125
rate_ctr_for_each_group
int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data)
Iterate over all counter groups.
Definition: rate_ctr.c:465
rate_ctr_group_upd_idx
static void rate_ctr_group_upd_idx(struct rate_ctr_group *grp, unsigned int idx)
Definition: rate_ctr.h:74
RATE_CTR_INTV_NUM
#define RATE_CTR_INTV_NUM
Number of rate counter intervals.
Definition: rate_ctr.h:12
rate_ctr_handler_t
int(* rate_ctr_handler_t)(struct rate_ctr_group *, struct rate_ctr *, const struct rate_ctr_desc *, void *)
Definition: rate_ctr.h:122
rate_ctr_get_group_by_name_idx
struct rate_ctr_group * rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx)
Search for counter group based on group name and index.
Definition: rate_ctr.c:398
rate_ctr_inc
static void rate_ctr_inc(struct rate_ctr *ctr)
Increment the counter by 1.
Definition: rate_ctr.h:100
rate_ctr_init
int rate_ctr_init(void *tall_ctx)
Initialize the counter module.
Definition: rate_ctr.c:367
rate_ctr_difference
int64_t rate_ctr_difference(struct rate_ctr *ctr)
Return the counter difference since the last call to this function.
Definition: rate_ctr.c:293
rate_ctr_for_each_counter
int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, rate_ctr_handler_t handle_counter, void *data)
Iterate over each counter in group and call function.
Definition: rate_ctr.c:443
rate_ctr_add
void rate_ctr_add(struct rate_ctr *ctr, int inc)
Increment the counter by inc.
Definition: rate_ctr.c:287
RATE_CTR_INTV_DAY
@ RATE_CTR_INTV_DAY
last day
Definition: rate_ctr.h:19
RATE_CTR_INTV_SEC
@ RATE_CTR_INTV_SEC
last second
Definition: rate_ctr.h:16
RATE_CTR_INTV_MIN
@ RATE_CTR_INTV_MIN
last minute
Definition: rate_ctr.h:17
RATE_CTR_INTV_HOUR
@ RATE_CTR_INTV_HOUR
last hour
Definition: rate_ctr.h:18
handle_counter
static int handle_counter(struct osmo_counter *counter, void *vctx_)
linuxlist.h
Simple doubly linked list implementation.
desc
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
rate_ctr_desc
rate counter description
Definition: rate_ctr.h:37
rate_ctr_desc::description
const char * description
description of the counter
Definition: rate_ctr.h:39
rate_ctr_desc::name
const char * name
name of the counter
Definition: rate_ctr.h:38
rate_ctr_group_desc
description of a rate counter group
Definition: rate_ctr.h:43
rate_ctr_group_desc::class_id
int class_id
The class to which this group belongs.
Definition: rate_ctr.h:49
rate_ctr_group_desc::ctr_desc
const struct rate_ctr_desc * ctr_desc
Pointer to array of counter names.
Definition: rate_ctr.h:53
rate_ctr_group_desc::num_ctr
unsigned int num_ctr
The number of counters in this group.
Definition: rate_ctr.h:51
rate_ctr_group_desc::group_name_prefix
const char * group_name_prefix
The prefix to the name of all counters in this group.
Definition: rate_ctr.h:45
rate_ctr_group_desc::group_description
const char * group_description
The human-readable description of the group.
Definition: rate_ctr.h:47
rate_ctr_group
One instance of a counter group class.
Definition: rate_ctr.h:57
rate_ctr_group::list
struct llist_head list
Linked list of all counter groups in the system.
Definition: rate_ctr.h:59
rate_ctr_group::ctr
struct rate_ctr ctr[0]
Actual counter structures below.
Definition: rate_ctr.h:67
rate_ctr_group::idx
unsigned int idx
The index of this ctr_group within its class.
Definition: rate_ctr.h:63
rate_ctr_group::name
char * name
Optional string-based identifier to be used instead of index at report time.
Definition: rate_ctr.h:65
rate_ctr_group::desc
const struct rate_ctr_group_desc * desc
Pointer to the counter group class.
Definition: rate_ctr.h:61
rate_ctr_per_intv
data we keep for each of the intervals
Definition: rate_ctr.h:23
rate_ctr_per_intv::rate
uint64_t rate
counter rate
Definition: rate_ctr.h:25
rate_ctr_per_intv::last
uint64_t last
counter value in last interval
Definition: rate_ctr.h:24
rate_ctr
data we keep for each actual value
Definition: rate_ctr.h:29
rate_ctr::current
uint64_t current
current value
Definition: rate_ctr.h:30
rate_ctr::intv
struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM]
per-interval data
Definition: rate_ctr.h:33
rate_ctr::previous
uint64_t previous
previous value, used for delta
Definition: rate_ctr.h:31