]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
threads: track wall clock time precisely
authorVictor Julien <vjulien@oisf.net>
Sun, 15 Sep 2024 13:53:24 +0000 (15:53 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 25 Oct 2024 05:47:11 +0000 (07:47 +0200)
Issue warning if thread sleeps for a long time.

src/tm-threads.c

index 84ef7d34abee70ef60bdd7520776131aabd82e82..b0d0f8686ba005ae4c05dfc4b1073ceb3b79052f 100644 (file)
@@ -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) {