Skip to content

Commit

Permalink
add support for Tracy profiler
Browse files Browse the repository at this point in the history
* add tracy client

* add Aidan's StackContext Zone

* a bit more documentation

* doc cleanup

* add HXCPP_TRACY_DISABLE_STACKS flag
  • Loading branch information
dazKind authored Sep 8, 2024
1 parent 08f88ff commit 05f8349
Show file tree
Hide file tree
Showing 79 changed files with 45,255 additions and 1 deletion.
25 changes: 24 additions & 1 deletion include/hx/StackContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define HX_STACK_CONTEXT_H

#include "QuickVec.h"
#include "../../src/hx/tracy/tracy/TracyC.h"

#ifdef HXCPP_SINGLE_THREADED_APP
#define HX_CTX_GET ::hx::gMainThreadContext
Expand Down Expand Up @@ -73,7 +74,7 @@
// Newer code will use the HX_STACKFRAME macro
#define HX_STACKFRAME(pos) ::hx::StackFrame _hx_stackframe(pos);
#define HX_GC_STACKFRAME(pos) ::hx::StackFrame _hx_stackframe(pos);

// Must record the stack state at the catch
#define HX_STACK_BEGIN_CATCH __hxcpp_stack_begin_catch();
#define HX_JUST_GC_STACKFRAME ::hx::JustGcStackFrame _hx_stackframe;
Expand Down Expand Up @@ -624,6 +625,10 @@ class StackFrame
#ifdef HXCPP_STACK_TRACE // {
const StackPosition *position;

#if defined(HXCPP_TRACY) && !defined(HXCPP_TRACY_DISABLE_STACKS)
TracyCZoneCtx tctx;
#endif

#ifdef HXCPP_STACK_LINE
// Current line number, changes during the lifetime of the stack frame.
// Only updated if HXCPP_STACK_LINE is defined.
Expand Down Expand Up @@ -657,13 +662,31 @@ class StackFrame

ctx = HX_CTX_GET;
ctx->pushFrame(this);

#if defined(HXCPP_TRACY) && !defined(HXCPP_TRACY_DISABLE_STACKS)
auto srcloc =
___tracy_alloc_srcloc(
lineNumber,
position->fileName,
strlen(position->fileName),
position->fullName,
strlen(position->fullName),
0
);

tctx = ___tracy_emit_zone_begin_alloc_callstack(srcloc, ctx->getDepth(), true);
#endif
}


// The destructor automatically removes the StackFrame from the list of
// stack frames for the current thread
~StackFrame()
{
#if defined(HXCPP_TRACY) && !defined(HXCPP_TRACY_DISABLE_STACKS)
___tracy_emit_zone_end(tctx);
#endif

ctx->popFrame(this);
}

Expand Down
4 changes: 4 additions & 0 deletions include/hxcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
#define HXCPP_ALIGN_ALLOC
#endif

#if defined(HXCPP_TRACY)
#define TRACY_ENABLE
#include "../src/hx/tracy/tracy/Tracy.hpp"
#endif


// Some compilers are over-enthusiastic about what they #define ...
Expand Down
35 changes: 35 additions & 0 deletions src/hx/tracy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
To activate the tracy integration, compile your app with:

```
-D HXCPP_TRACY
-D HXCPP_STACK_TRACE
```

and use the following call in your mainloop:

```
#if defined(HXCPP_TRACY)
untyped __cpp__('FrameMark');
#endif
```

> Note: The Tracy-Headers are included with `hxcpp.h` and the Tracy Macros should be available everywhere in your code.
> Alternatively you can use this simple extern class in your project: https://gist.github.com/dazKind/b36475c0846491aefdfb12c5f831daba

Then start Tracy listening on localhost and start your hxcpp-made exe.


## Some notes about the integration
### Haxe-Code
We hook Tracy into hxcpp's `hx::StackContext` and this way it spreads over all generated cpp code catching & measuring all the callstacks.

