Skip to content

Commit

Permalink
space-time-stack: allow demangled names
Browse files Browse the repository at this point in the history
  • Loading branch information
romintomasetti committed Dec 21, 2023
1 parent 6dae155 commit e54e907
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions common/utils/demangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
#define KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP

#include <memory>
#include <string>

#if defined(__GXX_ABI_VERSION)
#define HAVE_GCC_ABI_DEMANGLE
#endif

#if defined(HAVE_GCC_ABI_DEMANGLE)
#include <cxxabi.h>
#endif // HAVE_GCC_ABI_DEMANGLE

namespace KokkosTools
{

//! Demangle @p mangledName implementation.
inline auto demangleNameImpl(const char* mangled_name)
{
int status = 0;

std::unique_ptr<char, decltype(&std::free)> 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
21 changes: 1 addition & 20 deletions profiling/simple-kernel-timer/kp_kernel_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,10 @@
#include <string>
#include <cstring>

#if defined(__GXX_ABI_VERSION)
#define HAVE_GCC_ABI_DEMANGLE
#endif

#if defined(HAVE_GCC_ABI_DEMANGLE)
#include <cxxabi.h>
#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);
Expand Down
4 changes: 3 additions & 1 deletion profiling/space-time-stack/kp_space_time_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <algorithm>
#include <cstring>

#include "common/utils/demangle.hpp"

#include "kp_core.hpp"

#if USE_MPI
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit e54e907

Please sign in to comment.