aboutsummaryrefslogtreecommitdiffstats
path: root/src/stat_item.c
blob: 804972bf4c532a5c30cfa0b197786b8676e1a308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*! \file stat_item.c
 * utility routines for keeping statistical values */
/*
 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
 * (C) 2015 by sysmocom - s.f.m.c. GmbH
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

/*! \addtogroup osmo_stat_item
 *  @{
 *
 *  This osmo_stat_item module adds instrumentation capabilities to
 *  gather measurement and statistical values in a similar fashion to
 *  what we have as \ref osmo_counter_group.
 *
 *  As opposed to counters, osmo_stat_item do not increment but consist
 *  of a configurable-sized FIFO, which can store not only the current
 *  (most recent) value, but also historic values.
 *
 *  The only supported value type is an int32_t.
 *
 *  Getting values from osmo_stat_item is usually done at a high level
 *  through the stats API (stats.c). It uses item->stats_next_id to
 *  store what has been sent to all enabled reporters. It is also
 *  possible to read from osmo_stat_item directly, without modifying
 *  its state, by storing next_id outside of osmo_stat_item.
 *
 *  Each value stored in the FIFO of an osmo_stat_item has an associated
 *  value_id.  The value_id is increased with each value, so (until the
 *  counter wraps) more recent values will have higher values.
 *
 *  When a new value is set, the oldest value in the FIFO gets silently
 *  overwritten.  Lost values are skipped when getting values from the
 *  item.
 *
 */

/* Struct overview:
 *
 * Group and item descriptions:
 * Each group description exists once as osmo_stat_item_group_desc,
 * each such group description lists N osmo_stat_item_desc, i.e. describes N stat items.
 *
 * Actual stats:
 * The global osmo_stat_item_groups llist contains all group instances, each points at a group description.
 * This list mixes all types of groups in a single llist, where each instance points at its group desc and has an index.
 * There are one or more instances of each group, each storing stats for a distinct object (for example, one description
 * for a BTS group, and any number of BTS instances with independent stats). A group is identified by a group index nr
 * and possibly also a given name for that particular index (e.g. in osmo-mgw, a group instance is named
 * "virtual-trunk-0" and can be looked up by that name instead of its more or less arbitrary group index number).
 *
 * Each group instance contains one osmo_stat_item instance per global stat item description.
 * Each osmo_stat_item keeps track of the values for the current reporting period (min, last, max, sum, n),
 * and also stores the set of values reported at the end of the previous reporting period.
 *
 *  const osmo_stat_item_group_desc foo
 *                                   +-- group_name_prefix = "foo"
 *                                   +-- item_desc[] (array of osmo_stat_item_desc)
 *                                        +-- osmo_stat_item_desc bar
 *                                        |    +-- name = "bar"
 *                                        |    +-- description
 *                                        |    +-- unit
 *                                        |    +-- default_value
 *                                        |
 *                                        +-- osmo_stat_item_desc: baz
 *                                             +-- ...
 *
 *  const osmo_stat_item_group_desc moo
 *                                   +-- group_name_prefix = "moo"
 *                                   +-- item_desc[]
 *                                        +-- osmo_stat_item_desc goo
 *                                        |    +-- name = "goo"
 *                                        |    +-- description
 *                                        |    +-- unit
 *                                        |    +-- default_value
 *                                        |
 *                                        +-- osmo_stat_item_desc: loo
 *                                             +-- ...
 *
 *  osmo_stat_item_groups (llist of osmo_stat_item_group)
 *   |
 *   +-- group: foo[0]
 *   |    +-- desc --> osmo_stat_item_group_desc foo
 *   |    +-- idx = 0
 *   |    +-- name = NULL (no given name for this group instance)
 *   |    +-- items[]
 *   |         |
 *   |        [0] --> osmo_stat_item instance for "bar"
 *   |         |       +-- desc --> osmo_stat_item_desc bar (see above)
 *   |         |       +-- value.{min, last, max, n, sum}
 *   |         |       +-- reported.{min, last, max, n, sum}
 *   |         |
 *   |        [1] --> osmo_stat_item instance for "baz"
 *   |         |       +-- desc --> osmo_stat_item_desc baz
 *   |         |       +-- value.{min, last, max, n, sum}
 *   |         |       +-- reported.{min, last, max, n, sum}
 *   |         .
 *   |         :
 *   |
 *   +-- group: foo[1]
 *   |    +-- desc --> osmo_stat_item_group_desc foo
 *   |    +-- idx = 1
 *   |    +-- name = "special-foo" (instance can be looked up by this index-name)
 *   |    +-- items[]
 *   |         |
 *   |        [0] --> osmo_stat_item instance for "bar"
 *   |         |       +-- desc --> osmo_stat_item_desc bar
 *   |         |       +-- value.{min, last, max, n, sum}
 *   |         |       +-- reported.{min, last, max, n, sum}
 *   |         |
 *   |        [1] --> osmo_stat_item instance for "baz"
 *   |         |       +-- desc --> osmo_stat_item_desc baz
 *   |         |       +-- value.{min, last, max, n, sum}
 *   |         |       +-- reported.{min, last, max, n, sum}
 *   |         .
 *   |         :
 *   |
 *   +-- group: moo[0]
 *   |    +-- desc --> osmo_stat_item_group_desc moo
 *   |    +-- idx = 0
 *   |    +-- name = NULL
 *   |    +-- items[]
 *   |         |
 *   |        [0] --> osmo_stat_item instance for "goo"
 *   |         |       +-- desc --> osmo_stat_item_desc goo
 *   |         |       +-- value.{min, last, max, n, sum}
 *   |         |       +-- reported.{min, last, max, n, sum}
 *   |         |
 *   |        [1] --> osmo_stat_item instance for "loo"
 *   |         |       +-- desc --> osmo_stat_item_desc loo
 *   |         |       +-- value.{min, last, max, n, sum}
 *   |         |       +-- reported.{min, last, max, n, sum}
 *   |         .
 *   |         :
 *   .
 *   :
 *
 */

