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 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 17 | /*! data we keep for each actual item. Access via API functions only. |
| 18 | * (This struct was made opaque after libosmocore 1.5.1)*/ |
| 19 | struct osmo_stat_item; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 20 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 21 | /*! Statistics item description */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 22 | struct osmo_stat_item_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 23 | const char *name; /*!< name of the item */ |
| 24 | const char *description;/*!< description of the item */ |
| 25 | const char *unit; /*!< unit of a value */ |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 26 | unsigned int num_values;/*!< DEPRECATED, this value is ignored after libosmocore version 1.5.1 */ |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 27 | int32_t default_value; /*!< default value */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 30 | /*! Description of a statistics item group */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 31 | struct osmo_stat_item_group_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 32 | /*! The prefix to the name of all values in this group */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 33 | const char *group_name_prefix; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 34 | /*! The human-readable description of the group */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 35 | const char *group_description; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 36 | /*! The class to which this group belongs */ |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 37 | int class_id; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 38 | /*! The number of values in this group (size of item_desc) */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 39 | const unsigned int num_items; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 40 | /*! Pointer to array of value names, length as per num_items */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 41 | const struct osmo_stat_item_desc *item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 44 | /*! One instance of a counter group class */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 45 | struct osmo_stat_item_group { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! Linked list of all value groups in the system */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 47 | struct llist_head list; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 48 | /*! Pointer to the counter group class */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 49 | const struct osmo_stat_item_group_desc *desc; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | /*! The index of this value group within its class */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 51 | unsigned int idx; |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 52 | /*! Optional string-based identifier to be used instead of index at report time */ |
| 53 | char *name; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! Actual counter structures below */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 55 | struct osmo_stat_item *items[0]; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 58 | struct osmo_stat_item_group *osmo_stat_item_group_alloc( |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 59 | void *ctx, |
Neels Hofmeyr | 049fd5c | 2021-09-14 14:39:18 +0200 | [diff] [blame] | 60 | const struct osmo_stat_item_group_desc *group_desc, |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 61 | unsigned int idx); |
| 62 | |
Holger Hans Peter Freyther | 495b0db | 2015-11-04 14:39:37 +0100 | [diff] [blame] | 63 | static inline void osmo_stat_item_group_udp_idx( |
| 64 | struct osmo_stat_item_group *grp, unsigned int idx) |
| 65 | { |
| 66 | grp->idx = idx; |
| 67 | } |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 68 | 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] | 69 | 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] | 70 | void osmo_stat_item_group_free(struct osmo_stat_item_group *statg); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 71 | |
Alexander Couzens | cc72cc4 | 2019-04-27 23:19:55 +0200 | [diff] [blame] | 72 | void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value); |
| 73 | 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] | 74 | 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] | 75 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 76 | int osmo_stat_item_init(void *tall_ctx); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 77 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 78 | 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] | 79 | const char *name, const unsigned int idx); |
Neels Hofmeyr | 7fcfefb | 2021-09-05 16:22:23 +0200 | [diff] [blame] | 80 | struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idxname(const char *group_name, const char *idx_name); |
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 | const struct osmo_stat_item *osmo_stat_item_get_by_name( |
| 83 | const struct osmo_stat_item_group *statg, const char *name); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 84 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 85 | 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] | 86 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 87 | void osmo_stat_item_flush(struct osmo_stat_item *item); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 88 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 89 | typedef int (*osmo_stat_item_handler_t)( |
| 90 | struct osmo_stat_item_group *, struct osmo_stat_item *, void *); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 91 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 92 | 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] | 93 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 94 | int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, |
| 95 | osmo_stat_item_handler_t handle_item, void *data); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 96 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 97 | 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] | 98 | |
Daniel Willmann | ea71b43 | 2020-07-14 18:10:20 +0200 | [diff] [blame] | 99 | void osmo_stat_item_reset(struct osmo_stat_item *item); |
| 100 | void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg); |
| 101 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 102 | const struct osmo_stat_item_desc *osmo_stat_item_get_desc(struct osmo_stat_item *item); |
| 103 | |
| 104 | /* DEPRECATION: up until libosmocore 1.5.1, this API also defined |
| 105 | * - struct osmo_stat_item_value |
| 106 | * - osmo_stat_item_get_next() |
| 107 | * - osmo_stat_item_discard() |
| 108 | * - osmo_stat_item_discard_all() |
| 109 | * Despite our principle of never breaking API compatibility, we have decided to remove these, because there are no |
| 110 | * known users. These items were never practically used/usable outside of libosmocore since the generic stats reporter |
| 111 | * (stats.c) was introduced. |
| 112 | * We also decided to make struct osmo_stat_item opaque to allow future changes of the struct without API breakage. |
| 113 | */ |
| 114 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 115 | /*! @} */ |