Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file stat_item.c |
| 2 | * utility routines for keeping conters about events and the event rates. */ |
| 3 | /* |
| 4 | * (C) 2015 by Sysmocom s.f.m.c. GmbH |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 5 | * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 25 | /*! \addtogroup osmo_stat_item |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 26 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 27 | * \file stat_item.c */ |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 28 | |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include <osmocom/core/utils.h> |
| 33 | #include <osmocom/core/linuxlist.h> |
| 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/timer.h> |
| 36 | #include <osmocom/core/stat_item.h> |
| 37 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 38 | static LLIST_HEAD(osmo_stat_item_groups); |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 39 | static int32_t global_value_id = 0; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 40 | |
| 41 | static void *tall_stat_item_ctx; |
| 42 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 43 | /*! Allocate a new group of counters according to description |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 44 | * \param[in] ctx \ref talloc context |
| 45 | * \param[in] desc Statistics item group description |
| 46 | * \param[in] idx Index of new stat item group |
| 47 | */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 48 | struct osmo_stat_item_group *osmo_stat_item_group_alloc(void *ctx, |
| 49 | const struct osmo_stat_item_group_desc *desc, |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 50 | unsigned int idx) |
| 51 | { |
| 52 | unsigned int group_size; |
Harald Welte | b32a194 | 2015-11-20 10:22:14 +0100 | [diff] [blame] | 53 | unsigned long items_size = 0; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 54 | unsigned int item_idx; |
| 55 | void *items; |
| 56 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 57 | struct osmo_stat_item_group *group; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 58 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 59 | group_size = sizeof(struct osmo_stat_item_group) + |
| 60 | desc->num_items * sizeof(struct osmo_stat_item *); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 61 | |
| 62 | if (!ctx) |
| 63 | ctx = tall_stat_item_ctx; |
| 64 | |
| 65 | group = talloc_zero_size(ctx, group_size); |
| 66 | if (!group) |
| 67 | return NULL; |
| 68 | |
| 69 | group->desc = desc; |
| 70 | group->idx = idx; |
| 71 | |
| 72 | /* Get combined size of all items */ |
| 73 | for (item_idx = 0; item_idx < desc->num_items; item_idx++) { |
| 74 | unsigned int size; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 75 | size = sizeof(struct osmo_stat_item) + |
| 76 | sizeof(struct osmo_stat_item_value) * |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 77 | desc->item_desc[item_idx].num_values; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 95 | struct osmo_stat_item *item = (struct osmo_stat_item *) |
Harald Welte | b32a194 | 2015-11-20 10:22:14 +0100 | [diff] [blame] | 96 | ((uint8_t *)items + (unsigned long)group->items[item_idx]); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 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 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 104 | for (i = 0; i <= item->last_offs; i++) { |
| 105 | item->values[i].value = desc->item_desc[item_idx].default_value; |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 106 | item->values[i].id = OSMO_STAT_ITEM_NOVALUE_ID; |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 107 | } |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 110 | llist_add(&group->list, &osmo_stat_item_groups); |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 111 | |
| 112 | return group; |
| 113 | } |
| 114 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 115 | /*! Free the memory for the specified group of counters */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 116 | void osmo_stat_item_group_free(struct osmo_stat_item_group *grp) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 117 | { |
| 118 | llist_del(&grp->list); |
| 119 | talloc_free(grp); |
| 120 | } |
| 121 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 122 | 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] | 123 | { |
| 124 | item->last_offs += 1; |
| 125 | if (item->last_offs >= item->desc->num_values) |
| 126 | item->last_offs = 0; |
| 127 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 128 | global_value_id += 1; |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 129 | if (global_value_id == OSMO_STAT_ITEM_NOVALUE_ID) |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 130 | global_value_id += 1; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 131 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 132 | item->values[item->last_offs].value = value; |
| 133 | item->values[item->last_offs].id = global_value_id; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 136 | 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] | 137 | int32_t *value) |
| 138 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 139 | const struct osmo_stat_item_value *next_value; |
| 140 | const struct osmo_stat_item_value *item_value = NULL; |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 141 | int idx_delta; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 142 | int next_offs; |
| 143 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 144 | next_offs = item->last_offs; |
| 145 | next_value = &item->values[next_offs]; |
| 146 | |
| 147 | while (next_value->id - *next_idx >= 0 && |
Jacob Erlbeck | ac4ed17 | 2015-12-08 10:29:16 +0100 | [diff] [blame] | 148 | next_value->id != OSMO_STAT_ITEM_NOVALUE_ID) |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 149 | { |
| 150 | item_value = next_value; |
| 151 | |
| 152 | next_offs -= 1; |
| 153 | if (next_offs < 0) |
| 154 | next_offs = item->desc->num_values - 1; |
| 155 | if (next_offs == item->last_offs) |
| 156 | break; |
| 157 | next_value = &item->values[next_offs]; |
| 158 | } |
| 159 | |
| 160 | if (!item_value) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 161 | /* All items have been read */ |
| 162 | return 0; |
| 163 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 164 | *value = item_value->value; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 165 | |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 166 | idx_delta = item_value->id + 1 - *next_idx; |
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 | *next_idx = item_value->id + 1; |
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 | return idx_delta; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 173 | /*! Skip all values of this item and update idx accordingly */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 174 | 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] | 175 | { |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 176 | int discarded = item->values[item->last_offs].id + 1 - *idx; |
| 177 | *idx = item->values[item->last_offs].id + 1; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 178 | |
| 179 | return discarded; |
| 180 | } |
| 181 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 182 | /*! Skip all values of all items and update idx accordingly */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 183 | int osmo_stat_item_discard_all(int32_t *idx) |
Jacob Erlbeck | b27b352 | 2015-10-12 18:47:09 +0200 | [diff] [blame] | 184 | { |
| 185 | int discarded = global_value_id + 1 - *idx; |
| 186 | *idx = global_value_id + 1; |
| 187 | |
| 188 | return discarded; |
| 189 | } |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 190 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 191 | /*! Initialize the stat item module */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 192 | int osmo_stat_item_init(void *tall_ctx) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 193 | { |
| 194 | tall_stat_item_ctx = tall_ctx; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 199 | /*! Search for item group based on group name and index */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 200 | 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] | 201 | const char *name, const unsigned int idx) |
| 202 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 203 | struct osmo_stat_item_group *statg; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 204 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 205 | llist_for_each_entry(statg, &osmo_stat_item_groups, list) { |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 206 | if (!statg->desc) |
| 207 | continue; |
| 208 | |
| 209 | if (!strcmp(statg->desc->group_name_prefix, name) && |
| 210 | statg->idx == idx) |
| 211 | return statg; |
| 212 | } |
| 213 | return NULL; |
| 214 | } |
| 215 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 216 | /*! Search for item group based on group name */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 217 | const struct osmo_stat_item *osmo_stat_item_get_by_name( |
| 218 | const struct osmo_stat_item_group *statg, const char *name) |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 219 | { |
| 220 | int i; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 221 | const struct osmo_stat_item_desc *item_desc; |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 222 | |
| 223 | if (!statg->desc) |
| 224 | return NULL; |
| 225 | |
| 226 | for (i = 0; i < statg->desc->num_items; i++) { |
| 227 | item_desc = &statg->desc->item_desc[i]; |
| 228 | |
| 229 | if (!strcmp(item_desc->name, name)) { |
| 230 | return statg->items[i]; |
| 231 | } |
| 232 | } |
| 233 | return NULL; |
| 234 | } |
| 235 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 236 | int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, |
| 237 | osmo_stat_item_handler_t handle_item, void *data) |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 238 | { |
| 239 | int rc = 0; |
| 240 | int i; |
| 241 | |
| 242 | for (i = 0; i < statg->desc->num_items; i++) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 243 | struct osmo_stat_item *item = statg->items[i]; |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 244 | rc = handle_item(statg, item, data); |
| 245 | if (rc < 0) |
| 246 | return rc; |
| 247 | } |
| 248 | |
| 249 | return rc; |
| 250 | } |
| 251 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 252 | 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] | 253 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 254 | struct osmo_stat_item_group *statg; |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 255 | int rc = 0; |
| 256 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 257 | llist_for_each_entry(statg, &osmo_stat_item_groups, list) { |
Jacob Erlbeck | c6a7108 | 2015-10-19 14:04:38 +0200 | [diff] [blame] | 258 | rc = handle_group(statg, data); |
| 259 | if (rc < 0) |
| 260 | return rc; |
| 261 | } |
| 262 | |
| 263 | return rc; |
| 264 | } |
| 265 | |
Jacob Erlbeck | 9732cb4 | 2015-10-01 20:43:53 +0200 | [diff] [blame] | 266 | /*! @} */ |