#include <stdint.h>
#include <string.h>

#include <osmocom/core/utils.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/stat_item.h>

#include <stat_item_internal.h>

/*! global list of stat_item groups */
static LLIST_HEAD(osmo_stat_item_groups);

/*! talloc context from which we allocate */
static void *tall_stat_item_ctx;

/*! Allocate a new group of counters according to description.
 *  Allocate a group of stat items described in \a desc from talloc context \a ctx,
 *  giving the new group the index \a idx.
 *  \param[in] ctx \ref talloc context
 *  \param[in] desc Statistics item group description
 *  \param[in] idx Index of new stat item group
 */
struct osmo_stat_item_group *osmo_stat_item_group_alloc(void *ctx,
					    const struct osmo_stat_item_group_desc *group_desc,
					    unsigned int idx)
{
	unsigned int group_size;
	unsigned int item_idx;
	struct osmo_stat_item *items;

	struct osmo_stat_item_group *group;

	group_size = sizeof(struct osmo_stat_item_group) +
			group_desc->num_items * sizeof(struct osmo_stat_item *);

	if (!ctx)
		ctx = tall_stat_item_ctx;

	group = talloc_zero_size(ctx, group_size);
	if (!group)
		return NULL;

	group->desc = group_desc;
	group->idx = idx;

	items = talloc_array(group, struct osmo_stat_item, group_desc->num_items);
	OSMO_ASSERT(items);
	for (item_idx = 0; item_idx < group_desc->num_items; item_idx++) {
		struct osmo_stat_item *item = &items[item_idx];
		const struct osmo_stat_item_desc *item_desc = &group_desc->item_desc[item_idx];
		group->items[item_idx] = item;
		*item = (struct osmo_stat_item){
			.desc = item_desc,
			.value = {
				.n = 0,
				.last = item_desc->default_value,
				.min = item_desc->default_value,
				.max = item_desc->default_value,
				.sum = 0,
			},
		};
	}

	llist_add(&group->list, &osmo_stat_item_groups);
	return group;
}

/*! Free the memory for the specified group of stat items */
void osmo_stat_item_group_free(struct osmo_stat_item_group *grp)
{
	if (!grp)
		return;

	llist_del(&grp->list);
	talloc_free(grp);
}

/*! Get statistics item from group, identified by index idx
 *  \param[in] grp Rate counter group
 *  \param[in] idx Index of the counter to retrieve
 *  \returns rate counter requested
 */
struct osmo_stat_item *osmo_stat_item_group_get_item(struct osmo_stat_item_group *grp, unsigned int idx)
{
	return grp->items[idx];
}

/*! Set a name for the statistics item group to be used instead of index value
  at report time.
 *  \param[in] statg Statistics item group
 *  \param[in] name Name identifier to assign to the statistics item group
 */
void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name)
{
	osmo_talloc_replace_string(statg, &statg->name, name);
}

/*! Increase the stat_item to the given value.
 *  This function adds a new value for the given stat_item at the end of
 *  the FIFO.
 *  \param[in] item The stat_item whose \a value we want to set
 *  \param[in] value The numeric value we want to store at end of FIFO
 */
void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value)
{
	osmo_stat_item_set(item, item->value.last + value);
}

/*! Descrease the stat_item to the given value.
 *  This function adds a new value for the given stat_item at the end of
 *  the FIFO.
 *  \param[in] item The stat_item whose \a value we want to set
 *  \param[in] value The numeric value we want to store at end of FIFO
 */
void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value)
{
	osmo_stat_item_set(item, item->value.last - value);
}

/*! Set the a given stat_item to the given value.
 *  This function adds a new value for the given stat_item at the end of
 *  the FIFO.
 *  \param[in] item The stat_item whose \a value we want to set
 *  \param[in] value The numeric value we want to store at end of FIFO
 */
