tm-threads: unify thread names handling

TmThreadCreate copy string provided as name for threads to
avoid any issue is a non allocated string is used.

This patch also introduce TmThreadSetGroupName function. This
function is used to be sure we have an allocation when
assigning the thread group name. This way we can free allocated
memory at exit.

Both code changes have required some fixes in different parts of
the code to be in sync with the new API.

Good point about these changes is that it fixes an inconsistency
were some names were not allocated and some were.
pull/1910/head
Eric Leblond 10 years ago
parent d367750633
commit a53aef9c57

@ -207,15 +207,8 @@ int RunModeErfFileAutoFp(void)
SCLogDebug("tname %s, qname %s", tname, qname);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
printf("ERROR: Can't allocate thread name\n");
exit(EXIT_FAILURE);
}
SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu);
ThreadVars *tv_detect_ncpu =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
qname, "flow",
"packetpool", "packetpool",
"varslot");
@ -252,12 +245,7 @@ int RunModeErfFileAutoFp(void)
}
}
char *thread_group_name = SCStrdup("Detect");
if (unlikely(thread_group_name == NULL)) {
printf("Error allocating memory\n");
exit(EXIT_FAILURE);
}
tv_detect_ncpu->thread_group_name = thread_group_name;
TmThreadSetGroupName(tv_detect_ncpu, "Detect");
/* add outputs as well */
SetupOutputs(tv_detect_ncpu);

@ -222,15 +222,8 @@ int RunModeFilePcapAutoFp(void)
SCLogDebug("tname %s, qname %s", tname, qname);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "failed to strdup thread name");
exit(EXIT_FAILURE);
}
SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu);
ThreadVars *tv_detect_ncpu =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
qname, "flow",
"packetpool", "packetpool",
"varslot");
@ -254,12 +247,7 @@ int RunModeFilePcapAutoFp(void)
TmSlotSetFuncAppend(tv_detect_ncpu, tm_module, NULL);
}
char *thread_group_name = SCStrdup("Detect");
if (unlikely(thread_group_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "error allocating memory");
exit(EXIT_FAILURE);
}
tv_detect_ncpu->thread_group_name = thread_group_name;
TmThreadSetGroupName(tv_detect_ncpu, "Detect");
/* add outputs as well */
SetupOutputs(tv_detect_ncpu);

@ -214,15 +214,10 @@ int RunModeTileMpipeWorkers(void)
}
snprintf(tname, sizeof(tname), "Worker%d", pipe+1);
thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
printf("ERROR: SCStrdup failed for ReceiveMpipe\n");
exit(EXIT_FAILURE);
}
/* create the threads */
ThreadVars *tv_worker =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
"packetpool", "packetpool",
"packetpool", "packetpool",
"pktacqloop");

