Skip to content

Commit da545cd

Browse files
space-time-stack: allow demangled names
1 parent 0becae4 commit da545cd

File tree

7 files changed

+78
-55
lines changed

7 files changed

+78
-55
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_CURRENT_SOURCE_DIR})
121+
119122
set(SINGLELIB_PROFILERS "" CACHE STRING "" FORCE)
120123

121124
# Export settings

common/utils/demangle.hpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//@HEADER
2+
// ************************************************************************
3+
//
4+
// Kokkos v. 4.0
5+
// Copyright (2022) National Technology & Engineering
6+
// Solutions of Sandia, LLC (NTESS).
7+
//
8+
// Under the terms of Contract DE-NA0003525 with NTESS,
9+
// the U.S. Government retains certain rights in this software.
10+
//
11+
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12+
// See https://kokkos.org/LICENSE for license information.
13+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14+
//
15+
//@HEADER
16+
17+
#ifndef KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
18+
#define KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
19+
20+
#include <memory>
21+
#include <string>
22+
23+
#if defined(__GXX_ABI_VERSION)
24+
#define HAVE_GCC_ABI_DEMANGLE
25+
#endif
26+
27+
#if defined(HAVE_GCC_ABI_DEMANGLE)
28+
#include <cxxabi.h>
29+
#endif // HAVE_GCC_ABI_DEMANGLE
30+
31+
namespace KokkosTools {
32+
33+
//! Demangle @p mangled_name.
34+
inline std::string demangleName(const std::string_view mangled_name) {
35+
#if defined(HAVE_GCC_ABI_DEMANGLE)
36+
int status = 0;
37+
38+
std::unique_ptr<char, decltype(&std::free)> demangled_name(
39+
abi::__cxa_demangle(mangled_name.data(), nullptr, nullptr, &status),
40+
&std::free);
41+
42+
if (status == 0) return std::string(demangled_name.get());
43+
#endif
44+
return std::string(mangled_name);
45+
}
46+
47+
} // namespace KokkosTools
48+
49+
#endif // KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP

