From 52c8b29b12d9b6ccdbba5c4ed60846d30a67119e Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Tue, 6 Sep 2022 15:50:31 +0900 Subject: [PATCH 01/14] Set names to runtime internal threads --- src/coreclr/debug/ee/rcthread.cpp | 18 +++++++++--------- .../debug/shared/dbgtransportsession.cpp | 2 +- src/coreclr/pal/src/synchmgr/synchmanager.cpp | 8 ++++++++ .../vm/eventing/eventpipe/ep-rt-coreclr.h | 1 + src/native/libs/System.Native/pal_signal.c | 1 + 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/coreclr/debug/ee/rcthread.cpp b/src/coreclr/debug/ee/rcthread.cpp index 23a61151de4368..45b4a1ecc2369d 100644 --- a/src/coreclr/debug/ee/rcthread.cpp +++ b/src/coreclr/debug/ee/rcthread.cpp @@ -1365,14 +1365,18 @@ HRESULT DebuggerRCThread::Start(void) if (m_thread == NULL) { LOG((LF_CORDB, LL_EVERYTHING, "DebuggerRCThread failed, err=%d\n", GetLastError())); - hr = HRESULT_FROM_GetLastError(); - + return HRESULT_FROM_GetLastError(); } - else + + hr = SetThreadDescription(m_thread, W(".NET DebuggerRCThread")); + if (FAILED(hr)) { - LOG((LF_CORDB, LL_EVERYTHING, "DebuggerRCThread start was successful, id=%d\n", helperThreadId)); + LOG((LF_CORDB, LL_INFO10000, "DebuggerRCThread name set failed\n")); + return hr; } + LOG((LF_CORDB, LL_EVERYTHING, "DebuggerRCThread start was successful, id=%d\n", helperThreadId)); + // This gets published immediately. DebuggerIPCControlBlock* dcb = GetDCB(); PREFIX_ASSUME(dcb != NULL); @@ -1383,11 +1387,7 @@ HRESULT DebuggerRCThread::Start(void) m_DbgHelperThreadOSTid = helperThreadId ; #endif - if (m_thread != NULL) - { - ResumeThread(m_thread); - } - + ResumeThread(m_thread); } // unlock debugger lock is implied. diff --git a/src/coreclr/debug/shared/dbgtransportsession.cpp b/src/coreclr/debug/shared/dbgtransportsession.cpp index 259e45f42fa232..05dbcf135ed36f 100644 --- a/src/coreclr/debug/shared/dbgtransportsession.cpp +++ b/src/coreclr/debug/shared/dbgtransportsession.cpp @@ -157,7 +157,7 @@ HRESULT DbgTransportSession::Init(DebuggerIPCControlBlock *pDCB, AppDomainEnumer return E_OUTOFMEMORY; } - return S_OK; + return SetThreadDescription(m_hTransportThread, W(".NET Debug Transport Thread")); } // Drive the session to the SS_Closed state, which will deallocate all remaining transport resources diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.cpp b/src/coreclr/pal/src/synchmgr/synchmanager.cpp index 091e746a8170b4..a43fe83b0d96e8 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.cpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.cpp @@ -1519,6 +1519,14 @@ namespace CorUnix if (NO_ERROR == palErr) { pSynchManager->m_dwWorkerThreadTid = (DWORD)osThreadId; + palErr = InternalSetThreadDescription(pthrCurrent, + hWorkerThread, + W(".NET SynchManager")); + if (NO_ERROR != palErr) + { + ERROR("Unable to set worker thread name\n"); + } + palErr = InternalGetThreadDataFromHandle(pthrCurrent, hWorkerThread, &pSynchManager->m_pthrWorker, diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index a494b4d156607e..4ccc986097a913 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -2030,6 +2030,7 @@ ep_rt_thread_create ( DWORD thread_id = 0; HANDLE server_thread = ::CreateThread (nullptr, 0, reinterpret_cast(thread_func), nullptr, 0, &thread_id); if (server_thread != NULL) { + ::SetThreadDescription(server_thread, W(".NET Eventpipe Server")); ::CloseHandle (server_thread); if (id) *reinterpret_cast(id) = thread_id; diff --git a/src/native/libs/System.Native/pal_signal.c b/src/native/libs/System.Native/pal_signal.c index 0271f8c216ff05..4d77feeaea89c6 100644 --- a/src/native/libs/System.Native/pal_signal.c +++ b/src/native/libs/System.Native/pal_signal.c @@ -529,6 +529,7 @@ static bool CreateSignalHandlerThread(int* readFdPtr) pthread_t handlerThread; if (pthread_create(&handlerThread, &attr, SignalHandlerLoop, readFdPtr) == 0) { + pthread_setname_np(handlerThread, ".NET SigHandler"); success = true; } } From 7b7e47f2469f2c3be1810edc9cb5eb18a355aede Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Tue, 6 Sep 2022 17:44:04 +0900 Subject: [PATCH 02/14] Fix MAC build --- src/native/libs/System.Native/pal_signal.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/native/libs/System.Native/pal_signal.c b/src/native/libs/System.Native/pal_signal.c index 4d77feeaea89c6..494b638fd99097 100644 --- a/src/native/libs/System.Native/pal_signal.c +++ b/src/native/libs/System.Native/pal_signal.c @@ -313,6 +313,15 @@ static void* SignalHandlerLoop(void* arg) free(arg); assert(pipeFd >= 0); +#if defined(__linux__) || defined(__FreeBSD__) + pthread_setname_np(pthread_self(), ".NET SigHandler"); +#endif + +#if defined(__APPLE__) + // on macOS, pthread_setname_np only works for the calling thread. + pthread_setname_np(".NET Signal Handler"); +#endif + // Continually read a signal code from the signal pipe and process it, // until the pipe is closed. while (true) @@ -529,7 +538,6 @@ static bool CreateSignalHandlerThread(int* readFdPtr) pthread_t handlerThread; if (pthread_create(&handlerThread, &attr, SignalHandlerLoop, readFdPtr) == 0) { - pthread_setname_np(handlerThread, ".NET SigHandler"); success = true; } } From 7f58e1cb640496a4ddf84ca68e05412a1ba4f13b Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Tue, 6 Sep 2022 18:22:48 +0900 Subject: [PATCH 03/14] Shorten ThreadPool thread names --- .../src/System/Threading/PortableThreadPool.GateThread.cs | 2 +- .../src/System/Threading/PortableThreadPool.WaitThread.cs | 2 +- .../src/System/Threading/ThreadPoolWorkQueue.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs index 9eff225e7941d3..880c054edd21a4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs @@ -239,7 +239,7 @@ private static void CreateGateThread(PortableThreadPool threadPoolInstance) { IsThreadPoolThread = true, IsBackground = true, - Name = ".NET ThreadPool Gate" + Name = ".NET TPool Gate" }; gateThread.UnsafeStart(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs index ff944b29597c5b..051b5f3a2fa449 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs @@ -187,7 +187,7 @@ public WaitThread() { IsThreadPoolThread = true, IsBackground = true, - Name = ".NET ThreadPool Wait" + Name = ".NET TPool Wait" }; waitThread.UnsafeStart(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs index 21a1e6bf0acb90..d8532e06d247a5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs @@ -1355,7 +1355,7 @@ internal static void PerformWaitOrTimerCallback(_ThreadPoolWaitOrTimerCallback h public static partial class ThreadPool { - internal const string WorkerThreadName = ".NET ThreadPool Worker"; + internal const string WorkerThreadName = ".NET TPool Worker"; internal static readonly ThreadPoolWorkQueue s_workQueue = new ThreadPoolWorkQueue(); From f4af66c41d5ab1dbdbe0256010d317f460f7ba54 Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 7 Sep 2022 09:40:23 +0900 Subject: [PATCH 04/14] Apply Feedback --- src/coreclr/debug/ee/rcthread.cpp | 22 +++++++++++-------- .../PortableThreadPool.GateThread.cs | 2 +- .../PortableThreadPool.WaitThread.cs | 2 +- .../System/Threading/ThreadPoolWorkQueue.cs | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/coreclr/debug/ee/rcthread.cpp b/src/coreclr/debug/ee/rcthread.cpp index 45b4a1ecc2369d..1aef76e2d3b607 100644 --- a/src/coreclr/debug/ee/rcthread.cpp +++ b/src/coreclr/debug/ee/rcthread.cpp @@ -1311,6 +1311,11 @@ void DebuggerRCThread::TemporaryHelperThreadMainLoop() DebuggerRCThread* t = (DebuggerRCThread*)g_pRCThread; + if (FAILED(SetThreadDescription(t->m_thread, W(".NET RC Thread")))) + { + LOG((LF_CORDB, LL_INFO10000, "DebuggerRCThread name set failed\n")); + } + t->ThreadProc(); // this thread is local, go and become the helper return 0; @@ -1365,18 +1370,13 @@ HRESULT DebuggerRCThread::Start(void) if (m_thread == NULL) { LOG((LF_CORDB, LL_EVERYTHING, "DebuggerRCThread failed, err=%d\n", GetLastError())); - return HRESULT_FROM_GetLastError(); + hr = HRESULT_FROM_GetLastError(); } - - hr = SetThreadDescription(m_thread, W(".NET DebuggerRCThread")); - if (FAILED(hr)) + else { - LOG((LF_CORDB, LL_INFO10000, "DebuggerRCThread name set failed\n")); - return hr; + LOG((LF_CORDB, LL_EVERYTHING, "DebuggerRCThread start was successful, id=%d\n", helperThreadId)); } - LOG((LF_CORDB, LL_EVERYTHING, "DebuggerRCThread start was successful, id=%d\n", helperThreadId)); - // This gets published immediately. DebuggerIPCControlBlock* dcb = GetDCB(); PREFIX_ASSUME(dcb != NULL); @@ -1387,7 +1387,11 @@ HRESULT DebuggerRCThread::Start(void) m_DbgHelperThreadOSTid = helperThreadId ; #endif - ResumeThread(m_thread); + if (m_thread != NULL) + { + ResumeThread(m_thread); + } + } // unlock debugger lock is implied. diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs index 880c054edd21a4..c2a8544479b644 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.GateThread.cs @@ -239,7 +239,7 @@ private static void CreateGateThread(PortableThreadPool threadPoolInstance) { IsThreadPoolThread = true, IsBackground = true, - Name = ".NET TPool Gate" + Name = ".NET TP Gate" }; gateThread.UnsafeStart(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs index 051b5f3a2fa449..4c8512f9cdc796 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.WaitThread.cs @@ -187,7 +187,7 @@ public WaitThread() { IsThreadPoolThread = true, IsBackground = true, - Name = ".NET TPool Wait" + Name = ".NET TP Wait" }; waitThread.UnsafeStart(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs index d8532e06d247a5..8ad921e002cd5e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs @@ -1355,7 +1355,7 @@ internal static void PerformWaitOrTimerCallback(_ThreadPoolWaitOrTimerCallback h public static partial class ThreadPool { - internal const string WorkerThreadName = ".NET TPool Worker"; + internal const string WorkerThreadName = ".NET TP Worker"; internal static readonly ThreadPoolWorkQueue s_workQueue = new ThreadPoolWorkQueue(); From 26a9a8fce95bc66d7cc1fdea181870340c130cd6 Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 7 Sep 2022 09:56:10 +0900 Subject: [PATCH 05/14] Update src/coreclr/debug/ee/rcthread.cpp Thank you Co-authored-by: Jan Kotas --- src/coreclr/debug/ee/rcthread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/debug/ee/rcthread.cpp b/src/coreclr/debug/ee/rcthread.cpp index 1aef76e2d3b607..12479eb134fecd 100644 --- a/src/coreclr/debug/ee/rcthread.cpp +++ b/src/coreclr/debug/ee/rcthread.cpp @@ -1311,7 +1311,7 @@ void DebuggerRCThread::TemporaryHelperThreadMainLoop() DebuggerRCThread* t = (DebuggerRCThread*)g_pRCThread; - if (FAILED(SetThreadDescription(t->m_thread, W(".NET RC Thread")))) + if (FAILED(SetThreadDescription(t->m_thread, W(".NET Debugger")))) { LOG((LF_CORDB, LL_INFO10000, "DebuggerRCThread name set failed\n")); } From 3a1351996fcafe9258d478d79ed41ca4f6b9418e Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 7 Sep 2022 09:58:35 +0900 Subject: [PATCH 06/14] Update src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h Co-authored-by: Jan Kotas --- src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 4ccc986097a913..b04b6bb03d0cc2 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -2030,7 +2030,7 @@ ep_rt_thread_create ( DWORD thread_id = 0; HANDLE server_thread = ::CreateThread (nullptr, 0, reinterpret_cast(thread_func), nullptr, 0, &thread_id); if (server_thread != NULL) { - ::SetThreadDescription(server_thread, W(".NET Eventpipe Server")); + ::SetThreadDescription(server_thread, W(".NET EventPipe")); ::CloseHandle (server_thread); if (id) *reinterpret_cast(id) = thread_id; From ea88ce30b99f45706000d374856cbd9e0a4441f5 Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 7 Sep 2022 10:05:38 +0900 Subject: [PATCH 07/14] Apply Feedback Co-authored-by: Jan Kotas --- src/native/libs/System.Native/pal_signal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/libs/System.Native/pal_signal.c b/src/native/libs/System.Native/pal_signal.c index 494b638fd99097..d4890453f6d618 100644 --- a/src/native/libs/System.Native/pal_signal.c +++ b/src/native/libs/System.Native/pal_signal.c @@ -319,7 +319,7 @@ static void* SignalHandlerLoop(void* arg) #if defined(__APPLE__) // on macOS, pthread_setname_np only works for the calling thread. - pthread_setname_np(".NET Signal Handler"); + pthread_setname_np(".NET SigHandler"); #endif // Continually read a signal code from the signal pipe and process it, From e4f8691f01f5e2d13de01c3d839676a976482c8e Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Mon, 19 Sep 2022 16:14:18 +0900 Subject: [PATCH 08/14] Fix not to return SetThreadDescription() --- src/coreclr/debug/shared/dbgtransportsession.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/coreclr/debug/shared/dbgtransportsession.cpp b/src/coreclr/debug/shared/dbgtransportsession.cpp index 05dbcf135ed36f..acb0c552257538 100644 --- a/src/coreclr/debug/shared/dbgtransportsession.cpp +++ b/src/coreclr/debug/shared/dbgtransportsession.cpp @@ -157,7 +157,8 @@ HRESULT DbgTransportSession::Init(DebuggerIPCControlBlock *pDCB, AppDomainEnumer return E_OUTOFMEMORY; } - return SetThreadDescription(m_hTransportThread, W(".NET Debug Transport Thread")); + SetThreadDescription(m_hTransportThread, W(".NET Debug Transport")); + return S_OK; } // Drive the session to the SS_Closed state, which will deallocate all remaining transport resources From 635addb09c43115e11f7ff7a24129abada8544d2 Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Mon, 19 Sep 2022 19:12:58 +0900 Subject: [PATCH 09/14] Change thread names on linux only --- src/coreclr/debug/ee/rcthread.cpp | 2 ++ src/coreclr/debug/shared/dbgtransportsession.cpp | 3 ++- src/coreclr/pal/src/synchmgr/synchmanager.cpp | 3 ++- src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h | 2 ++ src/native/libs/System.Native/pal_signal.c | 5 ----- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/coreclr/debug/ee/rcthread.cpp b/src/coreclr/debug/ee/rcthread.cpp index 12479eb134fecd..404fea783f0a34 100644 --- a/src/coreclr/debug/ee/rcthread.cpp +++ b/src/coreclr/debug/ee/rcthread.cpp @@ -1311,10 +1311,12 @@ void DebuggerRCThread::TemporaryHelperThreadMainLoop() DebuggerRCThread* t = (DebuggerRCThread*)g_pRCThread; +#if defined(__linux__) || defined(__FreeBSD__) if (FAILED(SetThreadDescription(t->m_thread, W(".NET Debugger")))) { LOG((LF_CORDB, LL_INFO10000, "DebuggerRCThread name set failed\n")); } +#endif t->ThreadProc(); // this thread is local, go and become the helper diff --git a/src/coreclr/debug/shared/dbgtransportsession.cpp b/src/coreclr/debug/shared/dbgtransportsession.cpp index acb0c552257538..083db3c5b84b30 100644 --- a/src/coreclr/debug/shared/dbgtransportsession.cpp +++ b/src/coreclr/debug/shared/dbgtransportsession.cpp @@ -156,8 +156,9 @@ HRESULT DbgTransportSession::Init(DebuggerIPCControlBlock *pDCB, AppDomainEnumer Release(); return E_OUTOFMEMORY; } - +#if defined(__linux__) || defined(__FreeBSD__) SetThreadDescription(m_hTransportThread, W(".NET Debug Transport")); +#endif return S_OK; } diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.cpp b/src/coreclr/pal/src/synchmgr/synchmanager.cpp index a43fe83b0d96e8..2ed23abbdc7443 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.cpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.cpp @@ -1519,6 +1519,7 @@ namespace CorUnix if (NO_ERROR == palErr) { pSynchManager->m_dwWorkerThreadTid = (DWORD)osThreadId; +#if defined(__linux__) || defined(__FreeBSD__) palErr = InternalSetThreadDescription(pthrCurrent, hWorkerThread, W(".NET SynchManager")); @@ -1526,7 +1527,7 @@ namespace CorUnix { ERROR("Unable to set worker thread name\n"); } - +#endif palErr = InternalGetThreadDataFromHandle(pthrCurrent, hWorkerThread, &pSynchManager->m_pthrWorker, diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index b04b6bb03d0cc2..52fd45900eb2aa 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -2030,7 +2030,9 @@ ep_rt_thread_create ( DWORD thread_id = 0; HANDLE server_thread = ::CreateThread (nullptr, 0, reinterpret_cast(thread_func), nullptr, 0, &thread_id); if (server_thread != NULL) { +#if defined(__linux__) || defined(__FreeBSD__) ::SetThreadDescription(server_thread, W(".NET EventPipe")); +#endif ::CloseHandle (server_thread); if (id) *reinterpret_cast(id) = thread_id; diff --git a/src/native/libs/System.Native/pal_signal.c b/src/native/libs/System.Native/pal_signal.c index d4890453f6d618..268beec594d05d 100644 --- a/src/native/libs/System.Native/pal_signal.c +++ b/src/native/libs/System.Native/pal_signal.c @@ -317,11 +317,6 @@ static void* SignalHandlerLoop(void* arg) pthread_setname_np(pthread_self(), ".NET SigHandler"); #endif -#if defined(__APPLE__) - // on macOS, pthread_setname_np only works for the calling thread. - pthread_setname_np(".NET SigHandler"); -#endif - // Continually read a signal code from the signal pipe and process it, // until the pipe is closed. while (true) From b0e751dbee7622770e3f2e71f5bb81c7a21c952c Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Tue, 20 Sep 2022 06:38:39 +0900 Subject: [PATCH 10/14] Change SetThreadDescription to SetThreadName --- src/coreclr/debug/ee/rcthread.cpp | 4 +--- src/coreclr/debug/shared/dbgtransportsession.cpp | 4 +--- src/coreclr/pal/src/synchmgr/synchmanager.cpp | 2 -- src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h | 4 +--- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/coreclr/debug/ee/rcthread.cpp b/src/coreclr/debug/ee/rcthread.cpp index 404fea783f0a34..e7aa9f2b067ce4 100644 --- a/src/coreclr/debug/ee/rcthread.cpp +++ b/src/coreclr/debug/ee/rcthread.cpp @@ -1311,12 +1311,10 @@ void DebuggerRCThread::TemporaryHelperThreadMainLoop() DebuggerRCThread* t = (DebuggerRCThread*)g_pRCThread; -#if defined(__linux__) || defined(__FreeBSD__) - if (FAILED(SetThreadDescription(t->m_thread, W(".NET Debugger")))) + if (FAILED(SetThreadName(t->m_thread, W(".NET Debugger")))) { LOG((LF_CORDB, LL_INFO10000, "DebuggerRCThread name set failed\n")); } -#endif t->ThreadProc(); // this thread is local, go and become the helper diff --git a/src/coreclr/debug/shared/dbgtransportsession.cpp b/src/coreclr/debug/shared/dbgtransportsession.cpp index 083db3c5b84b30..b15905e1fa11ed 100644 --- a/src/coreclr/debug/shared/dbgtransportsession.cpp +++ b/src/coreclr/debug/shared/dbgtransportsession.cpp @@ -156,9 +156,7 @@ HRESULT DbgTransportSession::Init(DebuggerIPCControlBlock *pDCB, AppDomainEnumer Release(); return E_OUTOFMEMORY; } -#if defined(__linux__) || defined(__FreeBSD__) - SetThreadDescription(m_hTransportThread, W(".NET Debug Transport")); -#endif + SetThreadName(m_hTransportThread, W(".NET Debug Transport")); return S_OK; } diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.cpp b/src/coreclr/pal/src/synchmgr/synchmanager.cpp index 2ed23abbdc7443..89aef49130da86 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.cpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.cpp @@ -1519,7 +1519,6 @@ namespace CorUnix if (NO_ERROR == palErr) { pSynchManager->m_dwWorkerThreadTid = (DWORD)osThreadId; -#if defined(__linux__) || defined(__FreeBSD__) palErr = InternalSetThreadDescription(pthrCurrent, hWorkerThread, W(".NET SynchManager")); @@ -1527,7 +1526,6 @@ namespace CorUnix { ERROR("Unable to set worker thread name\n"); } -#endif palErr = InternalGetThreadDataFromHandle(pthrCurrent, hWorkerThread, &pSynchManager->m_pthrWorker, diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 52fd45900eb2aa..3093d81478bf5a 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -2030,9 +2030,7 @@ ep_rt_thread_create ( DWORD thread_id = 0; HANDLE server_thread = ::CreateThread (nullptr, 0, reinterpret_cast(thread_func), nullptr, 0, &thread_id); if (server_thread != NULL) { -#if defined(__linux__) || defined(__FreeBSD__) - ::SetThreadDescription(server_thread, W(".NET EventPipe")); -#endif + ::SetThreadName(server_thread, W(".NET EventPipe")); ::CloseHandle (server_thread); if (id) *reinterpret_cast(id) = thread_id; From 2ee5c1bebf420cbe748c9a75d1fd3025717223b3 Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 21 Sep 2022 09:33:36 +0900 Subject: [PATCH 11/14] Set thread names inside thread functions --- src/coreclr/debug/shared/dbgtransportsession.cpp | 4 +++- src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h | 9 ++++++++- src/mono/mono/eventpipe/ep-rt-mono.h | 8 ++++++++ src/native/eventpipe/ds-server.c | 6 ++++-- src/native/eventpipe/ep-rt.h | 4 ++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/coreclr/debug/shared/dbgtransportsession.cpp b/src/coreclr/debug/shared/dbgtransportsession.cpp index b15905e1fa11ed..2d0b713a2da340 100644 --- a/src/coreclr/debug/shared/dbgtransportsession.cpp +++ b/src/coreclr/debug/shared/dbgtransportsession.cpp @@ -156,7 +156,7 @@ HRESULT DbgTransportSession::Init(DebuggerIPCControlBlock *pDCB, AppDomainEnumer Release(); return E_OUTOFMEMORY; } - SetThreadName(m_hTransportThread, W(".NET Debug Transport")); + return S_OK; } @@ -1227,6 +1227,8 @@ void DbgTransportSession::InitSessionState() // instance method version defined below for convenience in the implementation. DWORD WINAPI DbgTransportSession::TransportWorkerStatic(LPVOID pvContext) { + SetThreadName(PAL_GetCurrentThread(), W(".NET DebugPipe")); + ((DbgTransportSession*)pvContext)->TransportWorker(); // Nobody looks at this result, the choice of 0 is arbitrary. diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index 3093d81478bf5a..fbcf694ee41c70 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -2030,7 +2030,6 @@ ep_rt_thread_create ( DWORD thread_id = 0; HANDLE server_thread = ::CreateThread (nullptr, 0, reinterpret_cast(thread_func), nullptr, 0, &thread_id); if (server_thread != NULL) { - ::SetThreadName(server_thread, W(".NET EventPipe")); ::CloseHandle (server_thread); if (id) *reinterpret_cast(id) = thread_id; @@ -2048,6 +2047,14 @@ ep_rt_thread_create ( return result; } +static +inline +void +ep_rt_set_server_name() +{ + ::SetThreadName(PAL_GetCurrentThread(), W(".NET EventPipe")); +} + static inline void diff --git a/src/mono/mono/eventpipe/ep-rt-mono.h b/src/mono/mono/eventpipe/ep-rt-mono.h index 348c7cbd3526a2..7afada2dba4188 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.h +++ b/src/mono/mono/eventpipe/ep-rt-mono.h @@ -1359,6 +1359,14 @@ ep_rt_thread_create ( return false; } +static +inline +void +ep_rt_set_server_name() +{ + mono_native_thread_set_name(mono_native_thread_id_get(), ".NET EventPipe"); +} + static inline void diff --git a/src/native/eventpipe/ds-server.c b/src/native/eventpipe/ds-server.c index 5185bc09f49f54..b3cf9f3b27e032 100644 --- a/src/native/eventpipe/ds-server.c +++ b/src/native/eventpipe/ds-server.c @@ -116,6 +116,8 @@ EP_RT_DEFINE_THREAD_FUNC (server_thread) { EP_ASSERT (server_volatile_load_shutting_down_state () || ds_ipc_stream_factory_has_active_ports ()); + ep_rt_set_server_name(); + if (!ds_ipc_stream_factory_has_active_ports ()) { #ifndef DS_IPC_DISABLE_LISTEN_PORTS DS_LOG_ERROR_0 ("Diagnostics IPC listener was undefined"); @@ -256,7 +258,7 @@ ds_server_shutdown (void) void ds_server_pause_for_diagnostics_monitor (void) { - _is_paused_for_startup = true; + _is_paused_for_startup = true; if (ds_ipc_stream_factory_any_suspended_ports ()) { EP_ASSERT (ep_rt_wait_event_is_valid (&_server_resume_runtime_startup_event)); @@ -278,7 +280,7 @@ ds_server_resume_runtime_startup (void) ds_ipc_stream_factory_resume_current_port (); if (!ds_ipc_stream_factory_any_suspended_ports () && ep_rt_wait_event_is_valid (&_server_resume_runtime_startup_event)) { ep_rt_wait_event_set (&_server_resume_runtime_startup_event); - _is_paused_for_startup = false; + _is_paused_for_startup = false; } } diff --git a/src/native/eventpipe/ep-rt.h b/src/native/eventpipe/ep-rt.h index 086975834d820f..e7d7a12c7d0a73 100644 --- a/src/native/eventpipe/ep-rt.h +++ b/src/native/eventpipe/ep-rt.h @@ -566,6 +566,10 @@ ep_rt_thread_create ( EventPipeThreadType thread_type, void *id); +static +void +ep_rt_set_server_name (void); + static void ep_rt_thread_sleep (uint64_t ns); From 9abb1b39537d64ea1d3fde49aa8007b426564f8d Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 21 Sep 2022 09:47:42 +0900 Subject: [PATCH 12/14] Add missing name for MAC --- src/native/libs/System.Native/pal_signal.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/native/libs/System.Native/pal_signal.c b/src/native/libs/System.Native/pal_signal.c index 268beec594d05d..88f43fac5023c2 100644 --- a/src/native/libs/System.Native/pal_signal.c +++ b/src/native/libs/System.Native/pal_signal.c @@ -313,8 +313,12 @@ static void* SignalHandlerLoop(void* arg) free(arg); assert(pipeFd >= 0); + char* threadName = ".NET SigHandler"; #if defined(__linux__) || defined(__FreeBSD__) - pthread_setname_np(pthread_self(), ".NET SigHandler"); + pthread_setname_np(pthread_self(), threadName); +#endif +#if defined(__APPLE__) + pthread_setname_np(threadName); #endif // Continually read a signal code from the signal pipe and process it, From f4a14fc90723abb0e053f282ffb1ff920a3189f8 Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 21 Sep 2022 09:58:47 +0900 Subject: [PATCH 13/14] Move name change info thread function --- src/coreclr/pal/src/synchmgr/synchmanager.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.cpp b/src/coreclr/pal/src/synchmgr/synchmanager.cpp index 89aef49130da86..d8a4fcfe00b20f 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.cpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.cpp @@ -1519,13 +1519,6 @@ namespace CorUnix if (NO_ERROR == palErr) { pSynchManager->m_dwWorkerThreadTid = (DWORD)osThreadId; - palErr = InternalSetThreadDescription(pthrCurrent, - hWorkerThread, - W(".NET SynchManager")); - if (NO_ERROR != palErr) - { - ERROR("Unable to set worker thread name\n"); - } palErr = InternalGetThreadDataFromHandle(pthrCurrent, hWorkerThread, &pSynchManager->m_pthrWorker, @@ -1714,6 +1707,10 @@ namespace CorUnix reinterpret_cast(pArg); CPalThread * pthrWorker = InternalGetCurrentThread(); + InternalSetThreadDescription(pthrWorker, + PAL_GetCurrentThread(), + W(".NET SynchManager")); + while (!fWorkerIsDone) { LONG lProcessCount; From 1de010320c531b4236f0409f5946b1171d65600c Mon Sep 17 00:00:00 2001 From: Hyungju Lee Date: Wed, 21 Sep 2022 10:43:49 +0900 Subject: [PATCH 14/14] Fix windows build error --- src/coreclr/debug/shared/dbgtransportsession.cpp | 2 +- src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/debug/shared/dbgtransportsession.cpp b/src/coreclr/debug/shared/dbgtransportsession.cpp index 2d0b713a2da340..b1b4cee234038c 100644 --- a/src/coreclr/debug/shared/dbgtransportsession.cpp +++ b/src/coreclr/debug/shared/dbgtransportsession.cpp @@ -1227,7 +1227,7 @@ void DbgTransportSession::InitSessionState() // instance method version defined below for convenience in the implementation. DWORD WINAPI DbgTransportSession::TransportWorkerStatic(LPVOID pvContext) { - SetThreadName(PAL_GetCurrentThread(), W(".NET DebugPipe")); + SetThreadName(GetCurrentThread(), W(".NET DebugPipe")); ((DbgTransportSession*)pvContext)->TransportWorker(); diff --git a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h index fbcf694ee41c70..2f0348d452fa62 100644 --- a/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h +++ b/src/coreclr/vm/eventing/eventpipe/ep-rt-coreclr.h @@ -2052,7 +2052,7 @@ inline void ep_rt_set_server_name() { - ::SetThreadName(PAL_GetCurrentThread(), W(".NET EventPipe")); + ::SetThreadName(GetCurrentThread(), W(".NET EventPipe")); } static