blob: 804972bf4c532a5c30cfa0b197786b8676e1a308 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file stat_item.c
Harald Welte781951b2017-10-15 19:24:57 +02002 * utility routines for keeping statistical values */
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02003/*
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02004 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Weltee08da972017-11-13 01:00:26 +09005 * (C) 2015 by sysmocom - s.f.m.c. GmbH
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020021 */
22
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010023/*! \addtogroup osmo_stat_item
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020024 * @{
Harald Welte781951b2017-10-15 19:24:57 +020025 *
26 * This osmo_stat_item module adds instrumentation capabilities to
27 * gather measurement and statistical values in a similar fashion to
28 * what we have as \ref osmo_counter_group.
29 *
30 * As opposed to counters, osmo_stat_item do not increment but consist
31 * of a configurable-sized FIFO, which can store not only the current
32 * (most recent) value, but also historic values.
33 *
34 * The only supported value type is an int32_t.
35 *
Oliver Smith61401942021-03-26 10:18:37 +010036 * Getting values from osmo_stat_item is usually done at a high level
37 * through the stats API (stats.c). It uses item->stats_next_id to
38 * store what has been sent to all enabled reporters. It is also
39 * possible to read from osmo_stat_item directly, without modifying
40 * its state, by storing next_id outside of osmo_stat_item.
Harald Welte781951b2017-10-15 19:24:57 +020041 *
42 * Each value stored in the FIFO of an osmo_stat_item has an associated
Oliver Smith61401942021-03-26 10:18:37 +010043 * value_id. The value_id is increased with each value, so (until the
44 * counter wraps) more recent values will have higher values.
Harald Welte781951b2017-10-15 19:24:57 +020045 *
46 * When a new value is set, the oldest value in the FIFO gets silently
47 * overwritten. Lost values are skipped when getting values from the
48 * item.
49 *
50 */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020051
Oliver Smith43686da2021-03-26 14:33:21 +010052/* Struct overview:
Oliver Smith43686da2021-03-26 14:33:21 +010053 *
Neels Hofmeyre90c7172021-09-14 14:37:38 +020054 * Group and item descriptions:
55 * Each group description exists once as osmo_stat_item_group_desc,
56 * each such group description lists N osmo_stat_item_desc, i.e. describes N stat items.
57 *
58 * Actual stats:
59 * The global osmo_stat_item_groups llist contains all group instances, each points at a group description.
60 * This list mixes all types of groups in a single llist, where each instance points at its group desc and has an index.
61 * There are one or more instances of each group, each storing stats for a distinct object (for example, one description
62 * for a BTS group, and any number of BTS instances with independent stats). A group is identified by a group index nr
63 * and possibly also a given name for that particular index (e.g. in osmo-mgw, a group instance is named
64 * "virtual-trunk-0" and can be looked up by that name instead of its more or less arbitrary group index number).
65 *
66 * Each group instance contains one osmo_stat_item instance per global stat item description.
67 * Each osmo_stat_item keeps track of the values for the current reporting period (min, last, max, sum, n),
68 * and also stores the set of values reported at the end of the previous reporting period.
69 *
70 * const osmo_stat_item_group_desc foo
71 * +-- group_name_prefix = "foo"
72 * +-- item_desc[] (array of osmo_stat_item_desc)
73 * +-- osmo_stat_item_desc bar
74 * | +-- name = "bar"
75 * | +-- description
76 * | +-- unit
77 * | +-- default_value
78 * |
79 * +-- osmo_stat_item_desc: baz
80 * +-- ...
81 *
82 * const osmo_stat_item_group_desc moo
83 * +-- group_name_prefix = "moo"
84 * +-- item_desc[]
85 * +-- osmo_stat_item_desc goo
86 * | +-- name = "goo"
87 * | +-- description
88 * | +-- unit
89 * | +-- default_value
90 * |
91 * +-- osmo_stat_item_desc: loo
92 * +-- ...
93 *
94 * osmo_stat_item_groups (llist of osmo_stat_item_group)
95 * |
96 * +-- group: foo[0]
97 * | +-- desc --> osmo_stat_item_group_desc foo
98 * | +-- idx = 0
99 * | +-- name = NULL (no given name for this group instance)
100 * | +-- items[]
101 * | |
102 * | [0] --> osmo_stat_item instance for "bar"
103 * | | +-- desc --> osmo_stat_item_desc bar (see above)
104 * | | +-- value.{min, last, max, n, sum}
105 * | | +-- reported.{min, last, max, n, sum}
106 * | |
107 * | [1] --> osmo_stat_item instance for "baz"
108 * | | +-- desc --> osmo_stat_item_desc baz
109 * | | +-- value.{min, last, max, n, sum}
110 * | | +-- reported.{min, last, max, n, sum}
111 * | .
112 * | :
113 * |
114 * +-- group: foo[1]
115 * | +-- desc --> osmo_stat_item_group_desc foo
116 * | +-- idx = 1
117 * | +-- name = "special-foo" (instance can be looked up by this index-name)
118 * | +-- items[]
119 * | |
120 * | [0] --> osmo_stat_item instance for "bar"
121 * | | +-- desc --> osmo_stat_item_desc bar
122 * | | +-- value.{min, last, max, n, sum}
123 * | | +-- reported.{min, last, max, n, sum}
124 * | |
125 * | [1] --> osmo_stat_item instance for "baz"
126 * | | +-- desc --> osmo_stat_item_desc baz
127 * | | +-- value.{min, last, max, n, sum}
128 * | | +-- reported.{min, last, max, n, sum}
129 * | .
130 * | :
131 * |
132 * +-- group: moo[0]
133 * | +-- desc --> osmo_stat_item_group_desc moo
134 * | +-- idx = 0
135 * | +-- name = NULL
136 * | +-- items[]
137 * | |
138 * | [0] --> osmo_stat_item instance for "goo"
139 * | | +-- desc --> osmo_stat_item_desc goo
140 * | | +-- value.{min, last, max, n, sum}
141 * | | +-- reported.{min, last, max, n, sum}
142 * | |
143 * | [1] --> osmo_stat_item instance for "loo"
144 * | | +-- desc --> osmo_stat_item_desc loo
145 * | | +-- value.{min, last, max, n, sum}
146 * | | +-- reported.{min, last, max, n, sum}
147 * | .
148 * | :
149 * .
150 * :
151 *
Oliver Smith43686da2021-03-26 14:33:21 +0100152 */
153
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200154#include <stdint.h>
155#include <string.h>
156
157#include <osmocom/core/utils.h>
158#include <osmocom/core/linuxlist.h>
Oliver Smith61401942021-03-26 10:18:37 +0100159#include <osmocom/core/logging.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200160#include <osmocom/core/talloc.h>
161#include <osmocom/core/timer.h>
162#include <osmocom/core/stat_item.h>
163
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200164#include <stat_item_internal.h>
165
Harald Welte781951b2017-10-15 19:24:57 +0200166/*! global list of stat_item groups */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100167static LLIST_HEAD(osmo_stat_item_groups);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200168
Harald Welte781951b2017-10-15 19:24:57 +0200169/*! talloc context from which we allocate */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200170static void *tall_stat_item_ctx;
171
Harald Welte781951b2017-10-15 19:24:57 +0200172/*! Allocate a new group of counters according to description.
173 * Allocate a group of stat items described in \a desc from talloc context \a ctx,
174 * giving the new group the index \a idx.
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200175 * \param[in] ctx \ref talloc context
176 * \param[in] desc Statistics item group description
177 * \param[in] idx Index of new stat item group
178 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100179struct osmo_stat_item_group *osmo_stat_item_group_alloc(void *ctx,
Neels Hofmeyr049fd5c2021-09-14 14:39:18 +0200180 const struct osmo_stat_item_group_desc *group_desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200181 unsigned int idx)
182{
183 unsigned int group_size;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200184 unsigned int item_idx;
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200185 struct osmo_stat_item *items;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200186
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100187 struct osmo_stat_item_group *group;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200188
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100189 group_size = sizeof(struct osmo_stat_item_group) +
Neels Hofmeyr049fd5c2021-09-14 14:39:18 +0200190 group_desc->num_items * sizeof(struct osmo_stat_item *);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200191
192 if (!ctx)
193 ctx = tall_stat_item_ctx;
194
195 group = talloc_zero_size(ctx, group_size);
196 if (!group)
197 return NULL;
198
Neels Hofmeyr049fd5c2021-09-14 14:39:18 +0200199 group->desc = group_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200200 group->idx = idx;
201
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200202 items = talloc_array(group, struct osmo_stat_item, group_desc->num_items);
203 OSMO_ASSERT(items);
Neels Hofmeyr049fd5c2021-09-14 14:39:18 +0200204 for (item_idx = 0; item_idx < group_desc->num_items; item_idx++) {
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200205 struct osmo_stat_item *item = &items[item_idx];
206 const struct osmo_stat_item_desc *item_desc = &group_desc->item_desc[item_idx];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200207 group->items[item_idx] = item;
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200208 *item = (struct osmo_stat_item){
209 .desc = item_desc,
210 .value = {
211 .n = 0,
212 .last = item_desc->default_value,
213 .min = item_desc->default_value,
214 .max = item_desc->default_value,
215 .sum = 0,
216 },
217 };
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200218 }
219
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100220 llist_add(&group->list, &osmo_stat_item_groups);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200221 return group;
222}
223
Harald Welte781951b2017-10-15 19:24:57 +0200224/*! Free the memory for the specified group of stat items */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100225void osmo_stat_item_group_free(struct osmo_stat_item_group *grp)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200226{
Philipp Maier627a8972021-12-14 15:32:53 +0100227 if (!grp)
228 return;
229
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200230 llist_del(&grp->list);
231 talloc_free(grp);
232}
233
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +0200234/*! Get statistics item from group, identified by index idx
235 * \param[in] grp Rate counter group
236 * \param[in] idx Index of the counter to retrieve
237 * \returns rate counter requested
238 */
239struct osmo_stat_item *osmo_stat_item_group_get_item(struct osmo_stat_item_group *grp, unsigned int idx)
240{
241 return grp->items[idx];
242}
243
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200244/*! Set a name for the statistics item group to be used instead of index value
245 at report time.
246 * \param[in] statg Statistics item group
247 * \param[in] name Name identifier to assign to the statistics item group
248 */
249void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name)
250{
251 osmo_talloc_replace_string(statg, &statg->name, name);
252}
253
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200254/*! Increase the stat_item to the given value.
255 * This function adds a new value for the given stat_item at the end of
256 * the FIFO.
257 * \param[in] item The stat_item whose \a value we want to set
258 * \param[in] value The numeric value we want to store at end of FIFO
259 */
260void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value)
261{
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200262 osmo_stat_item_set(item, item->value.last + value);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200263}
264
265/*! Descrease the stat_item to the given value.
266 * This function adds a new value for the given stat_item at the end of
267 * the FIFO.
268 * \param[in] item The stat_item whose \a value we want to set
269 * \param[in] value The numeric value we want to store at end of FIFO
270 */
271void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value)
272{
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200273 osmo_stat_item_set(item, item->value.last - value);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200274}
275
Harald Welte781951b2017-10-15 19:24:57 +0200276/*! Set the a given stat_item to the given value.
277 * This function adds a new value for the given stat_item at the end of
278 * the FIFO.
279 * \param[in] item The stat_item whose \a value we want to set
280 * \param[in] value The numeric value we want to store at end of FIFO
281 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100282void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200283{
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200284 item->value.last = value;
285 if (item->value.n == 0) {
286 /* No values recorded yet, clamp min and max to this first value. */
287 item->value.min = item->value.max = value;
288 /* Overwrite any cruft remaining in value.sum */
289 item->value.sum = value;
290 item->value.n = 1;
291 } else {
292 item->value.min = OSMO_MIN(item->value.min, value);
293 item->value.max = OSMO_MAX(item->value.max, value);
294 item->value.sum += value;
295 item->value.n++;
296 }
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200297}
298
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200299/*! Indicate that a reporting period has elapsed, and prepare the stat item for a new period of collecting min/max/avg.
300 * \param item Stat item to flush.
Harald Welte781951b2017-10-15 19:24:57 +0200301 */
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200302void osmo_stat_item_flush(struct osmo_stat_item *item)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200303{
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200304 item->reported = item->value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200305
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200306 /* Indicate a new reporting period: no values have been received, but the previous value.last remains unchanged
307 * for the case that an entire period elapses without a new value appearing. */
308 item->value.n = 0;
309 item->value.sum = 0;
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200310
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200311 /* Also for the case that an entire period elapses without any osmo_stat_item_set(), put the min and max to the
312 * last value. As soon as one osmo_stat_item_set() occurs, these are both set to the new value (when n is still
313 * zero from above). */
314 item->value.min = item->value.max = item->value.last;
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200315}
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200316
Harald Welte781951b2017-10-15 19:24:57 +0200317/*! Initialize the stat item module. Call this once from your program.
318 * \param[in] tall_ctx Talloc context from which this module allocates */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100319int osmo_stat_item_init(void *tall_ctx)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200320{
321 tall_stat_item_ctx = tall_ctx;
322
323 return 0;
324}
325
Harald Welte781951b2017-10-15 19:24:57 +0200326/*! Search for item group based on group name and index
327 * \param[in] name Name of stats_item_group we want to find
328 * \param[in] idx Index of the group we want to find
329 * \returns pointer to group, if found; NULL otherwise */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100330struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200331 const char *name, const unsigned int idx)
332{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100333 struct osmo_stat_item_group *statg;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200334
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100335 llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200336 if (!statg->desc)
337 continue;
338
339 if (!strcmp(statg->desc->group_name_prefix, name) &&
340 statg->idx == idx)
341 return statg;
342 }
343 return NULL;
344}
345
Neels Hofmeyr7fcfefb2021-09-05 16:22:23 +0200346/*! Search for item group based on group name and index's name.
347 * \param[in] name Name of stats_item_group we want to find.
348 * \param[in] idx_name Index of the group we want to find, by the index's name (osmo_stat_item_group->name).
349 * \returns pointer to group, if found; NULL otherwise. */
350struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idxname(const char *group_name, const char *idx_name)
351{
352 struct osmo_stat_item_group *statg;
353
354 llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
355 if (!statg->desc || !statg->name)
356 continue;
357 if (strcmp(statg->desc->group_name_prefix, group_name))
358 continue;
359 if (strcmp(statg->name, idx_name))
360 continue;
361 return statg;
362 }
363 return NULL;
364}
365
Harald Welte781951b2017-10-15 19:24:57 +0200366/*! Search for item based on group + item name
367 * \param[in] statg group in which to search for the item
368 * \param[in] name name of item to search within \a statg
369 * \returns pointer to item, if found; NULL otherwise */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100370const struct osmo_stat_item *osmo_stat_item_get_by_name(
371 const struct osmo_stat_item_group *statg, const char *name)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200372{
373 int i;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100374 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200375
376 if (!statg->desc)
377 return NULL;
378
379 for (i = 0; i < statg->desc->num_items; i++) {
380 item_desc = &statg->desc->item_desc[i];
381
382 if (!strcmp(item_desc->name, name)) {
383 return statg->items[i];
384 }
385 }
386 return NULL;
387}
388
Harald Welte781951b2017-10-15 19:24:57 +0200389/*! Iterate over all items in group, call user-supplied function on each
390 * \param[in] statg stat_item group over whose items to iterate
391 * \param[in] handle_item Call-back function, aborts if rc < 0
392 * \param[in] data Private data handed through to \a handle_item
393 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100394int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
395 osmo_stat_item_handler_t handle_item, void *data)
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200396{
397 int rc = 0;
398 int i;
399
400 for (i = 0; i < statg->desc->num_items; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100401 struct osmo_stat_item *item = statg->items[i];
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200402 rc = handle_item(statg, item, data);
403 if (rc < 0)
404 return rc;
405 }
406
407 return rc;
408}
409
Harald Welte781951b2017-10-15 19:24:57 +0200410/*! Iterate over all stat_item groups in system, call user-supplied function on each
411 * \param[in] handle_group Call-back function, aborts if rc < 0
412 * \param[in] data Private data handed through to \a handle_group
413 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100414int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data)
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200415{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100416 struct osmo_stat_item_group *statg;
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200417 int rc = 0;
418
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100419 llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200420 rc = handle_group(statg, data);
421 if (rc < 0)
422 return rc;
423 }
424
425 return rc;
426}
427
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200428/*! Get the last (freshest) value. */
429int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item)
430{
431 return item->value.last;
432}
Daniel Willmannea71b432020-07-14 18:10:20 +0200433
434/*! Remove all values of a stat item
435 * \param[in] item stat item to reset
436 */
437void osmo_stat_item_reset(struct osmo_stat_item *item)
438{
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200439 item->value.sum = 0;
440 item->value.n = 0;
441 item->value.last = item->value.min = item->value.max = item->desc->default_value;
Daniel Willmannea71b432020-07-14 18:10:20 +0200442}
443
444/*! Reset all osmo stat items in a group
445 * \param[in] statg stat item group to reset
446 */
447void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg)
448{
449 int i;
450
451 for (i = 0; i < statg->desc->num_items; i++) {
452 struct osmo_stat_item *item = statg->items[i];
453 osmo_stat_item_reset(item);
454 }
455}
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200456
457/*! Return the description for an osmo_stat_item. */
458const struct osmo_stat_item_desc *osmo_stat_item_get_desc(struct osmo_stat_item *item)
459{
460 return item->desc;
461}
462
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200463/*! @} */