Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:khuck/xpress-apex into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
khuck committed Feb 6, 2023
2 parents 0013ef9 + bc24449 commit 9c2c8bb
Show file tree
Hide file tree
Showing 44 changed files with 7,353 additions and 196 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ if(APEX_WITH_HIP)
endif()
endif(APEX_WITH_HIP)

if(APEX_WITH_LEVEL0)
find_package(LEVEL0 REQUIRED)
if (LEVEL0_FOUND)
include_directories(${LEVEL0_INCLUDE_DIRS})
add_definitions(-DAPEX_WITH_LEVEL0)
set(LIBS ${LIBS} ${LEVEL0_LIBRARIES})
if (NOT BUILD_STATIC_EXECUTABLES)
set (CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_RPATH} ${LEVEL0_LIBRARY_DIR})
endif()
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter -Rno-debug-disables-optimization")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Rno-debug-disables-optimization")
endif()
endif(APEX_WITH_LEVEL0)

# set(APEX_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING "Configuration type (one of Debug, RelWithDebInfo, Release, MinSizeRel)" FORCE)

# Always force CMAKE_CONFIGURATION_TYPES to be the same as CMAKE_BUILD_TYPE
Expand Down Expand Up @@ -280,6 +294,10 @@ endif (DEFINED APEX_SANITIZE_THREAD AND APEX_SANITIZE_THREAD)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${APEX_SANITIZE_OPTIONS} -DAPEX_ERROR_HANDLING")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${APEX_SANITIZE_OPTIONS} -DAPEX_ERROR_HANDLING")

if (DEFINED APEX_ERROR_HANDLING AND APEX_ERROR_HANDLING)
add_definitions(-DAPEX_ERROR_HANDLING)
endif (DEFINED APEX_ERROR_HANDLING AND APEX_ERROR_HANDLING)

# ---------------------------------------------------
# Set pedantic error flags if available
# ---------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion cmake/Modules/APEX_DefaultOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ option (APEX_WITH_ACTIVEHARMONY "Enable ActiveHarmony support" FALSE)
option (APEX_WITH_BFD "Enable Binutils (BFD)support" FALSE)
option (APEX_WITH_CUDA "Enable CUDA (CUPTI) support" FALSE)
option (APEX_WITH_HIP "Enable HIP (ROCTRACER) support" FALSE)
option (APEX_WITH_LEVEL0 "Enable LEVEL0 (Intel OneAPI) support" FALSE)
option (APEX_WITH_MPI "Enable MPI support" FALSE)
option (APEX_WITH_OMPT "Enable OpenMP Tools (OMPT) support" FALSE)
option (APEX_WITH_OTF2 "Enable Open Trace Format 2 (OTF2) support" FALSE)
Expand All @@ -16,7 +17,7 @@ option (APEX_WITH_STARPU "Enable APEX StarPU support" FALSE)
option (APEX_WITH_TCMALLOC "Enable TCMalloc heap management" FALSE)
option (APEX_WITH_JEMALLOC "Enable JEMalloc heap management" FALSE)
option (APEX_WITH_LM_SENSORS "Enable LM Sensors support" FALSE)
option (APEX_WITH_PERFETTO "Enable native Perfetto trace support" TRUE)
option (APEX_WITH_PERFETTO "Enable native Perfetto trace support" FALSE)
option (APEX_BUILD_TESTS "Build APEX tests (for 'make test')" FALSE)
option (APEX_CUDA_TESTS "Build APEX CUDA tests (for 'make test')" FALSE)
option (APEX_HIP_TESTS "Build APEX HIP tests (for 'make test')" FALSE)
Expand Down
35 changes: 35 additions & 0 deletions cmake/Modules/FindLEVEL0.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# - Try to find LibLEVEL0
# Once done this will define
# LEVEL0_FOUND - System has LEVEL0
# LEVEL0_INCLUDE_DIRS - The LEVEL0 include directories
# LEVEL0_LIBRARIES - The libraries needed to use LEVEL0
# LEVEL0_DEFINITIONS - Compiler switches required for using LEVEL0

if(NOT DEFINED $LEVEL0_ROOT)
if(DEFINED ENV{LEVEL0_ROOT})
# message(" env LEVEL0_ROOT is defined as $ENV{LEVEL0_ROOT}")
set(LEVEL0_ROOT $ENV{LEVEL0_ROOT})
endif()
endif()

