From a6ffd65d677bf4f52aad1d1925ef757c2ea21128 Mon Sep 17 00:00:00 2001 From: "romin.tomasetti" Date: Thu, 21 Dec 2023 12:38:12 +0000 Subject: [PATCH] space-time-stack: allow demangled names --- CMakeLists.txt | 3 + common/utils/demangle.hpp | 49 ++++++++++++++++ .../simple-kernel-timer/kp_json_writer.cpp | 2 +- .../simple-kernel-timer/kp_kernel_info.h | 57 ++++--------------- profiling/simple-kernel-timer/kp_reader.cpp | 10 ++-- profiling/simple-kernel-timer/kp_shared.h | 6 +- .../space-time-stack/kp_space_time_stack.cpp | 4 +- 7 files changed, 76 insertions(+), 55 deletions(-) create mode 100644 common/utils/demangle.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e7699414..ce7024470 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_CURRENT_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..47cdfebed --- /dev/null +++ b/common/utils/demangle.hpp @@ -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 +#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 mangled_name. +inline std::string demangleName(const std::string_view mangled_name) { +#if defined(HAVE_GCC_ABI_DEMANGLE) + int status = 0; + + std::unique_ptr 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 diff --git a/profiling/simple-kernel-timer/kp_json_writer.cpp b/profiling/simple-kernel-timer/kp_json_writer.cpp index b552218c0..3d58b260a 100644 --- a/profiling/simple-kernel-timer/kp_json_writer.cpp +++ b/profiling/simple-kernel-timer/kp_json_writer.cpp @@ -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) { diff --git a/profiling/simple-kernel-timer/kp_kernel_info.h b/profiling/simple-kernel-timer/kp_kernel_info.h index 93b7871eb..8e058e13e 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); @@ -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; } @@ -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; } @@ -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); @@ -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); @@ -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)); @@ -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, @@ -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; }; diff --git a/profiling/simple-kernel-timer/kp_reader.cpp b/profiling/simple-kernel-timer/kp_reader.cpp index 45493445f..551f33d67 100644 --- a/profiling/simple-kernel-timer/kp_reader.cpp +++ b/profiling/simple-kernel-timer/kp_reader.cpp @@ -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) { @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/profiling/simple-kernel-timer/kp_shared.h b/profiling/simple-kernel-timer/kp_shared.h index d92f97128..29540ef98 100644 --- a/profiling/simple-kernel-timer/kp_shared.h +++ b/profiling/simple-kernel-timer/kp_shared.h @@ -42,9 +42,9 @@ inline bool compareKernelPerformanceInfo(KernelPerformanceInfo* left, }; inline int find_index(const std::vector& 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; } } 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(); }