From: Michael Matirko (mmatirko) Date: Thu, 8 Feb 2024 15:51:11 +0000 (+0000) Subject: Pull request #4172: util: add macro for setting thread names on various systems X-Git-Tag: 3.1.81.0~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49370f821683c844cbe71676fead4d938951790c;p=thirdparty%2Fsnort3.git Pull request #4172: util: add macro for setting thread names on various systems Merge in SNORT/snort3 from ~MMATIRKO/snort3:thread_names to master Squashed commit of the following: commit 0a6d452e0c710757a3656427d11c0fa2fe62c119 Author: Michael Matirko Date: Wed Jan 24 16:05:28 2024 -0500 utils: add macro for setting thread name --- diff --git a/src/file_api/file_capture.cc b/src/file_api/file_capture.cc index a3bfa7bc6..c3f1a49ec 100644 --- a/src/file_api/file_capture.cc +++ b/src/file_api/file_capture.cc @@ -65,6 +65,8 @@ FileCaptureState FileCapture::error_capture(FileCaptureState state) // Only one writer thread supported void FileCapture::writer_thread() { + SET_THREAD_NAME(pthread_self(), "snort.filecap"); + while (true) { // Wait until there are files diff --git a/src/main/thread_config.cc b/src/main/thread_config.cc index 99a88921a..bede4b760 100644 --- a/src/main/thread_config.cc +++ b/src/main/thread_config.cc @@ -301,6 +301,9 @@ void ThreadConfig::implement_thread_affinity(SThreadType type, unsigned id) hwloc_cpuset_t current_cpuset, desired_cpuset; char* s; + std::string thread_name_suffix; + std::string thread_name; + auto iter = thread_affinity.find(key); if (iter != thread_affinity.end()) desired_cpuset = iter->second->cpuset; @@ -314,7 +317,32 @@ void ThreadConfig::implement_thread_affinity(SThreadType type, unsigned id) current_cpuset = hwloc_bitmap_alloc(); hwloc_get_cpubind(topology, current_cpuset, HWLOC_CPUBIND_THREAD); if (!hwloc_bitmap_isequal(current_cpuset, desired_cpuset)) + { LogMessage("Binding %s to CPU %s.\n", stringify_thread(type, id).c_str(), s); + thread_name_suffix = ".core-"; + thread_name_suffix.append(s); + } + else + { + thread_name_suffix = ".ins-"; + thread_name_suffix.append(std::to_string(id)); + } + + // Thread name is snort.ins-X for unpinned threads, and snort.core-X + // for threads pinned to CPU x + if (type == STHREAD_TYPE_MAIN) + { + thread_name = "snort3"; + thread_name_suffix = ""; + } + else + { + thread_name = "snort"; + } + + thread_name.append(thread_name_suffix); + SET_THREAD_NAME(pthread_self(), thread_name.c_str()); + hwloc_bitmap_free(current_cpuset); if (hwloc_set_cpubind(topology, desired_cpuset, HWLOC_CPUBIND_THREAD)) diff --git a/src/utils/util.h b/src/utils/util.h index 84f941f4c..e42ed1eca 100644 --- a/src/utils/util.h +++ b/src/utils/util.h @@ -38,6 +38,24 @@ #include "main/snort_types.h" +// Note: thread names > 15 chars aren't supported on most systems +#if defined(__linux__) + #include + #define SET_THREAD_NAME(thread, name) \ + pthread_setname_np(thread, name) + +#elif defined(__OpenBSD__) || defined(__FreeBSD__) + #include + #define SET_THREAD_NAME(thread, name) \ + pthread_set_name_np(thread, name) + +#else + #define SET_THREAD_NAME(thread, name) \ + UNUSED(thread);\ + UNUSED(name); + +#endif + #define TIMEBUF_SIZE 27 #define SECONDS_PER_DAY 86400 /* number of seconds in a day */