Skip to content

Commit

Permalink
Fixing TRACY_NO_EXIT on MacOS and supporting MacOS tracy builds. (ire…
Browse files Browse the repository at this point in the history
…e-org#15143)

Workaround from
wolfpld/tracy#8 (comment)

Reverts 94e7e23.
Made the workaround only kick in on MacOS where it has been tested to
work with both the UI and the command line capture tool.
  • Loading branch information
benvanik authored Oct 9, 2023
1 parent 9181525 commit 77a8741
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 46 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,8 @@ if(IREE_ENABLE_CLANG_TIDY)
endif()

if(IREE_BUILD_TRACY)
if(NOT CMAKE_SYSTEM_NAME MATCHES "Linux")
message(WARNING "Building Tracy (IREE_BUILD_TRACY) on non-Linux is unsupported and may fail below.")
if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Linux")
message(WARNING "Building Tracy (IREE_BUILD_TRACY) on non-Darwin/Linux is unsupported and may fail below.")
endif()
add_subdirectory(build_tools/third_party/tracy ${CMAKE_CURRENT_BINARY_DIR}/tracy)
if(NOT TARGET IREETracyCaptureServer)
Expand Down
103 changes: 62 additions & 41 deletions build_tools/third_party/tracy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,55 @@ find_package(Threads REQUIRED)
message(STATUS "Checking for Tracy dependencies...")
find_program(PKGCONFIG pkg-config)
if(NOT PKGCONFIG)
message(STATUS "Could not find pkg-config (on Ubuntu/Debian, 'apt install pkg-config')")
message(STATUS "Could not find pkg-config to get dependencies; you need to specify them manually or use pkg-config")
message(STATUS " Ubuntu/Debian: `apt install pkg-config`")
message(STATUS " MacOS: `brew install pkg-config`")
else()
include(FindPkgConfig)

# Deps slightly differ by platform but some are common.
pkg_check_modules(TRACY_DEPS
tbb
libzstd
)
pkg_check_modules(TRACY_CAPSTONE_DEPS
capstone
)
pkg_check_modules(TRACY_GTK_DEPS
freetype2
glfw3
gtk+-3.0
dbus-1
)
endif()


if(NOT TRACY_DEPS_FOUND)
message(STATUS "Could not find Tracy dependencies (Tracy server will not be built).")
message(STATUS "To build Tracy, install packages libzstd, and tbb (on Ubuntu/Debian, 'apt install libcapstone-dev libtbb-dev libzstd-dev')")
return()
endif()

if(NOT TRACY_CAPSTONE_DEPS_FOUND)
message(STATUS "Could not find capstone, a Tracy dependency (Tracy server will not be built).")
message(STATUS "To build Tracy, install capstone (on Ubuntu/Debian, 'apt install libcapstone-dev')")
return()
endif()

if(NOT TRACY_GTK_DEPS_FOUND)
message(STATUS
"Could not find deps required to build graphical programs: "
"Tracy graphical profiler will not be built (on Ubuntu/Debian, 'apt install libglfw3-dev libfreetype-dev libgtk-3-dev libdbus-1-dev')")
if(APPLE)
pkg_check_modules(TRACY_GUI_DEPS
freetype2
glfw3
)
else()
pkg_check_modules(TRACY_GUI_DEPS
freetype2
glfw3
gtk+-3.0
dbus-1
)
endif()

if(NOT TRACY_DEPS_FOUND)
message(STATUS "Could not find Tracy dependencies (Tracy server will not be built).")
message(STATUS "To build Tracy, install packages libzstd, and tbb:")
message(STATUS " Ubuntu/Debian: `apt install libcapstone-dev libtbb-dev libzstd-dev`")
message(STATUS " MacOS: `brew install capstone tbb zstd`")
return()
endif()

if(NOT TRACY_CAPSTONE_DEPS_FOUND)
message(STATUS "Could not find capstone, a Tracy dependency (Tracy server will not be built).")
message(STATUS "To build Tracy, install capstone or build from source:")
message(STATUS " Ubuntu/Debian: `apt install libcapstone-dev`")
message(STATUS " MacOS: `brew install capstone`")
return()
endif()

if(NOT TRACY_GUI_DEPS_FOUND)
message(STATUS "Could not find deps required to build graphical programs (Tracy graphical profiler will not be built).")
message(STATUS " Ubuntu/Debian: `apt install libglfw3-dev libfreetype-dev libgtk-3-dev libdbus-1-dev`")
message(STATUS " MacOS: `brew install glfw freetype`")
endif()
endif()

#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -94,20 +108,20 @@ endfunction()

function(setup_graphics_deps name)
target_compile_definitions(${name}
PRIVATE
DISPLAY_SERVER_X11
PRIVATE
DISPLAY_SERVER_X11
)
target_include_directories(${name}
PUBLIC
${TRACY_GTK_DEPS_INCLUDE_DIRS}
${TRACY_GUI_DEPS_INCLUDE_DIRS}
)
target_link_libraries(${name}
PRIVATE
${TRACY_GTK_DEPS_LIBRARIES}
${TRACY_GUI_DEPS_LIBRARIES}
)
target_link_directories(${name}
PRIVATE
${TRACY_GTK_DEPS_LIBRARY_DIRS}
${TRACY_GUI_DEPS_LIBRARY_DIRS}
)
endfunction()

Expand Down Expand Up @@ -175,7 +189,7 @@ target_link_libraries(IREETracyCSVExport
# Graphical frontends
#-------------------------------------------------------------------------------

if(TRACY_GTK_DEPS_FOUND)
if(TRACY_GUI_DEPS_FOUND)
#-----------------------------------------------------------------------------
# IMGUI library
#-----------------------------------------------------------------------------
Expand All @@ -188,17 +202,18 @@ if(TRACY_GTK_DEPS_FOUND)
${IMGUI_SOURCES}
)
setup_cxx_options(IREETracyIMGUI)
setup_graphics_deps(IREETracyIMGUI)

#-----------------------------------------------------------------------------
# NFD library
#-----------------------------------------------------------------------------

set(NFD_SOURCES
${TRACY_SOURCE_DIR}/nfd/nfd_portal.cpp
)
add_library(IREETracyNFD
${NFD_SOURCES}
)
if(APPLE)
set(NFD_SOURCES ${TRACY_SOURCE_DIR}/nfd/nfd_cocoa.m)
else()
set(NFD_SOURCES ${TRACY_SOURCE_DIR}/nfd/nfd_portal.cpp)
endif()
add_library(IREETracyNFD ${NFD_SOURCES})
setup_cxx_options(IREETracyNFD)
setup_graphics_deps(IREETracyNFD)

Expand All @@ -210,9 +225,7 @@ if(TRACY_GTK_DEPS_FOUND)
# Remove the source files for Wayland backend; right now we just use GLFW.
list(REMOVE_ITEM PROFILER_SRCS
"${TRACY_SOURCE_DIR}/profiler/src/BackendWayland.cpp")
add_executable(IREETracyProfiler
${PROFILER_SRCS}
)
add_executable(IREETracyProfiler ${PROFILER_SRCS})
set_target_properties(IREETracyProfiler
PROPERTIES
OUTPUT_NAME "iree-tracy-profiler"
Expand All @@ -227,4 +240,12 @@ if(TRACY_GTK_DEPS_FOUND)
IREETracyServer
${CMAKE_THREAD_LIBS_INIT}
)
if(APPLE)
target_link_libraries(IREETracyProfiler
PRIVATE
"-framework Foundation"
"-framework AppKit"
"-framework UniformTypeIdentifiers"
)
endif()
endif()
2 changes: 1 addition & 1 deletion docs/developers/developing_iree/profiling_with_tracy.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ brew install capstone
Install other dependencies:

```shell
brew install glfw freetype
brew install pkg-config glfw freetype tbb zstd
```

## Build the Tracy tools
Expand Down
18 changes: 18 additions & 0 deletions runtime/src/iree/base/tracing/tracy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ void IREEDbgHelpUnlock(void) { ReleaseMutex(iree_dbghelp_mutex); }

#if IREE_TRACING_FEATURES != 0

void iree_tracing_tracy_initialize() {
// No-op.
}

void iree_tracing_tracy_deinitialize() {
#if defined(IREE_PLATFORM_APPLE)
// Synchronously shut down the profiler service.
// This is required on some platforms to support TRACY_NO_EXIT=1 such as
// MacOS and iOS. It should be harmless on other platforms as it returns
// quickly if TRACY_NO_EXIT=1 is not set.
// See: https://github.com/wolfpld/tracy/issues/8
tracy::GetProfiler().RequestShutdown();
while (!tracy::GetProfiler().HasShutdownFinished()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
#endif // IREE_PLATFORM_*
}

iree_zone_id_t iree_tracing_zone_begin_impl(
const iree_tracing_location_t* src_loc, const char* name,
size_t name_length) {
Expand Down
7 changes: 5 additions & 2 deletions runtime/src/iree/base/tracing/tracy.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ typedef struct ___tracy_source_location_data iree_tracing_location_t;
(TracyCZoneCtx) { zone_id, 1 }
#endif // __cplusplus

void iree_tracing_tracy_initialize();
void iree_tracing_tracy_deinitialize();

IREE_MUST_USE_RESULT iree_zone_id_t
iree_tracing_zone_begin_impl(const iree_tracing_location_t* src_loc,
const char* name, size_t name_length);
Expand Down Expand Up @@ -185,8 +188,8 @@ void* iree_tracing_obscure_ptr(void* ptr);

#define IREE_TRACE(expr) expr

#define IREE_TRACE_APP_ENTER()
#define IREE_TRACE_APP_EXIT(exit_code)
#define IREE_TRACE_APP_ENTER() iree_tracing_tracy_initialize()
#define IREE_TRACE_APP_EXIT(exit_code) iree_tracing_tracy_deinitialize()
#define IREE_TRACE_SET_APP_INFO(value, value_length) \
___tracy_emit_message_appinfo(value, value_length)
#define IREE_TRACE_SET_THREAD_NAME(name) ___tracy_set_thread_name(name)
Expand Down

0 comments on commit 77a8741

Please sign in to comment.