Skip to content

Commit 54af892

Browse files
dazKindAidan63
andauthored
add support for Tracy profiler (#1153)
* add support for Tracy profiler * add tracy client * add Aidan's StackContext Zone * a bit more documentation * doc cleanup * add HXCPP_TRACY_DISABLE_STACKS flag * put tracy include behind HXCPP_TRACY * move code to third party; move tracy into it's own cachable <files> section in haxe-target.xml * shift tracy telemetry into its own cpp file * fix incorrect signature * add global __hxcpp_tracy_* telemetry functions * add ctx to cppia-stackframe; fix sourcelocation's fullName * remove the dependency on haxe's stackframe context, use tracyZones vector instead * update docs * bug: fix InternalNew's parameter being used as linenumber in its HX_STACK_FRAME * fix wrong path in HX_STACK_FRAME * add HXCPP_TRACY_NO_EXIT & HXCPP_TRACY_ON_DEMAND support * added HXCPP_TRACY_INCLUDE_CALLSTACKS, use this to generate & include callstacks in the profiler zones * allocation tracking * add no-op gc alloc function to hxtelemetry implementation * Account for the same large object pointer appearing before collection * add HXCPP_TRACY_MEMORY * stack depth needs +1 * track large objects globally * eagerly free large objects * add __hxcpp_tracy_set_thread_name_and_group; disable custom GC ___tracy_source_location_data * new tracy telemetry header and hxcpp zone macro * Use hxcpp float * fix argument order in custom scope * Remove test zone function * line unique scope variable * callstack macro variant * fix HXCPP_TRACY_ZONE * zone count function * update docs & comments * update readme --------- Co-authored-by: Aidan Lee <[email protected]>
1 parent c97ab28 commit 54af892

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+45618
-10
lines changed

include/hx/GC.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#ifdef HXCPP_TELEMETRY
2525
extern void __hxt_gc_new(hx::StackContext *inStack, void* obj, int inSize, const char *inName);
26+
extern void __hxt_gc_alloc(void* obj, int inSize);
27+
extern void __hxt_gc_free_large(void* obj);
2628
#endif
2729

2830

@@ -430,6 +432,7 @@ class ImmixAllocator
430432
#endif
431433

432434
#ifdef HXCPP_TELEMETRY
435+
__hxt_gc_alloc(buffer, inSize);
433436
__hxt_gc_new((hx::StackContext *)alloc,buffer, inSize, inName);
434437
#endif
435438
return buffer;

include/hx/StackContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
// Newer code will use the HX_STACKFRAME macro
7474
#define HX_STACKFRAME(pos) ::hx::StackFrame _hx_stackframe(pos);
7575
#define HX_GC_STACKFRAME(pos) ::hx::StackFrame _hx_stackframe(pos);
76-
76+
7777
// Must record the stack state at the catch
7878
#define HX_STACK_BEGIN_CATCH __hxcpp_stack_begin_catch();
7979
#define HX_JUST_GC_STACKFRAME ::hx::JustGcStackFrame _hx_stackframe;

include/hx/TelemetryTracy.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef HX_TELEMETRY_TRACY_H
2+
#define HX_TELEMETRY_TRACY_H
3+
4+
#ifndef HXCPP_TRACY
5+
#error "Error: HXCPP_TRACY must be defined."
6+
#endif
7+
8+
#define TRACY_ENABLE
9+
#include <hxcpp.h>
10+
#include "../../project/thirdparty/tracy-0.11.1/tracy/TracyC.h"
11+
#include "../../project/thirdparty/tracy-0.11.1/tracy/Tracy.hpp"
12+
13+
#ifdef HXCPP_TRACY_MEMORY
14+
#ifdef HXCPP_GC_MOVING
15+
#error "Error: HXCPP_TRACY_MEMORY is not supported when HXCPP_GC_MOVING is active."
16+
#endif
17+
#ifdef HXCPP_GC_GENERATIONAL
18+
#error "Error: HXCPP_TRACY_MEMORY is not supported when HXCPP_GC_GENERATIONAL is active."
19+
#endif
20+
#endif
21+
22+
#ifdef HXCPP_TRACY_INCLUDE_CALLSTACKS
23+
#define HXCPP_TRACY_ZONE(name) \
24+
::hx::strbuf TracyConcat(_hx_tracy_str_buffer, TracyLine); \
25+
int TracyConcat(_hx_tracy_str_length, TracyLine); \
26+
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)); \
27+
::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());
28+
#else
29+
#define HXCPP_TRACY_ZONE(name) \
30+
::hx::strbuf TracyConcat(_hx_tracy_str_buffer, TracyLine); \
31+
int TracyConcat(_hx_tracy_str_length, TracyLine); \
32+
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)); \
33+
::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));
34+
#endif
35+
36+
void __hxcpp_tracy_framemark();
37+
void __hxcpp_tracy_plot(::String name, ::Float val);
38+
void __hxcpp_tracy_plot_config(::String name, uint8_t format, bool step, bool fill, int color);
39+
void __hxcpp_tracy_message(::String msg, int color);
40+
void __hxcpp_tracy_message_app_info(::String info);
41+
void __hxcpp_tracy_set_thread_name_and_group(String name, int groupHint);
42+
int __hxcpp_tracy_get_zone_count();
43+
44+
#endif

include/hxcpp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
#endif
7676

7777

