add osmo_stat_item_inc/osmo_stat_item_dec to set it relative

Change-Id: Id2462c4866bd22bc2338c9c8f69b775f88ae7511
This commit is contained in:
Alexander Couzens 2019-04-27 23:19:55 +02:00 committed by Harald Welte
parent aa98c481fa
commit cc72cc45a4
3 changed files with 42 additions and 0 deletions

View File

@ -79,6 +79,8 @@ static inline void osmo_stat_item_group_udp_idx(
void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);
void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value);
void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);
int osmo_stat_item_init(void *tall_ctx);

View File

@ -150,6 +150,30 @@ void osmo_stat_item_group_free(struct osmo_stat_item_group *grp)
talloc_free(grp);
}
/*! Increase the stat_item to the given value.
* This function adds a new value for the given stat_item at the end of
* the FIFO.
* \param[in] item The stat_item whose \a value we want to set
* \param[in] value The numeric value we want to store at end of FIFO
*/
void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value)
{
int32_t oldvalue = item->values[item->last_offs].value;
osmo_stat_item_set(item, oldvalue + value);
}
/*! Descrease the stat_item to the given value.
* This function adds a new value for the given stat_item at the end of
* the FIFO.
* \param[in] item The stat_item whose \a value we want to set
* \param[in] value The numeric value we want to store at end of FIFO
*/
void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value)
{
int32_t oldvalue = item->values[item->last_offs].value;
osmo_stat_item_set(item, oldvalue - value);
}
/*! Set the a given stat_item to the given value.
* This function adds a new value for the given stat_item at the end of
* the FIFO.

View File

@ -147,6 +147,22 @@ static void stat_test(void)
OSMO_ASSERT(value == 1000 + i);
}
/* check if dec & inc is working */
osmo_stat_item_set(statg->items[TEST_A_ITEM], 42);
rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
OSMO_ASSERT(rc > 0);
OSMO_ASSERT(value == 42);
osmo_stat_item_dec(statg->items[TEST_A_ITEM], 21);
rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
OSMO_ASSERT(rc > 0);
OSMO_ASSERT(value == 21);
osmo_stat_item_inc(statg->items[TEST_A_ITEM], 21);
rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);
OSMO_ASSERT(rc > 0);
OSMO_ASSERT(value == 42);
/* Keep 2 in FIFO */
osmo_stat_item_set(statg->items[TEST_A_ITEM], 33);
osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33);