From cf3d8eb810066740f7e2fb1b17d7b4e51d10876d Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 30 Oct 2017 20:45:00 +0100 Subject: [PATCH] Manual collection of thread names on android. --- client/TracyProfiler.cpp | 6 ++++ common/TracySystem.cpp | 62 +++++++++++++++++++++++++++++++--------- common/TracySystem.hpp | 6 ++++ 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 9b4dca5a..681394c3 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -98,6 +98,12 @@ static InitTimeWrapper init_order(101) s_initTime { Profiler::GetTime() }; static RPMallocInit init_order(102) s_rpmalloc_init; moodycamel::ConcurrentQueue init_order(103) s_queue( QueuePrealloc ); std::atomic init_order(104) s_lockCounter( 0 ); + +#ifdef TRACY_COLLECT_THREAD_NAMES +struct ThreadNameData; +std::atomic init_order(104) s_threadNameData( nullptr ); +#endif + static Profiler init_order(105) s_profiler; diff --git a/common/TracySystem.cpp b/common/TracySystem.cpp index 90c1fb1b..b70738e8 100644 --- a/common/TracySystem.cpp +++ b/common/TracySystem.cpp @@ -6,17 +6,28 @@ # include #endif -#ifdef __ANDROID__ -# include -#endif - #include #include "TracySystem.hpp" +#ifdef TRACY_COLLECT_THREAD_NAMES +# include +# include "../client/tracy_rpmalloc.hpp" +#endif + namespace tracy { +#ifdef TRACY_COLLECT_THREAD_NAMES +struct ThreadNameData +{ + uint64_t id; + const char* name; + ThreadNameData* next; +}; +extern std::atomic s_threadNameData; +#endif + void SetThreadName( std::thread& thread, const char* name ) { SetThreadName( thread.native_handle(), name ); @@ -70,13 +81,42 @@ void SetThreadName( std::thread::native_handle_type handle, const char* name ) pthread_setname_np( handle, buf ); } #endif +#ifdef TRACY_COLLECT_THREAD_NAMES + { + const auto sz = strlen( name ); + char* buf = (char*)rpmalloc( sz+1 ); + memcpy( buf, name, sz ); + buf[sz+1] = '\0'; + auto data = (ThreadNameData*)rpmalloc( sizeof( ThreadNameData ) ); +# ifdef _WIN32 + data->id = GetThreadId( static_cast( handle ) ); +# else + data->id = (uint64_t)handle; +# endif + data->name = buf; + data->next = s_threadNameData.load( std::memory_order_relaxed ); + while( !s_threadNameData.compare_exchange_weak( data->next, data, std::memory_order_release, std::memory_order_relaxed ) ) {} + } +#endif } const char* GetThreadName( uint64_t id ) { static char buf[256]; -#ifdef _WIN32 -# ifdef NTDDI_WIN10_RS2 +#ifdef TRACY_COLLECT_THREAD_NAMES + auto ptr = s_threadNameData.load( std::memory_order_relaxed ); + while( ptr ) + { + if( ptr->id == id ) + { + strcpy( buf, ptr->name ); + return buf; + } + ptr = ptr->next; + } +#else +# ifdef _WIN32 +# ifdef NTDDI_WIN10_RS2 auto hnd = OpenThread( THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)id ); if( hnd != 0 ) { @@ -89,17 +129,13 @@ const char* GetThreadName( uint64_t id ) return buf; } } -# endif -#elif defined __ANDROID__ - if( prctl( PR_GET_NAME, (unsigned long)buf, 0, 0, 0 ) == 0 ) - { - return buf; - } -#elif defined _GNU_SOURCE +# endif +# elif defined _GNU_SOURCE if( pthread_getname_np( (pthread_t)id, buf, 256 ) == 0 ) { return buf; } +# endif #endif sprintf( buf, "%" PRIu64, id ); return buf; diff --git a/common/TracySystem.hpp b/common/TracySystem.hpp index bc3a442b..950c2b6e 100644 --- a/common/TracySystem.hpp +++ b/common/TracySystem.hpp @@ -1,6 +1,12 @@ #ifndef __TRACYSYSTEM_HPP__ #define __TRACYSYSTEM_HPP__ +#ifdef TRACY_ENABLE +# ifdef __ANDROID__ +# define TRACY_COLLECT_THREAD_NAMES +# endif +#endif + #ifdef _WIN32 extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void); #else