blob: 6d771a44a66721595ba7f81856eeb58e2586ca02 [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
23#include <stdint.h>
Harald Welte7b45d602010-05-13 11:35:30 +020024#include <string.h>
25
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/utils.h>
27#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/talloc.h>
29#include <osmocom/core/timer.h>
30#include <osmocom/core/rate_ctr.h>
Harald Welte7b45d602010-05-13 11:35:30 +020031
32static LLIST_HEAD(rate_ctr_groups);
33
34static void *tall_rate_ctr_ctx;
35
36struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
37 const struct rate_ctr_group_desc *desc,
38 unsigned int idx)
39{
40 unsigned int size;
41 struct rate_ctr_group *group;
42
43 size = sizeof(struct rate_ctr_group) +
44 desc->num_ctr * sizeof(struct rate_ctr);
45
46 if (!ctx)
47 ctx = tall_rate_ctr_ctx;
48
49 group = talloc_zero_size(ctx, size);
50 if (!group)
51 return NULL;
52
53 group->desc = desc;
Harald Welte087fcff2010-05-13 12:16:17 +020054 group->idx = idx;
Harald Welte7b45d602010-05-13 11:35:30 +020055
56 llist_add(&group->list, &rate_ctr_groups);
57
58 return group;
59}
60
61void rate_ctr_group_free(struct rate_ctr_group *grp)
62{
63 llist_del(&grp->list);
64 talloc_free(grp);
65}
66
67void rate_ctr_add(struct rate_ctr *ctr, int inc)
68{
69 ctr->current += inc;
70}
71
72static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
73{
74 /* calculate rate over last interval */
75 ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last;
76 /* save current counter for next interval */
77 ctr->intv[intv].last = ctr->current;
Harald Welted2dce6d2010-05-13 13:28:12 +020078
79 /* update the rate of the next bigger interval. This will
80 * be overwritten when that next larger interval expires */
81 if (intv + 1 < ARRAY_SIZE(ctr->intv))
82 ctr->intv[intv+1].rate += ctr->intv[intv].rate;
Harald Welte7b45d602010-05-13 11:35:30 +020083}
84
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +020085static struct osmo_timer_list rate_ctr_timer;
Harald Welte7b45d602010-05-13 11:35:30 +020086static uint64_t timer_ticks;
87
88/* The one-second interval has expired */
89static void rate_ctr_group_intv(struct rate_ctr_group *grp)
90{
91 unsigned int i;
92
93 for (i = 0; i < grp->desc->num_ctr; i++) {
94 struct rate_ctr *ctr = &grp->ctr[i];
95
96 interval_expired(ctr, RATE_CTR_INTV_SEC);
97 if ((timer_ticks % 60) == 0)
98 interval_expired(ctr, RATE_CTR_INTV_MIN);
99 if ((timer_ticks % (60*60)) == 0)
100 interval_expired(ctr, RATE_CTR_INTV_HOUR);
101 if ((timer_ticks % (24*60*60)) == 0)
102 interval_expired(ctr, RATE_CTR_INTV_DAY);
103 }
104}
105
106static void rate_ctr_timer_cb(void *data)
107{
108 struct rate_ctr_group *ctrg;
109
110 /* Increment number of ticks before we calculate intervals,
111 * as a counter value of 0 would already wrap all counters */
112 timer_ticks++;
113
114 llist_for_each_entry(ctrg, &rate_ctr_groups, list)
115 rate_ctr_group_intv(ctrg);
116
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200117 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200118}
119
120int rate_ctr_init(void *tall_ctx)
121{
122 tall_rate_ctr_ctx = tall_ctx;
123 rate_ctr_timer.cb = rate_ctr_timer_cb;
Pablo Neira Ayuso0b21c1c2011-05-07 12:42:28 +0200124 osmo_timer_schedule(&rate_ctr_timer, 1, 0);
Harald Welte7b45d602010-05-13 11:35:30 +0200125
126 return 0;
127}
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200128
129struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx)
130{
131 struct rate_ctr_group *ctrg;
132
133 llist_for_each_entry(ctrg, &rate_ctr_groups, list) {
134 if (!ctrg->desc)
135 continue;
136
137 if (!strcmp(ctrg->desc->group_name_prefix, name) &&
138 ctrg->idx == idx) {
139 return ctrg;
140 }
141 }
142 return NULL;
143}
144
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200145const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name)
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200146{
147 int i;
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +0200148 const struct rate_ctr_desc *ctr_desc;
Daniel Willmann2d42dde2011-04-08 10:46:18 +0200149
150 if (!ctrg->desc)
151 return NULL;
152
153 for (i = 0; i < ctrg->desc->num_ctr; i++) {
154 ctr_desc = &ctrg->desc->ctr_desc[i];
155
156 if (!strcmp(ctr_desc->name, name)) {
157 return &ctrg->ctr[i];
158 }
159 }
160 return NULL;
161}