Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 3 | /*! \defgroup osmo_stat_item Statistics value item |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 4 | * @{ |
| 5 | */ |
| 6 | |
| 7 | /*! \file stat_item.h */ |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <osmocom/core/linuxlist.h> |
| 12 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 13 | struct osmo_stat_item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 14 | |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 15 | #define OSMO_STAT_ITEM_NOVALUE_ID 0 |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 16 | #define OSMO_STAT_ITEM_NO_UNIT NULL |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 17 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 18 | struct osmo_stat_item_value { |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 19 | int32_t id; |
| 20 | int32_t value; |
| 21 | }; |
| 22 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 23 | /*! \brief data we keep for each actual value */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 24 | struct osmo_stat_item { |
| 25 | const struct osmo_stat_item_desc *desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 26 | /*! \brief the index of the freshest value */ |
| 27 | int32_t last_value_index; |
| 28 | /*! \brief offset to the freshest value in the value fifo */ |
| 29 | int16_t last_offs; |
| 30 | /*! \brief value fifo */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 31 | struct osmo_stat_item_value values[0]; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | /*! \brief statistics value description */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 35 | struct osmo_stat_item_desc { |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 36 | const char *name; /*!< \brief name of the item */ |
| 37 | const char *description;/*!< \brief description of the item */ |
| 38 | const char *unit; /*!< \brief unit of a value */ |
| 39 | unsigned int num_values;/*!< \brief number of values to store */ |
| 40 | int32_t default_value; |
| 41 | }; |
| 42 | |
| 43 | /*! \brief description of a statistics value group */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 44 | struct osmo_stat_item_group_desc { |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 45 | /*! \brief The prefix to the name of all values in this group */ |
| 46 | const char *group_name_prefix; |
| 47 | /*! \brief The human-readable description of the group */ |
| 48 | const char *group_description; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 49 | /*! \brief The class to which this group belongs */ |
| 50 | int class_id; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 51 | /*! \brief The number of values in this group */ |
| 52 | const unsigned int num_items; |
| 53 | /*! \brief Pointer to array of value names */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 54 | const struct osmo_stat_item_desc *item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /*! \brief One instance of a counter group class */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 58 | struct osmo_stat_item_group { |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 59 | /*! \brief Linked list of all value groups in the system */ |
| 60 | struct llist_head list; |
| 61 | /*! \brief Pointer to the counter group class */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 62 | const struct osmo_stat_item_group_desc *desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 63 | /*! \brief The index of this value group within its class */ |
| 64 | unsigned int idx; |
| 65 | /*! \brief Actual counter structures below */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 66 | struct osmo_stat_item *items[0]; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 69 | struct osmo_stat_item_group *osmo_stat_item_group_alloc( |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 70 | void *ctx, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 71 | const struct osmo_stat_item_group_desc *desc, |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 72 | unsigned int idx); |
| 73 | |
Holger Hans Peter Freyther | 495b0db | 2015-11-04 14:39:37 +0100 | [diff] [blame] | 74 | static 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 80 | void osmo_stat_item_group_free(struct osmo_stat_item_group *statg); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 81 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 82 | void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 83 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 84 | int osmo_stat_item_init(void *tall_ctx); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 85 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 86 | struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx( |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 87 | const char *name, const unsigned int idx); |
| 88 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 89 | const struct osmo_stat_item *osmo_stat_item_get_by_name( |
| 90 | const struct osmo_stat_item_group *statg, const char *name); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 91 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 92 | /*! \brief Retrieve the next value from the osmo_stat_item object. |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 93 | * 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 98 | * This way, the osmo_stat_item object can be kept stateless from the reader's |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 99 | * perspective and therefore be used by several backends simultaneously. |
| 100 | * |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 101 | * \param val the osmo_stat_item object |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 102 | * \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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 108 | int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *idx, int32_t *value); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 109 | |
| 110 | /*! \brief Get the last (freshest) value */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 111 | static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 112 | |
| 113 | /*! \brief Skip all values of the item and update idx accordingly */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 114 | int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 115 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 116 | /*! \brief Skip all values of all items and update idx accordingly */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 117 | int osmo_stat_item_discard_all(int32_t *idx); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 118 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 119 | typedef int (*osmo_stat_item_handler_t)( |
| 120 | struct osmo_stat_item_group *, struct osmo_stat_item *, void *); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 121 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 122 | typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 123 | |
| 124 | /*! \brief Iteate over all items |
| 125 | * \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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 128 | int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, |
| 129 | osmo_stat_item_handler_t handle_item, void *data); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 130 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 131 | int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 132 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 133 | static inline int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 134 | { |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 135 | return item->values[item->last_offs].value; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 136 | } |
| 137 | /*! @} */ |