profiling/simple-kernel-timer/kp_json_writer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ int main(int argc, char* argv[]) {
8383
KernelPerformanceInfo* new_kernel =
8484
new KernelPerformanceInfo("", PARALLEL_FOR);
8585
if (new_kernel->readFromFile(the_file)) {
86-
if (strlen(new_kernel->getName()) > 0) {
87-
int kernelIndex = find_index(kernelInfo, new_kernel->getName());
86+
if (!new_kernel->getName().empty()) {
87+
int kernelIndex =
88+
find_index(kernelInfo, new_kernel->getName().c_str());
8889

8990
if (kernelIndex > -1) {
9091
kernelInfo[kernelIndex]->addTime(new_kernel->getTime());

profiling/simple-kernel-timer/kp_kernel_info.h

+12-45
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);
@@ -62,15 +43,7 @@ enum KernelExecutionType {
6243
class KernelPerformanceInfo {
6344
public:
6445
KernelPerformanceInfo(std::string kName, KernelExecutionType kernelType)
65-
: kType(kernelType) {
66-
kernelName = (char*)malloc(sizeof(char) * (kName.size() + 1));
67-
strcpy(kernelName, kName.c_str());
68-
69-
callCount = 0;
70-
time = 0;
71-
}
72-
73-
~KernelPerformanceInfo() { free(kernelName); }
46+
: kernelName(std::move(kName)), kType(kernelType) {}
7447

7548
KernelExecutionType getKernelType() const { return kType; }
7649

@@ -95,7 +68,7 @@ class KernelPerformanceInfo {
9568

9669
double getTimeSq() { return timeSq; }
9770

98-
char* getName() const { return kernelName; }
71+
const std::string& getName() const { return kernelName; }
9972

10073
void addCallCount(const uint64_t newCalls) { callCount += newCalls; }
10174

@@ -112,13 +85,7 @@ class KernelPerformanceInfo {
11285
copy((char*)&kernelNameLength, &entry[nextIndex], sizeof(kernelNameLength));
11386
nextIndex += sizeof(kernelNameLength);
11487

115-
if (strlen(kernelName) > 0) {
116-
free(kernelName);
117-
}
118-
119-
kernelName = (char*)malloc(sizeof(char) * (kernelNameLength + 1));
120-
copy(kernelName, &entry[nextIndex], kernelNameLength);
121-
kernelName[kernelNameLength] = '\0';
88+
this->kernelName = std::string(&entry[nextIndex], kernelNameLength);
12289

12390
kernelName = demangleName(kernelName);
12491

@@ -152,7 +119,7 @@ class KernelPerformanceInfo {
152119
}
153120

154121
void writeToBinaryFile(FILE* output) {
155-
const uint32_t kernelNameLen = (uint32_t)strlen(kernelName);
122+
const uint32_t kernelNameLen = kernelName.size();
156123
const uint32_t recordLen = sizeof(uint32_t) + sizeof(char) * kernelNameLen +
157124
sizeof(uint64_t) + sizeof(double) +
158125
sizeof(double) + sizeof(uint32_t);
@@ -163,7 +130,7 @@ class KernelPerformanceInfo {
163130
copy(&entry[nextIndex], (char*)&kernelNameLen, sizeof(kernelNameLen));
164131
nextIndex += sizeof(kernelNameLen);
165132

166-
copy(&entry[nextIndex], kernelName, kernelNameLen);
133+
copy(&entry[nextIndex], kernelName.c_str(), kernelNameLen);
167134
nextIndex += kernelNameLen;
168135

169136
copy(&entry[nextIndex], (char*)&callCount, sizeof(callCount));
@@ -191,7 +158,7 @@ class KernelPerformanceInfo {
191158
snprintf(indentBuffer, 256, "%s ", indent);
192159

193160
fprintf(output, "%s\"kernel-name\" : \"%s\",\n", indentBuffer,
194-
kernelName);
161+
kernelName.c_str());
195162
// fprintf(output, "%s\"region\" : \"%s\",\n", indentBuffer,
196163
// regionName);
197164
fprintf(output, "%s\"call-count\" : %llu,\n", indentBuffer,
@@ -216,12 +183,12 @@ class KernelPerformanceInfo {
216183
}
217184
}
218185

219-
char* kernelName;
186+
std::string kernelName;
220187
// const char* regionName;
221-
uint64_t callCount;
222-
double time;
223-
double timeSq;
224-
double startTime;
188+
uint64_t callCount = 0;
189+
double time = 0;
190+
double timeSq = 0;
191+
double startTime = 0;
225192
KernelExecutionType kType;
226193
};
227194

profiling/simple-kernel-timer/kp_reader.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ int main(int argc, char* argv[]) {
6464
KernelPerformanceInfo* new_kernel =
6565
new KernelPerformanceInfo("", PARALLEL_FOR);
6666
if (new_kernel->readFromFile(the_file)) {
67-
if (strlen(new_kernel->getName()) > 0) {
68-
int kernelIndex = find_index(kernelInfo, new_kernel->getName());
67+
if (!new_kernel->getName().empty()) {
68+
int kernelIndex =
69+
find_index(kernelInfo, new_kernel->getName().c_str());
6970

7071
if (kernelIndex > -1) {
7172
kernelInfo[kernelIndex]->addTime(new_kernel->getTime());
@@ -97,7 +98,7 @@ int main(int argc, char* argv[]) {
9798
if (kernelInfo[i]->getKernelType() != REGION) continue;
9899
if (fixed_width)
99100
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
100-
kernelInfo[i]->getName(),
101+
kernelInfo[i]->getName().c_str(),
101102
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
102103
? (" (ParFor) ")
103104
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
@@ -112,7 +113,7 @@ int main(int argc, char* argv[]) {
112113
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
113114
else
114115
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
115-
kernelInfo[i]->getName(),
116+
kernelInfo[i]->getName().c_str(),
116117
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
117118
? (" (ParFor) ")
118119
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
@@ -139,7 +140,7 @@ int main(int argc, char* argv[]) {
139140
if (kernelInfo[i]->getKernelType() == REGION) continue;
140141
if (fixed_width)
141142
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
142-
kernelInfo[i]->getName(),
143+
kernelInfo[i]->getName().c_str(),
143144
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
144145
? (" (ParFor) ")
145146
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
@@ -154,7 +155,7 @@ int main(int argc, char* argv[]) {
154155
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
155156
else
156157
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
157-
kernelInfo[i]->getName(),
158+
kernelInfo[i]->getName().c_str(),
158159
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
159160
? (" (ParFor) ")
160161
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)

profiling/simple-kernel-timer/kp_shared.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ inline bool compareKernelPerformanceInfo(KernelPerformanceInfo* left,
4444
inline int find_index(const std::vector<KernelPerformanceInfo*>& kernels,
4545
const char* kernelName) {
4646
for (unsigned int i = 0; i < kernels.size(); i++) {
47-
if (strcmp(kernels[i]->getName(), kernelName) == 0) {
47+
if (kernels[i]->getName() == kernelName) {
4848
return i;
4949
}
5050
}

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)