blob: 7b169ea0c7de4e2f9d6d6266e88ce2315872cea4 [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001/* utility routines for keeping conters about events and the event rates */
2
3/* (C) 2015 by Sysmocom s.f.m.c. GmbH
4 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24/*! \addtogroup stat_item
25 * @{
26 */
27
28/*! \file stat_item.c */
29
30
31#include <stdint.h>
32#include <string.h>
33
34#include <osmocom/core/utils.h>
35#include <osmocom/core/linuxlist.h>
36#include <osmocom/core/talloc.h>
37#include <osmocom/core/timer.h>
38#include <osmocom/core/stat_item.h>
39
40static LLIST_HEAD(stat_item_groups);
41
42static void *tall_stat_item_ctx;
43
44/*! \brief Allocate a new group of counters according to description
45 * \param[in] ctx \ref talloc context
46 * \param[in] desc Statistics item group description
47 * \param[in] idx Index of new stat item group
48 */
49struct stat_item_group *stat_item_group_alloc(void *ctx,
50 const struct stat_item_group_desc *desc,
51 unsigned int idx)
52{
53 unsigned int group_size;
54 unsigned int items_size = 0;
55 unsigned int item_idx;
56 void *items;
57
58 struct stat_item_group *group;
59
60 group_size = sizeof(struct stat_item_group) +
61 desc->num_items * sizeof(struct stat_item *);
62
63 if (!ctx)
64 ctx = tall_stat_item_ctx;
65
66 group = talloc_zero_size(ctx, group_size);
67 if (!group)
68 return NULL;
69
70 group->desc = desc;
71 group->idx = idx;
72
73 /* Get combined size of all items */
74 for (item_idx = 0; item_idx < desc->num_items; item_idx++) {
75 unsigned int size;
76 size = sizeof(struct stat_item) +
77 sizeof(int32_t) * desc->item_desc[item_idx].num_values;
78 /* Align to pointer size */
79 size = (size + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
80
81 /* Store offsets into the item array */
82 group->items[item_idx] = (void *)items_size;
83
84 items_size += size;
85 }
86
87 items = talloc_zero_size(group, items_size);
88 if (!items) {
89 talloc_free(group);
90 return NULL;
91 }
92
93 /* Update item pointers */
94 for (item_idx = 0; item_idx < desc->num_items; item_idx++) {
95 struct stat_item *item = (struct stat_item *)
96 ((uint8_t *)items + (int)group->items[item_idx]);
97 unsigned int i;
98
99 group->items[item_idx] = item;
100 item->last_offs = desc->item_desc[item_idx].num_values - 1;
101 item->last_value_index = -1;
102 item->desc = &desc->item_desc[item_idx];
103
104 for (i = 0; i <= item->last_offs; i++)
105 item->values[i] = desc->item_desc[item_idx].default_value;
106 }
107
108 llist_add(&group->list, &stat_item_groups);
109
110 return group;
111}
112
113/*! \brief Free the memory for the specified group of counters */
114void stat_item_group_free(struct stat_item_group *grp)
115{
116 llist_del(&grp->list);
117 talloc_free(grp);
118}
119
120void stat_item_set(struct stat_item *item, int32_t value)
121{
122 item->last_offs += 1;
123 if (item->last_offs >= item->desc->num_values)
124 item->last_offs = 0;
125
126 item->last_value_index += 1;
127
128 item->values[item->last_offs] = value;
129}
130
131int stat_item_get_next(const struct stat_item *item, int32_t *next_idx,
132 int32_t *value)
133{
134 int32_t delta = item->last_value_index + 1 - *next_idx;
135 int n_values = 0;
136 int next_offs;
137
138 if (delta == 0)
139 /* All items have been read */
140 return 0;
141
142 if (delta < 0 || delta > item->desc->num_values) {
143 n_values = delta - item->desc->num_values;
144 delta = item->desc->num_values;
145 }
146
147 next_offs = item->last_offs + 1 - delta;
148 if (next_offs < 0)
149 next_offs += item->desc->num_values;
150
151 *value = item->values[next_offs];
152
153 n_values += 1;
154 delta -= 1;
155 *next_idx = item->last_value_index + 1 - delta;
156
157 return n_values;
158}
159
160/*! \brief Skip all values and update idx accordingly */
161int stat_item_discard(const struct stat_item *item, int32_t *idx)
162{
163 int discarded = item->last_value_index + 1 - *idx;
164 *idx = item->last_value_index + 1;
165
166 return discarded;
167}
168
169
170/*! \brief Initialize the stat item module */
171int stat_item_init(void *tall_ctx)
172{
173 tall_stat_item_ctx = tall_ctx;
174
175 return 0;
176}
177
178/*! \brief Search for item group based on group name and index */
179struct stat_item_group *stat_item_get_group_by_name_idx(
180 const char *name, const unsigned int idx)
181{
182 struct stat_item_group *statg;
183
184 llist_for_each_entry(statg, &stat_item_groups, list) {
185 if (!statg->desc)
186 continue;
187
188 if (!strcmp(statg->desc->group_name_prefix, name) &&
189 statg->idx == idx)
190 return statg;
191 }
192 return NULL;
193}
194
195/*! \brief Search for item group based on group name */
196const struct stat_item *stat_item_get_by_name(
197 const struct stat_item_group *statg, const char *name)
198{
199 int i;
200 const struct stat_item_desc *item_desc;
201
202 if (!statg->desc)
203 return NULL;
204
205 for (i = 0; i < statg->desc->num_items; i++) {
206 item_desc = &statg->desc->item_desc[i];
207
208 if (!strcmp(item_desc->name, name)) {
209 return statg->items[i];
210 }
211 }
212 return NULL;
213}
214
215/*! @} */