<p>ipse <strong>submitted</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/libosmocore/+/18141">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, but someone else must approve
  pespin: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">stats: Change timer to timerfd to make it a true interval timer.<br><br>Previously the interval between stats flushes would slowly increase<br>which would lead to reporting time jitter and confuse a timescale<br>database.<br><br>Change-Id: I23d8b5157ef8a9833ba16a81d9b28a126f303c30<br>---<br>M src/stats.c<br>1 file changed, 37 insertions(+), 7 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/stats.c b/src/stats.c</span><br><span>index 5954167..a5a4d4b 100644</span><br><span>--- a/src/stats.c</span><br><span>+++ b/src/stats.c</span><br><span>@@ -74,6 +74,7 @@</span><br><span> #include <errno.h></span><br><span> #include <stdio.h></span><br><span> #include <sys/types.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <inttypes.h></span><br><span> </span><br><span> #ifdef HAVE_SYS_SOCKET_H</span><br><span> #include <sys/socket.h></span><br><span>@@ -85,7 +86,7 @@</span><br><span> #include <osmocom/core/logging.h></span><br><span> #include <osmocom/core/rate_ctr.h></span><br><span> #include <osmocom/core/stat_item.h></span><br><span style="color: hsl(0, 100%, 40%);">-#include <osmocom/core/timer.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/select.h></span><br><span> #include <osmocom/core/counter.h></span><br><span> #include <osmocom/core/msgb.h></span><br><span> </span><br><span>@@ -102,7 +103,7 @@</span><br><span> };</span><br><span> struct osmo_stats_config *osmo_stats_config = &s_stats_config;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static struct osmo_timer_list osmo_stats_timer;</span><br><span style="color: hsl(120, 100%, 40%);">+static struct osmo_fd osmo_stats_timer = { .fd = -1 };</span><br><span> </span><br><span> static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,</span><br><span>    const struct rate_ctr_group *ctrg,</span><br><span>@@ -140,23 +141,52 @@</span><br><span>   return rc;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static void osmo_stats_timer_cb(void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-     int interval = osmo_stats_config->interval;</span><br><span style="color: hsl(120, 100%, 40%);">+        uint64_t expire_count;</span><br><span style="color: hsl(120, 100%, 40%);">+        int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* check that the timer has actually expired */</span><br><span style="color: hsl(120, 100%, 40%);">+       if (!(what & OSMO_FD_READ))</span><br><span style="color: hsl(120, 100%, 40%);">+               return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* read from timerfd: number of expirations of periodic timer */</span><br><span style="color: hsl(120, 100%, 40%);">+      rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count));</span><br><span style="color: hsl(120, 100%, 40%);">+      if (rc < 0 && errno == EAGAIN)</span><br><span style="color: hsl(120, 100%, 40%);">+             return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     OSMO_ASSERT(rc == sizeof(expire_count));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (expire_count > 1)</span><br><span style="color: hsl(120, 100%, 40%);">+              LOGP(DLSTATS, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                      expire_count, expire_count-1);</span><br><span> </span><br><span>   if (!llist_empty(&osmo_stats_reporter_list))</span><br><span>             osmo_stats_report();</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-        osmo_timer_schedule(&osmo_stats_timer, interval, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+      return 0;</span><br><span> }</span><br><span> </span><br><span> static int start_timer()</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+   int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+       int interval = osmo_stats_config->interval;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>     if (!is_initialised)</span><br><span>                 return -ESRCH;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-      osmo_timer_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL);</span><br><span style="color: hsl(0, 100%, 40%);">-     osmo_timer_schedule(&osmo_stats_timer, 0, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+     struct timespec ts_first = {.tv_sec=0, .tv_nsec=1000};</span><br><span style="color: hsl(120, 100%, 40%);">+        struct timespec ts_interval = {.tv_sec=interval, .tv_nsec=0};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       rc = osmo_timerfd_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (rc < 0)</span><br><span style="color: hsl(120, 100%, 40%);">+                LOGP(DLSTATS, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n",</span><br><span style="color: hsl(120, 100%, 40%);">+              rc, osmo_stats_timer.fd);</span><br><span style="color: hsl(120, 100%, 40%);">+        rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (rc < 0)</span><br><span style="color: hsl(120, 100%, 40%);">+                LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                  rc, osmo_stats_timer.fd, interval);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval);</span><br><span> </span><br><span>      return 0;</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/libosmocore/+/18141">change 18141</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/c/libosmocore/+/18141"/><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-Change-Id: I23d8b5157ef8a9833ba16a81d9b28a126f303c30 </div>
<div style="display:none"> Gerrit-Change-Number: 18141 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: ipse <Alexander.Chemeris@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: ipse <Alexander.Chemeris@gmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: pespin <pespin@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>