2017-10-16 19:34:39 +00:00
|
|
|
#ifdef TRACY_ENABLE
|
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
#ifdef _WIN32
|
2018-12-24 18:49:53 +00:00
|
|
|
# ifndef NOMINMAX
|
|
|
|
# define NOMINMAX
|
|
|
|
# endif
|
2018-12-24 18:50:52 +00:00
|
|
|
# include <winsock2.h>
|
|
|
|
# include <windows.h>
|
|
|
|
# include <tlhelp32.h>
|
2019-01-20 16:17:09 +00:00
|
|
|
# include <inttypes.h>
|
2017-09-14 17:25:16 +00:00
|
|
|
#else
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 16:15:14 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2017-10-03 21:17:16 +00:00
|
|
|
#ifdef _GNU_SOURCE
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-20 12:30:56 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <signal.h>
|
|
|
|
# include <dirent.h>
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
2019-02-20 12:13:29 +00:00
|
|
|
#ifdef __APPLE__
|
2019-02-20 12:52:55 +00:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/sysctl.h>
|
2019-02-20 12:13:29 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-23 19:33:05 +00:00
|
|
|
#include <atomic>
|
2017-09-10 15:43:56 +00:00
|
|
|
#include <assert.h>
|
2017-09-23 19:09:46 +00:00
|
|
|
#include <chrono>
|
2017-09-14 17:25:16 +00:00
|
|
|
#include <limits>
|
2017-09-21 22:36:36 +00:00
|
|
|
#include <memory>
|
2018-04-01 18:04:35 +00:00
|
|
|
#include <mutex>
|
2017-10-30 13:59:05 +00:00
|
|
|
#include <stdlib.h>
|
2017-09-19 00:19:20 +00:00
|
|
|
#include <string.h>
|
2017-09-10 15:43:56 +00:00
|
|
|
|
2018-03-31 12:03:55 +00:00
|
|
|
#include "../common/TracyAlign.hpp"
|
2017-09-13 20:56:55 +00:00
|
|
|
#include "../common/TracyProtocol.hpp"
|
2017-09-11 20:51:47 +00:00
|
|
|
#include "../common/TracySocket.hpp"
|
2017-09-12 23:31:55 +00:00
|
|
|
#include "../common/TracySystem.hpp"
|
2017-10-14 14:59:43 +00:00
|
|
|
#include "tracy_rpmalloc.hpp"
|
2018-06-18 23:17:19 +00:00
|
|
|
#include "TracyCallstack.hpp"
|
2017-09-24 14:02:09 +00:00
|
|
|
#include "TracyScoped.hpp"
|
2017-09-10 15:43:56 +00:00
|
|
|
#include "TracyProfiler.hpp"
|
2017-10-16 19:13:57 +00:00
|
|
|
#include "TracyThread.hpp"
|
2019-01-14 19:55:37 +00:00
|
|
|
#include "../TracyC.h"
|
2017-09-10 15:43:56 +00:00
|
|
|
|
2019-02-19 20:51:41 +00:00
|
|
|
#ifdef __APPLE__
|
2019-02-19 19:43:14 +00:00
|
|
|
# define TRACY_DELAYED_INIT
|
2017-10-08 22:39:12 +00:00
|
|
|
#else
|
2019-02-19 19:43:14 +00:00
|
|
|
# ifdef __GNUC__
|
|
|
|
# define init_order( val ) __attribute__ ((init_priority(val)))
|
|
|
|
# else
|
|
|
|
# define init_order(x)
|
|
|
|
# endif
|
2017-10-08 22:39:12 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-21 13:45:13 +00:00
|
|
|
#if defined TRACY_HW_TIMER && __ARM_ARCH >= 6 && !defined TARGET_OS_IOS
|
2018-04-27 14:58:45 +00:00
|
|
|
# include <signal.h>
|
|
|
|
# include <setjmp.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2018-08-19 16:15:14 +00:00
|
|
|
# include <lmcons.h>
|
2018-08-19 16:43:26 +00:00
|
|
|
extern "C" typedef LONG (WINAPI *t_RtlGetVersion)( PRTL_OSVERSIONINFOW );
|
2018-11-25 18:03:17 +00:00
|
|
|
# if _WIN32_WINNT >= _WIN32_WINNT_VISTA
|
|
|
|
# define TRACY_USE_INIT_ONCE
|
|
|
|
# endif
|
2018-08-19 16:15:14 +00:00
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <limits.h>
|
|
|
|
#endif
|
|
|
|
#if defined __APPLE__
|
|
|
|
# include "TargetConditionals.h"
|
|
|
|
#endif
|
2018-08-19 16:52:04 +00:00
|
|
|
#if defined __linux__
|
|
|
|
# include <sys/sysinfo.h>
|
2018-08-19 17:00:01 +00:00
|
|
|
# include <sys/utsname.h>
|
2018-08-19 16:52:04 +00:00
|
|
|
#endif
|
2018-08-19 16:15:14 +00:00
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
#ifndef TRACY_DELAYED_INIT
|
|
|
|
# if defined TRACY_USE_INIT_ONCE
|
2018-10-28 01:13:59 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
BOOL CALLBACK InitOnceCallback(
|
2019-02-20 15:28:27 +00:00
|
|
|
PINIT_ONCE /*initOnce*/,
|
|
|
|
PVOID /*Parameter*/,
|
|
|
|
PVOID* /*Context*/)
|
2018-10-28 01:13:59 +00:00
|
|
|
{
|
|
|
|
rpmalloc_initialize();
|
|
|
|
return TRUE;
|
|
|
|
}
|
2018-10-29 01:55:55 +00:00
|
|
|
|
|
|
|
INIT_ONCE InitOnce = INIT_ONCE_STATIC_INIT;
|
2018-10-28 01:13:59 +00:00
|
|
|
}
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif //if defined TRACY_USE_INIT_ONCE
|
2018-10-28 01:13:59 +00:00
|
|
|
|
2018-10-29 01:55:55 +00:00
|
|
|
struct RPMallocInit
|
|
|
|
{
|
|
|
|
RPMallocInit()
|
|
|
|
{
|
2019-02-19 19:43:14 +00:00
|
|
|
# if defined TRACY_USE_INIT_ONCE
|
2018-10-29 01:55:55 +00:00
|
|
|
InitOnceExecuteOnce(&InitOnce, InitOnceCallback, nullptr, nullptr);
|
2018-12-17 14:09:10 +00:00
|
|
|
//We must call rpmalloc_thread_initialize() explicitly here since the InitOnceCallback might
|
2018-10-29 01:55:55 +00:00
|
|
|
//not be called on this thread if another thread has executed it earlier.
|
|
|
|
rpmalloc_thread_initialize();
|
2019-02-19 19:43:14 +00:00
|
|
|
# else
|
2018-10-29 01:55:55 +00:00
|
|
|
rpmalloc_initialize();
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif //if defined TRACY_USE_INIT_ONCE
|
2018-10-29 01:55:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-14 14:59:43 +00:00
|
|
|
struct RPMallocThreadInit
|
|
|
|
{
|
2018-10-28 01:13:59 +00:00
|
|
|
RPMallocThreadInit()
|
|
|
|
{
|
2019-02-19 19:43:14 +00:00
|
|
|
# if defined TRACY_USE_INIT_ONCE
|
2018-10-28 01:13:59 +00:00
|
|
|
InitOnceExecuteOnce(&InitOnce, InitOnceCallback, nullptr, nullptr);
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif //if defined TRACY_USE_INIT_ONCE
|
2018-10-28 01:13:59 +00:00
|
|
|
rpmalloc_thread_initialize();
|
|
|
|
}
|
2017-10-14 14:59:43 +00:00
|
|
|
};
|
|
|
|
|
2017-10-16 23:07:34 +00:00
|
|
|
struct InitTimeWrapper
|
|
|
|
{
|
|
|
|
int64_t val;
|
|
|
|
};
|
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
struct ProducerWrapper
|
|
|
|
{
|
|
|
|
tracy::moodycamel::ConcurrentQueue<QueueItem>::ExplicitProducer* ptr;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-02-21 13:45:13 +00:00
|
|
|
#if defined TRACY_HW_TIMER && __ARM_ARCH >= 6 && !defined TARGET_OS_IOS
|
2018-04-27 14:58:45 +00:00
|
|
|
int64_t (*GetTimeImpl)();
|
|
|
|
|
|
|
|
int64_t GetTimeImplFallback()
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GetTimeImplCntvct()
|
|
|
|
{
|
|
|
|
int64_t t;
|
|
|
|
# ifdef __aarch64__
|
|
|
|
asm volatile ( "mrs %0, cntvct_el0" : "=r" (t) );
|
|
|
|
# else
|
|
|
|
asm volatile ( "mrrc p15, 1, %Q0, %R0, c14" : "=r" (t) );
|
|
|
|
# endif
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sigjmp_buf SigIllEnv;
|
|
|
|
|
|
|
|
static int SetupHwTimerFailed()
|
|
|
|
{
|
|
|
|
return sigsetjmp( SigIllEnv, 1 );
|
|
|
|
}
|
|
|
|
|
2018-08-01 12:07:30 +00:00
|
|
|
static void SetupHwTimerSigIllHandler( int /*signum*/ )
|
2018-04-27 14:58:45 +00:00
|
|
|
{
|
|
|
|
siglongjmp( SigIllEnv, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t SetupHwTimer()
|
|
|
|
{
|
|
|
|
struct sigaction act, oldact;
|
|
|
|
memset( &act, 0, sizeof( act ) );
|
|
|
|
act.sa_handler = SetupHwTimerSigIllHandler;
|
|
|
|
|
|
|
|
if( sigaction( SIGILL, &act, &oldact ) )
|
|
|
|
{
|
|
|
|
GetTimeImpl = GetTimeImplFallback;
|
|
|
|
return Profiler::GetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( SetupHwTimerFailed() )
|
|
|
|
{
|
|
|
|
sigaction( SIGILL, &oldact, nullptr );
|
|
|
|
GetTimeImpl = GetTimeImplFallback;
|
|
|
|
return Profiler::GetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
GetTimeImplCntvct();
|
|
|
|
|
|
|
|
sigaction( SIGILL, &oldact, nullptr );
|
|
|
|
GetTimeImpl = GetTimeImplCntvct;
|
|
|
|
return Profiler::GetTime();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int64_t SetupHwTimer()
|
|
|
|
{
|
|
|
|
return Profiler::GetTime();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-03 21:17:16 +00:00
|
|
|
static const char* GetProcessName()
|
|
|
|
{
|
2018-08-01 17:18:40 +00:00
|
|
|
const char* processName = "unknown";
|
2019-01-19 11:03:30 +00:00
|
|
|
#ifdef _WIN32
|
2017-10-03 21:17:16 +00:00
|
|
|
static char buf[_MAX_PATH];
|
|
|
|
GetModuleFileNameA( nullptr, buf, _MAX_PATH );
|
|
|
|
const char* ptr = buf;
|
|
|
|
while( *ptr != '\0' ) ptr++;
|
|
|
|
while( ptr > buf && *ptr != '\\' && *ptr != '/' ) ptr--;
|
|
|
|
if( ptr > buf ) ptr++;
|
2018-08-01 17:18:40 +00:00
|
|
|
processName = ptr;
|
2017-10-30 13:59:05 +00:00
|
|
|
#elif defined __ANDROID__
|
|
|
|
# if __ANDROID_API__ >= 21
|
|
|
|
auto buf = getprogname();
|
2018-08-01 17:18:40 +00:00
|
|
|
if( buf ) processName = buf;
|
2017-10-30 13:59:05 +00:00
|
|
|
# endif
|
2017-11-10 18:46:31 +00:00
|
|
|
#elif defined _GNU_SOURCE || defined __CYGWIN__
|
2018-08-01 17:18:40 +00:00
|
|
|
processName = program_invocation_short_name;
|
2019-02-20 12:13:29 +00:00
|
|
|
#elif defined __APPLE__
|
2019-02-21 10:40:56 +00:00
|
|
|
auto buf = getprogname();
|
|
|
|
if( buf ) processName = buf;
|
2018-08-01 12:07:30 +00:00
|
|
|
#endif
|
2018-08-01 17:18:40 +00:00
|
|
|
return processName;
|
2017-10-03 21:17:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 16:15:14 +00:00
|
|
|
static const char* GetHostInfo()
|
|
|
|
{
|
|
|
|
static char buf[1024];
|
|
|
|
auto ptr = buf;
|
2019-01-19 11:03:30 +00:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2018-08-19 16:43:26 +00:00
|
|
|
# ifdef UNICODE
|
|
|
|
t_RtlGetVersion RtlGetVersion = (t_RtlGetVersion)GetProcAddress( GetModuleHandle( L"ntdll.dll" ), "RtlGetVersion" );
|
|
|
|
# else
|
|
|
|
t_RtlGetVersion RtlGetVersion = (t_RtlGetVersion)GetProcAddress( GetModuleHandle( "ntdll.dll" ), "RtlGetVersion" );
|
|
|
|
# endif
|
|
|
|
|
|
|
|
if( !RtlGetVersion )
|
|
|
|
{
|
2019-01-19 11:03:30 +00:00
|
|
|
# ifdef __CYGWIN__
|
2018-08-19 16:43:26 +00:00
|
|
|
ptr += sprintf( ptr, "OS: Windows (Cygwin)\n" );
|
2019-01-19 11:03:30 +00:00
|
|
|
# elif defined __MINGW32__
|
|
|
|
ptr += sprintf( ptr, "OS: Windows (MingW)\n" );
|
|
|
|
# else
|
|
|
|
ptr += sprintf( ptr, "OS: Windows\n" );
|
2018-08-19 16:43:26 +00:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RTL_OSVERSIONINFOW ver = { sizeof( RTL_OSVERSIONINFOW ) };
|
|
|
|
RtlGetVersion( &ver );
|
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
# ifdef __CYGWIN__
|
2018-08-19 16:43:26 +00:00
|
|
|
ptr += sprintf( ptr, "OS: Windows %i.%i.%i (Cygwin)\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber );
|
2019-01-19 11:03:30 +00:00
|
|
|
# elif defined __MINGW32__
|
|
|
|
ptr += sprintf( ptr, "OS: Windows %i.%i.%i (MingW)\n", (int)ver.dwMajorVersion, (int)ver.dwMinorVersion, (int)ver.dwBuildNumber );
|
|
|
|
# else
|
|
|
|
ptr += sprintf( ptr, "OS: Windows %i.%i.%i\n", ver.dwMajorVersion, ver.dwMinorVersion, ver.dwBuildNumber );
|
2018-08-19 16:43:26 +00:00
|
|
|
# endif
|
|
|
|
}
|
2018-08-19 16:15:14 +00:00
|
|
|
#elif defined __linux__
|
2018-08-19 17:00:01 +00:00
|
|
|
struct utsname utsName;
|
|
|
|
uname( &utsName );
|
2018-08-19 16:15:14 +00:00
|
|
|
# if defined __ANDROID__
|
2018-08-19 17:00:01 +00:00
|
|
|
ptr += sprintf( ptr, "OS: Linux %s (Android)\n", utsName.release );
|
2018-08-19 16:15:14 +00:00
|
|
|
# else
|
2018-08-19 17:00:01 +00:00
|
|
|
ptr += sprintf( ptr, "OS: Linux %s\n", utsName.release );
|
2018-08-19 16:15:14 +00:00
|
|
|
# endif
|
|
|
|
#elif defined __APPLE__
|
2019-02-20 11:43:48 +00:00
|
|
|
# if TARGET_OS_IPHONE == 1
|
2018-08-19 16:15:14 +00:00
|
|
|
ptr += sprintf( ptr, "OS: Darwin (iOS)\n" );
|
2019-02-20 11:43:48 +00:00
|
|
|
# elif TARGET_OS_MAC == 1
|
2018-08-19 16:15:14 +00:00
|
|
|
ptr += sprintf( ptr, "OS: Darwin (OSX)\n" );
|
|
|
|
# else
|
|
|
|
ptr += sprintf( ptr, "OS: Darwin (unknown)\n" );
|
|
|
|
# endif
|
|
|
|
#elif defined __DragonFly__
|
|
|
|
ptr += sprintf( ptr, "OS: BSD (DragonFly)\n" );
|
|
|
|
#elif defined __FreeBSD__
|
|
|
|
ptr += sprintf( ptr, "OS: BSD (FreeBSD)\n" );
|
|
|
|
#elif defined __NetBSD__
|
|
|
|
ptr += sprintf( ptr, "OS: BSD (NetBSD)\n" );
|
|
|
|
#elif defined __OpenBSD__
|
|
|
|
ptr += sprintf( ptr, "OS: BSD (OpenBSD)\n" );
|
|
|
|
#else
|
|
|
|
ptr += sprintf( ptr, "OS: unknown\n" );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined _MSC_VER
|
|
|
|
ptr += sprintf( ptr, "Compiler: MSVC %i\n", _MSC_VER );
|
|
|
|
#elif defined __clang__
|
|
|
|
ptr += sprintf( ptr, "Compiler: clang %i.%i.%i\n", __clang_major__, __clang_minor__, __clang_patchlevel__ );
|
|
|
|
#elif defined __GNUC__
|
|
|
|
ptr += sprintf( ptr, "Compiler: gcc %i.%i\n", __GNUC__, __GNUC_MINOR__ );
|
|
|
|
#else
|
|
|
|
ptr += sprintf( ptr, "Compiler: unknown\n" );
|
|
|
|
#endif
|
|
|
|
|
2019-01-19 13:03:43 +00:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2018-08-19 16:15:14 +00:00
|
|
|
# ifndef __CYGWIN__
|
|
|
|
InitWinSock();
|
|
|
|
# endif
|
|
|
|
char hostname[512];
|
|
|
|
gethostname( hostname, 512 );
|
|
|
|
|
|
|
|
DWORD userSz = UNLEN+1;
|
|
|
|
char user[UNLEN+1];
|
|
|
|
GetUserNameA( user, &userSz );
|
|
|
|
|
|
|
|
ptr += sprintf( ptr, "User: %s@%s\n", user, hostname );
|
|
|
|
#else
|
2018-12-18 15:10:39 +00:00
|
|
|
char hostname[_POSIX_HOST_NAME_MAX]{};
|
|
|
|
char user[_POSIX_LOGIN_NAME_MAX]{};
|
2018-08-19 16:15:14 +00:00
|
|
|
|
2018-12-18 15:10:39 +00:00
|
|
|
gethostname( hostname, _POSIX_HOST_NAME_MAX );
|
2018-08-27 11:59:19 +00:00
|
|
|
# if defined __ANDROID__
|
|
|
|
const auto login = getlogin();
|
|
|
|
if( login )
|
|
|
|
{
|
|
|
|
strcpy( user, login );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( user, "(?)", 4 );
|
|
|
|
}
|
|
|
|
# else
|
2018-12-18 15:10:39 +00:00
|
|
|
getlogin_r( user, _POSIX_LOGIN_NAME_MAX );
|
2018-08-27 11:59:19 +00:00
|
|
|
# endif
|
2018-08-19 16:15:14 +00:00
|
|
|
|
|
|
|
ptr += sprintf( ptr, "User: %s@%s\n", user, hostname );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined __i386 || defined _M_IX86
|
|
|
|
ptr += sprintf( ptr, "Arch: x86\n" );
|
|
|
|
#elif defined __x86_64__ || defined _M_X64
|
|
|
|
ptr += sprintf( ptr, "Arch: x64\n" );
|
|
|
|
#elif defined __aarch64__
|
|
|
|
ptr += sprintf( ptr, "Arch: ARM64\n" );
|
|
|
|
#elif defined __ARM_ARCH
|
|
|
|
ptr += sprintf( ptr, "Arch: ARM\n" );
|
|
|
|
#else
|
|
|
|
ptr += sprintf( ptr, "Arch: unknown\n" );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64
|
|
|
|
uint32_t regs[4];
|
|
|
|
char cpuModel[4*4*3];
|
|
|
|
auto modelPtr = cpuModel;
|
2018-08-19 16:53:32 +00:00
|
|
|
for( uint32_t i=0x80000002; i<0x80000005; ++i )
|
2018-08-19 16:15:14 +00:00
|
|
|
{
|
2019-01-19 11:03:30 +00:00
|
|
|
# if defined _WIN32 || defined __CYGWIN__
|
2018-08-19 16:15:14 +00:00
|
|
|
__cpuidex( (int*)regs, i, 0 );
|
|
|
|
# else
|
|
|
|
int zero = 0;
|
|
|
|
asm volatile ( "cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) : "a" (i), "c" (zero) );
|
|
|
|
# endif
|
|
|
|
memcpy( modelPtr, regs, sizeof( regs ) ); modelPtr += sizeof( regs );
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr += sprintf( ptr, "CPU: %s\n", cpuModel );
|
|
|
|
#else
|
|
|
|
ptr += sprintf( ptr, "CPU: unknown\n" );
|
|
|
|
#endif
|
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2018-08-19 16:15:14 +00:00
|
|
|
MEMORYSTATUSEX statex;
|
|
|
|
statex.dwLength = sizeof( statex );
|
|
|
|
GlobalMemoryStatusEx( &statex );
|
2018-12-28 23:49:05 +00:00
|
|
|
# ifdef _MSC_VER
|
2018-08-22 14:31:09 +00:00
|
|
|
ptr += sprintf( ptr, "RAM: %I64u MB\n", statex.ullTotalPhys / 1024 / 1024 );
|
2018-12-28 23:49:05 +00:00
|
|
|
# else
|
|
|
|
ptr += sprintf( ptr, "RAM: %llu MB\n", statex.ullTotalPhys / 1024 / 1024 );
|
|
|
|
# endif
|
2018-08-19 16:52:04 +00:00
|
|
|
#elif defined __linux__
|
|
|
|
struct sysinfo sysInfo;
|
|
|
|
sysinfo( &sysInfo );
|
2018-08-20 10:53:14 +00:00
|
|
|
ptr += sprintf( ptr, "RAM: %lu MB\n", sysInfo.totalram / 1024 / 1024 );
|
2019-02-20 12:52:55 +00:00
|
|
|
#elif defined __APPLE__
|
|
|
|
size_t memSize;
|
|
|
|
size_t sz = sizeof( memSize );
|
|
|
|
sysctlbyname( "hw.memsize", &memSize, &sz, nullptr, 0 );
|
|
|
|
ptr += sprintf( ptr, "RAM: %zu MB\n", memSize / 1024 / 1024 );
|
2018-08-19 16:15:14 +00:00
|
|
|
#else
|
|
|
|
ptr += sprintf( ptr, "RAM: unknown\n" );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
#ifdef _WIN32
|
2018-08-19 23:05:09 +00:00
|
|
|
static DWORD s_profilerThreadId = 0;
|
2018-08-20 00:06:59 +00:00
|
|
|
static char s_crashText[1024];
|
2018-08-19 23:05:09 +00:00
|
|
|
|
|
|
|
LONG WINAPI CrashFilter( PEXCEPTION_POINTERS pExp )
|
|
|
|
{
|
2019-01-19 11:03:30 +00:00
|
|
|
const unsigned ec = pExp->ExceptionRecord->ExceptionCode;
|
2018-08-20 00:06:59 +00:00
|
|
|
auto msgPtr = s_crashText;
|
|
|
|
switch( ec )
|
|
|
|
{
|
|
|
|
case EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_ACCESS_VIOLATION (0x%x). ", ec );
|
|
|
|
switch( pExp->ExceptionRecord->ExceptionInformation[0] )
|
|
|
|
{
|
|
|
|
case 0:
|
2019-02-20 15:28:27 +00:00
|
|
|
msgPtr += sprintf( msgPtr, "Read violation at address 0x%" PRIxPTR ".", pExp->ExceptionRecord->ExceptionInformation[1] );
|
2018-08-20 00:06:59 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2019-02-20 15:28:27 +00:00
|
|
|
msgPtr += sprintf( msgPtr, "Write violation at address 0x%" PRIxPTR ".", pExp->ExceptionRecord->ExceptionInformation[1] );
|
2018-08-20 00:06:59 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
2019-02-20 15:28:27 +00:00
|
|
|
msgPtr += sprintf( msgPtr, "DEP violation at address 0x%" PRIxPTR ".", pExp->ExceptionRecord->ExceptionInformation[1] );
|
2018-08-20 00:06:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_ARRAY_BOUNDS_EXCEEDED (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
case EXCEPTION_DATATYPE_MISALIGNMENT:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_DATATYPE_MISALIGNMENT (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_FLT_DIVIDE_BY_ZERO (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
case EXCEPTION_ILLEGAL_INSTRUCTION:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_ILLEGAL_INSTRUCTION (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
case EXCEPTION_IN_PAGE_ERROR:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_IN_PAGE_ERROR (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_INT_DIVIDE_BY_ZERO (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
case EXCEPTION_PRIV_INSTRUCTION:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_PRIV_INSTRUCTION (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
case EXCEPTION_STACK_OVERFLOW:
|
|
|
|
msgPtr += sprintf( msgPtr, "Exception EXCEPTION_STACK_OVERFLOW (0x%x). ", ec );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
2018-08-19 23:05:09 +00:00
|
|
|
|
2018-08-20 00:07:31 +00:00
|
|
|
{
|
|
|
|
const auto thread = GetThreadHandle();
|
|
|
|
Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = GetToken();
|
2018-08-20 00:07:31 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
MemWrite( &item->hdr.type, QueueType::CrashReport );
|
|
|
|
item->crashReport.time = Profiler::GetTime();
|
|
|
|
item->crashReport.thread = thread;
|
|
|
|
item->crashReport.text = (uint64_t)s_crashText;
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().SendCallstack( 60, thread, "KiUserExceptionDispatcher" );
|
2018-08-20 00:07:31 +00:00
|
|
|
}
|
2018-08-19 23:05:09 +00:00
|
|
|
|
|
|
|
HANDLE h = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
|
|
|
|
if( h == INVALID_HANDLE_VALUE ) return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
THREADENTRY32 te = { sizeof( te ) };
|
|
|
|
if( !Thread32First( h, &te ) )
|
|
|
|
{
|
|
|
|
CloseHandle( h );
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto pid = GetCurrentProcessId();
|
|
|
|
const auto tid = GetCurrentThreadId();
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( te.th32OwnerProcessID == pid && te.th32ThreadID != tid && te.th32ThreadID != s_profilerThreadId )
|
|
|
|
{
|
|
|
|
HANDLE th = OpenThread( THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID );
|
|
|
|
if( th != INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
SuspendThread( th );
|
|
|
|
CloseHandle( th );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( Thread32Next( h, &te ) );
|
|
|
|
CloseHandle( h );
|
|
|
|
|
|
|
|
{
|
|
|
|
Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = GetToken();
|
2018-08-19 23:05:09 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
MemWrite( &item->hdr.type, QueueType::Crash );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 500 ) );
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().RequestShutdown();
|
|
|
|
while( !GetProfiler().HasShutdownFinished() ) { std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); };
|
2018-08-19 23:05:09 +00:00
|
|
|
|
|
|
|
TerminateProcess( GetCurrentProcess(), 1 );
|
|
|
|
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-20 12:30:56 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
static long s_profilerTid = 0;
|
|
|
|
static char s_crashText[1024];
|
2018-08-20 23:45:33 +00:00
|
|
|
static std::atomic<bool> s_alreadyCrashed( false );
|
2018-08-20 12:30:56 +00:00
|
|
|
|
2019-02-20 15:28:27 +00:00
|
|
|
static void ThreadFreezer( int /*signal*/ )
|
2018-08-20 12:30:56 +00:00
|
|
|
{
|
|
|
|
for(;;) sleep( 1000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void HexPrint( char*& ptr, uint64_t val )
|
|
|
|
{
|
|
|
|
if( val == 0 )
|
|
|
|
{
|
|
|
|
*ptr++ = '0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char HexTable[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
|
|
|
char buf[16];
|
|
|
|
auto bptr = buf;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*bptr++ = HexTable[val%16];
|
|
|
|
val /= 16;
|
|
|
|
}
|
|
|
|
while( val > 0 );
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*ptr++ = *--bptr;
|
|
|
|
}
|
|
|
|
while( bptr != buf );
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:28:27 +00:00
|
|
|
static void CrashHandler( int signal, siginfo_t* info, void* /*ucontext*/ )
|
2018-08-20 12:30:56 +00:00
|
|
|
{
|
2018-08-20 23:45:33 +00:00
|
|
|
bool expected = false;
|
|
|
|
if( !s_alreadyCrashed.compare_exchange_strong( expected, true ) ) ThreadFreezer( signal );
|
|
|
|
|
2018-08-20 12:30:56 +00:00
|
|
|
auto msgPtr = s_crashText;
|
|
|
|
switch( signal )
|
|
|
|
{
|
|
|
|
case SIGILL:
|
|
|
|
strcpy( msgPtr, "Illegal Instruction.\n" );
|
|
|
|
while( *msgPtr ) msgPtr++;
|
|
|
|
switch( info->si_code )
|
|
|
|
{
|
|
|
|
case ILL_ILLOPC:
|
|
|
|
strcpy( msgPtr, "Illegal opcode.\n" );
|
|
|
|
break;
|
|
|
|
case ILL_ILLOPN:
|
|
|
|
strcpy( msgPtr, "Illegal operand.\n" );
|
|
|
|
break;
|
|
|
|
case ILL_ILLADR:
|
|
|
|
strcpy( msgPtr, "Illegal addressing mode.\n" );
|
|
|
|
break;
|
|
|
|
case ILL_ILLTRP:
|
|
|
|
strcpy( msgPtr, "Illegal trap.\n" );
|
|
|
|
break;
|
|
|
|
case ILL_PRVOPC:
|
|
|
|
strcpy( msgPtr, "Privileged opcode.\n" );
|
|
|
|
break;
|
|
|
|
case ILL_PRVREG:
|
|
|
|
strcpy( msgPtr, "Privileged register.\n" );
|
|
|
|
break;
|
|
|
|
case ILL_COPROC:
|
|
|
|
strcpy( msgPtr, "Coprocessor error.\n" );
|
|
|
|
break;
|
|
|
|
case ILL_BADSTK:
|
|
|
|
strcpy( msgPtr, "Internal stack error.\n" );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGFPE:
|
|
|
|
strcpy( msgPtr, "Floating-point exception.\n" );
|
|
|
|
while( *msgPtr ) msgPtr++;
|
|
|
|
switch( info->si_code )
|
|
|
|
{
|
|
|
|
case FPE_INTDIV:
|
|
|
|
strcpy( msgPtr, "Integer divide by zero.\n" );
|
|
|
|
break;
|
|
|
|
case FPE_INTOVF:
|
|
|
|
strcpy( msgPtr, "Integer overflow.\n" );
|
|
|
|
break;
|
|
|
|
case FPE_FLTDIV:
|
|
|
|
strcpy( msgPtr, "Floating-point divide by zero.\n" );
|
|
|
|
break;
|
|
|
|
case FPE_FLTOVF:
|
|
|
|
strcpy( msgPtr, "Floating-point overflow.\n" );
|
|
|
|
break;
|
|
|
|
case FPE_FLTUND:
|
|
|
|
strcpy( msgPtr, "Floating-point underflow.\n" );
|
|
|
|
break;
|
|
|
|
case FPE_FLTRES:
|
|
|
|
strcpy( msgPtr, "Floating-point inexact result.\n" );
|
|
|
|
break;
|
|
|
|
case FPE_FLTINV:
|
|
|
|
strcpy( msgPtr, "Floating-point invalid operation.\n" );
|
|
|
|
break;
|
|
|
|
case FPE_FLTSUB:
|
|
|
|
strcpy( msgPtr, "Subscript out of range.\n" );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGSEGV:
|
|
|
|
strcpy( msgPtr, "Invalid memory reference.\n" );
|
|
|
|
while( *msgPtr ) msgPtr++;
|
|
|
|
switch( info->si_code )
|
|
|
|
{
|
|
|
|
case SEGV_MAPERR:
|
|
|
|
strcpy( msgPtr, "Address not mapped to object.\n" );
|
|
|
|
break;
|
|
|
|
case SEGV_ACCERR:
|
|
|
|
strcpy( msgPtr, "Invalid permissions for mapped object.\n" );
|
|
|
|
break;
|
2018-08-27 12:45:07 +00:00
|
|
|
# ifdef SEGV_BNDERR
|
2018-08-20 12:30:56 +00:00
|
|
|
case SEGV_BNDERR:
|
|
|
|
strcpy( msgPtr, "Failed address bound checks.\n" );
|
|
|
|
break;
|
2018-08-27 12:45:07 +00:00
|
|
|
# endif
|
|
|
|
# ifdef SEGV_PKUERR
|
2018-08-20 12:30:56 +00:00
|
|
|
case SEGV_PKUERR:
|
|
|
|
strcpy( msgPtr, "Access was denied by memory protection keys.\n" );
|
|
|
|
break;
|
2018-08-27 12:45:07 +00:00
|
|
|
# endif
|
2018-08-20 12:30:56 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGPIPE:
|
|
|
|
strcpy( msgPtr, "Broken pipe.\n" );
|
|
|
|
while( *msgPtr ) msgPtr++;
|
|
|
|
break;
|
|
|
|
case SIGBUS:
|
|
|
|
strcpy( msgPtr, "Bus error.\n" );
|
|
|
|
while( *msgPtr ) msgPtr++;
|
|
|
|
switch( info->si_code )
|
|
|
|
{
|
|
|
|
case BUS_ADRALN:
|
|
|
|
strcpy( msgPtr, "Invalid address alignment.\n" );
|
|
|
|
break;
|
|
|
|
case BUS_ADRERR:
|
|
|
|
strcpy( msgPtr, "Nonexistent physical address.\n" );
|
|
|
|
break;
|
|
|
|
case BUS_OBJERR:
|
|
|
|
strcpy( msgPtr, "Object-specific hardware error.\n" );
|
|
|
|
break;
|
|
|
|
case BUS_MCEERR_AR:
|
|
|
|
strcpy( msgPtr, "Hardware memory error consumed on a machine check; action required.\n" );
|
|
|
|
break;
|
|
|
|
case BUS_MCEERR_AO:
|
|
|
|
strcpy( msgPtr, "Hardware memory error detected in process but not consumed; action optional.\n" );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
while( *msgPtr ) msgPtr++;
|
|
|
|
|
|
|
|
if( signal != SIGPIPE )
|
|
|
|
{
|
|
|
|
strcpy( msgPtr, "Fault address: 0x" );
|
|
|
|
while( *msgPtr ) msgPtr++;
|
|
|
|
HexPrint( msgPtr, uint64_t( info->si_addr ) );
|
|
|
|
*msgPtr++ = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto thread = GetThreadHandle();
|
|
|
|
Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = GetToken();
|
2018-08-20 12:30:56 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
MemWrite( &item->hdr.type, QueueType::CrashReport );
|
|
|
|
item->crashReport.time = Profiler::GetTime();
|
|
|
|
item->crashReport.thread = thread;
|
|
|
|
item->crashReport.text = (uint64_t)s_crashText;
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().SendCallstack( 60, thread, "__kernel_rt_sigreturn" );
|
2018-08-20 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DIR* dp = opendir( "/proc/self/task" );
|
|
|
|
if( !dp ) abort();
|
|
|
|
|
|
|
|
const auto selfTid = syscall( SYS_gettid );
|
|
|
|
|
|
|
|
struct dirent* ep;
|
|
|
|
while( ( ep = readdir( dp ) ) != nullptr )
|
|
|
|
{
|
|
|
|
if( ep->d_name[0] == '.' ) continue;
|
|
|
|
int tid = atoi( ep->d_name );
|
|
|
|
if( tid != selfTid && tid != s_profilerTid )
|
|
|
|
{
|
|
|
|
syscall( SYS_tkill, tid, SIGPWR );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir( dp );
|
|
|
|
|
|
|
|
{
|
|
|
|
Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = GetToken();
|
2018-08-20 12:30:56 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
MemWrite( &item->hdr.type, QueueType::Crash );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 500 ) );
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().RequestShutdown();
|
|
|
|
while( !GetProfiler().HasShutdownFinished() ) { std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); };
|
2018-08-20 12:30:56 +00:00
|
|
|
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
|
|
|
|
enum { QueuePrealloc = 256 * 1024 };
|
|
|
|
|
|
|
|
static Profiler* s_instance;
|
|
|
|
static Thread* s_thread;
|
|
|
|
|
|
|
|
#ifdef TRACY_DELAYED_INIT
|
|
|
|
struct ThreadNameData;
|
|
|
|
moodycamel::ConcurrentQueue<QueueItem>& GetQueue();
|
|
|
|
|
|
|
|
struct RPMallocInit { RPMallocInit() { rpmalloc_initialize(); } };
|
|
|
|
struct RPMallocThreadInit { RPMallocThreadInit() { rpmalloc_thread_initialize(); } };
|
|
|
|
|
|
|
|
void InitRPMallocThread()
|
|
|
|
{
|
|
|
|
rpmalloc_initialize();
|
|
|
|
rpmalloc_thread_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ProfilerData
|
|
|
|
{
|
|
|
|
int64_t initTime = SetupHwTimer();
|
|
|
|
RPMallocInit rpmalloc_init;
|
|
|
|
moodycamel::ConcurrentQueue<QueueItem> queue;
|
|
|
|
Profiler profiler;
|
|
|
|
std::atomic<uint32_t> lockCounter = 0;
|
|
|
|
std::atomic<uint8_t> gpuCtxCounter = 0;
|
|
|
|
# ifdef TRACY_COLLECT_THREAD_NAMES
|
|
|
|
std::atomic<ThreadNameData*> threadNameData = nullptr;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-02-19 17:27:00 +00:00
|
|
|
struct ProducerWrapper
|
|
|
|
{
|
2019-02-19 19:43:14 +00:00
|
|
|
ProducerWrapper( ProfilerData& data ) : detail( data.queue ), ptr( data.queue.get_explicit_producer( detail ) ) {}
|
|
|
|
moodycamel::ProducerToken detail;
|
2019-02-19 17:27:00 +00:00
|
|
|
tracy::moodycamel::ConcurrentQueue<QueueItem>::ExplicitProducer* ptr;
|
|
|
|
};
|
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
struct ProfilerThreadData
|
|
|
|
{
|
|
|
|
ProfilerThreadData( ProfilerData& data ) : token( data ), gpuCtx( { nullptr } ) {}
|
|
|
|
RPMallocInit rpmalloc_init;
|
|
|
|
RPMallocThreadInit rpmalloc_thread_init;
|
|
|
|
ProducerWrapper token;
|
|
|
|
GpuCtxWrapper gpuCtx;
|
|
|
|
# ifdef TRACY_ON_DEMAND
|
|
|
|
LuaZoneState luaZoneState;
|
|
|
|
# endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static ProfilerData* profilerData;
|
2018-08-19 23:05:09 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
static ProfilerData& GetProfilerData()
|
|
|
|
{
|
|
|
|
// Cannot use magic statics here.
|
|
|
|
if( !profilerData )
|
|
|
|
{
|
|
|
|
profilerData = (ProfilerData*)malloc( sizeof( ProfilerData ) );
|
|
|
|
new (profilerData) ProfilerData();
|
|
|
|
}
|
|
|
|
return *profilerData;
|
|
|
|
}
|
2017-09-24 13:59:53 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
static ProfilerThreadData& GetProfilerThreadData()
|
|
|
|
{
|
|
|
|
thread_local ProfilerThreadData data( GetProfilerData() );
|
|
|
|
return data;
|
|
|
|
}
|
2017-09-18 17:08:54 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
moodycamel::ConcurrentQueue<QueueItem>::ExplicitProducer* GetToken() { return GetProfilerThreadData().token.ptr; }
|
|
|
|
Profiler& GetProfiler() { return GetProfilerData().profiler; }
|
|
|
|
moodycamel::ConcurrentQueue<QueueItem>& GetQueue() { return GetProfilerData().queue; }
|
|
|
|
int64_t GetInitTime() { return GetProfilerData().initTime; }
|
|
|
|
std::atomic<uint32_t>& GetLockCounter() { return GetProfilerData().lockCounter; }
|
|
|
|
std::atomic<uint8_t>& GetGpuCtxCounter() { return GetProfilerData().gpuCtxCounter; }
|
|
|
|
GpuCtxWrapper& GetGpuCtx() { return GetProfilerThreadData().gpuCtx; }
|
|
|
|
|
|
|
|
# ifdef TRACY_COLLECT_THREAD_NAMES
|
|
|
|
std::atomic<ThreadNameData*>& GetThreadNameData() { return GetProfilerData().threadNameData; }
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef TRACY_ON_DEMAND
|
|
|
|
LuaZoneState& GetLuaZoneState() { return GetProfilerThreadData().luaZoneState; }
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
void InitRPMallocThread()
|
|
|
|
{
|
|
|
|
rpmalloc_thread_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// MSVC static initialization order solution. gcc/clang uses init_order() to avoid all this.
|
2017-09-10 15:43:56 +00:00
|
|
|
|
2017-10-16 22:36:15 +00:00
|
|
|
// 1a. But s_queue is needed for initialization of variables in point 2.
|
|
|
|
extern moodycamel::ConcurrentQueue<QueueItem> s_queue;
|
|
|
|
|
2019-02-19 13:46:24 +00:00
|
|
|
thread_local RPMallocThreadInit init_order(106) s_rpmalloc_thread_init;
|
2017-10-16 22:36:15 +00:00
|
|
|
|
|
|
|
// 2. If these variables would be in the .CRT$XCB section, they would be initialized only in main thread.
|
2019-02-19 13:46:24 +00:00
|
|
|
thread_local moodycamel::ProducerToken init_order(107) s_token_detail( s_queue );
|
2017-10-24 20:02:49 +00:00
|
|
|
thread_local ProducerWrapper init_order(108) s_token { s_queue.get_explicit_producer( s_token_detail ) };
|
2017-10-16 22:36:15 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
# ifdef _MSC_VER
|
2017-10-16 22:36:15 +00:00
|
|
|
// 1. Initialize these static variables before all other variables.
|
2019-02-19 19:43:14 +00:00
|
|
|
# pragma warning( disable : 4075 )
|
|
|
|
# pragma init_seg( ".CRT$XCB" )
|
|
|
|
# endif
|
2017-10-16 22:36:15 +00:00
|
|
|
|
2018-04-27 14:58:45 +00:00
|
|
|
static InitTimeWrapper init_order(101) s_initTime { SetupHwTimer() };
|
2017-10-16 23:07:34 +00:00
|
|
|
static RPMallocInit init_order(102) s_rpmalloc_init;
|
|
|
|
moodycamel::ConcurrentQueue<QueueItem> init_order(103) s_queue( QueuePrealloc );
|
2017-10-24 20:02:49 +00:00
|
|
|
std::atomic<uint32_t> init_order(104) s_lockCounter( 0 );
|
2018-06-22 13:10:23 +00:00
|
|
|
std::atomic<uint8_t> init_order(104) s_gpuCtxCounter( 0 );
|
2017-10-30 19:45:00 +00:00
|
|
|
|
2017-11-14 22:29:48 +00:00
|
|
|
thread_local GpuCtxWrapper init_order(104) s_gpuCtx { nullptr };
|
2017-11-13 23:48:26 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
# ifdef TRACY_COLLECT_THREAD_NAMES
|
2017-10-30 19:45:00 +00:00
|
|
|
struct ThreadNameData;
|
2018-07-31 11:03:09 +00:00
|
|
|
static std::atomic<ThreadNameData*> init_order(104) s_threadNameDataInstance( nullptr );
|
2018-07-30 16:12:42 +00:00
|
|
|
std::atomic<ThreadNameData*>& s_threadNameData = s_threadNameDataInstance;
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif
|
2017-10-30 19:45:00 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
# ifdef TRACY_ON_DEMAND
|
2018-07-13 18:20:37 +00:00
|
|
|
thread_local LuaZoneState init_order(104) s_luaZoneState { 0, false };
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif
|
2018-07-12 10:53:35 +00:00
|
|
|
|
2019-02-19 17:38:08 +00:00
|
|
|
static Profiler init_order(105) s_profiler;
|
2018-07-30 16:12:42 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
moodycamel::ConcurrentQueue<QueueItem>::ExplicitProducer* GetToken() { return s_token.ptr; }
|
|
|
|
Profiler& GetProfiler() { return s_profiler; }
|
|
|
|
moodycamel::ConcurrentQueue<QueueItem>& GetQueue() { return s_queue; }
|
2019-02-19 19:43:14 +00:00
|
|
|
int64_t GetInitTime() { return s_initTime.val; }
|
2019-02-19 18:33:37 +00:00
|
|
|
std::atomic<uint32_t>& GetLockCounter() { return s_lockCounter; }
|
|
|
|
std::atomic<uint8_t>& GetGpuCtxCounter() { return s_gpuCtxCounter; }
|
|
|
|
GpuCtxWrapper& GetGpuCtx() { return s_gpuCtx; }
|
2019-02-19 17:27:00 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
# ifdef TRACY_COLLECT_THREAD_NAMES
|
2019-02-19 18:33:37 +00:00
|
|
|
std::atomic<ThreadNameData*>& GetThreadNameData() { return s_threadNameData; }
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif
|
2018-07-30 16:12:42 +00:00
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
# ifdef TRACY_ON_DEMAND
|
2019-02-19 18:33:37 +00:00
|
|
|
LuaZoneState& GetLuaZoneState() { return s_luaZoneState; }
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif
|
2019-02-19 18:33:37 +00:00
|
|
|
#endif
|
2019-02-19 17:45:41 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
// DLL exports to enable TracyClientDLL.cpp to retrieve the instances of Tracy objects and functions
|
2019-02-19 19:43:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# define DLL_EXPORT __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
# define DLL_EXPORT __attribute__((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
DLL_EXPORT void*(*get_rpmalloc())(size_t size) { return rpmalloc; }
|
|
|
|
DLL_EXPORT void(*get_rpfree())(void* ptr) { return rpfree; }
|
|
|
|
DLL_EXPORT moodycamel::ConcurrentQueue<QueueItem>::ExplicitProducer*(*get_token())() { return GetToken; }
|
|
|
|
DLL_EXPORT Profiler&(*get_profiler())() { return GetProfiler; }
|
|
|
|
DLL_EXPORT std::atomic<uint32_t>&(*get_getlockcounter())() { return GetLockCounter; }
|
|
|
|
DLL_EXPORT std::atomic<uint8_t>&(*get_getgpuctxcounter())() { return GetGpuCtxCounter; }
|
|
|
|
DLL_EXPORT GpuCtxWrapper&(*get_getgpuctx())() { return GetGpuCtx; }
|
2019-02-19 17:38:08 +00:00
|
|
|
|
2019-02-21 13:45:13 +00:00
|
|
|
#if defined TRACY_HW_TIMER && __ARM_ARCH >= 6 && !defined TARGET_OS_IOS
|
2019-02-19 18:33:37 +00:00
|
|
|
DLL_EXPORT int64_t(*get_GetTimeImpl())() { return GetTimeImpl; }
|
2018-07-30 16:12:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TRACY_COLLECT_THREAD_NAMES
|
2019-02-20 15:15:13 +00:00
|
|
|
DLL_EXPORT std::atomic<ThreadNameData*>&(*get_getthreadnamedata())() { return GetThreadNameData; }
|
2019-02-19 18:33:37 +00:00
|
|
|
DLL_EXPORT void(*get_rpmalloc_thread_initialize())() { return rpmalloc_thread_initialize; }
|
2019-02-20 15:15:13 +00:00
|
|
|
DLL_EXPORT void(*get_InitRPMallocThread())() { return InitRPMallocThread; }
|
2018-07-30 16:12:42 +00:00
|
|
|
#endif
|
2017-10-16 22:36:15 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
DLL_EXPORT LuaZoneState&(*get_getluazonestate())() { return GetLuaZoneState; }
|
|
|
|
#endif
|
2017-10-16 22:36:15 +00:00
|
|
|
|
2017-11-02 11:56:13 +00:00
|
|
|
enum { BulkSize = TargetFrameSize / QueueItemSize };
|
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
Profiler::Profiler()
|
2017-10-16 23:07:54 +00:00
|
|
|
: m_timeBegin( 0 )
|
|
|
|
, m_mainThread( GetThreadHandle() )
|
2017-10-03 22:34:05 +00:00
|
|
|
, m_epoch( std::chrono::duration_cast<std::chrono::seconds>( std::chrono::system_clock::now().time_since_epoch() ).count() )
|
2017-09-10 18:14:16 +00:00
|
|
|
, m_shutdown( false )
|
2018-08-20 19:49:23 +00:00
|
|
|
, m_shutdownManual( false )
|
2018-08-19 23:02:27 +00:00
|
|
|
, m_shutdownFinished( false )
|
2017-10-18 17:49:17 +00:00
|
|
|
, m_sock( nullptr )
|
2018-07-13 21:55:40 +00:00
|
|
|
, m_noExit( false )
|
2019-01-14 21:23:24 +00:00
|
|
|
, m_zoneId( 1 )
|
2017-09-17 11:10:42 +00:00
|
|
|
, m_stream( LZ4_createStream() )
|
2017-10-14 15:15:18 +00:00
|
|
|
, m_buffer( (char*)tracy_malloc( TargetFrameSize*3 ) )
|
2017-09-17 11:10:42 +00:00
|
|
|
, m_bufferOffset( 0 )
|
2017-11-11 13:16:37 +00:00
|
|
|
, m_bufferStart( 0 )
|
2017-11-02 11:56:13 +00:00
|
|
|
, m_itemBuf( (QueueItem*)tracy_malloc( sizeof( QueueItem ) * BulkSize ) )
|
2017-11-02 16:37:10 +00:00
|
|
|
, m_lz4Buf( (char*)tracy_malloc( LZ4Size + sizeof( lz4sz_t ) ) )
|
2018-04-01 17:53:05 +00:00
|
|
|
, m_serialQueue( 1024*1024 )
|
2018-04-14 13:46:11 +00:00
|
|
|
, m_serialDequeue( 1024*1024 )
|
2018-07-10 19:50:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
, m_isConnected( false )
|
2018-07-10 20:26:40 +00:00
|
|
|
, m_frameCount( 0 )
|
2018-07-11 10:14:28 +00:00
|
|
|
, m_deferredQueue( 64*1024 )
|
2018-07-10 19:50:00 +00:00
|
|
|
#endif
|
2017-09-10 15:43:56 +00:00
|
|
|
{
|
|
|
|
assert( !s_instance );
|
|
|
|
s_instance = this;
|
|
|
|
|
2019-02-19 19:43:14 +00:00
|
|
|
#ifndef TRACY_DELAYED_INIT
|
|
|
|
# ifdef _MSC_VER
|
2017-10-16 22:36:15 +00:00
|
|
|
// 3. But these variables need to be initialized in main thread within the .CRT$XCB section. Do it here.
|
|
|
|
s_token_detail = moodycamel::ProducerToken( s_queue );
|
|
|
|
s_token = ProducerWrapper { s_queue.get_explicit_producer( s_token_detail ) };
|
2019-02-19 19:43:14 +00:00
|
|
|
# endif
|
2017-10-16 22:36:15 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-23 19:33:05 +00:00
|
|
|
CalibrateTimer();
|
2017-09-24 14:02:09 +00:00
|
|
|
CalibrateDelay();
|
2017-09-23 19:33:05 +00:00
|
|
|
|
2018-07-13 21:55:40 +00:00
|
|
|
#ifndef TRACY_NO_EXIT
|
|
|
|
const char* noExitEnv = getenv( "TRACY_NO_EXIT" );
|
|
|
|
if( noExitEnv && noExitEnv[0] == '1' )
|
|
|
|
{
|
|
|
|
m_noExit = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-16 19:13:57 +00:00
|
|
|
s_thread = (Thread*)tracy_malloc( sizeof( Thread ) );
|
|
|
|
new(s_thread) Thread( LaunchWorker, this );
|
|
|
|
SetThreadName( s_thread->Handle(), "Tracy Profiler" );
|
2017-10-16 23:07:54 +00:00
|
|
|
|
2019-01-19 11:03:30 +00:00
|
|
|
#if defined PTW32_VERSION
|
|
|
|
s_profilerThreadId = pthread_getw32threadid_np( s_thread->Handle() );
|
|
|
|
#elif defined __WINPTHREADS_VERSION
|
|
|
|
s_profilerThreadId = GetThreadId( (HANDLE)pthread_gethandle( s_thread->Handle() ) );
|
|
|
|
#elif defined _MSC_VER
|
2018-08-19 23:05:09 +00:00
|
|
|
s_profilerThreadId = GetThreadId( s_thread->Handle() );
|
2019-01-19 11:03:30 +00:00
|
|
|
#endif
|
|
|
|
#if defined _WIN32
|
2018-08-19 23:05:09 +00:00
|
|
|
AddVectoredExceptionHandler( 1, CrashFilter );
|
|
|
|
#endif
|
|
|
|
|
2018-08-20 12:30:56 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
struct sigaction threadFreezer = {};
|
|
|
|
threadFreezer.sa_handler = ThreadFreezer;
|
|
|
|
sigaction( SIGPWR, &threadFreezer, nullptr );
|
|
|
|
|
|
|
|
struct sigaction crashHandler = {};
|
|
|
|
crashHandler.sa_sigaction = CrashHandler;
|
|
|
|
crashHandler.sa_flags = SA_SIGINFO;
|
|
|
|
sigaction( SIGILL, &crashHandler, nullptr );
|
|
|
|
sigaction( SIGFPE, &crashHandler, nullptr );
|
|
|
|
sigaction( SIGSEGV, &crashHandler, nullptr );
|
|
|
|
sigaction( SIGPIPE, &crashHandler, nullptr );
|
|
|
|
sigaction( SIGBUS, &crashHandler, nullptr );
|
|
|
|
#endif
|
|
|
|
|
2018-06-18 23:17:19 +00:00
|
|
|
#ifdef TRACY_HAS_CALLSTACK
|
|
|
|
InitCallstack();
|
|
|
|
#endif
|
|
|
|
|
2017-10-29 15:12:16 +00:00
|
|
|
m_timeBegin.store( GetTime(), std::memory_order_relaxed );
|
2017-09-10 15:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Profiler::~Profiler()
|
|
|
|
{
|
2017-09-13 21:36:40 +00:00
|
|
|
m_shutdown.store( true, std::memory_order_relaxed );
|
2017-10-16 19:13:57 +00:00
|
|
|
s_thread->~Thread();
|
|
|
|
tracy_free( s_thread );
|
2017-09-13 21:36:40 +00:00
|
|
|
|
2017-11-02 16:37:10 +00:00
|
|
|
tracy_free( m_lz4Buf );
|
2017-11-02 11:56:13 +00:00
|
|
|
tracy_free( m_itemBuf );
|
2017-10-14 15:15:18 +00:00
|
|
|
tracy_free( m_buffer );
|
2017-09-17 11:10:42 +00:00
|
|
|
LZ4_freeStream( m_stream );
|
|
|
|
|
2017-10-18 17:49:17 +00:00
|
|
|
if( m_sock )
|
|
|
|
{
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
|
|
|
}
|
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
assert( s_instance );
|
|
|
|
s_instance = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:23:50 +00:00
|
|
|
bool Profiler::ShouldExit()
|
|
|
|
{
|
|
|
|
return s_instance->m_shutdown.load( std::memory_order_relaxed );
|
|
|
|
}
|
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
void Profiler::Worker()
|
|
|
|
{
|
2018-08-20 12:30:56 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
s_profilerTid = syscall( SYS_gettid );
|
|
|
|
#endif
|
|
|
|
|
2017-10-18 21:30:54 +00:00
|
|
|
rpmalloc_thread_initialize();
|
|
|
|
|
2017-10-17 19:53:09 +00:00
|
|
|
const auto procname = GetProcessName();
|
|
|
|
const auto pnsz = std::min<size_t>( strlen( procname ), WelcomeMessageProgramNameSize - 1 );
|
|
|
|
|
2018-08-19 16:15:14 +00:00
|
|
|
const auto hostinfo = GetHostInfo();
|
2018-08-19 16:19:12 +00:00
|
|
|
const auto hisz = std::min<size_t>( strlen( hostinfo ), WelcomeMessageHostInfoSize - 1 );
|
2018-08-19 16:15:14 +00:00
|
|
|
|
2017-10-17 19:55:40 +00:00
|
|
|
while( m_timeBegin.load( std::memory_order_relaxed ) == 0 ) std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
|
2018-07-10 19:44:23 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
uint8_t onDemand = 1;
|
|
|
|
#else
|
|
|
|
uint8_t onDemand = 0;
|
|
|
|
#endif
|
|
|
|
|
2017-10-17 19:55:40 +00:00
|
|
|
WelcomeMessage welcome;
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &welcome.timerMul, m_timerMul );
|
2019-02-19 19:43:14 +00:00
|
|
|
MemWrite( &welcome.initBegin, GetInitTime() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &welcome.initEnd, m_timeBegin.load( std::memory_order_relaxed ) );
|
|
|
|
MemWrite( &welcome.delay, m_delay );
|
|
|
|
MemWrite( &welcome.resolution, m_resolution );
|
|
|
|
MemWrite( &welcome.epoch, m_epoch );
|
2018-07-10 19:44:23 +00:00
|
|
|
MemWrite( &welcome.onDemand, onDemand );
|
2017-10-17 19:55:40 +00:00
|
|
|
memcpy( welcome.programName, procname, pnsz );
|
|
|
|
memset( welcome.programName + pnsz, 0, WelcomeMessageProgramNameSize - pnsz );
|
2018-08-19 16:19:12 +00:00
|
|
|
memcpy( welcome.hostInfo, hostinfo, hisz );
|
|
|
|
memset( welcome.hostInfo + hisz, 0, WelcomeMessageHostInfoSize - hisz );
|
2017-10-17 19:55:40 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
moodycamel::ConsumerToken token( GetQueue() );
|
2017-09-10 18:23:06 +00:00
|
|
|
|
2017-09-11 20:51:47 +00:00
|
|
|
ListenSocket listen;
|
2018-12-21 17:14:30 +00:00
|
|
|
if( !listen.Listen( "8086", 8 ) )
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if( ShouldExit() )
|
|
|
|
{
|
|
|
|
m_shutdownFinished.store( true, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearQueues( token );
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 20:51:47 +00:00
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
2017-09-11 20:51:47 +00:00
|
|
|
for(;;)
|
2017-09-10 18:23:06 +00:00
|
|
|
{
|
2017-10-18 18:01:12 +00:00
|
|
|
#ifndef TRACY_NO_EXIT
|
2018-08-19 23:02:27 +00:00
|
|
|
if( !m_noExit && ShouldExit() )
|
|
|
|
{
|
|
|
|
m_shutdownFinished.store( true, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
2017-10-18 18:01:12 +00:00
|
|
|
#endif
|
2017-09-14 17:07:56 +00:00
|
|
|
m_sock = listen.Accept();
|
|
|
|
if( m_sock ) break;
|
2017-09-10 18:23:06 +00:00
|
|
|
}
|
2017-09-11 20:51:47 +00:00
|
|
|
|
2018-09-09 16:26:53 +00:00
|
|
|
{
|
|
|
|
char shibboleth[HandshakeShibbolethSize];
|
2019-02-10 14:45:23 +00:00
|
|
|
auto res = m_sock->ReadRaw( shibboleth, HandshakeShibbolethSize, 2000 );
|
2018-09-09 16:26:53 +00:00
|
|
|
if( !res || memcmp( shibboleth, HandshakeShibboleth, HandshakeShibbolethSize ) != 0 )
|
|
|
|
{
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-09 17:28:53 +00:00
|
|
|
|
|
|
|
uint32_t protocolVersion;
|
2019-02-10 14:45:23 +00:00
|
|
|
res = m_sock->ReadRaw( &protocolVersion, sizeof( protocolVersion ), 2000 );
|
2018-09-09 17:28:53 +00:00
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( protocolVersion != ProtocolVersion )
|
|
|
|
{
|
|
|
|
HandshakeStatus status = HandshakeProtocolMismatch;
|
|
|
|
m_sock->Send( &status, sizeof( status ) );
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-09 16:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 19:50:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
2018-07-10 20:01:13 +00:00
|
|
|
ClearQueues( token );
|
2018-07-10 19:50:00 +00:00
|
|
|
m_isConnected.store( true, std::memory_order_relaxed );
|
|
|
|
#endif
|
|
|
|
|
2018-09-09 17:28:53 +00:00
|
|
|
HandshakeStatus handshake = HandshakeWelcome;
|
|
|
|
m_sock->Send( &handshake, sizeof( handshake ) );
|
|
|
|
|
2017-09-17 11:10:42 +00:00
|
|
|
LZ4_resetStream( m_stream );
|
2018-07-10 20:37:39 +00:00
|
|
|
m_sock->Send( &welcome, sizeof( welcome ) );
|
|
|
|
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
OnDemandPayloadMessage onDemand;
|
|
|
|
onDemand.frames = m_frameCount.load( std::memory_order_relaxed );
|
|
|
|
|
|
|
|
m_sock->Send( &onDemand, sizeof( onDemand ) );
|
2018-07-11 10:28:40 +00:00
|
|
|
|
|
|
|
m_deferredLock.lock();
|
|
|
|
for( auto& item : m_deferredQueue )
|
|
|
|
{
|
|
|
|
const auto idx = MemRead<uint8_t>( &item.hdr.idx );
|
|
|
|
AppendData( &item, QueueDataSize[idx] );
|
|
|
|
}
|
|
|
|
m_deferredLock.unlock();
|
2018-07-10 20:37:39 +00:00
|
|
|
#endif
|
2017-09-17 11:10:42 +00:00
|
|
|
|
2018-07-10 19:33:22 +00:00
|
|
|
int keepAlive = 0;
|
2017-09-11 20:51:47 +00:00
|
|
|
for(;;)
|
2017-09-10 18:23:06 +00:00
|
|
|
{
|
2019-02-21 20:59:02 +00:00
|
|
|
ProcessSysTime();
|
2017-10-18 16:48:51 +00:00
|
|
|
const auto status = Dequeue( token );
|
2018-04-01 18:04:35 +00:00
|
|
|
const auto serialStatus = DequeueSerial();
|
|
|
|
if( status == ConnectionLost || serialStatus == ConnectionLost )
|
2017-09-11 20:51:47 +00:00
|
|
|
{
|
2017-10-18 16:48:51 +00:00
|
|
|
break;
|
2017-09-11 20:51:47 +00:00
|
|
|
}
|
2018-04-01 18:04:35 +00:00
|
|
|
else if( status == QueueEmpty && serialStatus == QueueEmpty )
|
2017-09-11 20:51:47 +00:00
|
|
|
{
|
2017-10-18 16:48:51 +00:00
|
|
|
if( ShouldExit() ) break;
|
2018-07-10 19:33:22 +00:00
|
|
|
if( m_bufferOffset != m_bufferStart )
|
|
|
|
{
|
|
|
|
if( !CommitData() ) break;
|
|
|
|
}
|
|
|
|
if( keepAlive == 500 )
|
|
|
|
{
|
|
|
|
QueueItem ka;
|
|
|
|
ka.hdr.type = QueueType::KeepAlive;
|
|
|
|
AppendData( &ka, QueueDataSize[ka.hdr.idx] );
|
|
|
|
if( !CommitData() ) break;
|
|
|
|
|
|
|
|
keepAlive = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keepAlive++;
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keepAlive = 0;
|
2017-09-11 20:51:47 +00:00
|
|
|
}
|
2017-09-14 17:25:16 +00:00
|
|
|
|
|
|
|
while( m_sock->HasData() )
|
|
|
|
{
|
2017-09-21 23:54:04 +00:00
|
|
|
if( !HandleServerQuery() ) break;
|
2017-09-14 17:25:16 +00:00
|
|
|
}
|
2017-09-10 18:23:06 +00:00
|
|
|
}
|
2017-10-18 16:48:51 +00:00
|
|
|
if( ShouldExit() ) break;
|
2018-07-10 19:50:00 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
m_isConnected.store( false, std::memory_order_relaxed );
|
|
|
|
#endif
|
2018-09-09 16:31:06 +00:00
|
|
|
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
2018-09-09 17:42:06 +00:00
|
|
|
|
2018-09-09 17:44:41 +00:00
|
|
|
#ifndef TRACY_ON_DEMAND
|
2018-09-09 17:42:06 +00:00
|
|
|
// Client is no longer available here
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if( ShouldExit() )
|
|
|
|
{
|
|
|
|
m_shutdownFinished.store( true, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-21 17:12:26 +00:00
|
|
|
ClearQueues( token );
|
2018-09-09 17:42:06 +00:00
|
|
|
|
|
|
|
m_sock = listen.Accept();
|
|
|
|
if( m_sock )
|
|
|
|
{
|
|
|
|
char shibboleth[HandshakeShibbolethSize];
|
2019-02-10 14:45:23 +00:00
|
|
|
auto res = m_sock->ReadRaw( shibboleth, HandshakeShibbolethSize, 1000 );
|
2018-09-09 17:42:06 +00:00
|
|
|
if( !res || memcmp( shibboleth, HandshakeShibboleth, HandshakeShibbolethSize ) != 0 )
|
|
|
|
{
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t protocolVersion;
|
2019-02-10 14:45:23 +00:00
|
|
|
res = m_sock->ReadRaw( &protocolVersion, sizeof( protocolVersion ), 1000 );
|
2018-09-09 17:42:06 +00:00
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
HandshakeStatus status = HandshakeNotAvailable;
|
|
|
|
m_sock->Send( &status, sizeof( status ) );
|
|
|
|
m_sock->~Socket();
|
|
|
|
tracy_free( m_sock );
|
|
|
|
}
|
|
|
|
}
|
2018-09-09 17:44:41 +00:00
|
|
|
#endif
|
2017-10-18 16:48:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 12:43:54 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
const auto status = Dequeue( token );
|
2018-04-01 18:04:35 +00:00
|
|
|
const auto serialStatus = DequeueSerial();
|
|
|
|
if( status == ConnectionLost || serialStatus == ConnectionLost )
|
2018-01-11 12:43:54 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-04-01 18:04:35 +00:00
|
|
|
else if( status == QueueEmpty && serialStatus == QueueEmpty )
|
2018-01-11 12:43:54 +00:00
|
|
|
{
|
|
|
|
if( m_bufferOffset != m_bufferStart ) CommitData();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( m_sock->HasData() )
|
|
|
|
{
|
|
|
|
if( !HandleServerQuery() ) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:48:51 +00:00
|
|
|
QueueItem terminate;
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &terminate.hdr.type, QueueType::Terminate );
|
2018-08-19 23:02:27 +00:00
|
|
|
if( !SendData( (const char*)&terminate, 1 ) )
|
|
|
|
{
|
|
|
|
m_shutdownFinished.store( true, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
2017-10-18 16:48:51 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if( m_sock->HasData() )
|
|
|
|
{
|
2017-11-11 13:16:37 +00:00
|
|
|
while( m_sock->HasData() )
|
|
|
|
{
|
|
|
|
if( !HandleServerQuery() )
|
|
|
|
{
|
|
|
|
if( m_bufferOffset != m_bufferStart ) CommitData();
|
2018-08-19 23:02:27 +00:00
|
|
|
m_shutdownFinished.store( true, std::memory_order_relaxed );
|
2017-11-11 13:16:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 16:48:51 +00:00
|
|
|
while( Dequeue( token ) == Success ) {}
|
2018-04-01 18:04:35 +00:00
|
|
|
while( DequeueSerial() == Success ) {}
|
2017-11-11 13:16:37 +00:00
|
|
|
if( m_bufferOffset != m_bufferStart )
|
|
|
|
{
|
2018-08-19 23:02:27 +00:00
|
|
|
if( !CommitData() )
|
|
|
|
{
|
|
|
|
m_shutdownFinished.store( true, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
2017-11-11 13:16:37 +00:00
|
|
|
}
|
2017-10-18 16:48:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-11 13:23:55 +00:00
|
|
|
if( m_bufferOffset != m_bufferStart ) CommitData();
|
2017-10-18 16:48:51 +00:00
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 23:34:48 +00:00
|
|
|
static void FreeAssociatedMemory( const QueueItem& item )
|
2018-07-10 20:01:13 +00:00
|
|
|
{
|
2018-07-10 23:34:48 +00:00
|
|
|
if( item.hdr.idx >= (int)QueueType::Terminate ) return;
|
|
|
|
|
|
|
|
uint64_t ptr;
|
|
|
|
switch( item.hdr.type )
|
|
|
|
{
|
|
|
|
case QueueType::ZoneText:
|
|
|
|
case QueueType::ZoneName:
|
|
|
|
ptr = MemRead<uint64_t>( &item.zoneText.text );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
case QueueType::Message:
|
|
|
|
ptr = MemRead<uint64_t>( &item.message.text );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
case QueueType::ZoneBeginAllocSrcLoc:
|
|
|
|
ptr = MemRead<uint64_t>( &item.zoneBegin.srcloc );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
case QueueType::CallstackMemory:
|
|
|
|
ptr = MemRead<uint64_t>( &item.callstackMemory.ptr );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
case QueueType::Callstack:
|
|
|
|
ptr = MemRead<uint64_t>( &item.callstack.ptr );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 20:01:13 +00:00
|
|
|
|
2018-07-10 23:34:48 +00:00
|
|
|
void Profiler::ClearQueues( moodycamel::ConsumerToken& token )
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
2019-02-19 18:33:37 +00:00
|
|
|
const auto sz = GetQueue().try_dequeue_bulk( token, m_itemBuf, BulkSize );
|
2018-07-10 23:34:48 +00:00
|
|
|
if( sz == 0 ) break;
|
|
|
|
for( size_t i=0; i<sz; i++ ) FreeAssociatedMemory( m_itemBuf[i] );
|
|
|
|
}
|
2018-07-10 20:01:13 +00:00
|
|
|
|
2018-12-21 17:10:29 +00:00
|
|
|
{
|
|
|
|
bool lockHeld = true;
|
|
|
|
while( !m_serialLock.try_lock() )
|
|
|
|
{
|
|
|
|
if( m_shutdownManual.load( std::memory_order_relaxed ) )
|
|
|
|
{
|
|
|
|
lockHeld = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for( auto& v : m_serialQueue ) FreeAssociatedMemory( v );
|
|
|
|
m_serialQueue.clear();
|
|
|
|
if( lockHeld )
|
|
|
|
{
|
|
|
|
m_serialLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
2018-07-10 23:34:48 +00:00
|
|
|
|
|
|
|
for( auto& v : m_serialDequeue ) FreeAssociatedMemory( v );
|
2018-07-10 20:01:13 +00:00
|
|
|
m_serialDequeue.clear();
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:48:51 +00:00
|
|
|
Profiler::DequeueStatus Profiler::Dequeue( moodycamel::ConsumerToken& token )
|
|
|
|
{
|
2019-02-19 18:33:37 +00:00
|
|
|
const auto sz = GetQueue().try_dequeue_bulk( token, m_itemBuf, BulkSize );
|
2017-10-18 16:48:51 +00:00
|
|
|
if( sz > 0 )
|
|
|
|
{
|
2017-11-22 01:07:23 +00:00
|
|
|
auto end = m_itemBuf + sz;
|
|
|
|
auto item = m_itemBuf;
|
|
|
|
while( item != end )
|
2017-10-18 16:48:51 +00:00
|
|
|
{
|
2017-11-11 14:41:21 +00:00
|
|
|
uint64_t ptr;
|
2018-04-03 14:45:55 +00:00
|
|
|
const auto idx = MemRead<uint8_t>( &item->hdr.idx );
|
2018-03-31 12:03:55 +00:00
|
|
|
if( idx < (int)QueueType::Terminate )
|
2017-11-11 14:22:55 +00:00
|
|
|
{
|
2018-03-31 12:03:55 +00:00
|
|
|
switch( (QueueType)idx )
|
2017-11-22 01:28:12 +00:00
|
|
|
{
|
|
|
|
case QueueType::ZoneText:
|
2018-06-29 14:01:31 +00:00
|
|
|
case QueueType::ZoneName:
|
2018-04-03 14:45:55 +00:00
|
|
|
ptr = MemRead<uint64_t>( &item->zoneText.text );
|
2017-11-22 01:28:12 +00:00
|
|
|
SendString( ptr, (const char*)ptr, QueueType::CustomStringData );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
case QueueType::Message:
|
2018-04-03 14:45:55 +00:00
|
|
|
ptr = MemRead<uint64_t>( &item->message.text );
|
2017-11-22 01:28:12 +00:00
|
|
|
SendString( ptr, (const char*)ptr, QueueType::CustomStringData );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
case QueueType::ZoneBeginAllocSrcLoc:
|
2018-04-03 14:45:55 +00:00
|
|
|
ptr = MemRead<uint64_t>( &item->zoneBegin.srcloc );
|
2017-11-22 01:28:12 +00:00
|
|
|
SendSourceLocationPayload( ptr );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
2018-06-21 22:56:01 +00:00
|
|
|
case QueueType::Callstack:
|
|
|
|
ptr = MemRead<uint64_t>( &item->callstack.ptr );
|
|
|
|
SendCallstackPayload( ptr );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
2017-11-22 01:28:12 +00:00
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
2017-11-11 14:22:55 +00:00
|
|
|
}
|
2018-03-31 12:03:55 +00:00
|
|
|
if( !AppendData( item, QueueDataSize[idx] ) ) return ConnectionLost;
|
2017-11-22 01:07:23 +00:00
|
|
|
item++;
|
2017-10-18 16:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QueueEmpty;
|
2017-09-10 15:43:56 +00:00
|
|
|
}
|
2017-10-18 16:48:51 +00:00
|
|
|
return Success;
|
2017-09-10 15:43:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 18:04:35 +00:00
|
|
|
Profiler::DequeueStatus Profiler::DequeueSerial()
|
|
|
|
{
|
2018-04-14 13:46:11 +00:00
|
|
|
{
|
2018-08-20 19:49:23 +00:00
|
|
|
bool lockHeld = true;
|
|
|
|
while( !m_serialLock.try_lock() )
|
|
|
|
{
|
|
|
|
if( m_shutdownManual.load( std::memory_order_relaxed ) )
|
|
|
|
{
|
|
|
|
lockHeld = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-14 13:46:11 +00:00
|
|
|
m_serialQueue.swap( m_serialDequeue );
|
2018-08-20 19:49:23 +00:00
|
|
|
if( lockHeld )
|
|
|
|
{
|
|
|
|
m_serialLock.unlock();
|
|
|
|
}
|
2018-04-14 13:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto sz = m_serialDequeue.size();
|
2018-04-01 18:04:35 +00:00
|
|
|
if( sz > 0 )
|
|
|
|
{
|
2018-04-14 13:46:11 +00:00
|
|
|
auto item = m_serialDequeue.data();
|
2018-04-01 18:04:35 +00:00
|
|
|
auto end = item + sz;
|
|
|
|
while( item != end )
|
|
|
|
{
|
2018-06-19 17:09:57 +00:00
|
|
|
uint64_t ptr;
|
2018-04-03 15:57:12 +00:00
|
|
|
const auto idx = MemRead<uint8_t>( &item->hdr.idx );
|
2018-06-19 17:09:57 +00:00
|
|
|
if( idx < (int)QueueType::Terminate )
|
|
|
|
{
|
|
|
|
switch( (QueueType)idx )
|
|
|
|
{
|
|
|
|
case QueueType::CallstackMemory:
|
|
|
|
ptr = MemRead<uint64_t>( &item->callstackMemory.ptr );
|
|
|
|
SendCallstackPayload( ptr );
|
|
|
|
tracy_free( (void*)ptr );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 18:04:35 +00:00
|
|
|
if( !AppendData( item, QueueDataSize[idx] ) ) return ConnectionLost;
|
|
|
|
item++;
|
|
|
|
}
|
2018-04-14 13:46:11 +00:00
|
|
|
m_serialDequeue.clear();
|
2018-04-01 18:04:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QueueEmpty;
|
|
|
|
}
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2017-11-11 13:16:37 +00:00
|
|
|
bool Profiler::AppendData( const void* data, size_t len )
|
|
|
|
{
|
2019-01-29 21:21:01 +00:00
|
|
|
const auto ret = NeedDataSize( len );
|
2018-06-23 00:16:58 +00:00
|
|
|
AppendDataUnsafe( data, len );
|
2017-11-11 13:16:37 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Profiler::CommitData()
|
|
|
|
{
|
|
|
|
bool ret = SendData( m_buffer + m_bufferStart, m_bufferOffset - m_bufferStart );
|
|
|
|
if( m_bufferOffset > TargetFrameSize * 2 ) m_bufferOffset = 0;
|
|
|
|
m_bufferStart = m_bufferOffset;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-11 14:08:03 +00:00
|
|
|
bool Profiler::NeedDataSize( size_t len )
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
if( m_bufferOffset - m_bufferStart + len > TargetFrameSize )
|
|
|
|
{
|
|
|
|
ret = CommitData();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:07:56 +00:00
|
|
|
bool Profiler::SendData( const char* data, size_t len )
|
|
|
|
{
|
2017-11-02 16:37:10 +00:00
|
|
|
const lz4sz_t lz4sz = LZ4_compress_fast_continue( m_stream, data, m_lz4Buf + sizeof( lz4sz_t ), (int)len, LZ4Size, 1 );
|
|
|
|
memcpy( m_lz4Buf, &lz4sz, sizeof( lz4sz ) );
|
2017-11-10 16:34:11 +00:00
|
|
|
return m_sock->Send( m_lz4Buf, lz4sz + sizeof( lz4sz_t ) ) != -1;
|
2017-09-14 17:07:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:00:57 +00:00
|
|
|
void Profiler::SendString( uint64_t str, const char* ptr, QueueType type )
|
2017-09-14 17:24:35 +00:00
|
|
|
{
|
2018-08-04 18:48:21 +00:00
|
|
|
assert( type == QueueType::StringData || type == QueueType::ThreadName || type == QueueType::CustomStringData || type == QueueType::PlotName || type == QueueType::FrameName );
|
2017-09-14 17:24:35 +00:00
|
|
|
|
2017-10-03 14:41:32 +00:00
|
|
|
QueueItem item;
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item.hdr.type, type );
|
|
|
|
MemWrite( &item.stringTransfer.ptr, str );
|
2017-09-14 17:24:35 +00:00
|
|
|
|
|
|
|
auto len = strlen( ptr );
|
|
|
|
assert( len <= std::numeric_limits<uint16_t>::max() );
|
2017-10-16 18:42:53 +00:00
|
|
|
auto l16 = uint16_t( len );
|
2017-11-11 14:08:03 +00:00
|
|
|
|
2018-03-31 12:03:55 +00:00
|
|
|
NeedDataSize( QueueDataSize[(int)type] + sizeof( l16 ) + l16 );
|
2017-11-11 14:08:03 +00:00
|
|
|
|
2018-06-23 00:19:23 +00:00
|
|
|
AppendDataUnsafe( &item, QueueDataSize[(int)type] );
|
|
|
|
AppendDataUnsafe( &l16, sizeof( l16 ) );
|
|
|
|
AppendDataUnsafe( ptr, l16 );
|
2017-09-14 17:24:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 17:00:25 +00:00
|
|
|
void Profiler::SendSourceLocation( uint64_t ptr )
|
2017-09-26 00:28:14 +00:00
|
|
|
{
|
2018-07-27 22:34:04 +00:00
|
|
|
auto srcloc = (const SourceLocationData*)ptr;
|
2017-09-26 00:28:14 +00:00
|
|
|
QueueItem item;
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item.hdr.type, QueueType::SourceLocation );
|
|
|
|
MemWrite( &item.srcloc.name, (uint64_t)srcloc->name );
|
|
|
|
MemWrite( &item.srcloc.file, (uint64_t)srcloc->file );
|
|
|
|
MemWrite( &item.srcloc.function, (uint64_t)srcloc->function );
|
|
|
|
MemWrite( &item.srcloc.line, srcloc->line );
|
|
|
|
MemWrite( &item.srcloc.r, uint8_t( ( srcloc->color ) & 0xFF ) );
|
|
|
|
MemWrite( &item.srcloc.g, uint8_t( ( srcloc->color >> 8 ) & 0xFF ) );
|
|
|
|
MemWrite( &item.srcloc.b, uint8_t( ( srcloc->color >> 16 ) & 0xFF ) );
|
|
|
|
AppendData( &item, QueueDataSize[(int)QueueType::SourceLocation] );
|
2017-09-26 00:28:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:00:57 +00:00
|
|
|
void Profiler::SendSourceLocationPayload( uint64_t _ptr )
|
2017-11-05 15:46:00 +00:00
|
|
|
{
|
|
|
|
auto ptr = (const char*)_ptr;
|
|
|
|
|
|
|
|
QueueItem item;
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item.hdr.type, QueueType::SourceLocationPayload );
|
|
|
|
MemWrite( &item.stringTransfer.ptr, _ptr );
|
2017-11-05 15:46:00 +00:00
|
|
|
|
|
|
|
const auto len = *((uint32_t*)ptr);
|
|
|
|
assert( len <= std::numeric_limits<uint16_t>::max() );
|
|
|
|
assert( len > 4 );
|
2017-11-11 13:16:37 +00:00
|
|
|
const auto l16 = uint16_t( len - 4 );
|
2017-11-11 14:08:03 +00:00
|
|
|
|
2018-03-31 12:03:55 +00:00
|
|
|
NeedDataSize( QueueDataSize[(int)QueueType::SourceLocationPayload] + sizeof( l16 ) + l16 );
|
2017-11-11 14:08:03 +00:00
|
|
|
|
2018-06-23 00:19:23 +00:00
|
|
|
AppendDataUnsafe( &item, QueueDataSize[(int)QueueType::SourceLocationPayload] );
|
|
|
|
AppendDataUnsafe( &l16, sizeof( l16 ) );
|
|
|
|
AppendDataUnsafe( ptr + 4, l16 );
|
2017-11-05 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:09:43 +00:00
|
|
|
void Profiler::SendCallstackPayload( uint64_t _ptr )
|
|
|
|
{
|
|
|
|
auto ptr = (uintptr_t*)_ptr;
|
|
|
|
|
|
|
|
QueueItem item;
|
|
|
|
MemWrite( &item.hdr.type, QueueType::CallstackPayload );
|
|
|
|
MemWrite( &item.stringTransfer.ptr, _ptr );
|
|
|
|
|
|
|
|
const auto sz = *ptr++;
|
|
|
|
const auto len = sz * sizeof( uint64_t );
|
|
|
|
const auto l16 = uint16_t( len );
|
|
|
|
|
|
|
|
NeedDataSize( QueueDataSize[(int)QueueType::CallstackPayload] + sizeof( l16 ) + l16 );
|
|
|
|
|
2018-06-23 00:19:23 +00:00
|
|
|
AppendDataUnsafe( &item, QueueDataSize[(int)QueueType::CallstackPayload] );
|
|
|
|
AppendDataUnsafe( &l16, sizeof( l16 ) );
|
2018-06-19 17:09:43 +00:00
|
|
|
|
2018-08-01 12:07:30 +00:00
|
|
|
if( compile_time_condition<sizeof( uintptr_t ) == sizeof( uint64_t )>::value )
|
2018-06-19 17:09:43 +00:00
|
|
|
{
|
2018-06-23 00:19:23 +00:00
|
|
|
AppendDataUnsafe( ptr, sizeof( uint64_t ) * sz );
|
2018-06-23 00:13:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( uintptr_t i=0; i<sz; i++ )
|
|
|
|
{
|
|
|
|
const auto val = uint64_t( *ptr++ );
|
2018-06-23 00:19:23 +00:00
|
|
|
AppendDataUnsafe( &val, sizeof( uint64_t ) );
|
2018-06-23 00:13:52 +00:00
|
|
|
}
|
2018-06-19 17:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 23:06:31 +00:00
|
|
|
void Profiler::SendCallstackFrame( uint64_t ptr )
|
|
|
|
{
|
|
|
|
#ifdef TRACY_HAS_CALLSTACK
|
2019-01-20 18:11:48 +00:00
|
|
|
const auto frameData = DecodeCallstackPtr( ptr );
|
2018-06-19 23:06:31 +00:00
|
|
|
|
2019-01-20 18:11:48 +00:00
|
|
|
{
|
|
|
|
QueueItem item;
|
|
|
|
MemWrite( &item.hdr.type, QueueType::CallstackFrameSize );
|
|
|
|
MemWrite( &item.callstackFrameSize.ptr, ptr );
|
|
|
|
MemWrite( &item.callstackFrameSize.size, frameData.size );
|
2018-06-19 23:06:31 +00:00
|
|
|
|
2019-01-20 18:11:48 +00:00
|
|
|
AppendData( &item, QueueDataSize[(int)QueueType::CallstackFrameSize] );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( uint8_t i=0; i<frameData.size; i++ )
|
|
|
|
{
|
|
|
|
const auto& frame = frameData.data[i];
|
|
|
|
|
|
|
|
SendString( uint64_t( frame.name ), frame.name, QueueType::CustomStringData );
|
|
|
|
SendString( uint64_t( frame.file ), frame.file, QueueType::CustomStringData );
|
|
|
|
|
|
|
|
QueueItem item;
|
|
|
|
MemWrite( &item.hdr.type, QueueType::CallstackFrame );
|
|
|
|
MemWrite( &item.callstackFrame.name, (uint64_t)frame.name );
|
|
|
|
MemWrite( &item.callstackFrame.file, (uint64_t)frame.file );
|
|
|
|
MemWrite( &item.callstackFrame.line, frame.line );
|
2018-06-19 23:06:31 +00:00
|
|
|
|
2019-01-20 18:11:48 +00:00
|
|
|
AppendData( &item, QueueDataSize[(int)QueueType::CallstackFrame] );
|
2018-06-19 23:06:31 +00:00
|
|
|
|
2019-01-20 18:11:48 +00:00
|
|
|
tracy_free( (void*)frame.name );
|
|
|
|
tracy_free( (void*)frame.file );
|
|
|
|
}
|
2018-06-19 23:06:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:09:43 +00:00
|
|
|
|
2017-10-17 20:02:47 +00:00
|
|
|
static bool DontExit() { return false; }
|
|
|
|
|
2017-09-21 23:54:04 +00:00
|
|
|
bool Profiler::HandleServerQuery()
|
|
|
|
{
|
|
|
|
uint8_t type;
|
2019-02-10 14:45:23 +00:00
|
|
|
if( !m_sock->Read( &type, sizeof( type ), 10, DontExit ) ) return false;
|
2017-09-21 23:54:04 +00:00
|
|
|
|
|
|
|
uint64_t ptr;
|
2019-02-10 14:45:23 +00:00
|
|
|
if( !m_sock->Read( &ptr, sizeof( ptr ), 10, DontExit ) ) return false;
|
2017-09-21 23:54:04 +00:00
|
|
|
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case ServerQueryString:
|
|
|
|
SendString( ptr, (const char*)ptr, QueueType::StringData );
|
|
|
|
break;
|
2017-09-21 23:55:02 +00:00
|
|
|
case ServerQueryThreadString:
|
2017-09-22 23:38:26 +00:00
|
|
|
if( ptr == m_mainThread )
|
|
|
|
{
|
|
|
|
SendString( ptr, "Main thread", QueueType::ThreadName );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SendString( ptr, GetThreadName( ptr ), QueueType::ThreadName );
|
|
|
|
}
|
2017-09-21 23:55:02 +00:00
|
|
|
break;
|
2017-09-26 00:28:14 +00:00
|
|
|
case ServerQuerySourceLocation:
|
|
|
|
SendSourceLocation( ptr );
|
|
|
|
break;
|
2017-10-13 01:36:59 +00:00
|
|
|
case ServerQueryPlotName:
|
|
|
|
SendString( ptr, (const char*)ptr, QueueType::PlotName );
|
|
|
|
break;
|
2017-10-18 16:48:51 +00:00
|
|
|
case ServerQueryTerminate:
|
|
|
|
return false;
|
2018-06-19 23:06:31 +00:00
|
|
|
case ServerQueryCallstackFrame:
|
|
|
|
SendCallstackFrame( ptr );
|
|
|
|
break;
|
2018-08-04 18:48:21 +00:00
|
|
|
case ServerQueryFrameName:
|
|
|
|
SendString( ptr, (const char*)ptr, QueueType::FrameName );
|
|
|
|
break;
|
2017-09-21 23:54:04 +00:00
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-23 19:33:05 +00:00
|
|
|
void Profiler::CalibrateTimer()
|
|
|
|
{
|
2018-04-26 13:25:54 +00:00
|
|
|
#ifdef TRACY_HW_TIMER
|
2019-02-21 13:45:13 +00:00
|
|
|
# if __ARM_ARCH >= 6 && !defined TARGET_OS_IOS
|
2018-04-27 14:58:45 +00:00
|
|
|
if( GetTimeImpl == GetTimeImplFallback )
|
|
|
|
{
|
|
|
|
m_timerMul = 1.;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2017-09-23 19:33:05 +00:00
|
|
|
std::atomic_signal_fence( std::memory_order_acq_rel );
|
|
|
|
const auto t0 = std::chrono::high_resolution_clock::now();
|
2018-04-26 13:29:09 +00:00
|
|
|
const auto r0 = GetTime();
|
2017-09-23 19:33:05 +00:00
|
|
|
std::atomic_signal_fence( std::memory_order_acq_rel );
|
2017-11-25 12:34:26 +00:00
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 200 ) );
|
2017-09-23 19:33:05 +00:00
|
|
|
std::atomic_signal_fence( std::memory_order_acq_rel );
|
|
|
|
const auto t1 = std::chrono::high_resolution_clock::now();
|
2018-04-26 13:29:09 +00:00
|
|
|
const auto r1 = GetTime();
|
2017-09-23 19:33:05 +00:00
|
|
|
std::atomic_signal_fence( std::memory_order_acq_rel );
|
|
|
|
|
|
|
|
const auto dt = std::chrono::duration_cast<std::chrono::nanoseconds>( t1 - t0 ).count();
|
|
|
|
const auto dr = r1 - r0;
|
|
|
|
|
|
|
|
m_timerMul = double( dt ) / double( dr );
|
2017-09-25 22:13:24 +00:00
|
|
|
#else
|
|
|
|
m_timerMul = 1.;
|
2017-09-23 19:33:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-24 14:02:09 +00:00
|
|
|
class FakeZone
|
|
|
|
{
|
|
|
|
public:
|
2018-07-27 22:34:04 +00:00
|
|
|
FakeZone( const SourceLocationData* srcloc ) : m_id( (uint64_t)srcloc ) {}
|
2017-09-24 14:02:09 +00:00
|
|
|
~FakeZone() {}
|
|
|
|
|
|
|
|
private:
|
2017-10-01 15:42:22 +00:00
|
|
|
volatile uint64_t m_id;
|
2017-09-24 14:02:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void Profiler::CalibrateDelay()
|
|
|
|
{
|
|
|
|
enum { Iterations = 50000 };
|
|
|
|
enum { Events = Iterations * 2 }; // start + end
|
|
|
|
static_assert( Events * 2 < QueuePrealloc, "Delay calibration loop will allocate memory in queue" );
|
2017-09-26 23:03:29 +00:00
|
|
|
|
2019-02-19 18:33:37 +00:00
|
|
|
moodycamel::ProducerToken ptoken_detail( GetQueue() );
|
|
|
|
moodycamel::ConcurrentQueue<QueueItem>::ExplicitProducer* ptoken = GetQueue().get_explicit_producer( ptoken_detail );
|
2017-09-24 14:02:09 +00:00
|
|
|
for( int i=0; i<Iterations; i++ )
|
|
|
|
{
|
2018-07-27 22:34:04 +00:00
|
|
|
static const tracy::SourceLocationData __tracy_source_location { nullptr, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 };
|
2017-10-01 15:49:45 +00:00
|
|
|
{
|
2017-10-03 12:50:55 +00:00
|
|
|
Magic magic;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = ptoken->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = ptoken->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::ZoneBegin );
|
|
|
|
MemWrite( &item->zoneBegin.thread, GetThreadHandle() );
|
2018-04-27 17:40:47 +00:00
|
|
|
#ifdef TRACY_RDTSCP_OPT
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->zoneBegin.time, Profiler::GetTime( item->zoneBegin.cpu ) );
|
|
|
|
#else
|
|
|
|
uint32_t cpu;
|
|
|
|
MemWrite( &item->zoneBegin.time, Profiler::GetTime( cpu ) );
|
|
|
|
MemWrite( &item->zoneBegin.cpu, cpu );
|
|
|
|
#endif
|
|
|
|
MemWrite( &item->zoneBegin.srcloc, (uint64_t)&__tracy_source_location );
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-01 15:49:45 +00:00
|
|
|
}
|
|
|
|
{
|
2017-10-03 12:50:55 +00:00
|
|
|
Magic magic;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = ptoken->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = ptoken->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::ZoneEnd );
|
|
|
|
MemWrite( &item->zoneEnd.thread, uint64_t( 0 ) );
|
2018-04-27 17:40:47 +00:00
|
|
|
#ifdef TRACY_RDTSCP_OPT
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->zoneEnd.time, GetTime( item->zoneEnd.cpu ) );
|
|
|
|
#else
|
|
|
|
uint32_t cpu;
|
|
|
|
MemWrite( &item->zoneEnd.time, GetTime( cpu ) );
|
|
|
|
MemWrite( &item->zoneEnd.cpu, cpu );
|
|
|
|
#endif
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-01 15:49:45 +00:00
|
|
|
}
|
2017-09-24 14:02:09 +00:00
|
|
|
}
|
2017-10-29 15:12:16 +00:00
|
|
|
const auto f0 = GetTime();
|
2017-09-24 14:02:09 +00:00
|
|
|
for( int i=0; i<Iterations; i++ )
|
|
|
|
{
|
2018-07-27 22:34:04 +00:00
|
|
|
static const tracy::SourceLocationData __tracy_source_location { nullptr, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 };
|
2017-09-26 16:54:48 +00:00
|
|
|
FakeZone ___tracy_scoped_zone( &__tracy_source_location );
|
2017-09-24 14:02:09 +00:00
|
|
|
}
|
2017-10-29 15:12:16 +00:00
|
|
|
const auto t0 = GetTime();
|
2017-09-24 14:02:09 +00:00
|
|
|
for( int i=0; i<Iterations; i++ )
|
|
|
|
{
|
2018-07-27 22:34:04 +00:00
|
|
|
static const tracy::SourceLocationData __tracy_source_location { nullptr, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 };
|
2017-10-01 15:49:45 +00:00
|
|
|
{
|
2017-10-03 12:50:55 +00:00
|
|
|
Magic magic;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = ptoken->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = ptoken->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::ZoneBegin );
|
|
|
|
MemWrite( &item->zoneBegin.thread, GetThreadHandle() );
|
2018-04-27 17:40:47 +00:00
|
|
|
#ifdef TRACY_RDTSCP_OPT
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->zoneBegin.time, Profiler::GetTime( item->zoneBegin.cpu ) );
|
|
|
|
#else
|
|
|
|
uint32_t cpu;
|
|
|
|
MemWrite( &item->zoneBegin.time, Profiler::GetTime( cpu ) );
|
|
|
|
MemWrite( &item->zoneBegin.cpu, cpu );
|
|
|
|
#endif
|
|
|
|
MemWrite( &item->zoneBegin.srcloc, (uint64_t)&__tracy_source_location );
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-01 15:49:45 +00:00
|
|
|
}
|
|
|
|
{
|
2017-10-03 12:50:55 +00:00
|
|
|
Magic magic;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = ptoken->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = ptoken->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::ZoneEnd );
|
|
|
|
MemWrite( &item->zoneEnd.thread, uint64_t( 0 ) );
|
2018-04-27 17:40:47 +00:00
|
|
|
#ifdef TRACY_RDTSCP_OPT
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->zoneEnd.time, GetTime( item->zoneEnd.cpu ) );
|
|
|
|
#else
|
|
|
|
uint32_t cpu;
|
|
|
|
MemWrite( &item->zoneEnd.time, GetTime( cpu ) );
|
|
|
|
MemWrite( &item->zoneEnd.cpu, cpu );
|
|
|
|
#endif
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-01 15:49:45 +00:00
|
|
|
}
|
2017-09-24 14:02:09 +00:00
|
|
|
}
|
2017-10-29 15:12:16 +00:00
|
|
|
const auto t1 = GetTime();
|
2017-09-24 14:02:09 +00:00
|
|
|
const auto dt = t1 - t0;
|
|
|
|
const auto df = t0 - f0;
|
|
|
|
m_delay = ( dt - df ) / Events;
|
|
|
|
|
2017-10-16 18:42:53 +00:00
|
|
|
auto mindiff = std::numeric_limits<int64_t>::max();
|
2017-09-29 16:29:39 +00:00
|
|
|
for( int i=0; i<Iterations * 10; i++ )
|
|
|
|
{
|
2018-06-30 13:36:06 +00:00
|
|
|
const auto t0i = GetTime();
|
|
|
|
const auto t1i = GetTime();
|
|
|
|
const auto dti = t1i - t0i;
|
|
|
|
if( dti > 0 && dti < mindiff ) mindiff = dti;
|
2017-09-29 16:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_resolution = mindiff;
|
|
|
|
|
2017-09-24 14:02:09 +00:00
|
|
|
enum { Bulk = 1000 };
|
2019-02-19 18:33:37 +00:00
|
|
|
moodycamel::ConsumerToken token( GetQueue() );
|
2017-09-24 14:02:09 +00:00
|
|
|
int left = Events * 2;
|
|
|
|
QueueItem item[Bulk];
|
|
|
|
while( left != 0 )
|
|
|
|
{
|
2019-02-19 18:33:37 +00:00
|
|
|
const auto sz = GetQueue().try_dequeue_bulk( token, item, std::min( left, (int)Bulk ) );
|
2017-09-24 14:02:09 +00:00
|
|
|
assert( sz > 0 );
|
2017-10-16 18:42:53 +00:00
|
|
|
left -= (int)sz;
|
2017-09-24 14:02:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 20:20:44 +00:00
|
|
|
void Profiler::SendCallstack( int depth, uint64_t thread, const char* skipBefore )
|
|
|
|
{
|
|
|
|
#ifdef TRACY_HAS_CALLSTACK
|
|
|
|
auto ptr = Callstack( depth );
|
|
|
|
auto data = (uintptr_t*)ptr;
|
|
|
|
const auto sz = *data++;
|
2018-08-20 23:24:00 +00:00
|
|
|
uintptr_t i;
|
2018-08-20 20:20:44 +00:00
|
|
|
for( i=0; i<sz; i++ )
|
|
|
|
{
|
2019-01-20 18:11:48 +00:00
|
|
|
auto frameData = DecodeCallstackPtr( uint64_t( data[i] ) );
|
|
|
|
const bool found = strcmp( frameData.data[0].name, skipBefore ) == 0;
|
|
|
|
for( uint8_t j=0; j<frameData.size; j++ )
|
|
|
|
{
|
|
|
|
tracy_free( (void*)frameData.data[j].name );
|
|
|
|
tracy_free( (void*)frameData.data[j].file );
|
|
|
|
}
|
2018-08-20 20:20:44 +00:00
|
|
|
if( found )
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i != sz )
|
|
|
|
{
|
|
|
|
memmove( data, data + i, ( sz - i ) * sizeof( uintptr_t* ) );
|
|
|
|
*--data = sz - i;
|
|
|
|
}
|
|
|
|
|
|
|
|
Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = GetToken();
|
2018-08-20 20:20:44 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
MemWrite( &item->hdr.type, QueueType::Callstack );
|
|
|
|
MemWrite( &item->callstack.ptr, ptr );
|
|
|
|
MemWrite( &item->callstack.thread, thread );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:59:02 +00:00
|
|
|
#ifdef TRACY_HAS_SYSTIME
|
|
|
|
void Profiler::ProcessSysTime()
|
|
|
|
{
|
|
|
|
auto t = std::chrono::high_resolution_clock::now().time_since_epoch().count();
|
|
|
|
if( t - m_sysTimeLast > 100000000 ) // 100 ms
|
|
|
|
{
|
|
|
|
m_sysTimeLast = t;
|
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto token = GetToken();
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
MemWrite( &item->hdr.type, QueueType::SysTimeReport );
|
|
|
|
MemWrite( &item->sysTime.time, GetTime() );
|
|
|
|
MemWrite( &item->sysTime.sysTime, m_sysTime.Get() );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-10 15:43:56 +00:00
|
|
|
}
|
2017-10-16 19:34:39 +00:00
|
|
|
|
2019-01-14 19:55:37 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TracyCZoneCtx ___tracy_emit_zone_begin( const struct ___tracy_source_location_data* srcloc, int active )
|
|
|
|
{
|
|
|
|
___tracy_c_zone_context ctx;
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
ctx.active = active && tracy::GetProfiler().IsConnected();
|
2019-01-14 19:55:37 +00:00
|
|
|
#else
|
|
|
|
ctx.active = active;
|
|
|
|
#endif
|
|
|
|
if( !ctx.active ) return ctx;
|
2019-01-14 21:28:58 +00:00
|
|
|
const auto thread = tracy::GetThreadHandle();
|
2019-02-19 17:38:08 +00:00
|
|
|
const auto id = tracy::GetProfiler().GetNextZoneId();
|
2019-01-14 21:28:58 +00:00
|
|
|
ctx.id = id;
|
|
|
|
|
2019-01-14 19:55:37 +00:00
|
|
|
tracy::Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = tracy::GetToken();
|
2019-01-14 19:55:37 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2019-01-15 17:59:05 +00:00
|
|
|
#ifndef TRACY_NO_VERIFY
|
2019-01-14 21:28:58 +00:00
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneValidation );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.id, id );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
2019-01-15 17:59:05 +00:00
|
|
|
#endif
|
2019-01-14 21:28:58 +00:00
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneBegin );
|
2019-01-14 19:55:37 +00:00
|
|
|
#ifdef TRACY_RDTSCP_OPT
|
2019-01-14 21:28:58 +00:00
|
|
|
tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( item->zoneBegin.cpu ) );
|
2019-01-14 19:55:37 +00:00
|
|
|
#else
|
2019-01-14 21:28:58 +00:00
|
|
|
uint32_t cpu;
|
|
|
|
tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( cpu ) );
|
|
|
|
tracy::MemWrite( &item->zoneBegin.cpu, cpu );
|
2019-01-14 19:55:37 +00:00
|
|
|
#endif
|
2019-01-14 21:28:58 +00:00
|
|
|
tracy::MemWrite( &item->zoneBegin.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneBegin.srcloc, (uint64_t)srcloc );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
2019-01-14 19:55:37 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
TracyCZoneCtx ___tracy_emit_zone_begin_callstack( const struct ___tracy_source_location_data* srcloc, int depth, int active )
|
|
|
|
{
|
|
|
|
___tracy_c_zone_context ctx;
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
ctx.active = active && tracy::GetProfiler().IsConnected();
|
2019-01-14 19:55:37 +00:00
|
|
|
#else
|
|
|
|
ctx.active = active;
|
|
|
|
#endif
|
|
|
|
if( !ctx.active ) return ctx;
|
|
|
|
const auto thread = tracy::GetThreadHandle();
|
2019-02-19 17:38:08 +00:00
|
|
|
const auto id = tracy::GetProfiler().GetNextZoneId();
|
2019-01-14 21:28:58 +00:00
|
|
|
ctx.id = id;
|
|
|
|
|
2019-01-14 19:55:37 +00:00
|
|
|
tracy::Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = tracy::GetToken();
|
2019-01-14 19:55:37 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2019-01-15 17:59:05 +00:00
|
|
|
#ifndef TRACY_NO_VERIFY
|
2019-01-14 21:28:58 +00:00
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneValidation );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.id, id );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
2019-01-15 17:59:05 +00:00
|
|
|
#endif
|
2019-01-14 21:28:58 +00:00
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneBeginCallstack );
|
2019-01-14 19:55:37 +00:00
|
|
|
#ifdef TRACY_RDTSCP_OPT
|
2019-01-14 21:28:58 +00:00
|
|
|
tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( item->zoneBegin.cpu ) );
|
2019-01-14 19:55:37 +00:00
|
|
|
#else
|
2019-01-14 21:28:58 +00:00
|
|
|
uint32_t cpu;
|
|
|
|
tracy::MemWrite( &item->zoneBegin.time, tracy::Profiler::GetTime( cpu ) );
|
|
|
|
tracy::MemWrite( &item->zoneBegin.cpu, cpu );
|
2019-01-14 19:55:37 +00:00
|
|
|
#endif
|
2019-01-14 21:28:58 +00:00
|
|
|
tracy::MemWrite( &item->zoneBegin.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneBegin.srcloc, (uint64_t)srcloc );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
2019-01-14 19:55:37 +00:00
|
|
|
|
2019-02-19 17:38:08 +00:00
|
|
|
tracy::GetProfiler().SendCallstack( depth, thread );
|
2019-01-14 19:55:37 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ___tracy_emit_zone_end( TracyCZoneCtx ctx )
|
|
|
|
{
|
|
|
|
if( !ctx.active ) return;
|
2019-01-14 21:28:58 +00:00
|
|
|
const auto thread = tracy::GetThreadHandle();
|
2019-01-14 19:55:37 +00:00
|
|
|
tracy::Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = tracy::GetToken();
|
2019-01-14 19:55:37 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2019-01-15 17:59:05 +00:00
|
|
|
#ifndef TRACY_NO_VERIFY
|
2019-01-14 21:28:58 +00:00
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneValidation );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.id, ctx.id );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
2019-01-15 17:59:05 +00:00
|
|
|
#endif
|
2019-01-14 21:28:58 +00:00
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneEnd );
|
2019-01-14 19:55:37 +00:00
|
|
|
#ifdef TRACY_RDTSCP_OPT
|
2019-01-14 21:28:58 +00:00
|
|
|
tracy::MemWrite( &item->zoneEnd.time, tracy::Profiler::GetTime( item->zoneEnd.cpu ) );
|
2019-01-14 19:55:37 +00:00
|
|
|
#else
|
2019-01-14 21:28:58 +00:00
|
|
|
uint32_t cpu;
|
|
|
|
tracy::MemWrite( &item->zoneEnd.time, tracy::Profiler::GetTime( cpu ) );
|
|
|
|
tracy::MemWrite( &item->zoneEnd.cpu, cpu );
|
2019-01-14 19:55:37 +00:00
|
|
|
#endif
|
2019-01-14 21:28:58 +00:00
|
|
|
tracy::MemWrite( &item->zoneEnd.thread, thread );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
2019-01-14 19:55:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 00:17:01 +00:00
|
|
|
void ___tracy_emit_zone_text( TracyCZoneCtx ctx, const char* txt, size_t size )
|
|
|
|
{
|
|
|
|
if( !ctx.active ) return;
|
|
|
|
const auto thread = tracy::GetThreadHandle();
|
|
|
|
tracy::Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = tracy::GetToken();
|
2019-01-16 00:17:01 +00:00
|
|
|
auto ptr = (char*)tracy::tracy_malloc( size+1 );
|
|
|
|
memcpy( ptr, txt, size );
|
|
|
|
ptr[size] = '\0';
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
#ifndef TRACY_NO_VERIFY
|
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneValidation );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.id, ctx.id );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneText );
|
|
|
|
tracy::MemWrite( &item->zoneText.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneText.text, (uint64_t)ptr );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ___tracy_emit_zone_name( TracyCZoneCtx ctx, const char* txt, size_t size )
|
|
|
|
{
|
|
|
|
if( !ctx.active ) return;
|
|
|
|
const auto thread = tracy::GetThreadHandle();
|
|
|
|
tracy::Magic magic;
|
2019-02-19 17:27:00 +00:00
|
|
|
auto token = tracy::GetToken();
|
2019-01-16 00:17:01 +00:00
|
|
|
auto ptr = (char*)tracy::tracy_malloc( size+1 );
|
|
|
|
memcpy( ptr, txt, size );
|
|
|
|
ptr[size] = '\0';
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
#ifndef TRACY_NO_VERIFY
|
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneValidation );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneValidation.id, ctx.id );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
auto item = token->enqueue_begin<tracy::moodycamel::CanAlloc>( magic );
|
|
|
|
tracy::MemWrite( &item->hdr.type, tracy::QueueType::ZoneName );
|
|
|
|
tracy::MemWrite( &item->zoneText.thread, thread );
|
|
|
|
tracy::MemWrite( &item->zoneText.text, (uint64_t)ptr );
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 19:55:37 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-16 19:34:39 +00:00
|
|
|
#endif
|