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 29, 2023
1 parent 0becae4 commit a6ffd65
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 55 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_CURRENT_SOURCE_DIR})

set(SINGLELIB_PROFILERS "" CACHE STRING "" FORCE)

# Export settings
Expand Down
49 changes: 49 additions & 0 deletions common/utils/demangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#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 mangled_name.
inline std::string demangleName(const std::string_view mangled_name) {
#if defined(HAVE_GCC_ABI_DEMANGLE)
int status = 0;

std::unique_ptr<char, decltype(&std::free)> demangled_name(
abi::__cxa_demangle(mangled_name.data(), nullptr, nullptr, &status),
&std::free);

if (status == 0) return std::string(demangled_name.get());
#endif
return std::string(mangled_name);
}

} // namespace KokkosTools

#endif // KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
2 changes: 1 addition & 1 deletion profiling/simple-kernel-timer/kp_json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main(int argc, char* argv[]) {
KernelPerformanceInfo* new_kernel =
new KernelPerformanceInfo("", PARALLEL_FOR);
if (new_kernel->readFromFile(the_file)) {
if (strlen(new_kernel->getName()) > 0) {
if (!new_kernel->getName().empty()) {
int kernelIndex = find_index(kernelInfo, new_kernel->getName());

if (kernelIndex > -1) {
Expand Down
57 changes: 12 additions & 45 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 All @@ -62,15 +43,7 @@ enum KernelExecutionType {
class KernelPerformanceInfo {
public:
KernelPerformanceInfo(std::string kName, KernelExecutionType kernelType)
: kType(kernelType) {
kernelName = (char*)malloc(sizeof(char) * (kName.size() + 1));
strcpy(kernelName, kName.c_str());

callCount = 0;
time = 0;
}

~KernelPerformanceInfo() { free(kernelName); }
: kernelName(std::move(kName)), kType(kernelType) {}

KernelExecutionType getKernelType() const { return kType; }

Expand All @@ -95,7 +68,7 @@ class KernelPerformanceInfo {

double getTimeSq() { return timeSq; }

char* getName() const { return kernelName; }
const std::string& getName() const { return kernelName; }

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

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

if (strlen(kernelName) > 0) {
free(kernelName);
}

kernelName = (char*)malloc(sizeof(char) * (kernelNameLength + 1));
copy(kernelName, &entry[nextIndex], kernelNameLength);
kernelName[kernelNameLength] = '\0';
this->kernelName = std::string(&entry[nextIndex], kernelNameLength);

kernelName = demangleName(kernelName);

Expand Down Expand Up @@ -152,7 +119,7 @@ class KernelPerformanceInfo {
}

void writeToBinaryFile(FILE* output) {
const uint32_t kernelNameLen = (uint32_t)strlen(kernelName);
const uint32_t kernelNameLen = kernelName.size();
const uint32_t recordLen = sizeof(uint32_t) + sizeof(char) * kernelNameLen +
sizeof(uint64_t) + sizeof(double) +
sizeof(double) + sizeof(uint32_t);
Expand All @@ -163,7 +130,7 @@ class KernelPerformanceInfo {
copy(&entry[nextIndex], (char*)&kernelNameLen, sizeof(kernelNameLen));
nextIndex += sizeof(kernelNameLen);

copy(&entry[nextIndex], kernelName, kernelNameLen);
copy(&entry[nextIndex], kernelName.c_str(), kernelNameLen);
nextIndex += kernelNameLen;

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

fprintf(output, "%s\"kernel-name\" : \"%s\",\n", indentBuffer,
kernelName);
kernelName.c_str());
// fprintf(output, "%s\"region\" : \"%s\",\n", indentBuffer,
// regionName);
fprintf(output, "%s\"call-count\" : %llu,\n", indentBuffer,
Expand All @@ -216,12 +183,12 @@ class KernelPerformanceInfo {
}
}

char* kernelName;
std::string kernelName;
// const char* regionName;
uint64_t callCount;
double time;
double timeSq;
double startTime;
uint64_t callCount = 0;
double time = 0;
double timeSq = 0;
double startTime = 0;
KernelExecutionType kType;
};

Expand Down
10 changes: 5 additions & 5 deletions profiling/simple-kernel-timer/kp_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main(int argc, char* argv[]) {
KernelPerformanceInfo* new_kernel =
new KernelPerformanceInfo("", PARALLEL_FOR);
if (new_kernel->readFromFile(the_file)) {
if (strlen(new_kernel->getName()) > 0) {
if (!new_kernel->getName().empty()) {
int kernelIndex = find_index(kernelInfo, new_kernel->getName());

if (kernelIndex > -1) {
Expand Down Expand Up @@ -97,7 +97,7 @@ int main(int argc, char* argv[]) {
if (kernelInfo[i]->getKernelType() != REGION) continue;
if (fixed_width)
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand All @@ -112,7 +112,7 @@ int main(int argc, char* argv[]) {
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
else
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand All @@ -139,7 +139,7 @@ int main(int argc, char* argv[]) {
if (kernelInfo[i]->getKernelType() == REGION) continue;
if (fixed_width)
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand All @@ -154,7 +154,7 @@ int main(int argc, char* argv[]) {
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
else
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand Down
6 changes: 3 additions & 3 deletions profiling/simple-kernel-timer/kp_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ inline bool compareKernelPerformanceInfo(KernelPerformanceInfo* left,
};

inline int find_index(const std::vector<KernelPerformanceInfo*>& kernels,
const char* kernelName) {
for (unsigned int i = 0; i < kernels.size(); i++) {
if (strcmp(kernels[i]->getName(), kernelName) == 0) {
const std::string& kernelName) {
for (unsigned int i = 0; i < kernels.size(); ++i) {
if (kernels[i]->getName() == kernelName) {
return i;
}
}
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 a6ffd65

Please sign in to comment.