blob: 3aa1f52371f8a63c55dfcd26007de4423bb85962 [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001/* tests for statistics */
2/*
Harald Weltee08da972017-11-13 01:00:26 +09003 * (C) 2015 sysmocom - s.m.f.c. GmbH
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02004 *
5 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02009 * 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
Oliver Smithd89d35e2021-03-17 15:29:55 +010025#include <osmocom/core/application.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020026#include <osmocom/core/logging.h>
27#include <osmocom/core/utils.h>
28#include <osmocom/core/stat_item.h>
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010029#include <osmocom/core/rate_ctr.h>
30#include <osmocom/core/stats.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020031
Neels Hofmeyre90c7172021-09-14 14:37:38 +020032#include <stat_item_internal.h>
33
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020034#include <stdio.h>
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +010035#include <inttypes.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020036
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010037enum test_ctr {
38 TEST_A_CTR,
39 TEST_B_CTR,
40};
41
42static const struct rate_ctr_desc ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080043 [TEST_A_CTR] = { "ctr:a", "The A counter value"},
44 [TEST_B_CTR] = { "ctr:b", "The B counter value"},
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010045};
46
47static const struct rate_ctr_group_desc ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080048 .group_name_prefix = "ctr-test:one",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010049 .group_description = "Counter test number 1",
50 .num_ctr = ARRAY_SIZE(ctr_description),
51 .ctr_desc = ctr_description,
52 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
53};
54
Harald Welte04c88122017-10-03 18:34:48 +080055static const struct rate_ctr_desc ctr_description_dot[] = {
56 [TEST_A_CTR] = { "ctr.a", "The A counter value with ."},
57 [TEST_B_CTR] = { "ctr.b", "The B counter value with ."},
58};
59
60static const struct rate_ctr_group_desc ctrg_desc_dot = {
61 .group_name_prefix = "ctr-test.one_dot",
62 .group_description = "Counter test number 1dot",
63 .num_ctr = ARRAY_SIZE(ctr_description_dot),
64 .ctr_desc = ctr_description_dot,
65 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
66};
67
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010068enum test_items {
69 TEST_A_ITEM,
70 TEST_B_ITEM,
71};
72
73static const struct osmo_stat_item_desc item_description[] = {
74 [TEST_A_ITEM] = { "item.a", "The A value", "ma", 4, -1 },
75 [TEST_B_ITEM] = { "item.b", "The B value", "kb", 7, -1 },
76};
77
78static const struct osmo_stat_item_group_desc statg_desc = {
79 .group_name_prefix = "test.one",
80 .group_description = "Test number 1",
81 .num_items = ARRAY_SIZE(item_description),
82 .item_desc = item_description,
83 .class_id = OSMO_STATS_CLASS_PEER,
84};
85
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020086static void stat_test(void)
87{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010088 struct osmo_stat_item_group *statg =
89 osmo_stat_item_group_alloc(NULL, &statg_desc, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020090
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010091 struct osmo_stat_item_group *sgrp2;
92 const struct osmo_stat_item *sitem1, *sitem2;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020093 int32_t value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020094 int i;
Neels Hofmeyre90c7172021-09-14 14:37:38 +020095 int64_t sum1;
96 int64_t sum2;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020097
98 OSMO_ASSERT(statg != NULL);
99
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100100 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200101 OSMO_ASSERT(sgrp2 == statg);
102
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200104 OSMO_ASSERT(sgrp2 == NULL);
105
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100106 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.two", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200107 OSMO_ASSERT(sgrp2 == NULL);
108
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109 sitem1 = osmo_stat_item_get_by_name(statg, "item.c");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200110 OSMO_ASSERT(sitem1 == NULL);
111
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112 sitem1 = osmo_stat_item_get_by_name(statg, "item.a");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200113 OSMO_ASSERT(sitem1 != NULL);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200114 OSMO_ASSERT(sitem1 == osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200115
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100116 sitem2 = osmo_stat_item_get_by_name(statg, "item.b");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200117 OSMO_ASSERT(sitem2 != NULL);
118 OSMO_ASSERT(sitem2 != sitem1);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200119 OSMO_ASSERT(sitem2 == osmo_stat_item_group_get_item(statg, TEST_B_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200120
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200121 /* No value set yet, expecting default value from osmo_stat_item_desc definition above. */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200122 value = osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200123 OSMO_ASSERT(value == -1);
124
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200125 /* No value set yet, expecting new value in all fields */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200126 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 1);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200127 sum1 = 1;
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200128 value = osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200129 OSMO_ASSERT(value == 1);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200130 OSMO_ASSERT(sitem1->value.n == 1);
131 OSMO_ASSERT(sitem1->value.min == 1);
132 OSMO_ASSERT(sitem1->value.last == 1);
133 OSMO_ASSERT(sitem1->value.max == 1);
134 OSMO_ASSERT(sitem1->value.sum == 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200135
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200136 sum2 = 0;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200137 for (i = 2; i <= 32; i++) {
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200138 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), i);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200139 sum1 += i;
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200140 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_B_ITEM), 1000 + i);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200141 sum2 += 1000 + i;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200142 }
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200143 OSMO_ASSERT(sitem1->value.n == 32);
144 OSMO_ASSERT(sitem1->value.min == 1);
145 OSMO_ASSERT(sitem1->value.last == 32);
146 OSMO_ASSERT(sitem1->value.max == 32);
147 OSMO_ASSERT(sitem1->value.sum == sum1);
148
149 OSMO_ASSERT(sitem2->value.n == 31);
150 OSMO_ASSERT(sitem2->value.min == 1002);
151 OSMO_ASSERT(sitem2->value.last == 1032);
152 OSMO_ASSERT(sitem2->value.max == 1032);
153 OSMO_ASSERT(sitem2->value.sum == sum2);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200154
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200155 /* check if dec & inc is working */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200156 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 42);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200157 sum1 += 42;
158 OSMO_ASSERT(sitem1->value.n == 33);
159 OSMO_ASSERT(sitem1->value.min == 1);
160 OSMO_ASSERT(sitem1->value.last == 42);
161 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 42);
162 OSMO_ASSERT(sitem1->value.max == 42);
163 OSMO_ASSERT(sitem1->value.sum == sum1);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200164
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200165 osmo_stat_item_dec(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 21);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200166 sum1 += 42 - 21;
167 OSMO_ASSERT(sitem1->value.n == 34);
168 OSMO_ASSERT(sitem1->value.min == 1);
169 OSMO_ASSERT(sitem1->value.last == 21);
170 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 21);
171 OSMO_ASSERT(sitem1->value.max == 42);
172 OSMO_ASSERT(sitem1->value.sum == sum1);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200173
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200174 osmo_stat_item_inc(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 21);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200175 sum1 += 42;
176 OSMO_ASSERT(sitem1->value.n == 35);
177 OSMO_ASSERT(sitem1->value.min == 1);
178 OSMO_ASSERT(sitem1->value.last == 42);
179 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 42);
180 OSMO_ASSERT(sitem1->value.max == 42);
181 OSMO_ASSERT(sitem1->value.sum == sum1);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200182
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200183 /* Test item flush, reporting period elapsing */
184 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
185 OSMO_ASSERT(sitem1->value.n == 0);
186 OSMO_ASSERT(sitem1->value.min == 42);
187 OSMO_ASSERT(sitem1->value.last == 42);
188 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 42);
189 OSMO_ASSERT(sitem1->value.max == 42);
190 OSMO_ASSERT(sitem1->value.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200191
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200192 /* Still see the previous reporting period in reported.* */
193 OSMO_ASSERT(sitem1->reported.n == 35);
194 OSMO_ASSERT(sitem1->reported.min == 1);
195 OSMO_ASSERT(sitem1->reported.last == 42);
196 OSMO_ASSERT(sitem1->reported.max == 42);
197 OSMO_ASSERT(sitem1->reported.sum == sum1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200198
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200199 /* After a flush, the first item replaces the last, min and max */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200200 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 97);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200201 OSMO_ASSERT(sitem1->value.n == 1);
202 OSMO_ASSERT(sitem1->value.min == 97);
203 OSMO_ASSERT(sitem1->value.last == 97);
204 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 97);
205 OSMO_ASSERT(sitem1->value.max == 97);
206 OSMO_ASSERT(sitem1->value.sum == 97);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200207
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200208 /* ...and still see the previous reporting period in reported.* */
209 OSMO_ASSERT(sitem1->reported.n == 35);
210 OSMO_ASSERT(sitem1->reported.min == 1);
211 OSMO_ASSERT(sitem1->reported.last == 42);
212 OSMO_ASSERT(sitem1->reported.max == 42);
213 OSMO_ASSERT(sitem1->reported.sum == sum1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200214
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200215 /* If an entire reporting period elapses without a new value, the last seen value remains. */
216 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
217 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
218 OSMO_ASSERT(sitem1->value.n == 0);
219 OSMO_ASSERT(sitem1->value.min == 97);
220 OSMO_ASSERT(sitem1->value.last == 97);
221 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 97);
222 OSMO_ASSERT(sitem1->value.max == 97);
223 OSMO_ASSERT(sitem1->value.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200224
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200225 /* now the previous reporting period got turned around */
226 OSMO_ASSERT(sitem1->reported.n == 0);
227 OSMO_ASSERT(sitem1->reported.min == 97);
228 OSMO_ASSERT(sitem1->reported.last == 97);
229 OSMO_ASSERT(sitem1->reported.max == 97);
230 OSMO_ASSERT(sitem1->reported.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200231
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200232 /* Another empty reporting period, everything remained the same. */
233 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
234 OSMO_ASSERT(sitem1->value.n == 0);
235 OSMO_ASSERT(sitem1->value.min == 97);
236 OSMO_ASSERT(sitem1->value.last == 97);
237 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 97);
238 OSMO_ASSERT(sitem1->value.max == 97);
239 OSMO_ASSERT(sitem1->value.sum == 0);
240 OSMO_ASSERT(sitem1->reported.n == 0);
241 OSMO_ASSERT(sitem1->reported.min == 97);
242 OSMO_ASSERT(sitem1->reported.last == 97);
243 OSMO_ASSERT(sitem1->reported.max == 97);
244 OSMO_ASSERT(sitem1->reported.sum == 0);
245
246 /* Test Reset, place back to default value. The previously reported value remains the same. */
247 osmo_stat_item_reset(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
248 OSMO_ASSERT(sitem1->value.n == 0);
249 OSMO_ASSERT(sitem1->value.min == -1);
250 OSMO_ASSERT(sitem1->value.last == -1);
251 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == -1);
252 OSMO_ASSERT(sitem1->value.max == -1);
253 OSMO_ASSERT(sitem1->value.sum == 0);
254 OSMO_ASSERT(sitem1->reported.n == 0);
255 OSMO_ASSERT(sitem1->reported.min == 97);
256 OSMO_ASSERT(sitem1->reported.last == 97);
257 OSMO_ASSERT(sitem1->reported.max == 97);
258 OSMO_ASSERT(sitem1->reported.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200259
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100260 osmo_stat_item_group_free(statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200261
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100262 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200263 OSMO_ASSERT(sgrp2 == NULL);
264}
265
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100266/*** stats reporter tests ***/
267
268/* define a special stats reporter for testing */
269
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200270static int sent_counter_vals;
271static int sent_stat_item_vals;
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100272
273enum {
274 OSMO_STATS_REPORTER_TEST = OSMO_STATS_REPORTER_LOG + 1,
275};
276
277static int stats_reporter_test_send_counter(struct osmo_stats_reporter *srep,
278 const struct rate_ctr_group *ctrg,
279 const struct rate_ctr_desc *desc,
280 int64_t value, int64_t delta)
281{
282 const char *group_name = ctrg ? ctrg->desc->group_name_prefix : "";
283
Oliver Smithd89d35e2021-03-17 15:29:55 +0100284 fprintf(stderr, " %s: counter p=%s g=%s i=%u n=%s v=%lld d=%lld\n",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100285 srep->name,
286 srep->name_prefix ? srep->name_prefix : "",
287 group_name, ctrg ? ctrg->idx : 0,
288 desc->name, (long long)value, (long long)delta);
289
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200290 sent_counter_vals++;
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100291 return 0;
292}
293
294static int stats_reporter_test_send_item(struct osmo_stats_reporter *srep,
295 const struct osmo_stat_item_group *statg,
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +0100296 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100297{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100298 fprintf(stderr, " %s: item p=%s g=%s i=%u n=%s v=%"PRId64" u=%s\n",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100299 srep->name,
300 srep->name_prefix ? srep->name_prefix : "",
301 statg->desc->group_name_prefix, statg->idx,
302 desc->name, value, desc->unit ? desc->unit : "");
303
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200304 sent_stat_item_vals++;
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100305 return 0;
306}
307
308static int stats_reporter_test_open(struct osmo_stats_reporter *srep)
309{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100310 fprintf(stderr, " %s: open\n", srep->name);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100311 return 0;
312}
313
314static int stats_reporter_test_close(struct osmo_stats_reporter *srep)
315{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100316 fprintf(stderr, " %s: close\n", srep->name);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100317 return 0;
318}
319
320static struct osmo_stats_reporter *stats_reporter_create_test(const char *name)
321{
322 struct osmo_stats_reporter *srep;
323 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_TEST, name);
324
325 srep->have_net_config = 0;
326
327 srep->open = stats_reporter_test_open;
328 srep->close = stats_reporter_test_close;
329 srep->send_counter = stats_reporter_test_send_counter;
330 srep->send_item = stats_reporter_test_send_item;
331
332 return srep;
333}
334
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200335static void _do_report(int expect_counter_vals, int expect_stat_item_vals, int line)
336{
337 sent_counter_vals = 0;
338 sent_stat_item_vals = 0;
339 osmo_stats_report();
340 fprintf(stderr, "reported: %d counter vals, %d stat item vals\n", sent_counter_vals, sent_stat_item_vals);
341 OSMO_ASSERT(sent_counter_vals == expect_counter_vals);
342 OSMO_ASSERT(sent_stat_item_vals == expect_stat_item_vals);
343}
344
345#define do_report(A, B) _do_report(A, B, __LINE__)
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100346
347static void test_reporting()
348{
349 struct osmo_stats_reporter *srep1, *srep2, *srep;
350 struct osmo_stat_item_group *statg1, *statg2;
Max3ef14a22017-12-15 20:19:10 +0100351 struct rate_ctr_group *ctrg1, *ctrg2, *ctrg3, *ctrg_dup;
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100352 void *stats_ctx = talloc_named_const(NULL, 1, "stats test context");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100353
354 int rc;
355
Oliver Smithd89d35e2021-03-17 15:29:55 +0100356 fprintf(stderr, "Start test: %s\n", __func__);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100357
358 /* Allocate counters and items */
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100359 statg1 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 1);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100360 OSMO_ASSERT(statg1 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100361 statg2 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100362 OSMO_ASSERT(statg2 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100363 ctrg1 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 1);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100364 OSMO_ASSERT(ctrg1 && ctrg1->idx == 1);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100365 ctrg2 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100366 OSMO_ASSERT(ctrg2 && ctrg2->idx == 2);
Max3ef14a22017-12-15 20:19:10 +0100367
368 ctrg_dup = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100369 OSMO_ASSERT(ctrg_dup && ctrg_dup->idx == 3);
370 rate_ctr_group_free(ctrg_dup);
Max3ef14a22017-12-15 20:19:10 +0100371
Harald Welte04c88122017-10-03 18:34:48 +0800372 ctrg3 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc_dot, 3);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100373 OSMO_ASSERT(ctrg3 && ctrg3->idx == 3);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100374
375 srep1 = stats_reporter_create_test("test1");
376 OSMO_ASSERT(srep1 != NULL);
377
378 srep2 = stats_reporter_create_test("test2");
379 OSMO_ASSERT(srep2 != NULL);
380
381 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test1");
382 OSMO_ASSERT(srep == srep1);
383 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test2");
384 OSMO_ASSERT(srep == srep2);
385
386 rc = osmo_stats_reporter_enable(srep1);
387 OSMO_ASSERT(rc >= 0);
388 OSMO_ASSERT(srep1->force_single_flush);
389 rc = osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
390 OSMO_ASSERT(rc >= 0);
391
392 rc = osmo_stats_reporter_enable(srep2);
393 OSMO_ASSERT(rc >= 0);
394 OSMO_ASSERT(srep2->force_single_flush);
395 rc = osmo_stats_reporter_set_max_class(srep2, OSMO_STATS_CLASS_SUBSCRIBER);
396 OSMO_ASSERT(rc >= 0);
397
Oliver Smithd89d35e2021-03-17 15:29:55 +0100398 fprintf(stderr, "report (initial):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200399 do_report(12, 8);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100400
Oliver Smithd89d35e2021-03-17 15:29:55 +0100401 fprintf(stderr, "report (srep1 global):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100402 /* force single flush */
403 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_GLOBAL);
404 srep1->force_single_flush = 1;
405 srep2->force_single_flush = 1;
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200406 do_report(6, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100407
Oliver Smithd89d35e2021-03-17 15:29:55 +0100408 fprintf(stderr, "report (srep1 peer):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100409 /* force single flush */
410 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_PEER);
411 srep1->force_single_flush = 1;
412 srep2->force_single_flush = 1;
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200413 do_report(6, 8);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100414
Oliver Smithd89d35e2021-03-17 15:29:55 +0100415 fprintf(stderr, "report (srep1 subscriber):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100416 /* force single flush */
417 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
418 srep1->force_single_flush = 1;
419 srep2->force_single_flush = 1;
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200420 do_report(12, 8);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100421
Oliver Smithd89d35e2021-03-17 15:29:55 +0100422 fprintf(stderr, "report (srep2 disabled):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100423 /* force single flush */
424 srep1->force_single_flush = 1;
425 srep2->force_single_flush = 1;
426 rc = osmo_stats_reporter_disable(srep2);
427 OSMO_ASSERT(rc >= 0);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200428 do_report(6, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100429
Oliver Smithd89d35e2021-03-17 15:29:55 +0100430 fprintf(stderr, "report (srep2 enabled, no flush forced):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100431 rc = osmo_stats_reporter_enable(srep2);
432 OSMO_ASSERT(rc >= 0);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200433 do_report(6, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100434
Oliver Smithd89d35e2021-03-17 15:29:55 +0100435 fprintf(stderr, "report (should be empty):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200436 do_report(0, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100437
Oliver Smithd89d35e2021-03-17 15:29:55 +0100438 fprintf(stderr, "report (group 1, counter 1 update):\n");
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200439 rate_ctr_inc(rate_ctr_group_get_ctr(ctrg1, TEST_A_CTR));
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200440 do_report(2, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100441
Oliver Smithd89d35e2021-03-17 15:29:55 +0100442 fprintf(stderr, "report (group 1, item 1 update):\n");
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200443 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 10);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200444 do_report(0, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100445
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200446 fprintf(stderr, "report (group 1, item 1 update twice, with same value):\n");
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200447 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 10);
448 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 10);
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200449 do_report(0, 0);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100450
Oliver Smithd89d35e2021-03-17 15:29:55 +0100451 fprintf(stderr, "report (group 1, item 1 update twice, check max):\n");
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200452 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 20);
453 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 10);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200454 do_report(0, 2);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100455
Oliver Smitha79a5492021-08-19 10:07:44 +0200456 fprintf(stderr, "report (group 1, item 1 no update, send last item (!= last max), OS#5215):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200457 do_report(0, 2);
Oliver Smitha79a5492021-08-19 10:07:44 +0200458
459 fprintf(stderr, "report (group 1, item 1 no update, nothing to send):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200460 do_report(0, 0);
Oliver Smitha79a5492021-08-19 10:07:44 +0200461
Oliver Smithd89d35e2021-03-17 15:29:55 +0100462 fprintf(stderr, "report (remove statg1, ctrg1):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100463 /* force single flush */
464 srep1->force_single_flush = 1;
465 srep2->force_single_flush = 1;
466 osmo_stat_item_group_free(statg1);
467 rate_ctr_group_free(ctrg1);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200468 do_report(8, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100469
Oliver Smithd89d35e2021-03-17 15:29:55 +0100470 fprintf(stderr, "report (remove srep1):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100471 /* force single flush */
472 srep1->force_single_flush = 1;
473 srep2->force_single_flush = 1;
474 osmo_stats_reporter_free(srep1);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200475 do_report(4, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100476
Oliver Smithd89d35e2021-03-17 15:29:55 +0100477 fprintf(stderr, "report (remove statg2):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100478 /* force single flush */
479 srep2->force_single_flush = 1;
480 osmo_stat_item_group_free(statg2);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200481 do_report(4, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100482
Oliver Smithd89d35e2021-03-17 15:29:55 +0100483 fprintf(stderr, "report (remove srep2):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100484 /* force single flush */
485 srep2->force_single_flush = 1;
486 osmo_stats_reporter_free(srep2);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200487 do_report(0, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100488
Oliver Smithd89d35e2021-03-17 15:29:55 +0100489 fprintf(stderr, "report (remove ctrg2, should be empty):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100490 rate_ctr_group_free(ctrg2);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200491 do_report(0, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100492
Harald Welte04c88122017-10-03 18:34:48 +0800493 rate_ctr_group_free(ctrg3);
494
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100495 /* Leak check */
496 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
497 talloc_free(stats_ctx);
498
Oliver Smithd89d35e2021-03-17 15:29:55 +0100499 fprintf(stderr, "End test: %s\n", __func__);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100500}
501
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200502int main(int argc, char **argv)
503{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100504 void *ctx = talloc_named_const(NULL, 0, "main");
505 osmo_init_logging2(ctx, NULL);
506
507 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
508 log_set_print_level(osmo_stderr_target, 1);
509 log_set_print_category(osmo_stderr_target, 1);
510 log_set_print_category_hex(osmo_stderr_target, 0);
511 log_set_use_color(osmo_stderr_target, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200512
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100513 osmo_stat_item_init(NULL);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200514
515 stat_test();
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100516 test_reporting();
Oliver Smithd89d35e2021-03-17 15:29:55 +0100517 talloc_free(ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200518 return 0;
519}