blob: fcd6781eb5b258ebaf8d6d5151b001f6fa1d81a8 [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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010024/*! \addtogroup osmo_stat_item
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020025 * @{
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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010040static LLIST_HEAD(osmo_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 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010050struct osmo_stat_item_group *osmo_stat_item_group_alloc(void *ctx,
51 const struct osmo_stat_item_group_desc *desc,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020052 unsigned int idx)
53{
54 unsigned int group_size;
Harald Welteb32a1942015-11-20 10:22:14 +010055 unsigned long items_size = 0;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020056 unsigned int item_idx;
57 void *items;
58
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010059 struct osmo_stat_item_group *group;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020060
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010061 group_size = sizeof(struct osmo_stat_item_group) +
62 desc->num_items * sizeof(struct osmo_stat_item *);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020063
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;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010077 size = sizeof(struct osmo_stat_item) +
78 sizeof(struct osmo_stat_item_value) *
Jacob Erlbeckb27b3522015-10-12 18:47:09 +020079 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++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010097 struct osmo_stat_item *item = (struct osmo_stat_item *)
Harald Welteb32a1942015-11-20 10:22:14 +010098 ((uint8_t *)items + (unsigned long)group->items[item_idx]);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020099 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;
Jacob Erlbeckac4ed172015-12-08 10:29:16 +0100108 item->values[i].id = OSMO_STAT_ITEM_NOVALUE_ID;
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200109 }
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200110 }
111
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112 llist_add(&group->list, &osmo_stat_item_groups);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200113
114 return group;
115}
116
117/*! \brief Free the memory for the specified group of counters */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118void osmo_stat_item_group_free(struct osmo_stat_item_group *grp)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200119{
120 llist_del(&grp->list);
121 talloc_free(grp);
122}
123
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100124void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200125{
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;
Jacob Erlbeckac4ed172015-12-08 10:29:16 +0100131 if (global_value_id == OSMO_STAT_ITEM_NOVALUE_ID)
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200132 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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100138int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *next_idx,
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200139 int32_t *value)
140{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100141 const struct osmo_stat_item_value *next_value;
142 const struct osmo_stat_item_value *item_value = NULL;
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200143 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 &&
Jacob Erlbeckac4ed172015-12-08 10:29:16 +0100150 next_value->id != OSMO_STAT_ITEM_NOVALUE_ID)
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200151 {
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 Erlbeckfc9533d2015-10-29 00:55:58 +0100176int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200177{
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 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100185int osmo_stat_item_discard_all(int32_t *idx)
Jacob Erlbeckb27b3522015-10-12 18:47:09 +0200186{
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 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100194int osmo_stat_item_init(void *tall_ctx)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200195{
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 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100202struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx(
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200203 const char *name, const unsigned int idx)
204{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100205 struct osmo_stat_item_group *statg;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200206
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100207 llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200208 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 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100219const struct osmo_stat_item *osmo_stat_item_get_by_name(
220 const struct osmo_stat_item_group *statg, const char *name)
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200221{
222 int i;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100223 const struct osmo_stat_item_desc *item_desc;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200224
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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100238int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg,
239 osmo_stat_item_handler_t handle_item, void *data)
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200240{
241 int rc = 0;
242 int i;
243
244 for (i = 0; i < statg->desc->num_items; i++) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100245 struct osmo_stat_item *item = statg->items[i];
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200246 rc = handle_item(statg, item, data);
247 if (rc < 0)
248 return rc;
249 }
250
251 return rc;
252}
253
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100254int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data)
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200255{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100256 struct osmo_stat_item_group *statg;
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200257 int rc = 0;
258
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100259 llist_for_each_entry(statg, &osmo_stat_item_groups, list) {
Jacob Erlbeckc6a71082015-10-19 14:04:38 +0200260 rc = handle_group(statg, data);
261 if (rc < 0)
262 return rc;
263 }
264
265 return rc;
266}
267
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200268/*! @} */