]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
stats: more accurate interval handling
authorVictor Julien <victor@inliniac.net>
Sat, 8 Dec 2018 17:51:23 +0000 (18:51 +0100)
committerVictor Julien <victor@inliniac.net>
Sat, 16 Feb 2019 13:58:18 +0000 (14:58 +0100)
In the stats loop sleep for a time period more closely matching
the stats.interval setting. Fix an off by one that would make
the loop wake up ~1 second early.

Bug #2716

src/counters.c
src/util-time.h

index a13a1c445772e4e28ca54101561c18564f7e5226..0960a446b54cecab2ce1e3a1e81361c8b7c8b860 100644 (file)
@@ -328,7 +328,6 @@ static void *StatsMgmtThread(void *arg)
 
     ThreadVars *tv_local = (ThreadVars *)arg;
     uint8_t run = 1;
-    struct timespec cond_time;
 
     /* Set the thread name */
     if (SCSetThreadName(tv_local->name) < 0) {
@@ -369,8 +368,11 @@ static void *StatsMgmtThread(void *arg)
             TmThreadsUnsetFlag(tv_local, THV_PAUSED);
         }
 
-        cond_time.tv_sec = time(NULL) + stats_tts;
-        cond_time.tv_nsec = 0;
+        struct timeval cur_timev;
+        gettimeofday(&cur_timev, NULL);
+
+        struct timespec cond_time = FROM_TIMEVAL(cur_timev);
+        cond_time.tv_sec += (stats_tts);
 
         /* wait for the set time, or until we are woken up by
          * the shutdown procedure */
index 4b2c82e1768f36eb75fe1a7fb01b05f95a956af1..7b743a4e233a54f7b8a17570da36c4467d2bcac4 100644 (file)
@@ -40,6 +40,9 @@ void TimeDeinit(void);
 void TimeSetByThread(const int thread_id, const struct timeval *tv);
 void TimeGet(struct timeval *);
 
+/** \brief intialize a 'struct timespec' from a 'struct timeval'. */
+#define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 }
+
 #ifdef UNITTESTS
 void TimeSet(struct timeval *);
 void TimeSetToCurrentTime(void);