diff --git a/builds/cmake/CMakeLists.txt b/builds/cmake/CMakeLists.txt index 11c3431a1e..8a172a8ad3 100644 --- a/builds/cmake/CMakeLists.txt +++ b/builds/cmake/CMakeLists.txt @@ -19,6 +19,21 @@ include(CheckSymbolExists) include(CheckCXXCompilerFlag) include(CheckCXXSourceCompiles) include(CheckLinkerFlag) +include(FetchContent) + +# Conditionally get tracy profiler +if (with-tracy) + # Define tracy version and define the source + FetchContent_Declare( + tracy + GIT_REPOSITORY https://github.com/wolfpld/tracy.git + GIT_TAG master + GIT_SHALLOW TRUE + GIT_PROGRESS TRUE + ) + # Download and make tracy available + FetchContent_MakeAvailable(tracy) +endif() set_property(GLOBAL PROPERTY USE_FOLDERS ON) @@ -148,6 +163,10 @@ set( with-tests "yes" CACHE BOOL "Compile with unit tests." ) #------------------------------------------------------------------------------ set( with-examples "yes" CACHE BOOL "Compile with examples." ) +# Implement -Dwith-tracy and declare with-tracy. +#------------------------------------------------------------------------------ +set( with-tracy "no" CACHE BOOL "Compile/Build with tracy profiler." ) + # Implement -Dwith-icu and define BOOST_HAS_ICU and output ${icu}. #------------------------------------------------------------------------------ set( with-icu "no" CACHE BOOL "Compile with International Components for Unicode." ) @@ -631,11 +650,23 @@ target_include_directories( ${CANONICAL_LIB_NAME} PRIVATE ${rt_INCLUDE_DIRS} ${icu_i18n_FOR_BUILD_INCLUDE_DIRS} ${dl_INCLUDE_DIRS} - ${secp256k1_FOR_BUILD_INCLUDE_DIRS} ) + ${secp256k1_FOR_BUILD_INCLUDE_DIRS} + # Conditionally add tracy includes + "$<$:${tracy_SOURCE_DIR}/public>" ) target_include_directories( ${CANONICAL_LIB_NAME} PUBLIC "../../include" ) +# Conditionally enable tracy profiling. +#------------------------------------------------------------------------------ +if (with-tracy) + target_compile_definitions( ${CANONICAL_LIB_NAME} PRIVATE + WITH_TRACY + TRACY_ENABLE + TRACY_ON_DEMAND + ) +endif() + # ${CANONICAL_LIB_NAME} project specific libraries noramalization for build. #------------------------------------------------------------------------------ if (BUILD_SHARED_LIBS) @@ -657,7 +688,9 @@ target_link_libraries( ${CANONICAL_LIB_NAME} ${rt_LIBRARIES} ${icu_i18n_FOR_BUILD_LIBRARIES} ${dl_LIBRARIES} - ${secp256k1_FOR_BUILD_LIBRARIES} ) + ${secp256k1_FOR_BUILD_LIBRARIES} + # Conditionally add tracy libs + "$<$:Tracy::TracyClient>" ) # Define libbitcoin-system-examples project. #------------------------------------------------------------------------------ diff --git a/include/bitcoin/system/define.hpp b/include/bitcoin/system/define.hpp index 65cd08cab9..68c121c5d8 100644 --- a/include/bitcoin/system/define.hpp +++ b/include/bitcoin/system/define.hpp @@ -37,6 +37,10 @@ // Pulls in all /system headers (except settings.hpp). #include +#ifdef WITH_TRACY + #include +#endif + #if defined(HAVE_MSC) #include #endif diff --git a/install-cmake.sh b/install-cmake.sh index ee0fd4b99c..39c5566455 100755 --- a/install-cmake.sh +++ b/install-cmake.sh @@ -17,6 +17,7 @@ # accesses this feature, so if you do not intend to # use passphrase normalization this dependency can # be avoided. +# --with-tracy Add support for tracy profiler. # --build-icu Builds ICU libraries. # --build-boost Builds Boost libraries. # --build-dir= Location of downloaded and intermediate files. @@ -232,6 +233,7 @@ display_help() display_message " accesses this feature, so if you do not intend to " display_message " use passphrase normalization this dependency can " display_message " be avoided." + display_message " --with-tracy Add support for tracy profiler." display_message " --build-icu Build ICU libraries." display_message " --build-boost Build Boost libraries." display_message " --build-secp256k1 Build libsecp256k1 libraries." @@ -262,11 +264,12 @@ parse_command_line_options() # Common project options. (--with-icu) WITH_ICU="yes";; + (--with-tracy) WITH_TRACY="yes";; # Custom build options. - (--build-icu) BUILD_ICU="yes";; - (--build-boost) BUILD_BOOST="yes";; - (--build-secp256k1) BUILD_SECP256K1="yes";; + (--build-icu) BUILD_ICU="yes";; + (--build-boost) BUILD_BOOST="yes";; + (--build-secp256k1) BUILD_SECP256K1="yes";; # Unique script options. (--build-dir=*) BUILD_DIR="${OPTION#*=}";; @@ -399,6 +402,12 @@ handle_custom_options() CUMULATIVE_FILTERED_ARGS+=" --with-icu" CUMULATIVE_FILTERED_ARGS_CMAKE+=" -Dwith-icu=yes" fi + + # Process tracy + if [[ $WITH_TRACY ]]; then + CUMULATIVE_FILTERED_ARGS+=" --with-tracy" + CUMULATIVE_FILTERED_ARGS_CMAKE+=" -Dwith-tracy=yes" + fi } remove_build_options() @@ -985,7 +994,6 @@ BOOST_FLAGS=( SECP256K1_FLAGS=( "-w") - # Define build options. #============================================================================== # Define icu options. diff --git a/src/arena.cpp b/src/arena.cpp index 1ea64cf81f..6b52a5fbcd 100644 --- a/src/arena.cpp +++ b/src/arena.cpp @@ -21,6 +21,10 @@ #include #include +#ifdef WITH_TRACY + #include +#endif + namespace libbitcoin { bool operator==(const arena& left, const arena& right) NOEXCEPT @@ -38,18 +42,36 @@ arena* default_arena::get() NOEXCEPT void* default_arena::do_allocate(size_t bytes, size_t) THROWS { + #ifdef WITH_TRACY + ZoneScopedN("default_arena::do_allocate"); + #endif + ////if (align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) //// return ::operator new(bytes, std::align_val_t{ align }); BC_PUSH_WARNING(NO_MALLOC_OR_FREE) - return std::malloc(bytes); + #ifdef WITH_TRACY + auto ptr = std::malloc(bytes); + TracyAlloc(ptr, bytes); + return ptr; + #else + return std::malloc(bytes); + #endif + BC_POP_WARNING() } void default_arena::do_deallocate(void* ptr, size_t, size_t) NOEXCEPT { + #ifdef WITH_TRACY + ZoneScopedN("default_arena::do_deallocate"); + #endif + ////if (align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) //// ::operator delete(ptr, std::align_val_t{ align }); BC_PUSH_WARNING(NO_MALLOC_OR_FREE) + #ifdef WITH_TRACY + TracyFree(ptr); + #endif std::free(ptr); BC_POP_WARNING() }