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 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | * \file stat_item.h */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 6 | |
| 7 | #include <stdint.h> |
| 8 | |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 9 | #include <osmocom/core/defs.h> |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 10 | #include <osmocom/core/linuxlist.h> |
| 11 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 12 | struct osmo_stat_item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 13 | |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 14 | #define OSMO_STAT_ITEM_NOVALUE_ID 0 |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 15 | #define OSMO_STAT_ITEM_NO_UNIT NULL |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 16 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 17 | /*! Individual entry in value FIFO */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 18 | struct osmo_stat_item_value { |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 19 | int32_t id; /*!< identifier of value */ |
| 20 | int32_t value; /*!< actual value */ |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 23 | /*! data we keep for each actual item */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 24 | struct osmo_stat_item { |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 25 | /*! back-reference to the item description */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 26 | const struct osmo_stat_item_desc *desc; |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 27 | /* internal use by stats API (stats.c): the id of the next value to |
| 28 | * be read from the FIFO. If accessing osmo_stat_item directly, without |
| 29 | * the stats API, store this value elsewhere. */ |
| 30 | int32_t stats_next_id; |
Oliver Smith | 11da4a4 | 2021-08-19 11:58:09 +0200 | [diff] [blame] | 31 | /* internal use by stats API: indicate if the last value sent to |
| 32 | * reporters was actually the last value in the FIFO. This may not be |
| 33 | * the case, as always a max of 1 or more values gets sent (OS#5215) */ |
| 34 | bool stats_last_sent_was_max; |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 35 | /*! the index of the last value written to the FIFO */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 36 | int16_t last_offs; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 37 | /*! value FIFO */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 38 | struct osmo_stat_item_value values[0]; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 41 | /*! Statistics item description */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 42 | struct osmo_stat_item_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 43 | const char *name; /*!< name of the item */ |
| 44 | const char *description;/*!< description of the item */ |
| 45 | const char *unit; /*!< unit of a value */ |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 46 | unsigned int num_values;/*!< number of values to store in FIFO */ |
| 47 | int32_t default_value; /*!< default value */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 50 | /*! Description of a statistics item group */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 51 | struct osmo_stat_item_group_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 52 | /*! The prefix to the name of all values in this group */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 53 | const char *group_name_prefix; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! The human-readable description of the group */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 55 | const char *group_description; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 56 | /*! The class to which this group belongs */ |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 57 | int class_id; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 58 | /*! The number of values in this group (size of item_desc) */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 59 | const unsigned int num_items; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 60 | /*! Pointer to array of value names, length as per num_items */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 61 | const struct osmo_stat_item_desc *item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 64 | /*! One instance of a counter group class */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 65 | struct osmo_stat_item_group { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 66 | /*! Linked list of all value groups in the system */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 67 | struct llist_head list; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 68 | /*! Pointer to the counter group class */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 69 | const struct osmo_stat_item_group_desc *desc; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 70 | /*! The index of this value group within its class */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 71 | unsigned int idx; |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 72 | /*! Optional string-based identifier to be used instead of index at report time */ |
| 73 | char *name; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 74 | /*! Actual counter structures below */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 75 | struct osmo_stat_item *items[0]; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 78 | struct osmo_stat_item_group *osmo_stat_item_group_alloc( |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 79 | void *ctx, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 80 | const struct osmo_stat_item_group_desc *desc, |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 81 | unsigned int idx); |
| 82 | |
Holger Hans Peter Freyther | 495b0db | 2015-11-04 14:39:37 +0100 | [diff] [blame] | 83 | static inline void osmo_stat_item_group_udp_idx( |
| 84 | struct osmo_stat_item_group *grp, unsigned int idx) |
| 85 | { |
| 86 | grp->idx = idx; |
| 87 | } |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 88 | struct osmo_stat_item *osmo_stat_item_group_get_item(struct osmo_stat_item_group *grp, unsigned int idx); |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 89 | void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 90 | void osmo_stat_item_group_free(struct osmo_stat_item_group *statg); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 91 | |
Alexander Couzens | cc72cc4 | 2019-04-27 23:19:55 +0200 | [diff] [blame] | 92 | void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value); |
| 93 | void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 94 | 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] | 95 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 96 | int osmo_stat_item_init(void *tall_ctx); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 97 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 98 | 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] | 99 | const char *name, const unsigned int idx); |
| 100 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 101 | const struct osmo_stat_item *osmo_stat_item_get_by_name( |
| 102 | const struct osmo_stat_item_group *statg, const char *name); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 103 | |
Oliver Smith | d3490bc | 2021-03-26 11:34:34 +0100 | [diff] [blame] | 104 | int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *next_id, int32_t *value); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 105 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 106 | /*! Get the last (freshest) value */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 107 | 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] | 108 | |
Oliver Smith | d3490bc | 2021-03-26 11:34:34 +0100 | [diff] [blame] | 109 | int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *next_id); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 110 | |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 111 | int osmo_stat_item_discard_all(int32_t *next_id) |
| 112 | OSMO_DEPRECATED("Use osmo_stat_item_discard with item-specific next_id instead"); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 113 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 114 | typedef int (*osmo_stat_item_handler_t)( |
| 115 | struct osmo_stat_item_group *, struct osmo_stat_item *, void *); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 116 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 117 | 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] | 118 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 119 | int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, |
| 120 | osmo_stat_item_handler_t handle_item, void *data); |
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 | 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] | 123 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 124 | 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] | 125 | { |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 126 | return item->values[item->last_offs].value; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 127 | } |
Daniel Willmann | ea71b43 | 2020-07-14 18:10:20 +0200 | [diff] [blame] | 128 | |
| 129 | void osmo_stat_item_reset(struct osmo_stat_item *item); |
| 130 | void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg); |
| 131 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 132 | /*! @} */ |