blob: ffae1b868f2c2059861d0c1ed85a44f93ab40c33 [file] [log] [blame]
Harald Welteae510dc2017-10-03 17:46:14 +08001/* (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
Harald Welte7b45d602010-05-13 11:35:30 +02002 *
3 * All Rights Reserved
4 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
6 *
Harald Welte7b45d602010-05-13 11:35:30 +02007 * 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 *
Harald Welte7b45d602010-05-13 11:35:30 +020017 */
18
Harald Welte9327c6d2011-08-17 16:06:06 +020019/*! \addtogroup rate_ctr
20 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020021 * Counters about events and their event rates.
22 *
Harald Welte53de0d32017-10-16 14:09:16 +020023 * As \ref osmo_counter and \ref osmo_stat_item are concerned only with
24 * a single given value that may be increased/decreased, or the difference
25 * to one given previous value, this module adds some support for keeping
26 * long term information about a given event rate.
27 *
28 * A \ref rate_ctr keeps information on the amount of events per second,
29 * per minute, per hour and per day.
30 *
31 * \ref rate_ctr come in groups: An application describes a group of counters
32 * with their names and identities once in a (typically const) \ref
33 * rate_ctr_group_desc.
34 *
35 * As objects (such as e.g. a subscriber or a PDP context) are
36 * allocated dynamically at runtime, the application calls \ref
37 * rate_ctr_group_alloc with a refernce to the \ref
38 * rate_ctr_group_desc, which causes the library to allocate one set of
39 * \ref rate_ctr: One for each in the group.
40 *
41 * The application then uses functions like \ref rate_ctr_add or \ref
42 * rate_ctr_inc to increment the value as certain events (e.g. location
43 * update) happens.
44 *
45 * The library internally keeps a timer once per second which iterates
46 * over all registered counters and which updates the per-second,
47 * per-minute, per-hour and per-day averages based on the current
48 * value.
49 *
50 * The counters can be reported using \ref stats or by VTY
51 * introspection, as well as by any application-specific code accessing
52 * the \ref rate_ctr.intv array directly.
53 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020054 * \file rate_ctr.c */
Harald Welte9327c6d2011-08-17 16:06:06 +020055
Harald Welteae510dc2017-10-03 17:46:14 +080056#include <stdbool.h>
Harald Welte7b45d602010-05-13 11:35:30 +020057#include <stdint.h>
Harald Welte7b45d602010-05-13 11:35:30 +020058#include <string.h>
59
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010060#include <osmocom/core/utils.h>
61#include <osmocom/core/linuxlist.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/core/timer.h>
64#include <osmocom/core/rate_ctr.h>
Harald Welteae510dc2017-10-03 17:46:14 +080065#include <osmocom/core/logging.h>
Harald Welte7b45d602010-05-13 11:35:30 +020066
67static LLIST_HEAD(rate_ctr_groups);
Harald Welteea8272c2019-05-10 11:52:02 +020068
Harald Welte7b45d602010-05-13 11:35:30 +020069static void *tall_rate_ctr_ctx;
70
Harald Welteae510dc2017-10-03 17:46:14 +080071
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +020072static bool rate_ctrl_group_desc_validate(const struct rate_ctr_group_desc *desc)
Harald Welteae510dc2017-10-03 17:46:14 +080073{
74 unsigned int i;
Harald Welteb48e82c2017-10-24 18:35:24 +020075 const struct rate_ctr_desc *ctr_desc;
Harald Welteae510dc2017-10-03 17:46:14 +080076
77 if (!desc) {
78 LOGP(DLGLOBAL, LOGL_ERROR, "NULL is not a valid counter group descriptor\n");
79 return false;
80 }
Harald Welteb48e82c2017-10-24 18:35:24 +020081 ctr_desc = desc->ctr_desc;
Harald Welteae510dc2017-10-03 17:46:14 +080082
83 DEBUGP(DLGLOBAL, "validating counter group %p(%s) with %u counters\n", desc,
84 desc->group_name_prefix, desc->num_ctr);
85
86 if (!osmo_identifier_valid(desc->group_name_prefix)) {
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +020087 LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter group identifier\n",
88 desc->group_name_prefix);
Harald Welteae510dc2017-10-03 17:46:14 +080089 return false;
90 }
91
92 for (i = 0; i < desc->num_ctr; i++) {
93 if (!osmo_identifier_valid(ctr_desc[i].name)) {
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +020094 LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter identifier\n",
95 ctr_desc[i].name);
Harald Welteae510dc2017-10-03 17:46:14 +080096 return false;
97 }
98 }
99
100 return true;
101}
102
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200103/* return 'in' if it doesn't contain any '.'; otherwise allocate a copy and
Harald Welteae510dc2017-10-03 17:46:14 +0800104 * replace all '.' with ':' */
105static char *mangle_identifier_ifneeded(const void *ctx, const char *in)
106{
107 char *out;
108 unsigned int i;
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200109 bool modified = false;
Harald Welteae510dc2017-10-03 17:46:14 +0800110
111 if (!in)
112 return NULL;
113
114 if (!strchr(in, '.'))
115 return (char *)in;
116
117 out = talloc_strdup(ctx, in);
118 OSMO_ASSERT(out);
119
120 for (i = 0; i < strlen(out); i++) {
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200121 if (out[i] == '.') {
Harald Welteae510dc2017-10-03 17:46:14 +0800122 out[i] = ':';
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200123 modified = true;
124 }
Harald Welteae510dc2017-10-03 17:46:14 +0800125 }
126
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200127 if (modified)
128 LOGP(DLGLOBAL, LOGL_NOTICE, "counter group name mangled: '%s' -> '%s'\n",
129 in, out);
130
Harald Welteae510dc2017-10-03 17:46:14 +0800131 return out;
132}
133
134/* "mangle" a rate counter group descriptor, i.e. replace any '.' with ':' */
135static struct rate_ctr_group_desc *
136rate_ctr_group_desc_mangle(void *ctx, const struct rate_ctr_group_desc *desc)
137{
138 struct rate_ctr_group_desc *desc_new = talloc_zero(ctx, struct rate_ctr_group_desc);
139 int i;
140
141 OSMO_ASSERT(desc_new);
142
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200143 LOGP(DLGLOBAL, LOGL_INFO, "Needed to mangle counter group '%s' names: it is still using '.' as "
144 "separator, which is not allowed. please consider updating the application\n",
145 desc->group_name_prefix);
146
Harald Welteae510dc2017-10-03 17:46:14 +0800147 /* mangle the name_prefix but copy/keep the rest */
148 desc_new->group_name_prefix = mangle_identifier_ifneeded(desc_new, desc->group_name_prefix);
149 desc_new->group_description = desc->group_description;
150 desc_new->class_id = desc->class_id;
151 desc_new->num_ctr = desc->num_ctr;
152 desc_new->ctr_desc = talloc_array(desc_new, struct rate_ctr_desc, desc_new->num_ctr);
153 OSMO_ASSERT(desc_new->ctr_desc);
154
155 for (i = 0; i < desc->num_ctr; i++) {
156 struct rate_ctr_desc *ctrd_new = (struct rate_ctr_desc *) desc_new->ctr_desc;
157 const struct rate_ctr_desc *ctrd = desc->ctr_desc;
158
159 if (!ctrd[i].name) {
160 LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s'[%d] == NULL, aborting\n",
161 desc->group_name_prefix, i);
162 goto err_free;
163 }
164
165 ctrd_new[i].name = mangle_identifier_ifneeded(desc_new->ctr_desc, ctrd[i].name);
166 ctrd_new[i].description = ctrd[i].description;
167 }
168
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200169 if (!rate_ctrl_group_desc_validate(desc_new)) {
Harald Welteae510dc2017-10-03 17:46:14 +0800170 /* simple mangling of identifiers ('.' -> ':') was not sufficient to render a valid
171 * descriptor, we have to bail out */
172 LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' still invalid after mangling\n",
173 desc->group_name_prefix);
174 goto err_free;
175 }
176
Harald Welteae510dc2017-10-03 17:46:14 +0800177 return desc_new;
178err_free:
179 talloc_free(desc_new);
180 return NULL;
181}
182
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100183/*! Find an unused index for this rate counter group.
184 * \param[in] name Name of the counter group
185 * \returns the largest used index number + 1, or 0 if none exist yet. */
186static unsigned int rate_ctr_get_unused_name_idx(const char *name)
187{
188 unsigned int idx = 0;
189 struct rate_ctr_group *ctrg;
190
191 llist_for_each_entry(ctrg, &rate_ctr_groups, list) {
192 if (!ctrg->desc)
193 continue;
194
195 if (strcmp(ctrg->desc->group_name_prefix, name))
196 continue;
197
198 if (idx <= ctrg->idx)
199 idx = ctrg->idx + 1;
200 }
201 return idx;
202}
203
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200204/*! Allocate a new group of counters according to description
Vadim Yanitskiyed3a3c62019-03-26 00:57:45 +0700205 * \param[in] ctx parent talloc context
Harald Welte9327c6d2011-08-17 16:06:06 +0200206 * \param[in] desc Rate counter group description
207 * \param[in] idx Index of new counter group
208 */
Harald Welte7b45d602010-05-13 11:35:30 +0200209struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
210 const struct rate_ctr_group_desc *desc,
211 unsigned int idx)
212{
213 unsigned int size;
214 struct rate_ctr_group *group;
215
Harald Welted589f1d2017-12-18 17:13:38 +0100216 if (rate_ctr_get_group_by_name_idx(desc->group_name_prefix, idx)) {
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100217 unsigned int new_idx = rate_ctr_get_unused_name_idx(desc->group_name_prefix);
218 LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' already exists for index %u,"
219 " instead using index %u. This is a software bug that needs fixing.\n",
220 desc->group_name_prefix, idx, new_idx);
221 idx = new_idx;
Harald Welted589f1d2017-12-18 17:13:38 +0100222 }
Max3ef14a22017-12-15 20:19:10 +0100223
Harald Welte7b45d602010-05-13 11:35:30 +0200224 size = sizeof(struct rate_ctr_group) +
225 desc->num_ctr * sizeof(struct rate_ctr);
226
227 if (!ctx)
228 ctx = tall_rate_ctr_ctx;
229
230 group = talloc_zero_size(ctx, size);
231 if (!group)
232 return NULL;
233
Neels Hofmeyr10ee73a2017-11-16 18:31:57 +0100234 /* attempt to mangle all '.' in identifiers to ':' for backwards compat */
Pau Espin Pedroldfc52a12018-06-27 16:53:31 +0200235 if (!rate_ctrl_group_desc_validate(desc)) {
Neels Hofmeyr10ee73a2017-11-16 18:31:57 +0100236 desc = rate_ctr_group_desc_mangle(group, desc);
237 if (!desc) {
238 talloc_free(group);
239 return NULL;
240 }
241 }
242
Harald Welte7b45d602010-05-13 11:35:30 +0200243 group->desc = desc;
Harald Welte087fcff2010-05-13 12:16:17 +0200244 group->idx = idx;
Harald Welte7b45d602010-05-13 11:35:30 +0200245
246 llist_add(&group->list, &rate_ctr_groups);
247
248 return group;
249}
250
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200251/*! Free the memory for the specified group of counters */
Harald Welte7b45d602010-05-13 11:35:30 +0200252void rate_ctr_group_free(struct rate_ctr_group *grp)
253{
Maxfb6f43e2019-03-18 15:41:26 +0100254 if (!grp)
255 return;
256
257 if (!llist_empty(&grp->list))
258 llist_del(&grp->list);
Harald Welte7b45d602010-05-13 11:35:30 +0200259 talloc_free(grp);
260}
261
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +0200262/*! Get rate counter from group, identified by index idx
263 * \param[in] grp Rate counter group
264 * \param[in] idx Index of the counter to retrieve
265 * \returns rate counter requested
266 */
267struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx)
268{
269 return &grp->ctr[idx];
270}
271
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200272/*! Set a name for the group of counters be used instead of index value
273 at report time.
274 * \param[in] grp Rate counter group
275 * \param[in] name Name identifier to assign to the rate counter group
276 */
277void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name)
278{
279 osmo_talloc_replace_string(grp, &grp->name, name);
280}
281
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200282/*! Add a number to the counter */
Harald Welte7b45d602010-05-13 11:35:30 +0200283void rate_ctr_add(struct rate_ctr *ctr, int inc)
284{
285 ctr->current += inc;
286}
287
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200288/*! Return the counter difference since the last call to this function */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200289int64_t rate_ctr_difference(struct rate_ctr *ctr)
290{
291 int64_t result = ctr->current - ctr->previous;
292 ctr->previous = ctr->current;
293
294 return result;
295}
296
Jacob Erlbeckee702cd2015-11-10 11:38:25 +0100297/* TODO: support update intervals > 1s */
298/* TODO: implement this as a special stats reporter */
299
Harald Welte7b45d602010-05-13 11:35:30 +0200300static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
301{
302 /* calculate rate over last interval */
303 ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last;
304 /* save current counter for next interval */
305 ctr->intv[intv].last = ctr->current;
Harald Welted2dce6d2010-05-13 13:28:12 +0200306
307 /* update the rate of the next bigger interval. This will
308 * be overwritten when that next larger interval expires */
309 if (intv + 1 < ARRAY_SIZE(ctr->intv))
310 ctr->intv[intv+1].rate += ctr->intv[intv].rate;
Harald Welte7b45d602010-05-13 11:35:30 +0200311}
312
Harald Welteea8272c2019-05-10 11:52:02 +0200313static struct osmo_timer_list rate_ctr_timer;
Harald Welte7b45d602010-05-13 11:35:30 +0200314static uint64_t timer_ticks;
315
316/* The one-second interval has expired */
317static void rate_ctr_group_intv(struct rate_ctr_group *grp)
318{
319 unsigned int i;
320
321 for (i = 0; i < grp->desc->num_ctr; i++) {
322 struct rate_ctr *ctr = &grp->ctr[i];
323
324 interval_expired(ctr, RATE_CTR_INTV_SEC);
325 if ((timer_ticks % 60) == 0)
326 interval_expired(ctr, RATE_CTR_INTV_MIN);
327 if ((timer_ticks % (60*60)) == 0)
328 interval_expired(ctr, RATE_CTR_INTV_HOUR);
329 if ((timer_ticks % (24*60*60)) == 0)
330 interval_expired(ctr, RATE_CTR_INTV_DAY);
331 }
332}
333
334static void rate_ctr_timer_cb(void *data)
335{
336 struct rate_ctr_group *ctrg;
337
338 /* Increment number of ticks before we calculate intervals,
339 * as a counter value of 0 would already wrap all counters */
340 timer_ticks++;
341
342 llist_for_each_entry(ctrg, &rate_ctr_groups, list)
343 rate_ctr_group_intv(ctrg);
344
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200345 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200346}
347
Harald Welte53de0d32017-10-16 14:09:16 +0200348/*! Initialize the counter module. Call this once from your application.
349 * \param[in] tall_ctx Talloc context from which rate_ctr_group will be allocated
350 * \returns 0 on success; negative on error */
Harald Welte7b45d602010-05-13 11:35:30 +0200351int rate_ctr_init(void *tall_ctx)
352{
Harald Welte5eb67c22021-11-14 20:45:07 +0100353 /* ignore repeated initialization */
354 if (osmo_timer_pending(&rate_ctr_timer))
355 return 0;
356
Harald Welte7b45d602010-05-13 11:35:30 +0200357 tall_rate_ctr_ctx = tall_ctx;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200358 osmo_timer_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL);
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200359 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200360
361 return 0;
362}
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200363
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200364/*! Search for counter group based on group name and index
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200365 * \param[in] name Name of the counter group you're looking for
366 * \param[in] idx Index inside the counter group
367 * \returns \ref rate_ctr_group or NULL in case of error */
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200368struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx)
369{
370 struct rate_ctr_group *ctrg;
371
372 llist_for_each_entry(ctrg, &rate_ctr_groups, list) {
373 if (!ctrg->desc)
374 continue;
375
376 if (!strcmp(ctrg->desc->group_name_prefix, name) &&
377 ctrg->idx == idx) {
378 return ctrg;
379 }
380 }
381 return NULL;
382}
383
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200384/*! Search for counter based on group + name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200385 * \param[in] ctrg pointer to \ref rate_ctr_group
386 * \param[in] name name of counter inside group
Harald Welte53de0d32017-10-16 14:09:16 +0200387 * \returns \ref rate_ctr or NULL in case of error
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200388 */
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200389const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name)
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200390{
391 int i;
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200392 const struct rate_ctr_desc *ctr_desc;
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200393
394 if (!ctrg->desc)
395 return NULL;
396
397 for (i = 0; i < ctrg->desc->num_ctr; i++) {
398 ctr_desc = &ctrg->desc->ctr_desc[i];
399
400 if (!strcmp(ctr_desc->name, name)) {
401 return &ctrg->ctr[i];
402 }
403 }
404 return NULL;
405}
Harald Welte9327c6d2011-08-17 16:06:06 +0200406
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200407/*! Iterate over each counter in group and call function
Vadim Yanitskiyc7610442019-03-26 00:48:30 +0700408 * \param[in] ctrg counter group over which to iterate
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200409 * \param[in] handle_counter function pointer
Vadim Yanitskiyed3a3c62019-03-26 00:57:45 +0700410 * \param[in] data Data to hand transparently to handle_counter()
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200411 * \returns 0 on success; negative otherwise
412 */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200413int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
414 rate_ctr_handler_t handle_counter, void *data)
415{
416 int rc = 0;
417 int i;
418
419 for (i = 0; i < ctrg->desc->num_ctr; i++) {
420 struct rate_ctr *ctr = &ctrg->ctr[i];
421 rc = handle_counter(ctrg,
422 ctr, &ctrg->desc->ctr_desc[i], data);
423 if (rc < 0)
424 return rc;
425 }
426
427 return rc;
428}
429
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200430/*! Iterate over all counter groups
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200431 * \param[in] handle_group function pointer of callback function
Vadim Yanitskiyed3a3c62019-03-26 00:57:45 +0700432 * \param[in] data Data to hand transparently to handle_group()
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200433 * \returns 0 on success; negative otherwise
434 */
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200435int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data)
436{
437 struct rate_ctr_group *statg;
438 int rc = 0;
439
440 llist_for_each_entry(statg, &rate_ctr_groups, list) {
441 rc = handle_group(statg, data);
442 if (rc < 0)
443 return rc;
444 }
445
446 return rc;
447}
448
Daniel Willmann26a95392020-07-14 18:04:18 +0200449/*! Reset a rate counter back to zero
450 * \param[in] ctr counter to reset
451 */
452void rate_ctr_reset(struct rate_ctr *ctr)
453{
454 memset(ctr, 0, sizeof(*ctr));
455}
456
457/*! Reset all counters in a group
458 * \param[in] ctrg counter group to reset
459 */
460void rate_ctr_group_reset(struct rate_ctr_group *ctrg)
461{
462 int i;
463
464 for (i = 0; i < ctrg->desc->num_ctr; i++) {
465 struct rate_ctr *ctr = &ctrg->ctr[i];
466 rate_ctr_reset(ctr);
467 }
468}
469
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200470/*! @} */