blob: 4cee8a8b784a3d0ddceea2e6e1893e3718bd6544 [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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010016struct osmo_stat_item_value {
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020017 int32_t id;
18 int32_t value;
19};
20
Neels Hofmeyr87e45502017-06-20 00:17:59 +020021/*! data we keep for each actual value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010022struct osmo_stat_item {
23 const struct osmo_stat_item_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020024 /*! the index of the freshest value */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020025 int32_t last_value_index;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 /*! offset to the freshest value in the value fifo */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020027 int16_t last_offs;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028 /*! value fifo */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010029 struct osmo_stat_item_value values[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020030};
31
Neels Hofmeyr87e45502017-06-20 00:17:59 +020032/*! statistics value description */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010033struct osmo_stat_item_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020034 const char *name; /*!< name of the item */
35 const char *description;/*!< description of the item */
36 const char *unit; /*!< unit of a value */
37 unsigned int num_values;/*!< number of values to store */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020038 int32_t default_value;
39};
40
Neels Hofmeyr87e45502017-06-20 00:17:59 +020041/*! description of a statistics value group */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010042struct osmo_stat_item_group_desc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043 /*! The prefix to the name of all values in this group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020044 const char *group_name_prefix;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045 /*! The human-readable description of the group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020046 const char *group_description;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047 /*! The class to which this group belongs */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010048 int class_id;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049 /*! The number of values in this group */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020050 const unsigned int num_items;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051 /*! Pointer to array of value names */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010052 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020053};
54
Neels Hofmeyr87e45502017-06-20 00:17:59 +020055/*! One instance of a counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010056struct osmo_stat_item_group {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020057 /*! Linked list of all value groups in the system */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020058 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059 /*! Pointer to the counter group class */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010060 const struct osmo_stat_item_group_desc *desc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020061 /*! The index of this value group within its class */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020062 unsigned int idx;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063 /*! Actual counter structures below */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010064 struct osmo_stat_item *items[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020065};
66
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010067struct osmo_stat_item_group *osmo_stat_item_group_alloc(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020068 void *ctx,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010069 const struct osmo_stat_item_group_desc *desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020070 unsigned int idx);
71
Holger Hans Peter Freyther495b0db2015-11-04 14:39:37 +010072static inline void osmo_stat_item_group_udp_idx(
73 struct osmo_stat_item_group *grp, unsigned int idx)
74{
75 grp->idx = idx;
76}
77
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010078void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020079
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010080void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020081
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010082int osmo_stat_item_init(void *tall_ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020083
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010084struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020085 const char *name, const unsigned int idx);
86
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010087const struct osmo_stat_item *osmo_stat_item_get_by_name(
88 const struct osmo_stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020089
Neels Hofmeyr87e45502017-06-20 00:17:59 +020090/*! Retrieve the next value from the osmo_stat_item object.
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020091 * If a new value has been set, it is returned. The idx is used to decide
92 * which value to return.
93 * On success, *idx is updated to refer to the next unread value. If
94 * values have been missed due to FIFO overflow, *idx is incremented by
95 * (1 + num_lost).
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010096 * This way, the osmo_stat_item object can be kept stateless from the reader's
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020097 * perspective and therefore be used by several backends simultaneously.
98 *
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010099 * \param val the osmo_stat_item object
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200100 * \param idx identifies the next value to be read
101 * \param value a pointer to store the value
102 * \returns the increment of the index (0: no value has been read,
103 * 1: one value has been taken,
104 * (1+n): n values have been skipped, one has been taken)
105 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100106int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *idx, int32_t *value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108/*! Get the last (freshest) value */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200110
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200111/*! Skip all values of the item and update idx accordingly */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! Skip all values of all items and update idx accordingly */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100115int osmo_stat_item_discard_all(int32_t *idx);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200116
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100117typedef int (*osmo_stat_item_handler_t)(
118 struct osmo_stat_item_group *, struct osmo_stat_item *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200119
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100120typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200121
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200122/*! Iteate over all items
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200123 * \param[in] handle_item Call-back function, aborts if rc < 0
124 * \param[in] data Private data handed through to \a handle_item
125 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100126int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
127 osmo_stat_item_handler_t handle_item, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200128
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100129int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data);
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200130
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100131static inline int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200132{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200133 return item->values[item->last_offs].value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200134}
135/*! @} */