Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 1 | /* (C) 2009-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 19 | /*! \addtogroup rate_ctr |
| 20 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 21 | * Counters about events and their event rates. |
| 22 | * |
Harald Welte | 53de0d3 | 2017-10-16 14:09:16 +0200 | [diff] [blame] | 23 | * As \ref osmo_counter and \ref osmo_stat_item are concerned only with |
| 24 | * a single given value that may be increased/decreased, or the difference |
| 25 | * to one given previous value, this module adds some support for keeping |
| 26 | * long term information about a given event rate. |
| 27 | * |
| 28 | * A \ref rate_ctr keeps information on the amount of events per second, |
| 29 | * per minute, per hour and per day. |
| 30 | * |
| 31 | * \ref rate_ctr come in groups: An application describes a group of counters |
| 32 | * with their names and identities once in a (typically const) \ref |
| 33 | * rate_ctr_group_desc. |
| 34 | * |
| 35 | * As objects (such as e.g. a subscriber or a PDP context) are |
| 36 | * allocated dynamically at runtime, the application calls \ref |
| 37 | * rate_ctr_group_alloc with a refernce to the \ref |
| 38 | * rate_ctr_group_desc, which causes the library to allocate one set of |
| 39 | * \ref rate_ctr: One for each in the group. |
| 40 | * |
| 41 | * The application then uses functions like \ref rate_ctr_add or \ref |
| 42 | * rate_ctr_inc to increment the value as certain events (e.g. location |
| 43 | * update) happens. |
| 44 | * |
| 45 | * The library internally keeps a timer once per second which iterates |
| 46 | * over all registered counters and which updates the per-second, |
| 47 | * per-minute, per-hour and per-day averages based on the current |
| 48 | * value. |
| 49 | * |
| 50 | * The counters can be reported using \ref stats or by VTY |
| 51 | * introspection, as well as by any application-specific code accessing |
| 52 | * the \ref rate_ctr.intv array directly. |
| 53 | * |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 54 | * \file rate_ctr.c */ |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 55 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 56 | #include <stdbool.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 57 | #include <stdint.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 58 | #include <string.h> |
| 59 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 60 | #include <osmocom/core/utils.h> |
| 61 | #include <osmocom/core/linuxlist.h> |
| 62 | #include <osmocom/core/talloc.h> |
| 63 | #include <osmocom/core/timer.h> |
| 64 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 65 | #include <osmocom/core/logging.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 66 | |
| 67 | static LLIST_HEAD(rate_ctr_groups); |
Harald Welte | ea8272c | 2019-05-10 11:52:02 +0200 | [diff] [blame] | 68 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 69 | static void *tall_rate_ctr_ctx; |
| 70 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 71 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 72 | static bool rate_ctrl_group_desc_validate(const struct rate_ctr_group_desc *desc) |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 73 | { |
| 74 | unsigned int i; |
Harald Welte | b48e82c | 2017-10-24 18:35:24 +0200 | [diff] [blame] | 75 | const struct rate_ctr_desc *ctr_desc; |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 76 | |
| 77 | if (!desc) { |
| 78 | LOGP(DLGLOBAL, LOGL_ERROR, "NULL is not a valid counter group descriptor\n"); |
| 79 | return false; |
| 80 | } |
Harald Welte | b48e82c | 2017-10-24 18:35:24 +0200 | [diff] [blame] | 81 | ctr_desc = desc->ctr_desc; |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 82 | |
| 83 | DEBUGP(DLGLOBAL, "validating counter group %p(%s) with %u counters\n", desc, |
| 84 | desc->group_name_prefix, desc->num_ctr); |
| 85 | |
| 86 | if (!osmo_identifier_valid(desc->group_name_prefix)) { |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 87 | LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter group identifier\n", |
| 88 | desc->group_name_prefix); |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | |
| 92 | for (i = 0; i < desc->num_ctr; i++) { |
| 93 | if (!osmo_identifier_valid(ctr_desc[i].name)) { |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 94 | LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter identifier\n", |
| 95 | ctr_desc[i].name); |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 103 | /* return 'in' if it doesn't contain any '.'; otherwise allocate a copy and |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 104 | * replace all '.' with ':' */ |
| 105 | static char *mangle_identifier_ifneeded(const void *ctx, const char *in) |
| 106 | { |
| 107 | char *out; |
| 108 | unsigned int i; |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 109 | bool modified = false; |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 110 | |
| 111 | if (!in) |
| 112 | return NULL; |
| 113 | |
| 114 | if (!strchr(in, '.')) |
| 115 | return (char *)in; |
| 116 | |
| 117 | out = talloc_strdup(ctx, in); |
| 118 | OSMO_ASSERT(out); |
| 119 | |
| 120 | for (i = 0; i < strlen(out); i++) { |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 121 | if (out[i] == '.') { |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 122 | out[i] = ':'; |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 123 | modified = true; |
| 124 | } |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 127 | if (modified) |
| 128 | LOGP(DLGLOBAL, LOGL_NOTICE, "counter group name mangled: '%s' -> '%s'\n", |
| 129 | in, out); |
| 130 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 131 | return out; |
| 132 | } |
| 133 | |
| 134 | /* "mangle" a rate counter group descriptor, i.e. replace any '.' with ':' */ |
| 135 | static struct rate_ctr_group_desc * |
| 136 | rate_ctr_group_desc_mangle(void *ctx, const struct rate_ctr_group_desc *desc) |
| 137 | { |
| 138 | struct rate_ctr_group_desc *desc_new = talloc_zero(ctx, struct rate_ctr_group_desc); |
| 139 | int i; |
| 140 | |
| 141 | OSMO_ASSERT(desc_new); |
| 142 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 143 | LOGP(DLGLOBAL, LOGL_INFO, "Needed to mangle counter group '%s' names: it is still using '.' as " |
| 144 | "separator, which is not allowed. please consider updating the application\n", |
| 145 | desc->group_name_prefix); |
| 146 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 147 | /* mangle the name_prefix but copy/keep the rest */ |
| 148 | desc_new->group_name_prefix = mangle_identifier_ifneeded(desc_new, desc->group_name_prefix); |
| 149 | desc_new->group_description = desc->group_description; |
| 150 | desc_new->class_id = desc->class_id; |
| 151 | desc_new->num_ctr = desc->num_ctr; |
| 152 | desc_new->ctr_desc = talloc_array(desc_new, struct rate_ctr_desc, desc_new->num_ctr); |
| 153 | OSMO_ASSERT(desc_new->ctr_desc); |
| 154 | |
| 155 | for (i = 0; i < desc->num_ctr; i++) { |
| 156 | struct rate_ctr_desc *ctrd_new = (struct rate_ctr_desc *) desc_new->ctr_desc; |
| 157 | const struct rate_ctr_desc *ctrd = desc->ctr_desc; |
| 158 | |
| 159 | if (!ctrd[i].name) { |
| 160 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s'[%d] == NULL, aborting\n", |
| 161 | desc->group_name_prefix, i); |
| 162 | goto err_free; |
| 163 | } |
| 164 | |
| 165 | ctrd_new[i].name = mangle_identifier_ifneeded(desc_new->ctr_desc, ctrd[i].name); |
| 166 | ctrd_new[i].description = ctrd[i].description; |
| 167 | } |
| 168 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 169 | if (!rate_ctrl_group_desc_validate(desc_new)) { |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 170 | /* simple mangling of identifiers ('.' -> ':') was not sufficient to render a valid |
| 171 | * descriptor, we have to bail out */ |
| 172 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' still invalid after mangling\n", |
| 173 | desc->group_name_prefix); |
| 174 | goto err_free; |
| 175 | } |
| 176 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 177 | return desc_new; |
| 178 | err_free: |
| 179 | talloc_free(desc_new); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
Neels Hofmeyr | 554f7b8 | 2017-12-20 01:14:31 +0100 | [diff] [blame] | 183 | /*! Find an unused index for this rate counter group. |
| 184 | * \param[in] name Name of the counter group |
| 185 | * \returns the largest used index number + 1, or 0 if none exist yet. */ |
| 186 | static unsigned int rate_ctr_get_unused_name_idx(const char *name) |
| 187 | { |
| 188 | unsigned int idx = 0; |
| 189 | struct rate_ctr_group *ctrg; |
| 190 | |
| 191 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) { |
| 192 | if (!ctrg->desc) |
| 193 | continue; |
| 194 | |
| 195 | if (strcmp(ctrg->desc->group_name_prefix, name)) |
| 196 | continue; |
| 197 | |
| 198 | if (idx <= ctrg->idx) |
| 199 | idx = ctrg->idx + 1; |
| 200 | } |
| 201 | return idx; |
| 202 | } |
| 203 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 204 | /*! Allocate a new group of counters according to description |
Vadim Yanitskiy | ed3a3c6 | 2019-03-26 00:57:45 +0700 | [diff] [blame] | 205 | * \param[in] ctx parent talloc context |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 206 | * \param[in] desc Rate counter group description |
| 207 | * \param[in] idx Index of new counter group |
| 208 | */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 209 | struct rate_ctr_group *rate_ctr_group_alloc(void *ctx, |
| 210 | const struct rate_ctr_group_desc *desc, |
| 211 | unsigned int idx) |
| 212 | { |
| 213 | unsigned int size; |
| 214 | struct rate_ctr_group *group; |
| 215 | |
Harald Welte | d589f1d | 2017-12-18 17:13:38 +0100 | [diff] [blame] | 216 | if (rate_ctr_get_group_by_name_idx(desc->group_name_prefix, idx)) { |
Neels Hofmeyr | 554f7b8 | 2017-12-20 01:14:31 +0100 | [diff] [blame] | 217 | unsigned int new_idx = rate_ctr_get_unused_name_idx(desc->group_name_prefix); |
| 218 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' already exists for index %u," |
| 219 | " instead using index %u. This is a software bug that needs fixing.\n", |
| 220 | desc->group_name_prefix, idx, new_idx); |
| 221 | idx = new_idx; |
Harald Welte | d589f1d | 2017-12-18 17:13:38 +0100 | [diff] [blame] | 222 | } |
Max | 3ef14a2 | 2017-12-15 20:19:10 +0100 | [diff] [blame] | 223 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 224 | size = sizeof(struct rate_ctr_group) + |
| 225 | desc->num_ctr * sizeof(struct rate_ctr); |
| 226 | |
| 227 | if (!ctx) |
| 228 | ctx = tall_rate_ctr_ctx; |
| 229 | |
| 230 | group = talloc_zero_size(ctx, size); |
| 231 | if (!group) |
| 232 | return NULL; |
| 233 | |
Neels Hofmeyr | 10ee73a | 2017-11-16 18:31:57 +0100 | [diff] [blame] | 234 | /* attempt to mangle all '.' in identifiers to ':' for backwards compat */ |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 235 | if (!rate_ctrl_group_desc_validate(desc)) { |
Neels Hofmeyr | 10ee73a | 2017-11-16 18:31:57 +0100 | [diff] [blame] | 236 | desc = rate_ctr_group_desc_mangle(group, desc); |
| 237 | if (!desc) { |
| 238 | talloc_free(group); |
| 239 | return NULL; |
| 240 | } |
| 241 | } |
| 242 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 243 | group->desc = desc; |
Harald Welte | 087fcff | 2010-05-13 12:16:17 +0200 | [diff] [blame] | 244 | group->idx = idx; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 245 | |
| 246 | llist_add(&group->list, &rate_ctr_groups); |
| 247 | |
| 248 | return group; |
| 249 | } |
| 250 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 251 | /*! Free the memory for the specified group of counters */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 252 | void rate_ctr_group_free(struct rate_ctr_group *grp) |
| 253 | { |
Max | fb6f43e | 2019-03-18 15:41:26 +0100 | [diff] [blame] | 254 | if (!grp) |
| 255 | return; |
| 256 | |
| 257 | if (!llist_empty(&grp->list)) |
| 258 | llist_del(&grp->list); |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 259 | talloc_free(grp); |
| 260 | } |
| 261 | |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 262 | /*! Get rate counter from group, identified by index idx |
| 263 | * \param[in] grp Rate counter group |
| 264 | * \param[in] idx Index of the counter to retrieve |
| 265 | * \returns rate counter requested |
| 266 | */ |
| 267 | struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx) |
| 268 | { |
| 269 | return &grp->ctr[idx]; |
| 270 | } |
| 271 | |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 272 | /*! Set a name for the group of counters be used instead of index value |
| 273 | at report time. |
| 274 | * \param[in] grp Rate counter group |
| 275 | * \param[in] name Name identifier to assign to the rate counter group |
| 276 | */ |
| 277 | void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name) |
| 278 | { |
| 279 | osmo_talloc_replace_string(grp, &grp->name, name); |
| 280 | } |
| 281 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 282 | /*! Add a number to the counter */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 283 | void rate_ctr_add(struct rate_ctr *ctr, int inc) |
| 284 | { |
| 285 | ctr->current += inc; |
| 286 | } |
| 287 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 288 | /*! Return the counter difference since the last call to this function */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 289 | int64_t rate_ctr_difference(struct rate_ctr *ctr) |
| 290 | { |
| 291 | int64_t result = ctr->current - ctr->previous; |
| 292 | ctr->previous = ctr->current; |
| 293 | |
| 294 | return result; |
| 295 | } |
| 296 | |
Jacob Erlbeck | ee702cd | 2015-11-10 11:38:25 +0100 | [diff] [blame] | 297 | /* TODO: support update intervals > 1s */ |
| 298 | /* TODO: implement this as a special stats reporter */ |
| 299 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 300 | static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv) |
| 301 | { |
| 302 | /* calculate rate over last interval */ |
| 303 | ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last; |
| 304 | /* save current counter for next interval */ |
| 305 | ctr->intv[intv].last = ctr->current; |
Harald Welte | d2dce6d | 2010-05-13 13:28:12 +0200 | [diff] [blame] | 306 | |
| 307 | /* update the rate of the next bigger interval. This will |
| 308 | * be overwritten when that next larger interval expires */ |
| 309 | if (intv + 1 < ARRAY_SIZE(ctr->intv)) |
| 310 | ctr->intv[intv+1].rate += ctr->intv[intv].rate; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Harald Welte | ea8272c | 2019-05-10 11:52:02 +0200 | [diff] [blame] | 313 | static struct osmo_timer_list rate_ctr_timer; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 314 | static uint64_t timer_ticks; |
| 315 | |
| 316 | /* The one-second interval has expired */ |
| 317 | static void rate_ctr_group_intv(struct rate_ctr_group *grp) |
| 318 | { |
| 319 | unsigned int i; |
| 320 | |
| 321 | for (i = 0; i < grp->desc->num_ctr; i++) { |
| 322 | struct rate_ctr *ctr = &grp->ctr[i]; |
| 323 | |
| 324 | interval_expired(ctr, RATE_CTR_INTV_SEC); |
| 325 | if ((timer_ticks % 60) == 0) |
| 326 | interval_expired(ctr, RATE_CTR_INTV_MIN); |
| 327 | if ((timer_ticks % (60*60)) == 0) |
| 328 | interval_expired(ctr, RATE_CTR_INTV_HOUR); |
| 329 | if ((timer_ticks % (24*60*60)) == 0) |
| 330 | interval_expired(ctr, RATE_CTR_INTV_DAY); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static void rate_ctr_timer_cb(void *data) |
| 335 | { |
| 336 | struct rate_ctr_group *ctrg; |
| 337 | |
| 338 | /* Increment number of ticks before we calculate intervals, |
| 339 | * as a counter value of 0 would already wrap all counters */ |
| 340 | timer_ticks++; |
| 341 | |
| 342 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) |
| 343 | rate_ctr_group_intv(ctrg); |
| 344 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 345 | osmo_timer_schedule(&rate_ctr_timer, 1, 0); |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Harald Welte | 53de0d3 | 2017-10-16 14:09:16 +0200 | [diff] [blame] | 348 | /*! Initialize the counter module. Call this once from your application. |
| 349 | * \param[in] tall_ctx Talloc context from which rate_ctr_group will be allocated |
| 350 | * \returns 0 on success; negative on error */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 351 | int rate_ctr_init(void *tall_ctx) |
| 352 | { |
Harald Welte | 5eb67c2 | 2021-11-14 20:45:07 +0100 | [diff] [blame] | 353 | /* ignore repeated initialization */ |
| 354 | if (osmo_timer_pending(&rate_ctr_timer)) |
| 355 | return 0; |
| 356 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 357 | tall_rate_ctr_ctx = tall_ctx; |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 358 | osmo_timer_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL); |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 359 | osmo_timer_schedule(&rate_ctr_timer, 1, 0); |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | } |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 363 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 364 | /*! Search for counter group based on group name and index |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 365 | * \param[in] name Name of the counter group you're looking for |
| 366 | * \param[in] idx Index inside the counter group |
| 367 | * \returns \ref rate_ctr_group or NULL in case of error */ |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 368 | struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx) |
| 369 | { |
| 370 | struct rate_ctr_group *ctrg; |
| 371 | |
| 372 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) { |
| 373 | if (!ctrg->desc) |
| 374 | continue; |
| 375 | |
| 376 | if (!strcmp(ctrg->desc->group_name_prefix, name) && |
| 377 | ctrg->idx == idx) { |
| 378 | return ctrg; |
| 379 | } |
| 380 | } |
| 381 | return NULL; |
| 382 | } |
| 383 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 384 | /*! Search for counter based on group + name |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 385 | * \param[in] ctrg pointer to \ref rate_ctr_group |
| 386 | * \param[in] name name of counter inside group |
Harald Welte | 53de0d3 | 2017-10-16 14:09:16 +0200 | [diff] [blame] | 387 | * \returns \ref rate_ctr or NULL in case of error |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 388 | */ |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 389 | const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name) |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 390 | { |
| 391 | int i; |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 392 | const struct rate_ctr_desc *ctr_desc; |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 393 | |
| 394 | if (!ctrg->desc) |
| 395 | return NULL; |
| 396 | |
| 397 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 398 | ctr_desc = &ctrg->desc->ctr_desc[i]; |
| 399 | |
| 400 | if (!strcmp(ctr_desc->name, name)) { |
| 401 | return &ctrg->ctr[i]; |
| 402 | } |
| 403 | } |
| 404 | return NULL; |
| 405 | } |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 406 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 407 | /*! Iterate over each counter in group and call function |
Vadim Yanitskiy | c761044 | 2019-03-26 00:48:30 +0700 | [diff] [blame] | 408 | * \param[in] ctrg counter group over which to iterate |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 409 | * \param[in] handle_counter function pointer |
Vadim Yanitskiy | ed3a3c6 | 2019-03-26 00:57:45 +0700 | [diff] [blame] | 410 | * \param[in] data Data to hand transparently to handle_counter() |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 411 | * \returns 0 on success; negative otherwise |
| 412 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 413 | int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, |
| 414 | rate_ctr_handler_t handle_counter, void *data) |
| 415 | { |
| 416 | int rc = 0; |
| 417 | int i; |
| 418 | |
| 419 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 420 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 421 | rc = handle_counter(ctrg, |
| 422 | ctr, &ctrg->desc->ctr_desc[i], data); |
| 423 | if (rc < 0) |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | return rc; |
| 428 | } |
| 429 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 430 | /*! Iterate over all counter groups |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 431 | * \param[in] handle_group function pointer of callback function |
Vadim Yanitskiy | ed3a3c6 | 2019-03-26 00:57:45 +0700 | [diff] [blame] | 432 | * \param[in] data Data to hand transparently to handle_group() |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 433 | * \returns 0 on success; negative otherwise |
| 434 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 435 | int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data) |
| 436 | { |
| 437 | struct rate_ctr_group *statg; |
| 438 | int rc = 0; |
| 439 | |
| 440 | llist_for_each_entry(statg, &rate_ctr_groups, list) { |
| 441 | rc = handle_group(statg, data); |
| 442 | if (rc < 0) |
| 443 | return rc; |
| 444 | } |
| 445 | |
| 446 | return rc; |
| 447 | } |
| 448 | |
Daniel Willmann | 26a9539 | 2020-07-14 18:04:18 +0200 | [diff] [blame] | 449 | /*! Reset a rate counter back to zero |
| 450 | * \param[in] ctr counter to reset |
| 451 | */ |
| 452 | void rate_ctr_reset(struct rate_ctr *ctr) |
| 453 | { |
| 454 | memset(ctr, 0, sizeof(*ctr)); |
| 455 | } |
| 456 | |
| 457 | /*! Reset all counters in a group |
| 458 | * \param[in] ctrg counter group to reset |
| 459 | */ |
| 460 | void rate_ctr_group_reset(struct rate_ctr_group *ctrg) |
| 461 | { |
| 462 | int i; |
| 463 | |
| 464 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 465 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 466 | rate_ctr_reset(ctr); |
| 467 | } |
| 468 | } |
| 469 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 470 | /*! @} */ |