blob: f58b5c4a1a9f66310234ea3ce60b0c3d8bd252c1 [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>
24#include <inttypes.h>
25#include <string.h>
26
Harald Welted2dce6d2010-05-13 13:28:12 +020027#include <osmocore/utils.h>
Harald Welte7b45d602010-05-13 11:35:30 +020028#include <osmocore/linuxlist.h>
29#include <osmocore/talloc.h>
30#include <osmocore/timer.h>
31#include <osmocore/rate_ctr.h>
32
33static LLIST_HEAD(rate_ctr_groups);
34
35static void *tall_rate_ctr_ctx;
36
37struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
38 const struct rate_ctr_group_desc *desc,
39 unsigned int idx)
40{
41 unsigned int size;
42 struct rate_ctr_group *group;
43
44 size = sizeof(struct rate_ctr_group) +
45 desc->num_ctr * sizeof(struct rate_ctr);
46
47 if (!ctx)
48 ctx = tall_rate_ctr_ctx;
49
50 group = talloc_zero_size(ctx, size);
51 if (!group)
52 return NULL;
53
54 group->desc = desc;
Harald Welte087fcff2010-05-13 12:16:17 +020055 group->idx = idx;
Harald Welte7b45d602010-05-13 11:35:30 +020056
57 llist_add(&group->list, &rate_ctr_groups);
58
59 return group;
60}
61
62void rate_ctr_group_free(struct rate_ctr_group *grp)
63{
64 llist_del(&grp->list);
65 talloc_free(grp);
66}
67
68void rate_ctr_add(struct rate_ctr *ctr, int inc)
69{
70 ctr->current += inc;
71}
72
73static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
74{
75 /* calculate rate over last interval */
76 ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last;
77 /* save current counter for next interval */
78 ctr->intv[intv].last = ctr->current;
Harald Welted2dce6d2010-05-13 13:28:12 +020079
80 /* update the rate of the next bigger interval. This will
81 * be overwritten when that next larger interval expires */
82 if (intv + 1 < ARRAY_SIZE(ctr->intv))
83 ctr->intv[intv+1].rate += ctr->intv[intv].rate;
Harald Welte7b45d602010-05-13 11:35:30 +020084}
85
86static struct timer_list rate_ctr_timer;
87static uint64_t timer_ticks;
88
89/* The one-second interval has expired */
90static void rate_ctr_group_intv(struct rate_ctr_group *grp)
91{
92 unsigned int i;
93
94 for (i = 0; i < grp->desc->num_ctr; i++) {
95 struct rate_ctr *ctr = &grp->ctr[i];
96
97 interval_expired(ctr, RATE_CTR_INTV_SEC);
98 if ((timer_ticks % 60) == 0)
99 interval_expired(ctr, RATE_CTR_INTV_MIN);
100 if ((timer_ticks % (60*60)) == 0)
101 interval_expired(ctr, RATE_CTR_INTV_HOUR);
102 if ((timer_ticks % (24*60*60)) == 0)
103 interval_expired(ctr, RATE_CTR_INTV_DAY);
104 }
105}
106
107static void rate_ctr_timer_cb(void *data)
108{
109 struct rate_ctr_group *ctrg;
110
111 /* Increment number of ticks before we calculate intervals,
112 * as a counter value of 0 would already wrap all counters */
113 timer_ticks++;
114
115 llist_for_each_entry(ctrg, &rate_ctr_groups, list)
116 rate_ctr_group_intv(ctrg);
117
118 bsc_schedule_timer(&rate_ctr_timer, 1, 0);
119}
120
121int rate_ctr_init(void *tall_ctx)
122{
123 tall_rate_ctr_ctx = tall_ctx;
124 rate_ctr_timer.cb = rate_ctr_timer_cb;
125 bsc_schedule_timer(&rate_ctr_timer, 1, 0);
126
127 return 0;
128}