blob: 50b3fe74cc8531782fc4828c4812dde06c97f7b9 [file] [log] [blame]
Harald Welte7b45d602010-05-13 11:35:30 +02001/* 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 Welte9327c6d2011-08-17 16:06:06 +020023/*! \addtogroup rate_ctr
24 * @{
25 */
26
27/*! \file rate_ctr.c */
28
29
Harald Welte7b45d602010-05-13 11:35:30 +020030#include <stdint.h>
Harald Welte7b45d602010-05-13 11:35:30 +020031#include <string.h>
32
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#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 Welte7b45d602010-05-13 11:35:30 +020038
39static LLIST_HEAD(rate_ctr_groups);
40
41static void *tall_rate_ctr_ctx;
42
Harald Welte9327c6d2011-08-17 16:06:06 +020043/*! \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 Welte7b45d602010-05-13 11:35:30 +020048struct 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 Welte087fcff2010-05-13 12:16:17 +020066 group->idx = idx;
Harald Welte7b45d602010-05-13 11:35:30 +020067
68 llist_add(&group->list, &rate_ctr_groups);
69
70 return group;
71}
72
Harald Welte9327c6d2011-08-17 16:06:06 +020073/*! \brief Free the memory for the specified group of counters */
Harald Welte7b45d602010-05-13 11:35:30 +020074void rate_ctr_group_free(struct rate_ctr_group *grp)
75{
76 llist_del(&grp->list);
77 talloc_free(grp);
78}
79
Harald Welte9327c6d2011-08-17 16:06:06 +020080/*! \brief Add a number to the counter */
Harald Welte7b45d602010-05-13 11:35:30 +020081void rate_ctr_add(struct rate_ctr *ctr, int inc)
82{
83 ctr->current += inc;
84}
85
Jacob Erlbeck423c1e52015-10-19 13:45:42 +020086/*! \brief Return the counter difference since the last call to this function */
87int64_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
Harald Welte7b45d602010-05-13 11:35:30 +020095static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
96{
97 /* calculate rate over last interval */
98 ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last;
99 /* save current counter for next interval */
100 ctr->intv[intv].last = ctr->current;
Harald Welted2dce6d2010-05-13 13:28:12 +0200101
102 /* update the rate of the next bigger interval. This will
103 * be overwritten when that next larger interval expires */
104 if (intv + 1 < ARRAY_SIZE(ctr->intv))
105 ctr->intv[intv+1].rate += ctr->intv[intv].rate;
Harald Welte7b45d602010-05-13 11:35:30 +0200106}
107
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200108static struct osmo_timer_list rate_ctr_timer;
Harald Welte7b45d602010-05-13 11:35:30 +0200109static uint64_t timer_ticks;
110
111/* The one-second interval has expired */
112static void rate_ctr_group_intv(struct rate_ctr_group *grp)
113{
114 unsigned int i;
115
116 for (i = 0; i < grp->desc->num_ctr; i++) {
117 struct rate_ctr *ctr = &grp->ctr[i];
118
119 interval_expired(ctr, RATE_CTR_INTV_SEC);
120 if ((timer_ticks % 60) == 0)
121 interval_expired(ctr, RATE_CTR_INTV_MIN);
122 if ((timer_ticks % (60*60)) == 0)
123 interval_expired(ctr, RATE_CTR_INTV_HOUR);
124 if ((timer_ticks % (24*60*60)) == 0)
125 interval_expired(ctr, RATE_CTR_INTV_DAY);
126 }
127}
128
129static void rate_ctr_timer_cb(void *data)
130{
131 struct rate_ctr_group *ctrg;
132
133 /* Increment number of ticks before we calculate intervals,
134 * as a counter value of 0 would already wrap all counters */
135 timer_ticks++;
136
137 llist_for_each_entry(ctrg, &rate_ctr_groups, list)
138 rate_ctr_group_intv(ctrg);
139
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200140 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200141}
142
Harald Welte9327c6d2011-08-17 16:06:06 +0200143/*! \brief Initialize the counter module */
Harald Welte7b45d602010-05-13 11:35:30 +0200144int rate_ctr_init(void *tall_ctx)
145{
146 tall_rate_ctr_ctx = tall_ctx;
147 rate_ctr_timer.cb = rate_ctr_timer_cb;
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200148 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200149
150 return 0;
151}
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200152
Harald Welte9327c6d2011-08-17 16:06:06 +0200153/*! \brief Search for counter group based on group name and index */
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200154struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx)
155{
156 struct rate_ctr_group *ctrg;
157
158 llist_for_each_entry(ctrg, &rate_ctr_groups, list) {
159 if (!ctrg->desc)
160 continue;
161
162 if (!strcmp(ctrg->desc->group_name_prefix, name) &&
163 ctrg->idx == idx) {
164 return ctrg;
165 }
166 }
167 return NULL;
168}
169
Harald Welte9327c6d2011-08-17 16:06:06 +0200170/*! \brief Search for counter group based on group name */
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200171const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name)
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200172{
173 int i;
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200174 const struct rate_ctr_desc *ctr_desc;
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200175
176 if (!ctrg->desc)
177 return NULL;
178
179 for (i = 0; i < ctrg->desc->num_ctr; i++) {
180 ctr_desc = &ctrg->desc->ctr_desc[i];
181
182 if (!strcmp(ctr_desc->name, name)) {
183 return &ctrg->ctr[i];
184 }
185 }
186 return NULL;
187}
Harald Welte9327c6d2011-08-17 16:06:06 +0200188
Jacob Erlbeck423c1e52015-10-19 13:45:42 +0200189int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
190 rate_ctr_handler_t handle_counter, void *data)
191{
192 int rc = 0;
193 int i;
194
195 for (i = 0; i < ctrg->desc->num_ctr; i++) {
196 struct rate_ctr *ctr = &ctrg->ctr[i];
197 rc = handle_counter(ctrg,
198 ctr, &ctrg->desc->ctr_desc[i], data);
199 if (rc < 0)
200 return rc;
201 }
202
203 return rc;
204}
205
206int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data)
207{
208 struct rate_ctr_group *statg;
209 int rc = 0;
210
211 llist_for_each_entry(statg, &rate_ctr_groups, list) {
212 rc = handle_group(statg, data);
213 if (rc < 0)
214 return rc;
215 }
216
217 return rc;
218}
219
220
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200221/*! @} */