blob: eda4129b693d137e2b6d27dc84cffc8c418cf87b [file] [log] [blame]
Jacob Erlbeck9732cb42015-10-01 20:43:53 +02001/* tests for statistics */
2/*
Vadim Yanitskiy03590fc2023-05-18 17:22:26 +07003 * (C) 2015 sysmocom - s.f.m.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 *
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020019 */
20
Oliver Smithd89d35e2021-03-17 15:29:55 +010021#include <osmocom/core/application.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020022#include <osmocom/core/logging.h>
23#include <osmocom/core/utils.h>
24#include <osmocom/core/stat_item.h>
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010025#include <osmocom/core/rate_ctr.h>
26#include <osmocom/core/stats.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020027
Neels Hofmeyre90c7172021-09-14 14:37:38 +020028#include <stat_item_internal.h>
29
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020030#include <stdio.h>
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +010031#include <inttypes.h>
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020032
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010033enum test_ctr {
34 TEST_A_CTR,
35 TEST_B_CTR,
36};
37
38static const struct rate_ctr_desc ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080039 [TEST_A_CTR] = { "ctr:a", "The A counter value"},
40 [TEST_B_CTR] = { "ctr:b", "The B counter value"},
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010041};
42
43static const struct rate_ctr_group_desc ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080044 .group_name_prefix = "ctr-test:one",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010045 .group_description = "Counter test number 1",
46 .num_ctr = ARRAY_SIZE(ctr_description),
47 .ctr_desc = ctr_description,
48 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
49};
50
Harald Welte04c88122017-10-03 18:34:48 +080051static const struct rate_ctr_desc ctr_description_dot[] = {
52 [TEST_A_CTR] = { "ctr.a", "The A counter value with ."},
53 [TEST_B_CTR] = { "ctr.b", "The B counter value with ."},
54};
55
56static const struct rate_ctr_group_desc ctrg_desc_dot = {
57 .group_name_prefix = "ctr-test.one_dot",
58 .group_description = "Counter test number 1dot",
59 .num_ctr = ARRAY_SIZE(ctr_description_dot),
60 .ctr_desc = ctr_description_dot,
61 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
62};
63
Jacob Erlbeck46b703d2015-11-09 17:25:27 +010064enum test_items {
65 TEST_A_ITEM,
66 TEST_B_ITEM,
67};
68
69static const struct osmo_stat_item_desc item_description[] = {
70 [TEST_A_ITEM] = { "item.a", "The A value", "ma", 4, -1 },
71 [TEST_B_ITEM] = { "item.b", "The B value", "kb", 7, -1 },
72};
73
74static const struct osmo_stat_item_group_desc statg_desc = {
75 .group_name_prefix = "test.one",
76 .group_description = "Test number 1",
77 .num_items = ARRAY_SIZE(item_description),
78 .item_desc = item_description,
79 .class_id = OSMO_STATS_CLASS_PEER,
80};
81
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020082static void stat_test(void)
83{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010084 struct osmo_stat_item_group *statg =
85 osmo_stat_item_group_alloc(NULL, &statg_desc, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020086
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010087 struct osmo_stat_item_group *sgrp2;
88 const struct osmo_stat_item *sitem1, *sitem2;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020089 int32_t value;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020090 int i;
Neels Hofmeyre90c7172021-09-14 14:37:38 +020091 int64_t sum1;
92 int64_t sum2;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020093
94 OSMO_ASSERT(statg != NULL);
95
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010096 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +020097 OSMO_ASSERT(sgrp2 == statg);
98
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010099 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200100 OSMO_ASSERT(sgrp2 == NULL);
101
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100102 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.two", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200103 OSMO_ASSERT(sgrp2 == NULL);
104
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100105 sitem1 = osmo_stat_item_get_by_name(statg, "item.c");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200106 OSMO_ASSERT(sitem1 == NULL);
107
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108 sitem1 = osmo_stat_item_get_by_name(statg, "item.a");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200109 OSMO_ASSERT(sitem1 != NULL);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200110 OSMO_ASSERT(sitem1 == osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200111
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112 sitem2 = osmo_stat_item_get_by_name(statg, "item.b");
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200113 OSMO_ASSERT(sitem2 != NULL);
114 OSMO_ASSERT(sitem2 != sitem1);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200115 OSMO_ASSERT(sitem2 == osmo_stat_item_group_get_item(statg, TEST_B_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200116
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200117 /* No value set yet, expecting default value from osmo_stat_item_desc definition above. */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200118 value = osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200119 OSMO_ASSERT(value == -1);
120
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200121 /* No value set yet, expecting new value in all fields */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200122 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 1);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200123 sum1 = 1;
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200124 value = osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200125 OSMO_ASSERT(value == 1);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200126 OSMO_ASSERT(sitem1->value.n == 1);
127 OSMO_ASSERT(sitem1->value.min == 1);
128 OSMO_ASSERT(sitem1->value.last == 1);
129 OSMO_ASSERT(sitem1->value.max == 1);
130 OSMO_ASSERT(sitem1->value.sum == 1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200131
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200132 sum2 = 0;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200133 for (i = 2; i <= 32; i++) {
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200134 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), i);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200135 sum1 += i;
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200136 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_B_ITEM), 1000 + i);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200137 sum2 += 1000 + i;
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200138 }
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200139 OSMO_ASSERT(sitem1->value.n == 32);
140 OSMO_ASSERT(sitem1->value.min == 1);
141 OSMO_ASSERT(sitem1->value.last == 32);
142 OSMO_ASSERT(sitem1->value.max == 32);
143 OSMO_ASSERT(sitem1->value.sum == sum1);
144
145 OSMO_ASSERT(sitem2->value.n == 31);
146 OSMO_ASSERT(sitem2->value.min == 1002);
147 OSMO_ASSERT(sitem2->value.last == 1032);
148 OSMO_ASSERT(sitem2->value.max == 1032);
149 OSMO_ASSERT(sitem2->value.sum == sum2);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200150
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200151 /* check if dec & inc is working */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200152 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 42);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200153 sum1 += 42;
154 OSMO_ASSERT(sitem1->value.n == 33);
155 OSMO_ASSERT(sitem1->value.min == 1);
156 OSMO_ASSERT(sitem1->value.last == 42);
157 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 42);
158 OSMO_ASSERT(sitem1->value.max == 42);
159 OSMO_ASSERT(sitem1->value.sum == sum1);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200160
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200161 osmo_stat_item_dec(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 21);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200162 sum1 += 42 - 21;
163 OSMO_ASSERT(sitem1->value.n == 34);
164 OSMO_ASSERT(sitem1->value.min == 1);
165 OSMO_ASSERT(sitem1->value.last == 21);
166 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 21);
167 OSMO_ASSERT(sitem1->value.max == 42);
168 OSMO_ASSERT(sitem1->value.sum == sum1);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200169
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200170 osmo_stat_item_inc(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 21);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200171 sum1 += 42;
172 OSMO_ASSERT(sitem1->value.n == 35);
173 OSMO_ASSERT(sitem1->value.min == 1);
174 OSMO_ASSERT(sitem1->value.last == 42);
175 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 42);
176 OSMO_ASSERT(sitem1->value.max == 42);
177 OSMO_ASSERT(sitem1->value.sum == sum1);
Alexander Couzenscc72cc42019-04-27 23:19:55 +0200178
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200179 /* Test item flush, reporting period elapsing */
180 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
181 OSMO_ASSERT(sitem1->value.n == 0);
182 OSMO_ASSERT(sitem1->value.min == 42);
183 OSMO_ASSERT(sitem1->value.last == 42);
184 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 42);
185 OSMO_ASSERT(sitem1->value.max == 42);
186 OSMO_ASSERT(sitem1->value.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200187
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200188 /* Still see the previous reporting period in reported.* */
189 OSMO_ASSERT(sitem1->reported.n == 35);
190 OSMO_ASSERT(sitem1->reported.min == 1);
191 OSMO_ASSERT(sitem1->reported.last == 42);
192 OSMO_ASSERT(sitem1->reported.max == 42);
193 OSMO_ASSERT(sitem1->reported.sum == sum1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200194
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200195 /* After a flush, the first item replaces the last, min and max */
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200196 osmo_stat_item_set(osmo_stat_item_group_get_item(statg, TEST_A_ITEM), 97);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200197 OSMO_ASSERT(sitem1->value.n == 1);
198 OSMO_ASSERT(sitem1->value.min == 97);
199 OSMO_ASSERT(sitem1->value.last == 97);
200 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 97);
201 OSMO_ASSERT(sitem1->value.max == 97);
202 OSMO_ASSERT(sitem1->value.sum == 97);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200203
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200204 /* ...and still see the previous reporting period in reported.* */
205 OSMO_ASSERT(sitem1->reported.n == 35);
206 OSMO_ASSERT(sitem1->reported.min == 1);
207 OSMO_ASSERT(sitem1->reported.last == 42);
208 OSMO_ASSERT(sitem1->reported.max == 42);
209 OSMO_ASSERT(sitem1->reported.sum == sum1);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200210
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200211 /* If an entire reporting period elapses without a new value, the last seen value remains. */
212 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
213 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
214 OSMO_ASSERT(sitem1->value.n == 0);
215 OSMO_ASSERT(sitem1->value.min == 97);
216 OSMO_ASSERT(sitem1->value.last == 97);
217 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 97);
218 OSMO_ASSERT(sitem1->value.max == 97);
219 OSMO_ASSERT(sitem1->value.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200220
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200221 /* now the previous reporting period got turned around */
222 OSMO_ASSERT(sitem1->reported.n == 0);
223 OSMO_ASSERT(sitem1->reported.min == 97);
224 OSMO_ASSERT(sitem1->reported.last == 97);
225 OSMO_ASSERT(sitem1->reported.max == 97);
226 OSMO_ASSERT(sitem1->reported.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200227
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200228 /* Another empty reporting period, everything remained the same. */
229 osmo_stat_item_flush(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
230 OSMO_ASSERT(sitem1->value.n == 0);
231 OSMO_ASSERT(sitem1->value.min == 97);
232 OSMO_ASSERT(sitem1->value.last == 97);
233 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == 97);
234 OSMO_ASSERT(sitem1->value.max == 97);
235 OSMO_ASSERT(sitem1->value.sum == 0);
236 OSMO_ASSERT(sitem1->reported.n == 0);
237 OSMO_ASSERT(sitem1->reported.min == 97);
238 OSMO_ASSERT(sitem1->reported.last == 97);
239 OSMO_ASSERT(sitem1->reported.max == 97);
240 OSMO_ASSERT(sitem1->reported.sum == 0);
241
242 /* Test Reset, place back to default value. The previously reported value remains the same. */
243 osmo_stat_item_reset(osmo_stat_item_group_get_item(statg, TEST_A_ITEM));
244 OSMO_ASSERT(sitem1->value.n == 0);
245 OSMO_ASSERT(sitem1->value.min == -1);
246 OSMO_ASSERT(sitem1->value.last == -1);
247 OSMO_ASSERT(osmo_stat_item_get_last(osmo_stat_item_group_get_item(statg, TEST_A_ITEM)) == -1);
248 OSMO_ASSERT(sitem1->value.max == -1);
249 OSMO_ASSERT(sitem1->value.sum == 0);
250 OSMO_ASSERT(sitem1->reported.n == 0);
251 OSMO_ASSERT(sitem1->reported.min == 97);
252 OSMO_ASSERT(sitem1->reported.last == 97);
253 OSMO_ASSERT(sitem1->reported.max == 97);
254 OSMO_ASSERT(sitem1->reported.sum == 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200255
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100256 osmo_stat_item_group_free(statg);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200257
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100258 sgrp2 = osmo_stat_item_get_group_by_name_idx("test.one", 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200259 OSMO_ASSERT(sgrp2 == NULL);
260}
261
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100262/*** stats reporter tests ***/
263
264/* define a special stats reporter for testing */
265
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200266static int sent_counter_vals;
267static int sent_stat_item_vals;
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100268
269enum {
270 OSMO_STATS_REPORTER_TEST = OSMO_STATS_REPORTER_LOG + 1,
271};
272
273static int stats_reporter_test_send_counter(struct osmo_stats_reporter *srep,
274 const struct rate_ctr_group *ctrg,
275 const struct rate_ctr_desc *desc,
276 int64_t value, int64_t delta)
277{
278 const char *group_name = ctrg ? ctrg->desc->group_name_prefix : "";
279
Oliver Smithd89d35e2021-03-17 15:29:55 +0100280 fprintf(stderr, " %s: counter p=%s g=%s i=%u n=%s v=%lld d=%lld\n",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100281 srep->name,
282 srep->name_prefix ? srep->name_prefix : "",
283 group_name, ctrg ? ctrg->idx : 0,
284 desc->name, (long long)value, (long long)delta);
285
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200286 sent_counter_vals++;
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100287 return 0;
288}
289
290static int stats_reporter_test_send_item(struct osmo_stats_reporter *srep,
291 const struct osmo_stat_item_group *statg,
Neels Hofmeyrb41b48e2017-01-13 00:11:34 +0100292 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100293{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100294 fprintf(stderr, " %s: item p=%s g=%s i=%u n=%s v=%"PRId64" u=%s\n",
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100295 srep->name,
296 srep->name_prefix ? srep->name_prefix : "",
297 statg->desc->group_name_prefix, statg->idx,
298 desc->name, value, desc->unit ? desc->unit : "");
299
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200300 sent_stat_item_vals++;
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100301 return 0;
302}
303
304static int stats_reporter_test_open(struct osmo_stats_reporter *srep)
305{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100306 fprintf(stderr, " %s: open\n", srep->name);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100307 return 0;
308}
309
310static int stats_reporter_test_close(struct osmo_stats_reporter *srep)
311{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100312 fprintf(stderr, " %s: close\n", srep->name);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100313 return 0;
314}
315
316static struct osmo_stats_reporter *stats_reporter_create_test(const char *name)
317{
318 struct osmo_stats_reporter *srep;
319 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_TEST, name);
320
321 srep->have_net_config = 0;
322
323 srep->open = stats_reporter_test_open;
324 srep->close = stats_reporter_test_close;
325 srep->send_counter = stats_reporter_test_send_counter;
326 srep->send_item = stats_reporter_test_send_item;
327
328 return srep;
329}
330
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200331static void _do_report(int expect_counter_vals, int expect_stat_item_vals, int line)
332{
333 sent_counter_vals = 0;
334 sent_stat_item_vals = 0;
335 osmo_stats_report();
336 fprintf(stderr, "reported: %d counter vals, %d stat item vals\n", sent_counter_vals, sent_stat_item_vals);
337 OSMO_ASSERT(sent_counter_vals == expect_counter_vals);
338 OSMO_ASSERT(sent_stat_item_vals == expect_stat_item_vals);
339}
340
341#define do_report(A, B) _do_report(A, B, __LINE__)
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100342
Harald Weltee61d4592022-11-03 11:05:58 +0100343static void test_reporting(void)
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100344{
345 struct osmo_stats_reporter *srep1, *srep2, *srep;
346 struct osmo_stat_item_group *statg1, *statg2;
Max3ef14a22017-12-15 20:19:10 +0100347 struct rate_ctr_group *ctrg1, *ctrg2, *ctrg3, *ctrg_dup;
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100348 void *stats_ctx = talloc_named_const(NULL, 1, "stats test context");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100349
350 int rc;
351
Oliver Smithd89d35e2021-03-17 15:29:55 +0100352 fprintf(stderr, "Start test: %s\n", __func__);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100353
354 /* Allocate counters and items */
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100355 statg1 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 1);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100356 OSMO_ASSERT(statg1 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100357 statg2 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100358 OSMO_ASSERT(statg2 != NULL);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100359 ctrg1 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 1);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100360 OSMO_ASSERT(ctrg1 && ctrg1->idx == 1);
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100361 ctrg2 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100362 OSMO_ASSERT(ctrg2 && ctrg2->idx == 2);
Max3ef14a22017-12-15 20:19:10 +0100363
364 ctrg_dup = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100365 OSMO_ASSERT(ctrg_dup && ctrg_dup->idx == 3);
366 rate_ctr_group_free(ctrg_dup);
Max3ef14a22017-12-15 20:19:10 +0100367
Harald Welte04c88122017-10-03 18:34:48 +0800368 ctrg3 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc_dot, 3);
Neels Hofmeyr554f7b82017-12-20 01:14:31 +0100369 OSMO_ASSERT(ctrg3 && ctrg3->idx == 3);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100370
371 srep1 = stats_reporter_create_test("test1");
372 OSMO_ASSERT(srep1 != NULL);
373
374 srep2 = stats_reporter_create_test("test2");
375 OSMO_ASSERT(srep2 != NULL);
376
377 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test1");
378 OSMO_ASSERT(srep == srep1);
379 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_TEST, "test2");
380 OSMO_ASSERT(srep == srep2);
381
382 rc = osmo_stats_reporter_enable(srep1);
383 OSMO_ASSERT(rc >= 0);
384 OSMO_ASSERT(srep1->force_single_flush);
385 rc = osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
386 OSMO_ASSERT(rc >= 0);
387
388 rc = osmo_stats_reporter_enable(srep2);
389 OSMO_ASSERT(rc >= 0);
390 OSMO_ASSERT(srep2->force_single_flush);
391 rc = osmo_stats_reporter_set_max_class(srep2, OSMO_STATS_CLASS_SUBSCRIBER);
392 OSMO_ASSERT(rc >= 0);
393
Oliver Smithd89d35e2021-03-17 15:29:55 +0100394 fprintf(stderr, "report (initial):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200395 do_report(12, 8);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100396
Oliver Smithd89d35e2021-03-17 15:29:55 +0100397 fprintf(stderr, "report (srep1 global):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100398 /* force single flush */
399 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_GLOBAL);
400 srep1->force_single_flush = 1;
401 srep2->force_single_flush = 1;
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200402 do_report(6, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100403
Oliver Smithd89d35e2021-03-17 15:29:55 +0100404 fprintf(stderr, "report (srep1 peer):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100405 /* force single flush */
406 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_PEER);
407 srep1->force_single_flush = 1;
408 srep2->force_single_flush = 1;
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200409 do_report(6, 8);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100410
Oliver Smithd89d35e2021-03-17 15:29:55 +0100411 fprintf(stderr, "report (srep1 subscriber):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100412 /* force single flush */
413 osmo_stats_reporter_set_max_class(srep1, OSMO_STATS_CLASS_SUBSCRIBER);
414 srep1->force_single_flush = 1;
415 srep2->force_single_flush = 1;
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200416 do_report(12, 8);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100417
Oliver Smithd89d35e2021-03-17 15:29:55 +0100418 fprintf(stderr, "report (srep2 disabled):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100419 /* force single flush */
420 srep1->force_single_flush = 1;
421 srep2->force_single_flush = 1;
422 rc = osmo_stats_reporter_disable(srep2);
423 OSMO_ASSERT(rc >= 0);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200424 do_report(6, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100425
Oliver Smithd89d35e2021-03-17 15:29:55 +0100426 fprintf(stderr, "report (srep2 enabled, no flush forced):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100427 rc = osmo_stats_reporter_enable(srep2);
428 OSMO_ASSERT(rc >= 0);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200429 do_report(6, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100430
Oliver Smithd89d35e2021-03-17 15:29:55 +0100431 fprintf(stderr, "report (should be empty):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200432 do_report(0, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100433
Oliver Smithd89d35e2021-03-17 15:29:55 +0100434 fprintf(stderr, "report (group 1, counter 1 update):\n");
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200435 rate_ctr_inc(rate_ctr_group_get_ctr(ctrg1, TEST_A_CTR));
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200436 do_report(2, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100437
Oliver Smithd89d35e2021-03-17 15:29:55 +0100438 fprintf(stderr, "report (group 1, item 1 update):\n");
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200439 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 10);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200440 do_report(0, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100441
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200442 fprintf(stderr, "report (group 1, item 1 update twice, with same value):\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);
444 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 10);
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200445 do_report(0, 0);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100446
Oliver Smithd89d35e2021-03-17 15:29:55 +0100447 fprintf(stderr, "report (group 1, item 1 update twice, check max):\n");
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200448 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 20);
449 osmo_stat_item_set(osmo_stat_item_group_get_item(statg1, TEST_A_ITEM), 10);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200450 do_report(0, 2);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100451
Oliver Smitha79a5492021-08-19 10:07:44 +0200452 fprintf(stderr, "report (group 1, item 1 no update, send last item (!= last max), OS#5215):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200453 do_report(0, 2);
Oliver Smitha79a5492021-08-19 10:07:44 +0200454
455 fprintf(stderr, "report (group 1, item 1 no update, nothing to send):\n");
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200456 do_report(0, 0);
Oliver Smitha79a5492021-08-19 10:07:44 +0200457
Oliver Smithd89d35e2021-03-17 15:29:55 +0100458 fprintf(stderr, "report (remove statg1, ctrg1):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100459 /* force single flush */
460 srep1->force_single_flush = 1;
461 srep2->force_single_flush = 1;
462 osmo_stat_item_group_free(statg1);
463 rate_ctr_group_free(ctrg1);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200464 do_report(8, 4);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100465
Oliver Smithd89d35e2021-03-17 15:29:55 +0100466 fprintf(stderr, "report (remove srep1):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100467 /* force single flush */
468 srep1->force_single_flush = 1;
469 srep2->force_single_flush = 1;
470 osmo_stats_reporter_free(srep1);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200471 do_report(4, 2);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100472
Oliver Smithd89d35e2021-03-17 15:29:55 +0100473 fprintf(stderr, "report (remove statg2):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100474 /* force single flush */
475 srep2->force_single_flush = 1;
476 osmo_stat_item_group_free(statg2);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200477 do_report(4, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100478
Oliver Smithd89d35e2021-03-17 15:29:55 +0100479 fprintf(stderr, "report (remove srep2):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100480 /* force single flush */
481 srep2->force_single_flush = 1;
482 osmo_stats_reporter_free(srep2);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200483 do_report(0, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100484
Oliver Smithd89d35e2021-03-17 15:29:55 +0100485 fprintf(stderr, "report (remove ctrg2, should be empty):\n");
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100486 rate_ctr_group_free(ctrg2);
Neels Hofmeyr599601e2021-09-14 17:41:25 +0200487 do_report(0, 0);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100488
Harald Welte04c88122017-10-03 18:34:48 +0800489 rate_ctr_group_free(ctrg3);
490
Jacob Erlbeckf13de862015-11-10 11:36:58 +0100491 /* Leak check */
492 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
493 talloc_free(stats_ctx);
494
Oliver Smithd89d35e2021-03-17 15:29:55 +0100495 fprintf(stderr, "End test: %s\n", __func__);
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100496}
497
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200498int main(int argc, char **argv)
499{
Oliver Smithd89d35e2021-03-17 15:29:55 +0100500 void *ctx = talloc_named_const(NULL, 0, "main");
501 osmo_init_logging2(ctx, NULL);
502
503 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
504 log_set_print_level(osmo_stderr_target, 1);
505 log_set_print_category(osmo_stderr_target, 1);
506 log_set_print_category_hex(osmo_stderr_target, 0);
507 log_set_use_color(osmo_stderr_target, 0);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200508
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100509 osmo_stat_item_init(NULL);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200510
511 stat_test();
Jacob Erlbeck46b703d2015-11-09 17:25:27 +0100512 test_reporting();
Oliver Smithd89d35e2021-03-17 15:29:55 +0100513 talloc_free(ctx);
Jacob Erlbeck9732cb42015-10-01 20:43:53 +0200514 return 0;
515}