Skip to content

Commit

Permalink
PC sampling: integration test with instruction decoding (#929)
Browse files Browse the repository at this point in the history
* PC sampling: integration test with instruction decoding

* PC sampling: verifying internal and external CIDs

The PC sampling integration test has been extended
to verify internal and external correlation IDs.

* tmp solution of using Instructions as keys

* wrapper for HIP call

* PCS integration test: ld_addr as instruction id

For the sake of the integration test, use  as the
instruction identifier. To support code object unloading
and relocations, use  as the identifier
(the change in the decoder is required).

* PCS integration test: removing shared_ptr

Completely removing usage of shared pointers.

* PCS integration test: removing decoder

When a code object has been unloaded, ensure all PC samples
corresponding to that object are decoded, prior to removing
the decoder.

* PCS integration test: fixing build flags and imports

* PCS integration test: fixing labels

* PCS integration test: cmake flags fix

* PC sampling cmake labels renamed

* PCS integration test refactoring

* PCS integration test: minimize usage of raw pointers

* PCS integration test: at least one sample should be delivered.

* PC sampling lables: pc-sampling
  • Loading branch information
vlaindic authored Jul 4, 2024
1 parent 64b8f83 commit 498b1f2
Show file tree
Hide file tree
Showing 20 changed files with 2,505 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ env:
ROCM_PATH: "/opt/rocm"
GPU_TARGETS: "gfx900 gfx906 gfx908 gfx90a gfx940 gfx941 gfx942 gfx1030 gfx1100 gfx1101 gfx1102"
PATH: "/usr/bin:$PATH"
PC_SAMPLING_TESTS_REGEX: ".*pc_sampling.*"
PC_SAMPLING_TESTS_REGEX: ".*pc-sampling.*"

jobs:
core:
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ add_subdirectory(async-copy-tracing)
add_subdirectory(scratch-memory-tracing)
add_subdirectory(c-tool)
add_subdirectory(page-migration)
add_subdirectory(pc_sampling)
add_subdirectory(thread-trace)
add_subdirectory(hip-graph-tracing)

Expand Down
142 changes: 142 additions & 0 deletions tests/pc_sampling/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#
#
#
cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR)

if(NOT CMAKE_HIP_COMPILER)
find_program(
amdclangpp_EXECUTABLE
NAMES amdclang++
HINTS ${ROCM_PATH} ENV ROCM_PATH /opt/rocm
PATHS ${ROCM_PATH} ENV ROCM_PATH /opt/rocm
PATH_SUFFIXES bin llvm/bin NO_CACHE)
mark_as_advanced(amdclangpp_EXECUTABLE)

if(amdclangpp_EXECUTABLE)
set(CMAKE_HIP_COMPILER "${amdclangpp_EXECUTABLE}")
endif()
endif()

project(rocprofiler-sdk-samples-pc-sampling-integration-test LANGUAGES CXX HIP)

foreach(_TYPE DEBUG MINSIZEREL RELEASE RELWITHDEBINFO)
if("${CMAKE_HIP_FLAGS_${_TYPE}}" STREQUAL "")
set(CMAKE_HIP_FLAGS_${_TYPE} "${CMAKE_CXX_FLAGS_${_TYPE}}")
endif()
endforeach()

find_package(rocprofiler-sdk REQUIRED)

find_package(PkgConfig)

if(PkgConfig_FOUND)
set(ENV{PKG_CONFIG_SYSTEM_INCLUDE_PATH} "")
pkg_check_modules(DW libdw)

if(DW_FOUND
AND DW_INCLUDE_DIRS
AND DW_LIBRARIES)
set(libdw_INCLUDE_DIR
"${DW_INCLUDE_DIRS}"
CACHE FILEPATH "libdw include directory")
set(libdw_LIBRARY
"${DW_LIBRARIES}"
CACHE FILEPATH "libdw libraries")
endif()
endif()

if(NOT libdw_INCLUDE_DIR OR NOT libdw_LIBRARY)
find_path(
libdw_ROOT_DIR
NAMES include/elfutils/libdw.h
HINTS ${libdw_ROOT}
PATHS ${libdw_ROOT})

