diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e7699414..4743eb0d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/profiling/all) set(COMMON_HEADERS_PATH ${CMAKE_CURRENT_BINARY_DIR}/common) include_directories(${COMMON_HEADERS_PATH}) +# Allow all tools to include any file. +include_directories(${CMAKE_SOURCE_DIR}) + set(SINGLELIB_PROFILERS "" CACHE STRING "" FORCE) # Export settings diff --git a/common/utils/demangle.hpp b/common/utils/demangle.hpp new file mode 100644 index 000000000..4cac858ef --- /dev/null +++ b/common/utils/demangle.hpp @@ -0,0 +1,70 @@ +#ifndef KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP +#define KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP + +#include +#include + +#if defined(__GXX_ABI_VERSION) +#define HAVE_GCC_ABI_DEMANGLE +#endif + +#if defined(HAVE_GCC_ABI_DEMANGLE) +#include +#endif // HAVE_GCC_ABI_DEMANGLE + +namespace KokkosTools +{ + +//! Demangle @p mangledName implementation. +inline auto demangleNameImpl(const char* mangled_name) +{ + int status = 0; + + std::unique_ptr demangled_name( + abi::__cxa_demangle(mangled_name, nullptr, nullptr, &status), + &std::free + ); + + if(status != 0) demangled_name.reset(); + + return demangled_name; +} + +/** + * @brief Demangle @p mangledName. + * + * @note If the name was successfully demangled, @p mangledName + * is freed and the returned pointer must be freed by the caller. + */ +inline char* demangleName(char* mangledName) +{ +#if defined(HAVE_GCC_ABI_DEMANGLE) + auto demangled_name = demangleNameImpl(mangledName); + + if(demangled_name != nullptr) + { + std::free(mangledName); + mangledName = demangled_name.release(); + } +#endif // HAVE_GCC_ABI_DEMANGLE + return mangledName; +} + +//! @overload +inline std::string demangleName(const std::string& mangledName ) +{ +#if defined(HAVE_GCC_ABI_DEMANGLE) + auto demangled_name = demangleNameImpl(mangledName.c_str()); + + if(demangled_name != nullptr) + { + return std::string(demangled_name.get()); + } +#else + return mangledName; +#endif +} + +} // namespace KokkosTools + +#endif // KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP diff --git a/profiling/simple-kernel-timer/kp_kernel_info.h b/profiling/simple-kernel-timer/kp_kernel_info.h index 93b7871eb..4db7de8af 100644 --- a/profiling/simple-kernel-timer/kp_kernel_info.h +++ b/profiling/simple-kernel-timer/kp_kernel_info.h @@ -22,29 +22,10 @@ #include #include -#if defined(__GXX_ABI_VERSION) -#define HAVE_GCC_ABI_DEMANGLE -#endif - -#if defined(HAVE_GCC_ABI_DEMANGLE) -#include -#endif // HAVE_GCC_ABI_DEMANGLE +#include "common/utils/demangle.hpp" namespace KokkosTools::KernelTimer { -inline char* demangleName(char* kernelName) { -#if defined(HAVE_GCC_ABI_DEMANGLE) - int status = -1; - char* demangledKernelName = - abi::__cxa_demangle(kernelName, NULL, NULL, &status); - if (status == 0) { - free(kernelName); - kernelName = demangledKernelName; - } -#endif // HAVE_GCC_ABI_DEMANGLE - return kernelName; -} - inline double seconds() { struct timeval now; gettimeofday(&now, NULL); diff --git a/profiling/space-time-stack/kp_space_time_stack.cpp b/profiling/space-time-stack/kp_space_time_stack.cpp index 292021787..3ee305255 100644 --- a/profiling/space-time-stack/kp_space_time_stack.cpp +++ b/profiling/space-time-stack/kp_space_time_stack.cpp @@ -31,6 +31,8 @@ #include #include +#include "common/utils/demangle.hpp" + #include "kp_core.hpp" #if USE_MPI @@ -741,7 +743,7 @@ struct State { } void begin_frame(const char* name, StackKind kind) { - std::string name_str(name); + std::string name_str(demangleName(name)); stack_frame = stack_frame->get_child(std::move(name_str), kind); stack_frame->begin(); }