find_path(LEVEL0_INCLUDE_DIR NAMES level_zero/ze_api.h
HINTS ${LEVEL0_ROOT}/include /usr ${LEVEL0_ROOT})

find_library(LEVEL0_LIBRARY NAMES ze_loader
HINTS ${LEVEL0_ROOT} ${LEVEL0_ROOT}/lib64 ${LEVEL0_ROOT}/lib /usr/lib64 /usr/lib)

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LEVEL0_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(LEVEL0 DEFAULT_MSG
LEVEL0_LIBRARY LEVEL0_INCLUDE_DIR)

mark_as_advanced(LEVEL0_INCLUDE_DIR LEVEL0_LIBRARY)

if(LEVEL0_FOUND)
set(LEVEL0_LIBRARIES ${LEVEL0_LIBRARY} )
set(LEVEL0_INCLUDE_DIRS ${LEVEL0_INCLUDE_DIR})
set(LEVEL0_DIR ${LEVEL0_ROOT})
add_definitions(-DAPEX_HAVE_LEVEL0)
endif()

5 changes: 5 additions & 0 deletions src/apex/CMakeLists.standalone
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ if (CUPTI_FOUND)
SET(CUPTI_SOURCE cupti_trace.cpp)
endif(CUPTI_FOUND)

if (LEVEL0_FOUND)
SET(LEVEL0_SOURCE apex_level0.cpp)
endif(LEVEL0_FOUND)

