2021-05-20 23:47:45 +00:00
|
|
|
#include "TracyDebug.hpp"
|
2021-10-22 20:20:30 +00:00
|
|
|
#include "TracyStringHelpers.hpp"
|
2019-08-12 18:08:15 +00:00
|
|
|
#include "TracySysTrace.hpp"
|
2021-05-19 21:38:32 +00:00
|
|
|
#include "../common/TracySystem.hpp"
|
2019-08-12 18:08:15 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_HAS_SYSTEM_TRACING
|
|
|
|
|
2021-05-11 16:31:20 +00:00
|
|
|
#ifndef TRACY_SAMPLING_HZ
|
2021-10-07 21:28:40 +00:00
|
|
|
# if defined _WIN32
|
2021-05-11 16:31:20 +00:00
|
|
|
# define TRACY_SAMPLING_HZ 8000
|
|
|
|
# elif defined __linux__
|
|
|
|
# define TRACY_SAMPLING_HZ 10000
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2024-07-30 16:42:05 +00:00
|
|
|
static int GetSamplingFrequency()
|
2021-05-11 16:31:20 +00:00
|
|
|
{
|
2024-07-30 16:42:05 +00:00
|
|
|
int samplingHz = TRACY_SAMPLING_HZ;
|
|
|
|
|
|
|
|
auto env = GetEnvVar( "TRACY_SAMPLING_HZ" );
|
|
|
|
if( env )
|
|
|
|
{
|
|
|
|
int val = atoi( env );
|
|
|
|
if( val > 0 ) samplingHz = val;
|
|
|
|
}
|
|
|
|
|
2021-10-07 21:28:40 +00:00
|
|
|
#if defined _WIN32
|
2024-07-30 16:42:05 +00:00
|
|
|
return samplingHz > 8000 ? 8000 : ( samplingHz < 1 ? 1 : samplingHz );
|
2021-05-11 16:31:20 +00:00
|
|
|
#else
|
2024-07-30 16:42:05 +00:00
|
|
|
return samplingHz > 1000000 ? 1000000 : ( samplingHz < 1 ? 1 : samplingHz );
|
2021-05-11 16:31:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:42:05 +00:00
|
|
|
static int GetSamplingPeriod()
|
2021-05-11 16:31:20 +00:00
|
|
|
{
|
|
|
|
return 1000000000 / GetSamplingFrequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-07 21:28:40 +00:00
|
|
|
# if defined _WIN32
|
2019-08-12 21:05:17 +00:00
|
|
|
|
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 } };
|
2021-11-12 11:22:29 +00:00
|
|
|
static const GUID ThreadV2Guid = { 0x3d6fa8d1, 0xfe05, 0x11d0, { 0x9d, 0xda, 0x00, 0xc0, 0x4f, 0xd7, 0xba, 0x7c } };
|
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-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 );
|
2021-10-07 22:42:52 +00:00
|
|
|
MemWrite( &item->contextSwitch.oldThread, cswitch->oldThreadId );
|
|
|
|
MemWrite( &item->contextSwitch.newThread, cswitch->newThreadId );
|
2020-02-22 11:56:33 +00:00
|
|
|
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 );
|
2021-10-07 22:42:52 +00:00
|
|
|
MemWrite( &item->threadWakeup.thread, rt->threadId );
|
2020-02-22 11:56:33 +00:00
|
|
|
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;
|
2021-06-14 23:40:21 +00:00
|
|
|
if( sw->stackProcess == s_pid )
|
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 );
|
2021-10-07 22:42:52 +00:00
|
|
|
MemWrite( &item->callstackSampleFat.thread, sw->stackThread );
|
2020-07-26 12:28:13 +00:00
|
|
|
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
|
|
|
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;
|
|
|
|
|
2022-07-30 17:53:40 +00:00
|
|
|
TracyLfqPrepare( QueueType::FrameVsync );
|
|
|
|
MemWrite( &item->frameVsync.time, hdr.TimeStamp.QuadPart );
|
|
|
|
MemWrite( &item->frameVsync.id, vs->vidPnTargetId );
|
2020-06-27 17:56:23 +00:00
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetupVsync()
|
|
|
|
{
|
2022-05-11 00:30:59 +00:00
|
|
|
#if _WIN32_WINNT >= _WIN32_WINNT_WINBLUE && !defined(__MINGW32__)
|
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
|
|
|
}
|
|
|
|
|
2024-08-09 05:22:52 +00:00
|
|
|
static int GetSamplingInterval()
|
2021-05-11 16:31:20 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2023-01-05 20:43:33 +00:00
|
|
|
#ifndef TRACY_NO_SAMPLING
|
2020-02-22 13:16:04 +00:00
|
|
|
if( isOs64Bit )
|
2020-02-22 12:13:49 +00:00
|
|
|
{
|
2021-11-12 11:22:29 +00:00
|
|
|
CLASSIC_EVENT_ID stackId[2] = {};
|
|
|
|
stackId[0].EventGuid = PerfInfoGuid;
|
|
|
|
stackId[0].Type = 46;
|
|
|
|
stackId[1].EventGuid = ThreadV2Guid;
|
|
|
|
stackId[1].Type = 36;
|
2020-02-22 13:16:04 +00:00
|
|
|
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
|
|
|
}
|
2023-01-05 20:43:33 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2021-10-22 20:20:30 +00:00
|
|
|
void SysTraceGetExternalName( uint64_t thread, const char*& threadName, const char*& name )
|
2019-08-16 17:22:23 +00:00
|
|
|
{
|
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 )
|
|
|
|
{
|
2021-10-22 20:20:30 +00:00
|
|
|
threadName = CopyString( buf, ret );
|
2021-05-04 14:13:42 +00:00
|
|
|
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
|
|
|
{
|
2021-10-22 20:20:30 +00:00
|
|
|
threadName = CopyString( buf2, modlen );
|
2019-08-17 18:22:06 +00:00
|
|
|
threadSent = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle( phnd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle( hnd );
|
2019-08-17 01:44:47 +00:00
|
|
|
if( !threadSent )
|
|
|
|
{
|
2021-10-22 20:20:30 +00:00
|
|
|
threadName = CopyString( "???", 3 );
|
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 )
|
|
|
|
{
|
2021-10-22 20:20:30 +00:00
|
|
|
name = CopyStringFast( "System", 6 );
|
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++;
|
2021-10-22 20:20:30 +00:00
|
|
|
name = CopyStringFast( ptr );
|
2019-08-17 01:44:47 +00:00
|
|
|
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 )
|
|
|
|
{
|
2021-10-22 20:20:30 +00:00
|
|
|
threadName = CopyString( "???", 3 );
|
2019-08-17 01:44:47 +00:00
|
|
|
}
|
2021-10-22 20:20:30 +00:00
|
|
|
name = CopyStringFast( "???", 3 );
|
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
|
|
|
|
2022-08-18 11:40:37 +00:00
|
|
|
# if defined __i386 || defined __x86_64__
|
2022-08-31 15:59:46 +00:00
|
|
|
# include "TracyCpuid.hpp"
|
2022-08-18 11:40:37 +00:00
|
|
|
# endif
|
|
|
|
|
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
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2020-03-10 17:56:49 +00:00
|
|
|
static std::atomic<bool> traceActive { false };
|
2020-08-12 12:06:00 +00:00
|
|
|
static int s_numCpus = 0;
|
2021-05-18 23:29:21 +00:00
|
|
|
static int s_numBuffers = 0;
|
2021-12-05 11:02:28 +00:00
|
|
|
static int s_ctxBufferIdx = 0;
|
2020-08-15 00:43:18 +00:00
|
|
|
|
2021-12-21 14:48:40 +00:00
|
|
|
static RingBuffer* s_ring = nullptr;
|
2020-08-12 12:06:00 +00:00
|
|
|
|
2021-12-21 12:52:52 +00:00
|
|
|
static const int ThreadHashSize = 4 * 1024;
|
|
|
|
static uint32_t s_threadHash[ThreadHashSize] = {};
|
|
|
|
|
|
|
|
static bool CurrentProcOwnsThread( uint32_t tid )
|
|
|
|
{
|
|
|
|
const auto hash = tid & ( ThreadHashSize-1 );
|
|
|
|
const auto hv = s_threadHash[hash];
|
|
|
|
if( hv == tid ) return true;
|
|
|
|
if( hv == -tid ) return false;
|
|
|
|
|
|
|
|
char path[256];
|
|
|
|
sprintf( path, "/proc/self/task/%d", tid );
|
|
|
|
struct stat st;
|
|
|
|
if( stat( path, &st ) == 0 )
|
|
|
|
{
|
|
|
|
s_threadHash[hash] = tid;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_threadHash[hash] = -tid;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2021-05-20 00:15:06 +00:00
|
|
|
EventInstructionsRetired,
|
|
|
|
EventCacheReference,
|
|
|
|
EventCacheMiss,
|
|
|
|
EventBranchRetired,
|
2021-12-05 11:02:28 +00:00
|
|
|
EventBranchMiss,
|
2022-07-30 19:29:44 +00:00
|
|
|
EventVsync,
|
2021-12-05 11:02:28 +00:00
|
|
|
EventContextSwitch,
|
|
|
|
EventWakeup,
|
2021-05-18 23:25:13 +00:00
|
|
|
};
|
|
|
|
|
2021-05-23 16:53:09 +00:00
|
|
|
static void ProbePreciseIp( perf_event_attr& pe, unsigned long long config0, unsigned long long config1, pid_t pid )
|
2021-05-20 23:32:45 +00:00
|
|
|
{
|
2021-05-22 00:16:49 +00:00
|
|
|
pe.config = config1;
|
2021-05-20 23:32:45 +00:00
|
|
|
pe.precise_ip = 3;
|
|
|
|
while( pe.precise_ip != 0 )
|
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, pid, 0, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-20 23:32:45 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pe.precise_ip--;
|
|
|
|
}
|
2021-05-22 00:16:49 +00:00
|
|
|
pe.config = config0;
|
|
|
|
while( pe.precise_ip != 0 )
|
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, pid, 0, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-22 00:16:49 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pe.precise_ip--;
|
|
|
|
}
|
2021-05-20 23:47:45 +00:00
|
|
|
TracyDebug( " Probed precise_ip: %i\n", pe.precise_ip );
|
2021-05-20 23:32:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:21:09 +00:00
|
|
|
static void ProbePreciseIp( perf_event_attr& pe, pid_t pid )
|
|
|
|
{
|
|
|
|
pe.precise_ip = 3;
|
|
|
|
while( pe.precise_ip != 0 )
|
|
|
|
{
|
|
|
|
const int fd = perf_event_open( &pe, pid, 0, -1, PERF_FLAG_FD_CLOEXEC );
|
|
|
|
if( fd != -1 )
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pe.precise_ip--;
|
|
|
|
}
|
|
|
|
TracyDebug( " Probed precise_ip: %i\n", pe.precise_ip );
|
|
|
|
}
|
|
|
|
|
2021-05-23 17:45:13 +00:00
|
|
|
static bool IsGenuineIntel()
|
|
|
|
{
|
|
|
|
#if defined __i386 || defined __x86_64__
|
2021-12-04 12:41:55 +00:00
|
|
|
uint32_t regs[4] = {};
|
2021-05-23 17:45:13 +00:00
|
|
|
__get_cpuid( 0, regs, regs+1, regs+2, regs+3 );
|
|
|
|
char manufacturer[12];
|
|
|
|
memcpy( manufacturer, regs+1, 4 );
|
|
|
|
memcpy( manufacturer+4, regs+3, 4 );
|
|
|
|
memcpy( manufacturer+8, regs+2, 4 );
|
|
|
|
return memcmp( manufacturer, "GenuineIntel", 12 ) == 0;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
static const char* ReadFile( const char* path )
|
|
|
|
{
|
|
|
|
int fd = open( path, O_RDONLY );
|
|
|
|
if( fd < 0 ) return nullptr;
|
|
|
|
|
|
|
|
static char tmp[64];
|
|
|
|
const auto cnt = read( fd, tmp, 63 );
|
|
|
|
close( fd );
|
|
|
|
if( cnt < 0 ) return nullptr;
|
|
|
|
tmp[cnt] = '\0';
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SysTraceStart( int64_t& samplingPeriod )
|
2020-08-12 12:06:00 +00:00
|
|
|
{
|
2020-08-12 23:41:05 +00:00
|
|
|
#ifndef CLOCK_MONOTONIC_RAW
|
2021-12-05 11:02:28 +00:00
|
|
|
return false;
|
2020-08-12 23:41:05 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
const auto paranoidLevelStr = ReadFile( "/proc/sys/kernel/perf_event_paranoid" );
|
|
|
|
if( !paranoidLevelStr ) return false;
|
2022-04-26 19:29:16 +00:00
|
|
|
#ifdef TRACY_VERBOSE
|
|
|
|
int paranoidLevel = 2;
|
2021-12-05 11:02:28 +00:00
|
|
|
paranoidLevel = atoi( paranoidLevelStr );
|
|
|
|
TracyDebug( "perf_event_paranoid: %i\n", paranoidLevel );
|
2022-04-26 19:29:16 +00:00
|
|
|
#endif
|
2021-12-05 11:02:28 +00:00
|
|
|
|
2022-07-30 19:29:44 +00:00
|
|
|
int switchId = -1, wakeupId = -1, vsyncId = -1;
|
2021-12-21 20:10:17 +00:00
|
|
|
const auto switchIdStr = ReadFile( "/sys/kernel/debug/tracing/events/sched/sched_switch/id" );
|
2021-12-05 11:02:28 +00:00
|
|
|
if( switchIdStr ) switchId = atoi( switchIdStr );
|
2021-12-21 20:10:17 +00:00
|
|
|
const auto wakeupIdStr = ReadFile( "/sys/kernel/debug/tracing/events/sched/sched_wakeup/id" );
|
2021-12-05 11:02:28 +00:00
|
|
|
if( wakeupIdStr ) wakeupId = atoi( wakeupIdStr );
|
2022-07-30 19:29:44 +00:00
|
|
|
const auto vsyncIdStr = ReadFile( "/sys/kernel/debug/tracing/events/drm/drm_vblank_event/id" );
|
|
|
|
if( vsyncIdStr ) vsyncId = atoi( vsyncIdStr );
|
2021-12-05 11:02:28 +00:00
|
|
|
|
2022-07-30 19:29:44 +00:00
|
|
|
TracyDebug( "sched_switch id: %i\n", switchId );
|
|
|
|
TracyDebug( "sched_wakeup id: %i\n", wakeupId );
|
|
|
|
TracyDebug( "drm_vblank_event id: %i\n", vsyncId );
|
2021-12-05 11:02:28 +00:00
|
|
|
|
2023-04-04 19:20:46 +00:00
|
|
|
#ifdef TRACY_NO_SAMPLING
|
|
|
|
const bool noSoftwareSampling = true;
|
|
|
|
#else
|
|
|
|
const char* noSoftwareSamplingEnv = GetEnvVar( "TRACY_NO_SAMPLING" );
|
|
|
|
const bool noSoftwareSampling = noSoftwareSamplingEnv && noSoftwareSamplingEnv[0] == '1';
|
|
|
|
#endif
|
|
|
|
|
2021-05-19 21:38:32 +00:00
|
|
|
#ifdef TRACY_NO_SAMPLE_RETIREMENT
|
|
|
|
const bool noRetirement = true;
|
|
|
|
#else
|
|
|
|
const char* noRetirementEnv = GetEnvVar( "TRACY_NO_SAMPLE_RETIREMENT" );
|
|
|
|
const bool noRetirement = noRetirementEnv && noRetirementEnv[0] == '1';
|
|
|
|
#endif
|
|
|
|
|
2021-05-20 00:15:06 +00:00
|
|
|
#ifdef TRACY_NO_SAMPLE_CACHE
|
|
|
|
const bool noCache = true;
|
|
|
|
#else
|
|
|
|
const char* noCacheEnv = GetEnvVar( "TRACY_NO_SAMPLE_CACHE" );
|
|
|
|
const bool noCache = noCacheEnv && noCacheEnv[0] == '1';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TRACY_NO_SAMPLE_BRANCH
|
|
|
|
const bool noBranch = true;
|
|
|
|
#else
|
|
|
|
const char* noBranchEnv = GetEnvVar( "TRACY_NO_SAMPLE_BRANCH" );
|
|
|
|
const bool noBranch = noBranchEnv && noBranchEnv[0] == '1';
|
|
|
|
#endif
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
#ifdef TRACY_NO_CONTEXT_SWITCH
|
|
|
|
const bool noCtxSwitch = true;
|
|
|
|
#else
|
|
|
|
const char* noCtxSwitchEnv = GetEnvVar( "TRACY_NO_CONTEXT_SWITCH" );
|
|
|
|
const bool noCtxSwitch = noCtxSwitchEnv && noCtxSwitchEnv[0] == '1';
|
|
|
|
#endif
|
|
|
|
|
2022-07-30 19:29:44 +00:00
|
|
|
#ifdef TRACY_NO_VSYNC_CAPTURE
|
|
|
|
const bool noVsync = true;
|
|
|
|
#else
|
|
|
|
const char* noVsyncEnv = GetEnvVar( "TRACY_NO_VSYNC_CAPTURE" );
|
|
|
|
const bool noVsync = noVsyncEnv && noVsyncEnv[0] == '1';
|
|
|
|
#endif
|
|
|
|
|
2021-05-11 16:31:20 +00:00
|
|
|
samplingPeriod = GetSamplingPeriod();
|
2021-05-23 16:53:09 +00:00
|
|
|
uint32_t currentPid = (uint32_t)getpid();
|
2020-08-12 12:06:00 +00:00
|
|
|
|
2022-03-24 14:28:27 +00:00
|
|
|
s_numCpus = (int)std::thread::hardware_concurrency();
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
const auto maxNumBuffers = s_numCpus * (
|
|
|
|
1 + // software sampling
|
|
|
|
2 + // CPU cycles + instructions retired
|
|
|
|
2 + // cache reference + miss
|
|
|
|
2 + // branch retired + miss
|
2022-07-30 19:29:44 +00:00
|
|
|
2 + // context switches + wakeups
|
|
|
|
1 // vsync
|
2021-12-05 11:02:28 +00:00
|
|
|
);
|
2021-12-21 14:48:40 +00:00
|
|
|
s_ring = (RingBuffer*)tracy_malloc( sizeof( RingBuffer ) * maxNumBuffers );
|
2021-05-18 23:29:21 +00:00
|
|
|
s_numBuffers = 0;
|
2020-08-12 12:06:00 +00:00
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
// software sampling
|
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.disabled = 1;
|
|
|
|
pe.freq = 1;
|
2021-05-23 16:53:09 +00:00
|
|
|
pe.inherit = 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
|
|
|
|
|
2023-04-04 19:20:46 +00:00
|
|
|
if( !noSoftwareSampling )
|
2020-08-12 12:06:00 +00:00
|
|
|
{
|
2023-04-04 19:20:46 +00:00
|
|
|
TracyDebug( "Setup software sampling\n" );
|
|
|
|
ProbePreciseIp( pe, currentPid );
|
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
2020-08-12 12:06:00 +00:00
|
|
|
{
|
2023-04-04 19:20:46 +00:00
|
|
|
int fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2022-04-28 21:50:13 +00:00
|
|
|
if( fd == -1 )
|
|
|
|
{
|
2023-04-04 19:20:46 +00:00
|
|
|
pe.exclude_kernel = 1;
|
|
|
|
ProbePreciseIp( pe, currentPid );
|
|
|
|
fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
|
|
|
if( fd == -1 )
|
|
|
|
{
|
|
|
|
TracyDebug( " Failed to setup!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
TracyDebug( " No access to kernel samples\n" );
|
|
|
|
}
|
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventCallstack );
|
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
2022-04-28 21:50:13 +00:00
|
|
|
}
|
2022-04-04 15:56:40 +00:00
|
|
|
}
|
2020-08-12 12:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 00:16:49 +00:00
|
|
|
// CPU cycles + instructions retired
|
2021-05-19 00:20:35 +00:00
|
|
|
pe = {};
|
|
|
|
pe.type = PERF_TYPE_HARDWARE;
|
|
|
|
pe.size = sizeof( perf_event_attr );
|
2021-05-23 17:03:11 +00:00
|
|
|
pe.sample_freq = 5000;
|
2021-06-04 10:50:55 +00:00
|
|
|
pe.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TIME;
|
2021-05-19 00:20:35 +00:00
|
|
|
pe.disabled = 1;
|
|
|
|
pe.exclude_kernel = 1;
|
2021-05-23 16:44:16 +00:00
|
|
|
pe.exclude_guest = 1;
|
|
|
|
pe.exclude_hv = 1;
|
2021-05-23 16:02:06 +00:00
|
|
|
pe.freq = 1;
|
2021-05-23 16:53:09 +00:00
|
|
|
pe.inherit = 1;
|
2021-06-19 11:40:31 +00:00
|
|
|
#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
|
|
|
|
|
2021-05-19 21:38:32 +00:00
|
|
|
if( !noRetirement )
|
2021-05-19 00:20:35 +00:00
|
|
|
{
|
2021-05-22 00:16:49 +00:00
|
|
|
TracyDebug( "Setup sampling cycles + retirement\n" );
|
2021-05-23 16:53:09 +00:00
|
|
|
ProbePreciseIp( pe, PERF_COUNT_HW_CPU_CYCLES, PERF_COUNT_HW_INSTRUCTIONS, currentPid );
|
2021-05-19 21:38:32 +00:00
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
2021-05-19 00:20:35 +00:00
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-19 21:38:32 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventCpuCycles );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2021-05-19 21:38:32 +00:00
|
|
|
}
|
2021-05-19 00:20:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 00:16:49 +00:00
|
|
|
pe.config = PERF_COUNT_HW_INSTRUCTIONS;
|
2021-05-19 21:38:32 +00:00
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
2021-05-19 00:20:35 +00:00
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-19 21:38:32 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventInstructionsRetired );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2021-05-19 21:38:32 +00:00
|
|
|
}
|
2021-05-19 00:20:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 00:16:49 +00:00
|
|
|
// cache reference + miss
|
2021-05-20 00:15:06 +00:00
|
|
|
if( !noCache )
|
|
|
|
{
|
2021-05-22 00:16:49 +00:00
|
|
|
TracyDebug( "Setup sampling CPU cache references + misses\n" );
|
2021-05-23 16:53:09 +00:00
|
|
|
ProbePreciseIp( pe, PERF_COUNT_HW_CACHE_REFERENCES, PERF_COUNT_HW_CACHE_MISSES, currentPid );
|
2021-05-23 17:45:13 +00:00
|
|
|
if( IsGenuineIntel() )
|
|
|
|
{
|
|
|
|
pe.precise_ip = 0;
|
|
|
|
TracyDebug( " CPU is GenuineIntel, forcing precise_ip down to 0\n" );
|
|
|
|
}
|
2021-05-20 00:15:06 +00:00
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-20 00:15:06 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventCacheReference );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2021-05-20 00:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 00:16:49 +00:00
|
|
|
pe.config = PERF_COUNT_HW_CACHE_MISSES;
|
2021-05-20 00:15:06 +00:00
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-20 00:15:06 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventCacheMiss );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2021-05-20 00:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 00:16:49 +00:00
|
|
|
// branch retired + miss
|
2021-05-20 00:15:06 +00:00
|
|
|
if( !noBranch )
|
|
|
|
{
|
2021-05-22 00:16:49 +00:00
|
|
|
TracyDebug( "Setup sampling CPU branch retirements + misses\n" );
|
2021-05-23 16:53:09 +00:00
|
|
|
ProbePreciseIp( pe, PERF_COUNT_HW_BRANCH_INSTRUCTIONS, PERF_COUNT_HW_BRANCH_MISSES, currentPid );
|
2021-05-20 00:15:06 +00:00
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-20 00:15:06 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventBranchRetired );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2021-05-20 00:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 00:16:49 +00:00
|
|
|
pe.config = PERF_COUNT_HW_BRANCH_MISSES;
|
2021-05-20 00:15:06 +00:00
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
|
|
|
{
|
2021-05-23 16:53:09 +00:00
|
|
|
const int fd = perf_event_open( &pe, currentPid, i, -1, PERF_FLAG_FD_CLOEXEC );
|
2021-05-20 00:15:06 +00:00
|
|
|
if( fd != -1 )
|
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventBranchMiss );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2021-05-20 00:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
s_ctxBufferIdx = s_numBuffers;
|
2021-05-19 00:20:35 +00:00
|
|
|
|
2022-07-30 19:29:44 +00:00
|
|
|
// vsync
|
|
|
|
if( !noVsync && vsyncId != -1 )
|
|
|
|
{
|
|
|
|
pe = {};
|
|
|
|
pe.type = PERF_TYPE_TRACEPOINT;
|
|
|
|
pe.size = sizeof( perf_event_attr );
|
|
|
|
pe.sample_period = 1;
|
|
|
|
pe.sample_type = PERF_SAMPLE_TIME | PERF_SAMPLE_RAW;
|
|
|
|
pe.disabled = 1;
|
|
|
|
pe.config = vsyncId;
|
|
|
|
#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
|
|
|
|
|
|
|
|
TracyDebug( "Setup vsync capture\n" );
|
|
|
|
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( 64*1024, fd, EventVsync, i );
|
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
// context switches
|
|
|
|
if( !noCtxSwitch && switchId != -1 )
|
|
|
|
{
|
|
|
|
pe = {};
|
|
|
|
pe.type = PERF_TYPE_TRACEPOINT;
|
|
|
|
pe.size = sizeof( perf_event_attr );
|
|
|
|
pe.sample_period = 1;
|
2021-12-21 12:59:16 +00:00
|
|
|
pe.sample_type = PERF_SAMPLE_TIME | PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN;
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION( 4, 8, 0 )
|
|
|
|
pe.sample_max_stack = 127;
|
|
|
|
#endif
|
2021-12-05 11:02:28 +00:00
|
|
|
pe.disabled = 1;
|
|
|
|
pe.inherit = 1;
|
|
|
|
pe.config = switchId;
|
|
|
|
#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;
|
2021-06-04 10:50:55 +00:00
|
|
|
#endif
|
2021-12-02 00:29:22 +00:00
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
TracyDebug( "Setup context switch capture\n" );
|
|
|
|
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 )
|
2020-08-12 12:06:00 +00:00
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 256*1024, fd, EventContextSwitch, i );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2020-08-12 12:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
if( wakeupId != -1 )
|
2019-08-14 11:56:15 +00:00
|
|
|
{
|
2021-12-05 11:02:28 +00:00
|
|
|
pe.config = wakeupId;
|
2021-12-21 12:59:16 +00:00
|
|
|
pe.config &= ~PERF_SAMPLE_CALLCHAIN;
|
2019-08-14 11:56:15 +00:00
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
TracyDebug( "Setup wakeup capture\n" );
|
|
|
|
for( int i=0; i<s_numCpus; i++ )
|
2019-09-27 13:51:29 +00:00
|
|
|
{
|
2021-12-05 11:02:28 +00:00
|
|
|
const int fd = perf_event_open( &pe, -1, i, -1, PERF_FLAG_FD_CLOEXEC );
|
|
|
|
if( fd != -1 )
|
|
|
|
{
|
2021-12-21 14:48:40 +00:00
|
|
|
new( s_ring+s_numBuffers ) RingBuffer( 64*1024, fd, EventWakeup, i );
|
2022-04-04 15:56:40 +00:00
|
|
|
if( s_ring[s_numBuffers].IsValid() )
|
|
|
|
{
|
|
|
|
s_numBuffers++;
|
|
|
|
TracyDebug( " Core %i ok\n", i );
|
|
|
|
}
|
2021-12-05 11:02:28 +00:00
|
|
|
}
|
2019-09-27 13:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
TracyDebug( "Ringbuffers in use: %i\n", s_numBuffers );
|
2019-09-27 13:51:29 +00:00
|
|
|
|
2020-03-10 17:56:49 +00:00
|
|
|
traceActive.store( true, std::memory_order_relaxed );
|
2019-08-14 11:56:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SysTraceStop()
|
|
|
|
{
|
2020-03-10 17:56:49 +00:00
|
|
|
traceActive.store( false, std::memory_order_relaxed );
|
2019-08-14 11:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 14:48:40 +00:00
|
|
|
static uint64_t* GetCallstackBlock( uint64_t cnt, RingBuffer& ring, uint64_t offset )
|
2021-12-21 12:58:43 +00:00
|
|
|
{
|
|
|
|
auto trace = (uint64_t*)tracy_malloc_fast( ( 1 + cnt ) * sizeof( uint64_t ) );
|
|
|
|
ring.Read( trace+1, offset, sizeof( uint64_t ) * cnt );
|
|
|
|
|
|
|
|
#if defined __x86_64__ || defined _M_X64
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for( uint64_t j=1; j<=cnt; j++ )
|
|
|
|
{
|
|
|
|
if( trace[j] >= (uint64_t)-4095 ) // PERF_CONTEXT_MAX
|
|
|
|
{
|
|
|
|
memmove( trace+j, trace+j+1, sizeof( uint64_t ) * ( cnt - j ) );
|
|
|
|
cnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( trace, &cnt, sizeof( uint64_t ) );
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
void SysTraceWorker( void* ptr )
|
2019-08-14 11:56:15 +00:00
|
|
|
{
|
2021-12-05 11:02:28 +00:00
|
|
|
ThreadExitHandler threadExitHandler;
|
|
|
|
SetThreadName( "Tracy Sampling" );
|
|
|
|
InitRpmalloc();
|
2022-06-16 12:24:11 +00:00
|
|
|
sched_param sp = { 99 };
|
2022-06-16 12:23:42 +00:00
|
|
|
if( pthread_setschedparam( pthread_self(), SCHED_FIFO, &sp ) != 0 ) TracyDebug( "Failed to increase SysTraceWorker thread priority!\n" );
|
2022-08-17 23:08:22 +00:00
|
|
|
auto ctxBufferIdx = s_ctxBufferIdx;
|
|
|
|
auto ringArray = s_ring;
|
|
|
|
auto numBuffers = s_numBuffers;
|
|
|
|
for( int i=0; i<numBuffers; i++ ) ringArray[i].Enable();
|
2019-08-14 11:56:15 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
2022-01-31 19:53:10 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
if( !GetProfiler().IsConnected() )
|
|
|
|
{
|
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
2022-08-17 23:08:22 +00:00
|
|
|
for( int i=0; i<numBuffers; i++ )
|
2022-01-31 19:53:10 +00:00
|
|
|
{
|
2022-08-17 23:08:22 +00:00
|
|
|
auto& ring = ringArray[i];
|
2022-01-31 19:53:10 +00:00
|
|
|
const auto head = ring.LoadHead();
|
|
|
|
const auto tail = ring.GetTail();
|
|
|
|
if( head != tail )
|
|
|
|
{
|
|
|
|
const auto end = head - tail;
|
|
|
|
ring.Advance( end );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
bool hadData = false;
|
2022-08-17 23:08:22 +00:00
|
|
|
for( int i=0; i<ctxBufferIdx; i++ )
|
2021-06-02 22:20:50 +00:00
|
|
|
{
|
2021-12-05 11:02:28 +00:00
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
2022-08-17 23:08:22 +00:00
|
|
|
auto& ring = ringArray[i];
|
2021-12-05 11:02:28 +00:00
|
|
|
const auto head = ring.LoadHead();
|
|
|
|
const auto tail = ring.GetTail();
|
|
|
|
if( head == tail ) continue;
|
|
|
|
assert( head > tail );
|
|
|
|
hadData = true;
|
|
|
|
|
2022-06-16 11:39:07 +00:00
|
|
|
const auto id = ring.GetId();
|
|
|
|
assert( id != EventContextSwitch );
|
2021-12-05 11:02:28 +00:00
|
|
|
const auto end = head - tail;
|
|
|
|
uint64_t pos = 0;
|
2022-06-16 11:48:15 +00:00
|
|
|
if( id == EventCallstack )
|
2021-06-02 22:20:50 +00:00
|
|
|
{
|
2022-06-16 11:48:15 +00:00
|
|
|
while( pos < end )
|
2021-12-05 11:02:28 +00:00
|
|
|
{
|
2022-06-16 11:48:15 +00:00
|
|
|
perf_event_header hdr;
|
|
|
|
ring.Read( &hdr, pos, sizeof( perf_event_header ) );
|
|
|
|
if( hdr.type == PERF_RECORD_SAMPLE )
|
2021-12-05 11:02:28 +00:00
|
|
|
{
|
2022-06-16 11:48:15 +00:00
|
|
|
auto offset = pos + sizeof( perf_event_header );
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
// Layout:
|
|
|
|
// u32 pid, tid
|
|
|
|
// u64 time
|
|
|
|
// u64 cnt
|
|
|
|
// u64 ip[cnt]
|
|
|
|
|
|
|
|
uint32_t tid;
|
|
|
|
uint64_t t0;
|
|
|
|
uint64_t cnt;
|
|
|
|
|
|
|
|
offset += sizeof( uint32_t );
|
|
|
|
ring.Read( &tid, offset, sizeof( uint32_t ) );
|
|
|
|
offset += sizeof( uint32_t );
|
|
|
|
ring.Read( &t0, offset, sizeof( uint64_t ) );
|
|
|
|
offset += sizeof( uint64_t );
|
|
|
|
ring.Read( &cnt, offset, sizeof( uint64_t ) );
|
|
|
|
offset += sizeof( uint64_t );
|
|
|
|
|
|
|
|
if( cnt > 0 )
|
|
|
|
{
|
|
|
|
#if defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
|
|
|
t0 = ring.ConvertTimeToTsc( t0 );
|
|
|
|
#endif
|
2021-12-21 12:58:43 +00:00
|
|
|
auto trace = GetCallstackBlock( cnt, ring, offset );
|
2019-08-19 13:26:09 +00:00
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
TracyLfqPrepare( QueueType::CallstackSample );
|
|
|
|
MemWrite( &item->callstackSampleFat.time, t0 );
|
|
|
|
MemWrite( &item->callstackSampleFat.thread, tid );
|
|
|
|
MemWrite( &item->callstackSampleFat.ptr, (uint64_t)trace );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 11:48:15 +00:00
|
|
|
pos += hdr.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while( pos < end )
|
|
|
|
{
|
|
|
|
perf_event_header hdr;
|
|
|
|
ring.Read( &hdr, pos, sizeof( perf_event_header ) );
|
|
|
|
if( hdr.type == PERF_RECORD_SAMPLE )
|
2021-12-05 11:02:28 +00:00
|
|
|
{
|
2022-06-16 11:48:15 +00:00
|
|
|
auto offset = pos + sizeof( perf_event_header );
|
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
// Layout:
|
|
|
|
// u64 ip
|
|
|
|
// u64 time
|
2021-06-03 10:47:00 +00:00
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
uint64_t ip, t0;
|
|
|
|
ring.Read( &ip, offset, sizeof( uint64_t ) );
|
|
|
|
offset += sizeof( uint64_t );
|
|
|
|
ring.Read( &t0, offset, sizeof( uint64_t ) );
|
|
|
|
|
|
|
|
#if defined TRACY_HW_TIMER && ( defined __i386 || defined _M_IX86 || defined __x86_64__ || defined _M_X64 )
|
|
|
|
t0 = ring.ConvertTimeToTsc( t0 );
|
2021-06-03 11:49:38 +00:00
|
|
|
#endif
|
2021-12-05 11:02:28 +00:00
|
|
|
QueueType type;
|
|
|
|
switch( id )
|
|
|
|
{
|
|
|
|
case EventCpuCycles:
|
|
|
|
type = QueueType::HwSampleCpuCycle;
|
|
|
|
break;
|
|
|
|
case EventInstructionsRetired:
|
|
|
|
type = QueueType::HwSampleInstructionRetired;
|
|
|
|
break;
|
|
|
|
case EventCacheReference:
|
|
|
|
type = QueueType::HwSampleCacheReference;
|
|
|
|
break;
|
|
|
|
case EventCacheMiss:
|
|
|
|
type = QueueType::HwSampleCacheMiss;
|
|
|
|
break;
|
|
|
|
case EventBranchRetired:
|
|
|
|
type = QueueType::HwSampleBranchRetired;
|
|
|
|
break;
|
|
|
|
case EventBranchMiss:
|
|
|
|
type = QueueType::HwSampleBranchMiss;
|
|
|
|
break;
|
|
|
|
default:
|
2023-01-25 14:08:51 +00:00
|
|
|
abort();
|
2021-12-05 11:02:28 +00:00
|
|
|
}
|
2021-06-03 10:47:00 +00:00
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
TracyLfqPrepare( type );
|
|
|
|
MemWrite( &item->hwSample.ip, ip );
|
|
|
|
MemWrite( &item->hwSample.time, t0 );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
2022-06-16 11:48:15 +00:00
|
|
|
pos += hdr.size;
|
2021-12-05 11:02:28 +00:00
|
|
|
}
|
2021-06-03 11:49:38 +00:00
|
|
|
}
|
2021-12-05 11:02:28 +00:00
|
|
|
assert( pos == end );
|
|
|
|
ring.Advance( end );
|
2021-06-03 11:49:38 +00:00
|
|
|
}
|
2021-12-05 11:02:28 +00:00
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
|
|
|
|
2022-08-17 23:08:22 +00:00
|
|
|
if( ctxBufferIdx != numBuffers )
|
2021-06-03 11:10:26 +00:00
|
|
|
{
|
2022-08-17 23:08:22 +00:00
|
|
|
const auto ctxBufNum = numBuffers - ctxBufferIdx;
|
2021-12-05 11:02:28 +00:00
|
|
|
|
|
|
|
int activeNum = 0;
|
2022-08-18 11:59:56 +00:00
|
|
|
uint16_t active[512];
|
2021-12-05 11:02:28 +00:00
|
|
|
uint32_t end[512];
|
|
|
|
uint32_t pos[512];
|
|
|
|
for( int i=0; i<ctxBufNum; i++ )
|
2021-06-03 11:10:26 +00:00
|
|
|
{
|
2022-08-17 23:08:22 +00:00
|
|
|
const auto rbIdx = ctxBufferIdx + i;
|
|
|
|
const auto rbHead = ringArray[rbIdx].LoadHead();
|
|
|
|
const auto rbTail = ringArray[rbIdx].GetTail();
|
2021-12-05 11:02:28 +00:00
|
|
|
const auto rbActive = rbHead != rbTail;
|
|
|
|
|
|
|
|
if( rbActive )
|
|
|
|
{
|
2022-08-18 11:59:56 +00:00
|
|
|
active[activeNum] = (uint16_t)i;
|
2021-12-05 11:02:28 +00:00
|
|
|
activeNum++;
|
|
|
|
end[i] = rbHead - rbTail;
|
|
|
|
pos[i] = 0;
|
|
|
|
}
|
|
|
|
else
|
2021-06-03 11:10:26 +00:00
|
|
|
{
|
2021-12-05 11:02:28 +00:00
|
|
|
end[i] = 0;
|
2021-06-03 11:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-05 11:02:28 +00:00
|
|
|
if( activeNum > 0 )
|
|
|
|
{
|
|
|
|
hadData = true;
|
|
|
|
while( activeNum > 0 )
|
|
|
|
{
|
|
|
|
int sel = -1;
|
2022-08-18 11:59:56 +00:00
|
|
|
int selPos;
|
2021-12-05 11:02:28 +00:00
|
|
|
int64_t t0 = std::numeric_limits<int64_t>::max();
|
2022-08-18 11:59:56 +00:00
|
|
|
for( int i=0; i<activeNum; i++ )
|
2021-12-05 11:02:28 +00:00
|
|
|
{
|
2022-08-18 11:59:56 +00:00
|
|
|
auto idx = active[i];
|
|
|
|
auto rbPos = pos[idx];
|
|
|
|
assert( rbPos < end[idx] );
|
|
|
|
const auto rbIdx = ctxBufferIdx + idx;
|
2021-12-05 11:02:28 +00:00
|
|
|
perf_event_header hdr;
|
2022-08-17 23:08:22 +00:00
|
|
|
ringArray[rbIdx].Read( &hdr, rbPos, sizeof( perf_event_header ) );
|
2021-12-05 11:02:28 +00:00
|
|
|
if( hdr.type == PERF_RECORD_SAMPLE )
|
|
|
|
{
|
|
|
|
int64_t rbTime;
|
2022-08-17 23:08:22 +00:00
|
|
|
ringArray[rbIdx].Read( &rbTime, rbPos + sizeof( perf_event_header ), sizeof( int64_t ) );
|
2021-12-05 11:02:28 +00:00
|
|
|
if( rbTime < t0 )
|
|
|
|
{
|
|
|
|
t0 = rbTime;
|
2022-08-18 11:59:56 +00:00
|
|
|
sel = idx;
|
|
|
|
selPos = i;
|
2021-12-05 11:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rbPos += hdr.size;
|
2022-08-18 11:59:56 +00:00
|
|
|
if( rbPos == end[idx] )
|
2021-12-05 11:02:28 +00:00
|
|
|
{
|
2022-08-18 11:59:56 +00:00
|
|
|
memmove( active+i, active+i+1, sizeof(*active) * ( activeNum - i - 1 ) );
|
2021-12-05 11:02:28 +00:00
|
|
|
activeNum--;
|
2022-08-18 11:59:56 +00:00
|
|
|
i--;
|
2021-12-05 11:02:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-18 11:59:56 +00:00
|
|
|
pos[idx] = rbPos;
|
2021-12-05 11:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( sel >= 0 )
|
|
|
|
{
|
2022-08-17 23:08:22 +00:00
|
|
|
auto& ring = ringArray[ctxBufferIdx + sel];
|
2021-12-05 11:02:28 +00:00
|
|
|
auto rbPos = pos[sel];
|
|
|
|
auto offset = rbPos;
|
|
|
|
perf_event_header hdr;
|
|
|
|
ring.Read( &hdr, offset, sizeof( perf_event_header ) );
|
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 )
|
2021-12-05 11:02:28 +00:00
|
|
|
t0 = ring.ConvertTimeToTsc( t0 );
|
2020-08-12 23:41:05 +00:00
|
|
|
#endif
|
2019-08-14 13:09:33 +00:00
|
|
|
|
2022-07-30 19:29:44 +00:00
|
|
|
const auto rid = ring.GetId();
|
|
|
|
if( rid == EventContextSwitch )
|
2021-12-05 11:02:28 +00:00
|
|
|
{
|
|
|
|
// Layout:
|
|
|
|
// u64 time
|
2021-12-21 12:59:16 +00:00
|
|
|
// u64 cnt
|
|
|
|
// u64 ip[cnt]
|
2021-12-05 11:02:28 +00:00
|
|
|
// u32 size
|
|
|
|
// u8 data[size]
|
|
|
|
// Data (not ABI stable, but has not changed since it was added, in 2009):
|
|
|
|
// u8 hdr[8]
|
|
|
|
// u8 prev_comm[16]
|
|
|
|
// u32 prev_pid
|
|
|
|
// u32 prev_prio
|
2022-02-14 21:34:20 +00:00
|
|
|
// lng prev_state
|
2021-12-05 11:02:28 +00:00
|
|
|
// u8 next_comm[16]
|
|
|
|
// u32 next_pid
|
|
|
|
// u32 next_prio
|
|
|
|
|
2021-12-21 12:59:16 +00:00
|
|
|
offset += sizeof( perf_event_header ) + sizeof( uint64_t );
|
|
|
|
|
|
|
|
uint64_t cnt;
|
|
|
|
ring.Read( &cnt, offset, sizeof( uint64_t ) );
|
|
|
|
offset += sizeof( uint64_t );
|
|
|
|
const auto traceOffset = offset;
|
|
|
|
offset += sizeof( uint64_t ) * cnt + sizeof( uint32_t ) + 8 + 16;
|
2021-12-05 11:02:28 +00:00
|
|
|
|
|
|
|
uint32_t prev_pid, next_pid;
|
2022-02-14 21:34:20 +00:00
|
|
|
long prev_state;
|
2021-12-05 11:02:28 +00:00
|
|
|
|
|
|
|
ring.Read( &prev_pid, offset, sizeof( uint32_t ) );
|
|
|
|
offset += sizeof( uint32_t ) + sizeof( uint32_t );
|
2022-02-14 21:34:20 +00:00
|
|
|
ring.Read( &prev_state, offset, sizeof( long ) );
|
|
|
|
offset += sizeof( long ) + 16;
|
2021-12-05 11:02:28 +00:00
|
|
|
ring.Read( &next_pid, offset, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
uint8_t reason = 100;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
if( prev_state & 0x0001 ) state = 104;
|
|
|
|
else if( prev_state & 0x0002 ) state = 101;
|
|
|
|
else if( prev_state & 0x0004 ) state = 105;
|
|
|
|
else if( prev_state & 0x0008 ) state = 106;
|
|
|
|
else if( prev_state & 0x0010 ) state = 108;
|
|
|
|
else if( prev_state & 0x0020 ) state = 109;
|
|
|
|
else if( prev_state & 0x0040 ) state = 110;
|
|
|
|
else if( prev_state & 0x0080 ) state = 102;
|
|
|
|
else state = 103;
|
|
|
|
|
|
|
|
TracyLfqPrepare( QueueType::ContextSwitch );
|
|
|
|
MemWrite( &item->contextSwitch.time, t0 );
|
|
|
|
MemWrite( &item->contextSwitch.oldThread, prev_pid );
|
|
|
|
MemWrite( &item->contextSwitch.newThread, next_pid );
|
|
|
|
MemWrite( &item->contextSwitch.cpu, uint8_t( ring.GetCpu() ) );
|
|
|
|
MemWrite( &item->contextSwitch.reason, reason );
|
|
|
|
MemWrite( &item->contextSwitch.state, state );
|
|
|
|
TracyLfqCommit;
|
2021-12-21 12:59:16 +00:00
|
|
|
|
|
|
|
if( cnt > 0 && prev_pid != 0 && CurrentProcOwnsThread( prev_pid ) )
|
|
|
|
{
|
|
|
|
auto trace = GetCallstackBlock( cnt, ring, traceOffset );
|
|
|
|
|
2021-12-21 13:05:07 +00:00
|
|
|
TracyLfqPrepare( QueueType::CallstackSampleContextSwitch );
|
2021-12-21 12:59:16 +00:00
|
|
|
MemWrite( &item->callstackSampleFat.time, t0 );
|
|
|
|
MemWrite( &item->callstackSampleFat.thread, prev_pid );
|
|
|
|
MemWrite( &item->callstackSampleFat.ptr, (uint64_t)trace );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
2021-12-05 11:02:28 +00:00
|
|
|
}
|
2022-07-30 19:29:44 +00:00
|
|
|
else if( rid == EventWakeup )
|
2021-12-05 11:02:28 +00:00
|
|
|
{
|
|
|
|
// Layout:
|
|
|
|
// u64 time
|
|
|
|
// u32 size
|
|
|
|
// u8 data[size]
|
|
|
|
// Data:
|
|
|
|
// u8 hdr[8]
|
|
|
|
// u8 comm[16]
|
|
|
|
// u32 pid
|
|
|
|
// u32 prio
|
|
|
|
// u64 target_cpu
|
|
|
|
|
|
|
|
offset += sizeof( perf_event_header ) + sizeof( uint64_t ) + sizeof( uint32_t ) + 8 + 16;
|
|
|
|
|
|
|
|
uint32_t pid;
|
|
|
|
ring.Read( &pid, offset, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
TracyLfqPrepare( QueueType::ThreadWakeup );
|
|
|
|
MemWrite( &item->threadWakeup.time, t0 );
|
|
|
|
MemWrite( &item->threadWakeup.thread, pid );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
2022-07-30 19:29:44 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert( rid == EventVsync );
|
|
|
|
// Layout:
|
|
|
|
// u64 time
|
|
|
|
// u32 size
|
|
|
|
// u8 data[size]
|
|
|
|
// Data (not ABI stable):
|
|
|
|
// u8 hdr[8]
|
|
|
|
// i32 crtc
|
|
|
|
// u32 seq
|
|
|
|
// i64 ktime
|
|
|
|
// u8 high precision
|
|
|
|
|
|
|
|
offset += sizeof( perf_event_header ) + sizeof( uint64_t ) + sizeof( uint32_t ) + 8;
|
|
|
|
|
|
|
|
int32_t crtc;
|
|
|
|
ring.Read( &crtc, offset, sizeof( int32_t ) );
|
|
|
|
|
|
|
|
// Note: The timestamp value t0 might be off by a number of microseconds from the
|
|
|
|
// true hardware vblank event. The ktime value should be used instead, but it is
|
|
|
|
// measured in CLOCK_MONOTONIC time. Tracy only supports the timestamp counter
|
|
|
|
// register (TSC) or CLOCK_MONOTONIC_RAW clock.
|
|
|
|
#if 0
|
|
|
|
offset += sizeof( uint32_t ) * 2;
|
|
|
|
int64_t ktime;
|
|
|
|
ring.Read( &ktime, offset, sizeof( int64_t ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TracyLfqPrepare( QueueType::FrameVsync );
|
|
|
|
MemWrite( &item->frameVsync.id, crtc );
|
|
|
|
MemWrite( &item->frameVsync.time, t0 );
|
|
|
|
TracyLfqCommit;
|
|
|
|
}
|
2019-08-17 15:17:54 +00:00
|
|
|
|
2021-12-05 11:02:28 +00:00
|
|
|
rbPos += hdr.size;
|
|
|
|
if( rbPos == end[sel] )
|
|
|
|
{
|
2022-08-18 11:59:56 +00:00
|
|
|
memmove( active+selPos, active+selPos+1, sizeof(*active) * ( activeNum - selPos - 1 ) );
|
2021-12-05 11:02:28 +00:00
|
|
|
activeNum--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos[sel] = rbPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for( int i=0; i<ctxBufNum; i++ )
|
2019-08-23 22:42:00 +00:00
|
|
|
{
|
2022-08-17 23:08:22 +00:00
|
|
|
if( end[i] != 0 ) ringArray[ctxBufferIdx + i].Advance( end[i] );
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-20 13:49:40 +00:00
|
|
|
}
|
2021-12-05 11:02:28 +00:00
|
|
|
if( !traceActive.load( std::memory_order_relaxed ) ) break;
|
|
|
|
if( !hadData )
|
2020-03-10 17:56:49 +00:00
|
|
|
{
|
2022-06-16 12:24:35 +00:00
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
|
2020-03-10 17:56:49 +00:00
|
|
|
}
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 23:08:22 +00:00
|
|
|
for( int i=0; i<numBuffers; i++ ) ringArray[i].~RingBuffer();
|
|
|
|
tracy_free_fast( ringArray );
|
2019-08-23 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 20:20:30 +00:00
|
|
|
void SysTraceGetExternalName( uint64_t thread, const char*& threadName, const char*& name )
|
2019-08-16 17:22:23 +00:00
|
|
|
{
|
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';
|
2021-10-22 20:20:30 +00:00
|
|
|
threadName = CopyString( buf );
|
2019-08-16 19:00:42 +00:00
|
|
|
fclose( f );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-22 20:20:30 +00:00
|
|
|
threadName = CopyString( "???", 3 );
|
2019-08-16 19:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( fn, "/proc/%" PRIu64 "/status", thread );
|
|
|
|
f = fopen( fn, "rb" );
|
|
|
|
if( f )
|
|
|
|
{
|
2021-12-21 19:28:11 +00:00
|
|
|
char* tmp = (char*)tracy_malloc_fast( 8*1024 );
|
|
|
|
const auto fsz = (ptrdiff_t)fread( tmp, 1, 8*1024, f );
|
|
|
|
fclose( f );
|
|
|
|
|
2019-08-16 19:00:42 +00:00
|
|
|
int pid = -1;
|
2021-12-21 19:28:11 +00:00
|
|
|
auto line = tmp;
|
2019-08-16 19:00:42 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if( memcmp( "Tgid:\t", line, 6 ) == 0 )
|
|
|
|
{
|
|
|
|
pid = atoi( line + 6 );
|
|
|
|
break;
|
|
|
|
}
|
2021-12-21 19:28:11 +00:00
|
|
|
while( line - tmp < fsz && *line != '\n' ) line++;
|
|
|
|
if( *line != '\n' ) break;
|
|
|
|
line++;
|
2019-08-16 19:00:42 +00:00
|
|
|
}
|
2021-12-21 19:28:11 +00:00
|
|
|
tracy_free_fast( tmp );
|
|
|
|
|
2019-08-16 19:00:42 +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-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';
|
2021-10-22 20:20:30 +00:00
|
|
|
name = CopyStringFast( buf );
|
2019-08-16 19:00:42 +00:00
|
|
|
fclose( f );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 20:20:30 +00:00
|
|
|
name = CopyStringFast( "???", 3 );
|
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
|