diff options
-rw-r--r-- | src/rate_ctr.c | 29 | ||||
-rw-r--r-- | tests/stats/stats_test.c | 10 |
2 files changed, 31 insertions, 8 deletions
diff --git a/src/rate_ctr.c b/src/rate_ctr.c index 72c6bb11..ec054c57 100644 --- a/src/rate_ctr.c +++ b/src/rate_ctr.c @@ -178,6 +178,27 @@ err_free: return NULL; } +/*! Find an unused index for this rate counter group. + * \param[in] name Name of the counter group + * \returns the largest used index number + 1, or 0 if none exist yet. */ +static unsigned int rate_ctr_get_unused_name_idx(const char *name) +{ + unsigned int idx = 0; + struct rate_ctr_group *ctrg; + + llist_for_each_entry(ctrg, &rate_ctr_groups, list) { + if (!ctrg->desc) + continue; + + if (strcmp(ctrg->desc->group_name_prefix, name)) + continue; + + if (idx <= ctrg->idx) + idx = ctrg->idx + 1; + } + return idx; +} + /*! Allocate a new group of counters according to description * \param[in] ctx \ref talloc context * \param[in] desc Rate counter group description @@ -191,9 +212,11 @@ struct rate_ctr_group *rate_ctr_group_alloc(void *ctx, struct rate_ctr_group *group; if (rate_ctr_get_group_by_name_idx(desc->group_name_prefix, idx)) { - LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' already exists for index %u\n", - desc->group_name_prefix, idx); - return NULL; /* group already exist */ + unsigned int new_idx = rate_ctr_get_unused_name_idx(desc->group_name_prefix); + LOGP(DLGLOBAL, LOGL_ERROR, "counter group '%s' already exists for index %u," + " instead using index %u. This is a software bug that needs fixing.\n", + desc->group_name_prefix, idx, new_idx); + idx = new_idx; } size = sizeof(struct rate_ctr_group) + diff --git a/tests/stats/stats_test.c b/tests/stats/stats_test.c index 35faf9a6..6ef88418 100644 --- a/tests/stats/stats_test.c +++ b/tests/stats/stats_test.c @@ -324,16 +324,16 @@ static void test_reporting() statg2 = osmo_stat_item_group_alloc(stats_ctx, &statg_desc, 2); OSMO_ASSERT(statg2 != NULL); ctrg1 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 1); - OSMO_ASSERT(ctrg1 != NULL); + OSMO_ASSERT(ctrg1 && ctrg1->idx == 1); ctrg2 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2); - OSMO_ASSERT(ctrg2 != NULL); + OSMO_ASSERT(ctrg2 && ctrg2->idx == 2); ctrg_dup = rate_ctr_group_alloc(stats_ctx, &ctrg_desc, 2); - if (ctrg_dup != NULL && ctrg2 != NULL) - printf("FAIL: successfully allocated already existing counter group!\n"); + OSMO_ASSERT(ctrg_dup && ctrg_dup->idx == 3); + rate_ctr_group_free(ctrg_dup); ctrg3 = rate_ctr_group_alloc(stats_ctx, &ctrg_desc_dot, 3); - OSMO_ASSERT(ctrg3 != NULL); + OSMO_ASSERT(ctrg3 && ctrg3->idx == 3); srep1 = stats_reporter_create_test("test1"); OSMO_ASSERT(srep1 != NULL); |