Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file stat_item.c |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 2 | * utility routines for keeping statistical values */ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 3 | /* |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 4 | * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * (C) 2015 by sysmocom - s.f.m.c. GmbH |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 23 | /*! \addtogroup osmo_stat_item |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 24 | * @{ |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 25 | * |
| 26 | * This osmo_stat_item module adds instrumentation capabilities to |
| 27 | * gather measurement and statistical values in a similar fashion to |
| 28 | * what we have as \ref osmo_counter_group. |
| 29 | * |
| 30 | * As opposed to counters, osmo_stat_item do not increment but consist |
| 31 | * of a configurable-sized FIFO, which can store not only the current |
| 32 | * (most recent) value, but also historic values. |
| 33 | * |
| 34 | * The only supported value type is an int32_t. |
| 35 | * |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 36 | * Getting values from osmo_stat_item is usually done at a high level |
| 37 | * through the stats API (stats.c). It uses item->stats_next_id to |
| 38 | * store what has been sent to all enabled reporters. It is also |
| 39 | * possible to read from osmo_stat_item directly, without modifying |
| 40 | * its state, by storing next_id outside of osmo_stat_item. |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 41 | * |
| 42 | * Each value stored in the FIFO of an osmo_stat_item has an associated |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 43 | * value_id. The value_id is increased with each value, so (until the |
| 44 | * counter wraps) more recent values will have higher values. |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 45 | * |
| 46 | * When a new value is set, the oldest value in the FIFO gets silently |
| 47 | * overwritten. Lost values are skipped when getting values from the |
| 48 | * item. |
| 49 | * |
| 50 | */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 51 | |
Oliver Smith | 43686da | 2021-03-26 14:33:21 +0100 | [diff] [blame] | 52 | /* Struct overview: |
Oliver Smith | 43686da | 2021-03-26 14:33:21 +0100 | [diff] [blame] | 53 | * |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 54 | * Group and item descriptions: |
| 55 | * Each group description exists once as osmo_stat_item_group_desc, |
| 56 | * each such group description lists N osmo_stat_item_desc, i.e. describes N stat items. |
| 57 | * |
| 58 | * Actual stats: |
| 59 | * The global osmo_stat_item_groups llist contains all group instances, each points at a group description. |
| 60 | * This list mixes all types of groups in a single llist, where each instance points at its group desc and has an index. |
| 61 | * There are one or more instances of each group, each storing stats for a distinct object (for example, one description |
| 62 | * for a BTS group, and any number of BTS instances with independent stats). A group is identified by a group index nr |
| 63 | * and possibly also a given name for that particular index (e.g. in osmo-mgw, a group instance is named |
| 64 | * "virtual-trunk-0" and can be looked up by that name instead of its more or less arbitrary group index number). |
| 65 | * |
| 66 | * Each group instance contains one osmo_stat_item instance per global stat item description. |
| 67 | * Each osmo_stat_item keeps track of the values for the current reporting period (min, last, max, sum, n), |
| 68 | * and also stores the set of values reported at the end of the previous reporting period. |
| 69 | * |
| 70 | * const osmo_stat_item_group_desc foo |
| 71 | * +-- group_name_prefix = "foo" |
| 72 | * +-- item_desc[] (array of osmo_stat_item_desc) |
| 73 | * +-- osmo_stat_item_desc bar |
| 74 | * | +-- name = "bar" |
| 75 | * | +-- description |
| 76 | * | +-- unit |
| 77 | * | +-- default_value |
| 78 | * | |
| 79 | * +-- osmo_stat_item_desc: baz |
| 80 | * +-- ... |
| 81 | * |
| 82 | * const osmo_stat_item_group_desc moo |
| 83 | * +-- group_name_prefix = "moo" |
| 84 | * +-- item_desc[] |
| 85 | * +-- osmo_stat_item_desc goo |
| 86 | * | +-- name = "goo" |
| 87 | * | +-- description |
| 88 | * | +-- unit |
| 89 | * | +-- default_value |
| 90 | * | |
| 91 | * +-- osmo_stat_item_desc: loo |
| 92 | * +-- ... |
| 93 | * |
| 94 | * osmo_stat_item_groups (llist of osmo_stat_item_group) |
| 95 | * | |
| 96 | * +-- group: foo[0] |
| 97 | * | +-- desc --> osmo_stat_item_group_desc foo |
| 98 | * | +-- idx = 0 |
| 99 | * | +-- name = NULL (no given name for this group instance) |
| 100 | * | +-- items[] |
| 101 | * | | |
| 102 | * | [0] --> osmo_stat_item instance for "bar" |
| 103 | * | | +-- desc --> osmo_stat_item_desc bar (see above) |
| 104 | * | | +-- value.{min, last, max, n, sum} |
| 105 | * | | +-- reported.{min, last, max, n, sum} |
| 106 | * | | |
| 107 | * | [1] --> osmo_stat_item instance for "baz" |
| 108 | * | | +-- desc --> osmo_stat_item_desc baz |
| 109 | * | | +-- value.{min, last, max, n, sum} |
| 110 | * | | +-- reported.{min, last, max, n, sum} |
| 111 | * | . |
| 112 | * | : |
| 113 | * | |
| 114 | * +-- group: foo[1] |
| 115 | * | +-- desc --> osmo_stat_item_group_desc foo |
| 116 | * | +-- idx = 1 |
| 117 | * | +-- name = "special-foo" (instance can be looked up by this index-name) |
| 118 | * | +-- items[] |
| 119 | * | | |
| 120 | * | [0] --> osmo_stat_item instance for "bar" |
| 121 | * | | +-- desc --> osmo_stat_item_desc bar |
| 122 | * | | +-- value.{min, last, max, n, sum} |
| 123 | * | | +-- reported.{min, last, max, n, sum} |
| 124 | * | | |
| 125 | * | [1] --> osmo_stat_item instance for "baz" |
| 126 | * | | +-- desc --> osmo_stat_item_desc baz |
| 127 | * | | +-- value.{min, last, max, n, sum} |
| 128 | * | | +-- reported.{min, last, max, n, sum} |
| 129 | * | . |
| 130 | * | : |
| 131 | * | |
| 132 | * +-- group: moo[0] |
| 133 | * | +-- desc --> osmo_stat_item_group_desc moo |
| 134 | * | +-- idx = 0 |
| 135 | * | +-- name = NULL |
| 136 | * | +-- items[] |
| 137 | * | | |
| 138 | * | [0] --> osmo_stat_item instance for "goo" |
| 139 | * | | +-- desc --> osmo_stat_item_desc goo |
| 140 | * | | +-- value.{min, last, max, n, sum} |
| 141 | * | | +-- reported.{min, last, max, n, sum} |
| 142 | * | | |
| 143 | * | [1] --> osmo_stat_item instance for "loo" |
| 144 | * | | +-- desc --> osmo_stat_item_desc loo |
| 145 | * | | +-- value.{min, last, max, n, sum} |
| 146 | * | | +-- reported.{min, last, max, n, sum} |
| 147 | * | . |
| 148 | * | : |
| 149 | * . |
| 150 | * : |
| 151 | * |
Oliver Smith | 43686da | 2021-03-26 14:33:21 +0100 | [diff] [blame] | 152 | */ |
| 153 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 154 | #include <stdint.h> |
| 155 | #include <string.h> |
| 156 | |
| 157 | #include <osmocom/core/utils.h> |
| 158 | #include <osmocom/core/linuxlist.h> |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 159 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 160 | #include <osmocom/core/talloc.h> |
| 161 | #include <osmocom/core/timer.h> |
| 162 | #include <osmocom/core/stat_item.h> |
| 163 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 164 | #include <stat_item_internal.h> |
| 165 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 166 | /*! global list of stat_item groups */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 167 | static LLIST_HEAD(osmo_stat_item_groups); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 168 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 169 | /*! talloc context from which we allocate */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 170 | static void *tall_stat_item_ctx; |
| 171 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 172 | /*! Allocate a new group of counters according to description. |
| 173 | * Allocate a group of stat items described in \a desc from talloc context \a ctx, |
| 174 | * giving the new group the index \a idx. |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 175 | * \param[in] ctx \ref talloc context |
| 176 | * \param[in] desc Statistics item group description |
| 177 | * \param[in] idx Index of new stat item group |
| 178 | */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 179 | struct osmo_stat_item_group *osmo_stat_item_group_alloc(void *ctx, |
Neels Hofmeyr | 049fd5c | 2021-09-14 14:39:18 +0200 | [diff] [blame] | 180 | const struct osmo_stat_item_group_desc *group_desc, |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 181 | unsigned int idx) |
| 182 | { |
| 183 | unsigned int group_size; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 184 | unsigned int item_idx; |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 185 | struct osmo_stat_item *items; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 186 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 187 | struct osmo_stat_item_group *group; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 188 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 189 | group_size = sizeof(struct osmo_stat_item_group) + |
Neels Hofmeyr | 049fd5c | 2021-09-14 14:39:18 +0200 | [diff] [blame] | 190 | group_desc->num_items * sizeof(struct osmo_stat_item *); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 191 | |
| 192 | if (!ctx) |
| 193 | ctx = tall_stat_item_ctx; |
| 194 | |
| 195 | group = talloc_zero_size(ctx, group_size); |
| 196 | if (!group) |
| 197 | return NULL; |
| 198 | |
Neels Hofmeyr | 049fd5c | 2021-09-14 14:39:18 +0200 | [diff] [blame] | 199 | group->desc = group_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 200 | group->idx = idx; |
| 201 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 202 | items = talloc_array(group, struct osmo_stat_item, group_desc->num_items); |
| 203 | OSMO_ASSERT(items); |
Neels Hofmeyr | 049fd5c | 2021-09-14 14:39:18 +0200 | [diff] [blame] | 204 | for (item_idx = 0; item_idx < group_desc->num_items; item_idx++) { |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 205 | struct osmo_stat_item *item = &items[item_idx]; |
| 206 | const struct osmo_stat_item_desc *item_desc = &group_desc->item_desc[item_idx]; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 207 | group->items[item_idx] = item; |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 208 | *item = (struct osmo_stat_item){ |
| 209 | .desc = item_desc, |
| 210 | .value = { |
| 211 | .n = 0, |
| 212 | .last = item_desc->default_value, |
| 213 | .min = item_desc->default_value, |
| 214 | .max = item_desc->default_value, |
| 215 | .sum = 0, |
| 216 | }, |
| 217 | }; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 220 | llist_add(&group->list, &osmo_stat_item_groups); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 221 | return group; |
| 222 | } |
| 223 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 224 | /*! Free the memory for the specified group of stat items */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 225 | void osmo_stat_item_group_free(struct osmo_stat_item_group *grp) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 226 | { |
Philipp Maier | 627a897 | 2021-12-14 15:32:53 +0100 | [diff] [blame] | 227 | if (!grp) |
| 228 | return; |
| 229 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 230 | llist_del(&grp->list); |
| 231 | talloc_free(grp); |
| 232 | } |
| 233 | |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 234 | /*! Get statistics item from group, identified by index idx |
| 235 | * \param[in] grp Rate counter group |
| 236 | * \param[in] idx Index of the counter to retrieve |
| 237 | * \returns rate counter requested |
| 238 | */ |
| 239 | struct osmo_stat_item *osmo_stat_item_group_get_item(struct osmo_stat_item_group *grp, unsigned int idx) |
| 240 | { |
| 241 | return grp->items[idx]; |
| 242 | } |
| 243 | |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 244 | /*! Set a name for the statistics item group to be used instead of index value |
| 245 | at report time. |
| 246 | * \param[in] statg Statistics item group |
| 247 | * \param[in] name Name identifier to assign to the statistics item group |
| 248 | */ |
| 249 | void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name) |
| 250 | { |
| 251 | osmo_talloc_replace_string(statg, &statg->name, name); |
| 252 | } |
| 253 | |
Alexander Couzens | cc72cc4 | 2019-04-27 23:19:55 +0200 | [diff] [blame] | 254 | /*! Increase the stat_item to the given value. |
| 255 | * This function adds a new value for the given stat_item at the end of |
| 256 | * the FIFO. |
| 257 | * \param[in] item The stat_item whose \a value we want to set |
| 258 | * \param[in] value The numeric value we want to store at end of FIFO |
| 259 | */ |
| 260 | void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value) |
| 261 | { |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 262 | osmo_stat_item_set(item, item->value.last + value); |
Alexander Couzens | cc72cc4 | 2019-04-27 23:19:55 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /*! Descrease the stat_item to the given value. |
| 266 | * This function adds a new value for the given stat_item at the end of |
| 267 | * the FIFO. |
| 268 | * \param[in] item The stat_item whose \a value we want to set |
| 269 | * \param[in] value The numeric value we want to store at end of FIFO |
| 270 | */ |
| 271 | void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value) |
| 272 | { |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 273 | osmo_stat_item_set(item, item->value.last - value); |
Alexander Couzens | cc72cc4 | 2019-04-27 23:19:55 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 276 | /*! Set the a given stat_item to the given value. |
| 277 | * This function adds a new value for the given stat_item at the end of |
| 278 | * the FIFO. |
| 279 | * \param[in] item The stat_item whose \a value we want to set |
| 280 | * \param[in] value The numeric value we want to store at end of FIFO |
| 281 | */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 282 | 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] | 283 | { |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 284 | item->value.last = value; |
| 285 | if (item->value.n == 0) { |
| 286 | /* No values recorded yet, clamp min and max to this first value. */ |
| 287 | item->value.min = item->value.max = value; |
| 288 | /* Overwrite any cruft remaining in value.sum */ |
| 289 | item->value.sum = value; |
| 290 | item->value.n = 1; |
| 291 | } else { |
| 292 | item->value.min = OSMO_MIN(item->value.min, value); |
| 293 | item->value.max = OSMO_MAX(item->value.max, value); |
| 294 | item->value.sum += value; |
| 295 | item->value.n++; |
| 296 | } |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 299 | /*! Indicate that a reporting period has elapsed, and prepare the stat item for a new period of collecting min/max/avg. |
| 300 | * \param item Stat item to flush. |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 301 | */ |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 302 | void osmo_stat_item_flush(struct osmo_stat_item *item) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 303 | { |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 304 | item->reported = item->value; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 305 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 306 | /* Indicate a new reporting period: no values have been received, but the previous value.last remains unchanged |
| 307 | * for the case that an entire period elapses without a new value appearing. */ |
| 308 | item->value.n = 0; |
| 309 | item->value.sum = 0; |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 310 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 311 | /* Also for the case that an entire period elapses without any osmo_stat_item_set(), put the min and max to the |
| 312 | * last value. As soon as one osmo_stat_item_set() occurs, these are both set to the new value (when n is still |
| 313 | * zero from above). */ |
| 314 | item->value.min = item->value.max = item->value.last; |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 315 | } |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 316 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 317 | /*! Initialize the stat item module. Call this once from your program. |
| 318 | * \param[in] tall_ctx Talloc context from which this module allocates */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 319 | int osmo_stat_item_init(void *tall_ctx) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 320 | { |
| 321 | tall_stat_item_ctx = tall_ctx; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 326 | /*! Search for item group based on group name and index |
| 327 | * \param[in] name Name of stats_item_group we want to find |
| 328 | * \param[in] idx Index of the group we want to find |
| 329 | * \returns pointer to group, if found; NULL otherwise */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 330 | 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] | 331 | const char *name, const unsigned int idx) |
| 332 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 333 | struct osmo_stat_item_group *statg; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 334 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 335 | llist_for_each_entry(statg, &osmo_stat_item_groups, list) { |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 336 | if (!statg->desc) |
| 337 | continue; |
| 338 | |
| 339 | if (!strcmp(statg->desc->group_name_prefix, name) && |
| 340 | statg->idx == idx) |
| 341 | return statg; |
| 342 | } |
| 343 | return NULL; |
| 344 | } |
| 345 | |
Neels Hofmeyr | 7fcfefb | 2021-09-05 16:22:23 +0200 | [diff] [blame] | 346 | /*! Search for item group based on group name and index's name. |
| 347 | * \param[in] name Name of stats_item_group we want to find. |
| 348 | * \param[in] idx_name Index of the group we want to find, by the index's name (osmo_stat_item_group->name). |
| 349 | * \returns pointer to group, if found; NULL otherwise. */ |
| 350 | struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idxname(const char *group_name, const char *idx_name) |
| 351 | { |
| 352 | struct osmo_stat_item_group *statg; |
| 353 | |
| 354 | llist_for_each_entry(statg, &osmo_stat_item_groups, list) { |
| 355 | if (!statg->desc || !statg->name) |
| 356 | continue; |
| 357 | if (strcmp(statg->desc->group_name_prefix, group_name)) |
| 358 | continue; |
| 359 | if (strcmp(statg->name, idx_name)) |
| 360 | continue; |
| 361 | return statg; |
| 362 | } |
| 363 | return NULL; |
| 364 | } |
| 365 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 366 | /*! Search for item based on group + item name |
| 367 | * \param[in] statg group in which to search for the item |
| 368 | * \param[in] name name of item to search within \a statg |
| 369 | * \returns pointer to item, if found; NULL otherwise */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 370 | const struct osmo_stat_item *osmo_stat_item_get_by_name( |
| 371 | const struct osmo_stat_item_group *statg, const char *name) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 372 | { |
| 373 | int i; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 374 | const struct osmo_stat_item_desc *item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 375 | |
| 376 | if (!statg->desc) |
| 377 | return NULL; |
| 378 | |
| 379 | for (i = 0; i < statg->desc->num_items; i++) { |
| 380 | item_desc = &statg->desc->item_desc[i]; |
| 381 | |
| 382 | if (!strcmp(item_desc->name, name)) { |
| 383 | return statg->items[i]; |
| 384 | } |
| 385 | } |
| 386 | return NULL; |
| 387 | } |
| 388 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 389 | /*! Iterate over all items in group, call user-supplied function on each |
| 390 | * \param[in] statg stat_item group over whose items to iterate |
| 391 | * \param[in] handle_item Call-back function, aborts if rc < 0 |
| 392 | * \param[in] data Private data handed through to \a handle_item |
| 393 | */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 394 | int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, |
| 395 | osmo_stat_item_handler_t handle_item, void *data) |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 396 | { |
| 397 | int rc = 0; |
| 398 | int i; |
| 399 | |
| 400 | for (i = 0; i < statg->desc->num_items; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 401 | struct osmo_stat_item *item = statg->items[i]; |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 402 | rc = handle_item(statg, item, data); |
| 403 | if (rc < 0) |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | return rc; |
| 408 | } |
| 409 | |
Harald Welte | 781951b | 2017-10-15 19:24:57 +0200 | [diff] [blame] | 410 | /*! Iterate over all stat_item groups in system, call user-supplied function on each |
| 411 | * \param[in] handle_group Call-back function, aborts if rc < 0 |
| 412 | * \param[in] data Private data handed through to \a handle_group |
| 413 | */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 414 | 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] | 415 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 416 | struct osmo_stat_item_group *statg; |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 417 | int rc = 0; |
| 418 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 419 | llist_for_each_entry(statg, &osmo_stat_item_groups, list) { |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 420 | rc = handle_group(statg, data); |
| 421 | if (rc < 0) |
| 422 | return rc; |
| 423 | } |
| 424 | |
| 425 | return rc; |
| 426 | } |
| 427 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 428 | /*! Get the last (freshest) value. */ |
| 429 | int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item) |
| 430 | { |
| 431 | return item->value.last; |
| 432 | } |
Daniel Willmann | ea71b43 | 2020-07-14 18:10:20 +0200 | [diff] [blame] | 433 | |
| 434 | /*! Remove all values of a stat item |
| 435 | * \param[in] item stat item to reset |
| 436 | */ |
| 437 | void osmo_stat_item_reset(struct osmo_stat_item *item) |
| 438 | { |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 439 | item->value.sum = 0; |
| 440 | item->value.n = 0; |
| 441 | item->value.last = item->value.min = item->value.max = item->desc->default_value; |
Daniel Willmann | ea71b43 | 2020-07-14 18:10:20 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /*! Reset all osmo stat items in a group |
| 445 | * \param[in] statg stat item group to reset |
| 446 | */ |
| 447 | void osmo_stat_item_group_reset(struct osmo_stat_item_group *statg) |
| 448 | { |
| 449 | int i; |
| 450 | |
| 451 | for (i = 0; i < statg->desc->num_items; i++) { |
| 452 | struct osmo_stat_item *item = statg->items[i]; |
| 453 | osmo_stat_item_reset(item); |
| 454 | } |
| 455 | } |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 456 | |
| 457 | /*! Return the description for an osmo_stat_item. */ |
| 458 | const struct osmo_stat_item_desc *osmo_stat_item_get_desc(struct osmo_stat_item *item) |
| 459 | { |
| 460 | return item->desc; |
| 461 | } |
| 462 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 463 | /*! @} */ |