if (ROCTRACER_FOUND)
SET(ROCTRACER_SOURCE hip_trace.cpp)
endif(ROCTRACER_FOUND)
Expand Down Expand Up @@ -92,6 +96,7 @@ endif()
SET(all_SOURCE
apex_preload.cpp
${CUPTI_SOURCE}
${LEVEL0_SOURCE}
${ROCPROFILER_SOURCE}
${ROCTRACER_SOURCE}
${NVML_SOURCE}
Expand Down
42 changes: 42 additions & 0 deletions src/apex/L0/demangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#if __has_include(<cxxabi.h>)
#define HAVE_CXXABI 1
#include <cxxabi.h>
#include <cstring>
#else
#define HAVE_CXXABI 0
#endif
#include <string>

#include "pti_assert.h"

namespace utils {

static inline std::string Demangle(const char* name) {
PTI_ASSERT(name != nullptr);

#if HAVE_CXXABI
int status = 0;
char* demangled = abi::__cxa_demangle(name, nullptr, 0, &status);
if (status != 0) {
return name;
}

constexpr const char* const prefix_to_skip = "typeinfo name for ";
const size_t prefix_to_skip_len = strlen(prefix_to_skip);
const size_t shift =
(std::strncmp(demangled, prefix_to_skip, prefix_to_skip_len) == 0) ?
prefix_to_skip_len : 0;

std::string result(demangled + shift);
free(demangled);
return result;
#else
return name;
#endif
}

} // namespace utils

#undef HAVE_CXXABI
17 changes: 17 additions & 0 deletions src/apex/L0/pti_assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//==============================================================
// Copyright © 2020 Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================

#pragma once

#ifdef NDEBUG
#undef NDEBUG
#include <assert.h>
#define NDEBUG
#else
#include <assert.h>
#endif

#define PTI_ASSERT(X) assert(X)
220 changes: 220 additions & 0 deletions src/apex/L0/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================

#pragma once

#if defined(_WIN32)
#include <windows.h>
#else
#include <unistd.h>
#include <sys/syscall.h>
#endif

#include <stdint.h>

#include <fstream>
#include <string>
#include <vector>

#include "pti_assert.h"

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

#define MAX_STR_SIZE 1024

#define BYTES_IN_MBYTES (1024 * 1024)

#define NSEC_IN_USEC 1000
#define MSEC_IN_SEC 1000
#define NSEC_IN_MSEC 1000000
#define NSEC_IN_SEC 1000000000

namespace utils {

struct Comparator {
template<typename T>
bool operator()(const T& left, const T& right) const {
if (left.second != right.second) {
return left.second > right.second;
}
return left.first > right.first;
}
};

#if defined(__gnu_linux__)

inline uint64_t GetTime(clockid_t id) {
timespec ts{0,0};
int status = clock_gettime(id, &ts);
PTI_ASSERT(status == 0);
return ts.tv_sec * NSEC_IN_SEC + ts.tv_nsec;
}

inline uint64_t ConvertClockMonotonicToRaw(uint64_t clock_monotonic) {
uint64_t raw = GetTime(CLOCK_MONOTONIC_RAW);
uint64_t monotonic = GetTime(CLOCK_MONOTONIC);
return (raw > monotonic) ?
clock_monotonic + (raw - monotonic) :
clock_monotonic - (monotonic - raw);
}

#endif

inline std::string GetFilePath(const std::string& filename) {
PTI_ASSERT(!filename.empty());

size_t pos = filename.find_last_of("/\\");
if (pos == std::string::npos) {
return "";
}

return filename.substr(0, pos + 1);
}

inline std::string GetExecutablePath() {
char buffer[MAX_STR_SIZE] = { 0 };
#if defined(_WIN32)
DWORD status = GetModuleFileNameA(nullptr, buffer, MAX_STR_SIZE);
PTI_ASSERT(status > 0);
#else
ssize_t status = readlink("/proc/self/exe", buffer, MAX_STR_SIZE);
PTI_ASSERT(status > 0);
#endif
return GetFilePath(buffer);
}

inline std::string GetExecutableName() {
char buffer[MAX_STR_SIZE] = { 0 };
#if defined(_WIN32)
DWORD status = GetModuleFileNameA(nullptr, buffer, MAX_STR_SIZE);
PTI_ASSERT(status > 0);
#else
ssize_t status = readlink("/proc/self/exe", buffer, MAX_STR_SIZE);
PTI_ASSERT(status > 0);
#endif
std::string path(buffer);
return path.substr(path.find_last_of("/\\") + 1);
}

inline std::vector<uint8_t> LoadBinaryFile(const std::string& path) {
std::vector<uint8_t> binary;
std::ifstream stream(path, std::ios::in | std::ios::binary);
if (!stream.good()) {
return binary;
}

stream.seekg(0, std::ifstream::end);
size_t size = stream.tellg();
stream.seekg(0, std::ifstream::beg);
if (size == 0) {
return binary;
}

binary.resize(size);
stream.read(reinterpret_cast<char *>(binary.data()), size);
return binary;
}

inline void SetEnv(const char* name, const char* value) {
PTI_ASSERT(name != nullptr);
PTI_ASSERT(value != nullptr);

int status = 0;
#if defined(_WIN32)
std::string str = std::string(name) + "=" + value;
status = _putenv(str.c_str());
#else
status = setenv(name, value, 1);
#endif
PTI_ASSERT(status == 0);
}

inline std::string GetEnv(const char* name) {
PTI_ASSERT(name != nullptr);
#if defined(_WIN32)
char* value = nullptr;
errno_t status = _dupenv_s(&value, nullptr, name);
PTI_ASSERT(status == 0);
if (value == nullptr) {
return std::string();
}
std::string result(value);
free(value);
return result;
#else
const char* value = getenv(name);
if (value == nullptr) {
return std::string();
}
return std::string(value);
#endif
}

inline uint32_t GetPid() {
#if defined(_WIN32)
return GetCurrentProcessId();
#else
return getpid();
#endif
}

inline uint32_t GetTid() {
#if defined(_WIN32)
return GetCurrentThreadId();
#else
#ifdef SYS_gettid
return syscall(SYS_gettid);
#else
#error "SYS_gettid is unavailable on this system"
#endif
#endif
}

inline uint64_t GetSystemTime() {
#if defined(_WIN32)
LARGE_INTEGER ticks{0};
LARGE_INTEGER frequency{0};
BOOL status = QueryPerformanceFrequency(&frequency);
PTI_ASSERT(status != 0);
status = QueryPerformanceCounter(&ticks);
PTI_ASSERT(status != 0);
return ticks.QuadPart * (NSEC_IN_SEC / frequency.QuadPart);
#else
return GetTime(CLOCK_MONOTONIC_RAW);
#endif
}

inline size_t LowerBound(const std::vector<uint64_t>& data, uint64_t value) {
size_t start = 0;
size_t end = data.size();
while (start < end) {
size_t middle = (start + end) / 2;
if (value <= data[middle]) {
end = middle;
} else {
start = middle + 1;
}
}
return start;
}

inline size_t UpperBound(const std::vector<uint64_t>& data, uint64_t value) {
size_t start = 0;
size_t end = data.size();
while (start < end) {
size_t middle = (start + end) / 2;
if (value >= data[middle]) {
start = middle + 1;
} else {
end = middle;
}
}
return start;
}

} // namespace utils

Loading

0 comments on commit 9c2c8bb

Please sign in to comment.