]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
stats: improve sync signalling 9882/head
authorVictor Julien <vjulien@oisf.net>
Wed, 22 Nov 2023 08:31:38 +0000 (09:31 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 24 Nov 2023 10:57:11 +0000 (11:57 +0100)
Make syncs more reliable by using a atomic "sync now" variable and
signalling the conditions under lock.

Ticket: #6569.

src/counters.c
src/counters.h

index 56332430d8a21df003225c7df6c4d5f38ce1663f..790f416ba050274aecce457c91c6af239a762a48 100644 (file)
@@ -130,7 +130,7 @@ static void StatsPublicThreadContextCleanup(StatsPublicThreadContext *t)
     SCMutexLock(&t->m);
     StatsReleaseCounters(t->head);
     t->head = NULL;
-    t->perf_flag = 0;
+    SC_ATOMIC_SET(t->sync_now, false);
     t->curr_id = 0;
     SCMutexUnlock(&t->m);
     SCMutexDestroy(&t->m);
@@ -460,7 +460,7 @@ void StatsSyncCounters(ThreadVars *tv)
 
 void StatsSyncCountersIfSignalled(ThreadVars *tv)
 {
-    if (tv->perf_public_ctx.perf_flag == 1) {
+    if (SC_ATOMIC_GET(tv->perf_public_ctx.sync_now) == true) {
         StatsUpdateCounterArray(&tv->perf_private_ctx, &tv->perf_public_ctx);
     }
 }
@@ -521,13 +521,13 @@ static void *StatsWakeupThread(void *arg)
                 continue;
             }
 
-            /* assuming the assignment of an int to be atomic, and even if it's
-             * not, it should be okay */
-            tv->perf_public_ctx.perf_flag = 1;
+            SC_ATOMIC_SET(tv->perf_public_ctx.sync_now, true);
 
             if (tv->inq != NULL) {
                 PacketQueue *q = tv->inq->pq;
+                SCMutexLock(&q->mutex_q);
                 SCCondSignal(&q->cond_q);
+                SCMutexUnlock(&q->mutex_q);
             }
 
             tv = tv->next;
@@ -541,9 +541,7 @@ static void *StatsWakeupThread(void *arg)
                 continue;
             }
 
-            /* assuming the assignment of an int to be atomic, and even if it's
-             * not, it should be okay */
-            tv->perf_public_ctx.perf_flag = 1;
+            SC_ATOMIC_SET(tv->perf_public_ctx.sync_now, true);
 
             tv = tv->next;
         }
@@ -1256,7 +1254,7 @@ int StatsUpdateCounterArray(StatsPrivateThreadContext *pca, StatsPublicThreadCon
     }
     SCMutexUnlock(&pctx->m);
 
-    pctx->perf_flag = 0;
+    SC_ATOMIC_SET(pctx->sync_now, false);
     return 1;
 }
 
index 100fec94f7e626a406ea9c79b55cd1b7315011d1..b3ffddbd569f415d458b13834ed20ae044b71093 100644 (file)
@@ -63,7 +63,7 @@ typedef struct StatsCounter_ {
  */
 typedef struct StatsPublicThreadContext_ {
     /* flag set by the wakeup thread, to inform the client threads to sync */
-    uint32_t perf_flag;
+    SC_ATOMIC_DECLARE(bool, sync_now);
 
     /* pointer to the head of a list of counters assigned under this context */
     StatsCounter *head;