]> granicus.if.org Git - icinga2/commitdiff
Implement OS-specific support for thread names.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 30 Aug 2013 08:19:32 +0000 (10:19 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 30 Aug 2013 08:19:32 +0000 (10:19 +0200)
components/checker/checkercomponent.cpp
components/cluster/clustercomponent.cpp
components/compat/compatcomponent.cpp
lib/base/application.cpp
lib/base/threadpool.cpp
lib/base/threadpool.h
lib/base/timer.cpp
lib/base/utility.cpp
lib/base/utility.h

index 13718f1b06cf1e6cf4066b0dccb67c4c09212961..633a5d4785a4c597a92179aff096d685ed268b7b 100644 (file)
@@ -66,6 +66,8 @@ void CheckerComponent::Stop(void)
 
 void CheckerComponent::CheckThreadProc(void)
 {
+       Utility::SetThreadName("Check Scheduler");
+
        boost::mutex::scoped_lock lock(m_Mutex);
 
        for (;;) {
index 7be73d26adab81ecf73b50a48737af3cfe4fd669..013bff4cd196715e72fa53d8d9473e196060b5bb 100644 (file)
@@ -157,6 +157,8 @@ void ClusterComponent::AddListener(const String& service)
 
 void ClusterComponent::ListenerThreadProc(const Socket::Ptr& server)
 {
+       Utility::SetThreadName("Cluster Listener");
+
        server->Listen();
 
        for (;;) {
index 69f6732b7f4c6303daf0bf717a4746b1d3b06966..f6b02fb5679eafc253d08674ff517094db4c2bd4 100644 (file)
@@ -113,6 +113,8 @@ String CompatComponent::GetCommandPath(void) const
 #ifndef _WIN32
 void CompatComponent::CommandPipeThread(const String& commandPath)
 {
+       Utility::SetThreadName("Command Pipe");
+
        struct stat statbuf;
        bool fifo_ok = false;
 
index 62f3a8bd948a2b5273b8e19a9a66910322760d9d..fe110d1a00a831b0c6428c52f8a282720c3fbb00 100644 (file)
@@ -173,6 +173,8 @@ void Application::RunEventLoop(void) const
  */
 void Application::TimeWatchThreadProc(void)
 {
+       Utility::SetThreadName("Time Watch");
+
        double lastLoop = Utility::GetTime();
 
        for (;;) {
index ef37df4269f4abfc798de100a7d326dacfbb9637..1f2cc798bcc78d32034856b453e3dc9b4a80b0c2 100644 (file)
 
 using namespace icinga;
 
+int ThreadPool::m_NextID = 1;
+
 ThreadPool::ThreadPool(void)
-       : m_WaitTime(0), m_ServiceTime(0),
+       : m_ID(m_NextID++), m_WaitTime(0), m_ServiceTime(0),
          m_TaskCount(0), m_Stopped(false)
 {
        for (int i = 0; i < 2; i++)
@@ -96,7 +98,7 @@ void ThreadPool::Join(void)
 void ThreadPool::QueueThreadProc(int tid)
 {
        std::ostringstream idbuf;
-       idbuf << "TP " << this << " Worker #" << tid;
+       idbuf << "TP #" << m_ID << " Worker #" << tid;
        Utility::SetThreadName(idbuf.str());
 
        for (;;) {
@@ -219,7 +221,7 @@ bool ThreadPool::Post(const ThreadPool::WorkFunction& callback)
 void ThreadPool::ManagerThreadProc(void)
 {
        std::ostringstream idbuf;
-       idbuf << "TP " << this << " Manager";
+       idbuf << "TP #" << m_ID << " Manager";
        Utility::SetThreadName(idbuf.str());
 
        for (;;) {
@@ -336,7 +338,7 @@ void ThreadPool::KillWorker(void)
 void ThreadPool::StatsThreadProc(void)
 {
        std::ostringstream idbuf;
-       idbuf << "TP " << this << " Stats";
+       idbuf << "TP #" << m_ID << " Stats";
        Utility::SetThreadName(idbuf.str());
 
        for (;;) {
index 8caf83c37814678015b6275fbdaa4dc0c498c320..b0e34f93ffbc22721d68d908b6dc72685eebc3d4 100644 (file)
@@ -69,6 +69,9 @@ private:
                { }
        };
 
+       int m_ID;
+       static int m_NextID;
+
        ThreadStats m_ThreadStats[512];
 
        boost::thread m_ManagerThread;
index e72b03d9c535e3cc3b116c75edce66ebaacb1fdc..92d5e98880cb41c29fde05e5a566a2b565e3ff0c 100644 (file)
@@ -261,6 +261,8 @@ void Timer::AdjustTimers(double adjustment)
  */
 void Timer::TimerThreadProc(void)
 {
+       Utility::SetThreadName("Timer Thread");
+
        for (;;) {
                boost::mutex::scoped_lock lock(l_Mutex);
 
index 9013d194d73ffbb5cdc900d90450b3ad25e607bb..b350ff5a49923729d158023f921d3298bb1901f4 100644 (file)
@@ -500,6 +500,29 @@ String Utility::EscapeShellCmd(const String& s)
 void Utility::SetThreadName(const String& name)
 {
        m_ThreadName.reset(new String(name));
+
+#ifdef _WIN32
+       THREADNAME_INFO info;
+       info.dwType = 0x1000;
+       info.szName = name.CStr();
+       info.dwThreadID = -1;
+       info.dwFlags = 0;
+
+       __try {
+               RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
+       } __except(EXCEPTION_EXECUTE_HANDLER) {
+               /* Nothing to do here. */
+       }
+#endif /* _WIN32 */
+
+#ifdef __APPLE__
+       pthread_setname_np(name.CStr());
+#endif /* __APPLE__ */
+
+#ifdef __linux__
+       String tname = name.SubStr(0, 15);
+       pthread_setname_np(pthread_self(), tname.CStr());
+#endif /* __linux__ */
 }
 
 String Utility::GetThreadName(void)
index 128f8ea2e859b1f1317c697f2b9e4e43fb77c09f..94d882890d7a48424b315a22c3efa447e0ecb2b5 100644 (file)
 namespace icinga
 {
 
+#ifdef _WIN32
+#define MS_VC_EXCEPTION 0x406D1388
+
+#      pragma pack(push, 8)
+struct THREADNAME_INFO
+{
+       DWORD dwType;
+       LPCSTR szName;
+       DWORD dwThreadID;
+       DWORD dwFlags;
+};
+#      pragma pack(pop)
+#endif
+
 /**
  * Helper functions.
  *