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