Lumenarium/src/foldhaus_debug.h

440 lines
13 KiB
C
Raw Normal View History

2020-01-02 02:41:43 +00:00
//
// File: foldhaus_debug.h
// Author: Peter Slattery
// Creation Date: 2020-01-01
//
#ifndef FOLDHAUS_DEBUG_H
2019-07-19 20:56:21 +00:00
#define SCOPE_NAME_LENGTH 256
2019-11-11 20:02:24 +00:00
struct scope_record
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
u32 NameHash;
s64 StartCycles;
s64 EndCycles;
s32 CallDepth;
};
struct collated_scope_record
{
u32 NameHash;
s64 TotalCycles;
s32 CallCount;
r32 PercentFrameTime;
r32 TotalSeconds;
r32 AverageSecondsPerCall;
};
#define SCOPE_NAME_BUFFER_LENGTH 128
struct scope_name
{
u32 Hash;
string Name;
char Buffer[SCOPE_NAME_BUFFER_LENGTH];
};
struct debug_scope_record_list
{
s32 ThreadId;
s32 Max;
s32 Count;
scope_record* Calls;
s32 CurrentScopeCallDepth;
};
#define DEBUG_FRAME_GROW_SIZE 8102
struct debug_frame
{
s64 FrameStartCycles;
s64 FrameEndCycles;
s32 ScopeNamesMax;
scope_name* ScopeNamesHash;
s32 ThreadCount;
debug_scope_record_list* ThreadCalls;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
s32 CollatedScopesMax;
collated_scope_record* CollatedScopes;
2019-07-19 20:56:21 +00:00
};
2019-11-11 20:02:24 +00:00
#define FRAME_VIEW_PROFILER 0
#define FRAME_VIEW_SCOPE_LIST 1
2019-07-19 20:56:21 +00:00
struct debug_interface
{
b32 ShowCameraMouse;
b32 ShowTrackedScopes;
b32 RenderSculpture;
b32 SendSACNData;
2019-11-11 20:02:24 +00:00
s32 FrameView;
2019-07-19 20:56:21 +00:00
};
2019-11-11 20:02:24 +00:00
typedef s32 debug_get_thread_id();
2019-07-19 20:56:21 +00:00
typedef s64 debug_timing_proc();
2019-11-11 20:02:24 +00:00
typedef u8* debug_alloc(s32 ElementSize, s32 ElementCount);
typedef u8* debug_realloc(u8* Memory, s32 OldSize, s32 NewSize);
2019-07-19 20:56:21 +00:00
#define HISTOGRAM_DEPTH 10
struct debug_histogram_entry
{
char ScopeName_[SCOPE_NAME_LENGTH];
string ScopeName;
u32 PerFrame_Cycles[HISTOGRAM_DEPTH];
u32 PerFrame_CallCount[HISTOGRAM_DEPTH];
s32 CurrentFrame;
// NOTE(Peter): Cached Values, recalculated ever frame
u32 Average_Cycles;
u32 Average_CallCount;
u32 Total_Cycles;
u32 Total_CallCount;
};
2019-11-11 20:02:24 +00:00
#define DEBUG_FRAME_COUNT 128
2019-07-19 20:56:21 +00:00
struct debug_services
{
s64 PerformanceCountFrequency;
2019-11-11 20:02:24 +00:00
b32 RecordFrames;
s32 CurrentDebugFrame;
debug_frame Frames[DEBUG_FRAME_COUNT];
2019-07-19 20:56:21 +00:00
debug_interface Interface;
2019-11-11 20:02:24 +00:00
debug_get_thread_id* GetThreadId;
2019-07-19 20:56:21 +00:00
debug_timing_proc* GetWallClock;
2019-11-11 20:02:24 +00:00
debug_alloc* Alloc;
debug_realloc* Realloc;
2019-07-19 20:56:21 +00:00
};
internal void
2019-11-11 20:02:24 +00:00
InitializeDebugFrame (debug_frame* Frame, s32 NameHashMax, s32 ThreadCount, s32 ScopeCallsMax, debug_services* Services)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
Frame->ScopeNamesMax = NameHashMax;
Frame->ScopeNamesHash = (scope_name*)Services->Alloc(sizeof(scope_name), NameHashMax);
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
// NOTE(Peter): We use the same size as scope names because we're only storing a single instance
// per scope. If ScopeNamesMax can't hold all the scopes, this will never get filled and
// we should assert and recompile with a resized NameHashMax
Frame->CollatedScopesMax = NameHashMax;
Frame->CollatedScopes = (collated_scope_record*)Services->Alloc(sizeof(collated_scope_record), NameHashMax);
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
for (s32 i = 0; i < Frame->ScopeNamesMax; i++)
{
scope_name* Entry = Frame->ScopeNamesHash + i;
Entry->Name = MakeString(Entry->Buffer, 0, SCOPE_NAME_BUFFER_LENGTH);
}
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
Frame->ThreadCount = ThreadCount;
Frame->ThreadCalls = (debug_scope_record_list*)Services->Alloc(sizeof(debug_scope_record_list),
ThreadCount);
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
for (s32 i = 0; i < ThreadCount; i++)
{
Frame->ThreadCalls[i].Max = ScopeCallsMax;
Frame->ThreadCalls[i].Count = 0;
Frame->ThreadCalls[i].Calls = (scope_record*)Services->Alloc(sizeof(scope_record), ScopeCallsMax);
Frame->ThreadCalls[i].CurrentScopeCallDepth = 0;
Frame->ThreadCalls[i].ThreadId = 0;
}
2019-11-02 20:17:23 +00:00
2019-11-11 20:02:24 +00:00
for (s32 c = 0; c < Frame->CollatedScopesMax; c++)
{
Frame->CollatedScopes[c].NameHash = 0;
}
2019-07-19 20:56:21 +00:00
}
2019-11-11 20:02:24 +00:00
internal void
StartDebugFrame(debug_frame* Frame, debug_services* Services)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
Frame->FrameStartCycles = Services->GetWallClock();
for (s32 i = 0; i < Frame->ThreadCount; i++)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
Frame->ThreadCalls[i].Count = 0;
Frame->ThreadCalls[i].CurrentScopeCallDepth = 0;
}
for (s32 c = 0; c < Frame->CollatedScopesMax; c++)
{
s32 Hash = Frame->CollatedScopes[c].NameHash;
Frame->CollatedScopes[c] = {};
Frame->CollatedScopes[c].NameHash = Hash;
2019-07-19 20:56:21 +00:00
}
}
2019-11-11 20:02:24 +00:00
internal void
InitDebugServices (debug_services* Services,
s64 PerformanceCountFrequency,
debug_alloc* Alloc,
debug_realloc* Realloc,
debug_timing_proc* GetWallClock,
debug_get_thread_id* GetThreadId,
s32 ThreadCount)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
Services->Alloc = Alloc;
Services->Realloc = Realloc;
Services->GetWallClock = GetWallClock;
Services->GetThreadId = GetThreadId;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
Services->RecordFrames = true;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
Services->CurrentDebugFrame = 0;
s32 NameHashMax = 4096;
s32 ScopeCallsMax = 4096;
for (s32 i = 0; i < DEBUG_FRAME_COUNT; i++)
{
InitializeDebugFrame(&Services->Frames[i], NameHashMax, ThreadCount, ScopeCallsMax, Services);
}
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
Services->Interface.RenderSculpture = true;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
Services->PerformanceCountFrequency = PerformanceCountFrequency;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
Services->Interface.ShowCameraMouse = false;
Services->Interface.ShowTrackedScopes = false;
Services->Interface.RenderSculpture = true;
Services->Interface.SendSACNData = false;
}
internal debug_frame*
GetCurrentDebugFrame (debug_services* Services)
{
debug_frame* Result = Services->Frames + Services->CurrentDebugFrame;
2019-07-19 20:56:21 +00:00
return Result;
}
2019-11-11 20:02:24 +00:00
internal debug_frame*
GetLastDebugFrame(debug_services* Services)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
s32 Index = (Services->CurrentDebugFrame - 1);
if (Index < 0) { Index += DEBUG_FRAME_COUNT; }
debug_frame* Result = Services->Frames + Index;
return Result;
2019-07-19 20:56:21 +00:00
}
2019-11-11 20:02:24 +00:00
internal s32
GetIndexForNameHash(debug_frame* Frame, u32 NameHash)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
s32 Result = -1;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
for (s32 Offset = 0; Offset < Frame->ScopeNamesMax; Offset++)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
u32 Index = (NameHash + Offset) % Frame->ScopeNamesMax;
if ((Frame->ScopeNamesHash[Index].Hash == NameHash))
{
Result = Index;
break;
}
2019-07-19 20:56:21 +00:00
}
2019-11-11 20:02:24 +00:00
// NOTE(Peter): Its not technically wrong to return a -1 here, just means we didn't find it.
// At the time of writing however, this function is only being called in contexts where we
// know there should be an entry in the Name table, so a -1 actually indicates a problem.
Assert(Result >= 0);
return Result;
2019-07-19 20:56:21 +00:00
}
2019-11-11 20:02:24 +00:00
internal debug_scope_record_list*
GetScopeListForThreadInFrame(debug_services* Services, debug_frame* Frame)
2019-11-02 20:17:23 +00:00
{
2019-11-11 20:02:24 +00:00
debug_scope_record_list* List = 0;
s32 CurrentThreadId = Services->GetThreadId();
for (s32 Offset = 0; Offset < Frame->ThreadCount; Offset++)
2019-11-02 20:17:23 +00:00
{
2019-11-11 20:02:24 +00:00
s32 Index = (CurrentThreadId + Offset) % Frame->ThreadCount;
if (Frame->ThreadCalls[Index].ThreadId == CurrentThreadId)
{
List = Frame->ThreadCalls + Index;
break;
}
else if (Frame->ThreadCalls[Index].ThreadId == 0)
2019-11-02 20:17:23 +00:00
{
2019-11-11 20:02:24 +00:00
Frame->ThreadCalls[Index].ThreadId = CurrentThreadId;
List = Frame->ThreadCalls + Index;
break;
2019-11-02 20:17:23 +00:00
}
}
2019-11-11 20:02:24 +00:00
Assert(List);
return List;
2019-11-02 20:17:23 +00:00
}
internal void
2019-11-11 20:02:24 +00:00
CollateThreadScopeCalls (debug_scope_record_list* ThreadRecords, debug_frame* Frame)
2019-11-02 20:17:23 +00:00
{
2019-11-11 20:02:24 +00:00
for (s32 i = 0; i < ThreadRecords->Count; i++)
2019-11-02 20:17:23 +00:00
{
2019-11-11 20:02:24 +00:00
scope_record Record = ThreadRecords->Calls[i];
s32 Index = GetIndexForNameHash (Frame, Record.NameHash);
collated_scope_record* CollatedRecord = Frame->CollatedScopes + Index;
if (CollatedRecord->NameHash != Record.NameHash)
{
CollatedRecord->NameHash = Record.NameHash;
CollatedRecord->TotalCycles = 0;
CollatedRecord->CallCount = 0;
}
CollatedRecord->TotalCycles += Record.EndCycles - Record.StartCycles;
CollatedRecord->CallCount += 1;
2019-11-02 20:17:23 +00:00
}
}
2019-07-19 20:56:21 +00:00
internal void
2019-11-11 20:02:24 +00:00
EndDebugFrame (debug_services* Services)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
debug_frame* ClosingFrame = GetCurrentDebugFrame(Services);
ClosingFrame->FrameEndCycles = Services->GetWallClock();
s64 FrameTotalCycles = ClosingFrame->FrameEndCycles - ClosingFrame->FrameStartCycles;
for (s32 t = 0; t < ClosingFrame->ThreadCount; t++)
{
CollateThreadScopeCalls(ClosingFrame->ThreadCalls + t, ClosingFrame);
}
for (s32 n = 0; n < ClosingFrame->ScopeNamesMax; n++)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
if (ClosingFrame->ScopeNamesHash[n].Hash != 0)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
collated_scope_record* CollatedRecord = ClosingFrame->CollatedScopes + n;
CollatedRecord->TotalSeconds = (r32)CollatedRecord->TotalCycles / (r32)Services->PerformanceCountFrequency;
CollatedRecord->PercentFrameTime = (r32)CollatedRecord->TotalCycles / (r32)FrameTotalCycles;
CollatedRecord->AverageSecondsPerCall = CollatedRecord->TotalSeconds / CollatedRecord->CallCount;
2019-07-19 20:56:21 +00:00
}
}
2019-11-11 20:02:24 +00:00
Services->CurrentDebugFrame = (Services->CurrentDebugFrame + 1) % DEBUG_FRAME_COUNT;
StartDebugFrame(&Services->Frames[Services->CurrentDebugFrame], Services);
}
internal u32
HashScopeName (char* ScopeName)
{
// djb2 hash
u32 Hash = 5381;
char* C = ScopeName;
while(*C)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
Hash = ((Hash << 5) + Hash) + *C;
C++;
2019-07-19 20:56:21 +00:00
}
2019-11-11 20:02:24 +00:00
return Hash;
2019-07-19 20:56:21 +00:00
}
2019-11-11 20:02:24 +00:00
internal scope_name*
GetOrAddNameHashEntry(debug_frame* Frame, u32 NameHash)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
scope_name* Result = 0;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
for (s32 Offset = 0; Offset < Frame->ScopeNamesMax; Offset++)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
u32 Index = (NameHash + Offset) % Frame->ScopeNamesMax;
if ((Frame->ScopeNamesHash[Index].Hash == 0) || (Frame->ScopeNamesHash[Index].Hash == NameHash))
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
Result = Frame->ScopeNamesHash + Index;
break;
2019-07-19 20:56:21 +00:00
}
}
2019-11-11 20:02:24 +00:00
return Result;
2019-07-19 20:56:21 +00:00
}
2019-11-11 20:02:24 +00:00
internal u32
BeginTrackingScopeAndGetNameHash (debug_services* Services, char* ScopeName)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
debug_frame* CurrentFrame = GetCurrentDebugFrame(Services);
debug_scope_record_list* ThreadList = GetScopeListForThreadInFrame(Services, CurrentFrame);
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
ThreadList->CurrentScopeCallDepth++;
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
u32 NameHash = HashScopeName(ScopeName);
scope_name* Entry = GetOrAddNameHashEntry(CurrentFrame, NameHash);
if (Entry->Hash == 0) // If its new
{
Entry->Hash = NameHash;
// TODO(Peter): need to initialize all entry name strings to point at the buffer
// This will break eventually. when it does, do this ^^^^ when on startup
CopyCharArrayToString(ScopeName, &Entry->Name);
}
2019-07-19 20:56:21 +00:00
2019-11-11 20:02:24 +00:00
return NameHash;
2019-07-19 20:56:21 +00:00
}
internal void
2019-11-11 20:02:24 +00:00
PushScopeTimeOnFrame (debug_services* Services, s32 NameHash, u64 StartCycles, u64 EndCycles)
2019-07-19 20:56:21 +00:00
{
2019-11-11 20:02:24 +00:00
debug_frame* CurrentFrame = GetCurrentDebugFrame(Services);
debug_scope_record_list* ThreadList = GetScopeListForThreadInFrame(Services, CurrentFrame);
if (ThreadList->Count >= ThreadList->Max)
{
s32 CurrentSize = ThreadList->Max * sizeof(scope_record);
s32 NewMax = (ThreadList->Max + DEBUG_FRAME_GROW_SIZE);
s32 NewSize = NewMax * sizeof(scope_record);
ThreadList->Calls = (scope_record*)Services->Realloc((u8*)ThreadList->Calls, CurrentSize, NewSize);
ThreadList->Max = NewMax;
}
Assert(ThreadList->Count < ThreadList->Max);
s32 EntryIndex = ThreadList->Count++;
scope_record* Record = ThreadList->Calls + EntryIndex;
Record->NameHash = NameHash;
Record->StartCycles = StartCycles;
Record->EndCycles = EndCycles;
Record->CallDepth = --ThreadList->CurrentScopeCallDepth;
2019-07-19 20:56:21 +00:00
}
internal r32 DEBUGGetSecondsElapsed (s64 Start, s64 End, r32 PerformanceCountFrequency)
{
r32 Result = ((r32)(End - Start) / (r32)PerformanceCountFrequency);
return Result;
}
2019-11-11 20:02:24 +00:00
#ifdef DEBUG
2019-07-19 20:56:21 +00:00
#define DEBUG_TRACK_FUNCTION scope_tracker ScopeTracker (__FUNCTION__, GlobalDebugServices)
#define DEBUG_TRACK_SCOPE(name) scope_tracker ScopeTracker_##name (#name, GlobalDebugServices)
#else
#define DEBUG_TRACK_FUNCTION
#define DEBUG_TRACK_SCOPE(name)
#endif
struct scope_tracker
{
s64 ScopeStart;
2019-11-11 20:02:24 +00:00
u32 ScopeNameHash;
2019-07-19 20:56:21 +00:00
debug_services* DebugServices;
scope_tracker(char* ScopeName, debug_services* DebugServices)
{
2019-11-11 20:02:24 +00:00
if (DebugServices->RecordFrames)
{
this->ScopeNameHash = BeginTrackingScopeAndGetNameHash(DebugServices, ScopeName);
this->ScopeStart = DebugServices->GetWallClock();
this->DebugServices = DebugServices;
}
else
{
this->DebugServices = 0;
}
2019-07-19 20:56:21 +00:00
}
~scope_tracker()
{
2019-11-11 20:02:24 +00:00
if (this->DebugServices) // NOTE(Peter): If DebugServices == 0, then we werent' recording this frame
{
s64 ScopeEnd = this->DebugServices->GetWallClock();
PushScopeTimeOnFrame(this->DebugServices, this->ScopeNameHash, this->ScopeStart, ScopeEnd);
}
2019-07-19 20:56:21 +00:00
}
};
2020-01-02 02:41:43 +00:00
#define FOLDHAUS_DEBUG_H
#endif // FOLDHAUS_DEBUG_H