From 0e21c71490aa2612ab7014e123170d240d75f638 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sun, 15 Sep 2024 15:53:24 +0200 Subject: [PATCH] threads: track wall clock time precisely Issue warning if thread sleeps for a long time. --- src/tm-threads.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/tm-threads.c b/src/tm-threads.c index 84ef7d34ab..b0d0f8686b 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -2066,7 +2066,7 @@ typedef struct Thread_ { SCTime_t pktts; /**< current packet time of this thread * (offline mode) */ - uint32_t sys_sec_stamp; /**< timestamp in seconds of the real system + SCTime_t sys_sec_stamp; /**< timestamp in real system * time when the pktts was last updated. */ } Thread; @@ -2190,14 +2190,24 @@ void TmThreadsSetThreadTimestamp(const int id, const SCTime_t ts) int idx = id - 1; Thread *t = &thread_store.threads[idx]; t->pktts = ts; - struct timeval systs; - gettimeofday(&systs, NULL); - t->sys_sec_stamp = (uint32_t)systs.tv_sec; + SCTime_t now = SCTimeGetTime(); + +#ifdef DEBUG + if (t->sys_sec_stamp.secs != 0) { + SCTime_t tmpts = SCTIME_ADD_SECS(t->sys_sec_stamp, 3); + if (SCTIME_CMP_LT(tmpts, now)) { + SCLogDebug("%s: thread slept for %u secs", t->name, (uint32_t)(now.secs - tmpts.secs)); + } + } +#endif + + t->sys_sec_stamp = now; SCMutexUnlock(&thread_store_lock); } bool TmThreadsTimeSubsysIsReady(void) { + static SCTime_t nullts = SCTIME_INITIALIZER; bool ready = true; SCMutexLock(&thread_store_lock); for (size_t s = 0; s < thread_store.threads_size; s++) { @@ -2206,7 +2216,7 @@ bool TmThreadsTimeSubsysIsReady(void) break; if (t->type != TVT_PPT) continue; - if (t->sys_sec_stamp == 0) { + if (SCTIME_CMP_EQ(t->sys_sec_stamp, nullts)) { ready = false; break; } @@ -2217,8 +2227,7 @@ bool TmThreadsTimeSubsysIsReady(void) void TmThreadsInitThreadsTimestamp(const SCTime_t ts) { - struct timeval systs; - gettimeofday(&systs, NULL); + SCTime_t now = SCTimeGetTime(); SCMutexLock(&thread_store_lock); for (size_t s = 0; s < thread_store.threads_size; s++) { Thread *t = &thread_store.threads[s]; @@ -2227,7 +2236,7 @@ void TmThreadsInitThreadsTimestamp(const SCTime_t ts) if (t->type != TVT_PPT) continue; t->pktts = ts; - t->sys_sec_stamp = (uint32_t)systs.tv_sec; + t->sys_sec_stamp = now; } SCMutexUnlock(&thread_store_lock); } @@ -2235,11 +2244,10 @@ void TmThreadsInitThreadsTimestamp(const SCTime_t ts) void TmThreadsGetMinimalTimestamp(struct timeval *ts) { struct timeval local = { 0 }; - static struct timeval nullts; + static SCTime_t nullts = SCTIME_INITIALIZER; bool set = false; size_t s; - struct timeval systs; - gettimeofday(&systs, NULL); + SCTime_t now = SCTimeGetTime(); SCMutexLock(&thread_store_lock); for (s = 0; s < thread_store.threads_size; s++) { @@ -2249,11 +2257,10 @@ void TmThreadsGetMinimalTimestamp(struct timeval *ts) /* only packet threads set timestamps based on packets */ if (t->type != TVT_PPT) continue; - struct timeval pkttv = { .tv_sec = SCTIME_SECS(t->pktts), - .tv_usec = SCTIME_USECS(t->pktts) }; - if (!(timercmp(&pkttv, &nullts, ==))) { + if (SCTIME_CMP_NEQ(t->pktts, nullts)) { + SCTime_t sys_sec_stamp = SCTIME_ADD_SECS(t->sys_sec_stamp, 1); /* ignore sleeping threads */ - if (t->sys_sec_stamp + 1 < (uint32_t)systs.tv_sec) + if (SCTIME_CMP_LT(sys_sec_stamp, now)) continue; if (!set) { -- 2.47.2