blob: 332bbe926f2cb29dacbf6abe4bef4c6cbf8d060e [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;
Oliver Smith11da4a42021-08-19 11:58:09 +020031 /* internal use by stats API: indicate if the last value sent to
32 * reporters was actually the last value in the FIFO. This may not be
33 * the case, as always a max of 1 or more values gets sent (OS#5215) */
34 bool stats_last_sent_was_max;
Oliver Smith61401942021-03-26 10:18:37 +010035 /*! the index of the last value written to the FIFO */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020036 int16_t last_offs;
Harald Welte781951b2017-10-15 19:24:57 +020037 /*! value FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010038 struct osmo_stat_item_value values[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020039};
40
Harald Welte781951b2017-10-15 19:24:57 +020041/*! Statistics item description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010042struct osmo_stat_item_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043 const char *name; /*!< name of the item */
44 const char *description;/*!< description of the item */
45 const char *unit; /*!< unit of a value */
Harald Welte781951b2017-10-15 19:24:57 +020046 unsigned int num_values;/*!< number of values to store in FIFO */
47 int32_t default_value; /*!< default value */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020048};
49
Harald Welte781951b2017-10-15 19:24:57 +020050/*! Description of a statistics item group */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010051struct osmo_stat_item_group_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 /*! The prefix to the name of all values in this group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020053 const char *group_name_prefix;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054 /*! The human-readable description of the group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020055 const char *group_description;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020056 /*! The class to which this group belongs */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010057 int class_id;
Harald Welte781951b2017-10-15 19:24:57 +020058 /*! The number of values in this group (size of item_desc) */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020059 const unsigned int num_items;
Harald Welte781951b2017-10-15 19:24:57 +020060 /*! Pointer to array of value names, length as per num_items */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010061 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020062};
63
Neels Hofmeyr87e45502017-06-20 00:17:59 +020064/*! One instance of a counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010065struct osmo_stat_item_group {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066 /*! Linked list of all value groups in the system */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020067 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020068 /*! Pointer to the counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010069 const struct osmo_stat_item_group_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070 /*! The index of this value group within its class */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020071 unsigned int idx;
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +020072 /*! Optional string-based identifier to be used instead of index at report time */
73 char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074 /*! Actual counter structures below */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010075 struct osmo_stat_item *items[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020076};
77
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010078struct osmo_stat_item_group *osmo_stat_item_group_alloc(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020079 void *ctx,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010080 const struct osmo_stat_item_group_desc *desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020081 unsigned int idx);
82
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010083static inline void osmo_stat_item_group_udp_idx(
84 struct osmo_stat_item_group *grp, unsigned int idx)
85{
86 grp->idx = idx;
87}
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +020088struct 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 +020089void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010090void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020091
Alexander Couzenscc72cc42019-04-27 23:19:55 +020092void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);
93void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010094void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020095
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010096int osmo_stat_item_init(void *tall_ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020097
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010098struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020099 const char *name, const unsigned int idx);
100
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100101const struct osmo_stat_item *osmo_stat_item_get_by_name(
102 const struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200103
Oliver Smithd3490bc2021-03-26 11:34:34 +0100104int 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 +0200105
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106/*! Get the last (freshest) value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200108
Oliver Smithd3490bc2021-03-26 11:34:34 +0100109int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *next_id);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200110
Oliver Smith61401942021-03-26 10:18:37 +0100111int osmo_stat_item_discard_all(int32_t *next_id)
112 OSMO_DEPRECATED("Use osmo_stat_item_discard with item-specific next_id instead");
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200113
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114typedef int (*osmo_stat_item_handler_t)(
115 struct osmo_stat_item_group *, struct osmo_stat_item *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200116
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100117typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200118
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100119int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
120 osmo_stat_item_handler_t handle_item, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200121
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200123
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100124static inline int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200125{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200126 return item->values[item->last_offs].value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200127}
Daniel Willmannea71b432020-07-14 18:10:20 +0200128
129void osmo_stat_item_reset(struct osmo_stat_item *item);
130void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg);
131
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200132/*! @} */