Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 1 | /* 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 24 | /*! \addtogroup osmo_stat_item |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 40 | static LLIST_HEAD(osmo_stat_item_groups); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 41 | static int32_t global_value_id = 0; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 42 | |
| 43 | static 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 50 | struct osmo_stat_item_group *osmo_stat_item_group_alloc(void *ctx, |
| 51 | const struct osmo_stat_item_group_desc *desc, |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 52 | unsigned int idx) |
| 53 | { |
| 54 | unsigned int group_size; |
Harald Welte | b32a194 | 2015-11-20 10:22:14 +0100 | [diff] [blame] | 55 | unsigned long items_size = 0; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 56 | unsigned int item_idx; |
| 57 | void *items; |
| 58 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 59 | struct osmo_stat_item_group *group; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 60 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 61 | group_size = sizeof(struct osmo_stat_item_group) + |
| 62 | desc->num_items * sizeof(struct osmo_stat_item *); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 77 | size = sizeof(struct osmo_stat_item) + |
| 78 | sizeof(struct osmo_stat_item_value) * |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 79 | desc->item_desc[item_idx].num_values; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 80 | /* 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 97 | struct osmo_stat_item *item = (struct osmo_stat_item *) |
Harald Welte | b32a194 | 2015-11-20 10:22:14 +0100 | [diff] [blame] | 98 | ((uint8_t *)items + (unsigned long)group->items[item_idx]); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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 Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 106 | for (i = 0; i <= item->last_offs; i++) { |
| 107 | item->values[i].value = desc->item_desc[item_idx].default_value; |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 108 | item->values[i].id = OSMO_STAT_ITEM_NOVALUE_ID; |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 109 | } |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 112 | llist_add(&group->list, &osmo_stat_item_groups); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 113 | |
| 114 | return group; |
| 115 | } |
| 116 | |
| 117 | /*! \brief Free the memory for the specified group of counters */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 118 | void osmo_stat_item_group_free(struct osmo_stat_item_group *grp) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 119 | { |
| 120 | llist_del(&grp->list); |
| 121 | talloc_free(grp); |
| 122 | } |
| 123 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 124 | void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 125 | { |
| 126 | item->last_offs += 1; |
| 127 | if (item->last_offs >= item->desc->num_values) |
| 128 | item->last_offs = 0; |
| 129 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 130 | global_value_id += 1; |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 131 | if (global_value_id == OSMO_STAT_ITEM_NOVALUE_ID) |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 132 | global_value_id += 1; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 133 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 134 | item->values[item->last_offs].value = value; |
| 135 | item->values[item->last_offs].id = global_value_id; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 138 | int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *next_idx, |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 139 | int32_t *value) |
| 140 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 141 | const struct osmo_stat_item_value *next_value; |
| 142 | const struct osmo_stat_item_value *item_value = NULL; |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 143 | int idx_delta; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 144 | int next_offs; |
| 145 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 146 | next_offs = item->last_offs; |
| 147 | next_value = &item->values[next_offs]; |
| 148 | |
| 149 | while (next_value->id - *next_idx >= 0 && |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 150 | next_value->id != OSMO_STAT_ITEM_NOVALUE_ID) |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 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 Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 163 | /* All items have been read */ |
| 164 | return 0; |
| 165 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 166 | *value = item_value->value; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 167 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 168 | idx_delta = item_value->id + 1 - *next_idx; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 169 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 170 | *next_idx = item_value->id + 1; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 171 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 172 | return idx_delta; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 175 | /*! \brief Skip all values of this item and update idx accordingly */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 176 | int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 177 | { |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 178 | int discarded = item->values[item->last_offs].id + 1 - *idx; |
| 179 | *idx = item->values[item->last_offs].id + 1; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 180 | |
| 181 | return discarded; |
| 182 | } |
| 183 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 184 | /*! \brief Skip all values of all items and update idx accordingly */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 185 | int osmo_stat_item_discard_all(int32_t *idx) |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 186 | { |
| 187 | int discarded = global_value_id + 1 - *idx; |
| 188 | *idx = global_value_id + 1; |
| 189 | |
| 190 | return discarded; |
| 191 | } |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 192 | |
| 193 | /*! \brief Initialize the stat item module */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 194 | int osmo_stat_item_init(void *tall_ctx) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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 */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 202 | struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx( |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 203 | const char *name, const unsigned int idx) |
| 204 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 205 | struct osmo_stat_item_group *statg; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 206 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 207 | llist_for_each_entry(statg, &osmo_stat_item_groups, list) { |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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 */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 219 | const struct osmo_stat_item *osmo_stat_item_get_by_name( |
| 220 | const struct osmo_stat_item_group *statg, const char *name) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 221 | { |
| 222 | int i; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 223 | const struct osmo_stat_item_desc *item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 238 | int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, |
| 239 | osmo_stat_item_handler_t handle_item, void *data) |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 240 | { |
| 241 | int rc = 0; |
| 242 | int i; |
| 243 | |
| 244 | for (i = 0; i < statg->desc->num_items; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 245 | struct osmo_stat_item *item = statg->items[i]; |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 246 | rc = handle_item(statg, item, data); |
| 247 | if (rc < 0) |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | return rc; |
| 252 | } |
| 253 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 254 | int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data) |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 255 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 256 | struct osmo_stat_item_group *statg; |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 257 | int rc = 0; |
| 258 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 259 | llist_for_each_entry(statg, &osmo_stat_item_groups, list) { |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 260 | rc = handle_group(statg, data); |
| 261 | if (rc < 0) |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | return rc; |
| 266 | } |
| 267 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 268 | /*! @} */ |