Change thread id size from 64 to 32 bits.

Both Windows and Linux use 32-bit thread identifiers. MacOS has a 64-bit
counter, but in practice it will never overflow during profiling and no false
aliasing will happen.

These changes are only done client-side and in the network protocol. The
server still uses 64-bit thread identifiers, to enable virtual threads, etc.
This commit is contained in:
Bartosz Taudul
2021-10-08 00:42:52 +02:00
parent 07bc665d8c
commit 250ef2cf6e
8 changed files with 54 additions and 52 deletions

View File

@@ -54,19 +54,19 @@ namespace tracy
namespace detail
{
TRACY_API uint64_t GetThreadHandleImpl()
TRACY_API uint32_t GetThreadHandleImpl()
{
#if defined _WIN32
static_assert( sizeof( decltype( GetCurrentThreadId() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" );
return uint64_t( GetCurrentThreadId() );
static_assert( sizeof( decltype( GetCurrentThreadId() ) ) <= sizeof( uint32_t ), "Thread handle too big to fit in protocol" );
return uint32_t( GetCurrentThreadId() );
#elif defined __APPLE__
uint64_t id;
pthread_threadid_np( pthread_self(), &id );
return id;
return uint32_t( id );
#elif defined __ANDROID__
return (uint64_t)gettid();
return (uint32_t)gettid();
#elif defined __linux__
return (uint64_t)syscall( SYS_gettid );
return (uint32_t)syscall( SYS_gettid );
#elif defined __FreeBSD__
long id;
thr_self( &id );
@@ -78,8 +78,13 @@ TRACY_API uint64_t GetThreadHandleImpl()
#elif defined __OpenBSD__
return getthrid();
#else
static_assert( sizeof( decltype( pthread_self() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" );
return uint64_t( pthread_self() );
// To add support for a platform, retrieve and return the kernel thread identifier here.
//
// Note that pthread_t (as for example returned by pthread_self()) is *not* a kernel
// thread identifier. It is a pointer to a library-allocated data structure instead.
// Such pointers will be reused heavily, making the pthread_t non-unique. Additionally
// a 64-bit pointer cannot be reliably truncated to 32 bits.
#error "Unsupported platform!"
#endif
}
@@ -89,7 +94,7 @@ TRACY_API uint64_t GetThreadHandleImpl()
#ifdef TRACY_ENABLE
struct ThreadNameData
{
uint64_t id;
uint32_t id;
const char* name;
ThreadNameData* next;
};
@@ -171,7 +176,7 @@ TRACY_API void SetThreadName( const char* name )
#endif
}
TRACY_API const char* GetThreadName( uint64_t id )
TRACY_API const char* GetThreadName( uint32_t id )
{
static char buf[256];
#ifdef TRACY_ENABLE
@@ -211,7 +216,7 @@ TRACY_API const char* GetThreadName( uint64_t id )
int tid = (int) syscall( SYS_gettid );
# endif
snprintf( path, sizeof( path ), "/proc/self/task/%d/comm", tid );
sprintf( buf, "%" PRIu64, id );
sprintf( buf, "%" PRIu32, id );
# ifndef __ANDROID__
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &cs );
# endif
@@ -233,7 +238,7 @@ TRACY_API const char* GetThreadName( uint64_t id )
return buf;
# endif
#endif
sprintf( buf, "%" PRIu64, id );
sprintf( buf, "%" PRIu32, id );
return buf;
}