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