blob: 4710dba715bf8b6ef76dd082d9d12fbde19d80b1 [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
9#include <osmocom/core/linuxlist.h>
10
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010011struct osmo_stat_item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020012
Jacob Erlbeckac4ed172015-12-08 10:29:16 +010013#define OSMO_STAT_ITEM_NOVALUE_ID 0
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +010014#define OSMO_STAT_ITEM_NO_UNIT NULL
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020015
Harald Welte781951b2017-10-15 19:24:57 +020016/*! Individual entry in value FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010017struct osmo_stat_item_value {
Harald Welte781951b2017-10-15 19:24:57 +020018 int32_t id; /*!< identifier of value */
19 int32_t value; /*!< actual value */
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020020};
21
Harald Welte781951b2017-10-15 19:24:57 +020022/*! data we keep for each actual item */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010023struct osmo_stat_item {
Harald Welte781951b2017-10-15 19:24:57 +020024 /*! back-reference to the item description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010025 const struct osmo_stat_item_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 /*! the index of the freshest value */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020027 int32_t last_value_index;
Harald Welte781951b2017-10-15 19:24:57 +020028 /*! offset to the freshest value in the value FIFO */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020029 int16_t last_offs;
Harald Welte781951b2017-10-15 19:24:57 +020030 /*! value FIFO */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010031 struct osmo_stat_item_value values[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020032};
33
Harald Welte781951b2017-10-15 19:24:57 +020034/*! Statistics item description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010035struct osmo_stat_item_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036 const char *name; /*!< name of the item */
37 const char *description;/*!< description of the item */
38 const char *unit; /*!< unit of a value */
Harald Welte781951b2017-10-15 19:24:57 +020039 unsigned int num_values;/*!< number of values to store in FIFO */
40 int32_t default_value; /*!< default value */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020041};
42
Harald Welte781951b2017-10-15 19:24:57 +020043/*! Description of a statistics item group */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010044struct osmo_stat_item_group_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045 /*! The prefix to the name of all values in this group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020046 const char *group_name_prefix;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047 /*! The human-readable description of the group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020048 const char *group_description;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049 /*! The class to which this group belongs */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010050 int class_id;
Harald Welte781951b2017-10-15 19:24:57 +020051 /*! The number of values in this group (size of item_desc) */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020052 const unsigned int num_items;
Harald Welte781951b2017-10-15 19:24:57 +020053 /*! Pointer to array of value names, length as per num_items */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010054 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020055};
56
Neels Hofmeyr87e45502017-06-20 00:17:59 +020057/*! One instance of a counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010058struct osmo_stat_item_group {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059 /*! Linked list of all value groups in the system */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020060 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020061 /*! Pointer to the counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010062 const struct osmo_stat_item_group_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063 /*! The index of this value group within its class */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020064 unsigned int idx;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065 /*! Actual counter structures below */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010066 struct osmo_stat_item *items[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020067};
68
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010069struct osmo_stat_item_group *osmo_stat_item_group_alloc(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020070 void *ctx,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010071 const struct osmo_stat_item_group_desc *desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020072 unsigned int idx);
73
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010074static inline void osmo_stat_item_group_udp_idx(
75 struct osmo_stat_item_group *grp, unsigned int idx)
76{
77 grp->idx = idx;
78}
79
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010080void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020081
Alexander Couzenscc72cc42019-04-27 23:19:55 +020082void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);
83void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010084void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020085
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010086int osmo_stat_item_init(void *tall_ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020087
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010088struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020089 const char *name, const unsigned int idx);
90
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010091const struct osmo_stat_item *osmo_stat_item_get_by_name(
92 const struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020093
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010094int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *idx, int32_t *value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020095
Neels Hofmeyr87e45502017-06-20 00:17:59 +020096/*! Get the last (freshest) value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010097static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020098
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010099int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200100
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100101int osmo_stat_item_discard_all(int32_t *idx);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200102
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103typedef int (*osmo_stat_item_handler_t)(
104 struct osmo_stat_item_group *, struct osmo_stat_item *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200105
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100106typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200107
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
109 osmo_stat_item_handler_t handle_item, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200110
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200112
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100113static inline int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200114{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200115 return item->values[item->last_offs].value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200116}
Daniel Willmannea71b432020-07-14 18:10:20 +0200117
118void osmo_stat_item_reset(struct osmo_stat_item *item);
119void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg);
120
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200121/*! @} */