78-
7978
// Some compilers are over-enthusiastic about what they #define ...
8079
//#ifdef NULL
8180
//#undef NULL
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
## How to use
3+
4+
To activate the tracy integration, compile your app with:
5+
6+
```
7+
-D HXCPP_TRACY
8+
-D HXCPP_TELEMETRY
9+
-D HXCPP_STACK_TRACE
10+
-D HXCPP_STACK_LINE
11+
```
12+
13+
and use the following call in your mainloop:
14+
15+
```
16+
cpp.vm.tracy.TracyProfiler.frameMark();
17+
```
18+
19+
Then start either Tracy's UI-App or cmdline server listening on localhost and start your hxcpp-made binary.
20+
21+
22+
## Some notes about the integration
23+
24+
### Haxe-Code
25+
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.
26+
27+
There are however native parts of hxcpp that wont be visible by default in Tracy (bc there are no ZoneScopes).
28+
29+
> Note: Exceptions are in a few spots in the GC-Code, so GC becomes visible for us.
30+
31+
> Note: Hxcpp's native calls will become visible if you use the option to capture callstacks.
32+
33+
> 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.
34+
35+
### externs
36+
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
37+
38+
### externs with static/dynamic libs
39+
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
40+
41+
## Optional Features
42+
43+
### Memory Profiling
44+
45+
The following define adds tracking (de-)allocations of hxcpp's small & large object heap.
46+
47+
```
48+
-D HXCPP_TRACY_MEMORY
49+
```
50+
51+
### Capture Callstacks
52+
53+
By default we only track zones. If you wanna inspect the actual callstack per zone, you should use the following define:
54+
55+
```
56+
-D HXCPP_TRACY_INCLUDE_CALLSTACKS
57+
```
58+
59+
> Note: This will inflate the telemetry data A LOT and cost more performance. Please be aware.
60+
61+
62+
### On Demand Profiling
63+
64+
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.
65+
66+
```
67+
-D HXCPP_TRACY_ON_DEMAND
68+
```
69+
70+
### Short-lived Application Support
71+
72+
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.
73+
74+
```
75+
-D HXCPP_TRACY_NO_EXIT
76+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Tracy profiler
3+
// ----------------
4+
//
5+
// For fast integration, compile and
6+
// link with this source file (and none
7+
// other) in your executable (or in the
8+
// main DLL / shared object on multi-DLL
9+
// projects).
10+
//
11+
12+
// Define TRACY_ENABLE to enable profiler.
13+
14+
#include "common/TracySystem.cpp"
15+
16+
#ifdef TRACY_ENABLE
17+
18+
#ifdef _MSC_VER
19+
# pragma warning(push, 0)
20+
#endif
21+
22+
#include "common/tracy_lz4.cpp"
23+
#include "client/TracyProfiler.cpp"
24+
#include "client/TracyCallstack.cpp"
25+
#include "client/TracySysPower.cpp"
26+
#include "client/TracySysTime.cpp"
27+
#include "client/TracySysTrace.cpp"
28+
#include "common/TracySocket.cpp"
29+
#include "client/tracy_rpmalloc.cpp"
30+
#include "client/TracyDxt1.cpp"
31+
#include "client/TracyAlloc.cpp"
32+
#include "client/TracyOverride.cpp"
33+
#include "client/TracyKCore.cpp"
34+
35+
#if defined(TRACY_HAS_CALLSTACK)
36+
# if TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3 || TRACY_HAS_CALLSTACK == 4 || TRACY_HAS_CALLSTACK == 6
37+
# include "libbacktrace/alloc.cpp"
38+
# include "libbacktrace/dwarf.cpp"
39+
# include "libbacktrace/fileline.cpp"
40+
# include "libbacktrace/mmapio.cpp"
41+
# include "libbacktrace/posix.cpp"
42+
# include "libbacktrace/sort.cpp"
43+
# include "libbacktrace/state.cpp"
44+
# if TRACY_HAS_CALLSTACK == 4
45+
# include "libbacktrace/macho.cpp"
46+
# else
47+
# include "libbacktrace/elf.cpp"
48+
# endif
49+
# include "common/TracyStackFrames.cpp"
50+
# endif
51+
#endif
52+
53+
#ifdef _MSC_VER
54+
# pragma comment(lib, "ws2_32.lib")
55+
# pragma comment(lib, "dbghelp.lib")
56+
# pragma comment(lib, "advapi32.lib")
57+
# pragma comment(lib, "user32.lib")
58+
# pragma warning(pop)
59+
#endif
60+
61+
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "../common/TracyAlloc.hpp"
2+
3+
#ifdef TRACY_USE_RPMALLOC
4+
5+
#include <atomic>
6+
7+
#include "../common/TracyForceInline.hpp"
8+
#include "../common/TracyYield.hpp"
9+
10+
namespace tracy
11+
{
12+
13+
extern thread_local bool RpThreadInitDone;
14+
extern std::atomic<int> RpInitDone;
15+
extern std::atomic<int> RpInitLock;
16+
17+
tracy_no_inline static void InitRpmallocPlumbing()
18+
{
19+
const auto done = RpInitDone.load( std::memory_order_acquire );
20+
if( !done )
21+
{
22+
int expected = 0;
23+
while( !RpInitLock.compare_exchange_weak( expected, 1, std::memory_order_release, std::memory_order_relaxed ) ) { expected = 0; YieldThread(); }
24+
const auto done = RpInitDone.load( std::memory_order_acquire );
25+
if( !done )
26+
{
27+
rpmalloc_initialize();
28+
RpInitDone.store( 1, std::memory_order_release );
29+
}
30+
RpInitLock.store( 0, std::memory_order_release );
31+
}
32+
rpmalloc_thread_initialize();
33+
RpThreadInitDone = true;
34+
}
35+
36+
TRACY_API void InitRpmalloc()
37+
{
38+
if( !RpThreadInitDone ) InitRpmallocPlumbing();
39+
}
40+
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)