From: Victor Julien Date: Sat, 8 Dec 2018 17:51:23 +0000 (+0100) Subject: stats: more accurate interval handling X-Git-Tag: suricata-4.0.7~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc924f65c2bf7b45bdfbb4be47f5c68f6018cb12;p=thirdparty%2Fsuricata.git stats: more accurate interval handling 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 --- diff --git a/src/counters.c b/src/counters.c index a13a1c4457..0960a446b5 100644 --- a/src/counters.c +++ b/src/counters.c @@ -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 */ diff --git a/src/util-time.h b/src/util-time.h index 4b2c82e176..7b743a4e23 100644 --- a/src/util-time.h +++ b/src/util-time.h @@ -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);