From: Gunnar Beutner Date: Fri, 30 Aug 2013 08:19:32 +0000 (+0200) Subject: Implement OS-specific support for thread names. X-Git-Tag: v0.0.3~645 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7f52e04a0145e8c0503f38f47f32658690325afe;p=icinga2 Implement OS-specific support for thread names. --- diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp index 13718f1b0..633a5d478 100644 --- a/components/checker/checkercomponent.cpp +++ b/components/checker/checkercomponent.cpp @@ -66,6 +66,8 @@ void CheckerComponent::Stop(void) void CheckerComponent::CheckThreadProc(void) { + Utility::SetThreadName("Check Scheduler"); + boost::mutex::scoped_lock lock(m_Mutex); for (;;) { diff --git a/components/cluster/clustercomponent.cpp b/components/cluster/clustercomponent.cpp index 7be73d26a..013bff4cd 100644 --- a/components/cluster/clustercomponent.cpp +++ b/components/cluster/clustercomponent.cpp @@ -157,6 +157,8 @@ void ClusterComponent::AddListener(const String& service) void ClusterComponent::ListenerThreadProc(const Socket::Ptr& server) { + Utility::SetThreadName("Cluster Listener"); + server->Listen(); for (;;) { diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 69f6732b7..f6b02fb56 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -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; diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 62f3a8bd9..fe110d1a0 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -173,6 +173,8 @@ void Application::RunEventLoop(void) const */ void Application::TimeWatchThreadProc(void) { + Utility::SetThreadName("Time Watch"); + double lastLoop = Utility::GetTime(); for (;;) { diff --git a/lib/base/threadpool.cpp b/lib/base/threadpool.cpp index ef37df426..1f2cc798b 100644 --- a/lib/base/threadpool.cpp +++ b/lib/base/threadpool.cpp @@ -30,8 +30,10 @@ 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 (;;) { diff --git a/lib/base/threadpool.h b/lib/base/threadpool.h index 8caf83c37..b0e34f93f 100644 --- a/lib/base/threadpool.h +++ b/lib/base/threadpool.h @@ -69,6 +69,9 @@ private: { } }; + int m_ID; + static int m_NextID; + ThreadStats m_ThreadStats[512]; boost::thread m_ManagerThread; diff --git a/lib/base/timer.cpp b/lib/base/timer.cpp index e72b03d9c..92d5e9888 100644 --- a/lib/base/timer.cpp +++ b/lib/base/timer.cpp @@ -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); diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 9013d194d..b350ff5a4 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -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) diff --git a/lib/base/utility.h b/lib/base/utility.h index 128f8ea2e..94d882890 100644 --- a/lib/base/utility.h +++ b/lib/base/utility.h @@ -29,6 +29,20 @@ 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. *