blob: fbe0433288af425ef983c0221bf07a8a4dbd778e [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001#pragma once
2
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +01003/*! \defgroup osmo_stat_item Statistics value item
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02004 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02005 * \file stat_item.h */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02006
7#include <stdint.h>
8
Oliver Smith61401942021-03-26 10:18:37 +01009#include <osmocom/core/defs.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020010#include <osmocom/core/linuxlist.h>
11
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010012struct osmo_stat_item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020013
Jacob Erlbeckac4ed172015-12-08 10:29:16 +010014#define OSMO_STAT_ITEM_NOVALUE_ID 0
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +010015#define OSMO_STAT_ITEM_NO_UNIT NULL
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020016
Harald Welte781951b2017-10-15 19:24:57 +020017/*! Individual entry in value FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010018struct osmo_stat_item_value {
Harald Welte781951b2017-10-15 19:24:57 +020019 int32_t id; /*!< identifier of value */
20 int32_t value; /*!< actual value */
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020021};
22
Harald Welte781951b2017-10-15 19:24:57 +020023/*! data we keep for each actual item */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010024struct osmo_stat_item {
Harald Welte781951b2017-10-15 19:24:57 +020025 /*! back-reference to the item description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010026 const struct osmo_stat_item_desc *desc;
Oliver Smith61401942021-03-26 10:18:37 +010027 /* internal use by stats API (stats.c): the id of the next value to
28 * be read from the FIFO. If accessing osmo_stat_item directly, without
29 * the stats API, store this value elsewhere. */
30 int32_t stats_next_id;
31 /*! the index of the last value written to the FIFO */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020032 int16_t last_offs;
Harald Welte781951b2017-10-15 19:24:57 +020033 /*! value FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010034 struct osmo_stat_item_value values[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020035};
36
Harald Welte781951b2017-10-15 19:24:57 +020037/*! Statistics item description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010038struct osmo_stat_item_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020039 const char *name; /*!< name of the item */
40 const char *description;/*!< description of the item */
41 const char *unit; /*!< unit of a value */
Harald Welte781951b2017-10-15 19:24:57 +020042 unsigned int num_values;/*!< number of values to store in FIFO */
43 int32_t default_value; /*!< default value */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020044};
45
Harald Welte781951b2017-10-15 19:24:57 +020046/*! Description of a statistics item group */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010047struct osmo_stat_item_group_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048 /*! The prefix to the name of all values in this group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020049 const char *group_name_prefix;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 /*! The human-readable description of the group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020051 const char *group_description;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 /*! The class to which this group belongs */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010053 int class_id;
Harald Welte781951b2017-10-15 19:24:57 +020054 /*! The number of values in this group (size of item_desc) */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020055 const unsigned int num_items;
Harald Welte781951b2017-10-15 19:24:57 +020056 /*! Pointer to array of value names, length as per num_items */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010057 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020058};
59
Neels Hofmeyr87e45502017-06-20 00:17:59 +020060/*! One instance of a counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010061struct osmo_stat_item_group {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 /*! Linked list of all value groups in the system */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020063 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020064 /*! Pointer to the counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010065 const struct osmo_stat_item_group_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066 /*! The index of this value group within its class */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020067 unsigned int idx;
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +020068 /*! Optional string-based identifier to be used instead of index at report time */
69 char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070 /*! Actual counter structures below */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010071 struct osmo_stat_item *items[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020072};
73
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010074struct osmo_stat_item_group *osmo_stat_item_group_alloc(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020075 void *ctx,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010076 const struct osmo_stat_item_group_desc *desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020077 unsigned int idx);
78
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010079static inline void osmo_stat_item_group_udp_idx(
80 struct osmo_stat_item_group *grp, unsigned int idx)
81{
82 grp->idx = idx;
83}
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +020084struct osmo_stat_item *osmo_stat_item_group_get_item(struct osmo_stat_item_group *grp, unsigned int idx);
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +020085void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010086void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020087
Alexander Couzenscc72cc42019-04-27 23:19:55 +020088void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);
89void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010090void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020091
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010092int osmo_stat_item_init(void *tall_ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020093
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010094struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020095 const char *name, const unsigned int idx);
96
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010097const struct osmo_stat_item *osmo_stat_item_get_by_name(
98 const struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020099
Oliver Smithd3490bc2021-03-26 11:34:34 +0100100int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *next_id, int32_t *value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200101
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200102/*! Get the last (freshest) value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200104
Oliver Smithd3490bc2021-03-26 11:34:34 +0100105int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *next_id);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200106
Oliver Smith61401942021-03-26 10:18:37 +0100107int osmo_stat_item_discard_all(int32_t *next_id)
108 OSMO_DEPRECATED("Use osmo_stat_item_discard with item-specific next_id instead");
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200109
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100110typedef int (*osmo_stat_item_handler_t)(
111 struct osmo_stat_item_group *, struct osmo_stat_item *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200112
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100113typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200114
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100115int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
116 osmo_stat_item_handler_t handle_item, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200117
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200119
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100120static inline int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200121{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200122 return item->values[item->last_offs].value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200123}
Daniel Willmannea71b432020-07-14 18:10:20 +0200124
125void osmo_stat_item_reset(struct osmo_stat_item *item);
126void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg);
127
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200128/*! @} */