]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4172: util: add macro for setting thread names on various systems
authorMichael Matirko (mmatirko) <mmatirko@cisco.com>
Thu, 8 Feb 2024 15:51:11 +0000 (15:51 +0000)
committerSteven Baigal (sbaigal) <sbaigal@cisco.com>
Thu, 8 Feb 2024 15:51:11 +0000 (15:51 +0000)
Merge in SNORT/snort3 from ~MMATIRKO/snort3:thread_names to master

Squashed commit of the following:

commit 0a6d452e0c710757a3656427d11c0fa2fe62c119
Author: Michael Matirko <mmatirko@cisco.com>
Date:   Wed Jan 24 16:05:28 2024 -0500

    utils: add macro for setting thread name

src/file_api/file_capture.cc
src/main/thread_config.cc
src/utils/util.h

index a3bfa7bc630c7be06ec0e6e707ae0546fb6d03d7..c3f1a49ece903d86e7a25583f8ebe4caec73c175 100644 (file)
@@ -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
index 99a88921aa190818d5c1ad5fffd56b1166a78636..bede4b760ee96fe44b6fe1f2f20b02b0f3301037 100644 (file)
@@ -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))
index 84f941f4c93845b3e527f08735c6b284f0457bf1..e42ed1eca5de738c4623e3f2eec9544f8569d246 100644 (file)
 
 #include "main/snort_types.h"
 
+// Note: thread names > 15 chars aren't supported on most systems
+#if defined(__linux__)
+    #include <pthread.h>
+    #define SET_THREAD_NAME(thread, name) \
+    pthread_setname_np(thread, name)
+
+#elif defined(__OpenBSD__) || defined(__FreeBSD__)
+    #include <pthread_np.h>
+    #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  */