blob: 003c9e07ab3aff52998fca5d1958339c2b3886a9 [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001#pragma once
2
3/*! \defgroup stat_item Statistics value item
4 * @{
5 */
6
7/*! \file stat_item.h */
8
9#include <stdint.h>
10
11#include <osmocom/core/linuxlist.h>
12
13struct stat_item_desc;
14
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020015#define STAT_ITEM_NOVALUE_ID 0
16
17struct stat_item_value {
18 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 */
23struct stat_item {
24 const struct stat_item_desc *desc;
25 /*! \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 Erlbeckb27b3522015-10-12 18:47:09 +020030 struct stat_item_value values[0];
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020031};
32
33/*! \brief statistics value description */
34struct stat_item_desc {
35 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 */
43struct stat_item_group_desc {
44 /*! \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 */
51 const struct stat_item_desc *item_desc;
52};
53
54/*! \brief One instance of a counter group class */
55struct stat_item_group {
56 /*! \brief Linked list of all value groups in the system */
57 struct llist_head list;
58 /*! \brief Pointer to the counter group class */
59 const struct stat_item_group_desc *desc;
60 /*! \brief The index of this value group within its class */
61 unsigned int idx;
62 /*! \brief Actual counter structures below */
63 struct stat_item *items[0];
64};
65
66struct stat_item_group *stat_item_group_alloc(
67 void *ctx,
68 const struct stat_item_group_desc *desc,
69 unsigned int idx);
70
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020071void stat_item_group_free(struct stat_item_group *statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020072
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020073void stat_item_set(struct stat_item *item, int32_t value);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020074
75int stat_item_init(void *tall_ctx);
76
77struct stat_item_group *stat_item_get_group_by_name_idx(
78 const char *name, const unsigned int idx);
79
80const struct stat_item *stat_item_get_by_name(
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020081 const struct stat_item_group *statg, const char *name);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020082
83/*! \brief Retrieve the next value from the stat_item object.
84 * 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).
89 * This way, the stat_item object can be kept stateless from the reader's
90 * perspective and therefore be used by several backends simultaneously.
91 *
92 * \param val the stat_item object
93 * \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 Erlbeckb27b3522015-10-12 18:47:09 +020099int stat_item_get_next(const struct 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 Erlbeckb27b3522015-10-12 18:47:09 +0200102static int32_t stat_item_get_last(const struct stat_item *item);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200103
104/*! \brief Skip all values of the item and update idx accordingly */
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200105int stat_item_discard(const struct 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 */
108int stat_item_discard_all(int32_t *idx);
109
110static inline int32_t stat_item_get_last(const struct stat_item *item)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200111{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200112 return item->values[item->last_offs].value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200113}
114/*! @} */