Skip to content

Commit

Permalink
Share pthread_setname via minipal (#108370)
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 authored Oct 6, 2024
1 parent 48dbc4f commit 112ef3d
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 355 deletions.
14 changes: 2 additions & 12 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "gcenv.unix.inl"
#include "volatile.h"
#include "numasupport.h"
#include <minipal/thread.h>

#if HAVE_SWAPCTL
#include <sys/swap.h>
Expand Down Expand Up @@ -389,18 +390,7 @@ void GCToOSInterface::Shutdown()
// Numeric id of the current thread, as best we can retrieve it.
uint64_t GCToOSInterface::GetCurrentThreadIdForLogging()
{
#if defined(__linux__)
return (uint64_t)syscall(SYS_gettid);
#elif HAVE_PTHREAD_GETTHREADID_NP
return (uint64_t)pthread_getthreadid_np();
#elif HAVE_PTHREAD_THREADID_NP
unsigned long long tid;
pthread_threadid_np(pthread_self(), &tid);
return (uint64_t)tid;
#else
// Fallback in case we don't know how to get integer thread id on the current platform
return (uint64_t)pthread_self();
#endif
return (uint64_t)minipal_get_current_thread_id();
}

// Get the process ID of the process.
Expand Down
5 changes: 1 addition & 4 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,7 @@ ep_rt_aot_current_thread_get_id (void)
{
STATIC_CONTRACT_NOTHROW;
#ifdef TARGET_UNIX
static __thread uint64_t tid;
if (!tid)
tid = PalGetCurrentOSThreadId();
return static_cast<ep_rt_thread_id_t>(tid);
return static_cast<ep_rt_thread_id_t>(PalGetCurrentOSThreadId());
#else
return static_cast<ep_rt_thread_id_t>(::GetCurrentThreadId ());
#endif
Expand Down
28 changes: 2 additions & 26 deletions src/coreclr/nativeaot/Runtime/unix/PalCreateDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "RhConfig.h"

#include <minipal/utils.h>
#include <minipal/thread.h>
#include <generatedumpflags.h>

#if !defined(HOST_MACCATALYST) && !defined(HOST_IOS) && !defined(HOST_TVOS)
Expand All @@ -59,31 +60,6 @@ const char* g_argvCreateDump[MAX_ARGV_ENTRIES] = { nullptr };
char* g_szCreateDumpPath = nullptr;
char* g_ppidarg = nullptr;

/*++
Function:
PlatformGetCurrentThreadId
Returns the current thread id
*/

#if defined(__linux__)
#define PlatformGetCurrentThreadId() (uint32_t)syscall(SYS_gettid)
#elif defined(__APPLE__)
inline uint32_t PlatformGetCurrentThreadId() {
uint64_t tid;
pthread_threadid_np(pthread_self(), &tid);
return (uint32_t)tid;
}
#elif defined(__FreeBSD__)
#include <pthread_np.h>
#define PlatformGetCurrentThreadId() (uint32_t)pthread_getthreadid_np()
#elif defined(__NetBSD__)
#include <lwp.h>
#define PlatformGetCurrentThreadId() (uint32_t)_lwp_self()
#else
#define PlatformGetCurrentThreadId() (uint32_t)pthread_self()
#endif

const size_t MaxUnsigned32BitDecString = STRING_LENGTH("4294967295");
const size_t MaxUnsigned64BitDecString = STRING_LENGTH("18446744073709551615");

Expand Down Expand Up @@ -386,7 +362,7 @@ PalCreateCrashDumpIfEnabled(int signal, siginfo_t* siginfo, void* exceptionRecor
}

// Add the current thread id to the command line. This function is always called on the crashing thread.
crashThreadArg = FormatInt(PlatformGetCurrentThreadId());
crashThreadArg = FormatInt(minipal_get_current_thread_id());
if (crashThreadArg != nullptr)
{
argv[argc++] = "--crashthread";
Expand Down
34 changes: 10 additions & 24 deletions src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <sys/time.h>
#include <cstdarg>
#include <signal.h>
#include <minipal/thread.h>

#if HAVE_PTHREAD_GETTHREADID_NP
#include <pthread_np.h>
Expand Down Expand Up @@ -718,15 +719,14 @@ REDHAWK_PALEXPORT bool REDHAWK_PALAPI PalStartBackgroundWork(_In_ BackgroundCall

REDHAWK_PALIMPORT bool REDHAWK_PALAPI PalSetCurrentThreadName(const char* name)
{
const int MAX_THREAD_NAME_SIZE = 15;
char name_copy[MAX_THREAD_NAME_SIZE + 1];
strncpy(name_copy, name, MAX_THREAD_NAME_SIZE);
name_copy[MAX_THREAD_NAME_SIZE] = '\0';
#ifdef __APPLE__
pthread_setname_np(name_copy);
#else
pthread_setname_np(pthread_self(), name_copy);
#endif //__APPLE__
// Ignore requests to set the main thread name because
// it causes the value returned by Process.ProcessName to change.
if ((pid_t)PalGetCurrentOSThreadId() != getpid())
{
int setNameResult = minipal_set_thread_name(pthread_self(), name);
(void)setNameResult; // used
assert(setNameResult == 0);
}
return true;
}

Expand Down Expand Up @@ -1292,19 +1292,5 @@ extern "C" uint64_t PalQueryPerformanceFrequency()

extern "C" uint64_t PalGetCurrentOSThreadId()
{
#if defined(__linux__)
return (uint64_t)syscall(SYS_gettid);
#elif defined(__APPLE__)
uint64_t tid;
pthread_threadid_np(pthread_self(), &tid);
return (uint64_t)tid;
#elif HAVE_PTHREAD_GETTHREADID_NP
return (uint64_t)pthread_getthreadid_np();
#elif HAVE_LWP_SELF
return (uint64_t)_lwp_self();
#else
// Fallback in case we don't know how to get integer thread id on the current platform
return (uint64_t)pthread_self();
#endif
return (uint64_t)minipal_get_current_thread_id();
}

63 changes: 4 additions & 59 deletions src/coreclr/pal/src/include/pal/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Module Name:
#include "threadinfo.hpp"
#include "synchobjects.hpp"
#include <errno.h>
#include <minipal/thread.h>

namespace CorUnix
{
Expand Down Expand Up @@ -91,13 +92,6 @@ namespace CorUnix
HANDLE *phThread
);

PAL_ERROR
InternalSetThreadDescription(
CPalThread *,
HANDLE,
PCWSTR
);

PAL_ERROR
CreateThreadData(
CPalThread **ppThread
Expand Down Expand Up @@ -192,14 +186,6 @@ namespace CorUnix
int
);

friend
PAL_ERROR
InternalSetThreadDescription(
CPalThread *,
HANDLE,
PCWSTR
);

friend
PAL_ERROR
CreateThreadData(
Expand Down Expand Up @@ -723,50 +709,9 @@ TLSCleanup(
extern PAL_ActivationFunction g_activationFunction;
extern PAL_SafeActivationCheckFunction g_safeActivationCheckFunction;

/*++
Macro:
THREADSilentGetCurrentThreadId
Abstract:
Same as GetCurrentThreadId, but it doesn't output any traces.
It is useful for tracing functions to display the thread ID
without generating any new traces.
TODO: how does the perf of pthread_self compare to
InternalGetCurrentThread when we find the thread in the
cache?
If the perf of pthread_self is comparable to that of the stack
bounds based lookaside system, why aren't we using it in the
cache?
In order to match the thread ids that debuggers use at least for
linux we need to use gettid().
--*/
#if defined(__linux__)
#define PlatformGetCurrentThreadId() (SIZE_T)syscall(SYS_gettid)
#elif defined(__APPLE__)
inline SIZE_T PlatformGetCurrentThreadId() {
uint64_t tid;
pthread_threadid_np(pthread_self(), &tid);
return (SIZE_T)tid;
}
#elif defined(__FreeBSD__)
#include <pthread_np.h>
#define PlatformGetCurrentThreadId() (SIZE_T)pthread_getthreadid_np()
#elif defined(__NetBSD__)
#include <lwp.h>
#define PlatformGetCurrentThreadId() (SIZE_T)_lwp_self()
#else
#define PlatformGetCurrentThreadId() (SIZE_T)pthread_self()
#endif

inline SIZE_T THREADSilentGetCurrentThreadId() {
static __thread SIZE_T tid;
if (!tid)
tid = PlatformGetCurrentThreadId();
return tid;
inline SIZE_T THREADSilentGetCurrentThreadId()
{
return minipal_get_current_thread_id();
}

#endif // _PAL_THREAD_HPP_
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/misc/perfjitdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace
{
JitCodeLoadRecord() :
pid(getpid()),
tid((uint32_t)PlatformGetCurrentThreadId())
tid((uint32_t)THREADSilentGetCurrentThreadId())
{
header.id = JIT_CODE_LOAD;
header.timestamp = GetTimeStampNS();
Expand Down
4 changes: 1 addition & 3 deletions src/coreclr/pal/src/synchmgr/synchmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1708,9 +1708,7 @@ namespace CorUnix
reinterpret_cast<CPalSynchronizationManager*>(pArg);
CPalThread * pthrWorker = InternalGetCurrentThread();

InternalSetThreadDescription(pthrWorker,
PAL_GetCurrentThread(),
W(".NET SynchManager"));
SetThreadDescription(PAL_GetCurrentThread(), W(".NET SynchManager"));

while (!fWorkerIsDone)
{
Expand Down
Loading

0 comments on commit 112ef3d

Please sign in to comment.