From 68cfa009a0105ff2ae3f116d587f41ed92d5ef3e Mon Sep 17 00:00:00 2001 From: Zachary Rasmor Date: Sat, 5 Dec 2015 19:52:20 -0500 Subject: [PATCH] Update thread creation and threads to use global thread names. Thread name is now stored as a static string buffer, string duplication and alloc/de-alloc is no longer required. --- src/counters.c | 5 +++-- src/detect-engine-loader.c | 7 ++++--- src/flow-manager.c | 8 ++++---- src/runmode-erf-file.c | 8 +++++--- src/runmode-pcap-file.c | 25 +++---------------------- src/runmode-tile.c | 2 +- src/runmodes.c | 4 ++++ src/runmodes.h | 4 ++++ src/unix-manager.c | 2 +- src/util-runmodes.c | 11 ----------- 10 files changed, 29 insertions(+), 47 deletions(-) diff --git a/src/counters.c b/src/counters.c index 5e6d558c46..e40e225778 100644 --- a/src/counters.c +++ b/src/counters.c @@ -36,6 +36,7 @@ #include "util-privs.h" #include "util-signal.h" #include "unix-manager.h" +#include "runmodes.h" #include "output.h" #include "output-stats.h" @@ -843,7 +844,7 @@ void StatsSpawnThreads(void) ThreadVars *tv_mgmt = NULL; /* spawn the stats wakeup thread */ - tv_wakeup = TmThreadCreateMgmtThread("StatsWakeupThread", + tv_wakeup = TmThreadCreateMgmtThread(thread_name_counter_wakeup, StatsWakeupThread, 1); if (tv_wakeup == NULL) { SCLogError(SC_ERR_THREAD_CREATE, "TmThreadCreateMgmtThread " @@ -858,7 +859,7 @@ void StatsSpawnThreads(void) } /* spawn the stats mgmt thread */ - tv_mgmt = TmThreadCreateMgmtThread("StatsMgmtThread", + tv_mgmt = TmThreadCreateMgmtThread(thread_name_counter_stats, StatsMgmtThread, 1); if (tv_mgmt == NULL) { SCLogError(SC_ERR_THREAD_CREATE, diff --git a/src/detect-engine-loader.c b/src/detect-engine-loader.c index c3a79b5cbe..c85265ea45 100644 --- a/src/detect-engine-loader.c +++ b/src/detect-engine-loader.c @@ -26,6 +26,7 @@ #include "conf.h" #include "debug.h" #include "detect.h" +#include "runmodes.h" #include "threads.h" #include "threadvars.h" #include "tm-threads.h" @@ -267,10 +268,10 @@ void DetectLoaderThreadSpawn() for (i = 0; i < num_loaders; i++) { ThreadVars *tv_loader = NULL; - char name[32]; - snprintf(name, sizeof(name), "DetectLoader%02d", i+1); + char name[TM_THREAD_NAME_MAX] = ""; + snprintf(name, sizeof(name), "%s#%02d", thread_name_detect_loader, i+1); - tv_loader = TmThreadCreateCmdThreadByName("DetectLoader", + tv_loader = TmThreadCreateCmdThreadByName(name, "DetectLoader", 1); BUG_ON(tv_loader == NULL); diff --git a/src/flow-manager.c b/src/flow-manager.c index 1ae68b954a..b204966e46 100644 --- a/src/flow-manager.c +++ b/src/flow-manager.c @@ -738,10 +738,10 @@ void FlowManagerThreadSpawn() { ThreadVars *tv_flowmgr = NULL; - char name[32] = ""; + char name[TM_THREAD_NAME_MAX] = ""; snprintf(name, sizeof(name), "%s#%02u", thread_name_flow_mgr, u+1); - tv_flowmgr = TmThreadCreateMgmtThreadByName(SCStrdup(name), + tv_flowmgr = TmThreadCreateMgmtThreadByName(name, "FlowManager", 0); BUG_ON(tv_flowmgr == NULL); @@ -902,10 +902,10 @@ void FlowRecyclerThreadSpawn() { ThreadVars *tv_flowmgr = NULL; - char name[32] = ""; + char name[TM_THREAD_NAME_MAX] = ""; snprintf(name, sizeof(name), "%s#%02u", thread_name_flow_rec, u+1); - tv_flowmgr = TmThreadCreateMgmtThreadByName(SCStrdup(name), + tv_flowmgr = TmThreadCreateMgmtThreadByName(name, "FlowRecycler", 0); BUG_ON(tv_flowmgr == NULL); diff --git a/src/runmode-erf-file.c b/src/runmode-erf-file.c index 0be5da148d..abb85244a2 100644 --- a/src/runmode-erf-file.c +++ b/src/runmode-erf-file.c @@ -71,7 +71,7 @@ int RunModeErfFileSingle(void) /* Basically the same setup as PCAP files. */ - ThreadVars *tv = TmThreadCreatePacketHandler("ErfFile", + ThreadVars *tv = TmThreadCreatePacketHandler(thread_name_single, "packetpool", "packetpool", "packetpool", "packetpool", "pktacqloop"); @@ -166,7 +166,7 @@ int RunModeErfFileAutoFp(void) /* create the threads */ ThreadVars *tv = - TmThreadCreatePacketHandler("ReceiveErfFile", + TmThreadCreatePacketHandler(thread_name_autofp, "packetpool", "packetpool", queues, "flow", "pktacqloop"); @@ -202,11 +202,13 @@ int RunModeErfFileAutoFp(void) } for (thread = 0; thread < thread_max; thread++) { - snprintf(tname, sizeof(tname), "Detect%d", thread+1); + snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread+1); snprintf(qname, sizeof(qname), "pickup%d", thread+1); SCLogDebug("tname %s, qname %s", tname, qname); + SCLogDebug("Assigning %s affinity to cpu %u", thread_name, cpu); + ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(tname, qname, "flow", diff --git a/src/runmode-pcap-file.c b/src/runmode-pcap-file.c index fdd0fe077c..29a65eeebf 100644 --- a/src/runmode-pcap-file.c +++ b/src/runmode-pcap-file.c @@ -76,15 +76,8 @@ int RunModeFilePcapSingle(void) snprintf(tname, sizeof(tname), "%s#01", thread_name_single); - char *thread_name = SCStrdup(tname); - if (unlikely(thread_name == NULL)) - { - SCLogError(SC_ERR_RUNMODE, "failed to strdup thread name"); - exit(EXIT_FAILURE); - } - /* create the threads */ - ThreadVars *tv = TmThreadCreatePacketHandler(thread_name, + ThreadVars *tv = TmThreadCreatePacketHandler(tname, "packetpool", "packetpool", "packetpool", "packetpool", "pktacqloop"); @@ -208,15 +201,9 @@ int RunModeFilePcapAutoFp(void) snprintf(tname, sizeof(tname), "%s#01", thread_name_autofp); - char *thread_name = SCStrdup(tname); - if (unlikely(thread_name == NULL)) { - SCLogError(SC_ERR_RUNMODE, "failed to strdup thread name"); - exit(EXIT_FAILURE); - } - /* create the threads */ ThreadVars *tv_receivepcap = - TmThreadCreatePacketHandler(thread_name, + TmThreadCreatePacketHandler(tname, "packetpool", "packetpool", queues, "flow", "pktacqloop"); @@ -252,13 +239,7 @@ int RunModeFilePcapAutoFp(void) snprintf(qname, sizeof(qname), "pickup%d", thread+1); SCLogDebug("tname %s, qname %s", tname, qname); - - 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); + SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu); ThreadVars *tv_detect_ncpu = TmThreadCreatePacketHandler(tname, diff --git a/src/runmode-tile.c b/src/runmode-tile.c index 688588c8be..913ee562f6 100644 --- a/src/runmode-tile.c +++ b/src/runmode-tile.c @@ -213,7 +213,7 @@ int RunModeTileMpipeWorkers(void) exit(EXIT_FAILURE); } - snprintf(tname, sizeof(tname), "Worker%d", pipe+1); + snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, pipe+1); /* create the threads */ ThreadVars *tv_worker = diff --git a/src/runmodes.c b/src/runmodes.c index 99a28fcddb..65cccdea2d 100644 --- a/src/runmodes.c +++ b/src/runmodes.c @@ -65,6 +65,10 @@ char *thread_name_workers = "W"; char *thread_name_verdict = "TX"; char *thread_name_flow_mgr = "FM"; char *thread_name_flow_rec = "FR"; +char *thread_name_unix_socket = "US"; +char *thread_name_detect_loader = "DL"; +char *thread_name_counter_stats = "CS"; +char *thread_name_counter_wakeup = "CW"; /** * \brief Holds description for a runmode. diff --git a/src/runmodes.h b/src/runmodes.h index 726d49992d..165bfe45c8 100644 --- a/src/runmodes.h +++ b/src/runmodes.h @@ -67,6 +67,10 @@ extern char *thread_name_workers; extern char *thread_name_verdict; extern char *thread_name_flow_mgr; extern char *thread_name_flow_rec; +extern char *thread_name_unix_socket; +extern char *thread_name_detect_loader; +extern char *thread_name_counter_stats; +extern char *thread_name_counter_wakeup; char *RunmodeGetActive(void); const char *RunModeGetMainMode(void); diff --git a/src/unix-manager.c b/src/unix-manager.c index c2b87ad3d9..919651cce8 100644 --- a/src/unix-manager.c +++ b/src/unix-manager.c @@ -994,7 +994,7 @@ void UnixManagerThreadSpawn(int mode) SCCtrlCondInit(&unix_manager_ctrl_cond, NULL); SCCtrlMutexInit(&unix_manager_ctrl_mutex, NULL); - tv_unixmgr = TmThreadCreateCmdThreadByName("UnixManagerThread", + tv_unixmgr = TmThreadCreateCmdThreadByName(thread_name_unix_socket, "UnixManager", 0); if (tv_unixmgr == NULL) { diff --git a/src/util-runmodes.c b/src/util-runmodes.c index 5e6f736629..9920653a3f 100644 --- a/src/util-runmodes.c +++ b/src/util-runmodes.c @@ -148,11 +148,6 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, /* create the threads */ for (thread = 0; thread < threads_count; thread++) { snprintf(tname, sizeof(tname), "%s#%02d", 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(tname, "packetpool", "packetpool", @@ -220,11 +215,6 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, snprintf(tname, sizeof(tname), "%s#%02d-%s", thread_name, thread+1, visual_devname); - 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(tname, "packetpool", "packetpool", @@ -333,7 +323,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; char visual_devname[11] = ""; int shortening_result; ThreadVars *tv = NULL;