blob: 1e283d4811d59dc8d5f59ca7b36a5f67271003b9 [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);
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020041static int32_t global_value_id = 0;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020042
43static void *tall_stat_item_ctx;
44
45/*! \brief Allocate a new group of counters according to description
46 * \param[in] ctx \ref talloc context
47 * \param[in] desc Statistics item group description
48 * \param[in] idx Index of new stat item group
49 */
50struct stat_item_group *stat_item_group_alloc(void *ctx,
51 const struct stat_item_group_desc *desc,
52 unsigned int idx)
53{
54 unsigned int group_size;
55 unsigned int items_size = 0;
56 unsigned int item_idx;
57 void *items;
58
59 struct stat_item_group *group;
60
61 group_size = sizeof(struct stat_item_group) +
62 desc->num_items * sizeof(struct stat_item *);
63
64 if (!ctx)
65 ctx = tall_stat_item_ctx;
66
67 group = talloc_zero_size(ctx, group_size);
68 if (!group)
69 return NULL;
70
71 group->desc = desc;
72 group->idx = idx;
73
74 /* Get combined size of all items */
75 for (item_idx = 0; item_idx < desc->num_items; item_idx++) {
76 unsigned int size;
77 size = sizeof(struct stat_item) +
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020078 sizeof(struct stat_item_value) *
79 desc->item_desc[item_idx].num_values;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020080 /* Align to pointer size */
81 size = (size + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
82
83 /* Store offsets into the item array */
84 group->items[item_idx] = (void *)items_size;
85
86 items_size += size;
87 }
88
89 items = talloc_zero_size(group, items_size);
90 if (!items) {
91 talloc_free(group);
92 return NULL;
93 }
94
95 /* Update item pointers */
96 for (item_idx = 0; item_idx < desc->num_items; item_idx++) {
97 struct stat_item *item = (struct stat_item *)
98 ((uint8_t *)items + (int)group->items[item_idx]);
99 unsigned int i;
100
101 group->items[item_idx] = item;
102 item->last_offs = desc->item_desc[item_idx].num_values - 1;
103 item->last_value_index = -1;
104 item->desc = &desc->item_desc[item_idx];
105
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200106 for (i = 0; i <= item->last_offs; i++) {
107 item->values[i].value = desc->item_desc[item_idx].default_value;
108 item->values[i].id = STAT_ITEM_NOVALUE_ID;
109 }
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200110 }
111
112 llist_add(&group->list, &stat_item_groups);
113
114 return group;
115}
116
117/*! \brief Free the memory for the specified group of counters */
118void stat_item_group_free(struct stat_item_group *grp)
119{
120 llist_del(&grp->list);
121 talloc_free(grp);
122}
123
124void stat_item_set(struct stat_item *item, int32_t value)
125{
126 item->last_offs += 1;
127 if (item->last_offs >= item->desc->num_values)
128 item->last_offs = 0;
129
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200130 global_value_id += 1;
131 if (global_value_id == STAT_ITEM_NOVALUE_ID)
132 global_value_id += 1;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200133
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200134 item->values[item->last_offs].value = value;
135 item->values[item->last_offs].id = global_value_id;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200136}
137
138int stat_item_get_next(const struct stat_item *item, int32_t *next_idx,
139 int32_t *value)
140{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200141 const struct stat_item_value *next_value;
142 const struct stat_item_value *item_value = NULL;
143 int idx_delta;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200144 int next_offs;
145
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200146 next_offs = item->last_offs;
147 next_value = &item->values[next_offs];
148
149 while (next_value->id - *next_idx >= 0 &&
150 next_value->id != STAT_ITEM_NOVALUE_ID)
151 {
152 item_value = next_value;
153
154 next_offs -= 1;
155 if (next_offs < 0)
156 next_offs = item->desc->num_values - 1;
157 if (next_offs == item->last_offs)
158 break;
159 next_value = &item->values[next_offs];
160 }
161
162 if (!item_value)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200163 /* All items have been read */
164 return 0;
165
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200166 *value = item_value->value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200167
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200168 idx_delta = item_value->id + 1 - *next_idx;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200169
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200170 *next_idx = item_value->id + 1;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200171
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200172 return idx_delta;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200173}
174
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200175/*! \brief Skip all values of this item and update idx accordingly */
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200176int stat_item_discard(const struct stat_item *item, int32_t *idx)
177{
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200178 int discarded = item->values[item->last_offs].id + 1 - *idx;
179 *idx = item->values[item->last_offs].id + 1;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200180
181 return discarded;
182}
183
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200184/*! \brief Skip all values of all items and update idx accordingly */
185int stat_item_discard_all(int32_t *idx)
186{
187 int discarded = global_value_id + 1 - *idx;
188 *idx = global_value_id + 1;
189
190 return discarded;
191}
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200192
193/*! \brief Initialize the stat item module */
194int stat_item_init(void *tall_ctx)
195{
196 tall_stat_item_ctx = tall_ctx;
197
198 return 0;
199}
200
201/*! \brief Search for item group based on group name and index */
202struct stat_item_group *stat_item_get_group_by_name_idx(
203 const char *name, const unsigned int idx)
204{
205 struct stat_item_group *statg;
206
207 llist_for_each_entry(statg, &stat_item_groups, list) {
208 if (!statg->desc)
209 continue;
210
211 if (!strcmp(statg->desc->group_name_prefix, name) &&
212 statg->idx == idx)
213 return statg;
214 }
215 return NULL;
216}
217
218/*! \brief Search for item group based on group name */
219const struct stat_item *stat_item_get_by_name(
220 const struct stat_item_group *statg, const char *name)
221{
222 int i;
223 const struct stat_item_desc *item_desc;
224
225 if (!statg->desc)
226 return NULL;
227
228 for (i = 0; i < statg->desc->num_items; i++) {
229 item_desc = &statg->desc->item_desc[i];
230
231 if (!strcmp(item_desc->name, name)) {
232 return statg->items[i];
233 }
234 }
235 return NULL;
236}
237
238/*! @} */