blob: 47067fb1289f137bb8059d818dfded3f1947cb7e [file] [log] [blame]
Harald Welte7b45d602010-05-13 11:35:30 +02001/* utility routines for keeping conters about events and the event rates */
2
3/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte9327c6d2011-08-17 16:06:06 +020023/*! \addtogroup rate_ctr
24 * @{
Harald Welte96e2a002017-06-12 21:44:18 +020025 * \brief conters about events and their event rates
Harald Welte9327c6d2011-08-17 16:06:06 +020026 */
27
28/*! \file rate_ctr.c */
29
30
Harald Welte7b45d602010-05-13 11:35:30 +020031#include <stdint.h>
Harald Welte7b45d602010-05-13 11:35:30 +020032#include <string.h>
33
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010034#include <osmocom/core/utils.h>
35#include <osmocom/core/linuxlist.h>
36#include <osmocom/core/talloc.h>
37#include <osmocom/core/timer.h>
38#include <osmocom/core/rate_ctr.h>
Harald Welte7b45d602010-05-13 11:35:30 +020039
40static LLIST_HEAD(rate_ctr_groups);
41
42static void *tall_rate_ctr_ctx;
43
Harald Welte9327c6d2011-08-17 16:06:06 +020044/*! \brief Allocate a new group of counters according to description
45 * \param[in] ctx \ref talloc context
46 * \param[in] desc Rate counter group description
47 * \param[in] idx Index of new counter group
48 */
Harald Welte7b45d602010-05-13 11:35:30 +020049struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
50 const struct rate_ctr_group_desc *desc,
51 unsigned int idx)
52{
53 unsigned int size;
54 struct rate_ctr_group *group;
55
56 size = sizeof(struct rate_ctr_group) +
57 desc->num_ctr * sizeof(struct rate_ctr);
58
59 if (!ctx)
60 ctx = tall_rate_ctr_ctx;
61
62 group = talloc_zero_size(ctx, size);
63 if (!group)
64 return NULL;
65
66 group->desc = desc;
Harald Welte087fcff2010-05-13 12:16:17 +020067 group->idx = idx;
Harald Welte7b45d602010-05-13 11:35:30 +020068
69 llist_add(&group->list, &rate_ctr_groups);
70
71 return group;
72}
73
Harald Welte9327c6d2011-08-17 16:06:06 +020074/*! \brief Free the memory for the specified group of counters */
Harald Welte7b45d602010-05-13 11:35:30 +020075void rate_ctr_group_free(struct rate_ctr_group *grp)
76{
77 llist_del(&grp->list);
78 talloc_free(grp);
79}
80
Harald Welte9327c6d2011-08-17 16:06:06 +020081/*! \brief Add a number to the counter */
Harald Welte7b45d602010-05-13 11:35:30 +020082void rate_ctr_add(struct rate_ctr *ctr, int inc)
83{
84 ctr->current += inc;
85}
86
Jacob Erlbeck423c1e52015-10-19 13:45:42 +020087/*! \brief Return the counter difference since the last call to this function */
88int64_t rate_ctr_difference(struct rate_ctr *ctr)
89{
90 int64_t result = ctr->current - ctr->previous;
91 ctr->previous = ctr->current;
92
93 return result;
94}
95
Jacob Erlbeckee702cd2015-11-10 11:38:25 +010096/* TODO: support update intervals > 1s */
97/* TODO: implement this as a special stats reporter */
98
Harald Welte7b45d602010-05-13 11:35:30 +020099static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
100{
101 /* calculate rate over last interval */
102 ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last;
103 /* save current counter for next interval */
104 ctr->intv[intv].last = ctr->current;
Harald Welted2dce6d2010-05-13 13:28:12 +0200105
106 /* update the rate of the next bigger interval. This will
107 * be overwritten when that next larger interval expires */
108 if (intv + 1 < ARRAY_SIZE(ctr->intv))
109 ctr->intv[intv+1].rate += ctr->intv[intv].rate;
Harald Welte7b45d602010-05-13 11:35:30 +0200110}
111
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200112static struct osmo_timer_list rate_ctr_timer;
Harald Welte7b45d602010-05-13 11:35:30 +0200113static uint64_t timer_ticks;
114
115/* The one-second interval has expired */
116static void rate_ctr_group_intv(struct rate_ctr_group *grp)
117{
118 unsigned int i;
119
120 for (i = 0; i < grp->desc->num_ctr; i++) {
121 struct rate_ctr *ctr = &grp->ctr[i];
122
123 interval_expired(ctr, RATE_CTR_INTV_SEC);
124 if ((timer_ticks % 60) == 0)
125 interval_expired(ctr, RATE_CTR_INTV_MIN);
126 if ((timer_ticks % (60*60)) == 0)
127 interval_expired(ctr, RATE_CTR_INTV_HOUR);
128 if ((timer_ticks % (24*60*60)) == 0)
129 interval_expired(ctr, RATE_CTR_INTV_DAY);
130 }
131}
132
133static void rate_ctr_timer_cb(void *data)
134{
135 struct rate_ctr_group *ctrg;
136
137 /* Increment number of ticks before we calculate intervals,
138 * as a counter value of 0 would already wrap all counters */
139 timer_ticks++;
140
141 llist_for_each_entry(ctrg, &rate_ctr_groups, list)
142 rate_ctr_group_intv(ctrg);
143
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200144 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200145}
146
Harald Welte9327c6d2011-08-17 16:06:06 +0200147/*! \brief Initialize the counter module */
Harald Welte7b45d602010-05-13 11:35:30 +0200148int rate_ctr_init(void *tall_ctx)
149{
150 tall_rate_ctr_ctx = tall_ctx;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200151 osmo_timer_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200152 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200153
154 return 0;
155}
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200156
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200157/*! \brief Search for counter group based on group name and index
158 * \param[in] name Name of the counter group you're looking for
159 * \param[in] idx Index inside the counter group
160 * \returns \ref rate_ctr_group or NULL in case of error */
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200161struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx)
162{
163 struct rate_ctr_group *ctrg;
164
165 llist_for_each_entry(ctrg, &rate_ctr_groups, list) {
166 if (!ctrg->desc)
167 continue;
168
169 if (!strcmp(ctrg->desc->group_name_prefix, name) &&
170 ctrg->idx == idx) {
171 return ctrg;
172 }
173 }
174 return NULL;
175}
176
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200177/*! \brief Search for counter based on group + name
178 * \param[in] ctrg pointer to \ref rate_ctr_group
179 * \param[in] name name of counter inside group
180 * \returns \ref rate_ctr or NULL in caes of error
181 */
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200182const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name)
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200183{
184 int i;
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200185 const struct rate_ctr_desc *ctr_desc;
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200186
187 if (!ctrg->desc)
188 return NULL;
189
190 for (i = 0; i < ctrg->desc->num_ctr; i++) {
191 ctr_desc = &ctrg->desc->ctr_desc[i];
192
193 if (!strcmp(ctr_desc->name, name)) {
194 return &ctrg->ctr[i];
195 }
196 }
197 return NULL;
198}
Harald Welte9327c6d2011-08-17 16:06:06 +0200199
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200200/*! \brief Iterate over each counter in group and call function
201 * \param[in] counter group over whose counter to iterate
202 * \param[in] handle_counter function pointer
203 * \param[in] data Data to hand transparently to \ref handle_counter
204 * \returns 0 on success; negative otherwise
205 */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200206int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
207 rate_ctr_handler_t handle_counter, void *data)
208{
209 int rc = 0;
210 int i;
211
212 for (i = 0; i < ctrg->desc->num_ctr; i++) {
213 struct rate_ctr *ctr = &ctrg->ctr[i];
214 rc = handle_counter(ctrg,
215 ctr, &ctrg->desc->ctr_desc[i], data);
216 if (rc < 0)
217 return rc;
218 }
219
220 return rc;
221}
222
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200223/*! \brief Iterate over all counter groups
224 * \param[in] handle_group function pointer of callback function
225 * \param[in] data Data to hand transparently to \ref handle_group
226 * \returns 0 on success; negative otherwise
227 */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200228int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data)
229{
230 struct rate_ctr_group *statg;
231 int rc = 0;
232
233 llist_for_each_entry(statg, &rate_ctr_groups, list) {
234 rc = handle_group(statg, data);
235 if (rc < 0)
236 return rc;
237 }
238
239 return rc;
240}
241
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200242/*! @} */