void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value)
{
	item->value.last = value;
	if (item->value.n == 0) {
		/* No values recorded yet, clamp min and max to this first value. */
		item->value.min = item->value.max = value;
		/* Overwrite any cruft remaining in value.sum */
		item->value.sum = value;
		item->value.n = 1;
	} else {
		item->value.min = OSMO_MIN(item->value.min, value);
		item->value.max = OSMO_MAX(item->value.max, value);
		item->value.sum += value;
		item->value.n++;
	}
}

/*! Indicate that a reporting period has elapsed, and prepare the stat item for a new period of collecting min/max/avg.
 * \param item  Stat item to flush.
 */
void osmo_stat_item_flush(struct osmo_stat_item *item)
{
	item->reported = item->value;

	/* Indicate a new reporting period: no values have been received, but the previous value.last remains unchanged
	 * for the case that an entire period elapses without a new value appearing. */
	item->value.n = 0;
	item->value.sum = 0;

	/* Also for the case that an entire period elapses without any osmo_stat_item_set(), put the min and max to the
	 * last value. As soon as one osmo_stat_item_set() occurs, these are both set to the new value (when n is still
	 * zero from above). */
	item->value.min = item->value.max = item->value.last;
}

/*! Initialize the stat item module. Call this once from your program.
 *  \param[in] tall_ctx Talloc context from which this module allocates */
int osmo_stat_item_init(void *tall_ctx)
{
	tall_stat_item_ctx = tall_ctx;

	return 0;
}

/*! Search for item group based on group name and index
 *  \param[in] name Name of stats_item_group we want to find
 *  \param[in] idx Index of the group we want to find
 *  \returns pointer to group, if found; NULL otherwise */
struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
	const char *name, const unsigned int idx)
{
	struct osmo_stat_item_group *statg;

	llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
		if (!statg->desc)
			continue;

		if (!strcmp(statg->desc->group_name_prefix, name) &&
				statg->idx == idx)
			return statg;
	}
	return NULL;
}

/*! Search for item group based on group name and index's name.
 *  \param[in] name Name of stats_item_group we want to find.
 *  \param[in] idx_name Index of the group we want to find, by the index's name (osmo_stat_item_group->name).
 *  \returns pointer to group, if found; NULL otherwise. */
struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idxname(const char *group_name, const char *idx_name)
{
	struct osmo_stat_item_group *statg;

	llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
		if (!statg->desc || !statg->name)
			continue;
		if (strcmp(statg->desc->group_name_prefix, group_name))
			continue;
		if (strcmp(statg->name, idx_name))
			continue;
		return statg;
	}
	return NULL;
}

/*! Search for item based on group + item name
 *  \param[in] statg group in which to search for the item
 *  \param[in] name name of item to search within \a statg
 *  \returns pointer to item, if found; NULL otherwise */
const struct osmo_stat_item *osmo_stat_item_get_by_name(
	const struct osmo_stat_item_group *statg, const char *name)
{
	int i;
	const struct osmo_stat_item_desc *item_desc;

	if (!statg->desc)
		return NULL;

	for (i = 0; i < statg->desc->num_items; i++) {
		item_desc = &statg->desc->item_desc[i];

		if (!strcmp(item_desc->name, name)) {
			return statg->items[i];
		}
	}
	return NULL;
}

/*! Iterate over all items in group, call user-supplied function on each
 *  \param[in] statg stat_item group over whose items to iterate
 *  \param[in] handle_item Call-back function, aborts if rc < 0
 *  \param[in] data Private data handed through to \a handle_item
 */
int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
	osmo_stat_item_handler_t handle_item, void *data)
{
	int rc = 0;
	int i;

	for (i = 0; i < statg->desc->num_items; i++) {
		struct osmo_stat_item *item = statg->items[i];
		rc = handle_item(statg, item, data);
		if (rc < 0)
			return rc;
	}

	return rc;
}

/*! Iterate over all stat_item groups in system, call user-supplied function on each
 *  \param[in] handle_group Call-back function, aborts if rc < 0
 *  \param[in] data Private data handed through to \a handle_group
 */
int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data)
{
	struct osmo_stat_item_group *statg;
	int rc = 0;

	llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
		rc = handle_group(statg, data);
		if (rc < 0)
			return rc;
	}

	return rc;
}

/*! Get the last (freshest) value. */
int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item)
{
	return item->value.last;
}

/*! Remove all values of a stat item
 *  \param[in] item stat item to reset
 */
void osmo_stat_item_reset(struct osmo_stat_item *item)
{
	item->value.sum = 0;
	item->value.n = 0;
	item->value.last = item->value.min = item->value.max = item->desc->default_value;
}

/*! Reset all osmo stat items in a group
 *  \param[in] statg stat item group to reset
 */
void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg)
{
	int i;

	for (i = 0; i < statg->desc->num_items; i++) {
		struct osmo_stat_item *item = statg->items[i];
                osmo_stat_item_reset(item);
	}
}

/*! Return the description for an osmo_stat_item. */
const struct osmo_stat_item_desc *osmo_stat_item_get_desc(struct osmo_stat_item *item)
{
	return item->desc;
}

/*! @} */