Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for Tracy profiler #1153

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
05f8349
add support for Tracy profiler
dazKind Sep 8, 2024
1426a88
put tracy include behind HXCPP_TRACY
dazKind Sep 8, 2024
7eb096a
move code to third party; move tracy into it's own cachable <files> s…
dazKind Sep 8, 2024
a80a01b
shift tracy telemetry into its own cpp file
Aidan63 Sep 8, 2024
4e9c48d
fix incorrect signature
Aidan63 Sep 8, 2024
1377502
add global __hxcpp_tracy_* telemetry functions
dazKind Sep 8, 2024
b722724
add ctx to cppia-stackframe; fix sourcelocation's fullName
dazKind Sep 8, 2024
b714dd3
remove the dependency on haxe's stackframe context, use tracyZones ve…
dazKind Sep 8, 2024
f25853d
update docs
dazKind Sep 8, 2024
bb57cad
bug: fix InternalNew's parameter being used as linenumber in its HX_…
dazKind Sep 8, 2024
8e5378e
fix wrong path in HX_STACK_FRAME
dazKind Sep 8, 2024
340e90b
add HXCPP_TRACY_NO_EXIT & HXCPP_TRACY_ON_DEMAND support
dazKind Sep 9, 2024
d39ecc1
added HXCPP_TRACY_INCLUDE_CALLSTACKS, use this to generate & include …
dazKind Sep 9, 2024
294b587
allocation tracking
Aidan63 Sep 10, 2024
539fc04
add no-op gc alloc function to hxtelemetry implementation
Aidan63 Sep 10, 2024
608f1f0
Account for the same large object pointer appearing before collection
Aidan63 Sep 10, 2024
4de9cd5
add HXCPP_TRACY_MEMORY
dazKind Sep 10, 2024
09df741
stack depth needs +1
dazKind Sep 10, 2024
1b53779
track large objects globally
Aidan63 Sep 11, 2024
9ecfa08
eagerly free large objects
Aidan63 Sep 12, 2024
56aaf05
add __hxcpp_tracy_set_thread_name_and_group; disable custom GC ___tra…
dazKind Sep 12, 2024
7b74420
new tracy telemetry header and hxcpp zone macro
Aidan63 Sep 12, 2024
21f8853
Use hxcpp float
Aidan63 Sep 12, 2024
510590f
fix argument order in custom scope
Aidan63 Sep 12, 2024
87b4be8
Remove test zone function
Aidan63 Sep 12, 2024
012a887
line unique scope variable
Aidan63 Sep 12, 2024
5075869
callstack macro variant
Aidan63 Sep 12, 2024
37ba18e
fix HXCPP_TRACY_ZONE
dazKind Sep 12, 2024
3a85591
zone count function
Aidan63 Sep 12, 2024
3daace9
update docs & comments
dazKind Sep 14, 2024
c6bac3d
update readme
dazKind Sep 14, 2024
e09e5d2
Merge remote-tracking branch 'upstream/master'
dazKind Sep 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/hx/GC.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#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);
extern void __hxt_gc_free_large(void* obj);
#endif


Expand Down Expand Up @@ -430,6 +432,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
2 changes: 1 addition & 1 deletion include/hx/StackContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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
44 changes: 44 additions & 0 deletions include/hx/TelemetryTracy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef HX_TELEMETRY_TRACY_H
#define HX_TELEMETRY_TRACY_H

#ifndef HXCPP_TRACY
#error "Error: HXCPP_TRACY must be defined."
#endif

#define TRACY_ENABLE
#include <hxcpp.h>
#include "../../project/thirdparty/tracy-0.11.1/tracy/TracyC.h"
#include "../../project/thirdparty/tracy-0.11.1/tracy/Tracy.hpp"

#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

