blob: 4047495880c44c7244b82d7e493a7eaabb8c4234 [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 * @{
5 */
6
7/*! \file stat_item.h */
8
9#include <stdint.h>
10
11#include <osmocom/core/linuxlist.h>
12
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010013struct osmo_stat_item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020014
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020015#define STAT_ITEM_NOVALUE_ID 0
16
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010017struct osmo_stat_item_value {
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020018 int32_t id;
19 int32_t value;
20};
21
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020022/*! \brief data we keep for each actual value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010023struct osmo_stat_item {
24 const struct osmo_stat_item_desc *desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020025 /*! \brief the index of the freshest value */
26 int32_t last_value_index;
27 /*! \brief offset to the freshest value in the value fifo */
28 int16_t last_offs;
29 /*! \brief value fifo */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010030 struct osmo_stat_item_value values[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020031};
32
33/*! \brief statistics value description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010034struct osmo_stat_item_desc {
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020035 const char *name; /*!< \brief name of the item */
36 const char *description;/*!< \brief description of the item */
37 const char *unit; /*!< \brief unit of a value */
38 unsigned int num_values;/*!< \brief number of values to store */
39 int32_t default_value;
40};
41
42/*! \brief description of a statistics value group */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010043struct osmo_stat_item_group_desc {
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020044 /*! \brief The prefix to the name of all values in this group */
45 const char *group_name_prefix;
46 /*! \brief The human-readable description of the group */
47 const char *group_description;
48 /*! \brief The number of values in this group */
49 const unsigned int num_items;
50 /*! \brief Pointer to array of value names */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010051 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020052};
53
54/*! \brief One instance of a counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010055struct osmo_stat_item_group {
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020056 /*! \brief Linked list of all value groups in the system */
57 struct llist_head list;
58 /*! \brief Pointer to the counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010059 const struct osmo_stat_item_group_desc *desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020060 /*! \brief The index of this value group within its class */
61 unsigned int idx;
62 /*! \brief Actual counter structures below */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010063 struct osmo_stat_item *items[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020064};
65
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010066struct osmo_stat_item_group *osmo_stat_item_group_alloc(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020067 void *ctx,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010068 const struct osmo_stat_item_group_desc *desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020069 unsigned int idx);
70
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010071void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020072
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010073void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020074
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010075int osmo_stat_item_init(void *tall_ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020076
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010077struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020078 const char *name, const unsigned int idx);
79
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010080const struct osmo_stat_item *osmo_stat_item_get_by_name(
81 const struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020082
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010083/*! \brief Retrieve the next value from the osmo_stat_item object.
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020084 * If a new value has been set, it is returned. The idx is used to decide
85 * which value to return.
86 * On success, *idx is updated to refer to the next unread value. If
87 * values have been missed due to FIFO overflow, *idx is incremented by
88 * (1 + num_lost).
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010089 * This way, the osmo_stat_item object can be kept stateless from the reader's
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020090 * perspective and therefore be used by several backends simultaneously.
91 *
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010092 * \param val the osmo_stat_item object
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020093 * \param idx identifies the next value to be read
94 * \param value a pointer to store the value
95 * \returns the increment of the index (0: no value has been read,
96 * 1: one value has been taken,
97 * (1+n): n values have been skipped, one has been taken)
98 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010099int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *idx, int32_t *value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200100
101/*! \brief Get the last (freshest) value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100102static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200103
104/*! \brief Skip all values of the item and update idx accordingly */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100105int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200106
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200107/*! \brief Skip all values of all items and update idx accordingly */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108int osmo_stat_item_discard_all(int32_t *idx);
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
115/*! \brief Iteate over all items
116 * \param[in] handle_item Call-back function, aborts if rc < 0
117 * \param[in] data Private data handed through to \a handle_item
118 */
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}
128/*! @} */