Skip to content

Commit e54e907

Browse files
space-time-stack: allow demangled names
1 parent 6dae155 commit e54e907

File tree

4 files changed

+79
-21
lines changed

4 files changed

+79
-21
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/profiling/all)
116116
set(COMMON_HEADERS_PATH ${CMAKE_CURRENT_BINARY_DIR}/common)
117117
include_directories(${COMMON_HEADERS_PATH})
118118

119+
# Allow all tools to include any file.
120+
include_directories(${CMAKE_SOURCE_DIR})
121+
119122
set(SINGLELIB_PROFILERS "" CACHE STRING "" FORCE)
120123

121124
# Export settings

common/utils/demangle.hpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
2+
#define KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
3+
4+
#include <memory>
5+
#include <string>
6+
7+
#if defined(__GXX_ABI_VERSION)
8+
#define HAVE_GCC_ABI_DEMANGLE
9+
#endif
10+
11+
#if defined(HAVE_GCC_ABI_DEMANGLE)
12+
#include <cxxabi.h>
13+
#endif // HAVE_GCC_ABI_DEMANGLE
14+
15+
namespace KokkosTools
16+
{
17+
18+
//! Demangle @p mangledName implementation.
19+
inline auto demangleNameImpl(const char* mangled_name)
20+
{
21+
int status = 0;
22+
23+
std::unique_ptr<char, decltype(&std::free)> demangled_name(
24+
abi::__cxa_demangle(
25+
mangled_name, nullptr, nullptr, &status
26+
),
27+
&std::free
28+
);
29+
30+
if(status != 0) demangled_name.reset();
31+
32+
return demangled_name;
33+
}
34+
35+
/**
36+
* @brief Demangle @p mangledName.
37+
*
38+
* @note If the name was successfully demangled, @p mangledName
39+
* is freed and the returned pointer must be freed by the caller.
40+
*/
41+
inline char* demangleName(char* mangledName)
42+
{
43+
#if defined(HAVE_GCC_ABI_DEMANGLE)
44+
auto demangled_name = demangleNameImpl(mangledName);
45+
46+
if(demangled_name != nullptr)
47+
{
48+
std::free(mangledName);
49+
mangledName = demangled_name.release();
50+
}
51+
#endif // HAVE_GCC_ABI_DEMANGLE
52+
return mangledName;
53+
}
54+
55+
//! @overload
56+
inline std::string demangleName(const std::string& mangledName )
57+
{
58+
#if defined(HAVE_GCC_ABI_DEMANGLE)
59+
auto demangled_name = demangleNameImpl(mangledName.c_str());
60+
61+
if(demangled_name != nullptr)
62+
{
63+
return std::string(demangled_name.get());
64+
}
65+
#else
66+
return mangledName;
67+
#endif
68+
}
69+
70+
} // namespace KokkosTools
71+
72+
#endif // KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP

profiling/simple-kernel-timer/kp_kernel_info.h

+1-20
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,10 @@
2222
#include <string>
2323
#include <cstring>
2424

25-
#if defined(__GXX_ABI_VERSION)
26-
#define HAVE_GCC_ABI_DEMANGLE
27-
#endif
28-
29-
#if defined(HAVE_GCC_ABI_DEMANGLE)
30-
#include <cxxabi.h>
31-
#endif // HAVE_GCC_ABI_DEMANGLE
25+
#include "common/utils/demangle.hpp"
3226

3327
namespace KokkosTools::KernelTimer {
3428

35-
inline char* demangleName(char* kernelName) {
36-
#if defined(HAVE_GCC_ABI_DEMANGLE)
37-
int status = -1;
38-
char* demangledKernelName =
39-
abi::__cxa_demangle(kernelName, NULL, NULL, &status);
40-
if (status == 0) {
41-
free(kernelName);
42-
kernelName = demangledKernelName;
43-
}
44-
#endif // HAVE_GCC_ABI_DEMANGLE
45-
return kernelName;
46-
}
47-
4829
inline double seconds() {
4930
struct timeval now;
5031
gettimeofday(&now, NULL);

profiling/space-time-stack/kp_space_time_stack.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <algorithm>
3232
#include <cstring>
3333

34+
#include "common/utils/demangle.hpp"
35+
3436
#include "kp_core.hpp"
3537

3638
#if USE_MPI
@@ -741,7 +743,7 @@ struct State {
741743
}
742744

743745
void begin_frame(const char* name, StackKind kind) {
744-
std::string name_str(name);
746+
std::string name_str(demangleName(name));
745747
stack_frame = stack_frame->get_child(std::move(name_str), kind);
746748
stack_frame->begin();
747749
}

0 commit comments

Comments
 (0)