#ifdef HXCPP_TRACY_INCLUDE_CALLSTACKS
#define HXCPP_TRACY_ZONE(name) \
::hx::strbuf TracyConcat(_hx_tracy_str_buffer, TracyLine); \
int TracyConcat(_hx_tracy_str_length, TracyLine); \
const char *TracyConcat(_hx_tracy_str_buffer_ptr, TracyLine) = name.utf8_str(&TracyConcat(_hx_tracy_str_buffer, TracyLine), false, &TracyConcat(_hx_tracy_str_length, TracyLine)); \
::tracy::ScopedZone TracyConcat(_hx_tracy_scoped_zone,TracyLine)(_hx_stackframe.lineNumber, _hx_stackframe.position->fileName, strlen(_hx_stackframe.position->fileName), _hx_stackframe.position->fullName, strlen(_hx_stackframe.position->fullName), TracyConcat(_hx_tracy_str_buffer_ptr, TracyLine), TracyConcat(_hx_tracy_str_length, TracyLine), __hxcpp_tracy_get_zone_count());
#else
#define HXCPP_TRACY_ZONE(name) \
::hx::strbuf TracyConcat(_hx_tracy_str_buffer, TracyLine); \
int TracyConcat(_hx_tracy_str_length, TracyLine); \
const char *TracyConcat(_hx_tracy_str_buffer_ptr, TracyLine) = name.utf8_str(&TracyConcat(_hx_tracy_str_buffer, TracyLine), false, &TracyConcat(_hx_tracy_str_length, TracyLine)); \
::tracy::ScopedZone TracyConcat(_hx_tracy_scoped_zone,TracyLine)(_hx_stackframe.lineNumber, _hx_stackframe.position->fileName, strlen(_hx_stackframe.position->fileName), _hx_stackframe.position->fullName, strlen(_hx_stackframe.position->fullName), TracyConcat(_hx_tracy_str_buffer_ptr, TracyLine), TracyConcat(_hx_tracy_str_length, TracyLine));
#endif

void __hxcpp_tracy_framemark();
void __hxcpp_tracy_plot(::String name, ::Float val);
void __hxcpp_tracy_plot_config(::String name, uint8_t format, bool step, bool fill, int color);
void __hxcpp_tracy_message(::String msg, int color);
void __hxcpp_tracy_message_app_info(::String info);
void __hxcpp_tracy_set_thread_name_and_group(String name, int groupHint);
int __hxcpp_tracy_get_zone_count();

#endif
1 change: 0 additions & 1 deletion include/hxcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
#endif



// Some compilers are over-enthusiastic about what they #define ...
//#ifdef NULL
//#undef NULL
Expand Down
76 changes: 76 additions & 0 deletions project/thirdparty/tracy-0.11.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

## How to use

To activate the tracy integration, compile your app with:

```
-D HXCPP_TRACY
-D HXCPP_TELEMETRY
-D HXCPP_STACK_TRACE
-D HXCPP_STACK_LINE
```

and use the following call in your mainloop:

```
cpp.vm.tracy.TracyProfiler.frameMark();
```

Then start either Tracy's UI-App or cmdline server listening on localhost and start your hxcpp-made binary.


## Some notes about the integration

### Haxe-Code
We integrate Tracy into hxcpp as a Telemetry option which utilizes `hx::StackPosition` and offer a set of static functions to set zones and other tracy functionality. Through this all your haxe-code will be captured in a profiler-session.

There are however native parts of hxcpp that wont be visible by default in Tracy (bc there are no ZoneScopes).

> Note: Exceptions are in a few spots in the GC-Code, so GC becomes visible for us.

> Note: Hxcpp's native calls will become visible if you use the option to capture callstacks.

> Note: We capture source-locations and their filepaths. By default these are relative to your project and thus the sourcecode preview / browsing in Tracy wont work since it expects absolute paths. To solve this you can use `-D absolute-path` in your builds.

### externs
The same is true about externs you might be using in your project. If you want to make these visible, you need to `@:include('hx/TelemetryTracy.h')` and you gain access to Tracy's C-Macros that you can use in your extern's c/cpp-code. Please refer to the official Tracy documentation: https://github.com/wolfpld/tracy/releases/latest/download/tracy.pdf

### 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

## Optional Features

### Memory Profiling

The following define adds tracking (de-)allocations of hxcpp's small & large object heap.

```
-D HXCPP_TRACY_MEMORY
```

### Capture Callstacks

By default we only track zones. If you wanna inspect the actual callstack per zone, you should use the following define:

```
-D HXCPP_TRACY_INCLUDE_CALLSTACKS
```

> Note: This will inflate the telemetry data A LOT and cost more performance. Please be aware.


### On Demand Profiling

By default this integration will start sampling & collecting telemetry with the start of your application. You can change this behavior by the following define and your app will only generate telemetry if the Tracy Profiler app is open and reachable.

```
-D HXCPP_TRACY_ON_DEMAND
```

### Short-lived Application Support

In cases where you dont have a mainloop or a very short-lived application you can use the following define to let your application stay around to complete sending telemetry data it has collected.

```
-D HXCPP_TRACY_NO_EXIT
```
61 changes: 61 additions & 0 deletions project/thirdparty/tracy-0.11.1/TracyClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// 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 "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 project/thirdparty/tracy-0.11.1/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