Skip to content

Commit

Permalink
threads: move wait for unpause outside of loop
Browse files Browse the repository at this point in the history
Threads are only set to paused upon initialization and never again, we
should only have to wait once, so move the wait before any loop that
was waiting before.

Additionally, if the thread was killed while waiting to be unpaused,
don't enter the loop.
  • Loading branch information
jasonish authored and victorjulien committed Oct 12, 2024
1 parent 7492fb6 commit 15c4eb3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 33 deletions.
10 changes: 4 additions & 6 deletions src/counters.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,8 @@ static void *StatsMgmtThread(void *arg)
SCLogDebug("stats_thread_data %p", &stats_thread_data);

TmThreadsSetFlag(tv_local, THV_INIT_DONE | THV_RUNNING);
while (1) {
TmThreadsWaitForUnpause(tv_local);

bool run = TmThreadsWaitForUnpause(tv_local);
while (run) {
struct timeval cur_timev;
gettimeofday(&cur_timev, NULL);
struct timespec cond_time = FROM_TIMEVAL(cur_timev);
Expand Down Expand Up @@ -483,10 +482,9 @@ static void *StatsWakeupThread(void *arg)
}

TmThreadsSetFlag(tv_local, THV_INIT_DONE | THV_RUNNING);
bool run = TmThreadsWaitForUnpause(tv_local);

while (1) {
TmThreadsWaitForUnpause(tv_local);

while (run) {
struct timeval cur_timev;
gettimeofday(&cur_timev, NULL);
struct timespec cond_time = FROM_TIMEVAL(cur_timev);
Expand Down
6 changes: 2 additions & 4 deletions src/detect-engine-loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,8 @@ static TmEcode DetectLoader(ThreadVars *th_v, void *thread_data)

TmThreadsSetFlag(th_v, THV_INIT_DONE | THV_RUNNING);
SCLogDebug("loader thread started");
while (1)
{
TmThreadsWaitForUnpause(th_v);

bool run = TmThreadsWaitForUnpause(th_v);
while (run) {
/* see if we have tasks */

DetectLoaderControl *loader = &loaders[ftd->instance];
Expand Down
4 changes: 2 additions & 2 deletions src/flow-bypass.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ static TmEcode BypassedFlowManager(ThreadVars *th_v, void *thread_data)
return TM_ECODE_OK;

TmThreadsSetFlag(th_v, THV_RUNNING);
bool run = TmThreadsWaitForUnpause(th_v);

while (1) {
TmThreadsWaitForUnpause(th_v);
while (run) {
SCLogDebug("Dumping the table");
gettimeofday(&tv, NULL);
TIMEVAL_TO_TIMESPEC(&tv, &curtime);
Expand Down
11 changes: 4 additions & 7 deletions src/flow-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,11 +817,9 @@ static TmEcode FlowManager(ThreadVars *th_v, void *thread_data)
StatsSetUI64(th_v, ftd->cnt.flow_mgr_rows_sec, rows_sec);

TmThreadsSetFlag(th_v, THV_RUNNING);
bool run = TmThreadsWaitForUnpause(th_v);

while (1)
{
TmThreadsWaitForUnpause(th_v);

while (run) {
bool emerg = ((SC_ATOMIC_GET(flow_flags) & FLOW_EMERGENCY) != 0);

/* Get the time */
Expand Down Expand Up @@ -1078,10 +1076,9 @@ static TmEcode FlowRecycler(ThreadVars *th_v, void *thread_data)
FlowQueuePrivate ret_queue = { NULL, NULL, 0 };

TmThreadsSetFlag(th_v, THV_RUNNING);
bool run = TmThreadsWaitForUnpause(th_v);

while (1)
{
TmThreadsWaitForUnpause(th_v);
while (run) {
SC_ATOMIC_ADD(flowrec_busy,1);
FlowQueuePrivate list = FlowQueueExtractPrivate(&flow_recycle_q);

Expand Down
24 changes: 11 additions & 13 deletions src/tm-threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ static void *TmThreadsSlotPktAcqLoop(void *td)
{
ThreadVars *tv = (ThreadVars *)td;
TmSlot *s = tv->tm_slots;
char run = 1;
TmEcode r = TM_ECODE_OK;
TmSlot *slot = NULL;

Expand Down Expand Up @@ -303,21 +302,20 @@ static void *TmThreadsSlotPktAcqLoop(void *td)
StatsSetupPrivate(tv);

TmThreadsSetFlag(tv, THV_INIT_DONE);
bool run = TmThreadsWaitForUnpause(tv);

while(run) {
TmThreadsWaitForUnpause(tv);

while (run) {
r = s->PktAcqLoop(tv, SC_ATOMIC_GET(s->slot_data), s);

if (r == TM_ECODE_FAILED) {
TmThreadsSetFlag(tv, THV_FAILED);
run = 0;
run = false;
}
if (TmThreadsCheckFlag(tv, THV_KILL_PKTACQ) || suricata_ctl_flags) {
run = 0;
run = false;
}
if (r == TM_ECODE_DONE) {
run = 0;
run = false;
}
}
StatsSyncCounters(tv);
Expand Down Expand Up @@ -361,7 +359,7 @@ static void *TmThreadsSlotPktAcqLoop(void *td)
/**
* Also returns if the kill flag is set.
*/
void TmThreadsWaitForUnpause(ThreadVars *tv)
bool TmThreadsWaitForUnpause(ThreadVars *tv)
{
if (TmThreadsCheckFlag(tv, THV_PAUSE)) {
TmThreadsSetFlag(tv, THV_PAUSED);
Expand All @@ -370,19 +368,20 @@ void TmThreadsWaitForUnpause(ThreadVars *tv)
SleepUsec(100);

if (TmThreadsCheckFlag(tv, THV_KILL))
break;
return false;
}

TmThreadsUnsetFlag(tv, THV_PAUSED);
}

return true;
}

static void *TmThreadsSlotVar(void *td)
{
ThreadVars *tv = (ThreadVars *)td;
TmSlot *s = (TmSlot *)tv->tm_slots;
Packet *p = NULL;
char run = 1;
TmEcode r = TM_ECODE_OK;

CaptureStatsSetup(tv);
Expand Down Expand Up @@ -453,12 +452,11 @@ static void *TmThreadsSlotVar(void *td)
// enter infinite loops. They use this as the core loop. As a result, at this
// point the worker threads can be considered both initialized and running.
TmThreadsSetFlag(tv, THV_INIT_DONE | THV_RUNNING);
bool run = TmThreadsWaitForUnpause(tv);

s = (TmSlot *)tv->tm_slots;

while (run) {
TmThreadsWaitForUnpause(tv);

/* input a packet */
p = tv->tmqh_in(tv);

Expand Down Expand Up @@ -490,7 +488,7 @@ static void *TmThreadsSlotVar(void *td)
}

if (TmThreadsCheckFlag(tv, THV_KILL)) {
run = 0;
run = false;
}
} /* while (run) */
StatsSyncCounters(tv);
Expand Down
4 changes: 3 additions & 1 deletion src/tm-threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ bool TmThreadsTimeSubsysIsReady(void);
*
* Check if a thread should wait to be unpaused and wait if so, or
* until the thread kill flag is set.
*
* \returns true if the thread was unpaused, false if killed.
*/
void TmThreadsWaitForUnpause(ThreadVars *tv);
bool TmThreadsWaitForUnpause(ThreadVars *tv);

#endif /* SURICATA_TM_THREADS_H */

0 comments on commit 15c4eb3

Please sign in to comment.