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