There are however native parts of hxcpp that wont be visible by default in Tracy (bc there are no ZoneScopes).
> Note: There is a exception in the GC-Code, so it becomes visible for us.
### externs
The same is true about externs you might be using in your project. If you want to make these visible, you need to add Tracy's C-Macros yourself in the extern's c/cpp-code. You should be able to include Tracy's client headers or if it's cpp-files you can just `#include <hxcpp.h>`, which you need for precompiled headers anyway.

### externs with static/dynamic libs
Another special case are static or dynamic libs your externs might be using. For these you will have to make more changes that are beyond the scope of this doc here, please refer to Tracy's official documentation over here: https://github.com/wolfpld/tracy/releases/latest/download/tracy.pdf
62 changes: 62 additions & 0 deletions src/hx/tracy/TracyClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Tracy profiler
// ----------------
//
// For fast integration, compile and
// link with this source file (and none
// other) in your executable (or in the
// main DLL / shared object on multi-DLL
// projects).
//

// Define TRACY_ENABLE to enable profiler.

#include "hxcpp.h"
#include "common/TracySystem.cpp"

#ifdef TRACY_ENABLE

#ifdef _MSC_VER
# pragma warning(push, 0)
#endif

#include "common/tracy_lz4.cpp"
#include "client/TracyProfiler.cpp"
#include "client/TracyCallstack.cpp"
#include "client/TracySysPower.cpp"
#include "client/TracySysTime.cpp"
#include "client/TracySysTrace.cpp"
#include "common/TracySocket.cpp"
#include "client/tracy_rpmalloc.cpp"
#include "client/TracyDxt1.cpp"
#include "client/TracyAlloc.cpp"
#include "client/TracyOverride.cpp"
#include "client/TracyKCore.cpp"

#if defined(TRACY_HAS_CALLSTACK)
# if TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3 || TRACY_HAS_CALLSTACK == 4 || TRACY_HAS_CALLSTACK == 6
# include "libbacktrace/alloc.cpp"
# include "libbacktrace/dwarf.cpp"
# include "libbacktrace/fileline.cpp"
# include "libbacktrace/mmapio.cpp"
# include "libbacktrace/posix.cpp"
# include "libbacktrace/sort.cpp"
# include "libbacktrace/state.cpp"
# if TRACY_HAS_CALLSTACK == 4
# include "libbacktrace/macho.cpp"
# else
# include "libbacktrace/elf.cpp"
# endif
# include "common/TracyStackFrames.cpp"
# endif
#endif

#ifdef _MSC_VER
# pragma comment(lib, "ws2_32.lib")
# pragma comment(lib, "dbghelp.lib")
# pragma comment(lib, "advapi32.lib")
# pragma comment(lib, "user32.lib")
# pragma warning(pop)
#endif

#endif
43 changes: 43 additions & 0 deletions src/hx/tracy/client/TracyAlloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "../common/TracyAlloc.hpp"

#ifdef TRACY_USE_RPMALLOC

#include <atomic>

#include "../common/TracyForceInline.hpp"
#include "../common/TracyYield.hpp"

namespace tracy
{

extern thread_local bool RpThreadInitDone;
extern std::atomic<int> RpInitDone;
extern std::atomic<int> RpInitLock;

tracy_no_inline static void InitRpmallocPlumbing()
{
const auto done = RpInitDone.load( std::memory_order_acquire );
if( !done )
{
int expected = 0;
while( !RpInitLock.compare_exchange_weak( expected, 1, std::memory_order_release, std::memory_order_relaxed ) ) { expected = 0; YieldThread(); }
const auto done = RpInitDone.load( std::memory_order_acquire );
if( !done )
{
rpmalloc_initialize();
RpInitDone.store( 1, std::memory_order_release );
}
RpInitLock.store( 0, std::memory_order_release );
}
rpmalloc_thread_initialize();
RpThreadInitDone = true;
}

TRACY_API void InitRpmalloc()
{
if( !RpThreadInitDone ) InitRpmallocPlumbing();
}

}

#endif
Loading

0 comments on commit 05f8349

Please sign in to comment.