Skip to content

Commit 0948a32

Browse files
committed
runmodes: limit thread count consistently
Limit to 1024 like with worker threads. General minor cleanups.
1 parent f6b67a8 commit 0948a32

1 file changed

Lines changed: 34 additions & 46 deletions

File tree

src/util-runmodes.c

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
#include "flow-hash.h"
5050

51+
#define THREADS_MAX (uint16_t)1024
52+
5153
/** \brief create a queue string for autofp to pass to
5254
* the flow queue handler.
5355
*
@@ -106,13 +108,13 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
106108
FatalError("Failed to allocate config for %s", live_dev);
107109
}
108110

109-
int threads_count = ModThreadsCount(aconf);
110-
SCLogInfo("Going to use %" PRId32 " %s receive thread(s)",
111-
threads_count, recv_mod_name);
111+
const uint16_t threads_count = MIN(ModThreadsCount(aconf), THREADS_MAX);
112+
SCLogInfo("Going to use %" PRIu16 " %s receive thread(s)", threads_count, recv_mod_name);
112113

113114
/* create the threads */
114-
for (int thread = 0; thread < threads_count; thread++) {
115-
snprintf(tname, sizeof(tname), "%s#%02d", thread_name, thread+1);
115+
for (uint16_t thread = 0; thread < threads_count; thread++) {
116+
const uint16_t thread_id = (uint16_t)(thread + 1);
117+
snprintf(tname, sizeof(tname), "%s#%02u", thread_name, thread_id);
116118
ThreadVars *tv_receive =
117119
TmThreadCreatePacketHandler(tname,
118120
"packetpool", "packetpool",
@@ -155,17 +157,18 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
155157
FatalError("Multidev: Failed to allocate config for %s (%d)", dev, lthread);
156158
}
157159

158-
uint16_t threads_count = ModThreadsCount(aconf);
160+
const uint16_t threads_count = MIN(ModThreadsCount(aconf), THREADS_MAX);
159161
for (uint16_t thread = 0; thread < threads_count; thread++) {
162+
const uint16_t thread_id = (uint16_t)(thread + 1);
160163
const size_t printable_threadname_size = strlen(thread_name) + 5 + strlen(dev) + 1;
161164
char *printable_threadname = SCMalloc(printable_threadname_size);
162165
if (unlikely(printable_threadname == NULL)) {
163166
FatalError("failed to alloc printable thread name: %s", strerror(errno));
164167
}
165-
snprintf(tname, sizeof(tname), "%s#%02u-%s", thread_name, (uint16_t)(thread + 1),
166-
visual_devname);
168+
snprintf(
169+
tname, sizeof(tname), "%s#%02u-%s", thread_name, thread_id, visual_devname);
167170
snprintf(printable_threadname, printable_threadname_size, "%s#%02u-%s", thread_name,
168-
(uint16_t)(thread + 1), dev);
171+
thread_id, dev);
169172

170173
ThreadVars *tv_receive =
171174
TmThreadCreatePacketHandler(tname,
@@ -202,9 +205,9 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
202205
}
203206

204207
for (uint16_t thread = 0; thread < thread_max; thread++) {
205-
snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, (uint16_t)(thread + 1));
206-
snprintf(qname, sizeof(qname), "pickup%u", (uint16_t)(thread + 1));
207-
208+
const uint16_t thread_id = (uint16_t)(thread + 1);
209+
snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread_id);
210+
snprintf(qname, sizeof(qname), "pickup%u", thread_id);
208211
SCLogDebug("tname %s, qname %s", tname, qname);
209212

210213
ThreadVars *tv_detect_ncpu =
@@ -249,7 +252,7 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod
249252
unsigned char single_mode)
250253
{
251254
uint16_t threads_count;
252-
uint16_t thread_max = TmThreadsGetWorkerThreadMax();
255+
const uint16_t thread_max = TmThreadsGetWorkerThreadMax();
253256

254257
if (single_mode) {
255258
threads_count = 1;
@@ -261,8 +264,7 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod
261264

262265
/* create the threads */
263266
for (uint16_t thread = 0; thread < threads_count; thread++) {
264-
char tname[TM_THREAD_NAME_MAX];
265-
TmModule *tm_module = NULL;
267+
const uint16_t thread_id = (uint16_t)(thread + 1);
266268
const char *visual_devname = LiveGetShortName(live_dev);
267269
const size_t printable_threadname_size = strlen(thread_name) + 5 + strlen(live_dev) + 1;
268270
char *printable_threadname = SCMalloc(printable_threadname_size);
@@ -271,15 +273,15 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod
271273
exit(EXIT_FAILURE);
272274
}
273275

276+
char tname[TM_THREAD_NAME_MAX];
274277
if (single_mode) {
275278
snprintf(tname, sizeof(tname), "%s#01-%s", thread_name, visual_devname);
276279
snprintf(printable_threadname, printable_threadname_size, "%s#01-%s", thread_name,
277280
live_dev);
278281
} else {
279-
snprintf(tname, sizeof(tname), "%s#%02u-%s", thread_name, (uint16_t)(thread + 1),
280-
visual_devname);
282+
snprintf(tname, sizeof(tname), "%s#%02u-%s", thread_name, thread_id, visual_devname);
281283
snprintf(printable_threadname, printable_threadname_size, "%s#%02u-%s", thread_name,
282-
(uint16_t)(thread + 1), live_dev);
284+
thread_id, live_dev);
283285
}
284286
ThreadVars *tv = TmThreadCreatePacketHandler(tname,
285287
"packetpool", "packetpool",
@@ -294,7 +296,7 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod
294296
FatalError("Failed to allocate memory for iface name");
295297
}
296298

297-
tm_module = TmModuleGetByName(recv_mod_name);
299+
TmModule *tm_module = TmModuleGetByName(recv_mod_name);
298300
if (tm_module == NULL) {
299301
FatalError("TmModuleGetByName failed for %s", recv_mod_name);
300302
}
@@ -332,26 +334,21 @@ int RunModeSetLiveCaptureWorkers(ConfigIfaceParserFunc ConfigParser,
332334
ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name,
333335
const char *decode_mod_name, const char *thread_name, const char *live_dev)
334336
{
335-
int nlive = LiveGetDeviceCount();
336-
void *aconf;
337-
int ldev;
337+
const int nlive = LiveGetDeviceCount();
338338

339-
for (ldev = 0; ldev < nlive; ldev++) {
339+
for (int ldev = 0; ldev < nlive; ldev++) {
340340
const char *live_dev_c = NULL;
341+
void *aconf;
342+
341343
if ((nlive <= 1) && (live_dev != NULL)) {
342344
aconf = ConfigParser(live_dev);
343345
live_dev_c = live_dev;
344346
} else {
345347
live_dev_c = LiveGetDeviceName(ldev);
346348
aconf = ConfigParser(live_dev_c);
347349
}
348-
RunModeSetLiveCaptureWorkersForDevice(ModThreadsCount,
349-
recv_mod_name,
350-
decode_mod_name,
351-
thread_name,
352-
live_dev_c,
353-
aconf,
354-
0);
350+
RunModeSetLiveCaptureWorkersForDevice(
351+
ModThreadsCount, recv_mod_name, decode_mod_name, thread_name, live_dev_c, aconf, 0);
355352
}
356353

357354
return 0;
@@ -363,7 +360,7 @@ int RunModeSetLiveCaptureSingle(ConfigIfaceParserFunc ConfigParser,
363360
const char *decode_mod_name, const char *thread_name,
364361
const char *live_dev)
365362
{
366-
int nlive = LiveGetDeviceCount();
363+
const int nlive = LiveGetDeviceCount();
367364
const char *live_dev_c = NULL;
368365
void *aconf;
369366

@@ -380,13 +377,7 @@ int RunModeSetLiveCaptureSingle(ConfigIfaceParserFunc ConfigParser,
380377
}
381378

382379
return RunModeSetLiveCaptureWorkersForDevice(
383-
ModThreadsCount,
384-
recv_mod_name,
385-
decode_mod_name,
386-
thread_name,
387-
live_dev_c,
388-
aconf,
389-
1);
380+
ModThreadsCount, recv_mod_name, decode_mod_name, thread_name, live_dev_c, aconf, 1);
390381
}
391382

392383

@@ -401,10 +392,8 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser,
401392
char tname[TM_THREAD_NAME_MAX];
402393
TmModule *tm_module ;
403394

404-
/* Available cpus */
405395
const int nqueue = LiveGetDeviceCount();
406-
407-
uint16_t thread_max = TmThreadsGetWorkerThreadMax();
396+
const uint16_t thread_max = TmThreadsGetWorkerThreadMax();
408397

409398
char *queues = RunmodeAutoFpCreatePickupQueuesString(thread_max);
410399
if (queues == NULL) {
@@ -447,10 +436,10 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser,
447436

448437
}
449438
for (uint16_t thread = 0; thread < thread_max; thread++) {
450-
snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, (uint16_t)(thread + 1));
439+
const uint16_t thread_id = (uint16_t)(thread + 1);
440+
snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread_id);
451441
char qname[TM_QUEUE_NAME_MAX];
452-
snprintf(qname, sizeof(qname), "pickup%u", (uint16_t)(thread + 1));
453-
442+
snprintf(qname, sizeof(qname), "pickup%u", thread_id);
454443
SCLogDebug("tname %s, qname %s", tname, qname);
455444

456445
ThreadVars *tv_detect_ncpu =
@@ -520,7 +509,6 @@ int RunModeSetIPSWorker(ConfigIPSParserFunc ConfigParser,
520509
const char *verdict_mod_name,
521510
const char *decode_mod_name)
522511
{
523-
TmModule *tm_module = NULL;
524512
const int nqueue = LiveGetDeviceCount();
525513

526514
for (int i = 0; i < nqueue; i++) {
@@ -542,7 +530,7 @@ int RunModeSetIPSWorker(ConfigIPSParserFunc ConfigParser,
542530
FatalError("TmThreadsCreate failed");
543531
}
544532

545-
tm_module = TmModuleGetByName(recv_mod_name);
533+
TmModule *tm_module = TmModuleGetByName(recv_mod_name);
546534
if (tm_module == NULL) {
547535
FatalError("TmModuleGetByName failed for %s", recv_mod_name);
548536
}

0 commit comments

Comments
 (0)