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 | |
| 9 | #include <osmocom/core/linuxlist.h> |
| 10 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 11 | struct osmo_stat_item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 12 | |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 13 | #define OSMO_STAT_ITEM_NOVALUE_ID 0 |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 14 | #define OSMO_STAT_ITEM_NO_UNIT NULL |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 15 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 16 | /*! Individual entry in value FIFO */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 17 | struct osmo_stat_item_value { |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 18 | int32_t id; /*!< identifier of value */ |
| 19 | int32_t value; /*!< actual value */ |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 22 | /*! data we keep for each actual item */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 23 | struct osmo_stat_item { |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 24 | /*! back-reference to the item description */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 25 | const struct osmo_stat_item_desc *desc; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 26 | /*! the index of the freshest value */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 27 | int32_t last_value_index; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 28 | /*! offset to the freshest value in the value FIFO */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 29 | int16_t last_offs; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 30 | /*! 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 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 34 | /*! Statistics item description */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 35 | struct osmo_stat_item_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 36 | const char *name; /*!< name of the item */ |
| 37 | const char *description;/*!< description of the item */ |
| 38 | const char *unit; /*!< unit of a value */ |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 39 | unsigned int num_values;/*!< number of values to store in FIFO */ |
| 40 | int32_t default_value; /*!< default value */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 43 | /*! Description of a statistics item group */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 44 | struct osmo_stat_item_group_desc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 45 | /*! The prefix to the name of all values in this group */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 46 | const char *group_name_prefix; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 47 | /*! The human-readable description of the group */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 48 | const char *group_description; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 49 | /*! The class to which this group belongs */ |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 50 | int class_id; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 51 | /*! The number of values in this group (size of item_desc) */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 52 | const unsigned int num_items; |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 53 | /*! Pointer to array of value names, length as per num_items */ |
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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 57 | /*! 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 { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 59 | /*! Linked list of all value groups in the system */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 60 | struct llist_head list; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 61 | /*! 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; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 63 | /*! The index of this value group within its class */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 64 | unsigned int idx; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 65 | /*! 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 | |
Alexander Couzens | cc72cc4 | 2019-04-27 23:19:55 +0200 | [diff] [blame] | 82 | void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value); |
| 83 | 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] | 84 | 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] | 85 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 86 | int osmo_stat_item_init(void *tall_ctx); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 87 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 88 | 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] | 89 | const char *name, const unsigned int idx); |
| 90 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 91 | const struct osmo_stat_item *osmo_stat_item_get_by_name( |
| 92 | const struct osmo_stat_item_group *statg, const char *name); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 93 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 94 | 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] | 95 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 96 | /*! Get the last (freshest) value */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 97 | 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] | 98 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 99 | 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] | 100 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 101 | int osmo_stat_item_discard_all(int32_t *idx); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 102 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 103 | typedef int (*osmo_stat_item_handler_t)( |
| 104 | struct osmo_stat_item_group *, struct osmo_stat_item *, void *); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 105 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 106 | 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] | 107 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 108 | int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, |
| 109 | osmo_stat_item_handler_t handle_item, void *data); |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 110 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 111 | 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] | 112 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 113 | 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] | 114 | { |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 115 | return item->values[item->last_offs].value; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 116 | } |
Daniel Willmann | ea71b43 | 2020-07-14 18:10:20 +0200 | [diff] [blame] | 117 | |
| 118 | void osmo_stat_item_reset(struct osmo_stat_item *item); |
| 119 | void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg); |
| 120 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 121 | /*! @} */ |