1 /* utility routines for keeping conters about events and the event rates */
3 /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
23 /*! \addtogroup rate_ctr
27 /*! \file rate_ctr.c */
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>
39 static LLIST_HEAD(rate_ctr_groups
);
41 static void *tall_rate_ctr_ctx
;
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
48 struct rate_ctr_group
*rate_ctr_group_alloc(void *ctx
,
49 const struct rate_ctr_group_desc
*desc
,
53 struct rate_ctr_group
*group
;
55 size
= sizeof(struct rate_ctr_group
) +
56 desc
->num_ctr
* sizeof(struct rate_ctr
);
59 ctx
= tall_rate_ctr_ctx
;
61 group
= talloc_zero_size(ctx
, size
);
68 llist_add(&group
->list
, &rate_ctr_groups
);
73 /*! \brief Free the memory for the specified group of counters */
74 void rate_ctr_group_free(struct rate_ctr_group
*grp
)
76 llist_del(&grp
->list
);
80 /*! \brief Add a number to the counter */
81 void rate_ctr_add(struct rate_ctr
*ctr
, int inc
)
86 static void interval_expired(struct rate_ctr
*ctr
, enum rate_ctr_intv intv
)
88 /* calculate rate over last interval */
89 ctr
->intv
[intv
].rate
= ctr
->current
- ctr
->intv
[intv
].last
;
90 /* save current counter for next interval */
91 ctr
->intv
[intv
].last
= ctr
->current
;
93 /* update the rate of the next bigger interval. This will
94 * be overwritten when that next larger interval expires */
95 if (intv
+ 1 < ARRAY_SIZE(ctr
->intv
))
96 ctr
->intv
[intv
+1].rate
+= ctr
->intv
[intv
].rate
;
99 static struct osmo_timer_list rate_ctr_timer
;
100 static uint64_t timer_ticks
;
102 /* The one-second interval has expired */
103 static void rate_ctr_group_intv(struct rate_ctr_group
*grp
)
107 for (i
= 0; i
< grp
->desc
->num_ctr
; i
++) {
108 struct rate_ctr
*ctr
= &grp
->ctr
[i
];
110 interval_expired(ctr
, RATE_CTR_INTV_SEC
);
111 if ((timer_ticks
% 60) == 0)
112 interval_expired(ctr
, RATE_CTR_INTV_MIN
);
113 if ((timer_ticks
% (60*60)) == 0)
114 interval_expired(ctr
, RATE_CTR_INTV_HOUR
);
115 if ((timer_ticks
% (24*60*60)) == 0)
116 interval_expired(ctr
, RATE_CTR_INTV_DAY
);
120 static void rate_ctr_timer_cb(void *data
)
122 struct rate_ctr_group
*ctrg
;
124 /* Increment number of ticks before we calculate intervals,
125 * as a counter value of 0 would already wrap all counters */
128 llist_for_each_entry(ctrg
, &rate_ctr_groups
, list
)
129 rate_ctr_group_intv(ctrg
);
131 osmo_timer_schedule(&rate_ctr_timer
, 1, 0);
134 /*! \brief Initialize the counter module */
135 int rate_ctr_init(void *tall_ctx
)
137 tall_rate_ctr_ctx
= tall_ctx
;
138 rate_ctr_timer
.cb
= rate_ctr_timer_cb
;
139 osmo_timer_schedule(&rate_ctr_timer
, 1, 0);
144 /*! \brief Search for counter group based on group name and index */
145 struct rate_ctr_group
*rate_ctr_get_group_by_name_idx(const char *name
, const unsigned int idx
)
147 struct rate_ctr_group
*ctrg
;
149 llist_for_each_entry(ctrg
, &rate_ctr_groups
, list
) {
153 if (!strcmp(ctrg
->desc
->group_name_prefix
, name
) &&
161 /*! \brief Search for counter group based on group name */
162 const struct rate_ctr
*rate_ctr_get_by_name(const struct rate_ctr_group
*ctrg
, const char *name
)
165 const struct rate_ctr_desc
*ctr_desc
;
170 for (i
= 0; i
< ctrg
->desc
->num_ctr
; i
++) {
171 ctr_desc
= &ctrg
->desc
->ctr_desc
[i
];
173 if (!strcmp(ctr_desc
->name
, name
)) {
174 return &ctrg
->ctr
[i
];