Skip to content

Commit

Permalink
allocation tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan63 committed Sep 10, 2024
1 parent d39ecc1 commit 294b587
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/hx/GC.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#ifdef HXCPP_TELEMETRY
extern void __hxt_gc_new(hx::StackContext *inStack, void* obj, int inSize, const char *inName);
extern void __hxt_gc_alloc(void* obj, int inSize);
#endif


Expand Down Expand Up @@ -418,6 +419,7 @@ class ImmixAllocator
#endif

#ifdef HXCPP_TELEMETRY
__hxt_gc_alloc(buffer, inSize);
__hxt_gc_new((hx::StackContext *)alloc,buffer, inSize, inName);
#endif
return buffer;
Expand Down
71 changes: 67 additions & 4 deletions src/hx/TelemetryTracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ namespace
TracyCZoneCtx gcZone;

___tracy_source_location_data gcSourceLocation = { "GC", "__hxt_gc_start", TracyFile, (uint32_t)TracyLine, 0 };

const char* sohName = "SOH";
const char* lohName = "LOH";

std::vector<hx::Telemetry*> created;

bool isLargeObject(void* ptr)
{
auto ptrHeader = reinterpret_cast<size_t>(ptr) - sizeof(int);
auto flags = *reinterpret_cast<unsigned int*>(ptrHeader);

return (flags & 0xffff) == 0;
}
}

namespace hx
Expand All @@ -18,8 +31,9 @@ namespace hx
{
public:
std::vector<TracyCZoneCtx> tracyZones;
hx::QuickVec<void*> allocs;

Telemetry() : tracyZones(0) {}
Telemetry() : tracyZones(0), allocs() {}
};
}

Expand Down Expand Up @@ -49,9 +63,54 @@ void __hxt_new_hash(void* obj, int inSize) { }

void __hxt_gc_new(hx::StackContext* stack, void* obj, int inSize, const char* name) { }

void __hxt_gc_alloc(void* obj, int inSize)
{
if (isLargeObject(obj))
{
TracyAllocN(obj, inSize, lohName);
}
else
{
TracyAllocN(obj, inSize, sohName);
}

hx::StackContext::getCurrent()->mTelemetry->allocs.push(obj);
}

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

void __hxt_gc_after_mark(int byteMarkId, int endianByteMarkId) { }
void __hxt_gc_after_mark(int byteMarkId, int endianByteMarkId)
{
for (auto&& telemetry : created)
{
hx::QuickVec<void*> retained;

retained.safeReserveExtra(telemetry->allocs.size());

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

telemetry->allocs.swap(retained);
}
}

void __hxt_gc_start()
{
Expand All @@ -65,13 +124,17 @@ void __hxt_gc_end()

hx::Telemetry* hx::tlmCreate(StackContext* stack)
{
TracyNoop;
auto obj = new hx::Telemetry();

return new hx::Telemetry();
created.push_back(obj);

return obj;
}

void hx::tlmDestroy(hx::Telemetry* telemetry)
{
created.erase(std::find(created.begin(), created.end(), telemetry));

delete telemetry;
}

Expand Down
8 changes: 8 additions & 0 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3298,6 +3298,10 @@ class GlobalAllocator
if (do_lock)
mLargeListLock.Unlock();

#ifdef HXCPP_TELEMETRY
__hxt_gc_alloc(result + 2, inSize);
#endif

return result+2;
}

Expand Down Expand Up @@ -6291,6 +6295,10 @@ class LocalAllocator : public hx::StackContext
hx::GCOnNewPointer(buffer);
#endif

#ifdef HXCPP_TELEMETRY
__hxt_gc_alloc(buffer, inSize);
#endif

return buffer;
}
if (mFraggedRows)
Expand Down

0 comments on commit 294b587

Please sign in to comment.