Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 1 | /* utility routines for keeping conters about events and the event rates */ |
| 2 | |
| 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 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 | * @{ |
| 25 | */ |
| 26 | |
| 27 | /*! \file rate_ctr.c */ |
| 28 | |
| 29 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 30 | #include <stdint.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 31 | #include <string.h> |
| 32 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 33 | #include <osmocom/core/utils.h> |
| 34 | #include <osmocom/core/linuxlist.h> |
| 35 | #include <osmocom/core/talloc.h> |
| 36 | #include <osmocom/core/timer.h> |
| 37 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 38 | |
| 39 | static LLIST_HEAD(rate_ctr_groups); |
| 40 | |
| 41 | static void *tall_rate_ctr_ctx; |
| 42 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 43 | /*! \brief Allocate a new group of counters according to description |
| 44 | * \param[in] ctx \ref talloc context |
| 45 | * \param[in] desc Rate counter group description |
| 46 | * \param[in] idx Index of new counter group |
| 47 | */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 48 | struct rate_ctr_group *rate_ctr_group_alloc(void *ctx, |
| 49 | const struct rate_ctr_group_desc *desc, |
| 50 | unsigned int idx) |
| 51 | { |
| 52 | unsigned int size; |
| 53 | struct rate_ctr_group *group; |
| 54 | |
| 55 | size = sizeof(struct rate_ctr_group) + |
| 56 | desc->num_ctr * sizeof(struct rate_ctr); |
| 57 | |
| 58 | if (!ctx) |
| 59 | ctx = tall_rate_ctr_ctx; |
| 60 | |
| 61 | group = talloc_zero_size(ctx, size); |
| 62 | if (!group) |
| 63 | return NULL; |
| 64 | |
| 65 | group->desc = desc; |
Harald Welte | 087fcff | 2010-05-13 12:16:17 +0200 | [diff] [blame] | 66 | group->idx = idx; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 67 | |
| 68 | llist_add(&group->list, &rate_ctr_groups); |
| 69 | |
| 70 | return group; |
| 71 | } |
| 72 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 73 | /*! \brief Free the memory for the specified group of counters */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 74 | void rate_ctr_group_free(struct rate_ctr_group *grp) |
| 75 | { |
| 76 | llist_del(&grp->list); |
| 77 | talloc_free(grp); |
| 78 | } |
| 79 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 80 | /*! \brief Add a number to the counter */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 81 | void rate_ctr_add(struct rate_ctr *ctr, int inc) |
| 82 | { |
| 83 | ctr->current += inc; |
| 84 | } |
| 85 | |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 86 | /*! \brief Return the counter difference since the last call to this function */ |
| 87 | int64_t rate_ctr_difference(struct rate_ctr *ctr) |
| 88 | { |
| 89 | int64_t result = ctr->current - ctr->previous; |
| 90 | ctr->previous = ctr->current; |
| 91 | |
| 92 | return result; |
| 93 | } |
| 94 | |
Jacob Erlbeck | ee702cd | 2015-11-10 11:38:25 +0100 | [diff] [blame] | 95 | /* TODO: support update intervals > 1s */ |
| 96 | /* TODO: implement this as a special stats reporter */ |
| 97 | |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 98 | static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv) |
| 99 | { |
| 100 | /* calculate rate over last interval */ |
| 101 | ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last; |
| 102 | /* save current counter for next interval */ |
| 103 | ctr->intv[intv].last = ctr->current; |
Harald Welte | d2dce6d | 2010-05-13 13:28:12 +0200 | [diff] [blame] | 104 | |
| 105 | /* update the rate of the next bigger interval. This will |
| 106 | * be overwritten when that next larger interval expires */ |
| 107 | if (intv + 1 < ARRAY_SIZE(ctr->intv)) |
| 108 | ctr->intv[intv+1].rate += ctr->intv[intv].rate; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 111 | static struct osmo_timer_list rate_ctr_timer; |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 112 | static uint64_t timer_ticks; |
| 113 | |
| 114 | /* The one-second interval has expired */ |
| 115 | static void rate_ctr_group_intv(struct rate_ctr_group *grp) |
| 116 | { |
| 117 | unsigned int i; |
| 118 | |
| 119 | for (i = 0; i < grp->desc->num_ctr; i++) { |
| 120 | struct rate_ctr *ctr = &grp->ctr[i]; |
| 121 | |
| 122 | interval_expired(ctr, RATE_CTR_INTV_SEC); |
| 123 | if ((timer_ticks % 60) == 0) |
| 124 | interval_expired(ctr, RATE_CTR_INTV_MIN); |
| 125 | if ((timer_ticks % (60*60)) == 0) |
| 126 | interval_expired(ctr, RATE_CTR_INTV_HOUR); |
| 127 | if ((timer_ticks % (24*60*60)) == 0) |
| 128 | interval_expired(ctr, RATE_CTR_INTV_DAY); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void rate_ctr_timer_cb(void *data) |
| 133 | { |
| 134 | struct rate_ctr_group *ctrg; |
| 135 | |
| 136 | /* Increment number of ticks before we calculate intervals, |
| 137 | * as a counter value of 0 would already wrap all counters */ |
| 138 | timer_ticks++; |
| 139 | |
| 140 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) |
| 141 | rate_ctr_group_intv(ctrg); |
| 142 | |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 143 | osmo_timer_schedule(&rate_ctr_timer, 1, 0); |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 146 | /*! \brief Initialize the counter module */ |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 147 | int rate_ctr_init(void *tall_ctx) |
| 148 | { |
| 149 | tall_rate_ctr_ctx = tall_ctx; |
| 150 | rate_ctr_timer.cb = rate_ctr_timer_cb; |
Pablo Neira Ayuso | 0b21c1c | 2011-05-07 12:42:28 +0200 | [diff] [blame] | 151 | osmo_timer_schedule(&rate_ctr_timer, 1, 0); |
Harald Welte | 7b45d60 | 2010-05-13 11:35:30 +0200 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 155 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 156 | /*! \brief Search for counter group based on group name and index |
| 157 | * \param[in] name Name of the counter group you're looking for |
| 158 | * \param[in] idx Index inside the counter group |
| 159 | * \returns \ref rate_ctr_group or NULL in case of error */ |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 160 | struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx) |
| 161 | { |
| 162 | struct rate_ctr_group *ctrg; |
| 163 | |
| 164 | llist_for_each_entry(ctrg, &rate_ctr_groups, list) { |
| 165 | if (!ctrg->desc) |
| 166 | continue; |
| 167 | |
| 168 | if (!strcmp(ctrg->desc->group_name_prefix, name) && |
| 169 | ctrg->idx == idx) { |
| 170 | return ctrg; |
| 171 | } |
| 172 | } |
| 173 | return NULL; |
| 174 | } |
| 175 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 176 | /*! \brief Search for counter based on group + name |
| 177 | * \param[in] ctrg pointer to \ref rate_ctr_group |
| 178 | * \param[in] name name of counter inside group |
| 179 | * \returns \ref rate_ctr or NULL in caes of error |
| 180 | */ |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 181 | 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] | 182 | { |
| 183 | int i; |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 184 | const struct rate_ctr_desc *ctr_desc; |
Daniel Willmann | 2d42dde | 2011-04-08 10:46:18 +0200 | [diff] [blame] | 185 | |
| 186 | if (!ctrg->desc) |
| 187 | return NULL; |
| 188 | |
| 189 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 190 | ctr_desc = &ctrg->desc->ctr_desc[i]; |
| 191 | |
| 192 | if (!strcmp(ctr_desc->name, name)) { |
| 193 | return &ctrg->ctr[i]; |
| 194 | } |
| 195 | } |
| 196 | return NULL; |
| 197 | } |
Harald Welte | 9327c6d | 2011-08-17 16:06:06 +0200 | [diff] [blame] | 198 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 199 | /*! \brief Iterate over each counter in group and call function |
| 200 | * \param[in] counter group over whose counter to iterate |
| 201 | * \param[in] handle_counter function pointer |
| 202 | * \param[in] data Data to hand transparently to \ref handle_counter |
| 203 | * \returns 0 on success; negative otherwise |
| 204 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 205 | int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, |
| 206 | rate_ctr_handler_t handle_counter, void *data) |
| 207 | { |
| 208 | int rc = 0; |
| 209 | int i; |
| 210 | |
| 211 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 212 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 213 | rc = handle_counter(ctrg, |
| 214 | ctr, &ctrg->desc->ctr_desc[i], data); |
| 215 | if (rc < 0) |
| 216 | return rc; |
| 217 | } |
| 218 | |
| 219 | return rc; |
| 220 | } |
| 221 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 222 | /*! \brief Iterate over all counter groups |
| 223 | * \param[in] handle_group function pointer of callback function |
| 224 | * \param[in] data Data to hand transparently to \ref handle_group |
| 225 | * \returns 0 on success; negative otherwise |
| 226 | */ |
Jacob Erlbeck | 423c1e5 | 2015-10-19 13:45:42 +0200 | [diff] [blame] | 227 | int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data) |
| 228 | { |
| 229 | struct rate_ctr_group *statg; |
| 230 | int rc = 0; |
| 231 | |
| 232 | llist_for_each_entry(statg, &rate_ctr_groups, list) { |
| 233 | rc = handle_group(statg, data); |
| 234 | if (rc < 0) |
| 235 | return rc; |
| 236 | } |
| 237 | |
| 238 | return rc; |
| 239 | } |
| 240 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 241 | /*! @} */ |