blob: 7f6857c0cf8134d1f0db2caba87a1fcf9cd01afa [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
Neels Hofmeyre90c7172021-09-14 14:37:38 +020017/*! data we keep for each actual item. Access via API functions only.
18 * (This struct was made opaque after libosmocore 1.5.1)*/
19struct osmo_stat_item;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020020
Harald Welte781951b2017-10-15 19:24:57 +020021/*! Statistics item description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010022struct osmo_stat_item_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020023 const char *name; /*!< name of the item */
24 const char *description;/*!< description of the item */
25 const char *unit; /*!< unit of a value */
Neels Hofmeyre90c7172021-09-14 14:37:38 +020026 unsigned int num_values;/*!< DEPRECATED, this value is ignored after libosmocore version 1.5.1 */
Harald Welte781951b2017-10-15 19:24:57 +020027 int32_t default_value; /*!< default value */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020028};
29
Harald Welte781951b2017-10-15 19:24:57 +020030/*! Description of a statistics item group */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010031struct osmo_stat_item_group_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020032 /*! The prefix to the name of all values in this group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020033 const char *group_name_prefix;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034 /*! The human-readable description of the group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020035 const char *group_description;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036 /*! The class to which this group belongs */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010037 int class_id;
Harald Welte781951b2017-10-15 19:24:57 +020038 /*! The number of values in this group (size of item_desc) */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020039 const unsigned int num_items;
Harald Welte781951b2017-10-15 19:24:57 +020040 /*! Pointer to array of value names, length as per num_items */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010041 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020042};
43
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044/*! One instance of a counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010045struct osmo_stat_item_group {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046 /*! Linked list of all value groups in the system */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020047 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048 /*! Pointer to the counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010049 const struct osmo_stat_item_group_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 /*! The index of this value group within its class */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020051 unsigned int idx;
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +020052 /*! Optional string-based identifier to be used instead of index at report time */
53 char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054 /*! Actual counter structures below */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010055 struct osmo_stat_item *items[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020056};
57
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010058struct osmo_stat_item_group *osmo_stat_item_group_alloc(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020059 void *ctx,
Neels Hofmeyr049fd5c2021-09-14 14:39:18 +020060 const struct osmo_stat_item_group_desc *group_desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020061 unsigned int idx);
62
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010063static inline void osmo_stat_item_group_udp_idx(
64 struct osmo_stat_item_group *grp, unsigned int idx)
65{
66 grp->idx = idx;
67}
Pau Espin Pedrol5fe3de52021-05-31 13:39:07 +020068struct 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 +020069void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010070void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020071
Alexander Couzenscc72cc42019-04-27 23:19:55 +020072void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);
73void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010074void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020075
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010076int osmo_stat_item_init(void *tall_ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020077
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010078struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020079 const char *name, const unsigned int idx);
Neels Hofmeyr7fcfefb2021-09-05 16:22:23 +020080struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idxname(const char *group_name, const char *idx_name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020081
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010082const struct osmo_stat_item *osmo_stat_item_get_by_name(
83 const struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020084
Neels Hofmeyre90c7172021-09-14 14:37:38 +020085int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020086
Neels Hofmeyre90c7172021-09-14 14:37:38 +020087void osmo_stat_item_flush(struct osmo_stat_item *item);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020088
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010089typedef int (*osmo_stat_item_handler_t)(
90 struct osmo_stat_item_group *, struct osmo_stat_item *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +020091
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010092typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +020093
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010094int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
95 osmo_stat_item_handler_t handle_item, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +020096
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010097int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +020098
Daniel Willmannea71b432020-07-14 18:10:20 +020099void osmo_stat_item_reset(struct osmo_stat_item *item);
100void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg);
101
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200102const struct osmo_stat_item_desc *osmo_stat_item_get_desc(struct osmo_stat_item *item);
103
104/* DEPRECATION: up until libosmocore 1.5.1, this API also defined
105 * - struct osmo_stat_item_value
106 * - osmo_stat_item_get_next()
107 * - osmo_stat_item_discard()
108 * - osmo_stat_item_discard_all()
109 * Despite our principle of never breaking API compatibility, we have decided to remove these, because there are no
110 * known users. These items were never practically used/usable outside of libosmocore since the generic stats reporter
111 * (stats.c) was introduced.
112 * We also decided to make struct osmo_stat_item opaque to allow future changes of the struct without API breakage.
113 */
114
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200115/*! @} */