Skip to content

Commit e87f7ac

Browse files
jeongseok-metafacebook-github-bot
authored andcommitted
Enable profiling with Tracy profiler (#192)
Summary: This PR adds support for profiling using [Tracy](https://github.com/wolfpld/tracy). - Add CMake option, `MOMENTUM_ENABLE_PROFILING`, to enable/disable building with profiling annotations - Add CMake option, `MOMENTUM_USE_SYSTEM_TRACY`, to choose whether to use Tracy from system or fetching from the repo. - Add profile annotation macros for Tracy (currently incomplete) ## Checklist: - [x] Adheres to the [style guidelines](https://facebookincubator.github.io/momentum/docs/developer_guide/style_guide) - [x] Codebase formatted by running `pixi run lint` Pull Request resolved: #192 Test Plan: ``` # Console 1: Open Tracy GUI window pixi run tracy # Console 2: Run executable to profile. For example: pixi build test pixi shell ./build/default/cpp/release/character_solver_test ``` <img width="1271" alt="image" src="https://github.com/user-attachments/assets/1ba9a4db-237f-4cdd-aa03-eae59df76432" /> Reviewed By: yutingye Differential Revision: D68994492 Pulled By: jeongseok-meta fbshipit-source-id: 92a156641a9c4367585dcd2b5ed6e338aa79a34d
1 parent 0ec76b4 commit e87f7ac

File tree

9 files changed

+651
-1647
lines changed

9 files changed

+651
-1647
lines changed

CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,21 @@ mt_option(MOMENTUM_BUILD_IO_FBX "Build with IO FBX" OFF)
6363
mt_option(MOMENTUM_BUILD_PYMOMENTUM "Build Python binding" OFF)
6464
mt_option(MOMENTUM_BUILD_TESTING "Enable building tests" OFF)
6565
mt_option(MOMENTUM_BUILD_EXAMPLES "Enable building examples" OFF)
66+
mt_option(MOMENTUM_ENABLE_PROFILING "Enable building with profiling annotations" OFF)
6667
mt_option(MOMENTUM_ENABLE_SIMD "Enable building with SIMD instructions" ON)
6768
mt_option(MOMENTUM_USE_SYSTEM_GOOGLETEST "Use GoogleTest installed in system" OFF)
6869
mt_option(MOMENTUM_USE_SYSTEM_PYBIND11 "Use pybind11 installed in system" OFF)
6970
mt_option(MOMENTUM_USE_SYSTEM_RERUN_CPP_SDK "Use Rerun C++ SDK installed in system" OFF)
71+
mt_option(MOMENTUM_USE_SYSTEM_TRACY "Use Tracy installed in system" OFF)
72+
73+
if(MOMENTUM_ENABLE_PROFILING AND CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
74+
# Force disable profiling on Mac x86 to avoid initialization failure:
75+
# "Tracy Profiler initialization failure: CPU doesn't support invariant TSC."
76+
# Defining TRACY_NO_INVARIANT_CHECK=1 or TRACY_TIMER_FALLBACK is not recommended,
77+
# as it may lead to inaccurate profiling results.
78+
message(WARNING "MOMENTUM_ENABLE_PROFILING was set to ON, but Tracy profiling is not supported on Mac x86 due to CPU incompatibility. Disabling profiling.")
79+
set(MOMENTUM_ENABLE_PROFILING OFF CACHE BOOL "Enable building with profiling annotations" FORCE)
80+
endif()
7081

7182
mt_print_options()
7283

@@ -88,6 +99,7 @@ find_package(nlohmann_json CONFIG REQUIRED)
8899
find_package(openfbx CONFIG REQUIRED)
89100
find_package(re2 MODULE REQUIRED)
90101
find_package(spdlog CONFIG REQUIRED)
102+
91103
if(MOMENTUM_USE_SYSTEM_RERUN_CPP_SDK)
92104
find_package(rerun_sdk CONFIG REQUIRED)
93105
else()
@@ -98,6 +110,26 @@ else()
98110
FetchContent_MakeAvailable(rerun_sdk)
99111
endif()
100112

113+
if(MOMENTUM_ENABLE_PROFILING)
114+
if(MOMENTUM_USE_SYSTEM_TRACY)
115+
find_package(Tracy CONFIG REQUIRED)
116+
else()
117+
include(FetchContent)
118+
FetchContent_Declare(tracy
119+
GIT_REPOSITORY https://github.com/wolfpld/tracy.git
120+
GIT_TAG v0.11.1
121+
GIT_SHALLOW TRUE
122+
GIT_PROGRESS TRUE
123+
)
124+
FetchContent_MakeAvailable(tracy)
125+
if(MSVC)
126+
target_compile_options(TracyClient PRIVATE /W0)
127+
else()
128+
target_compile_options(TracyClient PRIVATE -w)
129+
endif()
130+
endif()
131+
endif()
132+
101133
# 1st party
102134
# TODO: Make Axel as a separate project and change to find_package(axel CONFIG REQUIRED)
103135
add_subdirectory(axel)

axel/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ target_link_libraries(${target_name}
7171
drjit
7272
)
7373

74+
if(MOMENTUM_ENABLE_PROFILING)
75+
target_link_libraries(${target_name} PUBLIC Tracy::TracyClient)
76+
target_compile_definitions(${target_name} PUBLIC -DAXEL_WITH_TRACY_PROFILER=1)
77+
endif()
78+
7479
#===============================================================================
7580
# Install axel
7681
#===============================================================================

axel/axel-config.cmake.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ find_dependency(Eigen3 3.4.0 CONFIG)
1212
find_dependency(Microsoft.GSL CONFIG)
1313
find_dependency(Dispenso CONFIG)
1414

15+
if(@MOMENTUM_ENABLE_PROFILING@)
16+
find_dependency(Tracy CONFIG)
17+
endif()
18+
1519
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
1620

1721
check_required_components("@PROJECT_NAME@")

axel/axel/Profile.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,45 @@
77

88
#pragma once
99

10-
#if defined(AXEL_WITH_XR_PROFILE)
10+
#if defined(AXEL_WITH_XR_PROFILER)
1111

1212
#include <arvr/libraries/profile_redirect/annotate.hpp>
1313

14+
#elif defined(AXEL_WITH_TRACY_PROFILER)
15+
16+
#include <tracy/Tracy.hpp>
17+
#include <tracy/TracyC.h>
18+
#include <stack>
19+
20+
#define _XR_PROFILE_CONCATENATE_DETAIL(x, y) x##y
21+
#define _XR_PROFILE_CONCATENATE(x, y) _XR_PROFILE_CONCATENATE_DETAIL(x, y)
22+
#define _XR_PROFILE_MAKE_UNIQUE(x) _XR_PROFILE_CONCATENATE(x, __LINE__)
23+
24+
#define XR_PROFILE_FUNCTION() ZoneScoped
25+
#define XR_PROFILE_FUNCTION_CATEGORY(CATEGORY)
26+
#define XR_PROFILE_EVENT(NAME) ZoneNamedN(_XR_PROFILE_MAKE_UNIQUE(__tracy), NAME, true)
27+
#define XR_PROFILE_EVENT_DYNAMIC(NAME) ZoneTransientN(_XR_PROFILE_MAKE_UNIQUE(__tracy), NAME, true)
28+
#define XR_PROFILE_CATEGORY(NAME, CATEGORY)
29+
#define XR_PROFILE_PREPARE_PUSH_POP() std::stack<TracyCZoneCtx> ___tracy_xr_stack
30+
#define XR_PROFILE_PUSH(NAME) \
31+
static const struct ___tracy_source_location_data TracyConcat( \
32+
__tracy_source_location, TracyLine) = {NAME, __func__, TracyFile, (uint32_t)TracyLine, 0}; \
33+
___tracy_xr_stack.push( \
34+
___tracy_emit_zone_begin(&TracyConcat(__tracy_source_location, TracyLine), true));
35+
#define XR_PROFILE_POP() \
36+
TracyCZoneEnd(___tracy_xr_stack.top()); \
37+
___tracy_xr_stack.pop()
38+
#define XR_PROFILE_THREAD(THREAD_NAME) \
39+
{ \
40+
/* support both std::string and c strings */ \
41+
std::string threadNameStr(THREAD_NAME); \
42+
TracyCSetThreadName(threadNameStr.c_str()); \
43+
}
44+
#define XR_PROFILE_UPDATE()
45+
#define XR_PROFILE_BEGIN_FRAME() FrameMark
46+
#define XR_PROFILE_END_FRAME()
47+
#define XR_PROFILE_METADATA(NAME, DATA)
48+
1449
#else
1550

1651
#define XR_PROFILE_FUNCTION()

cmake/momentum-config.cmake.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ find_dependency(openfbx CONFIG)
2626
find_dependency(re2 MODULE)
2727
find_dependency(spdlog CONFIG)
2828

29+
if(@MOMENTUM_ENABLE_PROFILING@)
30+
find_dependency(Tracy CONFIG)
31+
endif()
32+
2933
list(REMOVE_AT CMAKE_MODULE_PATH -1)
3034

3135
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")

cmake/mt_defs.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,15 @@ function(mt_library)
243243
)
244244
target_include_directories(${_ARG_NAME} ${private_or_interface} ${_ARG_PRIVATE_INCLUDE_DIRECTORIES})
245245
target_compile_features(${_ARG_NAME} ${public_or_interface} cxx_std_17)
246+
246247
target_link_libraries(${_ARG_NAME} ${public_or_interface} ${_ARG_PUBLIC_LINK_LIBRARIES})
247248
target_link_libraries(${_ARG_NAME} ${private_or_interface} ${_ARG_PRIVATE_LINK_LIBRARIES})
249+
250+
if(MOMENTUM_ENABLE_PROFILING)
251+
target_link_libraries(${_ARG_NAME} ${public_or_interface} Tracy::TracyClient)
252+
target_compile_definitions(${_ARG_NAME} ${public_or_interface} -DMOMENTUM_WITH_TRACY_PROFILER=1)
253+
endif()
254+
248255
set_target_properties(${_ARG_NAME} PROPERTIES
249256
OUTPUT_NAME momentum_${_ARG_NAME}
250257
POSITION_INDEPENDENT_CODE ON

momentum/common/profile.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#pragma once
99

10-
#if defined(MOMENTUM_WITH_XR_PROFILE)
10+
#if defined(MOMENTUM_WITH_XR_PROFILER)
1111

1212
#include <arvr/libraries/profile_redirect/annotate.hpp>
1313

@@ -25,6 +25,41 @@
2525
#define MT_PROFILE_END_FRAME() XR_PROFILE_END_FRAME()
2626
#define MT_PROFILE_METADATA(NAME, DATA) XR_PROFILE_METADATA(NAME, DATA)
2727

28+
#elif defined(MOMENTUM_WITH_TRACY_PROFILER)
29+
30+
#include <tracy/Tracy.hpp>
31+
#include <tracy/TracyC.h>
32+
#include <stack>
33+
34+
#define _MT_PROFILE_CONCATENATE_DETAIL(x, y) x##y
35+
#define _MT_PROFILE_CONCATENATE(x, y) _MT_PROFILE_CONCATENATE_DETAIL(x, y)
36+
#define _MT_PROFILE_MAKE_UNIQUE(x) _MT_PROFILE_CONCATENATE(x, __LINE__)
37+
38+
#define MT_PROFILE_FUNCTION() ZoneScoped
39+
#define MT_PROFILE_FUNCTION_CATEGORY(CATEGORY)
40+
#define MT_PROFILE_EVENT(NAME) ZoneNamedN(_MT_PROFILE_MAKE_UNIQUE(__tracy), NAME, true)
41+
#define MT_PROFILE_EVENT_DYNAMIC(NAME) ZoneTransientN(_MT_PROFILE_MAKE_UNIQUE(__tracy), NAME, true)
42+
#define MT_PROFILE_CATEGORY(NAME, CATEGORY)
43+
#define MT_PROFILE_PREPARE_PUSH_POP() std::stack<TracyCZoneCtx> ___tracy_xr_stack
44+
#define MT_PROFILE_PUSH(NAME) \
45+
static const struct ___tracy_source_location_data TracyConcat( \
46+
__tracy_source_location, TracyLine) = {NAME, __func__, TracyFile, (uint32_t)TracyLine, 0}; \
47+
___tracy_xr_stack.push( \
48+
___tracy_emit_zone_begin(&TracyConcat(__tracy_source_location, TracyLine), true));
49+
#define MT_PROFILE_POP() \
50+
TracyCZoneEnd(___tracy_xr_stack.top()); \
51+
___tracy_xr_stack.pop()
52+
#define MT_PROFILE_THREAD(THREAD_NAME) \
53+
{ \
54+
/* support both std::string and c strings */ \
55+
std::string threadNameStr(THREAD_NAME); \
56+
TracyCSetThreadName(threadNameStr.c_str()); \
57+
}
58+
#define MT_PROFILE_UPDATE()
59+
#define MT_PROFILE_BEGIN_FRAME() FrameMark
60+
#define MT_PROFILE_END_FRAME()
61+
#define MT_PROFILE_METADATA(NAME, DATA)
62+
2863
#else
2964

3065
#define MT_PROFILE_FUNCTION()

0 commit comments

Comments
 (0)