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); |
| 72 | |
| 73 | static void *tall_rate_ctr_ctx; |
| 74 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 75 | |
| 76 | static bool rate_ctrl_group_desc_validate(const struct rate_ctr_group_desc *desc, bool quiet) |
| 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)) { |
| 91 | if (!quiet) |
| 92 | LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter group identifier\n", |
| 93 | desc->group_name_prefix); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | for (i = 0; i < desc->num_ctr; i++) { |
| 98 | if (!osmo_identifier_valid(ctr_desc[i].name)) { |
| 99 | if (!quiet) |
| 100 | LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter identifier\n", |
| 101 | ctr_desc[i].name); |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | /* return 'in' if it doesn't contaon any '.'; otherwise allocate a copy and |
| 110 | * replace all '.' with ':' */ |
| 111 | static char *mangle_identifier_ifneeded(const void *ctx, const char *in) |
| 112 | { |
| 113 | char *out; |
| 114 | unsigned int i; |
| 115 | |
| 116 | if (!in) |
| 117 | return NULL; |
| 118 | |
| 119 | if (!strchr(in, '.')) |
| 120 | return (char *)in; |
| 121 | |
| 122 | out = talloc_strdup(ctx, in); |
| 123 | OSMO_ASSERT(out); |
| 124 | |
| 125 | for (i = 0; i < strlen(out); i++) { |
| 126 | if (out[i] == '.') |
| 127 | out[i] = ':'; |
| 128 | } |
| 129 | |
| 130 | return out; |
| 131 | } |
| 132 | |
| 133 | /* "mangle" a rate counter group descriptor, i.e. replace any '.' with ':' */ |
| 134 | static struct rate_ctr_group_desc * |
| 135 | rate_ctr_group_desc_mangle(void *ctx, const struct rate_ctr_group_desc *desc) |
| 136 | { |
| 137 | struct rate_ctr_group_desc *desc_new = talloc_zero(ctx, struct rate_ctr_group_desc); |
| 138 | int i; |
| 139 | |
| 140 | OSMO_ASSERT(desc_new); |
| 141 | |
| 142 | /* mangle the name_prefix but copy/keep the rest */ |
| 143 | desc_new->group_name_prefix = mangle_identifier_ifneeded(desc_new, desc->group_name_prefix); |
| 144 | desc_new->group_description = desc->group_description; |
| 145 | desc_new->class_id = desc->class_id; |
| 146 | desc_new->num_ctr = desc->num_ctr; |
| 147 | desc_new->ctr_desc = talloc_array(desc_new, struct rate_ctr_desc, desc_new->num_ctr); |
| 148 | OSMO_ASSERT(desc_new->ctr_desc); |
| 149 | |
| 150 | for (i = 0; i < desc->num_ctr; i++) { |
| 151 | struct rate_ctr_desc *ctrd_new = (struct rate_ctr_desc *) desc_new->ctr_desc; |
| 152 | const struct rate_ctr_desc *ctrd = desc->ctr_desc; |
| 153 | |
| 154 | if (!ctrd[i].name) { |
| 155 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s'[%d] == NULL, aborting\n", |
| 156 | desc->group_name_prefix, i); |
| 157 | goto err_free; |
| 158 | } |
| 159 | |
| 160 | ctrd_new[i].name = mangle_identifier_ifneeded(desc_new->ctr_desc, ctrd[i].name); |
| 161 | ctrd_new[i].description = ctrd[i].description; |
| 162 | } |
| 163 | |
| 164 | if (!rate_ctrl_group_desc_validate(desc_new, false)) { |
| 165 | /* simple mangling of identifiers ('.' -> ':') was not sufficient to render a valid |
| 166 | * descriptor, we have to bail out */ |
| 167 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' still invalid after mangling\n", |
| 168 | desc->group_name_prefix); |
| 169 | goto err_free; |
| 170 | } |
| 171 | |
| 172 | LOGP(DLGLOBAL, LOGL_INFO, "Needed to mangle ounter group '%s' names still using '.' as " |
| 173 | "separator, please consider updating the application\n", desc->group_name_prefix); |
| 174 | |
| 175 | return desc_new; |
| 176 | err_free: |
| 177 | talloc_free(desc_new); |
| 178 | return NULL; |
| 179 | } |
| 180 | |
Neels Hofmeyr | 554f7b8 | 2017-12-20 01:14:31 +0100 | [diff] [blame] | 181 | /*! Find an unused index for this rate counter group. |
| 182 | * \param[in] name Name of the counter group |
| 183 | * \returns the largest used index number + 1, or 0 if none exist yet. */ |
| 184 | static unsigned int rate_ctr_get_unused_name_idx(const char *name) |
| 185 | { |
| 186 | unsigned int idx = 0; |
| 187 | struct rate_ctr_group *ctrg; |
| 188 | |
| 189 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) { |
| 190 | if (!ctrg->desc) |
| 191 | continue; |
| 192 | |
| 193 | if (strcmp(ctrg->desc->group_name_prefix, name)) |
| 194 | continue; |
| 195 | |
| 196 | if (idx <= ctrg->idx) |
| 197 | idx = ctrg->idx + 1; |
| 198 | } |
| 199 | return idx; |
| 200 | } |
| 201 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 202 | /*! Allocate a new group of counters according to description |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 203 | * \param[in] ctx \ref talloc context |
| 204 | * \param[in] desc Rate counter group description |
| 205 | * \param[in] idx Index of new counter group |
| 206 | */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 207 | struct rate_ctr_group *rate_ctr_group_alloc(void *ctx, |
| 208 | const struct rate_ctr_group_desc *desc, |
| 209 | unsigned int idx) |
| 210 | { |
| 211 | unsigned int size; |
| 212 | struct rate_ctr_group *group; |
| 213 | |
Harald Welte | d589f1d | 2017-12-18 17:13:38 +0100 | [diff] [blame] | 214 | 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] | 215 | unsigned int new_idx = rate_ctr_get_unused_name_idx(desc->group_name_prefix); |
| 216 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' already exists for index %u," |
| 217 | " instead using index %u. This is a software bug that needs fixing.\n", |
| 218 | desc->group_name_prefix, idx, new_idx); |
| 219 | idx = new_idx; |
Harald Welte | d589f1d | 2017-12-18 17:13:38 +0100 | [diff] [blame] | 220 | } |
Max | 3ef14a2 | 2017-12-15 20:19:10 +0100 | [diff] [blame] | 221 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 222 | size = sizeof(struct rate_ctr_group) + |
| 223 | desc->num_ctr * sizeof(struct rate_ctr); |
| 224 | |
| 225 | if (!ctx) |
| 226 | ctx = tall_rate_ctr_ctx; |
| 227 | |
| 228 | group = talloc_zero_size(ctx, size); |
| 229 | if (!group) |
| 230 | return NULL; |
| 231 | |
Neels Hofmeyr | 10ee73a | 2017-11-16 18:31:57 +0100 | [diff] [blame] | 232 | /* attempt to mangle all '.' in identifiers to ':' for backwards compat */ |
| 233 | if (!rate_ctrl_group_desc_validate(desc, true)) { |
| 234 | desc = rate_ctr_group_desc_mangle(group, desc); |
| 235 | if (!desc) { |
| 236 | talloc_free(group); |
| 237 | return NULL; |
| 238 | } |
| 239 | } |
| 240 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 241 | group->desc = desc; |
Harald Welte | 087fcff | 2010-05-13 12:16:17 +0200 | [diff] [blame] | 242 | group->idx = idx; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 243 | |
| 244 | llist_add(&group->list, &rate_ctr_groups); |
| 245 | |
| 246 | return group; |
| 247 | } |
| 248 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 249 | /*! Free the memory for the specified group of counters */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 250 | void rate_ctr_group_free(struct rate_ctr_group *grp) |
| 251 | { |
| 252 | llist_del(&grp->list); |
| 253 | talloc_free(grp); |
| 254 | } |
| 255 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 256 | /*! Add a number to the counter */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 257 | void rate_ctr_add(struct rate_ctr *ctr, int inc) |
| 258 | { |
| 259 | ctr->current += inc; |
| 260 | } |
| 261 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 262 | /*! Return the counter difference since the last call to this function */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 263 | int64_t rate_ctr_difference(struct rate_ctr *ctr) |
| 264 | { |
| 265 | int64_t result = ctr->current - ctr->previous; |
| 266 | ctr->previous = ctr->current; |
| 267 | |
| 268 | return result; |
| 269 | } |
| 270 | |
Jacob Erlbeck | ee702cd | 2015-11-10 11:38:25 +0100 | [diff] [blame] | 271 | /* TODO: support update intervals > 1s */ |
| 272 | /* TODO: implement this as a special stats reporter */ |
| 273 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 274 | static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv) |
| 275 | { |
| 276 | /* calculate rate over last interval */ |
| 277 | ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last; |
| 278 | /* save current counter for next interval */ |
| 279 | ctr->intv[intv].last = ctr->current; |
Harald Welte | d2dce6d | 2010-05-13 13:28:12 +0200 | [diff] [blame] | 280 | |
| 281 | /* update the rate of the next bigger interval. This will |
| 282 | * be overwritten when that next larger interval expires */ |
| 283 | if (intv + 1 < ARRAY_SIZE(ctr->intv)) |
| 284 | ctr->intv[intv+1].rate += ctr->intv[intv].rate; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 287 | static struct osmo_timer_list rate_ctr_timer; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 288 | static uint64_t timer_ticks; |
| 289 | |
| 290 | /* The one-second interval has expired */ |
| 291 | static void rate_ctr_group_intv(struct rate_ctr_group *grp) |
| 292 | { |
| 293 | unsigned int i; |
| 294 | |
| 295 | for (i = 0; i < grp->desc->num_ctr; i++) { |
| 296 | struct rate_ctr *ctr = &grp->ctr[i]; |
| 297 | |
| 298 | interval_expired(ctr, RATE_CTR_INTV_SEC); |
| 299 | if ((timer_ticks % 60) == 0) |
| 300 | interval_expired(ctr, RATE_CTR_INTV_MIN); |
| 301 | if ((timer_ticks % (60*60)) == 0) |
| 302 | interval_expired(ctr, RATE_CTR_INTV_HOUR); |
| 303 | if ((timer_ticks % (24*60*60)) == 0) |
| 304 | interval_expired(ctr, RATE_CTR_INTV_DAY); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static void rate_ctr_timer_cb(void *data) |
| 309 | { |
| 310 | struct rate_ctr_group *ctrg; |
| 311 | |
| 312 | /* Increment number of ticks before we calculate intervals, |
| 313 | * as a counter value of 0 would already wrap all counters */ |
| 314 | timer_ticks++; |
| 315 | |
| 316 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) |
| 317 | rate_ctr_group_intv(ctrg); |
| 318 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 319 | osmo_timer_schedule(&rate_ctr_timer, 1, 0); |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Harald Welte | 53de0d3 | 2017-10-16 14:09:16 +0200 | [diff] [blame] | 322 | /*! Initialize the counter module. Call this once from your application. |
| 323 | * \param[in] tall_ctx Talloc context from which rate_ctr_group will be allocated |
| 324 | * \returns 0 on success; negative on error */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 325 | int rate_ctr_init(void *tall_ctx) |
| 326 | { |
| 327 | tall_rate_ctr_ctx = tall_ctx; |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 328 | osmo_timer_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL); |
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 | return 0; |
| 332 | } |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 333 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 334 | /*! Search for counter group based on group name and index |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 335 | * \param[in] name Name of the counter group you're looking for |
| 336 | * \param[in] idx Index inside the counter group |
| 337 | * \returns \ref rate_ctr_group or NULL in case of error */ |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 338 | struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx) |
| 339 | { |
| 340 | struct rate_ctr_group *ctrg; |
| 341 | |
| 342 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) { |
| 343 | if (!ctrg->desc) |
| 344 | continue; |
| 345 | |
| 346 | if (!strcmp(ctrg->desc->group_name_prefix, name) && |
| 347 | ctrg->idx == idx) { |
| 348 | return ctrg; |
| 349 | } |
| 350 | } |
| 351 | return NULL; |
| 352 | } |
| 353 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 354 | /*! Search for counter based on group + name |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 355 | * \param[in] ctrg pointer to \ref rate_ctr_group |
| 356 | * \param[in] name name of counter inside group |
Harald Welte | 53de0d3 | 2017-10-16 14:09:16 +0200 | [diff] [blame] | 357 | * \returns \ref rate_ctr or NULL in case of error |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 358 | */ |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 359 | 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] | 360 | { |
| 361 | int i; |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 362 | const struct rate_ctr_desc *ctr_desc; |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 363 | |
| 364 | if (!ctrg->desc) |
| 365 | return NULL; |
| 366 | |
| 367 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 368 | ctr_desc = &ctrg->desc->ctr_desc[i]; |
| 369 | |
| 370 | if (!strcmp(ctr_desc->name, name)) { |
| 371 | return &ctrg->ctr[i]; |
| 372 | } |
| 373 | } |
| 374 | return NULL; |
| 375 | } |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 376 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 377 | /*! Iterate over each counter in group and call function |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 378 | * \param[in] counter group over whose counter to iterate |
| 379 | * \param[in] handle_counter function pointer |
| 380 | * \param[in] data Data to hand transparently to \ref handle_counter |
| 381 | * \returns 0 on success; negative otherwise |
| 382 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 383 | int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, |
| 384 | rate_ctr_handler_t handle_counter, void *data) |
| 385 | { |
| 386 | int rc = 0; |
| 387 | int i; |
| 388 | |
| 389 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 390 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 391 | rc = handle_counter(ctrg, |
| 392 | ctr, &ctrg->desc->ctr_desc[i], data); |
| 393 | if (rc < 0) |
| 394 | return rc; |
| 395 | } |
| 396 | |
| 397 | return rc; |
| 398 | } |
| 399 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 400 | /*! Iterate over all counter groups |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 401 | * \param[in] handle_group function pointer of callback function |
| 402 | * \param[in] data Data to hand transparently to \ref handle_group |
| 403 | * \returns 0 on success; negative otherwise |
| 404 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 405 | int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data) |
| 406 | { |
| 407 | struct rate_ctr_group *statg; |
| 408 | int rc = 0; |
| 409 | |
| 410 | llist_for_each_entry(statg, &rate_ctr_groups, list) { |
| 411 | rc = handle_group(statg, data); |
| 412 | if (rc < 0) |
| 413 | return rc; |
| 414 | } |
| 415 | |
| 416 | return rc; |
| 417 | } |
| 418 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 419 | /*! @} */ |