blob: 9b2459e8901e04e838cc688a3db0ca4af0e9d75f [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
27#include <osmocore/linuxlist.h>
28#include <osmocore/talloc.h>
29#include <osmocore/timer.h>
30#include <osmocore/rate_ctr.h>
31
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;
54 /* Generate the Group prefix from the user-specified index */
55 group->name_prefix = talloc_size(group, strlen(desc->group_prefix_fmt) + 20);
56 sprintf(group->name_prefix, desc->group_prefix_fmt, idx);
57
58 llist_add(&group->list, &rate_ctr_groups);
59
60 return group;
61}
62
63void rate_ctr_group_free(struct rate_ctr_group *grp)
64{
65 llist_del(&grp->list);
66 talloc_free(grp);
67}
68
69void rate_ctr_add(struct rate_ctr *ctr, int inc)
70{
71 ctr->current += inc;
72}
73
74static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
75{
76 /* calculate rate over last interval */
77 ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last;
78 /* save current counter for next interval */
79 ctr->intv[intv].last = ctr->current;
80}
81
82static struct timer_list rate_ctr_timer;
83static uint64_t timer_ticks;
84
85/* The one-second interval has expired */
86static void rate_ctr_group_intv(struct rate_ctr_group *grp)
87{
88 unsigned int i;
89
90 for (i = 0; i < grp->desc->num_ctr; i++) {
91 struct rate_ctr *ctr = &grp->ctr[i];
92
93 interval_expired(ctr, RATE_CTR_INTV_SEC);
94 if ((timer_ticks % 60) == 0)
95 interval_expired(ctr, RATE_CTR_INTV_MIN);
96 if ((timer_ticks % (60*60)) == 0)
97 interval_expired(ctr, RATE_CTR_INTV_HOUR);
98 if ((timer_ticks % (24*60*60)) == 0)
99 interval_expired(ctr, RATE_CTR_INTV_DAY);
100 }
101}
102
103static void rate_ctr_timer_cb(void *data)
104{
105 struct rate_ctr_group *ctrg;
106
107 /* Increment number of ticks before we calculate intervals,
108 * as a counter value of 0 would already wrap all counters */
109 timer_ticks++;
110
111 llist_for_each_entry(ctrg, &rate_ctr_groups, list)
112 rate_ctr_group_intv(ctrg);
113
114 bsc_schedule_timer(&rate_ctr_timer, 1, 0);
115}
116
117int rate_ctr_init(void *tall_ctx)
118{
119 tall_rate_ctr_ctx = tall_ctx;
120 rate_ctr_timer.cb = rate_ctr_timer_cb;
121 bsc_schedule_timer(&rate_ctr_timer, 1, 0);
122
123 return 0;
124}