<p>Harald Welte <strong>merged</strong> this change.</p><p><a href="https://gerrit.osmocom.org/13801">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Harald Welte: Looks good to me, approved
  Daniel Willmann: Looks good to me, but someone else must approve
  Mykola Shchetinin: Looks good to me, approved
  Jenkins Builder: Verified

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">add osmo_stat_item_inc/osmo_stat_item_dec to set it relative<br><br>Change-Id: Id2462c4866bd22bc2338c9c8f69b775f88ae7511<br>---<br>M include/osmocom/core/stat_item.h<br>M src/stat_item.c<br>M tests/stats/stats_test.c<br>3 files changed, 42 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/core/stat_item.h b/include/osmocom/core/stat_item.h</span><br><span>index 260ffb1..806173a 100644</span><br><span>--- a/include/osmocom/core/stat_item.h</span><br><span>+++ b/include/osmocom/core/stat_item.h</span><br><span>@@ -79,6 +79,8 @@</span><br><span> </span><br><span> void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);</span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value);</span><br><span> void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value);</span><br><span> </span><br><span> int osmo_stat_item_init(void *tall_ctx);</span><br><span>diff --git a/src/stat_item.c b/src/stat_item.c</span><br><span>index cb9b90f..6716575 100644</span><br><span>--- a/src/stat_item.c</span><br><span>+++ b/src/stat_item.c</span><br><span>@@ -150,6 +150,30 @@</span><br><span>         talloc_free(grp);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Increase the stat_item to the given value.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  This function adds a new value for the given stat_item at the end of</span><br><span style="color: hsl(120, 100%, 40%);">+ *  the FIFO.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] item The stat_item whose \a value we want to set</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] value The numeric value we want to store at end of FIFO</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     int32_t oldvalue = item->values[item->last_offs].value;</span><br><span style="color: hsl(120, 100%, 40%);">+ osmo_stat_item_set(item, oldvalue + value);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Descrease the stat_item to the given value.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  This function adds a new value for the given stat_item at the end of</span><br><span style="color: hsl(120, 100%, 40%);">+ *  the FIFO.</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] item The stat_item whose \a value we want to set</span><br><span style="color: hsl(120, 100%, 40%);">+ *  \param[in] value The numeric value we want to store at end of FIFO</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_stat_item_dec(struct osmo_stat_item *item, int32_t value)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      int32_t oldvalue = item->values[item->last_offs].value;</span><br><span style="color: hsl(120, 100%, 40%);">+ osmo_stat_item_set(item, oldvalue - value);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! Set the a given stat_item to the given value.</span><br><span>  *  This function adds a new value for the given stat_item at the end of</span><br><span>  *  the FIFO.</span><br><span>diff --git a/tests/stats/stats_test.c b/tests/stats/stats_test.c</span><br><span>index 6ef8841..71f710a 100644</span><br><span>--- a/tests/stats/stats_test.c</span><br><span>+++ b/tests/stats/stats_test.c</span><br><span>@@ -147,6 +147,22 @@</span><br><span>               OSMO_ASSERT(value == 1000 + i);</span><br><span>      }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* check if dec & inc is working */</span><br><span style="color: hsl(120, 100%, 40%);">+       osmo_stat_item_set(statg->items[TEST_A_ITEM], 42);</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(rc > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+       OSMO_ASSERT(value == 42);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   osmo_stat_item_dec(statg->items[TEST_A_ITEM], 21);</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(rc > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+       OSMO_ASSERT(value == 21);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   osmo_stat_item_inc(statg->items[TEST_A_ITEM], 21);</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = osmo_stat_item_get_next(statg->items[TEST_A_ITEM], &rd_a, &value);</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(rc > 0);</span><br><span style="color: hsl(120, 100%, 40%);">+       OSMO_ASSERT(value == 42);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  /* Keep 2 in FIFO */</span><br><span>         osmo_stat_item_set(statg->items[TEST_A_ITEM], 33);</span><br><span>        osmo_stat_item_set(statg->items[TEST_B_ITEM], 1000 + 33);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/13801">change 13801</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/13801"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Id2462c4866bd22bc2338c9c8f69b775f88ae7511 </div>
<div style="display:none"> Gerrit-Change-Number: 13801 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: lynxis lazus <lynxis@fe80.eu> </div>
<div style="display:none"> Gerrit-Reviewer: Daniel Willmann <dwillmann@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Harald Welte <laforge@gnumonks.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder (1000002) </div>
<div style="display:none"> Gerrit-Reviewer: Mykola Shchetinin <mykola@pentonet.com> </div>
<div style="display:none"> Gerrit-Reviewer: Pau Espin Pedrol <pespin@sysmocom.de> </div>