blob: 957918aeeca6afcdd06571a92565a0099023f36b [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 Erlbeckac4ed172015-12-08 10:29:16 +010015#define OSMO_STAT_ITEM_NOVALUE_ID 0
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +010016#define OSMO_STAT_ITEM_NO_UNIT NULL
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020017
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010018struct osmo_stat_item_value {
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020019 int32_t id;
20 int32_t value;
21};
22
Neels Hofmeyr87e45502017-06-20 00:17:59 +020023/*! data we keep for each actual value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010024struct osmo_stat_item {
25 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;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028 /*! offset to the freshest value in the value fifo */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020029 int16_t last_offs;
Neels Hofmeyr87e45502017-06-20 00:17:59 +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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034/*! statistics value 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 */
39 unsigned int num_values;/*!< number of values to store */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020040 int32_t default_value;
41};
42
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043/*! description of a statistics value 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;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051 /*! The number of values in this group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020052 const unsigned int num_items;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053 /*! Pointer to array of value names */
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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010082void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020083
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010084int osmo_stat_item_init(void *tall_ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020085
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010086struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020087 const char *name, const unsigned int idx);
88
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010089const struct osmo_stat_item *osmo_stat_item_get_by_name(
90 const struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020091
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092/*! Retrieve the next value from the osmo_stat_item object.
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020093 * If a new value has been set, it is returned. The idx is used to decide
94 * which value to return.
95 * On success, *idx is updated to refer to the next unread value. If
96 * values have been missed due to FIFO overflow, *idx is incremented by
97 * (1 + num_lost).
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010098 * This way, the osmo_stat_item object can be kept stateless from the reader's
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020099 * perspective and therefore be used by several backends simultaneously.
100 *
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100101 * \param val the osmo_stat_item object
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200102 * \param idx identifies the next value to be read
103 * \param value a pointer to store the value
104 * \returns the increment of the index (0: no value has been read,
105 * 1: one value has been taken,
106 * (1+n): n values have been skipped, one has been taken)
107 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *idx, int32_t *value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200109
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200110/*! Get the last (freshest) value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200112
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200113/*! Skip all values of the item and update idx accordingly */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200115
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200116/*! Skip all values of all items and update idx accordingly */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100117int osmo_stat_item_discard_all(int32_t *idx);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200118
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100119typedef int (*osmo_stat_item_handler_t)(
120 struct osmo_stat_item_group *, struct osmo_stat_item *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200121
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200123
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200124/*! Iteate over all items
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200125 * \param[in] handle_item Call-back function, aborts if rc < 0
126 * \param[in] data Private data handed through to \a handle_item
127 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100128int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
129 osmo_stat_item_handler_t handle_item, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200130
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100131int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200132
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100133static inline int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200134{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200135 return item->values[item->last_offs].value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200136}
137/*! @} */