Skip to content

Commit

Permalink
add HXCPP_TRACY_MEMORY
Browse files Browse the repository at this point in the history
  • Loading branch information
dazKind committed Sep 10, 2024
1 parent 608f1f0 commit 4de9cd5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/hx/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ StackContext::StackContext()
mIsUnwindingException = false;
#endif

#ifdef HXCPP_TELEMETRY && HXCPP_TRACY
#if HXCPP_TELEMETRY
mTelemetry = tlmCreate(this);
#endif

Expand Down
111 changes: 63 additions & 48 deletions src/hx/TelemetryTracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
#include "../../project/thirdparty/tracy-0.11.1/tracy/Tracy.hpp"
#include <vector>

#ifdef HXCPP_TRACY_MEMORY
#ifdef HXCPP_GC_MOVING
#error "Error: HXCPP_TRACY_MEMORY is not supported when HXCPP_GC_MOVING is active."
#endif
#ifdef HXCPP_GC_GENERATIONAL
#error "Error: HXCPP_TRACY_MEMORY is not supported when HXCPP_GC_GENERATIONAL is active."
#endif
#endif

namespace
{
TracyCZoneCtx gcZone;
Expand Down Expand Up @@ -66,74 +75,78 @@ void __hxt_gc_new(hx::StackContext* stack, void* obj, int inSize, const char* na

void __hxt_gc_alloc(void* obj, int inSize)
{
auto stack = hx::StackContext::getCurrent();
#ifdef HXCPP_TRACY_MEMORY
auto stack = hx::StackContext::getCurrent();

if (isLargeObject(obj))
{
// Skip multiple large object allocations since they can be recycled in-between GC collections.
for (auto i = 0; i < stack->mTelemetry->largeAllocs.size(); i++)
if (isLargeObject(obj))
{
if (stack->mTelemetry->largeAllocs[i] == obj)
// Skip multiple large object allocations since they can be recycled in-between GC collections.
for (auto i = 0; i < stack->mTelemetry->largeAllocs.size(); i++)
{
return;
if (stack->mTelemetry->largeAllocs[i] == obj)
{
return;
}
}
}

stack->mTelemetry->largeAllocs.push(obj);
stack->mTelemetry->largeAllocs.push(obj);

TracyAllocN(obj, inSize, lohName);
}
else
{
stack->mTelemetry->smallAllocs.push(obj);
TracyAllocN(obj, inSize, lohName);
}
else
{
stack->mTelemetry->smallAllocs.push(obj);

TracyAllocN(obj, inSize, sohName);
}
TracyAllocN(obj, inSize, sohName);
}
#endif
}

void __hxt_gc_realloc(void* oldObj, void* newObj, int newSize) { }

void __hxt_gc_after_mark(int byteMarkId, int endianByteMarkId)
{
for (auto&& telemetry : created)
{
hx::QuickVec<void*> smallRetained;
hx::QuickVec<void*> largeRetained;
#ifdef HXCPP_TRACY_MEMORY
for (auto&& telemetry : created)
{
hx::QuickVec<void*> smallRetained;
hx::QuickVec<void*> largeRetained;

smallRetained.safeReserveExtra(telemetry->smallAllocs.size());
largeRetained.safeReserveExtra(telemetry->largeAllocs.size());
smallRetained.safeReserveExtra(telemetry->smallAllocs.size());
largeRetained.safeReserveExtra(telemetry->largeAllocs.size());

for (auto i = 0; i < telemetry->smallAllocs.size(); i++)
{
auto ptr = telemetry->smallAllocs[i];
auto markByte = reinterpret_cast<unsigned char*>(ptr)[endianByteMarkId];
if (markByte != byteMarkId)
{
TracyFreeN(ptr, sohName);
}
else
for (auto i = 0; i < telemetry->smallAllocs.size(); i++)
{
smallRetained.push(ptr);
auto ptr = telemetry->smallAllocs[i];
auto markByte = reinterpret_cast<unsigned char*>(ptr)[endianByteMarkId];
if (markByte != byteMarkId)
{
TracyFreeN(ptr, sohName);
}
else
{
smallRetained.push(ptr);
}
}
}

for (auto i = 0; i < telemetry->largeAllocs.size(); i++)
{
auto ptr = telemetry->largeAllocs[i];
auto markByte = reinterpret_cast<unsigned char*>(ptr)[endianByteMarkId];
if (markByte != byteMarkId)
{
TracyFreeN(ptr, lohName);
}
else
for (auto i = 0; i < telemetry->largeAllocs.size(); i++)
{
largeRetained.push(ptr);
auto ptr = telemetry->largeAllocs[i];
auto markByte = reinterpret_cast<unsigned char*>(ptr)[endianByteMarkId];
if (markByte != byteMarkId)
{
TracyFreeN(ptr, lohName);
}
else
{
largeRetained.push(ptr);
}
}
}

telemetry->smallAllocs.swap(smallRetained);
telemetry->largeAllocs.swap(largeRetained);
}
telemetry->smallAllocs.swap(smallRetained);
telemetry->largeAllocs.swap(largeRetained);
}
#endif
}

void __hxt_gc_start()
Expand Down Expand Up @@ -186,6 +199,8 @@ void hx::tlmSampleEnter(hx::Telemetry* telemetry, hx::StackFrame* frame)
#if HXCPP_TRACY_INCLUDE_CALLSTACKS
// Determine depth from tracyZones vector: +1 since we are about to add one
auto depth = telemetry->tracyZones.size();

// TODO: Tracy doesnt support Callstacks outside this scope: depth >= 1 && depth < 63
telemetry->tracyZones.push_back(___tracy_emit_zone_begin_alloc_callstack(srcloc, depth, true));
#else
telemetry->tracyZones.push_back(___tracy_emit_zone_begin_alloc(srcloc, true));
Expand Down Expand Up @@ -227,4 +242,4 @@ void __hxcpp_tracy_message(String msg, int color)
void __hxcpp_tracy_message_app_info(String info)
{
::tracy::Profiler::MessageAppInfo(info.c_str(), info.length);
}
}
6 changes: 6 additions & 0 deletions toolchain/common-defines.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
<flag value='-DHXCPP_GC_DEBUG_LEVEL=${HXCPP_GC_DEBUG_LEVEL}' if="HXCPP_GC_DEBUG_LEVEL" tag="gc" />
<flag value='-DHXCPP_WINXP_COMPAT' if="HXCPP_WINXP_COMPAT" tag="haxe"/>

<!-- fwd these defines into haxe target so we check for it in telemetry -->
<flag value="-DHXCPP_GC_MOVING" if="HXCPP_GC_MOVING" tag="haxe" />
<flag value="-DHXCPP_GC_GENERATIONAL" if="HXCPP_GC_GENERATIONAL" tag="haxe" />

<!-- tracy features -->
<flag value='-DHXCPP_TRACY' if="HXCPP_TRACY" tag="haxe"/>
<flag value='-DHXCPP_TRACY_MEMORY' if="HXCPP_TRACY_MEMORY" tag="haxe"/>
<flag value='-DHXCPP_TRACY_NO_EXIT' if="HXCPP_TRACY_NO_EXIT" tag="haxe"/>
<flag value='-DHXCPP_TRACY_ON_DEMAND' if="HXCPP_TRACY_ON_DEMAND" tag="haxe"/>
<flag value='-DHXCPP_TRACY_INCLUDE_CALLSTACKS' if="HXCPP_TRACY_INCLUDE_CALLSTACKS" tag="haxe"/>
Expand Down

0 comments on commit 4de9cd5

Please sign in to comment.