2020-01-20 21:49:03 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning(disable:4996)
|
|
|
|
#endif
|
2021-10-07 21:28:40 +00:00
|
|
|
#if defined _WIN32
|
|
|
|
# ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# endif
|
|
|
|
# ifndef NOMINMAX
|
|
|
|
# define NOMINMAX
|
|
|
|
# endif
|
2017-09-10 15:46:20 +00:00
|
|
|
# include <windows.h>
|
2021-04-09 22:33:01 +00:00
|
|
|
# include <malloc.h>
|
2022-02-10 19:26:45 +00:00
|
|
|
# include "TracyUwp.hpp"
|
2017-09-10 15:46:20 +00:00
|
|
|
#else
|
|
|
|
# include <pthread.h>
|
2017-09-22 19:48:21 +00:00
|
|
|
# include <string.h>
|
2017-09-10 15:46:20 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-14 11:26:55 +00:00
|
|
|
#ifdef __linux__
|
2020-02-11 13:40:49 +00:00
|
|
|
# ifdef __ANDROID__
|
|
|
|
# include <sys/types.h>
|
|
|
|
# else
|
|
|
|
# include <sys/syscall.h>
|
2019-08-13 23:12:23 +00:00
|
|
|
# endif
|
|
|
|
# include <fcntl.h>
|
2020-02-11 13:40:49 +00:00
|
|
|
#elif defined __FreeBSD__
|
|
|
|
# include <sys/thr.h>
|
|
|
|
#elif defined __NetBSD__ || defined __DragonFly__
|
|
|
|
# include <sys/lwp.h>
|
2018-07-14 11:26:55 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
# define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
2017-09-25 19:13:59 +00:00
|
|
|
#include <inttypes.h>
|
2017-10-30 20:51:24 +00:00
|
|
|
#include <stdio.h>
|
2019-08-14 00:22:45 +00:00
|
|
|
#include <stdlib.h>
|
2017-09-10 15:46:20 +00:00
|
|
|
|
|
|
|
#include "TracySystem.hpp"
|
|
|
|
|
2021-10-07 21:28:40 +00:00
|
|
|
#if defined _WIN32
|
2020-04-07 19:33:03 +00:00
|
|
|
extern "C" typedef HRESULT (WINAPI *t_SetThreadDescription)( HANDLE, PCWSTR );
|
|
|
|
extern "C" typedef HRESULT (WINAPI *t_GetThreadDescription)( HANDLE, PWSTR* );
|
|
|
|
#endif
|
|
|
|
|
2019-08-14 14:52:04 +00:00
|
|
|
#ifdef TRACY_ENABLE
|
2017-10-30 19:45:00 +00:00
|
|
|
# include <atomic>
|
2017-11-02 11:57:02 +00:00
|
|
|
# include "TracyAlloc.hpp"
|
2017-10-30 19:45:00 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-10 15:46:20 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2020-02-11 13:40:49 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
2021-10-07 22:42:52 +00:00
|
|
|
TRACY_API uint32_t GetThreadHandleImpl()
|
2020-02-11 13:40:49 +00:00
|
|
|
{
|
2021-10-07 21:28:40 +00:00
|
|
|
#if defined _WIN32
|
2021-10-07 22:42:52 +00:00
|
|
|
static_assert( sizeof( decltype( GetCurrentThreadId() ) ) <= sizeof( uint32_t ), "Thread handle too big to fit in protocol" );
|
|
|
|
return uint32_t( GetCurrentThreadId() );
|
2020-02-11 13:40:49 +00:00
|
|
|
#elif defined __APPLE__
|
|
|
|
uint64_t id;
|
|
|
|
pthread_threadid_np( pthread_self(), &id );
|
2021-10-07 22:42:52 +00:00
|
|
|
return uint32_t( id );
|
2020-02-11 13:40:49 +00:00
|
|
|
#elif defined __ANDROID__
|
2021-10-07 22:42:52 +00:00
|
|
|
return (uint32_t)gettid();
|
2020-02-11 13:40:49 +00:00
|
|
|
#elif defined __linux__
|
2021-10-07 22:42:52 +00:00
|
|
|
return (uint32_t)syscall( SYS_gettid );
|
2020-02-11 13:40:49 +00:00
|
|
|
#elif defined __FreeBSD__
|
|
|
|
long id;
|
|
|
|
thr_self( &id );
|
|
|
|
return id;
|
|
|
|
#elif defined __NetBSD__
|
|
|
|
return _lwp_self();
|
|
|
|
#elif defined __DragonFly__
|
|
|
|
return lwp_gettid();
|
|
|
|
#elif defined __OpenBSD__
|
|
|
|
return getthrid();
|
2022-07-19 13:49:29 +00:00
|
|
|
#elif defined __EMSCRIPTEN__
|
|
|
|
// Not supported, but let it compile.
|
|
|
|
return 0;
|
2020-02-11 13:40:49 +00:00
|
|
|
#else
|
2021-10-07 22:42:52 +00:00
|
|
|
// 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!"
|
2020-02-11 13:40:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-14 14:52:04 +00:00
|
|
|
#ifdef TRACY_ENABLE
|
2017-10-30 19:45:00 +00:00
|
|
|
struct ThreadNameData
|
|
|
|
{
|
2021-10-07 22:42:52 +00:00
|
|
|
uint32_t id;
|
2017-10-30 19:45:00 +00:00
|
|
|
const char* name;
|
|
|
|
ThreadNameData* next;
|
|
|
|
};
|
2020-01-25 15:17:03 +00:00
|
|
|
std::atomic<ThreadNameData*>& GetThreadNameData();
|
2017-10-30 19:45:00 +00:00
|
|
|
#endif
|
|
|
|
|
2021-04-29 17:10:30 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma pack( push, 8 )
|
|
|
|
struct THREADNAME_INFO
|
|
|
|
{
|
|
|
|
DWORD dwType;
|
|
|
|
LPCSTR szName;
|
|
|
|
DWORD dwThreadID;
|
|
|
|
DWORD dwFlags;
|
|
|
|
};
|
2022-08-03 04:42:21 +00:00
|
|
|
# pragma pack( pop )
|
2021-04-29 17:10:30 +00:00
|
|
|
|
|
|
|
void ThreadNameMsvcMagic( const THREADNAME_INFO& info )
|
|
|
|
{
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-25 15:17:03 +00:00
|
|
|
TRACY_API void SetThreadName( const char* name )
|
2017-09-10 15:46:20 +00:00
|
|
|
{
|
2021-10-07 21:28:40 +00:00
|
|
|
#if defined _WIN32
|
2022-02-10 19:26:45 +00:00
|
|
|
# ifdef TRACY_UWP
|
2022-02-09 17:25:04 +00:00
|
|
|
static auto _SetThreadDescription = &::SetThreadDescription;
|
|
|
|
# else
|
2020-04-07 19:33:03 +00:00
|
|
|
static auto _SetThreadDescription = (t_SetThreadDescription)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "SetThreadDescription" );
|
2022-02-09 17:25:04 +00:00
|
|
|
# endif
|
2020-04-07 19:33:03 +00:00
|
|
|
if( _SetThreadDescription )
|
|
|
|
{
|
|
|
|
wchar_t buf[256];
|
|
|
|
mbstowcs( buf, name, 256 );
|
|
|
|
_SetThreadDescription( GetCurrentThread(), buf );
|
|
|
|
}
|
|
|
|
else
|
2017-09-21 22:37:28 +00:00
|
|
|
{
|
2020-04-07 19:33:03 +00:00
|
|
|
# if defined _MSC_VER
|
|
|
|
THREADNAME_INFO info;
|
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = name;
|
2021-04-29 17:10:30 +00:00
|
|
|
info.dwThreadID = GetCurrentThreadId();
|
2020-04-07 19:33:03 +00:00
|
|
|
info.dwFlags = 0;
|
2021-04-29 17:10:30 +00:00
|
|
|
ThreadNameMsvcMagic( info );
|
2017-09-21 22:37:28 +00:00
|
|
|
# endif
|
2020-04-07 19:33:03 +00:00
|
|
|
}
|
2021-10-07 21:28:40 +00:00
|
|
|
#elif defined _GNU_SOURCE && !defined __EMSCRIPTEN__
|
2017-09-22 19:45:35 +00:00
|
|
|
{
|
2018-06-30 13:36:06 +00:00
|
|
|
const auto sz = strlen( name );
|
|
|
|
if( sz <= 15 )
|
|
|
|
{
|
2022-02-28 16:05:36 +00:00
|
|
|
#if defined __APPLE__
|
|
|
|
pthread_setname_np( name );
|
|
|
|
#else
|
2019-08-14 00:22:45 +00:00
|
|
|
pthread_setname_np( pthread_self(), name );
|
2022-02-28 16:05:36 +00:00
|
|
|
#endif
|
2018-06-30 13:36:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
memcpy( buf, name, 15 );
|
|
|
|
buf[15] = '\0';
|
2022-02-28 16:05:36 +00:00
|
|
|
#if defined __APPLE__
|
|
|
|
pthread_setname_np( buf );
|
|
|
|
#else
|
2019-08-14 00:22:45 +00:00
|
|
|
pthread_setname_np( pthread_self(), buf );
|
2022-02-28 16:05:36 +00:00
|
|
|
#endif
|
2018-06-30 13:36:06 +00:00
|
|
|
}
|
2017-09-22 19:45:35 +00:00
|
|
|
}
|
2017-09-10 15:46:20 +00:00
|
|
|
#endif
|
2019-08-14 14:52:04 +00:00
|
|
|
#ifdef TRACY_ENABLE
|
2017-10-30 19:45:00 +00:00
|
|
|
{
|
2018-06-30 13:36:06 +00:00
|
|
|
const auto sz = strlen( name );
|
|
|
|
char* buf = (char*)tracy_malloc( sz+1 );
|
|
|
|
memcpy( buf, name, sz );
|
2020-06-10 10:21:58 +00:00
|
|
|
buf[sz] = '\0';
|
2021-06-09 23:48:11 +00:00
|
|
|
auto data = (ThreadNameData*)tracy_malloc_fast( sizeof( ThreadNameData ) );
|
2019-08-14 00:22:45 +00:00
|
|
|
data->id = detail::GetThreadHandleImpl();
|
2017-10-30 19:45:00 +00:00
|
|
|
data->name = buf;
|
2019-02-19 18:33:37 +00:00
|
|
|
data->next = GetThreadNameData().load( std::memory_order_relaxed );
|
|
|
|
while( !GetThreadNameData().compare_exchange_weak( data->next, data, std::memory_order_release, std::memory_order_relaxed ) ) {}
|
2017-10-30 19:45:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-09-10 15:46:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 22:42:52 +00:00
|
|
|
TRACY_API const char* GetThreadName( uint32_t id )
|
2017-09-25 19:13:59 +00:00
|
|
|
{
|
|
|
|
static char buf[256];
|
2019-08-14 14:52:04 +00:00
|
|
|
#ifdef TRACY_ENABLE
|
2019-02-19 18:33:37 +00:00
|
|
|
auto ptr = GetThreadNameData().load( std::memory_order_relaxed );
|
2017-10-30 19:45:00 +00:00
|
|
|
while( ptr )
|
|
|
|
{
|
|
|
|
if( ptr->id == id )
|
|
|
|
{
|
2018-03-30 23:38:57 +00:00
|
|
|
return ptr->name;
|
2017-10-30 19:45:00 +00:00
|
|
|
}
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
#else
|
2021-10-07 21:28:40 +00:00
|
|
|
# if defined _WIN32
|
2022-02-10 19:26:45 +00:00
|
|
|
# ifdef TRACY_UWP
|
2022-02-09 17:25:04 +00:00
|
|
|
static auto _GetThreadDescription = &::GetThreadDescription;
|
|
|
|
# else
|
2020-04-07 19:33:03 +00:00
|
|
|
static auto _GetThreadDescription = (t_GetThreadDescription)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "GetThreadDescription" );
|
2022-02-09 17:25:04 +00:00
|
|
|
# endif
|
2020-04-07 19:33:03 +00:00
|
|
|
if( _GetThreadDescription )
|
2017-09-25 19:13:59 +00:00
|
|
|
{
|
2020-04-07 19:33:03 +00:00
|
|
|
auto hnd = OpenThread( THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)id );
|
|
|
|
if( hnd != 0 )
|
2017-10-15 11:26:05 +00:00
|
|
|
{
|
2020-04-07 19:33:03 +00:00
|
|
|
PWSTR tmp;
|
|
|
|
_GetThreadDescription( hnd, &tmp );
|
|
|
|
auto ret = wcstombs( buf, tmp, 256 );
|
|
|
|
CloseHandle( hnd );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
2017-10-15 11:26:05 +00:00
|
|
|
}
|
2017-09-25 19:13:59 +00:00
|
|
|
}
|
2018-07-14 11:26:55 +00:00
|
|
|
# elif defined __linux__
|
|
|
|
int cs, fd;
|
|
|
|
char path[32];
|
2018-07-24 11:43:25 +00:00
|
|
|
# ifdef __ANDROID__
|
|
|
|
int tid = gettid();
|
|
|
|
# else
|
2018-07-14 11:46:25 +00:00
|
|
|
int tid = (int) syscall( SYS_gettid );
|
2018-07-24 11:43:25 +00:00
|
|
|
# endif
|
2018-07-14 11:46:25 +00:00
|
|
|
snprintf( path, sizeof( path ), "/proc/self/task/%d/comm", tid );
|
2021-10-07 22:42:52 +00:00
|
|
|
sprintf( buf, "%" PRIu32, id );
|
2018-09-07 15:21:27 +00:00
|
|
|
# ifndef __ANDROID__
|
2018-07-14 11:26:55 +00:00
|
|
|
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &cs );
|
2018-09-07 15:21:27 +00:00
|
|
|
# endif
|
2018-07-14 11:46:25 +00:00
|
|
|
if ( ( fd = open( path, O_RDONLY ) ) > 0) {
|
2018-07-14 11:26:55 +00:00
|
|
|
int len = read( fd, buf, 255 );
|
2019-07-03 14:12:14 +00:00
|
|
|
if( len > 0 )
|
|
|
|
{
|
2018-07-14 11:26:55 +00:00
|
|
|
buf[len] = 0;
|
2019-07-03 14:12:14 +00:00
|
|
|
if( len > 1 && buf[len-1] == '\n' )
|
|
|
|
{
|
|
|
|
buf[len-1] = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 11:46:25 +00:00
|
|
|
close( fd );
|
2018-07-14 11:26:55 +00:00
|
|
|
}
|
2018-09-07 15:21:27 +00:00
|
|
|
# ifndef __ANDROID__
|
2018-07-14 11:46:25 +00:00
|
|
|
pthread_setcancelstate( cs, 0 );
|
2018-09-07 15:21:27 +00:00
|
|
|
# endif
|
2018-07-14 11:26:55 +00:00
|
|
|
return buf;
|
2017-10-30 19:45:00 +00:00
|
|
|
# endif
|
2017-09-25 19:13:59 +00:00
|
|
|
#endif
|
2021-10-07 22:42:52 +00:00
|
|
|
sprintf( buf, "%" PRIu32, id );
|
2017-09-25 19:13:59 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2021-04-09 22:33:01 +00:00
|
|
|
TRACY_API const char* GetEnvVar( const char* name )
|
|
|
|
{
|
2021-06-15 22:52:12 +00:00
|
|
|
#if defined _WIN32
|
2021-04-09 22:33:01 +00:00
|
|
|
// unfortunately getenv() on Windows is just fundamentally broken. It caches the entire
|
|
|
|
// environment block once on startup, then never refreshes it again. If any environment
|
|
|
|
// strings are added or modified after startup of the CRT, those changes will not be
|
|
|
|
// seen by getenv(). This removes the possibility of an app using this SDK from
|
|
|
|
// programmatically setting any of the behaviour controlling envvars here.
|
|
|
|
//
|
|
|
|
// To work around this, we'll instead go directly to the Win32 environment strings APIs
|
|
|
|
// to get the current value.
|
|
|
|
static char buffer[1024];
|
|
|
|
DWORD const kBufferSize = DWORD(sizeof(buffer) / sizeof(buffer[0]));
|
|
|
|
DWORD count = GetEnvironmentVariableA(name, buffer, kBufferSize);
|
|
|
|
|
|
|
|
if( count == 0 )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if( count >= kBufferSize )
|
|
|
|
{
|
|
|
|
char* buf = reinterpret_cast<char*>(_alloca(count + 1));
|
|
|
|
count = GetEnvironmentVariableA(name, buf, count + 1);
|
|
|
|
memcpy(buffer, buf, kBufferSize);
|
|
|
|
buffer[kBufferSize - 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
#else
|
|
|
|
return getenv(name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:48:21 +00:00
|
|
|
}
|
2020-08-03 15:31:00 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TRACY_API void ___tracy_set_thread_name( const char* name ) { tracy::SetThreadName( name ); }
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|