mark_as_advanced(libdw_ROOT_DIR)

find_path(
libdw_INCLUDE_DIR
NAMES elfutils/libdw.h
HINTS ${libdw_ROOT}
PATHS ${libdw_ROOT}
PATH_SUFFIXES include)

find_library(
libdw_LIBRARY
NAMES dw
HINTS ${libdw_ROOT}
PATHS ${libdw_ROOT}
PATH_SUFFIXES lib lib64)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(libdw DEFAULT_MSG libdw_LIBRARY libdw_INCLUDE_DIR)

if(libdw_FOUND AND NOT TARGET libdw::libdw)
add_library(libdw::libdw INTERFACE IMPORTED)
if(TARGET PkgConfig::DW AND DW_FOUND)
target_link_libraries(libdw::libdw INTERFACE PkgConfig::DW)
else()
target_link_libraries(libdw::libdw INTERFACE ${libdw_LIBRARY})
target_include_directories(libdw::libdw SYSTEM INTERFACE ${libdw_INCLUDE_DIR})
endif()
endif()

add_library(pc-sampling-integration-test-client SHARED)
target_sources(
pc-sampling-integration-test-client
PRIVATE address_translation.cpp
address_translation.hpp
client.cpp
client.hpp
cid_retirement.cpp
cid_retirement.hpp
codeobj.cpp
codeobj.hpp
external_cid.cpp
external_cid.hpp
kernel_tracing.cpp
kernel_tracing.hpp
pcs.hpp
pcs.cpp
utils.hpp
utils.cpp)
target_link_libraries(
pc-sampling-integration-test-client
PRIVATE rocprofiler-sdk::rocprofiler-sdk rocprofiler-sdk::tests-build-flags
rocprofiler-sdk::tests-common-library amd_comgr dw)

set_source_files_properties(main.cpp PROPERTIES LANGUAGE HIP)
find_package(Threads REQUIRED)

add_executable(pc-sampling-integration-test)
target_sources(pc-sampling-integration-test PRIVATE main.cpp)
target_link_libraries(
pc-sampling-integration-test
PRIVATE pc-sampling-integration-test-client Threads::Threads
rocprofiler-sdk::tests-build-flags)

# rocprofiler_pc-sampling-integration_get_preload_env(PRELOAD_ENV
# pc-sampling-integration-test-client)
# rocprofiler_pc-sampling-integration_get_ld_library_path_env(LIBRARY_PATH_ENV)

# set(pc-sampling-integration-test-env ${PRELOAD_ENV} ${LIBRARY_PATH_ENV})

add_test(NAME pc-sampling-integration-test
COMMAND $<TARGET_FILE:pc-sampling-integration-test>)

set_tests_properties(
pc-sampling-integration-test
PROPERTIES
TIMEOUT
45
LABELS
"integration-tests;pc-sampling"
# ENVIRONMENT
# "${ROCPROFILER_MEMCHECK_PRELOAD_ENV};HSA_TOOLS_LIB=$<TARGET_FILE:rocprofiler::rocprofiler-shared-library>"
SKIP_REGULAR_EXPRESSION
"PC sampling unavailable"
ENVIRONMENT
"${pc-sampling-integration-test-env}"
FAIL_REGULAR_EXPRESSION
"${ROCPROFILER_DEFAULT_FAIL_REGEX}")
197 changes: 197 additions & 0 deletions tests/pc_sampling/address_translation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// MIT License
//
// Copyright (c) 2024 ROCm Developer Tools
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// undefine NDEBUG so asserts are implemented
#ifdef NDEBUG
# undef NDEBUG
#endif

#include "address_translation.hpp"
#include "pcs.hpp"
#include "utils.hpp"

#include <cassert>
#include <cstdio>
#include <iostream>
#include <memory>
#include <sstream>
#include <unordered_set>