@ -1044,7 +1044,9 @@ ThreadVars *TmThreadCreate(char *name, char *inq_name, char *inqh_name,
SC_ATOMIC_INIT(tv->flags);
SCMutexInit(&tv->perf_public_ctx.m, NULL);
tv->name = name;
tv->name = SCStrdup(name);
if (unlikely(tv->name == NULL))
goto error;
/* default state for every newly created thread */
TmThreadsSetFlag(tv, THV_PAUSE);
TmThreadsSetFlag(tv, THV_USE);
@ -1658,6 +1660,13 @@ void TmThreadFree(ThreadVars *tv)
TmThreadDeinitMC(tv);
if (tv->name) {
SCFree(tv->name);
}
if (tv->thread_group_name) {
SCFree(tv->thread_group_name);
}
s = (TmSlot *)tv->tm_slots;
while (s) {
ps = s;
@ -1669,6 +1678,24 @@ void TmThreadFree(ThreadVars *tv)
SCFree(tv);
}
void TmThreadSetGroupName(ThreadVars *tv, const char *name)
{
char *thread_group_name = NULL;
if (name == NULL)
return;
if (tv == NULL)
return;
thread_group_name = SCStrdup(name);
if (unlikely(thread_group_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "error allocating memory");
return;
}
tv->thread_group_name = thread_group_name;
}
void TmThreadClearThreadsFamily(int family)
{
ThreadVars *tv = NULL;

@ -102,6 +102,7 @@ void TmThreadKillThreads(void);
void TmThreadClearThreadsFamily(int family);
void TmThreadAppend(ThreadVars *, int);
void TmThreadRemove(ThreadVars *, int);
void TmThreadSetGroupName(ThreadVars *tv, const char *name);
TmEcode TmThreadSetCPUAffinity(ThreadVars *, uint16_t);
TmEcode TmThreadSetThreadPriority(ThreadVars *, int);

@ -148,13 +148,8 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
/* create the threads */
for (thread = 0; thread < threads_count; thread++) {
snprintf(tname, sizeof(tname), "%s%d", thread_name, thread+1);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name");
exit(EXIT_FAILURE);
}
ThreadVars *tv_receive =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
"packetpool", "packetpool",
queues, "flow", "pktacqloop");
if (tv_receive == NULL) {
@ -211,13 +206,8 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
for (thread = 0; thread < threads_count; thread++) {
snprintf(tname, sizeof(tname), "%s%s%d", thread_name,
live_dev, thread+1);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name");
exit(EXIT_FAILURE);
}
ThreadVars *tv_receive =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
"packetpool", "packetpool",
queues, "flow", "pktacqloop");
if (tv_receive == NULL) {
@ -254,13 +244,8 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
SCLogDebug("tname %s, qname %s", tname, qname);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name");
exit(EXIT_FAILURE);
}
ThreadVars *tv_detect_ncpu =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
qname, "flow",
"packetpool", "packetpool",
"varslot");
@ -286,12 +271,7 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
TmThreadSetCPU(tv_detect_ncpu, DETECT_CPU_SET);
char *thread_group_name = SCStrdup("Detect");
if (unlikely(thread_group_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "Error allocating memory");
exit(EXIT_FAILURE);
}
tv_detect_ncpu->thread_group_name = thread_group_name;
TmThreadSetGroupName(tv_detect_ncpu, "Detect");
tm_module = TmModuleGetByName("RespondReject");
if (tm_module == NULL) {
@ -334,7 +314,6 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod
/* create the threads */
for (thread = 0; thread < threads_count; thread++) {
char tname[TM_THREAD_NAME_MAX];
char *n_thread_name = NULL;
ThreadVars *tv = NULL;
TmModule *tm_module = NULL;
@ -344,12 +323,7 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod
snprintf(tname, sizeof(tname), "%s%s%d",
thread_name, live_dev, thread+1);
}
n_thread_name = SCStrdup(tname);
if (unlikely(n_thread_name == NULL)) {
SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name");
exit(EXIT_FAILURE);
}
tv = TmThreadCreatePacketHandler(n_thread_name,
tv = TmThreadCreatePacketHandler(tname,
"packetpool", "packetpool",
"packetpool", "packetpool",
"pktacqloop");
@ -521,13 +495,8 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser,
memset(tname, 0, sizeof(tname));
snprintf(tname, sizeof(tname), "Recv-Q%s", cur_queue);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "thread name creation failed");
exit(EXIT_FAILURE);
}
ThreadVars *tv_receive =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
"packetpool", "packetpool",
queues, "flow", "pktacqloop");
if (tv_receive == NULL) {
@ -562,13 +531,8 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser,
SCLogDebug("tname %s, qname %s", tname, qname);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_MEM_ALLOC, "Can't allocate thread name");
exit(EXIT_FAILURE);
}
ThreadVars *tv_detect_ncpu =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
qname, "flow",
"verdict-queue", "simple",
"varslot");
@ -596,12 +560,7 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser,
SetupOutputs(tv_detect_ncpu);
char *thread_group_name = SCStrdup("Detect");
if (unlikely(thread_group_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "Error allocating memory");
exit(EXIT_FAILURE);
}
tv_detect_ncpu->thread_group_name = thread_group_name;
TmThreadSetGroupName(tv_detect_ncpu, "Detect");
if (TmThreadSpawn(tv_detect_ncpu) != TM_ECODE_OK) {
SCLogError(SC_ERR_RUNMODE, "TmThreadSpawn failed");
@ -614,13 +573,8 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser,
memset(tname, 0, sizeof(tname));
snprintf(tname, sizeof(tname), "Verdict%d", i);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "Error allocating memory");
exit(EXIT_FAILURE);
}
ThreadVars *tv_verdict =
TmThreadCreatePacketHandler(thread_name,
TmThreadCreatePacketHandler(tname,
"verdict-queue", "simple",
"packetpool", "packetpool",
"varslot");
@ -678,12 +632,7 @@ int RunModeSetIPSWorker(ConfigIPSParserFunc ConfigParser,
memset(tname, 0, sizeof(tname));
snprintf(tname, sizeof(tname), "Worker-Q%s", cur_queue);
char *thread_name = SCStrdup(tname);
if (unlikely(thread_name == NULL)) {
SCLogError(SC_ERR_RUNMODE, "Error allocating memory");
exit(EXIT_FAILURE);
}
tv = TmThreadCreatePacketHandler(thread_name,
tv = TmThreadCreatePacketHandler(tname,
"packetpool", "packetpool",
"packetpool", "packetpool",
"pktacqloop");

Loading…
Cancel
Save