From f7c4600482322dc62304f3d98228da135f89954d Mon Sep 17 00:00:00 2001 From: Joshua Lumb Date: Fri, 17 Jul 2020 09:29:20 -0400 Subject: [PATCH] threads/runmode: Changes to thread config behaviour --- src/tm-threads.c | 16 ++++++++++++++++ src/tm-threads.h | 1 + src/util-runmodes.c | 29 +++++------------------------ 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/tm-threads.c b/src/tm-threads.c index 549af74baa..dd9a4fcec6 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -2177,6 +2177,22 @@ void TmThreadsGetMinimalTimestamp(struct timeval *ts) SCLogDebug("ts->tv_sec %"PRIuMAX, (uintmax_t)ts->tv_sec); } +uint16_t TmThreadsGetWorkerThreadMax() +{ + uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); + int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET); + /* always create at least one thread */ + if (thread_max == 0) + thread_max = ncpus * threading_detect_ratio; + if (thread_max < 1) + thread_max = 1; + if (thread_max > 1024) { + SCLogWarning(SC_ERR_RUNMODE, "limited number of 'worker' threads to 1024. Wanted %d", thread_max); + thread_max = 1024; + } + return thread_max; +} + /** * \retval r 1 if packet was accepted, 0 otherwise * \note if packet was not accepted, it's still the responsibility diff --git a/src/tm-threads.h b/src/tm-threads.h index 6d901e8f18..278e156bb5 100644 --- a/src/tm-threads.h +++ b/src/tm-threads.h @@ -243,6 +243,7 @@ int TmThreadsInjectPacketsById(Packet **, int id); void TmThreadsInitThreadsTimestamp(const struct timeval *ts); void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts); void TmThreadsGetMinimalTimestamp(struct timeval *ts); +uint16_t TmThreadsGetWorkerThreadMax(void); bool TmThreadsTimeSubsysIsReady(void); #endif /* __TM_THREADS_H__ */ diff --git a/src/util-runmodes.c b/src/util-runmodes.c index 2788ab1f03..889a459ca6 100644 --- a/src/util-runmodes.c +++ b/src/util-runmodes.c @@ -97,18 +97,8 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, char qname[TM_QUEUE_NAME_MAX]; /* Available cpus */ - uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); int nlive = LiveGetDeviceCount(); - int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET); - /* always create at least one thread */ - if (thread_max == 0) - thread_max = ncpus * threading_detect_ratio; - if (thread_max < 1) - thread_max = 1; - if (thread_max > 1024) { - SCLogWarning(SC_ERR_RUNMODE, "limited number of 'worker' threads to 1024. Wanted %d", thread_max); - thread_max = 1024; - } + uint16_t thread_max = TmThreadsGetWorkerThreadMax(); char *queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); if (queues == NULL) { @@ -129,7 +119,7 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, threads_count, recv_mod_name); /* create the threads */ - for (int thread = 0; thread < MIN(thread_max, threads_count); thread++) { + for (int thread = 0; thread < threads_count; thread++) { snprintf(tname, sizeof(tname), "%s#%02d", thread_name, thread+1); ThreadVars *tv_receive = TmThreadCreatePacketHandler(tname, @@ -266,11 +256,12 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod unsigned char single_mode) { int threads_count; + uint16_t thread_max = TmThreadsGetWorkerThreadMax(); if (single_mode) { threads_count = 1; } else { - threads_count = ModThreadsCount(aconf); + threads_count = MIN(ModThreadsCount(aconf), thread_max); SCLogInfo("Going to use %" PRId32 " thread(s)", threads_count); } @@ -418,19 +409,9 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser, TmModule *tm_module ; /* Available cpus */ - uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); const int nqueue = LiveGetDeviceCount(); - int thread_max = TmThreadGetNbThreads(WORKER_CPU_SET); - /* always create at least one thread */ - if (thread_max == 0) - thread_max = ncpus * threading_detect_ratio; - if (thread_max < 1) - thread_max = 1; - if (thread_max > 1024) { - SCLogWarning(SC_ERR_RUNMODE, "limited number of 'worker' threads to 1024. Wanted %d", thread_max); - thread_max = 1024; - } + uint16_t thread_max = TmThreadsGetWorkerThreadMax(); char *queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); if (queues == NULL) {