Implement OS-specific support for thread names.

This commit is contained in:
Gunnar Beutner 2013-08-30 10:19:32 +02:00
parent f3638877eb
commit 7f52e04a01
9 changed files with 56 additions and 4 deletions

View File

@ -66,6 +66,8 @@ void CheckerComponent::Stop(void)
void CheckerComponent::CheckThreadProc(void) void CheckerComponent::CheckThreadProc(void)
{ {
Utility::SetThreadName("Check Scheduler");
boost::mutex::scoped_lock lock(m_Mutex); boost::mutex::scoped_lock lock(m_Mutex);
for (;;) { for (;;) {

View File

@ -157,6 +157,8 @@ void ClusterComponent::AddListener(const String& service)
void ClusterComponent::ListenerThreadProc(const Socket::Ptr& server) void ClusterComponent::ListenerThreadProc(const Socket::Ptr& server)
{ {
Utility::SetThreadName("Cluster Listener");
server->Listen(); server->Listen();
for (;;) { for (;;) {

View File

@ -113,6 +113,8 @@ String CompatComponent::GetCommandPath(void) const
#ifndef _WIN32 #ifndef _WIN32
void CompatComponent::CommandPipeThread(const String& commandPath) void CompatComponent::CommandPipeThread(const String& commandPath)
{ {
Utility::SetThreadName("Command Pipe");
struct stat statbuf; struct stat statbuf;
bool fifo_ok = false; bool fifo_ok = false;

View File

@ -173,6 +173,8 @@ void Application::RunEventLoop(void) const
*/ */
void Application::TimeWatchThreadProc(void) void Application::TimeWatchThreadProc(void)
{ {
Utility::SetThreadName("Time Watch");
double lastLoop = Utility::GetTime(); double lastLoop = Utility::GetTime();
for (;;) { for (;;) {

View File

@ -30,8 +30,10 @@
using namespace icinga; using namespace icinga;
int ThreadPool::m_NextID = 1;
ThreadPool::ThreadPool(void) 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) m_TaskCount(0), m_Stopped(false)
{ {
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
@ -96,7 +98,7 @@ void ThreadPool::Join(void)
void ThreadPool::QueueThreadProc(int tid) void ThreadPool::QueueThreadProc(int tid)
{ {
std::ostringstream idbuf; std::ostringstream idbuf;
idbuf << "TP " << this << " Worker #" << tid; idbuf << "TP #" << m_ID << " Worker #" << tid;
Utility::SetThreadName(idbuf.str()); Utility::SetThreadName(idbuf.str());
for (;;) { for (;;) {
@ -219,7 +221,7 @@ bool ThreadPool::Post(const ThreadPool::WorkFunction& callback)
void ThreadPool::ManagerThreadProc(void) void ThreadPool::ManagerThreadProc(void)
{ {
std::ostringstream idbuf; std::ostringstream idbuf;
idbuf << "TP " << this << " Manager"; idbuf << "TP #" << m_ID << " Manager";
Utility::SetThreadName(idbuf.str()); Utility::SetThreadName(idbuf.str());
for (;;) { for (;;) {
@ -336,7 +338,7 @@ void ThreadPool::KillWorker(void)
void ThreadPool::StatsThreadProc(void) void ThreadPool::StatsThreadProc(void)
{ {
std::ostringstream idbuf; std::ostringstream idbuf;
idbuf << "TP " << this << " Stats"; idbuf << "TP #" << m_ID << " Stats";
Utility::SetThreadName(idbuf.str()); Utility::SetThreadName(idbuf.str());
for (;;) { for (;;) {

View File

@ -69,6 +69,9 @@ private:
{ } { }
}; };
int m_ID;
static int m_NextID;
ThreadStats m_ThreadStats[512]; ThreadStats m_ThreadStats[512];
boost::thread m_ManagerThread; boost::thread m_ManagerThread;

View File

@ -261,6 +261,8 @@ void Timer::AdjustTimers(double adjustment)
*/ */
void Timer::TimerThreadProc(void) void Timer::TimerThreadProc(void)
{ {
Utility::SetThreadName("Timer Thread");
for (;;) { for (;;) {
boost::mutex::scoped_lock lock(l_Mutex); boost::mutex::scoped_lock lock(l_Mutex);

View File

@ -500,6 +500,29 @@ String Utility::EscapeShellCmd(const String& s)
void Utility::SetThreadName(const String& name) void Utility::SetThreadName(const String& name)
{ {
m_ThreadName.reset(new 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) String Utility::GetThreadName(void)

View File

@ -29,6 +29,20 @@
namespace icinga 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. * Helper functions.
* *