2019-08-12 18:08:15 +00:00
|
|
|
#include "TracySysTrace.hpp"
|
|
|
|
|
|
|
|
#ifdef TRACY_HAS_SYSTEM_TRACING
|
|
|
|
|
2021-05-11 16:31:20 +00:00
|
|
|
#ifndef TRACY_SAMPLING_HZ
|
|
|
|
# if defined _WIN32 || defined __CYGWIN__
|
|
|
|
# define TRACY_SAMPLING_HZ 8000
|
|
|
|
# elif defined __linux__
|
|
|
|
# define TRACY_SAMPLING_HZ 10000
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
static constexpr int GetSamplingFrequency()
|
|
|
|
{
|
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
|
|
|
return TRACY_SAMPLING_HZ > 8000 ? 8000 : ( TRACY_SAMPLING_HZ < 1 ? 1 : TRACY_SAMPLING_HZ );
|
|
|
|
#else
|
|
|
|
return TRACY_SAMPLING_HZ > 1000000 ? 1000000 : ( TRACY_SAMPLING_HZ < 1 ? 1 : TRACY_SAMPLING_HZ );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr int GetSamplingPeriod()
|
|
|
|
{
|
|
|
|
return 1000000000 / GetSamplingFrequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-12 21:05:17 +00:00
|
|
|
# if defined _WIN32 || defined __CYGWIN__
|
|
|
|
|
2019-10-10 18:29:06 +00:00
|
|
|
# ifndef NOMINMAX
|
|
|
|
# define NOMINMAX
|
|
|
|
# endif
|
|
|
|
|
2019-08-12 21:05:17 +00:00
|
|
|
# define INITGUID
|
|
|
|
# include <assert.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <windows.h>
|
2019-08-17 18:22:06 +00:00
|
|
|
# include <dbghelp.h>
|
2019-08-12 21:05:17 +00:00
|
|
|
# include <evntrace.h>
|
|
|
|
# include <evntcons.h>
|
2019-08-16 17:22:23 +00:00
|
|
|
# include <psapi.h>
|
2019-08-17 18:22:06 +00:00
|
|
|
# include <winternl.h>
|
2019-08-12 21:05:17 +00:00
|
|
|
|
|
|
|
# include "../common/TracyAlloc.hpp"
|
2019-08-14 00:22:45 +00:00
|
|
|
# include "../common/TracySystem.hpp"
|
2019-08-12 21:05:17 +00:00
|
|
|
# include "TracyProfiler.hpp"
|
2020-06-27 17:56:23 +00:00
|
|
|
# include "TracyThread.hpp"
|
2019-08-12 21:05:17 +00:00
|
|
|
|
2019-08-12 18:08:15 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2020-12-21 15:13:59 +00:00
|
|
|
static const GUID PerfInfoGuid = { 0xce1dbfb4, 0x137e, 0x4da6, { 0x87, 0xb0, 0x3f, 0x59, 0xaa, 0x10, 0x2c, 0xbc } };
|
|
|
|
static const GUID DxgKrnlGuid = { 0x802ec45a, 0x1e99, 0x4b83, { 0x99, 0x20, 0x87, 0xc9, 0x82, 0x77, 0xba, 0x9d } };
|
2020-06-27 17:56:23 +00:00
|
|
|
|
2020-02-22 12:13:32 +00:00
|
|
|
|
2020-02-22 12:08:35 +00:00
|
|
|
static TRACEHANDLE s_traceHandle;
|
|
|
|
static TRACEHANDLE s_traceHandle2;
|
|
|
|
static EVENT_TRACE_PROPERTIES* s_prop;
|
2020-02-22 12:11:16 +00:00
|
|
|
static DWORD s_pid;
|
2019-08-12 21:05:17 +00:00
|
|
|
|
2020-06-27 17:56:23 +00:00
|
|
|
static EVENT_TRACE_PROPERTIES* s_propVsync;
|
|
|
|
static TRACEHANDLE s_traceHandleVsync;
|
|
|
|
static TRACEHANDLE s_traceHandleVsync2;
|
|
|
|
Thread* s_threadVsync = nullptr;
|
|
|
|
|
2019-08-12 21:05:17 +00:00
|
|
|
struct CSwitch
|
|
|
|
{
|
|
|
|
uint32_t newThreadId;
|
|
|
|
uint32_t oldThreadId;
|
|
|
|
int8_t newThreadPriority;
|
|
|
|
int8_t oldThreadPriority;
|
|
|
|
uint8_t previousCState;
|
|
|
|
int8_t spareByte;
|
|
|
|
int8_t oldThreadWaitReason;
|
|
|
|
int8_t oldThreadWaitMode;
|
|
|
|
int8_t oldThreadState;
|
|
|
|
int8_t oldThreadWaitIdealProcessor;
|
|
|
|
uint32_t newThreadWaitTime;
|
|
|
|
uint32_t reserved;
|
|
|
|
};
|
|
|
|
|
2019-08-17 15:05:29 +00:00
|
|
|
struct ReadyThread
|
|
|
|
{
|
|
|
|
uint32_t threadId;
|
|
|
|
int8_t adjustReason;
|
|
|
|
int8_t adjustIncrement;
|
|
|
|
int8_t flag;
|
|
|
|
int8_t reserverd;
|
|
|
|
};
|
|
|
|
|
2020-01-14 01:06:22 +00:00
|
|
|
struct ThreadTrace
|
|
|
|
{
|
|
|
|
uint32_t processId;
|
|
|
|
uint32_t threadId;
|
|
|
|
uint32_t stackBase;
|
|
|
|
uint32_t stackLimit;
|
|
|
|
uint32_t userStackBase;
|
|
|
|
uint32_t userStackLimit;
|
|
|
|
uint32_t startAddr;
|
|
|
|
uint32_t win32StartAddr;
|
|
|
|
uint32_t tebBase;
|
|
|
|
uint32_t subProcessTag;
|
|
|
|
};
|
|
|
|
|
2020-02-22 12:42:09 +00:00
|
|
|
struct StackWalkEvent
|
|
|
|
{
|
|
|
|
uint64_t eventTimeStamp;
|
|
|
|
uint32_t stackProcess;
|
|
|
|
uint32_t stackThread;
|
|
|
|
uint64_t stack[192];
|
|
|
|
};
|
|
|
|
|
2020-06-27 17:56:23 +00:00
|
|
|
struct VSyncInfo
|
|
|
|
{
|
|
|
|
void* dxgAdapter;
|
|
|
|
uint32_t vidPnTargetId;
|
|
|
|
uint64_t scannedPhysicalAddress;
|
|
|
|
uint32_t vidPnSourceId;
|
|
|
|
uint32_t frameNumber;
|
|
|
|
int64_t frameQpcTime;
|
|
|
|
void* hFlipDevice;
|
|
|
|
uint32_t flipType;
|
|
|
|
uint64_t flipFenceId;
|
|
|
|
};
|
|
|
|
|
2020-02-22 15:03:07 +00:00
|
|
|
#ifdef __CYGWIN__
|
2020-04-07 19:33:03 +00:00
|
|
|
extern "C" typedef DWORD (WINAPI *t_GetProcessIdOfThread)( HANDLE );
|
|
|
|
extern "C" typedef DWORD (WINAPI *t_GetProcessImageFileNameA)( HANDLE, LPSTR, DWORD );
|
2020-02-22 15:03:07 +00:00
|
|
|
extern "C" ULONG WMIAPI TraceSetInformation(TRACEHANDLE SessionHandle, TRACE_INFO_CLASS InformationClass, PVOID TraceInformation, ULONG InformationLength);
|
2020-04-07 19:35:37 +00:00
|
|
|
t_GetProcessIdOfThread GetProcessIdOfThread = (t_GetProcessIdOfThread)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "GetProcessIdOfThread" );
|
|
|
|
t_GetProcessImageFileNameA GetProcessImageFileNameA = (t_GetProcessImageFileNameA)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "K32GetProcessImageFileNameA" );
|
2020-02-22 15:03:07 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-07 19:33:03 +00:00
|
|
|
extern "C" typedef NTSTATUS (WINAPI *t_NtQueryInformationThread)( HANDLE, THREADINFOCLASS, PVOID, ULONG, PULONG );
|
|
|
|
extern "C" typedef BOOL (WINAPI *t_EnumProcessModules)( HANDLE, HMODULE*, DWORD, LPDWORD );
|
|
|
|
extern "C" typedef BOOL (WINAPI *t_GetModuleInformation)( HANDLE, HMODULE, LPMODULEINFO, DWORD );
|
|
|
|
extern "C" typedef DWORD (WINAPI *t_GetModuleBaseNameA)( HANDLE, HMODULE, LPSTR, DWORD );
|
|
|
|
extern "C" typedef HRESULT (WINAPI *t_GetThreadDescription)( HANDLE, PWSTR* );
|
2020-04-07 19:35:37 +00:00
|
|
|
|
|
|
|
t_NtQueryInformationThread NtQueryInformationThread = (t_NtQueryInformationThread)GetProcAddress( GetModuleHandleA( "ntdll.dll" ), "NtQueryInformationThread" );
|
|
|
|
t_EnumProcessModules _EnumProcessModules = (t_EnumProcessModules)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "K32EnumProcessModules" );
|
|
|
|
t_GetModuleInformation _GetModuleInformation = (t_GetModuleInformation)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "K32GetModuleInformation" );
|
|
|
|
t_GetModuleBaseNameA _GetModuleBaseNameA = (t_GetModuleBaseNameA)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "K32GetModuleBaseNameA" );
|
2020-04-07 19:33:03 +00:00
|
|
|
|
|
|
|
static t_GetThreadDescription _GetThreadDescription = 0;
|
|
|
|
|
|
|
|
|
2019-08-26 15:59:58 +00:00
|
|
|
void WINAPI EventRecordCallback( PEVENT_RECORD record )
|
2019-08-12 21:05:17 +00:00
|
|
|
{
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !GetProfiler().IsConnected() ) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const auto& hdr = record->EventHeader;
|
2020-02-22 11:56:33 +00:00
|
|
|
switch( hdr.ProviderId.Data1 )
|
2019-08-17 15:05:29 +00:00
|
|
|
{
|
2020-02-22 11:56:33 +00:00
|
|
|
case 0x3d6fa8d1: // Thread Guid
|
|
|
|
if( hdr.EventDescriptor.Opcode == 36 )
|
|
|
|
{
|
|
|
|
const auto cswitch = (const CSwitch*)record->UserData;
|
|
|
|
|
|
|
|
TracyLfqPrepare( QueueType::ContextSwitch );
|
|
|
|
MemWrite( &item->contextSwitch.time, hdr.TimeStamp.QuadPart );
|
|
|
|
memcpy( &item->contextSwitch.oldThread, &cswitch->oldThreadId, sizeof( cswitch->oldThreadId ) );
|
|
|
|
memcpy( &item->contextSwitch.newThread, &cswitch->newThreadId, sizeof( cswitch->newThreadId ) );
|
|
|
|
memset( ((char*)&item->contextSwitch.oldThread)+4, 0, 4 );
|
|
|
|
memset( ((char*)&item->contextSwitch.newThread)+4, 0, 4 );
|
|
|
|
MemWrite( &item->contextSwitch.cpu, record->BufferContext.ProcessorNumber );
|
|
|
|
MemWrite( &item->contextSwitch.reason, cswitch->oldThreadWaitReason );
|
|
|
|
MemWrite( &item->contextSwitch.state, cswitch->oldThreadState );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
|
|
|
else if( hdr.EventDescriptor.Opcode == 50 )
|
|
|
|
{
|
|
|
|
const auto rt = (const ReadyThread*)record->UserData;
|
|
|
|
|
|
|
|
TracyLfqPrepare( QueueType::ThreadWakeup );
|
|
|
|
MemWrite( &item->threadWakeup.time, hdr.TimeStamp.QuadPart );
|
|
|
|
memcpy( &item->threadWakeup.thread, &rt->threadId, sizeof( rt->threadId ) );
|
|
|
|
memset( ((char*)&item->threadWakeup.thread)+4, 0, 4 );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
|
|
|
else if( hdr.EventDescriptor.Opcode == 1 || hdr.EventDescriptor.Opcode == 3 )
|
|
|
|
{
|
|
|
|
const auto tt = (const ThreadTrace*)record->UserData;
|
|
|
|
|
|
|
|
uint64_t tid = tt->threadId;
|
|
|
|
if( tid == 0 ) return;
|
|
|
|
uint64_t pid = tt->processId;
|
|
|
|
TracyLfqPrepare( QueueType::TidToPid );
|
|
|
|
MemWrite( &item->tidToPid.tid, tid );
|
|
|
|
MemWrite( &item->tidToPid.pid, pid );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
|
|
|
break;
|
2020-02-22 12:42:09 +00:00
|
|
|
case 0xdef2fe46: // StackWalk Guid
|
|
|
|
if( hdr.EventDescriptor.Opcode == 32 )
|
|
|
|
{
|
|
|
|
const auto sw = (const StackWalkEvent*)record->UserData;
|
2020-03-21 14:23:51 +00:00
|
|
|
if( sw->stackProcess == s_pid && ( sw->stack[0] & 0x8000000000000000 ) == 0 )
|
2020-02-22 12:42:09 +00:00
|
|
|
{
|
|
|
|
const uint64_t sz = ( record->UserDataLength - 16 ) / 8;
|
2020-03-28 15:09:44 +00:00
|
|
|
if( sz > 0 )
|
|
|
|
{
|
|
|
|
auto trace = (uint64_t*)tracy_malloc( ( 1 + sz ) * sizeof( uint64_t ) );
|
|
|
|
memcpy( trace, &sz, sizeof( uint64_t ) );
|
|
|
|
memcpy( trace+1, sw->stack, sizeof( uint64_t ) * sz );
|
|
|
|
TracyLfqPrepare( QueueType::CallstackSample );
|
2020-07-26 12:28:13 +00:00
|
|
|
MemWrite( &item->callstackSampleFat.time, sw->eventTimeStamp );
|
|
|
|
MemWrite( &item->callstackSampleFat.thread, (uint64_t)sw->stackThread );
|
|
|
|
MemWrite( &item->callstackSampleFat.ptr, (uint64_t)trace );
|
2020-03-28 15:09:44 +00:00
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
2020-02-22 12:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-02-22 11:56:33 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-01-14 01:06:22 +00:00
|
|
|
}
|
2019-08-12 21:05:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 17:56:23 +00:00
|
|
|
static constexpr const char* VsyncName[] = {
|
|
|
|
"[0] Vsync",
|
|
|
|
"[1] Vsync",
|
|
|
|
"[2] Vsync",
|
|
|
|
"[3] Vsync",
|
|
|
|
"[4] Vsync",
|
|
|
|
"[5] Vsync",
|
|
|
|
"[6] Vsync",
|
|
|
|
"[7] Vsync",
|
|
|
|
"Vsync"
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint32_t VsyncTarget[8] = {};
|
|
|
|
|
|
|
|
void WINAPI EventRecordCallbackVsync( PEVENT_RECORD record )
|
|
|
|
{
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !GetProfiler().IsConnected() ) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const auto& hdr = record->EventHeader;
|
|
|
|
assert( hdr.ProviderId.Data1 == 0x802EC45A );
|
|
|
|
assert( hdr.EventDescriptor.Id == 0x0011 );
|
|
|
|
|
|
|
|
const auto vs = (const VSyncInfo*)record->UserData;
|
|
|
|
|
|
|
|
int idx = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( VsyncTarget[idx] == 0 )
|
|
|
|
{
|
|
|
|
VsyncTarget[idx] = vs->vidPnTargetId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( VsyncTarget[idx] == vs->vidPnTargetId )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( ++idx < 8 );
|
|
|
|
|
|
|
|
TracyLfqPrepare( QueueType::FrameMarkMsg );
|
|
|
|
MemWrite( &item->frameMark.time, hdr.TimeStamp.QuadPart );
|
|
|
|
MemWrite( &item->frameMark.name, uint64_t( VsyncName[idx] ) );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetupVsync()
|
|
|
|
{
|
2020-09-06 12:02:18 +00:00
|
|
|
#if _WIN32_WINNT >= _WIN32_WINNT_WINBLUE
|
2020-06-27 17:56:23 +00:00
|
|
|
const auto psz = sizeof( EVENT_TRACE_PROPERTIES ) + MAX_PATH;
|
|
|
|
s_propVsync = (EVENT_TRACE_PROPERTIES*)tracy_malloc( psz );
|
|
|
|
memset( s_propVsync, 0, sizeof( EVENT_TRACE_PROPERTIES ) );
|
|
|
|
s_propVsync->LogFileMode = EVENT_TRACE_REAL_TIME_MODE;
|
|
|
|
s_propVsync->Wnode.BufferSize = psz;
|
|
|
|
#ifdef TRACY_TIMER_QPC
|
|
|
|
s_propVsync->Wnode.ClientContext = 1;
|
|
|
|
#else
|
|
|
|
s_propVsync->Wnode.ClientContext = 3;
|
|
|
|
#endif
|
|
|
|
s_propVsync->LoggerNameOffset = sizeof( EVENT_TRACE_PROPERTIES );
|
|
|
|
strcpy( ((char*)s_propVsync) + sizeof( EVENT_TRACE_PROPERTIES ), "TracyVsync" );
|
|
|
|
|
|
|
|
auto backup = tracy_malloc( psz );
|
|
|
|
memcpy( backup, s_propVsync, psz );
|
|
|
|
|
|
|
|
const auto controlStatus = ControlTraceA( 0, "TracyVsync", s_propVsync, EVENT_TRACE_CONTROL_STOP );
|
|
|
|
if( controlStatus != ERROR_SUCCESS && controlStatus != ERROR_WMI_INSTANCE_NOT_FOUND )
|
|
|
|
{
|
|
|
|
tracy_free( backup );
|
|
|
|
tracy_free( s_propVsync );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( s_propVsync, backup, psz );
|
|
|
|
tracy_free( backup );
|
|
|
|
|
|
|
|
const auto startStatus = StartTraceA( &s_traceHandleVsync, "TracyVsync", s_propVsync );
|
|
|
|
if( startStatus != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
tracy_free( s_propVsync );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVENT_FILTER_EVENT_ID fe = {};
|
|
|
|
fe.FilterIn = TRUE;
|
|
|
|
fe.Count = 1;
|
|
|
|
fe.Events[0] = 0x0011; // VSyncDPC_Info
|
|
|
|
|
|
|
|
EVENT_FILTER_DESCRIPTOR desc = {};
|
|
|
|
desc.Ptr = (ULONGLONG)&fe;
|
|
|
|
desc.Size = sizeof( fe );
|
|
|
|
desc.Type = EVENT_FILTER_TYPE_EVENT_ID;
|
|
|
|
|
|
|
|
ENABLE_TRACE_PARAMETERS params = {};
|
|
|
|
params.Version = ENABLE_TRACE_PARAMETERS_VERSION_2;
|
|
|
|
params.EnableProperty = EVENT_ENABLE_PROPERTY_IGNORE_KEYWORD_0;
|
|
|
|
params.SourceId = s_propVsync->Wnode.Guid;
|
|
|
|
params.EnableFilterDesc = &desc;
|
|
|
|
params.FilterDescCount = 1;
|
|
|
|
|
|
|
|
uint64_t mask = 0x4000000000000001; // Microsoft_Windows_DxgKrnl_Performance | Base
|
2020-12-21 14:41:01 +00:00
|
|
|
if( EnableTraceEx2( s_traceHandleVsync, &DxgKrnlGuid, EVENT_CONTROL_CODE_ENABLE_PROVIDER, TRACE_LEVEL_INFORMATION, mask, mask, 0, ¶ms ) != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
tracy_free( s_propVsync );
|
|
|
|
return;
|
|
|
|
}
|
2020-06-27 17:56:23 +00:00
|
|
|
|
|
|
|
char loggerName[MAX_PATH];
|
|
|
|
strcpy( loggerName, "TracyVsync" );
|
|
|
|
|
|
|
|
EVENT_TRACE_LOGFILEA log = {};
|
|
|
|
log.LoggerName = loggerName;
|
|
|
|
log.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD | PROCESS_TRACE_MODE_RAW_TIMESTAMP;
|
|
|
|
log.EventRecordCallback = EventRecordCallbackVsync;
|
|
|
|
|
|
|
|
s_traceHandleVsync2 = OpenTraceA( &log );
|
|
|
|
if( s_traceHandleVsync2 == (TRACEHANDLE)INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
CloseTrace( s_traceHandleVsync );
|
|
|
|
tracy_free( s_propVsync );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_threadVsync = (Thread*)tracy_malloc( sizeof( Thread ) );
|
|
|
|
new(s_threadVsync) Thread( [] (void*) {
|
2020-07-19 01:24:49 +00:00
|
|
|
ThreadExitHandler threadExitHandler;
|
2020-08-05 13:34:39 +00:00
|
|
|
SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
|
2020-06-27 17:56:23 +00:00
|
|
|
SetThreadName( "Tracy Vsync" );
|
|
|
|
ProcessTrace( &s_traceHandleVsync2, 1, nullptr, nullptr );
|
|
|
|
}, nullptr );
|
2020-09-06 12:02:18 +00:00
|
|
|
#endif
|
2020-06-27 17:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 16:31:20 +00:00
|
|
|
static constexpr int GetSamplingInterval()
|
|
|
|
{
|
|
|
|
return GetSamplingPeriod() / 100;
|
|
|
|
}
|
|
|
|
|
2020-02-25 22:08:52 +00:00
|
|
|
bool SysTraceStart( int64_t& samplingPeriod )
|
2019-08-12 18:08:15 +00:00
|
|
|
{
|
2020-04-07 19:33:03 +00:00
|
|
|
if( !_GetThreadDescription ) _GetThreadDescription = (t_GetThreadDescription)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "GetThreadDescription" );
|
|
|
|
|
2020-02-22 12:11:16 +00:00
|
|
|
s_pid = GetCurrentProcessId();
|
|
|
|
|
2020-02-22 13:16:04 +00:00
|
|
|
#if defined _WIN64
|
|
|
|
constexpr bool isOs64Bit = true;
|
|
|
|
#else
|
|
|
|
BOOL _iswow64;
|
|
|
|
IsWow64Process( GetCurrentProcess(), &_iswow64 );
|
|
|
|
const bool isOs64Bit = _iswow64;
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 21:05:17 +00:00
|
|
|
TOKEN_PRIVILEGES priv = {};
|
|
|
|
priv.PrivilegeCount = 1;
|
|
|
|
priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
|
|
|
if( LookupPrivilegeValue( nullptr, SE_SYSTEM_PROFILE_NAME, &priv.Privileges[0].Luid ) == 0 ) return false;
|
|
|
|
|
|
|
|
HANDLE pt;
|
|
|
|
if( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &pt ) == 0 ) return false;
|
|
|
|
const auto adjust = AdjustTokenPrivileges( pt, FALSE, &priv, 0, nullptr, nullptr );
|
|
|
|
CloseHandle( pt );
|
|
|
|
if( adjust == 0 ) return false;
|
|
|
|
const auto status = GetLastError();
|
|
|
|
if( status != ERROR_SUCCESS ) return false;
|
|
|
|
|
2020-02-22 13:16:04 +00:00
|
|
|
if( isOs64Bit )
|
|
|
|
{
|
|
|
|
TRACE_PROFILE_INTERVAL interval = {};
|
2021-05-11 16:31:20 +00:00
|
|
|
interval.Interval = GetSamplingInterval();
|
2020-02-22 13:16:04 +00:00
|
|
|
const auto intervalStatus = TraceSetInformation( 0, TraceSampledProfileIntervalInfo, &interval, sizeof( interval ) );
|
|
|
|
if( intervalStatus != ERROR_SUCCESS ) return false;
|
2021-05-11 16:31:20 +00:00
|
|
|
samplingPeriod = GetSamplingPeriod();
|
2020-02-22 13:16:04 +00:00
|
|
|
}
|
2020-02-22 12:13:32 +00:00
|
|
|
|
2019-08-12 21:05:17 +00:00
|
|
|
const auto psz = sizeof( EVENT_TRACE_PROPERTIES ) + sizeof( KERNEL_LOGGER_NAME );
|
2019-08-12 23:58:16 +00:00
|
|
|
s_prop = (EVENT_TRACE_PROPERTIES*)tracy_malloc( psz );
|
|
|
|
memset( s_prop, 0, sizeof( EVENT_TRACE_PROPERTIES ) );
|
2020-11-05 22:56:05 +00:00
|
|
|
ULONG flags = 0;
|
|
|
|
#ifndef TRACY_NO_CONTEXT_SWITCH
|
|
|
|
flags = EVENT_TRACE_FLAG_CSWITCH | EVENT_TRACE_FLAG_DISPATCHER | EVENT_TRACE_FLAG_THREAD;
|
|
|
|
#endif
|
2020-11-05 22:59:52 +00:00
|
|
|
#ifndef TRACY_NO_SAMPLING
|
2020-02-22 13:16:04 +00:00
|
|
|
if( isOs64Bit ) flags |= EVENT_TRACE_FLAG_PROFILE;
|
2020-11-05 22:59:52 +00:00
|
|
|
#endif
|
2020-02-22 13:16:04 +00:00
|
|
|
s_prop->EnableFlags = flags;
|
2019-08-12 23:58:16 +00:00
|
|
|
s_prop->LogFileMode = EVENT_TRACE_REAL_TIME_MODE;
|
|
|
|
s_prop->Wnode.BufferSize = psz;
|
|
|
|
s_prop->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
|
2020-04-07 20:01:31 +00:00
|
|
|
#ifdef TRACY_TIMER_QPC
|
|
|
|
s_prop->Wnode.ClientContext = 1;
|
|
|
|
#else
|
2019-08-12 23:58:16 +00:00
|
|
|
s_prop->Wnode.ClientContext = 3;
|
2020-04-07 20:01:31 +00:00
|
|
|
#endif
|
2019-08-12 23:58:16 +00:00
|
|
|
s_prop->Wnode.Guid = SystemTraceControlGuid;
|
2020-02-22 17:34:52 +00:00
|
|
|
s_prop->BufferSize = 1024;
|
2020-08-13 13:26:36 +00:00
|
|
|
s_prop->MinimumBuffers = std::thread::hardware_concurrency() * 4;
|
|
|
|
s_prop->MaximumBuffers = std::thread::hardware_concurrency() * 6;
|
2019-08-12 23:58:16 +00:00
|
|
|
s_prop->LoggerNameOffset = sizeof( EVENT_TRACE_PROPERTIES );
|
|
|
|
memcpy( ((char*)s_prop) + sizeof( EVENT_TRACE_PROPERTIES ), KERNEL_LOGGER_NAME, sizeof( KERNEL_LOGGER_NAME ) );
|
2019-08-12 21:05:17 +00:00
|
|
|
|
|
|
|
auto backup = tracy_malloc( psz );
|
2019-08-12 23:58:16 +00:00
|
|
|
memcpy( backup, s_prop, psz );
|
2019-08-12 21:05:17 +00:00
|
|
|
|
2019-08-12 23:58:16 +00:00
|
|
|
const auto controlStatus = ControlTrace( 0, KERNEL_LOGGER_NAME, s_prop, EVENT_TRACE_CONTROL_STOP );
|
2019-08-12 21:05:17 +00:00
|
|
|
if( controlStatus != ERROR_SUCCESS && controlStatus != ERROR_WMI_INSTANCE_NOT_FOUND )
|
|
|
|
{
|
2020-06-27 15:43:08 +00:00
|
|
|
tracy_free( backup );
|
2019-08-12 23:58:16 +00:00
|
|
|
tracy_free( s_prop );
|
2019-08-12 21:05:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-12 23:58:16 +00:00
|
|
|
memcpy( s_prop, backup, psz );
|
2019-08-12 21:05:17 +00:00
|
|
|
tracy_free( backup );
|
|
|
|
|
2019-08-12 23:58:16 +00:00
|
|
|
const auto startStatus = StartTrace( &s_traceHandle, KERNEL_LOGGER_NAME, s_prop );
|
|
|
|
if( startStatus != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
tracy_free( s_prop );
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-12 21:05:17 +00:00
|
|
|
|
2020-02-22 13:16:04 +00:00
|
|
|
if( isOs64Bit )
|
2020-02-22 12:13:49 +00:00
|
|
|
{
|
2020-02-22 13:16:04 +00:00
|
|
|
CLASSIC_EVENT_ID stackId;
|
|
|
|
stackId.EventGuid = PerfInfoGuid;
|
|
|
|
stackId.Type = 46;
|
|
|
|
const auto stackStatus = TraceSetInformation( s_traceHandle, TraceStackTracingInfo, &stackId, sizeof( stackId ) );
|
|
|
|
if( stackStatus != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
tracy_free( s_prop );
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-22 12:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 11:09:27 +00:00
|
|
|
#ifdef UNICODE
|
|
|
|
WCHAR KernelLoggerName[sizeof( KERNEL_LOGGER_NAME )];
|
|
|
|
#else
|
2019-08-16 11:03:20 +00:00
|
|
|
char KernelLoggerName[sizeof( KERNEL_LOGGER_NAME )];
|
2019-08-16 11:09:27 +00:00
|
|
|
#endif
|
2019-08-16 11:03:20 +00:00
|
|
|
memcpy( KernelLoggerName, KERNEL_LOGGER_NAME, sizeof( KERNEL_LOGGER_NAME ) );
|
2019-08-12 21:05:17 +00:00
|
|
|
EVENT_TRACE_LOGFILE log = {};
|
2019-08-16 11:03:20 +00:00
|
|
|
log.LoggerName = KernelLoggerName;
|
2019-08-12 21:05:17 +00:00
|
|
|
log.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD | PROCESS_TRACE_MODE_RAW_TIMESTAMP;
|
|
|
|
log.EventRecordCallback = EventRecordCallback;
|
|
|
|
|
|
|
|
s_traceHandle2 = OpenTrace( &log );
|
|
|
|
if( s_traceHandle2 == (TRACEHANDLE)INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
CloseTrace( s_traceHandle );
|
2019-08-12 23:58:16 +00:00
|
|
|
tracy_free( s_prop );
|
2019-08-12 21:05:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:44:28 +00:00
|
|
|
#ifndef TRACY_NO_VSYNC_CAPTURE
|
2020-06-27 17:56:23 +00:00
|
|
|
SetupVsync();
|
2020-11-05 22:44:28 +00:00
|
|
|
#endif
|
2020-06-27 17:56:23 +00:00
|
|
|
|
2019-08-12 18:08:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysTraceStop()
|
|
|
|
{
|
2020-06-27 17:56:23 +00:00
|
|
|
if( s_threadVsync )
|
|
|
|
{
|
|
|
|
CloseTrace( s_traceHandleVsync2 );
|
|
|
|
CloseTrace( s_traceHandleVsync );
|
|
|
|
s_threadVsync->~Thread();
|
|
|
|
tracy_free( s_threadVsync );
|
|
|
|
}
|
|
|
|
|
2019-08-12 21:05:17 +00:00
|
|
|
CloseTrace( s_traceHandle2 );
|
|
|
|
CloseTrace( s_traceHandle );
|
2019-08-12 18:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SysTraceWorker( void* ptr )
|
|
|
|
{
|
2020-07-19 01:24:49 +00:00
|
|
|
ThreadExitHandler threadExitHandler;
|
2020-08-05 13:34:39 +00:00
|
|
|
SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );
|
2019-08-20 14:22:54 +00:00
|
|
|
SetThreadName( "Tracy SysTrace" );
|
2019-08-12 21:05:17 +00:00
|
|
|
ProcessTrace( &s_traceHandle2, 1, 0, 0 );
|
2019-08-12 23:58:16 +00:00
|
|
|
ControlTrace( 0, KERNEL_LOGGER_NAME, s_prop, EVENT_TRACE_CONTROL_STOP );
|
|
|
|
tracy_free( s_prop );
|
2019-08-12 18:08:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:22:23 +00:00
|
|
|
void SysTraceSendExternalName( uint64_t thread )
|
|
|
|
{
|
2019-08-16 17:49:16 +00:00
|
|
|
bool threadSent = false;
|
2019-08-24 22:33:22 +00:00
|
|
|
auto hnd = OpenThread( THREAD_QUERY_INFORMATION, FALSE, DWORD( thread ) );
|
|
|
|
if( hnd == 0 )
|
|
|
|
{
|
|
|
|
hnd = OpenThread( THREAD_QUERY_LIMITED_INFORMATION, FALSE, DWORD( thread ) );
|
|
|
|
}
|
2019-08-17 01:44:11 +00:00
|
|
|
if( hnd != 0 )
|
2019-08-16 17:22:23 +00:00
|
|
|
{
|
2021-05-04 14:13:42 +00:00
|
|
|
if( _GetThreadDescription )
|
2019-08-16 17:49:16 +00:00
|
|
|
{
|
2021-05-04 14:13:42 +00:00
|
|
|
PWSTR tmp;
|
|
|
|
_GetThreadDescription( hnd, &tmp );
|
|
|
|
char buf[256];
|
|
|
|
if( tmp )
|
2019-08-16 17:49:16 +00:00
|
|
|
{
|
2021-05-04 14:13:42 +00:00
|
|
|
auto ret = wcstombs( buf, tmp, 256 );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
GetProfiler().SendString( thread, buf, ret, QueueType::ExternalThreadName );
|
|
|
|
threadSent = true;
|
|
|
|
}
|
2019-08-16 17:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-17 18:22:06 +00:00
|
|
|
const auto pid = GetProcessIdOfThread( hnd );
|
2019-08-19 10:47:27 +00:00
|
|
|
if( !threadSent && NtQueryInformationThread && _EnumProcessModules && _GetModuleInformation && _GetModuleBaseNameA )
|
2019-08-17 18:22:06 +00:00
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
ULONG retlen;
|
|
|
|
auto status = NtQueryInformationThread( hnd, (THREADINFOCLASS)9 /*ThreadQuerySetWin32StartAddress*/, &ptr, sizeof( &ptr ), &retlen );
|
|
|
|
if( status == 0 )
|
|
|
|
{
|
|
|
|
const auto phnd = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid );
|
|
|
|
if( phnd != INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
HMODULE modules[1024];
|
|
|
|
DWORD needed;
|
|
|
|
if( _EnumProcessModules( phnd, modules, 1024 * sizeof( HMODULE ), &needed ) != 0 )
|
|
|
|
{
|
|
|
|
const auto sz = std::min( DWORD( needed / sizeof( HMODULE ) ), DWORD( 1024 ) );
|
2019-10-10 18:30:08 +00:00
|
|
|
for( DWORD i=0; i<sz; i++ )
|
2019-08-17 18:22:06 +00:00
|
|
|
{
|
|
|
|
MODULEINFO info;
|
|
|
|
if( _GetModuleInformation( phnd, modules[i], &info, sizeof( info ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( (uint64_t)ptr >= (uint64_t)info.lpBaseOfDll && (uint64_t)ptr <= (uint64_t)info.lpBaseOfDll + (uint64_t)info.SizeOfImage )
|
|
|
|
{
|
2020-05-02 21:51:52 +00:00
|
|
|
char buf2[1024];
|
2020-07-21 15:21:09 +00:00
|
|
|
const auto modlen = _GetModuleBaseNameA( phnd, modules[i], buf2, 1024 );
|
|
|
|
if( modlen != 0 )
|
2019-08-17 18:22:06 +00:00
|
|
|
{
|
2020-07-21 15:21:09 +00:00
|
|
|
GetProfiler().SendString( thread, buf2, modlen, QueueType::ExternalThreadName );
|
2019-08-17 18:22:06 +00:00
|
|
|
threadSent = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle( phnd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle( hnd );
|
2019-08-17 01:44:47 +00:00
|
|
|
if( !threadSent )
|
|
|
|
{
|
2020-07-21 15:21:09 +00:00
|
|
|
GetProfiler().SendString( thread, "???", 3, QueueType::ExternalThreadName );
|
2019-10-04 17:18:47 +00:00
|
|
|
threadSent = true;
|
2019-08-17 01:44:47 +00:00
|
|
|
}
|
2019-08-16 17:22:23 +00:00
|
|
|
if( pid != 0 )
|
|
|
|
{
|
2019-08-17 20:32:41 +00:00
|
|
|
{
|
|
|
|
uint64_t _pid = pid;
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::TidToPid );
|
2019-08-17 20:32:41 +00:00
|
|
|
MemWrite( &item->tidToPid.tid, thread );
|
|
|
|
MemWrite( &item->tidToPid.pid, _pid );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2019-08-17 20:32:41 +00:00
|
|
|
}
|
2019-08-17 01:44:47 +00:00
|
|
|
if( pid == 4 )
|
|
|
|
{
|
2020-07-21 15:21:09 +00:00
|
|
|
GetProfiler().SendString( thread, "System", 6, QueueType::ExternalName );
|
2019-08-17 01:44:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
2019-08-16 17:22:23 +00:00
|
|
|
{
|
2019-08-17 01:44:47 +00:00
|
|
|
const auto phnd = OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid );
|
|
|
|
if( phnd != INVALID_HANDLE_VALUE )
|
2019-08-16 17:22:23 +00:00
|
|
|
{
|
2020-05-02 21:51:52 +00:00
|
|
|
char buf2[1024];
|
|
|
|
const auto sz = GetProcessImageFileNameA( phnd, buf2, 1024 );
|
2019-08-17 01:44:47 +00:00
|
|
|
CloseHandle( phnd );
|
|
|
|
if( sz != 0 )
|
2019-08-16 17:49:16 +00:00
|
|
|
{
|
2020-05-02 21:51:52 +00:00
|
|
|
auto ptr = buf2 + sz - 1;
|
|
|
|
while( ptr > buf2 && *ptr != '\\' ) ptr--;
|
2019-08-17 01:44:47 +00:00
|
|
|
if( *ptr == '\\' ) ptr++;
|
|
|
|
GetProfiler().SendString( thread, ptr, QueueType::ExternalName );
|
|
|
|
return;
|
2019-08-16 17:49:16 +00:00
|
|
|
}
|
2019-08-16 17:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 01:44:47 +00:00
|
|
|
if( !threadSent )
|
|
|
|
{
|
2020-07-21 15:21:09 +00:00
|
|
|
GetProfiler().SendString( thread, "???", 3, QueueType::ExternalThreadName );
|
2019-08-17 01:44:47 +00:00
|
|
|
}
|
2020-07-21 15:21:09 +00:00
|
|
|
GetProfiler().SendString( thread, "???", 3, QueueType::ExternalName );
|
2019-08-16 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 18:08:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 11:56:15 +00:00
|
|
|
# elif defined __linux__
|
|
|
|
|
2019-08-14 13:09:33 +00:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/stat.h>
|
2019-09-27 13:51:29 +00:00
|
|
|
# include <sys/wait.h>
|
2019-08-14 13:09:33 +00:00
|
|
|
# include <fcntl.h>
|
2019-08-16 19:00:42 +00:00
|
|
|
# include <inttypes.h>
|
2019-08-23 23:06:21 +00:00
|
|
|
# include <limits>
|
|
|
|
# include <poll.h>
|
2019-08-14 13:09:33 +00:00
|
|
|
# include <stdio.h>
|
2019-08-20 10:18:46 +00:00
|
|
|
# include <stdlib.h>
|
2019-08-14 13:09:33 +00:00
|
|
|
# include <string.h>
|
|
|
|
# include <unistd.h>
|
2020-03-10 17:56:49 +00:00
|
|
|
# include <atomic>
|
2020-08-12 12:06:00 +00:00
|
|
|
# include <thread>
|
|
|
|
# include <linux/perf_event.h>
|
2020-09-15 15:38:35 +00:00
|
|
|
# include <linux/version.h>
|
2020-08-12 12:06:00 +00:00
|
|
|
# include <sys/mman.h>
|
|
|
|
# include <sys/ioctl.h>
|
2020-12-26 13:48:31 +00:00
|
|
|
# include <sys/syscall.h>
|
2019-08-14 13:09:33 +00:00
|
|
|
|
|
|
|
# include "TracyProfiler.hpp"
|
2020-08-12 12:06:00 +00:00
|
|
|
# include "TracyRingBuffer.hpp"
|
|
|
|
# include "TracyThread.hpp"
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-09-27 13:51:29 +00:00
|
|
|
# ifdef __ANDROID__
|
|
|
|
# include "TracySysTracePayload.hpp"
|
|
|
|
# endif
|
|
|
|
|
2019-08-14 11:56:15 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
static const char BasePath[] = "/sys/kernel/debug/tracing/";
|
|
|
|
static const char TracingOn[] = "tracing_on";
|
|
|
|
static const char CurrentTracer[] = "current_tracer";
|
|
|
|
static const char TraceOptions[] = "trace_options";
|
|
|
|
static const char TraceClock[] = "trace_clock";
|
|
|
|
static const char SchedSwitch[] = "events/sched/sched_switch/enable";
|
2019-08-17 15:17:54 +00:00
|
|
|
static const char SchedWakeup[] = "events/sched/sched_wakeup/enable";
|
2019-11-03 20:52:20 +00:00
|
|
|
static const char BufferSizeKb[] = "buffer_size_kb";
|
2019-08-14 11:56:15 +00:00
|
|
|
static const char TracePipe[] = "trace_pipe";
|
|
|
|
|
2020-03-10 17:56:49 +00:00
|
|
|
static std::atomic<bool> traceActive { false };
|
2020-08-12 12:06:00 +00:00
|
|
|
static Thread* s_threadSampling = nullptr;
|
|
|
|
static int s_numCpus = 0;
|
2021-05-18 23:29:21 +00:00
|
|
|
static int s_numBuffers = 0;
|
2020-08-15 00:43:18 +00:00
|
|
|
|
|
|
|
static constexpr size_t RingBufSize = 64*1024;
|
|
|
|
static RingBuffer<RingBufSize>* s_ring = nullptr;
|
2020-08-12 12:06:00 +00:00
|
|
|
|
|
|
|
static int perf_event_open( struct perf_event_attr* hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags )
|
|
|
|
{
|
|
|
|
return syscall( __NR_perf_event_open, hw_event, pid, cpu, group_fd, flags );
|
|
|
|
}
|
|
|
|
|
2021-05-18 23:25:13 +00:00
|
|
|
enum TraceEventId
|
|
|
|
{
|
2021-05-19 00:20:35 +00:00
|
|
|
EventCallstack,
|
|
|
|
EventCpuCycles,
|
|
|
|
EventInstructionsRetired
|
2021-05-18 23:25:13 +00:00
|
|
|
};
|
|
|
|
|
2020-08-12 12:06:00 +00:00
|
|
|
static void SetupSampling( int64_t& samplingPeriod )
|
|
|
|
{
|
2020-08-12 23:41:05 +00:00
|
|
|
#ifndef CLOCK_MONOTONIC_RAW
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2021-05-11 16:31:20 +00:00
|
|
|
samplingPeriod = GetSamplingPeriod();
|
2020-08-12 12:06:00 +00:00
|
|
|
|
|
|
|
s_numCpus = (int)std::thread::hardware_concurrency();
|
2021-05-19 00:20:35 +00:00
|
|
|
s_ring = (RingBuffer<RingBufSize>*)tracy_malloc( sizeof( RingBuffer<RingBufSize> ) * s_numCpus * 3 );
|
2021-05-18 23:29:21 +00:00
|
|
|
s_numBuffers = 0;
|
2020-08-12 12:06:00 +00:00
|
|
|
|
2021-05-19 00:20:35 +00:00
|
|
|
// Stack traces
|
2020-08-12 12:06:00 +00:00
|
|
|
perf_event_attr pe = {};
|
|
|
|
pe.type = PERF_TYPE_SOFTWARE;
|
|
|
|
pe.size = sizeof( perf_event_attr );
|
|
|
|
pe.config = PERF_COUNT_SW_CPU_CLOCK;
|
2021-05-11 16:31:20 +00:00
|
|
|
pe.sample_freq = GetSamplingFrequency();
|
2020-08-12 12:06:00 +00:00
|
|
|
pe.sample_type = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CALLCHAIN;
|
2020-09-15 15:38:35 +00:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION( 4, 8, 0 )
|
2020-08-12 12:06:00 +00:00
|
|
|
pe.sample_max_stack = 127;
|
2020-09-15 15:38:35 +00:00
|
|
|
#endif
|
2020-08-12 12:06:00 +00:00
|
|
|
pe.exclude_callchain_kernel = 1;
|
|
|
|
pe.disabled = 1;
|
|
|
|
pe.freq = 1;
|
2020-08-12 23:41:05 +00:00
|
|
|
#if !defined TRACY_HW_TIMER || !( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
2020-08-12 12:06:00 +00:00
|
|
|
pe.use_clockid = 1;
|
|
|
|
pe.clockid = CLOCK_MONOTONIC_RAW;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
|
|
|
{
|
2021-05-18 23:34:57 +00:00
|
|
|
const int fd = perf_event_open( &pe, -1, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2020-08-12 12:06:00 +00:00
|
|
|
if( fd == -1 )
|
|
|
|
{
|
2021-05-18 23:29:21 +00:00
|
|
|
for( int j=0; j<s_numBuffers; j++ ) s_ring[j].~RingBuffer<RingBufSize>();
|
2020-08-12 12:06:00 +00:00
|
|
|
tracy_free( s_ring );
|
|
|
|
return;
|
|
|
|
}
|
2021-05-18 23:29:21 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer<RingBufSize>( fd, EventCallstack );
|
|
|
|
s_numBuffers++;
|
2020-08-12 12:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 00:20:35 +00:00
|
|
|
// CPU cycles
|
|
|
|
pe = {};
|
|
|
|
pe.type = PERF_TYPE_HARDWARE;
|
|
|
|
pe.size = sizeof( perf_event_attr );
|
|
|
|
pe.config = PERF_COUNT_HW_CPU_CYCLES;
|
|
|
|
pe.sample_freq = 25*1000*1000;
|
|
|
|
pe.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME;
|
|
|
|
pe.disabled = 1;
|
|
|
|
pe.exclude_kernel = 1;
|
|
|
|
pe.exclude_idle = 1;
|
|
|
|
pe.precise_ip = 2;
|
|
|
|
#if !defined TRACY_HW_TIMER || !( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
|
|
|
pe.use_clockid = 1;
|
|
|
|
pe.clockid = CLOCK_MONOTONIC_RAW;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
|
|
|
{
|
|
|
|
const int fd = perf_event_open( &pe, -1, i, -1, PERF_FLAG_FD_CLOEXEC );
|
|
|
|
if( fd != -1 )
|
|
|
|
{
|
|
|
|
new( s_ring+s_numBuffers ) RingBuffer<RingBufSize>( fd, EventCpuCycles );
|
|
|
|
s_numBuffers++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instructions retired
|
|
|
|
pe.config = PERF_COUNT_HW_INSTRUCTIONS;
|
|
|
|
|
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
|
|
|
{
|
|
|
|
const int fd = perf_event_open( &pe, -1, i, -1, PERF_FLAG_FD_CLOEXEC );
|
|
|
|
if( fd != -1 )
|
|
|
|
{
|
|
|
|
new( s_ring+s_numBuffers ) RingBuffer<RingBufSize>( fd, EventInstructionsRetired );
|
|
|
|
s_numBuffers++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-12 12:06:00 +00:00
|
|
|
s_threadSampling = (Thread*)tracy_malloc( sizeof( Thread ) );
|
|
|
|
new(s_threadSampling) Thread( [] (void*) {
|
|
|
|
ThreadExitHandler threadExitHandler;
|
|
|
|
SetThreadName( "Tracy Sampling" );
|
|
|
|
sched_param sp = { 5 };
|
|
|
|
pthread_setschedparam( pthread_self(), SCHED_FIFO, &sp );
|
|
|
|
uint32_t currentPid = (uint32_t)getpid();
|
2020-08-12 23:41:05 +00:00
|
|
|
#if defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
2021-05-18 23:29:21 +00:00
|
|
|
for( int i=0; i<s_numBuffers; i++ )
|
2020-08-12 23:41:05 +00:00
|
|
|
{
|
|
|
|
if( !s_ring[i].CheckTscCaps() )
|
|
|
|
{
|
2021-05-18 23:29:21 +00:00
|
|
|
for( int j=0; j<s_numBuffers; j++ ) s_ring[j].~RingBuffer<RingBufSize>();
|
2020-08-12 23:41:05 +00:00
|
|
|
tracy_free( s_ring );
|
|
|
|
const char* err = "Tracy Profiler: sampling is disabled due to non-native scheduler clock. Are you running under a VM?";
|
|
|
|
Profiler::MessageAppInfo( err, strlen( err ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-05-18 23:29:21 +00:00
|
|
|
for( int i=0; i<s_numBuffers; i++ ) s_ring[i].Enable();
|
2020-08-12 12:06:00 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
bool hadData = false;
|
2021-05-18 23:29:21 +00:00
|
|
|
for( int i=0; i<s_numBuffers; i++ )
|
2020-08-12 12:06:00 +00:00
|
|
|
{
|
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
|
|
|
if( !s_ring[i].HasData() ) continue;
|
|
|
|
hadData = true;
|
|
|
|
|
|
|
|
perf_event_header hdr;
|
|
|
|
s_ring[i].Read( &hdr, 0, sizeof( perf_event_header ) );
|
|
|
|
if( hdr.type == PERF_RECORD_SAMPLE )
|
|
|
|
{
|
|
|
|
auto offset = sizeof( perf_event_header );
|
2021-05-19 00:20:35 +00:00
|
|
|
const auto id = s_ring[i].GetId();
|
|
|
|
if( id == EventCallstack )
|
2020-08-12 12:06:00 +00:00
|
|
|
{
|
2021-05-19 00:20:35 +00:00
|
|
|
// Layout:
|
|
|
|
// u32 pid, tid
|
|
|
|
// u64 time
|
|
|
|
// u64 cnt
|
|
|
|
// u64 ip[cnt]
|
|
|
|
|
|
|
|
uint32_t pid;
|
|
|
|
s_ring[i].Read( &pid, offset, sizeof( uint32_t ) );
|
|
|
|
if( pid == currentPid )
|
2020-10-06 16:27:14 +00:00
|
|
|
{
|
2021-05-19 00:20:35 +00:00
|
|
|
uint32_t tid;
|
|
|
|
uint64_t t0;
|
|
|
|
uint64_t cnt;
|
|
|
|
|
|
|
|
offset += sizeof( uint32_t );
|
|
|
|
s_ring[i].Read( &tid, offset, sizeof( uint32_t ) );
|
|
|
|
offset += sizeof( uint32_t );
|
|
|
|
s_ring[i].Read( &t0, offset, sizeof( uint64_t ) );
|
|
|
|
offset += sizeof( uint64_t );
|
|
|
|
s_ring[i].Read( &cnt, offset, sizeof( uint64_t ) );
|
|
|
|
offset += sizeof( uint64_t );
|
|
|
|
|
|
|
|
if( cnt > 0 )
|
|
|
|
{
|
|
|
|
auto trace = (uint64_t*)tracy_malloc( ( 1 + cnt ) * sizeof( uint64_t ) );
|
|
|
|
s_ring[i].Read( trace+1, offset, sizeof( uint64_t ) * cnt );
|
2020-10-02 20:14:33 +00:00
|
|
|
|
2021-04-29 14:38:12 +00:00
|
|
|
#if defined __x86_64__ || defined _M_X64
|
2021-05-19 00:20:35 +00:00
|
|
|
// remove non-canonical pointers
|
|
|
|
do
|
|
|
|
{
|
|
|
|
const auto test = (int64_t)trace[cnt];
|
|
|
|
const auto m1 = test >> 63;
|
|
|
|
const auto m2 = test >> 47;
|
|
|
|
if( m1 == m2 ) break;
|
|
|
|
}
|
|
|
|
while( --cnt > 0 );
|
|
|
|
for( uint64_t j=1; j<cnt; j++ )
|
|
|
|
{
|
|
|
|
const auto test = (int64_t)trace[j];
|
|
|
|
const auto m1 = test >> 63;
|
|
|
|
const auto m2 = test >> 47;
|
|
|
|
if( m1 != m2 ) trace[j] = 0;
|
|
|
|
}
|
2021-04-29 14:38:12 +00:00
|
|
|
#endif
|
2021-04-29 14:10:59 +00:00
|
|
|
|
2021-05-19 00:20:35 +00:00
|
|
|
// skip kernel frames
|
|
|
|
uint64_t j;
|
|
|
|
for( j=0; j<cnt; j++ )
|
2021-04-29 14:10:59 +00:00
|
|
|
{
|
2021-05-19 00:20:35 +00:00
|
|
|
if( (int64_t)trace[j+1] >= 0 ) break;
|
2021-04-29 14:10:59 +00:00
|
|
|
}
|
2021-05-19 00:20:35 +00:00
|
|
|
if( j == cnt )
|
|
|
|
{
|
|
|
|
tracy_free( trace );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( j > 0 )
|
|
|
|
{
|
|
|
|
cnt -= j;
|
|
|
|
memmove( trace+1, trace+1+j, sizeof( uint64_t ) * cnt );
|
|
|
|
}
|
|
|
|
memcpy( trace, &cnt, sizeof( uint64_t ) );
|
|
|
|
|
|
|
|
#if defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
|
|
|
t0 = s_ring[i].ConvertTimeToTsc( t0 );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TracyLfqPrepare( QueueType::CallstackSample );
|
|
|
|
MemWrite( &item->callstackSampleFat.time, t0 );
|
|
|
|
MemWrite( &item->callstackSampleFat.thread, (uint64_t)tid );
|
|
|
|
MemWrite( &item->callstackSampleFat.ptr, (uint64_t)trace );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Layout:
|
|
|
|
// u64 ip
|
|
|
|
// u32 pid, tid
|
|
|
|
// u64 time
|
|
|
|
|
|
|
|
uint32_t pid;
|
|
|
|
s_ring[i].Read( &pid, offset + sizeof( uint64_t ), sizeof( uint32_t ) );
|
|
|
|
if( pid == currentPid )
|
|
|
|
{
|
|
|
|
uint64_t ip, t0;
|
|
|
|
uint32_t tid;
|
|
|
|
|
|
|
|
s_ring[i].Read( &ip, offset, sizeof( uint64_t ) );
|
|
|
|
offset += sizeof( uint64_t ) + sizeof( uint32_t );
|
|
|
|
s_ring[i].Read( &tid, offset, sizeof( uint32_t ) );
|
|
|
|
offset += sizeof( uint32_t );
|
|
|
|
s_ring[i].Read( &t0, offset, sizeof( uint64_t ) );
|
2020-08-12 12:06:00 +00:00
|
|
|
|
2020-08-12 23:41:05 +00:00
|
|
|
#if defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
2021-05-19 00:20:35 +00:00
|
|
|
t0 = s_ring[i].ConvertTimeToTsc( t0 );
|
2020-08-12 23:41:05 +00:00
|
|
|
#endif
|
|
|
|
|
2021-05-19 00:20:35 +00:00
|
|
|
QueueType type;
|
|
|
|
switch( id )
|
|
|
|
{
|
|
|
|
case EventCpuCycles:
|
|
|
|
type = QueueType::HwSampleCpuCycle;
|
|
|
|
break;
|
|
|
|
case EventInstructionsRetired:
|
|
|
|
type = QueueType::HwSampleInstructionRetired;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
2021-04-29 14:10:59 +00:00
|
|
|
}
|
2021-05-19 00:20:35 +00:00
|
|
|
|
|
|
|
TracyLfqPrepare( type );
|
|
|
|
MemWrite( &item->hwSample.ip, ip );
|
|
|
|
MemWrite( &item->hwSample.thread, (uint64_t)tid );
|
|
|
|
MemWrite( &item->hwSample.time, t0 );
|
|
|
|
TracyLfqCommit;
|
2020-08-12 12:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s_ring[i].Advance( hdr.size );
|
|
|
|
}
|
2020-11-26 19:33:54 +00:00
|
|
|
if( !traceActive.load( std::memory_order_relaxed) ) break;
|
2020-08-12 12:06:00 +00:00
|
|
|
if( !hadData )
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 23:29:21 +00:00
|
|
|
for( int i=0; i<s_numBuffers; i++ ) s_ring[i].~RingBuffer<RingBufSize>();
|
2020-08-12 12:06:00 +00:00
|
|
|
tracy_free( s_ring );
|
|
|
|
}, nullptr );
|
|
|
|
}
|
2020-03-10 17:56:49 +00:00
|
|
|
|
2019-08-20 10:18:46 +00:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
static bool TraceWrite( const char* path, size_t psz, const char* val, size_t vsz )
|
|
|
|
{
|
2020-11-17 20:11:48 +00:00
|
|
|
// Explanation for "su root sh -c": there are 2 flavors of "su" in circulation
|
|
|
|
// on Android. The default Android su has the following syntax to run a command
|
|
|
|
// as root:
|
|
|
|
// su root 'command'
|
|
|
|
// and 'command' is exec'd not passed to a shell, so if shell interpretation is
|
|
|
|
// wanted, one needs to do:
|
|
|
|
// su root sh -c 'command'
|
|
|
|
// Besides that default Android 'su' command, some Android devices use a different
|
|
|
|
// su with a command-line interface closer to the familiar util-linux su found
|
|
|
|
// on Linux distributions. Fortunately, both the util-linux su and the one
|
|
|
|
// in https://github.com/topjohnwu/Magisk seem to be happy with the above
|
|
|
|
// `su root sh -c 'command'` command line syntax.
|
2019-08-20 10:18:46 +00:00
|
|
|
char tmp[256];
|
2020-11-17 20:11:48 +00:00
|
|
|
sprintf( tmp, "su root sh -c 'echo \"%s\" > %s%s'", val, BasePath, path );
|
2019-08-20 10:18:46 +00:00
|
|
|
return system( tmp ) == 0;
|
|
|
|
}
|
|
|
|
#else
|
2019-08-14 11:56:15 +00:00
|
|
|
static bool TraceWrite( const char* path, size_t psz, const char* val, size_t vsz )
|
|
|
|
{
|
|
|
|
char tmp[256];
|
|
|
|
memcpy( tmp, BasePath, sizeof( BasePath ) - 1 );
|
|
|
|
memcpy( tmp + sizeof( BasePath ) - 1, path, psz );
|
|
|
|
|
|
|
|
int fd = open( tmp, O_WRONLY );
|
|
|
|
if( fd < 0 ) return false;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
ssize_t cnt = write( fd, val, vsz );
|
|
|
|
if( cnt == (ssize_t)vsz )
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if( cnt < 0 )
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
vsz -= cnt;
|
|
|
|
val += cnt;
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 10:18:46 +00:00
|
|
|
#endif
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-09-27 13:51:29 +00:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
void SysTraceInjectPayload()
|
|
|
|
{
|
|
|
|
int pipefd[2];
|
|
|
|
if( pipe( pipefd ) == 0 )
|
|
|
|
{
|
|
|
|
const auto pid = fork();
|
|
|
|
if( pid == 0 )
|
|
|
|
{
|
|
|
|
// child
|
|
|
|
close( pipefd[1] );
|
|
|
|
if( dup2( pipefd[0], STDIN_FILENO ) >= 0 )
|
|
|
|
{
|
|
|
|
close( pipefd[0] );
|
2020-11-17 20:11:48 +00:00
|
|
|
execlp( "su", "su", "root", "sh", "-c", "cat > /data/tracy_systrace", (char*)nullptr );
|
2019-09-27 13:51:29 +00:00
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( pid > 0 )
|
|
|
|
{
|
|
|
|
// parent
|
|
|
|
close( pipefd[0] );
|
|
|
|
|
|
|
|
#ifdef __aarch64__
|
|
|
|
write( pipefd[1], tracy_systrace_aarch64_data, tracy_systrace_aarch64_size );
|
|
|
|
#else
|
|
|
|
write( pipefd[1], tracy_systrace_armv7_data, tracy_systrace_armv7_size );
|
|
|
|
#endif
|
|
|
|
close( pipefd[1] );
|
|
|
|
waitpid( pid, nullptr, 0 );
|
|
|
|
|
2020-11-17 20:11:48 +00:00
|
|
|
system( "su root sh -c 'chmod 700 /data/tracy_systrace'" );
|
2019-09-27 13:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-25 22:08:52 +00:00
|
|
|
bool SysTraceStart( int64_t& samplingPeriod )
|
2019-08-14 11:56:15 +00:00
|
|
|
{
|
2020-08-12 12:06:00 +00:00
|
|
|
#ifndef CLOCK_MONOTONIC_RAW
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2019-08-14 11:56:15 +00:00
|
|
|
if( !TraceWrite( TracingOn, sizeof( TracingOn ), "0", 2 ) ) return false;
|
|
|
|
if( !TraceWrite( CurrentTracer, sizeof( CurrentTracer ), "nop", 4 ) ) return false;
|
2019-08-20 10:19:05 +00:00
|
|
|
TraceWrite( TraceOptions, sizeof( TraceOptions ), "norecord-cmd", 13 );
|
|
|
|
TraceWrite( TraceOptions, sizeof( TraceOptions ), "norecord-tgid", 14 );
|
|
|
|
TraceWrite( TraceOptions, sizeof( TraceOptions ), "noirq-info", 11 );
|
2020-02-23 17:20:48 +00:00
|
|
|
TraceWrite( TraceOptions, sizeof( TraceOptions ), "noannotate", 11 );
|
2020-08-12 23:41:05 +00:00
|
|
|
#if defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
|
|
|
if( !TraceWrite( TraceClock, sizeof( TraceClock ), "x86-tsc", 8 ) ) return false;
|
|
|
|
#else
|
2019-11-03 20:51:49 +00:00
|
|
|
if( !TraceWrite( TraceClock, sizeof( TraceClock ), "mono_raw", 9 ) ) return false;
|
2020-08-12 23:41:05 +00:00
|
|
|
#endif
|
2019-08-14 11:56:15 +00:00
|
|
|
if( !TraceWrite( SchedSwitch, sizeof( SchedSwitch ), "1", 2 ) ) return false;
|
2019-08-17 15:17:54 +00:00
|
|
|
if( !TraceWrite( SchedWakeup, sizeof( SchedWakeup ), "1", 2 ) ) return false;
|
2020-08-11 23:19:10 +00:00
|
|
|
if( !TraceWrite( BufferSizeKb, sizeof( BufferSizeKb ), "4096", 5 ) ) return false;
|
2019-09-27 13:51:29 +00:00
|
|
|
|
|
|
|
#if defined __ANDROID__ && ( defined __aarch64__ || defined __ARM_ARCH )
|
|
|
|
SysTraceInjectPayload();
|
|
|
|
#endif
|
|
|
|
|
2019-08-14 11:56:15 +00:00
|
|
|
if( !TraceWrite( TracingOn, sizeof( TracingOn ), "1", 2 ) ) return false;
|
2020-03-10 17:56:49 +00:00
|
|
|
traceActive.store( true, std::memory_order_relaxed );
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2020-08-12 12:06:00 +00:00
|
|
|
SetupSampling( samplingPeriod );
|
|
|
|
|
2019-08-14 11:56:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysTraceStop()
|
|
|
|
{
|
|
|
|
TraceWrite( TracingOn, sizeof( TracingOn ), "0", 2 );
|
2020-03-10 17:56:49 +00:00
|
|
|
traceActive.store( false, std::memory_order_relaxed );
|
2020-08-12 12:06:00 +00:00
|
|
|
if( s_threadSampling )
|
|
|
|
{
|
|
|
|
s_threadSampling->~Thread();
|
|
|
|
tracy_free( s_threadSampling );
|
|
|
|
}
|
2019-08-14 11:56:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 19:36:09 +00:00
|
|
|
static uint64_t ReadNumber( const char*& data )
|
2019-08-14 11:56:15 +00:00
|
|
|
{
|
2020-08-18 19:36:09 +00:00
|
|
|
auto ptr = data;
|
2020-08-18 18:07:15 +00:00
|
|
|
assert( *ptr >= '0' && *ptr <= '9' );
|
|
|
|
uint64_t val = *ptr++ - '0';
|
2019-08-14 11:56:15 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
2020-08-18 18:07:15 +00:00
|
|
|
const uint8_t v = uint8_t( *ptr - '0' );
|
|
|
|
if( v > 9 ) break;
|
|
|
|
val = val * 10 + v;
|
|
|
|
ptr++;
|
2019-08-14 11:56:15 +00:00
|
|
|
}
|
2020-08-18 19:36:09 +00:00
|
|
|
data = ptr;
|
2020-08-18 18:07:15 +00:00
|
|
|
return val;
|
2019-08-14 11:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t ReadState( char state )
|
|
|
|
{
|
|
|
|
switch( state )
|
|
|
|
{
|
|
|
|
case 'D': return 101;
|
|
|
|
case 'I': return 102;
|
|
|
|
case 'R': return 103;
|
|
|
|
case 'S': return 104;
|
|
|
|
case 'T': return 105;
|
|
|
|
case 't': return 106;
|
|
|
|
case 'W': return 107;
|
|
|
|
case 'X': return 108;
|
|
|
|
case 'Z': return 109;
|
|
|
|
default: return 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 13:26:09 +00:00
|
|
|
#if defined __ANDROID__ && defined __ANDROID_API__ && __ANDROID_API__ < 18
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ssize_t getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
|
|
|
|
{
|
|
|
|
char *ptr, *eptr;
|
|
|
|
|
|
|
|
if (*buf == NULL || *bufsiz == 0) {
|
|
|
|
*bufsiz = BUFSIZ;
|
|
|
|
if ((*buf = (char*)malloc(*bufsiz)) == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ptr = *buf, eptr = *buf + *bufsiz;;) {
|
|
|
|
int c = fgetc(fp);
|
|
|
|
if (c == -1) {
|
|
|
|
if (feof(fp))
|
|
|
|
return ptr == *buf ? -1 : ptr - *buf;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*ptr++ = c;
|
|
|
|
if (c == delimiter) {
|
|
|
|
*ptr = '\0';
|
|
|
|
return ptr - *buf;
|
|
|
|
}
|
|
|
|
if (ptr + 2 >= eptr) {
|
|
|
|
char *nbuf;
|
|
|
|
size_t nbufsiz = *bufsiz * 2;
|
|
|
|
ssize_t d = ptr - *buf;
|
|
|
|
if ((nbuf = (char*)realloc(*buf, nbufsiz)) == NULL)
|
|
|
|
return -1;
|
|
|
|
*buf = nbuf;
|
|
|
|
*bufsiz = nbufsiz;
|
|
|
|
eptr = nbuf + nbufsiz;
|
|
|
|
ptr = nbuf + d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t getline(char **buf, size_t *bufsiz, FILE *fp)
|
|
|
|
{
|
|
|
|
return getdelim(buf, bufsiz, '\n', fp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
static void HandleTraceLine( const char* line )
|
2019-08-14 11:56:15 +00:00
|
|
|
{
|
2020-02-23 17:23:53 +00:00
|
|
|
line += 23;
|
|
|
|
while( *line != '[' ) line++;
|
|
|
|
line++;
|
2019-08-23 22:42:00 +00:00
|
|
|
const auto cpu = (uint8_t)ReadNumber( line );
|
|
|
|
line++; // ']'
|
|
|
|
while( *line == ' ' ) line++;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2020-08-12 23:41:05 +00:00
|
|
|
#if defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
|
|
|
const auto time = ReadNumber( line );
|
|
|
|
#else
|
2019-08-23 22:42:00 +00:00
|
|
|
const auto ts = ReadNumber( line );
|
|
|
|
line++; // '.'
|
|
|
|
const auto tus = ReadNumber( line );
|
|
|
|
const auto time = ts * 1000000000ll + tus * 1000ll;
|
2020-08-12 23:41:05 +00:00
|
|
|
#endif
|
2019-08-14 13:09:33 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
line += 2; // ': '
|
|
|
|
if( memcmp( line, "sched_switch", 12 ) == 0 )
|
|
|
|
{
|
|
|
|
line += 14;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
while( memcmp( line, "prev_pid", 8 ) != 0 ) line++;
|
|
|
|
line += 9;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
const auto oldPid = ReadNumber( line );
|
|
|
|
line++;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
while( memcmp( line, "prev_state", 10 ) != 0 ) line++;
|
|
|
|
line += 11;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
const auto oldState = (uint8_t)ReadState( *line );
|
|
|
|
line += 5;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
while( memcmp( line, "next_pid", 8 ) != 0 ) line++;
|
|
|
|
line += 9;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
const auto newPid = ReadNumber( line );
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
uint8_t reason = 100;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::ContextSwitch );
|
2019-08-23 22:42:00 +00:00
|
|
|
MemWrite( &item->contextSwitch.time, time );
|
|
|
|
MemWrite( &item->contextSwitch.oldThread, oldPid );
|
|
|
|
MemWrite( &item->contextSwitch.newThread, newPid );
|
|
|
|
MemWrite( &item->contextSwitch.cpu, cpu );
|
|
|
|
MemWrite( &item->contextSwitch.reason, reason );
|
|
|
|
MemWrite( &item->contextSwitch.state, oldState );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
else if( memcmp( line, "sched_wakeup", 12 ) == 0 )
|
|
|
|
{
|
|
|
|
line += 14;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2020-11-19 10:37:05 +00:00
|
|
|
while( memcmp( line, "pid=", 4 ) != 0 ) line++;
|
2019-08-23 22:42:00 +00:00
|
|
|
line += 4;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
const auto pid = ReadNumber( line );
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::ThreadWakeup );
|
2019-08-23 22:42:00 +00:00
|
|
|
MemWrite( &item->threadWakeup.time, time );
|
|
|
|
MemWrite( &item->threadWakeup.thread, pid );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-17 15:17:54 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
static void ProcessTraceLines( int fd )
|
|
|
|
{
|
|
|
|
// Linux pipe buffer is 64KB, additional 1KB is for unfinished lines
|
|
|
|
char* buf = (char*)tracy_malloc( (64+1)*1024 );
|
|
|
|
char* line = buf;
|
2019-08-17 15:17:54 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
2020-03-10 17:56:49 +00:00
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
const auto rd = read( fd, line, 64*1024 );
|
|
|
|
if( rd <= 0 ) break;
|
2019-08-17 15:17:54 +00:00
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !GetProfiler().IsConnected() )
|
|
|
|
{
|
|
|
|
if( rd < 64*1024 )
|
|
|
|
{
|
|
|
|
assert( line[rd-1] == '\n' );
|
|
|
|
line = buf;
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto end = line + rd;
|
|
|
|
line = end - 1;
|
|
|
|
while( line > buf && *line != '\n' ) line--;
|
|
|
|
if( line > buf )
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
const auto lsz = end - line;
|
|
|
|
memmove( buf, line, lsz );
|
|
|
|
line = buf + lsz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const auto end = line + rd;
|
|
|
|
line = buf;
|
|
|
|
for(;;)
|
|
|
|
{
|
2020-08-18 17:51:02 +00:00
|
|
|
auto next = (char*)memchr( line, '\n', end - line );
|
|
|
|
if( !next )
|
2019-08-23 22:42:00 +00:00
|
|
|
{
|
|
|
|
const auto lsz = end - line;
|
|
|
|
memmove( buf, line, lsz );
|
|
|
|
line = buf + lsz;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
HandleTraceLine( line );
|
2020-08-18 17:51:02 +00:00
|
|
|
line = ++next;
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
if( rd < 64*1024 )
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
2019-08-17 15:17:54 +00:00
|
|
|
}
|
2019-08-14 11:56:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 22:42:00 +00:00
|
|
|
tracy_free( buf );
|
2019-08-20 13:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SysTraceWorker( void* ptr )
|
|
|
|
{
|
2020-07-19 01:24:49 +00:00
|
|
|
ThreadExitHandler threadExitHandler;
|
2019-08-20 14:23:00 +00:00
|
|
|
SetThreadName( "Tracy SysTrace" );
|
2019-08-20 13:49:40 +00:00
|
|
|
int pipefd[2];
|
|
|
|
if( pipe( pipefd ) == 0 )
|
|
|
|
{
|
|
|
|
const auto pid = fork();
|
|
|
|
if( pid == 0 )
|
|
|
|
{
|
|
|
|
// child
|
|
|
|
close( pipefd[0] );
|
2020-11-26 19:33:54 +00:00
|
|
|
dup2( open( "/dev/null", O_WRONLY ), STDERR_FILENO );
|
2019-08-20 13:49:40 +00:00
|
|
|
if( dup2( pipefd[1], STDOUT_FILENO ) >= 0 )
|
|
|
|
{
|
|
|
|
close( pipefd[1] );
|
2020-08-11 23:27:59 +00:00
|
|
|
sched_param sp = { 4 };
|
|
|
|
pthread_setschedparam( pthread_self(), SCHED_FIFO, &sp );
|
2019-09-27 13:51:29 +00:00
|
|
|
#if defined __ANDROID__ && ( defined __aarch64__ || defined __ARM_ARCH )
|
2020-11-17 20:11:48 +00:00
|
|
|
execlp( "su", "su", "root", "sh", "-c", "/data/tracy_systrace", (char*)nullptr );
|
2019-09-27 13:51:29 +00:00
|
|
|
#endif
|
2020-11-17 20:11:48 +00:00
|
|
|
execlp( "su", "su", "root", "sh", "-c", "cat /sys/kernel/debug/tracing/trace_pipe", (char*)nullptr );
|
2019-08-20 13:49:40 +00:00
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( pid > 0 )
|
|
|
|
{
|
|
|
|
// parent
|
|
|
|
close( pipefd[1] );
|
2020-08-11 23:27:59 +00:00
|
|
|
sched_param sp = { 5 };
|
|
|
|
pthread_setschedparam( pthread_self(), SCHED_FIFO, &sp );
|
2019-08-23 22:42:00 +00:00
|
|
|
ProcessTraceLines( pipefd[0] );
|
|
|
|
close( pipefd[0] );
|
2020-11-26 19:33:54 +00:00
|
|
|
waitpid( pid, nullptr, 0 );
|
2019-08-20 13:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2019-08-23 23:06:21 +00:00
|
|
|
static void ProcessTraceLines( int fd )
|
2019-08-23 22:42:00 +00:00
|
|
|
{
|
2019-08-23 23:06:21 +00:00
|
|
|
char* buf = (char*)tracy_malloc( 64*1024 );
|
|
|
|
|
|
|
|
struct pollfd pfd;
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = POLLIN | POLLERR;
|
2019-08-23 22:42:00 +00:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2020-03-10 17:56:49 +00:00
|
|
|
while( poll( &pfd, 1, 0 ) <= 0 )
|
|
|
|
{
|
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
}
|
2019-08-23 23:06:21 +00:00
|
|
|
|
|
|
|
const auto rd = read( fd, buf, 64*1024 );
|
|
|
|
if( rd <= 0 ) break;
|
2019-08-23 22:42:00 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !GetProfiler().IsConnected() ) continue;
|
|
|
|
#endif
|
|
|
|
|
2019-08-23 23:06:21 +00:00
|
|
|
auto line = buf;
|
|
|
|
const auto end = buf + rd;
|
|
|
|
for(;;)
|
|
|
|
{
|
2020-08-18 17:51:02 +00:00
|
|
|
auto next = (char*)memchr( line, '\n', end - line );
|
|
|
|
if( !next ) break;
|
2019-08-23 23:06:21 +00:00
|
|
|
HandleTraceLine( line );
|
2020-08-18 17:51:02 +00:00
|
|
|
line = ++next;
|
2019-08-23 23:06:21 +00:00
|
|
|
}
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 23:06:21 +00:00
|
|
|
tracy_free( buf );
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 13:49:40 +00:00
|
|
|
void SysTraceWorker( void* ptr )
|
|
|
|
{
|
2020-07-19 01:24:49 +00:00
|
|
|
ThreadExitHandler threadExitHandler;
|
2019-08-20 14:23:00 +00:00
|
|
|
SetThreadName( "Tracy SysTrace" );
|
2019-08-20 13:49:40 +00:00
|
|
|
char tmp[256];
|
|
|
|
memcpy( tmp, BasePath, sizeof( BasePath ) - 1 );
|
|
|
|
memcpy( tmp + sizeof( BasePath ) - 1, TracePipe, sizeof( TracePipe ) );
|
|
|
|
|
2019-08-23 23:06:21 +00:00
|
|
|
int fd = open( tmp, O_RDONLY );
|
|
|
|
if( fd < 0 ) return;
|
2020-08-11 23:27:59 +00:00
|
|
|
sched_param sp = { 5 };
|
|
|
|
pthread_setschedparam( pthread_self(), SCHED_FIFO, &sp );
|
2019-08-23 23:06:21 +00:00
|
|
|
ProcessTraceLines( fd );
|
|
|
|
close( fd );
|
2019-08-14 11:56:15 +00:00
|
|
|
}
|
2019-08-20 13:49:40 +00:00
|
|
|
#endif
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2019-08-16 17:22:23 +00:00
|
|
|
void SysTraceSendExternalName( uint64_t thread )
|
|
|
|
{
|
2019-08-16 19:00:42 +00:00
|
|
|
FILE* f;
|
|
|
|
char fn[256];
|
|
|
|
sprintf( fn, "/proc/%" PRIu64 "/comm", thread );
|
|
|
|
f = fopen( fn, "rb" );
|
|
|
|
if( f )
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
const auto sz = fread( buf, 1, 256, f );
|
|
|
|
if( sz > 0 && buf[sz-1] == '\n' ) buf[sz-1] = '\0';
|
|
|
|
GetProfiler().SendString( thread, buf, QueueType::ExternalThreadName );
|
|
|
|
fclose( f );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-21 15:21:09 +00:00
|
|
|
GetProfiler().SendString( thread, "???", 3, QueueType::ExternalThreadName );
|
2019-08-16 19:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( fn, "/proc/%" PRIu64 "/status", thread );
|
|
|
|
f = fopen( fn, "rb" );
|
|
|
|
if( f )
|
|
|
|
{
|
|
|
|
int pid = -1;
|
|
|
|
size_t lsz = 1024;
|
2020-08-11 23:30:22 +00:00
|
|
|
auto line = (char*)tracy_malloc( lsz );
|
2019-08-16 19:00:42 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
auto rd = getline( &line, &lsz, f );
|
|
|
|
if( rd <= 0 ) break;
|
|
|
|
if( memcmp( "Tgid:\t", line, 6 ) == 0 )
|
|
|
|
{
|
|
|
|
pid = atoi( line + 6 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 23:30:22 +00:00
|
|
|
tracy_free( line );
|
2019-08-16 19:00:42 +00:00
|
|
|
fclose( f );
|
|
|
|
if( pid >= 0 )
|
|
|
|
{
|
2019-08-17 20:32:41 +00:00
|
|
|
{
|
|
|
|
uint64_t _pid = pid;
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqPrepare( QueueType::TidToPid );
|
2019-08-17 20:32:41 +00:00
|
|
|
MemWrite( &item->tidToPid.tid, thread );
|
|
|
|
MemWrite( &item->tidToPid.pid, _pid );
|
2020-01-19 14:06:11 +00:00
|
|
|
TracyLfqCommit;
|
2019-08-17 20:32:41 +00:00
|
|
|
}
|
2019-08-16 19:00:42 +00:00
|
|
|
sprintf( fn, "/proc/%i/comm", pid );
|
|
|
|
f = fopen( fn, "rb" );
|
|
|
|
if( f )
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
const auto sz = fread( buf, 1, 256, f );
|
|
|
|
if( sz > 0 && buf[sz-1] == '\n' ) buf[sz-1] = '\0';
|
|
|
|
GetProfiler().SendString( thread, buf, QueueType::ExternalName );
|
|
|
|
fclose( f );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 15:21:09 +00:00
|
|
|
GetProfiler().SendString( thread, "???", 3, QueueType::ExternalName );
|
2019-08-16 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 11:56:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 21:05:17 +00:00
|
|
|
# endif
|
|
|
|
|
2019-08-12 18:08:15 +00:00
|
|
|
#endif
|