Change in libosmocore[master]: add osmo_stat_item_inc/osmo_stat_item_dec to set it relative

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

lynxis lazus gerrit-no-reply at lists.osmocom.org
Sat Apr 27 21:21:05 UTC 2019


lynxis lazus has uploaded this change for review. ( https://gerrit.osmocom.org/13801


Change subject: add osmo_stat_item_inc/osmo_stat_item_dec to set it relative
......................................................................

add osmo_stat_item_inc/osmo_stat_item_dec to set it relative

Change-Id: Id2462c4866bd22bc2338c9c8f69b775f88ae7511
---
M include/osmocom/core/stat_item.h
M src/stat_item.c
M tests/stats/stats_test.c
3 files changed, 42 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/01/13801/1

diff --git a/include/osmocom/core/stat_item.h b/include/osmocom/core/stat_item.h
index 260ffb1..806173a 100644
--- a/include/osmocom/core/stat_item.h
+++ b/include/osmocom/core/stat_item.h
@@ -79,6 +79,8 @@
 
 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);
diff --git a/src/stat_item.c b/src/stat_item.c
index cb9b90f..6716575 100644
--- a/src/stat_item.c
+++ b/src/stat_item.c
@@ -150,6 +150,30 @@
 	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.
diff --git a/tests/stats/stats_test.c b/tests/stats/stats_test.c
index 6ef8841..71f710a 100644
--- a/tests/stats/stats_test.c
+++ b/tests/stats/stats_test.c
@@ -147,6 +147,22 @@
 		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);

-- 
To view, visit https://gerrit.osmocom.org/13801
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id2462c4866bd22bc2338c9c8f69b775f88ae7511
Gerrit-Change-Number: 13801
Gerrit-PatchSet: 1
Gerrit-Owner: lynxis lazus <lynxis at fe80.eu>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190427/bfd6210f/attachment.htm>


More information about the gerrit-log mailing list