namespace client
{
namespace address_translation
{
namespace
{
struct FlatProfiler
{
public:
FlatProfiler() = default;
~FlatProfiler() = default;

CodeobjAddressTranslate translator;
KernelObjectMap kernel_object_map;
FlatProfile flat_profile;
std::mutex global_mut;
};
} // namespace

// Raw pointer to prevent early destruction of static objects
FlatProfiler* flat_profiler = nullptr;

void
init()
{
flat_profiler = new FlatProfiler();
}

void
fini()
{
delete flat_profiler;
}

CodeobjAddressTranslate&
get_address_translator()
{
return flat_profiler->translator;
}

KernelObjectMap&
get_kernel_object_map()
{
return flat_profiler->kernel_object_map;
}

FlatProfile&
get_flat_profile()
{
return flat_profiler->flat_profile;
}

std::mutex&
get_global_mutex()
{
return flat_profiler->global_mut;
}

KernelObject::KernelObject(uint64_t code_object_id,
std::string kernel_name,
uint64_t begin_address,
uint64_t end_address)
: code_object_id_(code_object_id)
, kernel_name_(kernel_name)
, begin_address_(begin_address)
, end_address_(end_address)
{
auto& translator = get_address_translator();
uint64_t vaddr = begin_address;
while(vaddr < end_address)
{
auto inst = translator.get(vaddr);
vaddr += inst->size;
this->add_instruction(std::move(inst));
}
}

void
dump_flat_profile()
{
// It seems that an instruction can be part of multiple
// instances of the same kernel loaded on two different devices.
// We need to prevent counting the same instruction multiple times.
std::unordered_set<Instruction*> visited_instructions;

const auto& kernel_object_map = get_kernel_object_map();
const auto& flat_profile = get_flat_profile();

std::stringstream ss;
uint64_t samples_num = 0;
kernel_object_map.iterate_kernel_objects([&](const KernelObject* kernel_obj) {
ss << "\n====================================";
ss << "The kernel: " << kernel_obj->kernel_name()
<< " with the begin address: " << kernel_obj->begin_address()
<< " from code object with id: " << kernel_obj->code_object_id() << std::endl;
kernel_obj->iterate_instrunctions([&](const Instruction& inst) {
ss << "\t";
ss << inst.inst << "\t";
ss << inst.comment << "\t";
ss << "samples: ";
const auto* _sample_instruction = flat_profile.get_sample_instruction(inst);
if(_sample_instruction == nullptr)
ss << "0";
else
{
_sample_instruction->process([&](const SampleInstruction& sample_instruction) {
ss << sample_instruction.sample_count();
// Assure that each instruction is counted once.
if(visited_instructions.count(sample_instruction.inst()) == 0)
{
samples_num += sample_instruction.sample_count();
visited_instructions.insert(sample_instruction.inst());
}

if(sample_instruction.exec_mask_counts().size() <= 1)
{
ss << ", exec_mask: " << std::hex;
ss << sample_instruction.exec_mask_counts().begin()->first;
ss << std::dec;
assert(sample_instruction.sample_count() ==
sample_instruction.exec_mask_counts().begin()->second);
}
else
{
uint64_t num_samples_sum = 0;
// More than one exec_mask
for(auto& [exec_mask, samples_per_exec] :
sample_instruction.exec_mask_counts())
{
ss << std::endl;
ss << "\t\t"
<< "exec_mask: " << std::hex << exec_mask;
ss << "\t"
<< "samples: " << std::dec << samples_per_exec;
num_samples_sum += samples_per_exec;
ss << std::endl;
}
assert(sample_instruction.sample_count() == num_samples_sum);
}
});
}
ss << std::endl;
});
ss << "====================================\n" << std::endl;
});

ss << "The total number of decoded samples: " << samples_num << std::endl;
ss << "The total number of collected samples: " << client::pcs::total_samples_num()
<< std::endl;

*utils::get_output_stream() << ss.str() << std::endl;

assert(samples_num == client::pcs::total_samples_num());
// We expect at least one PC sample to be decoded/delivered;
assert(samples_num > 0);
}

} // namespace address_translation
} // namespace client
Loading

0 comments on commit 498b1f2

Please sign in to comment.