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 | |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 56 | #include <errno.h> |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 57 | #include <stdbool.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 58 | #include <stdint.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 59 | #include <string.h> |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 60 | #include <unistd.h> |
| 61 | #include <inttypes.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 62 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 63 | #include <osmocom/core/utils.h> |
| 64 | #include <osmocom/core/linuxlist.h> |
| 65 | #include <osmocom/core/talloc.h> |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 66 | #include <osmocom/core/select.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 67 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 68 | #include <osmocom/core/logging.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 69 | |
| 70 | static LLIST_HEAD(rate_ctr_groups); |
Harald Welte | ea8272c | 2019-05-10 11:52:02 +0200 | [diff] [blame] | 71 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 72 | static void *tall_rate_ctr_ctx; |
| 73 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 74 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 75 | 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] | 76 | { |
| 77 | unsigned int i; |
Harald Welte | b48e82c | 2017-10-24 18:35:24 +0200 | [diff] [blame] | 78 | const struct rate_ctr_desc *ctr_desc; |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 79 | |
| 80 | if (!desc) { |
| 81 | LOGP(DLGLOBAL, LOGL_ERROR, "NULL is not a valid counter group descriptor\n"); |
| 82 | return false; |
| 83 | } |
Harald Welte | b48e82c | 2017-10-24 18:35:24 +0200 | [diff] [blame] | 84 | ctr_desc = desc->ctr_desc; |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 85 | |
| 86 | DEBUGP(DLGLOBAL, "validating counter group %p(%s) with %u counters\n", desc, |
| 87 | desc->group_name_prefix, desc->num_ctr); |
| 88 | |
| 89 | if (!osmo_identifier_valid(desc->group_name_prefix)) { |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 90 | LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter group identifier\n", |
| 91 | desc->group_name_prefix); |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
| 95 | for (i = 0; i < desc->num_ctr; i++) { |
| 96 | if (!osmo_identifier_valid(ctr_desc[i].name)) { |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 97 | LOGP(DLGLOBAL, LOGL_ERROR, "'%s' is not a valid counter identifier\n", |
| 98 | ctr_desc[i].name); |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 106 | /* 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] | 107 | * replace all '.' with ':' */ |
| 108 | static char *mangle_identifier_ifneeded(const void *ctx, const char *in) |
| 109 | { |
| 110 | char *out; |
| 111 | unsigned int i; |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 112 | bool modified = false; |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 113 | |
| 114 | if (!in) |
| 115 | return NULL; |
| 116 | |
| 117 | if (!strchr(in, '.')) |
| 118 | return (char *)in; |
| 119 | |
| 120 | out = talloc_strdup(ctx, in); |
| 121 | OSMO_ASSERT(out); |
| 122 | |
| 123 | for (i = 0; i < strlen(out); i++) { |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 124 | if (out[i] == '.') { |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 125 | out[i] = ':'; |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 126 | modified = true; |
| 127 | } |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 130 | if (modified) |
| 131 | LOGP(DLGLOBAL, LOGL_NOTICE, "counter group name mangled: '%s' -> '%s'\n", |
| 132 | in, out); |
| 133 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 134 | return out; |
| 135 | } |
| 136 | |
| 137 | /* "mangle" a rate counter group descriptor, i.e. replace any '.' with ':' */ |
| 138 | static struct rate_ctr_group_desc * |
| 139 | rate_ctr_group_desc_mangle(void *ctx, const struct rate_ctr_group_desc *desc) |
| 140 | { |
| 141 | struct rate_ctr_group_desc *desc_new = talloc_zero(ctx, struct rate_ctr_group_desc); |
| 142 | int i; |
| 143 | |
| 144 | OSMO_ASSERT(desc_new); |
| 145 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 146 | LOGP(DLGLOBAL, LOGL_INFO, "Needed to mangle counter group '%s' names: it is still using '.' as " |
| 147 | "separator, which is not allowed. please consider updating the application\n", |
| 148 | desc->group_name_prefix); |
| 149 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 150 | /* mangle the name_prefix but copy/keep the rest */ |
| 151 | desc_new->group_name_prefix = mangle_identifier_ifneeded(desc_new, desc->group_name_prefix); |
| 152 | desc_new->group_description = desc->group_description; |
| 153 | desc_new->class_id = desc->class_id; |
| 154 | desc_new->num_ctr = desc->num_ctr; |
| 155 | desc_new->ctr_desc = talloc_array(desc_new, struct rate_ctr_desc, desc_new->num_ctr); |
| 156 | OSMO_ASSERT(desc_new->ctr_desc); |
| 157 | |
| 158 | for (i = 0; i < desc->num_ctr; i++) { |
| 159 | struct rate_ctr_desc *ctrd_new = (struct rate_ctr_desc *) desc_new->ctr_desc; |
| 160 | const struct rate_ctr_desc *ctrd = desc->ctr_desc; |
| 161 | |
| 162 | if (!ctrd[i].name) { |
| 163 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s'[%d] == NULL, aborting\n", |
| 164 | desc->group_name_prefix, i); |
| 165 | goto err_free; |
| 166 | } |
| 167 | |
| 168 | ctrd_new[i].name = mangle_identifier_ifneeded(desc_new->ctr_desc, ctrd[i].name); |
| 169 | ctrd_new[i].description = ctrd[i].description; |
| 170 | } |
| 171 | |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 172 | if (!rate_ctrl_group_desc_validate(desc_new)) { |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 173 | /* simple mangling of identifiers ('.' -> ':') was not sufficient to render a valid |
| 174 | * descriptor, we have to bail out */ |
| 175 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' still invalid after mangling\n", |
| 176 | desc->group_name_prefix); |
| 177 | goto err_free; |
| 178 | } |
| 179 | |
Harald Welte | ae510dc | 2017-10-03 17:46:14 +0800 | [diff] [blame] | 180 | return desc_new; |
| 181 | err_free: |
| 182 | talloc_free(desc_new); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Neels Hofmeyr | 554f7b8 | 2017-12-20 01:14:31 +0100 | [diff] [blame] | 186 | /*! Find an unused index for this rate counter group. |
| 187 | * \param[in] name Name of the counter group |
| 188 | * \returns the largest used index number + 1, or 0 if none exist yet. */ |
| 189 | static unsigned int rate_ctr_get_unused_name_idx(const char *name) |
| 190 | { |
| 191 | unsigned int idx = 0; |
| 192 | struct rate_ctr_group *ctrg; |
| 193 | |
| 194 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) { |
| 195 | if (!ctrg->desc) |
| 196 | continue; |
| 197 | |
| 198 | if (strcmp(ctrg->desc->group_name_prefix, name)) |
| 199 | continue; |
| 200 | |
| 201 | if (idx <= ctrg->idx) |
| 202 | idx = ctrg->idx + 1; |
| 203 | } |
| 204 | return idx; |
| 205 | } |
| 206 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 207 | /*! Allocate a new group of counters according to description |
Vadim Yanitskiy | ed3a3c6 | 2019-03-26 00:57:45 +0700 | [diff] [blame] | 208 | * \param[in] ctx parent talloc context |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 209 | * \param[in] desc Rate counter group description |
| 210 | * \param[in] idx Index of new counter group |
| 211 | */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 212 | struct rate_ctr_group *rate_ctr_group_alloc(void *ctx, |
| 213 | const struct rate_ctr_group_desc *desc, |
| 214 | unsigned int idx) |
| 215 | { |
| 216 | unsigned int size; |
| 217 | struct rate_ctr_group *group; |
| 218 | |
Harald Welte | d589f1d | 2017-12-18 17:13:38 +0100 | [diff] [blame] | 219 | 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] | 220 | unsigned int new_idx = rate_ctr_get_unused_name_idx(desc->group_name_prefix); |
| 221 | LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' already exists for index %u," |
| 222 | " instead using index %u. This is a software bug that needs fixing.\n", |
| 223 | desc->group_name_prefix, idx, new_idx); |
| 224 | idx = new_idx; |
Harald Welte | d589f1d | 2017-12-18 17:13:38 +0100 | [diff] [blame] | 225 | } |
Max | 3ef14a2 | 2017-12-15 20:19:10 +0100 | [diff] [blame] | 226 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 227 | size = sizeof(struct rate_ctr_group) + |
| 228 | desc->num_ctr * sizeof(struct rate_ctr); |
| 229 | |
| 230 | if (!ctx) |
| 231 | ctx = tall_rate_ctr_ctx; |
| 232 | |
| 233 | group = talloc_zero_size(ctx, size); |
| 234 | if (!group) |
| 235 | return NULL; |
| 236 | |
Neels Hofmeyr | 10ee73a | 2017-11-16 18:31:57 +0100 | [diff] [blame] | 237 | /* attempt to mangle all '.' in identifiers to ':' for backwards compat */ |
Pau Espin Pedrol | dfc52a1 | 2018-06-27 16:53:31 +0200 | [diff] [blame] | 238 | if (!rate_ctrl_group_desc_validate(desc)) { |
Neels Hofmeyr | 10ee73a | 2017-11-16 18:31:57 +0100 | [diff] [blame] | 239 | desc = rate_ctr_group_desc_mangle(group, desc); |
| 240 | if (!desc) { |
| 241 | talloc_free(group); |
| 242 | return NULL; |
| 243 | } |
| 244 | } |
| 245 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 246 | group->desc = desc; |
Harald Welte | 087fcff | 2010-05-13 12:16:17 +0200 | [diff] [blame] | 247 | group->idx = idx; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 248 | |
| 249 | llist_add(&group->list, &rate_ctr_groups); |
| 250 | |
| 251 | return group; |
| 252 | } |
| 253 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 254 | /*! Free the memory for the specified group of counters */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 255 | void rate_ctr_group_free(struct rate_ctr_group *grp) |
| 256 | { |
Max | fb6f43e | 2019-03-18 15:41:26 +0100 | [diff] [blame] | 257 | if (!grp) |
| 258 | return; |
| 259 | |
| 260 | if (!llist_empty(&grp->list)) |
| 261 | llist_del(&grp->list); |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 262 | talloc_free(grp); |
| 263 | } |
| 264 | |
Pau Espin Pedrol | 5fe3de5 | 2021-05-31 13:39:07 +0200 | [diff] [blame] | 265 | /*! Get rate counter from group, identified by index idx |
| 266 | * \param[in] grp Rate counter group |
| 267 | * \param[in] idx Index of the counter to retrieve |
| 268 | * \returns rate counter requested |
| 269 | */ |
| 270 | struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx) |
| 271 | { |
| 272 | return &grp->ctr[idx]; |
| 273 | } |
| 274 | |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 275 | /*! Set a name for the group of counters be used instead of index value |
| 276 | at report time. |
| 277 | * \param[in] grp Rate counter group |
| 278 | * \param[in] name Name identifier to assign to the rate counter group |
| 279 | */ |
| 280 | void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name) |
| 281 | { |
| 282 | osmo_talloc_replace_string(grp, &grp->name, name); |
| 283 | } |
| 284 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 285 | /*! Add a number to the counter */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 286 | void rate_ctr_add(struct rate_ctr *ctr, int inc) |
| 287 | { |
| 288 | ctr->current += inc; |
| 289 | } |
| 290 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 291 | /*! Return the counter difference since the last call to this function */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 292 | int64_t rate_ctr_difference(struct rate_ctr *ctr) |
| 293 | { |
| 294 | int64_t result = ctr->current - ctr->previous; |
| 295 | ctr->previous = ctr->current; |
| 296 | |
| 297 | return result; |
| 298 | } |
| 299 | |
Jacob Erlbeck | ee702cd | 2015-11-10 11:38:25 +0100 | [diff] [blame] | 300 | /* TODO: support update intervals > 1s */ |
| 301 | /* TODO: implement this as a special stats reporter */ |
| 302 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 303 | static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv) |
| 304 | { |
| 305 | /* calculate rate over last interval */ |
| 306 | ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last; |
| 307 | /* save current counter for next interval */ |
| 308 | ctr->intv[intv].last = ctr->current; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 311 | static struct osmo_fd rate_ctr_timer = { .fd = -1 }; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 312 | static uint64_t timer_ticks; |
| 313 | |
| 314 | /* The one-second interval has expired */ |
| 315 | static void rate_ctr_group_intv(struct rate_ctr_group *grp) |
| 316 | { |
| 317 | unsigned int i; |
| 318 | |
| 319 | for (i = 0; i < grp->desc->num_ctr; i++) { |
| 320 | struct rate_ctr *ctr = &grp->ctr[i]; |
| 321 | |
| 322 | interval_expired(ctr, RATE_CTR_INTV_SEC); |
| 323 | if ((timer_ticks % 60) == 0) |
| 324 | interval_expired(ctr, RATE_CTR_INTV_MIN); |
| 325 | if ((timer_ticks % (60*60)) == 0) |
| 326 | interval_expired(ctr, RATE_CTR_INTV_HOUR); |
| 327 | if ((timer_ticks % (24*60*60)) == 0) |
| 328 | interval_expired(ctr, RATE_CTR_INTV_DAY); |
| 329 | } |
| 330 | } |
| 331 | |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 332 | static int rate_ctr_timer_cb(struct osmo_fd *ofd, unsigned int what) |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 333 | { |
| 334 | struct rate_ctr_group *ctrg; |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 335 | uint64_t expire_count; |
| 336 | int rc; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 337 | |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 338 | /* check that the timer has actually expired */ |
| 339 | if (!(what & OSMO_FD_READ)) |
| 340 | return 0; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 341 | |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 342 | /* read from timerfd: number of expirations of periodic timer */ |
| 343 | rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count)); |
| 344 | if (rc < 0 && errno == EAGAIN) |
| 345 | return 0; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 346 | |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 347 | OSMO_ASSERT(rc == sizeof(expire_count)); |
| 348 | |
| 349 | if (expire_count > 1) |
| 350 | LOGP(DLGLOBAL, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n", |
| 351 | expire_count, expire_count - 1); |
| 352 | |
| 353 | do { /* Increment number of ticks before we calculate intervals, |
| 354 | * as a counter value of 0 would already wrap all counters */ |
| 355 | timer_ticks++; |
| 356 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) |
| 357 | rate_ctr_group_intv(ctrg); |
| 358 | } while (--expire_count); |
| 359 | |
| 360 | return 0; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Harald Welte | 53de0d3 | 2017-10-16 14:09:16 +0200 | [diff] [blame] | 363 | /*! Initialize the counter module. Call this once from your application. |
| 364 | * \param[in] tall_ctx Talloc context from which rate_ctr_group will be allocated |
| 365 | * \returns 0 on success; negative on error */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 366 | int rate_ctr_init(void *tall_ctx) |
| 367 | { |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 368 | struct timespec ts_interval = { .tv_sec = 1, .tv_nsec = 0 }; |
| 369 | int rc; |
| 370 | |
Harald Welte | 5eb67c2 | 2021-11-14 20:45:07 +0100 | [diff] [blame] | 371 | /* ignore repeated initialization */ |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 372 | if (osmo_fd_is_registered(&rate_ctr_timer)) |
Harald Welte | 5eb67c2 | 2021-11-14 20:45:07 +0100 | [diff] [blame] | 373 | return 0; |
| 374 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 375 | tall_rate_ctr_ctx = tall_ctx; |
Max | 7c1b6cb | 2022-11-24 17:42:02 +0300 | [diff] [blame] | 376 | |
| 377 | rc = osmo_timerfd_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL); |
| 378 | if (rc < 0) { |
| 379 | LOGP(DLGLOBAL, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n", |
| 380 | rc, rate_ctr_timer.fd); |
| 381 | return rc; |
| 382 | } |
| 383 | |
| 384 | rc = osmo_timerfd_schedule(&rate_ctr_timer, NULL, &ts_interval); |
| 385 | if (rc < 0) { |
| 386 | LOGP(DLGLOBAL, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d)\n", |
| 387 | rc, rate_ctr_timer.fd); |
| 388 | } |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 392 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 393 | /*! Search for counter group based on group name and index |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 394 | * \param[in] name Name of the counter group you're looking for |
| 395 | * \param[in] idx Index inside the counter group |
| 396 | * \returns \ref rate_ctr_group or NULL in case of error */ |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 397 | struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx) |
| 398 | { |
| 399 | struct rate_ctr_group *ctrg; |
| 400 | |
| 401 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) { |
| 402 | if (!ctrg->desc) |
| 403 | continue; |
| 404 | |
| 405 | if (!strcmp(ctrg->desc->group_name_prefix, name) && |
| 406 | ctrg->idx == idx) { |
| 407 | return ctrg; |
| 408 | } |
| 409 | } |
| 410 | return NULL; |
| 411 | } |
| 412 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 413 | /*! Search for counter based on group + name |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 414 | * \param[in] ctrg pointer to \ref rate_ctr_group |
| 415 | * \param[in] name name of counter inside group |
Harald Welte | 53de0d3 | 2017-10-16 14:09:16 +0200 | [diff] [blame] | 416 | * \returns \ref rate_ctr or NULL in case of error |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 417 | */ |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 418 | 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] | 419 | { |
| 420 | int i; |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 421 | const struct rate_ctr_desc *ctr_desc; |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 422 | |
| 423 | if (!ctrg->desc) |
| 424 | return NULL; |
| 425 | |
| 426 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 427 | ctr_desc = &ctrg->desc->ctr_desc[i]; |
| 428 | |
| 429 | if (!strcmp(ctr_desc->name, name)) { |
| 430 | return &ctrg->ctr[i]; |
| 431 | } |
| 432 | } |
| 433 | return NULL; |
| 434 | } |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 435 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 436 | /*! Iterate over each counter in group and call function |
Vadim Yanitskiy | c761044 | 2019-03-26 00:48:30 +0700 | [diff] [blame] | 437 | * \param[in] ctrg counter group over which to iterate |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 438 | * \param[in] handle_counter function pointer |
Vadim Yanitskiy | ed3a3c6 | 2019-03-26 00:57:45 +0700 | [diff] [blame] | 439 | * \param[in] data Data to hand transparently to handle_counter() |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 440 | * \returns 0 on success; negative otherwise |
| 441 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 442 | int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, |
| 443 | rate_ctr_handler_t handle_counter, void *data) |
| 444 | { |
| 445 | int rc = 0; |
| 446 | int i; |
| 447 | |
| 448 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 449 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 450 | rc = handle_counter(ctrg, |
| 451 | ctr, &ctrg->desc->ctr_desc[i], data); |
| 452 | if (rc < 0) |
| 453 | return rc; |
| 454 | } |
| 455 | |
| 456 | return rc; |
| 457 | } |
| 458 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 459 | /*! Iterate over all counter groups |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 460 | * \param[in] handle_group function pointer of callback function |
Vadim Yanitskiy | ed3a3c6 | 2019-03-26 00:57:45 +0700 | [diff] [blame] | 461 | * \param[in] data Data to hand transparently to handle_group() |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 462 | * \returns 0 on success; negative otherwise |
| 463 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 464 | int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data) |
| 465 | { |
| 466 | struct rate_ctr_group *statg; |
| 467 | int rc = 0; |
| 468 | |
| 469 | llist_for_each_entry(statg, &rate_ctr_groups, list) { |
| 470 | rc = handle_group(statg, data); |
| 471 | if (rc < 0) |
| 472 | return rc; |
| 473 | } |
| 474 | |
| 475 | return rc; |
| 476 | } |
| 477 | |
Daniel Willmann | 26a9539 | 2020-07-14 18:04:18 +0200 | [diff] [blame] | 478 | /*! Reset a rate counter back to zero |
| 479 | * \param[in] ctr counter to reset |
| 480 | */ |
| 481 | void rate_ctr_reset(struct rate_ctr *ctr) |
| 482 | { |
| 483 | memset(ctr, 0, sizeof(*ctr)); |
| 484 | } |
| 485 | |
| 486 | /*! Reset all counters in a group |
| 487 | * \param[in] ctrg counter group to reset |
| 488 | */ |
| 489 | void rate_ctr_group_reset(struct rate_ctr_group *ctrg) |
| 490 | { |
| 491 | int i; |
| 492 | |
| 493 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 494 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 495 | rate_ctr_reset(ctr); |
| 496 | } |
| 497 | } |
| 498 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 499 | /*! @} */ |