]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
threads: give threads more time to get ready 11181/head 11202/head
authorVictor Julien <vjulien@oisf.net>
Wed, 29 May 2024 05:03:24 +0000 (07:03 +0200)
committerVictor Julien <vjulien@oisf.net>
Thu, 30 May 2024 07:38:04 +0000 (09:38 +0200)
In certain conditions, it can take a long time for threads to start up.
For example in af-packet, setting up the socket, rings, etc has been
observed to take close to half a second per thread, and since the
threads go one by one in a preset order, this means the start up can
take a lot of time if there are many threads. The old logic would just
allow a hard coded 60s. This was not always enough when the number of
threads was high.

This patch makes the wait time take the number of threads into account.
It adds a second of time budget to the base 60s for each thread.

So as an example, if a system has 112 af-packet threads, it would wait
172 seconds (60 + 112) for the threads to get ready.

Ticket: #7048.
(cherry picked from commit 41b9836b11bbd653953f5c5dc5f87875e15fae8d)

src/tm-threads.c

index 0c7b37d63883c6c44bba849d2785e2992297c1c2..f8eb88a7b48566a7971a4e1e50b44e6190d80ace 100644 (file)
@@ -1803,10 +1803,20 @@ static TmEcode WaitOnThreadsRunningByType(const int t)
 {
     struct timeval start_ts;
     struct timeval cur_ts;
-    gettimeofday(&start_ts, NULL);
+    uint32_t thread_cnt = 0;
 
     /* on retries, this will init to the last thread that started up already */
     ThreadVars *tv_start = tv_root[t];
+    SCMutexLock(&tv_root_lock);
+    for (ThreadVars *tv = tv_start; tv != NULL; tv = tv->next) {
+        thread_cnt++;
+    }
+    SCMutexUnlock(&tv_root_lock);
+
+    /* give threads a second each to start up, plus a margin of a minute. */
+    uint32_t time_budget = 60 + thread_cnt;
+
+    gettimeofday(&start_ts, NULL);
 again:
     SCMutexLock(&tv_root_lock);
     ThreadVars *tv = tv_start;
@@ -1826,10 +1836,10 @@ again:
             /* 60 seconds provided for the thread to transition from
              * THV_INIT_DONE to THV_RUNNING */
             gettimeofday(&cur_ts, NULL);
-            if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) {
+            if (((uint32_t)cur_ts.tv_sec - (uint32_t)start_ts.tv_sec) > time_budget) {
                 SCLogError("thread \"%s\" failed to "
-                           "start in time: flags %04x",
-                        tv->name, SC_ATOMIC_GET(tv->flags));
+                           "start in time: flags %04x. Total threads: %u. Time budget %us",
+                        tv->name, SC_ATOMIC_GET(tv->flags), thread_cnt, time_budget);
                 return TM_ECODE_FAILED;
             }