PC Sampling Support (#715)

* cmake formatting (cmake-format) (#188)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* source formatting (clang-format v11) (#189)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: design of the pc sampling data struct; guarding parts of code that uses ROCr marker packets

* source formatting (clang-format v11) (#191)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* cmake formatting (cmake-format) (#192)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: shadow variable fix

* pcs: fix for compiler errors reported by CI/CD

* source formatting (clang-format v11) (#193)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: docs fix; samples uses rocprofiler::rocprofiler library

* cmake formatting (cmake-format) (#195)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: client in samples folder fixed

* pcs: client requires rocprofiler package as dependency

* pcs: client uses single context

* source formatting (clang-format v11) (#196)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: client using single buffer; no buffer destroy in client

* pcs: client::setup explicitly called from the example

* pcs: rocprofiler_pc_sample_record_t updated

* pcs: fixed init of external correlation id

* source formatting (clang-format v11) (#198)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: remove outdated files; update CMakeLists

* cmake formatting (cmake-format) (#212)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: using rocprofiler_agent_id_t

* pcs: Removing trailing whitespaces

Co-authored-by: Jonathan R. Madsen <jrmadsen@users.noreply.github.com>

* source formatting (clang-format v11) (#214)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: mapping agent_id to the agent

* source formatting (clang-format v11) (#215)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: const while iterating over agents

* source formatting (clang-format v11) (#216)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: calling get_buffer instead of get_buffers

* pcs: workgroup typo

* pcs: documentation for the public PC sampling API

* pcs: queue_cb_t signature adaptation

* pcs: mocks removed

* pcs: updating HsaApiTable with HSA/ROCr PC sampling API

* pcs: querying available PC sampling configs through IOCTL

* pcs: create the PCS session in IOCTL

* pcs: first actual PC samples delivered to the rocprofiler's client :)

* pcs: works with marker packet too

* pcs: using HSA table to call pc sampling related functions

* pcs: using ioctl instead of kfd in naming

* pcs: configuration service test fixed

* pcs: sample processing test fixed

* pcs: marker packet macro wrapper removed

* pcs: marker packet is part of the rocprofiler_packet union

* pcs: one fixme added

* pcs: client that uses pc-sampling and code obj tracing

* pcs: client that supprts PC sampling and code obj tracing refactored

* pcs: show more info for each PC sample

* pcs: hex output for the samples that do not belong to the matmul kernel

* pcs: querying avail configuration happens immediately before configuring

* pcs: hsa_ven_amd_pcs_create_from_id renamed

* pcs: using hsa_stop; accessing a buffer by id from parser

* pcs: includes reworked, tests returned to life

* pcs: rocrofiler dir removed as outdated

* cmake formatting (cmake-format) (#271)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* source formatting (clang-format v11) (#272)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: some warnings fixed

* source formatting (clang-format v11) (#273)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* cmake formatting (cmake-format) (#274)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: show MI200 relevant information in the sample

* pcs: queue cb fixed; rocr.h include fixed

* source formatting (clang-format v11) (#296)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: getting hsa_agent and the doorbell_id from hsa_queue

* source formatting (clang-format v11) (#297)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: correlation ID logic fixed

* source formatting (clang-format v11) (#303)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: pure pc sampling example fixed

* source formatting (clang-format v11) (#307)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* cmake formatting (cmake-format) (#308)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: interval value if the PC sampling is already configured

* pcs: ROCPROFILER_STATUS_ERROR_PC_SAMPLING_ALREADY_CONFIGURED

New status code if another process configured PC sampling service with different configuration.
Samples are extended to consider this case and retry if it happens.

* pcs: hsa_amd_queue_get_info mocked in tests

* source formatting (clang-format v11) (#328)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs (tests): query configs after configuring service

* source formatting (clang-format v11) (#329)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: sample checks workgroup_id_* and wave_id

* source formatting (clang-format v11) (#330)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs samples: running samples on the device 0

* pcs: kfd_ioctl updated

* pcs: ioctl config struct changed fields names

* pcs: status when PC sampling is configured by another process is renamed

* pcs: HSA PC sampling API table fixed

* pcs: tmp hack to be able to use HSA pc sampling table

* source formatting (clang-format v11) (#443)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs service use CIDs generated by HIP API tracing service

* source formatting (clang-format v11) (#455)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* cmake formatting (cmake-format) (#456)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: CID manager

* pcs: explicit flush with no delivered data executes retirement logic

* source formatting (clang-format v11) (#464)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: rocprofiler_query_pc_sampling_agent_configurations docs update

* source formatting (clang-format v11) (#465)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: rocprofiler_configure_pc_sampling_service docs update

* pcs: explicit sync introduced in PCSCIDManager

* pcs: new logic for retiring CIDs in PC sampling service documented

* pcs: queue interception cb signature updated

* source formatting (clang-format v11) (#471)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: if no agents supports PC sampling, fail gracefully

* elaborating when KFD returns EBUSY and EEXIST

* pcs: the second PC sampling examples fails gracefully

* code samples use only single kernel for now

* pcs: CID manager refactored

* source formatting (clang-format v11) (#481)

Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>

* pcs: ioctl update

* source formatting (clang-format v11) (#531)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs:code sample to test PC sampling applied on concurrent kernels

* source formatting (clang-format v11) (#533)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: pc sampling strest test included

* cmake formatting (cmake-format) (#539)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* source formatting (clang-format v11) (#540)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: standalone benchmark

* cmake formatting (cmake-format) (#555)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: glance in external correlation IDs

* source formatting (clang-format v11) (#557)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* another change in ioctl interface

* pcs: update queue interceptor callbacks and samples accroding to the agent 0 version

* source formatting (clang-format v11) (#611)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: avoid running problematic PC sampling test

* pcs: guarding tests not to fail on architectures not supporting PC sampling

* source formatting (clang-format v11) (#617)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: check IOCTL version prior to each KFD call

* pcs: ioctl refactoring

* pcs: PC sampling service increases the ref_count of the correlation ID of the kernel dispatch

* cmake formatting (cmake-format) (#631)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* source formatting (clang-format v11) (#632)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: PC sampling service provides external correlation IDs

* source formatting (clang-format v11) (#644)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: use rocprofiler_dim3_t for workgrou_ip

* source formatting (clang-format v11) (#645)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: minor fixes

* pcs: updating the documentation for the pc sampling API functions

* pcs: api table and queue controller fix

* pcs: don't generate marker packets for the agent if PC sampling is not configured on it

* pcs: multi-GPU and single-GPU clients

* source formatting (clang-format v11) (#700)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: warning and errors fixed

* source formatting (clang-format v11) (#702)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: clang compiler errors and warnings fixed

* source formatting (clang-format v11) (#716)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: const reference in cid manager

* source formatting (clang-format v11) (#717)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: const & func in manager explicit

* pcs: test to cover creating PC sampling service of agent that does not exist

* pcs: generate marker packets if service is active

* source formatting (clang-format v11) (#719)

Co-authored-by: vlaindic <139573562+vlaindic@users.noreply.github.com>

* pcs: refactoring hsa_adapter; use the correlation_id->thread_idx

* Update source/lib/rocprofiler-sdk/pc_sampling/cid_manager.cpp

* Update source/lib/rocprofiler-sdk/pc_sampling/cid_manager.cpp

* Update source/lib/rocprofiler-sdk/pc_sampling/hsa_adapter.cpp

* Update source/lib/rocprofiler-sdk/pc_sampling/hsa_adapter.cpp

* Update source/lib/rocprofiler-sdk/pc_sampling/hsa_adapter.cpp

* Update source/lib/rocprofiler-sdk/pc_sampling/hsa_adapter.cpp

* Update source/lib/rocprofiler-sdk/pc_sampling/utils.cpp

* Update utils.cpp

* moving pc-sampling tests and samples to pc-sampling label

* Format fix

* pcs: use configured instead of active service

* Update source/lib/rocprofiler-sdk/pc_sampling/service.cpp

* pcs: ensure configuring PC sampling on the HSA level is called only once

* pcs: minor fix

* Update CMakeLists.txt

* Update CMakeLists.txt

* Update CMakeLists.txt

* Update CMakeLists.txt

* pcs: refactoring IOCTL integration

* Update source/lib/rocprofiler-sdk/pc_sampling/tests/CMakeLists.txt

Co-authored-by: Ammar ELWazir <ammar.elwazir@amd.com>

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter_types.hpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter_types.hpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.hpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* pcs: reverting back what bot doubled

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter_types.hpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* pcs: retesting the bot

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter_types.hpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* pcs: why bot fails on this IOCTL status

* pcs: why failing on <vector>

* Update source/lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* pcs: returning commits removed by bot

* pcs: formatting locally

* pcs: clients are flushing buffers inside the tool_fini

* pcs: sync function in public API

* pcs: sync prior to unloading the code object

* pcs: sync function requires context

* pcs: client uses CID retirement service

* pcs: test for flusing internal ROCr buffers

* pcs: source formatting

* Update source/lib/rocprofiler-sdk/pc_sampling/tests/CMakeLists.txt

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* pcs: code samples refactoring

* pcs: public API header refactored

* pcs: rocprofiler_buffer_flush drains internal PC sampling buffers too

* pcs: remove unnecessary functions

* pcs: do not call hsa's copytables

* pcs: include reordering

* pcs: using ROCP_ERROR inside PC sampling implementation

* pcs: pc_sampling sample uses ostream instean of printfs

* pcs: pc_sampling_codeobj tracing using ostream instead of prints

* pcs: registering once for interceptor callbacks

* pcs: do not generate internal CIDs if not in debug mode

* pcs: rebasing fixed; missing external correlation IDs

* pcs: code formatting

* enable kernel tracing service to receive external correlation IDs

* pcs: using ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL

* pcs: polishing parser

* formatting

* updating parser to use workgroup_id

* kfd_ioctl.h extracted in details folder

* refactoring

* pcs: preparing to generate code object information

* flush internal buffers prior to unloading code object

* pcs: generating marker records

* pcs: wrap code_object's shutdown function

* ROCR_VISIBLE_DEVICES and HIP_VISISBLE_DEVICES unsupported at the moment

* documenting the ignorance of ROCR/HIP_VISIBLE_DEVICES

* pcs: separate structs for code object loading/unloading markers

* pcs: inst_pkt_t changed the namespace

* pcs: removing wrapper around the shutdown function

* pcs: size in record field

* pcs: documentation refactoring + typdefs

* renaming PCSAgentConfig to PCSAgentSession

* pcs: service does not keep a pointer to the context

* pcs: static assertions related to the versioning

* pcs: rocprofiler_pc_sampling_configuration_t size field

* pcs: report API unimplemented unleass explicitly enabled

* pcs: skip tests if KFD does not support PC sampling

* pcs: if ROCr hides some devices, no PC samples will be delivered for it

* pcs: hip error check after kernel launch

* formatting

* removing PCS info from agent.h

* fix based on review

* Update continuous integration workflow

- use mi200 runner for code coverage (supports PC sampling)
- split sanitizer jobs across navi3, vega20, and mi300

* Updating pc sampling test labels

* ROCP_PC_SAMPLING_ENABLED env in CI

* ROCP_PC_SAMPLING_ENABLED for all CI mi200 jobs

* Rearrange sanitizer assignments

* fixes according to review

* removed unused functions

* pcs: rocprofiler_agent_id_t instead of handle as a key in map

* Update source/lib/rocprofiler-sdk/context/context.hpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* removing drm_fd from the agent.h

* pcs: removing one sample due to complexity

* pcs: refactoring sample

* simplifying sample

* new lines

* Improve queue_control enable intercepter logic

* Update lib/rocprofiler-sdk/hsa/types.hpp

- handle amd_ext size for HSA 1.12.0

* ROCP_PC_SAMPLING_ENABLED -> ROCPROFILER_PC_SAMPLING_BETA_ENABLED

* Update hsa_adapter.cpp

- anonymous namespace + remove debug

* parser update

* Apply suggestions from code review

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: vlaindic <vlaindic@users.noreply.github.com>
Co-authored-by: vlaindic <vladimir.indic@amd.com>
Co-authored-by: vlaindic <vlaindic@amd.com>
Co-authored-by: Vladimir Indic <139573562+vlaindic@users.noreply.github.com>
Co-authored-by: Jonathan R. Madsen <jrmadsen@users.noreply.github.com>
Co-authored-by: gobhardw <gopesh.bhardwaj@amd.com>
Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
This commit is contained in:
Ammar ELWazir
2024-05-24 09:49:44 -05:00
committed by GitHub
orang tua f14f5a994e
melakukan 987ae3cc47
64 mengubah file dengan 6831 tambahan dan 2012 penghapusan
+22 -10
Melihat File
@@ -70,6 +70,12 @@ jobs:
run: |
echo 'EXCLUDED_TESTS=${{ env.PC_SAMPLING_TESTS_REGEX }}' >> $GITHUB_ENV
- name: Enable PC Sampling
if: ${{ contains(matrix.runner, 'mi200') }}
shell: bash
run: |
echo 'ROCPROFILER_PC_SAMPLING_BETA_ENABLED=1' >> $GITHUB_ENV
- name: Configure, Build, and Test
timeout-minutes: 30
shell: bash
@@ -153,7 +159,7 @@ jobs:
strategy:
fail-fast: false
matrix:
runner: ['navi3']
runner: ['mi200']
os: ['ubuntu-22.04']
build-type: ['Release']
@@ -164,6 +170,7 @@ jobs:
env:
GIT_DISCOVERY_ACROSS_FILESYSTEM: 1
GCC_COMPILER_VERSION: 11
ROCPROFILER_PC_SAMPLING_BETA_ENABLED: 1
steps:
- name: Patch Git
@@ -225,12 +232,6 @@ jobs:
for i in python3 git cmake ctest gcc g++ gcov; do which-realpath $i; done
ls -la
- name: Exclude PC Sampling Tests
if: ${{ !contains(matrix.runner, 'mi200') && !contains(matrix.runner, 'mi300') }}
shell: bash
run: |
echo 'EXCLUDED_TESTS=${{ env.PC_SAMPLING_TESTS_REGEX }}' >> $GITHUB_ENV
- name: Configure, Build, and Test (Total Code Coverage)
timeout-minutes: 30
shell: bash
@@ -384,11 +385,17 @@ jobs:
strategy:
fail-fast: false
matrix:
runner: ['mi200']
runner: ['navi3', 'vega20', 'mi300']
sanitizer: ['AddressSanitizer', 'ThreadSanitizer', 'LeakSanitizer']
os: ['ubuntu-22.04']
build-type: ['RelWithDebInfo']
ci-flags: ['']
exclude:
- { runner: 'navi3', sanitizer: 'ThreadSanitizer' }
- { runner: 'navi3', sanitizer: 'LeakSanitizer' }
- { runner: 'vega20', sanitizer: 'AddressSanitizer' }
- { runner: 'vega20', sanitizer: 'LeakSanitizer' }
- { runner: 'mi300', sanitizer: 'AddressSanitizer' }
- { runner: 'mi300', sanitizer: 'ThreadSanitizer' }
if: ${{ contains(github.event_name, 'pull_request') }}
runs-on: ${{ matrix.runner }}-runner-set
@@ -427,6 +434,12 @@ jobs:
run: |
echo 'EXCLUDED_TESTS=${{ env.PC_SAMPLING_TESTS_REGEX }}' >> $GITHUB_ENV
- name: Enable PC Sampling
if: ${{ contains(matrix.runner, 'mi200') }}
shell: bash
run: |
echo 'ROCPROFILER_PC_SAMPLING_BETA_ENABLED=1' >> $GITHUB_ENV
- name: Configure, Build, and Test
timeout-minutes: 45
shell: bash
@@ -438,7 +451,6 @@ jobs:
--gpu-targets ${{ env.GPU_TARGETS }}
--memcheck ${{ matrix.sanitizer }}
--run-attempt ${{ github.run_attempt }}
${{ matrix.ci-flags }}
--
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }}
-DCMAKE_INSTALL_PREFIX="${{ env.ROCM_PATH }}"
+1
Melihat File
@@ -32,3 +32,4 @@ add_subdirectory(intercept_table)
add_subdirectory(code_object_isa_decode)
add_subdirectory(advanced_thread_trace)
add_subdirectory(external_correlation_id_request)
add_subdirectory(pc_sampling)
+63
Melihat File
@@ -0,0 +1,63 @@
#
#
#
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 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)
add_library(pc-sampling-client SHARED)
target_sources(pc-sampling-client PRIVATE client.cpp pcs.hpp pcs.cpp utils.hpp utils.cpp)
target_link_libraries(
pc-sampling-client
PRIVATE rocprofiler-sdk::rocprofiler-sdk rocprofiler::samples-build-flags
rocprofiler::samples-common-library)
set_source_files_properties(main.cpp PROPERTIES LANGUAGE HIP)
find_package(Threads REQUIRED)
add_executable(pc-sampling)
target_sources(pc-sampling PRIVATE main.cpp)
target_link_libraries(pc-sampling PRIVATE pc-sampling-client Threads::Threads
rocprofiler::samples-build-flags)
rocprofiler_samples_get_preload_env(PRELOAD_ENV pc-sampling-client)
rocprofiler_samples_get_ld_library_path_env(LIBRARY_PATH_ENV)
set(pc-sampling-env ${PRELOAD_ENV} ${LIBRARY_PATH_ENV})
add_test(NAME pc-sampling COMMAND $<TARGET_FILE:pc-sampling>)
set_tests_properties(
pc-sampling
PROPERTIES TIMEOUT
45
LABELS
"samples;pc-sampling"
ENVIRONMENT
"${pc-sampling-env}"
FAIL_REGULAR_EXPRESSION
"${ROCPROFILER_DEFAULT_FAIL_REGEX}"
SKIP_REGULAR_EXPRESSION
"PC sampling unavailable")
+230
Melihat File
@@ -0,0 +1,230 @@
// MIT License
//
// Copyright (c) 2023 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
/**
* @file samples/pc_sampling_library/client.cpp
*
* @brief Example rocprofiler client (tool)
*/
#include "pcs.hpp"
#include "utils.hpp"
#include <rocprofiler-sdk/buffer.h>
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/internal_threading.h>
#include <rocprofiler-sdk/registration.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include "common/defines.hpp"
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <regex>
#include <string>
#include <vector>
namespace client
{
namespace
{
rocprofiler_client_id_t* client_id = nullptr;
rocprofiler_client_finalize_t client_fini_func = nullptr;
rocprofiler_context_id_t client_ctx;
int
tool_init(rocprofiler_client_finalize_t fini_func, void* /*tool_data*/)
{
client_fini_func = fini_func;
client::pcs::find_all_gpu_agents_supporting_pc_sampling();
if(client::pcs::gpu_agents.empty())
{
*utils::get_output_stream() << "No availabe gpu agents supporting PC sampling" << std::endl;
// Exit with no error if none of the GPUs support PC sampling.
exit(0);
}
// The relations assumed:
// - One context for all gpu agents
// - a buffer per agent
// - a callback thread per buffer
// - a pc sampling service per agent/buffer
ROCPROFILER_CHECK(rocprofiler_create_context(&client_ctx));
for(auto& gpu_agent : pcs::gpu_agents)
{
// creating a buffer that will hold pc sampling information
rocprofiler_buffer_policy_t drop_buffer_action = ROCPROFILER_BUFFER_POLICY_LOSSLESS;
auto buffer_id = rocprofiler_buffer_id_t{};
ROCPROFILER_CHECK(rocprofiler_create_buffer(client_ctx,
client::pcs::BUFFER_SIZE_BYTES,
client::pcs::WATERMARK,
drop_buffer_action,
client::pcs::rocprofiler_pc_sampling_callback,
nullptr,
&buffer_id));
client::pcs::configure_pc_sampling_prefer_stochastic(
gpu_agent.get(), client_ctx, buffer_id);
// One helper thread per GPU agent's buffer.
auto client_agent_thread = rocprofiler_callback_thread_t{};
ROCPROFILER_CHECK(rocprofiler_create_callback_thread(&client_agent_thread));
ROCPROFILER_CHECK(rocprofiler_assign_callback_thread(buffer_id, client_agent_thread));
client::pcs::buffer_ids.emplace_back(buffer_id);
}
int valid_ctx = 0;
ROCPROFILER_CHECK(rocprofiler_context_is_valid(client_ctx, &valid_ctx));
if(valid_ctx == 0)
{
// notify rocprofiler that initialization failed
// and all the contexts, buffers, etc. created
// should be ignored
return -1;
}
// Start PC sampling
ROCPROFILER_CHECK(rocprofiler_start_context(client_ctx));
return 0;
}
void
tool_fini(void* /*tool_data*/)
{
if(client_id)
{
// Assert the context is inactive.
int state = -1;
ROCPROFILER_CHECK(rocprofiler_context_is_active(client_ctx, &state))
assert(state == 0);
// No need to stop the context, since it has been stopped implicitly by the rocprofiler-SDK.
for(size_t i = 0; i < client::pcs::buffer_ids.size(); i++)
{
// Flush the buffer explicitly
ROCPROFILER_CHECK(rocprofiler_flush_buffer(client::pcs::buffer_ids.at(i)));
// Destroying the buffer
rocprofiler_status_t status = rocprofiler_destroy_buffer(client::pcs::buffer_ids.at(i));
if(status == ROCPROFILER_STATUS_ERROR_BUFFER_BUSY)
{
*utils::get_output_stream()
<< "The buffer is busy, so we cannot destroy it at the moment." << std::endl;
}
else
{
ROCPROFILER_CHECK(status);
}
}
}
}
} // namespace
// forward declaration
void
setup();
void
setup()
{
if(int status = 0;
rocprofiler_is_initialized(&status) == ROCPROFILER_STATUS_SUCCESS && status == 0)
{
ROCPROFILER_CHECK(rocprofiler_force_configure(&rocprofiler_configure));
}
}
void
shutdown()
{}
} // namespace client
extern "C" rocprofiler_tool_configure_result_t*
rocprofiler_configure(uint32_t version,
const char* runtime_version,
uint32_t priority,
rocprofiler_client_id_t* id)
{
// only activate if main tool
if(priority > 0) return nullptr;
// set the client name
id->name = "PCSamplingExampleTool";
// store client info
client::client_id = id;
// compute major/minor/patch version info
uint32_t major = version / 10000;
uint32_t minor = (version % 10000) / 100;
uint32_t patch = version % 100;
// generate info string
auto info = std::stringstream{};
info << id->name << " is using rocprofiler v" << major << "." << minor << "." << patch << " ("
<< runtime_version << ")";
std::clog << info.str() << std::endl;
std::ostream* output_stream = nullptr;
std::string filename = "pc_sampling.log";
if(auto* outfile = getenv("ROCPROFILER_SAMPLE_OUTPUT_FILE"); outfile) filename = outfile;
if(filename == "stdout")
output_stream = &std::cout;
else if(filename == "stderr")
output_stream = &std::cerr;
else
output_stream = new std::ofstream{filename};
client::utils::get_output_stream() = output_stream;
// create configure data
static auto cfg =
rocprofiler_tool_configure_result_t{sizeof(rocprofiler_tool_configure_result_t),
&client::tool_init,
&client::tool_fini,
static_cast<void*>(output_stream)};
// return pointer to configure data
return &cfg;
}
+412
Melihat File
@@ -0,0 +1,412 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// 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.
#include "hip/hip_runtime.h"
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <random>
#include <sstream>
#include <stdexcept>
#define HIP_API_CALL(CALL) \
{ \
hipError_t error_ = (CALL); \
if(error_ != hipSuccess) \
{ \
auto _hip_api_print_lk = auto_lock_t{print_lock}; \
fprintf(stderr, \
"%s:%d :: HIP error : %s\n", \
__FILE__, \
__LINE__, \
hipGetErrorString(error_)); \
throw std::runtime_error("hip_api_call"); \
} \
}
namespace
{
using auto_lock_t = std::unique_lock<std::mutex>;
auto print_lock = std::mutex{};
size_t nthread_per_device = 2;
size_t nitr = 500;
size_t nsync = 10;
constexpr unsigned shared_mem_tile_dim = 32;
void
check_hip_error(void);
void
verify(int* in, int* out, int M, int N);
} // namespace
__global__ void
transpose(const int* in, int* out, int M, int N);
void
run(int rank, int tid, int devid, int argc, char** argv);
void
run_transpose(int rank, int tid, hipStream_t stream, int argc, char** argv);
void
run_migrate(int rank, int tid, hipStream_t stream, int, char** argv);
void
run_scratch(int rank, int tid, hipStream_t stream, int argc, char** argv);
int
main(int argc, char** argv)
{
auto* exe_name = ::basename(argv[0]);
int rank = 0;
for(int i = 1; i < argc; ++i)
{
auto _arg = std::string{argv[i]};
if(_arg == "?" || _arg == "-h" || _arg == "--help")
{
fprintf(stderr,
"usage: %s [NUM_THREADS_PER_DEVICE (%zu)] [NUM_ITERATION (%zu)] "
"[SYNC_EVERY_N_ITERATIONS (%zu)]\n",
exe_name,
nthread_per_device,
nitr,
nsync);
exit(EXIT_SUCCESS);
}
}
if(argc > 1) nthread_per_device = atoll(argv[1]);
if(argc > 2) nitr = atoll(argv[2]);
if(argc > 3) nsync = atoll(argv[3]);
int ndevice = 0;
HIP_API_CALL(hipGetDeviceCount(&ndevice));
auto nthreads = (ndevice * nthread_per_device);
printf("[%s] Number of devices found: %i\n", exe_name, ndevice);
printf("[%s] Number of threads (per device): %zu\n", exe_name, nthread_per_device);
printf("[%s] Number of threads (total): %zu\n", exe_name, nthreads);
printf("[%s] Number of iterations: %zu\n", exe_name, nitr);
printf("[%s] Syncing every %zu iterations\n", exe_name, nsync);
{
auto _threads = std::vector<std::thread>{};
for(size_t i = 0; i < nthreads; ++i)
_threads.emplace_back(run, rank, i, i % ndevice, argc, argv);
for(auto& itr : _threads)
itr.join();
}
HIP_API_CALL(hipDeviceSynchronize());
HIP_API_CALL(hipDeviceReset());
return 0;
}
__global__ void
transpose(const int* in, int* out, int M, int N)
{
__shared__ int tile[shared_mem_tile_dim][shared_mem_tile_dim];
int idx = (blockIdx.y * blockDim.y + threadIdx.y) * M + blockIdx.x * blockDim.x + threadIdx.x;
tile[threadIdx.y][threadIdx.x] = in[idx];
__syncthreads();
idx = (blockIdx.x * blockDim.x + threadIdx.y) * N + blockIdx.y * blockDim.y + threadIdx.x;
out[idx] = tile[threadIdx.x][threadIdx.y];
}
template <typename Tp>
__global__ void
test_page_migrate(Tp* data, Tp val)
{
int idx = (blockIdx.x * blockDim.x) + threadIdx.x;
data[idx] += val;
}
__global__ void
test_kern_large(uint64_t* output)
{
uint64_t result = 0;
int test[4000];
memset(test, 5, 4000);
for(int& i : test)
{
i = i + 7;
*output += i;
result += i;
}
*output ^= result;
*output ^= result;
}
__global__ void
test_kern_medium(uint64_t* output)
{
uint64_t result = 0;
int test[175];
memset(test, 5, 175);
for(int& i : test)
{
i = i + 7;
*output += i;
result += i;
}
*output ^= result;
*output ^= result;
}
__global__ void
test_kern_small(uint64_t* output)
{
uint64_t result = 0;
int test[2];
for(int& i : test)
{
i = i + 7;
*output += i;
result += i;
}
*output ^= result;
*output ^= result;
}
void
run(int rank, int tid, int devid, int argc, char** argv)
{
auto* stream = hipStream_t{};
HIP_API_CALL(hipSetDevice(devid));
HIP_API_CALL(hipStreamCreate(&stream));
run_migrate(rank, tid, stream, argc, argv);
run_scratch(rank, tid, stream, argc, argv);
run_transpose(rank, tid, stream, argc, argv);
HIP_API_CALL(hipStreamSynchronize(stream));
HIP_API_CALL(hipStreamDestroy(stream));
}
void
run_transpose(int rank, int tid, hipStream_t stream, int argc, char** argv)
{
auto* exe_name = ::basename(argv[0]);
unsigned int M = 4960 * 2;
unsigned int N = 4960 * 2;
if(argc > 2) nitr = atoll(argv[2]);
if(argc > 3) nsync = atoll(argv[3]);
auto_lock_t _lk{print_lock};
std::cout << "[" << exe_name << "][transpose][" << rank << "][" << tid << "] M: " << M
<< " N: " << N << std::endl;
_lk.unlock();
std::default_random_engine _engine{std::random_device{}() * (rank + 1) * (tid + 1)};
std::uniform_int_distribution<int> _dist{0, 1000};
size_t size = sizeof(int) * M * N;
int* inp_matrix = new int[size];
int* out_matrix = new int[size];
for(size_t i = 0; i < M * N; i++)
{
inp_matrix[i] = _dist(_engine);
out_matrix[i] = 0;
}
int* in = nullptr;
int* out = nullptr;
HIP_API_CALL(hipMalloc(&in, size));
HIP_API_CALL(hipMalloc(&out, size));
HIP_API_CALL(hipMemsetAsync(in, 0, size, stream));
HIP_API_CALL(hipMemsetAsync(out, 0, size, stream));
HIP_API_CALL(hipMemcpyAsync(in, inp_matrix, size, hipMemcpyHostToDevice, stream));
HIP_API_CALL(hipStreamSynchronize(stream));
dim3 grid(M / 32, N / 32, 1);
dim3 block(32, 32, 1); // transpose
print_lock.lock();
printf("[%s][transpose][%i][%i] grid=(%i,%i,%i), block=(%i,%i,%i)\n",
exe_name,
rank,
tid,
grid.x,
grid.y,
grid.z,
block.x,
block.y,
block.z);
print_lock.unlock();
auto t1 = std::chrono::high_resolution_clock::now();
for(size_t i = 0; i < nitr; ++i)
{
transpose<<<grid, block, 0, stream>>>(in, out, M, N);
check_hip_error();
if(i % nsync == (nsync - 1)) HIP_API_CALL(hipStreamSynchronize(stream));
}
auto t2 = std::chrono::high_resolution_clock::now();
HIP_API_CALL(hipStreamSynchronize(stream));
HIP_API_CALL(hipMemcpyAsync(out_matrix, out, size, hipMemcpyDeviceToHost, stream));
double time = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count();
float GB = (float) size * nitr * 2 / (1 << 30);
print_lock.lock();
std::cout << "[" << exe_name << "][transpose][" << rank << "][" << tid
<< "] Runtime of transpose is " << time << " sec\n";
std::cout << "[" << exe_name << "][transpose][" << rank << "][" << tid
<< "] The average performance of transpose is " << GB / time << " GBytes/sec"
<< std::endl;
print_lock.unlock();
HIP_API_CALL(hipStreamSynchronize(stream));
// cpu_transpose(matrix, out_matrix, M, N);
verify(inp_matrix, out_matrix, M, N);
HIP_API_CALL(hipFree(in));
HIP_API_CALL(hipFree(out));
delete[] inp_matrix;
delete[] out_matrix;
}
void
run_scratch(int rank, int tid, hipStream_t stream, int, char** argv)
{
auto t1 = std::chrono::high_resolution_clock::now();
HIP_API_CALL(hipStreamSynchronize(stream));
const auto* exe_name = ::basename(argv[0]);
uint64_t* data_ptr = nullptr;
HIP_API_CALL(hipHostMalloc(&data_ptr, sizeof(uint64_t), 0));
*data_ptr = 0;
test_kern_small<<<1000, 1, 0, stream>>>(data_ptr);
test_kern_medium<<<1000, 1, 0, stream>>>(data_ptr);
test_kern_small<<<1000, 1, 0, stream>>>(data_ptr);
test_kern_large<<<1100, 1, 0, stream>>>(data_ptr);
HIP_API_CALL(hipStreamSynchronize(stream));
test_kern_small<<<1000, 1, 0, stream>>>(data_ptr);
HIP_API_CALL(hipStreamSynchronize(stream));
test_kern_medium<<<1000, 1, 0, stream>>>(data_ptr);
HIP_API_CALL(hipStreamSynchronize(stream));
test_kern_small<<<1000, 1, 0, stream>>>(data_ptr);
HIP_API_CALL(hipStreamSynchronize(stream));
test_kern_large<<<1100, 1, 0, stream>>>(data_ptr);
HIP_API_CALL(hipStreamSynchronize(stream));
auto t2 = std::chrono::high_resolution_clock::now();
double time = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count();
print_lock.lock();
std::cout << "[" << exe_name << "][scratch][" << rank << "][" << tid
<< "] Runtime of scratch is " << time << " sec\n";
print_lock.unlock();
}
void
run_migrate(int rank, int tid, hipStream_t stream, int, char** argv)
{
using data_type = uint64_t;
constexpr data_type init_v = 1;
constexpr data_type incr_v = 1;
auto t1 = std::chrono::high_resolution_clock::now();
HIP_API_CALL(hipStreamSynchronize(stream));
const auto* exe_name = ::basename(argv[0]);
auto page_data = std::vector<data_type>(1024, 0);
HIP_API_CALL(hipHostRegister(
page_data.data(), page_data.size() * sizeof(data_type), hipHostRegisterDefault));
for(auto& itr : page_data)
itr = init_v;
test_page_migrate<<<1, 1024, 0, stream>>>(page_data.data(), incr_v);
HIP_API_CALL(hipStreamSynchronize(stream));
for(auto& itr : page_data)
{
auto diff = (itr - incr_v);
if(diff != init_v)
{
auto msg = std::stringstream{};
msg << "invalid diff: " << diff << ". expected: " << init_v;
throw std::runtime_error{msg.str()};
}
}
HIP_API_CALL(hipHostUnregister(page_data.data()));
auto t2 = std::chrono::high_resolution_clock::now();
double time = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count();
print_lock.lock();
std::cout << "[" << exe_name << "][migrate][" << rank << "][" << tid
<< "] Runtime of migrate is " << time << " sec\n";
print_lock.unlock();
}
namespace
{
void
check_hip_error(void)
{
hipError_t err = hipGetLastError();
if(err != hipSuccess)
{
auto_lock_t _lk{print_lock};
std::cerr << "Error: " << hipGetErrorString(err) << std::endl;
throw std::runtime_error("hip_api_call");
}
}
void
verify(int* in, int* out, int M, int N)
{
for(int i = 0; i < 10; i++)
{
int row = rand() % M;
int col = rand() % N;
if(in[row * N + col] != out[col * M + row])
{
auto_lock_t _lk{print_lock};
std::cout << "mismatch: " << row << ", " << col << " : " << in[row * N + col] << " | "
<< out[col * M + row] << "\n";
}
}
}
} // namespace
+317
Melihat File
@@ -0,0 +1,317 @@
// MIT License
//
// Copyright (c) 2023 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 "pcs.hpp"
#include "utils.hpp"
#include "common/defines.hpp"
#include <cassert>
#include <cstdio>
#include <iomanip>
#include <memory>
#include <sstream>
namespace client
{
namespace pcs
{
tool_agent_info_vec_t gpu_agents;
std::vector<rocprofiler_buffer_id_t> buffer_ids;
rocprofiler_status_t
find_all_gpu_agents_supporting_pc_sampling_impl(rocprofiler_agent_version_t version,
const void** agents,
size_t num_agents,
void* user_data)
{
assert(version == ROCPROFILER_AGENT_INFO_VERSION_0);
// user_data represent the pointer to the array where gpu_agent will be stored
if(!user_data) return ROCPROFILER_STATUS_ERROR;
std::stringstream ss;
auto* _out_agents = static_cast<tool_agent_info_vec_t*>(user_data);
auto* _agents = reinterpret_cast<const rocprofiler_agent_t**>(agents);
for(size_t i = 0; i < num_agents; i++)
{
if(_agents[i]->type == ROCPROFILER_AGENT_TYPE_GPU)
{
// Instantiate the tool_agent_info.
// Store pointer to the rocprofiler_agent_t and instatiate a vector of
// available configurations.
// Move the ownership to the _out_agents
auto tool_gpu_agent = std::make_unique<tool_agent_info>();
tool_gpu_agent->agent_id = _agents[i]->id;
tool_gpu_agent->avail_configs = std::make_unique<avail_configs_vec_t>();
tool_gpu_agent->agent = _agents[i];
// Check if the GPU agent supports PC sampling. If so, add it to the
// output list `_out_agents`.
if(query_avail_configs_for_agent(tool_gpu_agent.get()))
_out_agents->push_back(std::move(tool_gpu_agent));
}
ss << "[" << __FUNCTION__ << "] " << _agents[i]->name << " :: "
<< "id=" << _agents[i]->id.handle << ", "
<< "type=" << _agents[i]->type << "\n";
}
*utils::get_output_stream() << ss.str() << std::endl;
return ROCPROFILER_STATUS_SUCCESS;
}
void
find_all_gpu_agents_supporting_pc_sampling()
{
// This function returns the all gpu agents supporting some kind of PC sampling
ROCPROFILER_CHECK(
rocprofiler_query_available_agents(ROCPROFILER_AGENT_INFO_VERSION_0,
&find_all_gpu_agents_supporting_pc_sampling_impl,
sizeof(rocprofiler_agent_t),
static_cast<void*>(&gpu_agents)));
}
/**
* @brief The function queries available PC sampling configurations.
* If there is at least one available configuration, it returns true.
* Otherwise, this function returns false to indicate the agent does
* not support PC sampling.
*/
bool
query_avail_configs_for_agent(tool_agent_info* agent_info)
{
// Clear the available configurations vector
agent_info->avail_configs->clear();
auto cb = [](const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data) {
auto* avail_configs = static_cast<avail_configs_vec_t*>(user_data);
for(size_t i = 0; i < num_config; i++)
{
avail_configs->emplace_back(configs[i]);
}
return ROCPROFILER_STATUS_SUCCESS;
};
auto status = rocprofiler_query_pc_sampling_agent_configurations(
agent_info->agent_id, cb, agent_info->avail_configs.get());
std::stringstream ss;
if(status != ROCPROFILER_STATUS_SUCCESS)
{
// The query operation failed, so consider the PC sampling is unsupported at the agent.
// This can happen if the PC sampling service is invoked within the ROCgdb.
ss << "Querying PC sampling capabilities failed with status: " << status << std::endl;
*utils::get_output_stream() << ss.str() << std::endl;
return false;
}
else if(agent_info->avail_configs->size() == 0)
{
// No available configuration at the moment, so mark the PC sampling as unsupported.
return false;
}
ss << "The agent with the id: " << agent_info->agent_id.handle << " supports the "
<< agent_info->avail_configs->size() << " configurations: " << std::endl;
size_t ind = 0;
for(auto& cfg : *agent_info->avail_configs)
{
ss << "(" << ++ind << ".) "
<< "method: " << cfg.method << ", "
<< "unit: " << cfg.unit << ", "
<< "min_interval: " << cfg.min_interval << ", "
<< "max_interval: " << cfg.max_interval << ", "
<< "flags: " << std::hex << cfg.flags << std::dec << std::endl;
}
*utils::get_output_stream() << ss.str() << std::flush;
return true;
}
void
configure_pc_sampling_prefer_stochastic(tool_agent_info* agent_info,
rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer_id)
{
int failures = 10;
size_t interval = 0;
do
{
// Update the list of available configurations
auto success = query_avail_configs_for_agent(agent_info);
if(!success)
{
// An error occured while querying PC sampling capabilities,
// so avoid trying configuring PC sampling service.
// Instead return false to indicated a failure.
ROCPROFILER_CHECK(ROCPROFILER_STATUS_ERROR);
}
const rocprofiler_pc_sampling_configuration_t* first_host_trap_config = nullptr;
const rocprofiler_pc_sampling_configuration_t* first_stochastic_config = nullptr;
// Search until encountering on the stochastic configuration, if any.
// Otherwise, use the host trap config
for(auto const& cfg : *agent_info->avail_configs)
{
if(cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC)
{
first_stochastic_config = &cfg;
break;
}
else if(!first_host_trap_config &&
cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP)
{
first_host_trap_config = &cfg;
}
}
// Check if the stochastic config is found. Use host trap config otherwise.
const rocprofiler_pc_sampling_configuration_t* picked_cfg =
(first_stochastic_config != nullptr) ? first_stochastic_config : first_host_trap_config;
if(picked_cfg->min_interval == picked_cfg->max_interval)
{
// Another process already configured PC sampling, so use the intreval it set up.
interval = picked_cfg->min_interval;
}
else
{
interval = 10000;
}
auto status = rocprofiler_configure_pc_sampling_service(context_id,
agent_info->agent_id,
picked_cfg->method,
picked_cfg->unit,
interval,
buffer_id);
if(status == ROCPROFILER_STATUS_SUCCESS)
{
*utils::get_output_stream()
<< ">>> We chose PC sampling interval: " << interval
<< " on the agent: " << agent_info->agent->id.handle << std::endl;
return;
}
else if(status != ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE)
{
ROCPROFILER_CHECK(status);
}
// status == ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE
// means another process P2 already configured PC sampling.
// Query available configurations again and receive the configurations picked by P2.
// However, if P2 destroys PC sampling service after query function finished,
// but before the `rocprofiler_configure_pc_sampling_service` is called,
// then the `rocprofiler_configure_pc_sampling_service` will fail again.
// The process P1 executing this loop can spin wait (starve) if it is unlucky enough
// to always be interuppted by some other process P2 that creates/destroys
// PC sampling service on the same device while P1 is executing the code
// after the `query_avail_configs_for_agent` and
// before the `rocprofiler_configure_pc_sampling_service`.
// This should happen very rarely, but just to be sure, we introduce a counter `failures`
// that will allow certain amount of failures to process P1.
} while(--failures);
// The process failed too many times configuring PC sampling,
// report this to user;
ROCPROFILER_CHECK(ROCPROFILER_STATUS_ERROR);
}
void
rocprofiler_pc_sampling_callback(rocprofiler_context_id_t /*context_id*/,
rocprofiler_buffer_id_t /*buffer_id*/,
rocprofiler_record_header_t** headers,
size_t num_headers,
void* /*data*/,
uint64_t drop_count)
{
std::stringstream ss;
ss << "The number of delivered samples is: " << num_headers << ", "
<< "while the number of dropped samples is: " << drop_count << std::endl;
for(size_t i = 0; i < num_headers; i++)
{
auto* cur_header = headers[i];
if(cur_header == nullptr)
{
throw std::runtime_error{
"rocprofiler provided a null pointer to header. this should never happen"};
}
else if(cur_header->hash !=
rocprofiler_record_header_compute_hash(cur_header->category, cur_header->kind))
{
throw std::runtime_error{"rocprofiler_record_header_t (category | kind) != hash"};
}
else if(cur_header->category == ROCPROFILER_BUFFER_CATEGORY_PC_SAMPLING)
{
if(cur_header->kind == ROCPROFILER_PC_SAMPLING_RECORD_SAMPLE)
{
auto* pc_sample =
static_cast<rocprofiler_pc_sampling_record_t*>(cur_header->payload);
ss << "pc: " << std::hex << pc_sample->pc << ", "
<< "timestamp: " << std::dec << pc_sample->timestamp << ", "
<< "exec: " << std::hex << std::setw(16) << pc_sample->exec_mask << ", "
<< "workgroup_id_(x=" << std::dec << std::setw(5) << pc_sample->workgroup_id.x
<< ", "
<< "y=" << std::setw(5) << pc_sample->workgroup_id.y << ", "
<< "z=" << std::setw(5) << pc_sample->workgroup_id.z << "), "
<< "wave_id: " << std::setw(2) << static_cast<unsigned int>(pc_sample->wave_id)
<< ", "
<< "cu_id: " << pc_sample->hw_id << ", "
<< "correlation: {internal=" << std::setw(7)
<< pc_sample->correlation_id.internal << ", "
<< "external=" << std::setw(5) << pc_sample->correlation_id.external.value << "}"
<< std::endl;
}
else if(cur_header->kind == ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_LOAD_MARKER)
{
auto* marker = static_cast<rocprofiler_pc_sampling_code_object_load_marker_t*>(
cur_header->payload);
ss << "code object loading: " << marker->code_object_id << std::endl;
}
else if(cur_header->kind == ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_UNLOAD_MARKER)
{
auto* marker = static_cast<rocprofiler_pc_sampling_code_object_unload_marker_t*>(
cur_header->payload);
ss << "code object unloading: " << marker->code_object_id << std::endl;
}
}
else
{
throw std::runtime_error{"unexpected rocprofiler_record_header_t category + kind"};
}
}
*utils::get_output_stream() << ss.str() << std::endl;
}
} // namespace pcs
} // namespace client
+79
Melihat File
@@ -0,0 +1,79 @@
// MIT License
//
// Copyright (c) 2023 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.
#pragma once
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include <vector>
namespace client
{
namespace pcs
{
constexpr size_t BUFFER_SIZE_BYTES = 8192;
constexpr size_t WATERMARK = (BUFFER_SIZE_BYTES / 4);
struct tool_agent_info;
using avail_configs_vec_t = std::vector<rocprofiler_pc_sampling_configuration_t>;
using tool_agent_info_vec_t = std::vector<std::unique_ptr<tool_agent_info>>;
struct tool_agent_info
{
rocprofiler_agent_id_t agent_id;
std::unique_ptr<avail_configs_vec_t> avail_configs;
const rocprofiler_agent_t* agent;
};
// GPU agents supporting some kind of PC sampling.
// Note that for some of these agent, the corresponding context might be invalid,
// meaning we were not able to enable PC sampling service.
// Check the `tool_init` for more information.
extern tool_agent_info_vec_t gpu_agents;
// Ids of the buffers used as containers for PC sampling records
extern std::vector<rocprofiler_buffer_id_t> buffer_ids;
void
find_all_gpu_agents_supporting_pc_sampling();
/**
* @brief The return value indicates if the agent supports PC sampling.
* Check the implementation for more info.
*/
bool
query_avail_configs_for_agent(tool_agent_info* agent_info);
void
configure_pc_sampling_prefer_stochastic(tool_agent_info* agent_info,
rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer_id);
void
rocprofiler_pc_sampling_callback(rocprofiler_context_id_t context_id,
rocprofiler_buffer_id_t buffer_id,
rocprofiler_record_header_t** headers,
size_t num_headers,
void* data,
uint64_t drop_count);
} // namespace pcs
} // namespace client
+37
Melihat File
@@ -0,0 +1,37 @@
// MIT License
//
// Copyright (c) 2023 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.
#include "utils.hpp"
namespace client
{
namespace utils
{
std::ostream*&
get_output_stream()
{
// The output strea is initially unitialized
static std::ostream* _v = nullptr;
return _v;
}
} // namespace utils
} // namespace client
+36
Melihat File
@@ -0,0 +1,36 @@
// MIT License
//
// Copyright (c) 2023 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.
#pragma once
#include <rocprofiler-sdk/fwd.h>
#include <iostream>
namespace client
{
namespace utils
{
std::ostream*&
get_output_stream();
}
} // namespace client
+4 -11
Melihat File
@@ -187,17 +187,10 @@ typedef struct rocprofiler_agent_v0_t
const char* vendor_name; ///< Vendor of agent (will be AMD)
const char* product_name; ///< Marketing name
const char* model_name; ///< GPU only. Will be something like vega20, mi200, etc.
uint64_t num_pc_sampling_configs; ///< GPU only. Number of PC sampling modes available for this
///< device type. Note: if another process is currently using
///< PC sampling on this agent, this value will be zero so
///< do not assume the number of PC sampling configurations
///< based on the device type.
const rocprofiler_pc_sampling_configuration_t*
pc_sampling_configs; ///< GPU only. Array of PC sampling configuration types.
uint32_t node_id; ///< Node sequence number. This will be equivalent to the HSA-runtime
///< HSA_AMD_AGENT_INFO_DRIVER_NODE_ID property
int32_t logical_node_id; ///< Logical sequence number. This will always be [0..N) where N is
///< the total number of agents
uint32_t node_id; ///< Node sequence number. This will be equivalent to the HSA-runtime
///< HSA_AMD_AGENT_INFO_DRIVER_NODE_ID property
int32_t logical_node_id; ///< Logical sequence number. This will always be [0..N) where N is
///< the total number of agents
} rocprofiler_agent_v0_t;
typedef rocprofiler_agent_v0_t rocprofiler_agent_t;
@@ -720,7 +720,6 @@ save(ArchiveT& ar, const rocprofiler_agent_v0_t& data)
ROCP_SDK_SAVE_DATA_CSTR(vendor_name);
ROCP_SDK_SAVE_DATA_CSTR(product_name);
ROCP_SDK_SAVE_DATA_CSTR(model_name);
ROCP_SDK_SAVE_DATA_FIELD(num_pc_sampling_configs);
ROCP_SDK_SAVE_DATA_FIELD(node_id);
ROCP_SDK_SAVE_DATA_FIELD(logical_node_id);
+16 -4
Melihat File
@@ -102,6 +102,9 @@ typedef enum // NOLINT(performance-enum-size)
ROCPROFILER_STATUS_ERROR_NO_PROFILE_QUEUE, ///< Profile queue creation failed
ROCPROFILER_STATUS_ERROR_NO_HARDWARE_COUNTERS, ///< No hardware counters were specified
ROCPROFILER_STATUS_ERROR_AGENT_MISMATCH, ///< Agent mismatch between profile and context.
ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE, ///< The service is not available.
///< Please refer to API functions that return this
///< status code for more information.
ROCPROFILER_STATUS_LAST,
} rocprofiler_status_t;
@@ -400,6 +403,19 @@ typedef enum
ROCPROFILER_COUNTER_FLAG_LAST,
} rocprofiler_counter_flag_t;
/**
* @brief Enumeration for distinguishing different buffer record kinds within the
* ::ROCPROFILER_BUFFER_CATEGORY_PC_SAMPLING category
*/
typedef enum
{
ROCPROFILER_PC_SAMPLING_RECORD_NONE = 0,
ROCPROFILER_PC_SAMPLING_RECORD_SAMPLE, ///< ::rocprofiler_pc_sampling_record_t
ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_LOAD_MARKER, ///< ::rocprofiler_pc_sampling_code_object_load_marker_t
ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_UNLOAD_MARKER, ///< ::rocprofiler_pc_sampling_code_object_unload_marker_t
ROCPROFILER_PC_SAMPLING_RECORD_LAST,
} rocprofiler_pc_sampling_record_kind_t;
//--------------------------------------------------------------------------------------//
//
// ALIASES
@@ -442,10 +458,6 @@ typedef uint64_t rocprofiler_kernel_id_t;
// */
typedef uint64_t rocprofiler_dispatch_id_t;
// forward declaration of struct
typedef struct rocprofiler_pc_sampling_configuration_s rocprofiler_pc_sampling_configuration_t;
typedef struct rocprofiler_pc_sampling_record_s rocprofiler_pc_sampling_record_t;
/**
* @brief Unique record id encoding both the counter
* and dimensional values (positions) for the record.
+164 -135
Melihat File
@@ -38,53 +38,57 @@ ROCPROFILER_EXTERN_C_INIT
* @brief Function used to configure the PC sampling service on the GPU agent with @p agent_id.
*
* Prerequisites are the following:
* - The user must create a context and supply its @p context_id. By using this context,
* - The user must create a context and supply its @p context_id. By using this context,
* the user can start/stop PC sampling on the agent. For more information,
* please @see `rocprofiler_start_context`/`rocprofiler_stop_context`.
* - The user must create a buffer and supply its @p buffer_id. Rocprofiler uses the buffer
* - The user must create a buffer and supply its @p buffer_id. Rocprofiler uses the buffer
* to deliver the PC samples to the user. For more information about the data delivery,
* please @see `rocprofiler_create_buffer` and `rocprofiler_buffer_tracing_cb_t`.
* - The client must create a context and supply its @p context_id. By using this context,
* the client can start/stop PC sampling on the agent. For more information,
* please @see rocprofiler_start_context/rocprofiler_stop_context.
* - The user must create a buffer and supply its @p buffer_id. Rocprofiler-SDK uses the buffer
* to deliver the PC samples to the client. For more information about the data delivery,
* please @see rocprofiler_create_buffer and @see rocprofiler_buffer_tracing_cb_t.
*
* Before calling this function, we recommend querying PC sampling configurations
* supported by the GPU agent via the `rocprofiler_query_pc_sampling_agent_configurations`.
* The user then chooses the @p method, @p unit, and @p interval to match one of the
* available configurations. Note that the @p interval must belong to the range of values
* The user then chooses the @p method, @p unit, and @p interval to match one of the
* supported by the GPU agent via the @see rocprofiler_query_pc_sampling_agent_configurations.
* The client chooses the @p method, @p unit, and @p interval to match one of the
* available configurations. Note that the @p interval must belong to the range of values
* [available_config.min_interval, available_config.max_interval],
* where available_config is the instance of the `rocprofiler_pc_sampling_configuration_s`
* supported at the moment.
* where available_config is the instance of the @see rocprofiler_pc_sampling_configuration_s
* supported/available at the moment.
*
* Rocprofiler checks whether the requsted configuration is actually supported
* Rocprofiler-SDK checks whether the requsted configuration is actually supported
* at the moment of calling this function. If the answer is yes, it returns
* the ROCPROFILER_STATUS_SUCCESS. Otherwise, notifies the caller about the
* the @see ROCPROFILER_STATUS_SUCCESS. Otherwise, it notifies the client about the
* rejection reason via the returned status code. For more information
* about the status codes, please @see rocprofiler_status_t.
*
* There are a few constraints a client's code needs to be aware of.
*
* Constraint1: A GPU agent can be configured to support at most one running PC sampling
* configuration at any time, which implies some of the consequences described below.
* After the tool configures the PC sampling with one of the available configurations,
* rocprofiler guarantees that this configuration will be valid for the tool's
* rocprofiler-SDK guarantees that this configuration will be valid for the tool's
* lifetime. The tool can start and stop the configured PC sampling service whenever convenient.
*
* Constraint2: Since the same GPU agent can be used by multiple processes concurrently,
* Rocprofiler cannot guarantee the exclusive access to the PC sampling capability.
* Rocprofiler-SDK cannot guarantee the exclusive access to the PC sampling capability.
* The consequence is the following scenario. The tool TA that belongs to the process PA,
* calls the `rocprofiler_query_pc_sampling_agent_configurations` that returns the
* two supported configurations CA and CB by the agent. Then the toolb TB of the process PB,
* calls the @see rocprofiler_query_pc_sampling_agent_configurations that returns the
* two supported configurations CA and CB by the agent. Then the tool TB of the process PB,
* configures the PC sampling on the same agent by using the configuration CB.
* Subsequently, the TA tries configuring the CA on the agent, and it fails.
* To point out that this case happened, we introduce a special status code (TODO: ARE WE)?
* When this status code is observed by the tool TA, it queties all available configurations again
* by calling `rocprofiler_query_pc_sampling_agent_configurations`,
* To point out that this case happened, we introduce a special status code
* @see ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE.
* When this status code is observed by the tool TA, it queries all available configurations again
* by calling @see rocprofiler_query_pc_sampling_agent_configurations,
* that returns only CB this time. The tool TA can choose CB, so that both
* TA and TB use the PC sampling capability in the separate processes.
* Both TA and TB receives samples generated by the kernels launched by the
* corresponding processes PA and PB, respectively.
*
* Constraints3: We allow only one context to contain the configured PC sampling service
* within the process, that implies that at most one of the loaded tools can use PC sampling.
* One context can contains multiple PC sampling services configured for different GPU agents.
* Constraint3: Rocprofiler-SDK allows only one context to contain the configured PC sampling
* service within the process, that implies that at most one of the loaded tools can use PC
* sampling. One context can contains multiple PC sampling services configured for different GPU
* agents.
*
* Constraint4: PC sampling feature is not available within the ROCgdb.
*
* @param [in] context_id - id of the context used for starting/stopping PC sampling service
* @param [in] agent_id - id of the agent on which caller tries using PC sampling capability
@@ -93,6 +97,14 @@ ROCPROFILER_EXTERN_C_INIT
* @param [in] interval - frequency at which PC samples are generated
* @param [in] buffer_id - id of the buffer used for delivering PC samples
* @return ::rocprofiler_status_t
* @retval ::ROCPROFILER_STATUS_SUCCESS PC sampling service configured successfully
* @retval ::ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE One of the scenarios is present:
* 1. PC sampling is already configured with configuration different than requested,
* 2. PC sampling is requested from a process that runs within the ROCgdb.
* 3. HSA runtime does not support PC sampling.
* @retval ::ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL the amdgpu driver installed on the system
* does not support the PC sampling feature
* @retval ::ROCPROFILER_STATUS_ERROR a general error caused by the amdgpu driver
*
*/
rocprofiler_status_t ROCPROFILER_API
@@ -105,45 +117,45 @@ rocprofiler_configure_pc_sampling_service(rocprofiler_context_id_t conte
/**
* @brief PC sampling configuration supported by a GPU agent.
* @var rocprofiler_pc_sampling_configuration_s::method
* Sampling method supported by the GPU
* agent. Currenlty, it can take one of the following two values:
* - ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP: a background host thread
* periodically interrupts waves execution on the GPU to generate PC samples
* - ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC: performance monitoring hardware
* on the GPU periodically interrupts waves to generate PC samples.
* @var rocprofiler_pc_sampling_configuration_s::unit
* A unit used to specify the period of the
* @ref method for samples generation.
* @var rocprofiler_pc_sampling_configuration_s::min_interval
* the highest possible frequencey for
* generating samples using @ref method.
* @var rocprofiler_pc_sampling_configuration_s::max_interval
* the lowest possible frequency for
* generating samples using @ref method
* @var rocprofiler_pc_sampling_configuration_s::flags
* TODO: ???
*/
struct rocprofiler_pc_sampling_configuration_s
typedef struct
{
uint64_t size; ///< Size of this struct
rocprofiler_pc_sampling_method_t method;
rocprofiler_pc_sampling_unit_t unit;
size_t min_interval;
size_t max_interval;
uint64_t flags;
};
uint64_t flags; /// for future use
/// @var method
/// @brief Sampling method supported by the GPU agent.
/// Currently, it can take one of the following two values:
/// - ::ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP: a background host kernel thread
/// periodically interrupts waves execution on the GPU to generate PC samples
/// - ::ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC: performance monitoring hardware
/// on the GPU periodically interrupts waves to generate PC samples.
/// @var unit
/// @brief A unit used to specify the interval of the @ref method for samples generation.
/// @var min_interval
/// @brief the highest possible frequencey for generating samples using @ref method.
/// @var max_interva
/// @brief the lowest possible frequency for generating samples using @ref method
} rocprofiler_pc_sampling_configuration_t;
/**
* @brief The rocprofiler calls the tool's callback to deliver the list
* of available configurations upon the calls to the @ref
* rocprofiler_query_pc_sampling_agent_configurations.
* @brief Rocprofiler SDK's callback function to deliver the list of available PC
* sampling configurations upon the call to the
* @ref rocprofiler_query_pc_sampling_agent_configurations.
*
* @param[out] configs - The list of PC sampling configurations supported by the agent of the
* moment of invoking @ref rocprofiler_query_pc_sampling_agent_configurations.
* @param[out] num_config - The number of configuration contained in the underlying
* @param[out] configs - The array of PC sampling configurations supported by the agent
* at the moment of invoking @ref rocprofiler_query_pc_sampling_agent_configurations.
* @param[out] num_config - The number of configurations contained in the underlying array
* @p configs.
* In case the GPU agent does not support PC sampling, the value is 0.
* @param[in] user_data - A pointer passed as the last argument of the
* @param[in] user_data - client's private data passed via
* @ref rocprofiler_query_pc_sampling_agent_configurations
* @return ::rocprofiler_status_t
*/
typedef rocprofiler_status_t (*rocprofiler_available_pc_sampling_configurations_cb_t)(
const rocprofiler_pc_sampling_configuration_t* configs,
@@ -153,10 +165,24 @@ typedef rocprofiler_status_t (*rocprofiler_available_pc_sampling_configurations_
/**
* @brief Query PC Sampling Configuration.
*
* @param [in] agent_id - id of the agent for which available configuration will be listed
* Lists PC sampling configurations a GPU agent with @p agent_id supports at the moment
* of invoking the function. Delivers configurations via @p cb.
* In case the PC sampling is configured on the GPU agent, the @p cb delivers information
* about the active PC sampling configuration.
* In case the GPU agent does not support PC sampling capability,
* the @p cb delivers none PC sampling configurations.
*
* @param [in] agent_id - id of the agent for which available configurations will be listed
* @param [in] cb - User callback that delivers the available PC sampling configurations
* @param [in] user_data - passed to the @p cb
* @return ::rocprofiler_status_t
* @retval ::ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE One of the scenarios is present:
* 1. PC sampling is requested from a process that runs within the ROCgdb.
* 2. HSA runtime does not support PC sampling.
* @retval ::ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL the amdgpu driver installed on the system
* does not support the PC sampling feature.
* @retval ::ROCPROFILER_STATUS_ERROR a general error caused by the amdgpu driver
* @retval ::ROCPROFILER_STATUS_SUCCESS @p cb successfully finished
*/
rocprofiler_status_t ROCPROFILER_API
rocprofiler_query_pc_sampling_agent_configurations(
@@ -165,36 +191,31 @@ rocprofiler_query_pc_sampling_agent_configurations(
void* user_data) ROCPROFILER_NONNULL(2, 3);
/**
* @brief The header of the @ref rocprofiler_pc_sampling_record_s, indicating
* what fields of the @ref rocprofiler_pc_sampling_record_s instance are meaningful
* @brief The header of the @ref rocprofiler_pc_sampling_record_s, indicating
* what fields of the @ref rocprofiler_pc_sampling_record_s instance are meaningful
* @brief The header of the @ref rocprofiler_pc_sampling_record_t, indicating
* what fields of the @ref rocprofiler_pc_sampling_record_t instance are meaningful
* for the sample.
* @var rocprofiler_pc_sampling_header_v1_t::valid
* the sample is valid
* @var rocprofiler_pc_sampling_header_v1_t::type
* The following values are possible:
* - 0 - reserved
* - 1 - host trap pc sample
* - 2 - stochastic pc sample
* - 3 - perfcounter (unsupported at the moment)
* - other values does not mean anything at the moment
* @var rocprofiler_pc_sampling_header_v1_t::has_stall_reason
* whether the sample contains
* information about the stall reason. If so, please @see rocprofiler_pc_sampling_snapshot_v1_t.
* @var rocprofiler_pc_sampling_header_v1_t::has_wave_cnt
* whether the @ref rocprofiler_pc_sampling_record_s::wave_count contains
* meaningful value
* @var rocprofiler_pc_sampling_header_v1_t::reserved
* for future use
*/
typedef struct
{
uint8_t valid : 1;
uint8_t type : 4; // 0=reserved, 1=hosttrap, 2=stochastic
uint8_t valid : 1; /// sample is valid
uint8_t type : 4;
uint8_t has_stall_reason : 1;
uint8_t has_wave_cnt : 1;
uint8_t reserved : 1;
uint8_t reserved : 1; /// for future use
/// @var type
/// @brief The following values are possible:
/// - 0 - reserved
/// - 1 - host trap pc sample
/// - 2 - stochastic pc sample
/// - 3 - perfcounter (unsupported at the moment)
/// - other values does not mean anything at the moment
/// @var has_stall_reason
/// @brief whether the sample contains information about the stall reason.
/// If so, please @see rocprofiler_pc_sampling_snapshot_v1_t.
/// @var has_wave_cnt
/// @brief whether the @ref rocprofiler_pc_sampling_record_t::wave_count
/// contains meaningful value
} rocprofiler_pc_sampling_header_v1_t;
/**
@@ -213,65 +234,73 @@ typedef struct
// to reduce the space needed to represent a single sample.
/**
* @brief ROCProfiler PC Sampling Record corresponding to the interrupted wave.
* @var rocprofiler_pc_sampling_record_s::flags
* header that indicates what fields are meaningful
* for the PC sample. The values depend on what the underlying GPU agent architecture supports.
* @var rocprofiler_pc_sampling_record_s::chiplet
* chiplet index
* @var rocprofiler_pc_sampling_record_s::wave_id
* wave identifier within the workgroup
* @var rocprofiler_pc_sampling_record_s::wave_issued
* a flags indicated whether the wave is
* issueing the instruction' represented by the @ref pc at the moment of interruption.
* @var rocprofiler_pc_sampling_record_s::reserved
* FIXME: reserved 7 bits, must be zero.
* @var rocprofiler_pc_sampling_record_s::hw_id
* compute unit identifier
* @var rocprofiler_pc_sampling_record_s::pc
* The current program counter of the wave at the moment
* of interruption
* @var rocprofiler_pc_sampling_record_s::exec_mask
* shows how many SIMD lanes of the wave were
* executing the instruction represented by the @ref pc. Useful to understand thread-divergance
* within the wave
* @var rocprofiler_pc_sampling_record_s::workgroup_id_x
* the x coordinate of the wave within the workgroup
* @var rocprofiler_pc_sampling_record_s::workgroup_id_y
* the y coordinate of the wave within the workgroup
* @var rocprofiler_pc_sampling_record_s::workgroup_id_z
* the y coordinate of the wave within the workgroup
* @var rocprofiler_pc_sampling_record_s::wave_count
* FIXME: number of waves active at the CU at the moment of sample generation???
* @var rocprofiler_pc_sampling_record_s::timestamp
* represents the GPU timestamp when the sample is generated
* @var rocprofiler_pc_sampling_record_s::correlation_id
* correlation id of the API call that
* initiated kernel laucnh. The interrupted wave is executed as part of the kernel.
* @var rocprofiler_pc_sampling_record_s::snapshot
* TODO:
* @var rocprofiler_pc_sampling_record_s::reserved2
* for future use
*/
struct rocprofiler_pc_sampling_record_s
typedef struct
{
rocprofiler_pc_sampling_header_v1_t flags;
uint8_t chiplet;
uint8_t wave_id;
uint8_t wave_issued : 1;
uint8_t reserved : 7;
uint32_t hw_id;
uint64_t pc;
uint64_t exec_mask;
uint32_t workgroup_id_x;
uint32_t workgroup_id_y;
uint32_t workgroup_id_z;
uint32_t wave_count;
uint64_t timestamp;
rocprofiler_correlation_id_t correlation_id;
rocprofiler_pc_sampling_snapshot_v1_t snapshot;
uint32_t reserved2;
};
uint64_t size; ///< Size of this struct
rocprofiler_pc_sampling_header_v1_t flags;
uint8_t chiplet; /// chiplet index
uint8_t wave_id; /// wave identifier within the workgroup
uint8_t wave_issued : 1;
uint8_t reserved : 7; /// reserved 7 bits, must be zero
uint32_t hw_id; /// compute unit identifier
uint64_t pc; /// Program counter of the wave of the moment of interruption
uint64_t exec_mask;
rocprofiler_dim3_t workgroup_id; /// wave coordinates within the workgroup
uint32_t wave_count;
uint64_t timestamp; /// timestamp when sample is generated
rocprofiler_correlation_id_t correlation_id;
rocprofiler_pc_sampling_snapshot_v1_t
snapshot; /// @see ::rocprofiler_pc_sampling_snapshot_v1_t
uint32_t reserved2; /// for future use
/// @var flags
/// @brief indicates what fields of this struct are meaningful for the represented sample.
/// The values depend on what the underlying GPU agent architecture supports.
/// @var wave_issue
/// @brief indicates whether the wave is issueing the instruction represented by the @ref pc
/// @var exec_mask
/// @brief shows how many SIMD lanes of the wave were executing the instruction
/// represented by the @ref pc. Useful to understand thread-divergance within the wave
/// @var wave_count
/// @brief number of active waves on the CU at the moment of sample generation
/// @var correlation_id
/// @brief correlation id of the API call that initiated kernel launch.
/// The interrupted wave is executed as part of the kernel.
} rocprofiler_pc_sampling_record_t;
/**
* @brief Marker representing code object loading event.
*
* @see rocprofiler_callback_tracing_code_object_load_data_t
* for more information
*/
typedef struct
{
uint64_t size; ///< Size of this struct
uint64_t code_object_id; /// unique code object identifier
} rocprofiler_pc_sampling_code_object_load_marker_t;
/**
* @brief Marker representing code object unloading event.
*
* @see rocprofiler_callback_tracing_code_object_load_data_t
* for more information
*/
typedef struct
{
uint64_t size; ///< Size of this struct
uint64_t code_object_id; /// unique code object identifier
} rocprofiler_pc_sampling_code_object_unload_marker_t;
/** @} */
ROCPROFILER_EXTERN_C_FINI
ROCPROFILER_CXX_CODE(
static_assert(sizeof(rocprofiler_pc_sampling_record_t) == 80,
"Increasing the size of the pc sampling record is not permitted."));
ROCPROFILER_CXX_CODE(static_assert(offsetof(rocprofiler_pc_sampling_record_t, chiplet) == 9 &&
offsetof(rocprofiler_pc_sampling_record_t, reserved2) == 76,
"PC sampling record layout changed."));
@@ -48,6 +48,7 @@ add_subdirectory(thread_trace)
add_subdirectory(tracing)
add_subdirectory(kernel_dispatch)
add_subdirectory(page_migration)
add_subdirectory(details)
target_link_libraries(
rocprofiler-object-library
-27
Melihat File
@@ -355,12 +355,6 @@ read_property(const MapT& data, const std::string& label, Tp& value)
}
}
constexpr auto
compute_version(uint32_t major_v, uint32_t minor_v, uint32_t patch_v)
{
return (major_v * 10000) + (minor_v * 100) + patch_v;
}
auto
read_topology()
{
@@ -371,15 +365,6 @@ read_topology()
throw std::runtime_error{
fmt::format("sysfs nodes path '{}' does not exist", sysfs_nodes_path.string())};
using pc_sampling_config_vec_t = std::vector<rocprofiler_pc_sampling_configuration_t>;
static auto mi200_pc_sampling_config = pc_sampling_config_vec_t{
rocprofiler_pc_sampling_configuration_t{ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP,
ROCPROFILER_PC_SAMPLING_UNIT_TIME,
1UL,
1000000000UL,
0}};
const auto& cpu_info_v = get_cpu_info();
auto data = std::vector<unique_agent_t>{};
uint64_t idcount = 0;
@@ -513,18 +498,6 @@ read_topology()
}
drmClose(drm_fd);
}
// TODO(jomadsen): make contingent on whether this process acquired the PC sampling
// device lock
{
constexpr auto gfx90a_version = compute_version(9, 0, 10);
if(agent_info.gfx_target_version >= gfx90a_version)
{
agent_info.pc_sampling_configs = mi200_pc_sampling_config.data();
agent_info.num_pc_sampling_configs = mi200_pc_sampling_config.size();
}
}
}
else if(agent_info.type == ROCPROFILER_AGENT_TYPE_CPU)
{
+5
Melihat File
@@ -29,6 +29,7 @@
#include "lib/rocprofiler-sdk/context/domain.hpp"
#include "lib/rocprofiler-sdk/hsa/hsa.hpp"
#include "lib/rocprofiler-sdk/internal_threading.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include "lib/rocprofiler-sdk/registration.hpp"
#include <rocprofiler-sdk/fwd.h>
@@ -271,6 +272,10 @@ rocprofiler_create_buffer(rocprofiler_context_id_t context,
rocprofiler_status_t
rocprofiler_flush_buffer(rocprofiler_buffer_id_t buffer_id)
{
// Drain internal PC sampling buffers, if needed.
auto status = rocprofiler::pc_sampling::flush_internal_agent_buffers(buffer_id);
if(status != ROCPROFILER_STATUS_SUCCESS) return status;
return rocprofiler::buffer::flush(buffer_id, true);
}
@@ -32,6 +32,7 @@
#include "lib/rocprofiler-sdk/buffer.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/counters/core.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include "lib/rocprofiler-sdk/thread_trace/att_core.hpp"
#include <unistd.h>
@@ -322,6 +323,7 @@ start_context(rocprofiler_context_id_t context_id)
if(cfg->counter_collection) rocprofiler::counters::start_context(cfg);
if(cfg->thread_trace) cfg->thread_trace->start_context();
if(cfg->agent_counter_collection) status = rocprofiler::counters::start_agent_ctx(cfg);
if(cfg->pc_sampler) status = rocprofiler::pc_sampling::start_service(cfg);
return status;
}
@@ -357,6 +359,12 @@ stop_context(rocprofiler_context_id_t idx)
{
rocprofiler::counters::stop_agent_ctx(const_cast<context*>(_expected));
}
if(_expected->pc_sampler)
{
rocprofiler::pc_sampling::stop_service(_expected);
}
return ROCPROFILER_STATUS_SUCCESS;
}
}
@@ -33,13 +33,16 @@
#include "lib/rocprofiler-sdk/counters/agent_profiling.hpp"
#include "lib/rocprofiler-sdk/counters/core.hpp"
#include "lib/rocprofiler-sdk/external_correlation.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/types.hpp"
#include "lib/rocprofiler-sdk/thread_trace/att_core.hpp"
#include "rocprofiler-sdk/agent.h"
#include <array>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <unordered_map>
namespace rocprofiler
{
@@ -112,6 +115,16 @@ struct agent_counter_collection_service
common::Synchronized<bool> enabled{false};
};
struct pc_sampling_service
{
// Contains a map with pairs (rocprofiler_agent_id_t, PCSAgentSession*).
// The PCSAgentSession encapsulates the information about the configured PC sampling session
// used on the agent with `rocprofiler_agent_id_t`.
std::unordered_map<rocprofiler_agent_id_t,
std::unique_ptr<rocprofiler::pc_sampling::PCSAgentSession>>
agent_sessions;
};
struct context
{
// size is used to ensure that we never read past the end of the version
@@ -125,6 +138,7 @@ struct context
std::unique_ptr<dispatch_counter_collection_service> counter_collection = {};
std::unique_ptr<agent_counter_collection_service> agent_counter_collection = {};
std::shared_ptr<ThreadTracer> thread_trace = {};
std::unique_ptr<pc_sampling_service> pc_sampler = {};
};
// set the client index needs to be called before allocate_context()
@@ -0,0 +1,8 @@
#
#
#
set(ROCPROFILER_DETAILS_SOURCES)
set(ROCPROFILER_DETAILS_HEADERS kfd_ioctl.h)
target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_DETAILS_SOURCES}
${ROCPROFILER_DETAILS_HEADERS})
File diff ditekan karena terlalu besar Load Diff
@@ -29,6 +29,8 @@
#include "lib/rocprofiler-sdk/hsa/hsa.hpp"
#include "lib/rocprofiler-sdk/hsa/queue_controller.hpp"
#include "lib/rocprofiler-sdk/kernel_dispatch/tracing.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/hsa_adapter.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include "lib/rocprofiler-sdk/registration.hpp"
#include "lib/rocprofiler-sdk/tracing/tracing.hpp"
@@ -369,6 +371,12 @@ WriteInterceptor(const void* packets,
CreateBarrierPacket(nullptr, nullptr, transformed_packets);
}
if(pc_sampling::is_pc_sample_service_configured(queue.get_agent().get_rocp_agent()->id))
{
transformed_packets.emplace_back(pc_sampling::hsa::generate_marker_packet_for_kernel(
corr_id, tracing_data_v.external_correlation_ids));
}
transformed_packets.emplace_back(kernel_pkt);
// Make a copy of the original packet, adding its signal to a barrier
@@ -136,8 +136,6 @@ constexpr rocprofiler_agent_t default_agent =
.vendor_name = nullptr,
.product_name = nullptr,
.model_name = nullptr,
.num_pc_sampling_configs = 0,
.pc_sampling_configs = nullptr,
.node_id = 0,
.logical_node_id = 0};
} // namespace
@@ -256,26 +254,24 @@ QueueController::init(CoreApiTable& core_table, AmdExtTable& ext_table)
auto enable_intercepter = false;
for(const auto& itr : context::get_registered_contexts())
{
constexpr auto expected_context_size = 192UL;
constexpr auto expected_context_size = 200UL;
static_assert(
sizeof(context::context) ==
expected_context_size + sizeof(std::shared_ptr<rocprofiler::ThreadTracer>),
"If you added a new field to context struct, make sure there is a check here if it "
"requires queue interception. Once you have done so, increment expected_context_size");
if(itr->counter_collection)
bool has_kernel_tracing =
(itr->callback_tracer &&
itr->callback_tracer->domains(ROCPROFILER_CALLBACK_TRACING_KERNEL_DISPATCH)) ||
(itr->buffered_tracer &&
itr->buffered_tracer->domains(ROCPROFILER_BUFFER_TRACING_KERNEL_DISPATCH));
if(itr->counter_collection || itr->pc_sampler || has_kernel_tracing)
{
enable_intercepter = true;
break;
}
else if(itr->buffered_tracer)
{
if(itr->buffered_tracer->domains(ROCPROFILER_BUFFER_TRACING_KERNEL_DISPATCH))
{
enable_intercepter = true;
break;
}
}
else if(itr->thread_trace)
{
enable_intercepter = true;
@@ -288,15 +284,6 @@ QueueController::init(CoreApiTable& core_table, AmdExtTable& ext_table)
[trace](const AgentCache& cache, const CoreApiTable&, const AmdExtTable&) {
if(auto locked = trace.lock()) locked->resource_deinit(cache);
});
break;
}
else if(itr->callback_tracer)
{
if(itr->callback_tracer->domains(ROCPROFILER_CALLBACK_TRACING_KERNEL_DISPATCH))
{
enable_intercepter = true;
break;
}
}
}
@@ -45,6 +45,7 @@ union rocprofiler_packet
hsa_kernel_dispatch_packet_t kernel_dispatch;
hsa_barrier_and_packet_t barrier_and;
hsa_barrier_or_packet_t barrier_or;
amd_aql_intercept_marker_t marker;
rocprofiler_packet()
: ext_amd_aql_pm4{null_amd_aql_pm4_packet}
@@ -66,6 +67,10 @@ union rocprofiler_packet
: barrier_or{val}
{}
rocprofiler_packet(amd_aql_intercept_marker_t val)
: marker{val}
{}
~rocprofiler_packet() = default;
rocprofiler_packet(const rocprofiler_packet&) = default;
rocprofiler_packet(rocprofiler_packet&&) noexcept = default;
+3 -1
Melihat File
@@ -152,8 +152,10 @@ struct table_size<ROCPROFILER_COMPUTE_VERSION(1, 12, 0)>
// TODO(jomadsen): come up with a better way of handling this
# if HSA_AMD_EXT_API_TABLE_STEP_VERSION == 0x00
static constexpr size_t amd_ext = 552;
# else
# elif HSA_AMD_EXT_API_TABLE_STEP_VERSION == 0x1
static constexpr size_t amd_ext = 560;
# else
static constexpr size_t amd_ext = 568;
# endif
};
@@ -5,5 +5,3 @@ set(ROCPROFILER_LIB_UVM_HEADERS defines.hpp page_migration.hpp utils.hpp)
target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_LIB_UVM_SOURCES}
${ROCPROFILER_LIB_UVM_HEADERS})
add_subdirectory(details)
@@ -1,7 +0,0 @@
#
#
set(ROCPROFILER_LIB_UVM_DETAILS_SOURCES)
set(ROCPROFILER_LIB_UVM_DETAILS_HEADERS kfd_ioctl.h)
target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_LIB_UVM_DETAILS_SOURCES}
${ROCPROFILER_LIB_UVM_DETAILS_HEADERS})
File diff ditekan karena terlalu besar Load Diff
@@ -27,8 +27,8 @@
#include "lib/rocprofiler-sdk/agent.hpp"
#include "lib/rocprofiler-sdk/buffer.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/details/kfd_ioctl.h"
#include "lib/rocprofiler-sdk/internal_threading.hpp"
#include "lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h"
#include "lib/rocprofiler-sdk/page_migration/utils.hpp"
#include <rocprofiler-sdk/agent.h>
@@ -22,7 +22,7 @@
#pragma once
#include "lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h"
#include "lib/rocprofiler-sdk/details/kfd_ioctl.h"
#include <rocprofiler-sdk/buffer_tracing.h>
#include <rocprofiler-sdk/fwd.h>
+77 -8
Melihat File
@@ -23,10 +23,38 @@
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/pc_sampling.h>
#include "lib/common/utility.hpp"
#include "lib/common/environment.hpp"
#include "lib/rocprofiler-sdk/agent.hpp"
#include "lib/rocprofiler-sdk/buffer.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/hsa/hsa.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/types.hpp"
#include "lib/rocprofiler-sdk/registration.hpp"
using ::rocprofiler::common::consume_args;
namespace
{
/**
* @brief The functions checks if the `ROCPROFILER_PC_SAMPLING_BETA_ENABLED` is set.
* If so, it will enable PC sampling API. Otherwise, the API is reported
* as not implemented.
*
* The PC sampling is in experimental phase and its usage may hang the machine
* requiring the reboot. By enabling the `ROCPROFILER_PC_SAMPLING_BETA_ENABLED`,
* user accepts all consequences of using early implementation of PC sampling API.
*/
bool
is_pc_sampling_explicitly_enabled()
{
auto pc_sampling_enabled =
rocprofiler::common::get_env("ROCPROFILER_PC_SAMPLING_BETA_ENABLED", false);
if(!pc_sampling_enabled) LOG(ERROR) << "PC sampling unavailable\n";
return pc_sampling_enabled;
}
} // namespace
extern "C" {
rocprofiler_status_t
@@ -37,20 +65,61 @@ rocprofiler_configure_pc_sampling_service(rocprofiler_context_id_t conte
uint64_t interval,
rocprofiler_buffer_id_t buffer_id)
{
if(rocprofiler::registration::get_init_status() > 0)
if(!is_pc_sampling_explicitly_enabled()) return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
if(rocprofiler::registration::get_init_status() > -1)
return ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED;
consume_args(context_id, agent_id, method, unit, interval, buffer_id);
return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
const auto* agent = rocprofiler::agent::get_agent(agent_id);
if(!agent) return ROCPROFILER_STATUS_ERROR_AGENT_NOT_FOUND;
// checking if the registered context exists
auto* ctx = rocprofiler::context::get_mutable_registered_context(context_id);
if(!ctx) return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
// checking if the buffer is registered
auto const* buff = rocprofiler::buffer::get_buffer(buffer_id);
if(!buff) return ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND;
return rocprofiler::pc_sampling::configure_pc_sampling_service(
ctx, agent, method, unit, interval, buffer_id);
#else
(void) context_id;
(void) agent_id;
(void) method;
(void) unit;
(void) interval;
(void) buffer_id;
// ROCr runtime is missing PC sampling.
return ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE;
#endif
}
rocprofiler_status_t ROCPROFILER_API
rocprofiler_status_t
rocprofiler_query_pc_sampling_agent_configurations(
rocprofiler_agent_id_t agent_id,
rocprofiler_available_pc_sampling_configurations_cb_t cb,
void* user_data)
{
consume_args(agent_id, cb, user_data);
return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
if(!is_pc_sampling_explicitly_enabled()) return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED;
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
const auto* agent = rocprofiler::agent::get_agent(agent_id);
if(!agent) return ROCPROFILER_STATUS_ERROR_AGENT_NOT_FOUND;
std::vector<rocprofiler_pc_sampling_configuration_t> configs;
auto status = rocprofiler::pc_sampling::ioctl::ioctl_query_pcs_configs(agent, configs);
return (status == ROCPROFILER_STATUS_SUCCESS) ? cb(configs.data(), configs.size(), user_data)
: status;
#else
(void) agent_id;
(void) cb;
(void) user_data;
// ROCr runtime is missing PC sampling.
return ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE;
#endif
}
}
@@ -1 +1,14 @@
set(ROCPROFILER_PC_SAMPLING_SOURCES hsa_adapter.cpp utils.cpp service.cpp cid_manager.cpp
code_object.cpp)
set(ROCPROFILER_PC_SAMPLING_HEADERS hsa_adapter.hpp utils.hpp service.hpp types.hpp
cid_manager.hpp code_object.hpp)
target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_PC_SAMPLING_SOURCES}
${ROCPROFILER_PC_SAMPLING_HEADERS})
add_subdirectory(parser)
add_subdirectory(ioctl)
if(ROCPROFILER_BUILD_TESTS)
add_subdirectory(tests)
endif()
@@ -0,0 +1,142 @@
// MIT License
//
// Copyright (c) 2023 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.
#include "lib/rocprofiler-sdk/pc_sampling/cid_manager.hpp"
#include <algorithm>
namespace rocprofiler
{
namespace pc_sampling
{
void
PCSCIDManager::cid_async_activity_completed(context::correlation_id* cid)
{
// Hold the lock while updating the state of PCSCIDManager
std::unique_lock<std::mutex> lock(m);
// The kernel of the `cid` completed, so add cid to `q1`.
q1.emplace_back(cid);
}
void
PCSCIDManager::manage_cids_implicit(const pc_samples_copy_fn_t& pc_samples_copy_fn)
{
std::vector<context::correlation_id*> q3;
{
// To manipulate the contents of q1 and q2 and change the state of PCSCIDManager,
// acquire the lock.
std::unique_lock<std::mutex> lock(m);
// Move all CIDs from q2 to the q3 local for this function.
// Note: two buffer flushes happened since kernels of q3's CIDs completed.
q3 = std::move(q2);
// Move all CIDs from q1 to q2.
// Note: exactly one buffer flush occured since kernels of q2's CIDs completed.
q2 = std::move(q1);
// We move CIDs from one queue to another to reflect that an implicit ROCr's buffer flush
// occured. move from q1 to q2 reflects the first buffer flush since kernels of q1's CIDs
// completed move from q2 to local q3 reflects the second buffer flush since kernels of q2's
// CIDs completed.
// Empty the q1 to indicate that there are no CIDs with the following property:
// no buffer flush occured since the kernel of CID is marked completed.
q1.clear();
// The code that follows does not change the state of the PCSCIDManager, so release the lock
// implicitly.
}
// Copy PC samples from the ROCr's buffer to the SDK's buffer by invoking the passed function.
pc_samples_copy_fn();
// Exactly two implicit buffer flushes occured since kernels of q3's CIDs completed.
// Since all PC samples corresponding to these CIDs are placed in the SDK's buffer,
// decrement their reference counters to indicate that PC sampling service will not use
// these CIDs anymore.
// Eventually, CIDs retirement service will report retirement of these CIDs
// to the client tool.
// Note: the q3 is local to the function, so there is no need for inter-thread synchronization.
retire_cids_of(q3);
}
void
PCSCIDManager::manage_cids_explicit(const pc_samples_copy_fn_t& pc_samples_explicit_flush_fn)
{
std::vector<context::correlation_id*> q1_copy;
std::vector<context::correlation_id*> q2_copy;
{
// To manipulate the contents of q1 and q2 and change the state of PCSCIDManager,
// acquire the lock.
std::unique_lock<std::mutex> lock(m);
// Move all CIDs from q1 and q2 to local q1_copy and q2_copy, respectively
q1_copy = std::move(q1);
q2_copy = std::move(q2);
// Drop CIDs from q1 and q2, because the following explicit flush
// will deliver corresponding samples.
q1.clear();
q2.clear();
// The code that follows does not change the state of the PCSCIDManager, so release the lock
// implicitly.
}
// Call the passed lambda function to initiate an explicit flush of ROCr buffer by leveraging
// the `hsa_ven_amd_pcs_flush flush`. The latter function guarantees delivery of all samples
// generated (sequenced) before the call to the `hsa_ven_amd_pcs_flush`.
// Thus, all samples corresponding to CIDs of `q1_copy` and `q2_copy` will be copied
// from the ROCr's buffer to the SDK's buffer,
// meaning CIDs of `q1_copy` and `q2_copy` will not be used anymore by the PC sampling service.
pc_samples_explicit_flush_fn();
// The PC sampling service will not use q1_copy's and q2_copy's CIDs anymore, so it decrements
// their CIDs. Eventually, CIDs retirement service will report retirement of these CIDs to the
// client tool. Note: both `q1_copy` and `q2_copy` are local to the function, so there is no
// need for inter-thread synchronization.
retire_cids_of(q1_copy);
retire_cids_of(q2_copy);
}
/**
* @brief A helper function used to notify that the correlation IDs of @p q
* are ready to be retired by decrementing their ref_counters.
* Furthermore, this function notifies the PC sampling parser that
* kernels matching these CIDs are completed and can be removed from parser's
* internal maps.
*/
void
PCSCIDManager::retire_cids_of(std::vector<context::correlation_id*>& q)
{
// This function does not change the local state of the manager,
// so it does not need synchronization.
for(auto* cid : q)
{
// Notify the parser that the kernel has completed.
pcs_parser->completeDispatch(cid->internal);
// Decrement the ref_counter. Eventually, the CID is retired.
cid->sub_ref_count();
}
}
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,119 @@
// MIT License
//
// Copyright (c) 2023 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.
#pragma once
#include "lib/rocprofiler-sdk/context/correlation_id.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/pc_record_interface.hpp"
#include <functional>
#include <mutex>
#include <vector>
namespace rocprofiler
{
namespace pc_sampling
{
/**
* @brief A class that encapsulates the logic for marking the correlation IDs retired
* by PC sampling service.
*
* To reduce the overhead, SDK's PC sampling service tries to avoid flushing the ROCr's buffer
* explicitly. Instead, it waits for the ROCr to deliver the PC samples once the buffer's watermark
* is crossed.
*
* There are some subtleties we need to consider when implementing the PC sampling service.
* Currently, the 2nd level trap handler uses the double-buffering scheme, meaning the following
* scenario can occur. Assume that one of the buffers (referred to as A) is full and is reported to
* the PC sampling service via `data_ready_callback`. In the meantime, the 2nd level trap handler is
* filling the buffer B with samples of currently active kernel K that is about to finish. Let's
* mark the thread executing the `data_ready_callback` as TA. Before TA accesses the information
* about all completed correlation IDs, it might be intercepted by another thread TB that receives
* the kernel completion callback for the kernel K. While executing this callback, the thread TB
* marks the K's correlation ID as completed. After TB finishes executing the callback, the TA
* continues executing the `data_ready_callback` and observes that the K's CID has been marked as
* completed. The TA drains the buffer A and decrements ref counts of all completed CIDs including
* K's CID. If the count reaches zero, then the K's CID might be reported as retired. However, the
* buffer B might still contain samples generated by the kernel K. To be sure that PC sampling
* service drains all samples generated by the kernel K, we require one of the following
* two scenarios to happen:
*
* 1. two implicit buffer flushes happened after the kernel of the correlation ID has completed,
* 2. one explicit buffer flush initiated via `hsa_ven_amd_pcs_flush` happened after the kernel
* of the correlation ID has completed. The reason why only one explicit flush is enough is because
* the `hsa_ven_amd_pcs_flush` guarantees that all samples generated prior to (sequenced-before) the
* call to the `hsa_ven_amd_pcs_flush` will be delivered.
*
* This way, we can guarantee that all samples are
* drained from both buffers filled by 2nd level trap handler.
*
* To know if all samples produced by a kernel are drained from the ROCr's and 2nd level trap
* handler's buffers and placed in the SDK's buffer, the PC sampling service employs the CID
* retirement protocol implemented in the PCSCIDManager class. Refer to the comments of the
* PCSCIDManager's attributes and methods for more details about the CID retirement protocol.
*
* PCSCIDManager is a singleton per PCSAgentSession.
*/
class PCSCIDManager
{
/// A lock that must be hold while updating the state of PCSCIDManager.
std::mutex m;
/// Correlation IDs with the following property: no ROCr's buffer flush happened
/// since a corresponding kernel completed
std::vector<context::correlation_id*> q1;
/// Correlation IDs with the following property: exactly one ROCr's buffer flush occured
/// since a corresponding kernel completed
std::vector<context::correlation_id*> q2;
/// A pointer to the PC sampling parser to be notified when the CID is retired.
PCSamplingParserContext* pcs_parser = nullptr;
/// Prepare the CIDs of q to be retired. Refer to the implementation for more information.
void retire_cids_of(std::vector<context::correlation_id*>& q);
public:
PCSCIDManager(PCSamplingParserContext* parser)
: pcs_parser(parser)
{}
/// Called by the `kernel_completion_callback` to mark the kernel matching @p cid completed.
void cid_async_activity_completed(context::correlation_id* cid);
/// a callback function for copying PC samples from ROCr's buffer to the SDK's buffer
using pc_samples_copy_fn_t = std::function<void(void)>;
/// Called by the @p data_ready_callback.
/// Encapsulates the logic for verifying that two implicit ROCr's buffer flushes
/// happened after a kernel of the CID is marked completed (scenario 1 from above),
/// before retiring that CID.
/// @p manage_cids_implicit calls @p pc_samples_copy_fn to copy samples from
/// ROCr's buffer to the SDK's buffer.
void manage_cids_implicit(const pc_samples_copy_fn_t& pc_samples_copy_fn);
/// Called by the PC sampling service prior to initiating an explicit ROCr's buffer flush.
/// The explicit flush is initiated by the @p pc_samples_explicit_flush_fn` callback.
/// @p manage_cids_explicit` retires all CIDs whose corresponding kernels completed
/// (sequenced) before the call to the @p manage_cids_explicit (scenario 2 from above).
void manage_cids_explicit(const pc_samples_copy_fn_t& pc_samples_explicit_flush_fn);
};
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,190 @@
// 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.
#include "lib/rocprofiler-sdk/pc_sampling/code_object.hpp"
#include "lib/common/container/operators.hpp"
#include "lib/common/logging.hpp"
#include "lib/rocprofiler-sdk/code_object/code_object.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/pc_sampling.h>
#include <rocprofiler-sdk/cxx/operators.hpp>
#include <glog/logging.h>
#include <hsa/hsa.h>
#include <hsa/hsa_api_trace.h>
#include <hsa/hsa_ven_amd_loader.h>
namespace rocprofiler
{
namespace pc_sampling
{
namespace code_object
{
namespace
{
auto&
get_freeze_function()
{
static decltype(::hsa_executable_freeze)* _v = nullptr;
return _v;
}
auto&
get_destroy_function()
{
static decltype(::hsa_executable_destroy)* _v = nullptr;
return _v;
}
/**
* @brief Flush internal PC sampling buffers and generate a marker record
* for the code object load/unload event.
*
* By using the @p code_object, the function finds the corresponding agent.
* Then, it drains internal (ROCr + 2nd level trap) buffers of this agent
* and places all samples in the SDK PC sampling buffer.
* Finally, it places the marker record representing code object load/unload event
* in the SDK PC sampling buffer.
*
* @param [in] phase - loading/unloading phase
* @param [in] code_object - loaded/unloaded code object.
*/
void
flush_buffers_generate_marker_record(rocprofiler_callback_phase_t phase,
const rocprofiler::code_object::hsa::code_object& code_object)
{
auto agent_id = code_object.rocp_data.rocp_agent;
if(!is_pc_sample_service_configured(agent_id)) return;
// The PC sampling service is configured on the agent.
// Find the agent's buffer and place marker record.
// TODO: Creating a function that gives the buffer_id based on the agent_id?
const auto* pcs_service = get_configured_pc_sampling_service().load();
const auto* agent_session = pcs_service->agent_sessions.at(agent_id).get();
auto agent_buffer_id = agent_session->buffer_id;
// flush internal PC sampling buffers
flush_internal_agent_buffers(agent_buffer_id);
auto* buff = rocprofiler::buffer::get_buffer(agent_buffer_id);
// create code object load/unload marker record and emplace it into the SDK's PC SAMPLING
// buffer.
if(phase == ROCPROFILER_CALLBACK_PHASE_LOAD)
{
auto marker =
common::init_public_api_struct(rocprofiler_pc_sampling_code_object_load_marker_t{});
marker.code_object_id = code_object.rocp_data.code_object_id;
// emplace marker to the SDK's PC sampling buffer
buff->emplace(ROCPROFILER_BUFFER_CATEGORY_PC_SAMPLING,
ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_LOAD_MARKER,
marker);
}
else
{
auto marker =
common::init_public_api_struct(rocprofiler_pc_sampling_code_object_unload_marker_t{});
marker.code_object_id = code_object.rocp_data.code_object_id;
// emplace marker to the SDK's PC sampling buffer
buff->emplace(ROCPROFILER_BUFFER_CATEGORY_PC_SAMPLING,
ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_UNLOAD_MARKER,
marker);
}
// Assuming that the `rocprofiler_pc_sampling_code_object_load_marker_t` and
// `rocprofiler_pc_sampling_code_object_unload_marker_t` share the same content,
// we could replace the previous if else with the following
/*
auto marker =
common::init_public_api_struct(rocprofiler_pc_sampling_code_object_load_marker_t{});
marker.code_object_id = code_object.rocp_data.code_object_id;
// emplace marker to the SDK's PC sampling buffer
buff->emplace(ROCPROFILER_BUFFER_CATEGORY_PC_SAMPLING,
(phase == ROCPROFILER_CALLBACK_PHASE_LOAD) ?
ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_LOAD_MARKER
: ROCPROFILER_PC_SAMPLING_RECORD_CODE_OBJECT_UNLOAD_MARKER,
marker);
*/
}
hsa_status_t
executable_freeze(hsa_executable_t executable, const char* options)
{
// Call underlying function
hsa_status_t status = CHECK_NOTNULL(get_freeze_function())(executable, options);
if(status != HSA_STATUS_SUCCESS) return status;
rocprofiler::code_object::iterate_loaded_code_objects(
[&](const rocprofiler::code_object::hsa::code_object& code_object) {
if(code_object.hsa_executable == executable)
flush_buffers_generate_marker_record(ROCPROFILER_CALLBACK_PHASE_LOAD, code_object);
});
return HSA_STATUS_SUCCESS;
}
hsa_status_t
executable_destroy(hsa_executable_t executable)
{
rocprofiler::code_object::iterate_loaded_code_objects(
[&](const rocprofiler::code_object::hsa::code_object& code_object) {
if(code_object.hsa_executable == executable)
flush_buffers_generate_marker_record(ROCPROFILER_CALLBACK_PHASE_UNLOAD,
code_object);
});
// Call underlying function
return CHECK_NOTNULL(get_destroy_function())(executable);
}
} // namespace
void
initialize(HsaApiTable* table)
{
(void) table;
auto& core_table = *table->core_;
get_freeze_function() = CHECK_NOTNULL(core_table.hsa_executable_freeze_fn);
get_destroy_function() = CHECK_NOTNULL(core_table.hsa_executable_destroy_fn);
core_table.hsa_executable_freeze_fn = executable_freeze;
core_table.hsa_executable_destroy_fn = executable_destroy;
LOG_IF(FATAL, get_freeze_function() == core_table.hsa_executable_freeze_fn)
<< "infinite recursion";
LOG_IF(FATAL, get_destroy_function() == core_table.hsa_executable_destroy_fn)
<< "infinite recursion";
}
void
finalize()
{
rocprofiler::code_object::iterate_loaded_code_objects(
[&](const rocprofiler::code_object::hsa::code_object& code_object) {
flush_buffers_generate_marker_record(ROCPROFILER_CALLBACK_PHASE_UNLOAD, code_object);
});
}
} // namespace code_object
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,40 @@
// 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.
#pragma once
#include <hsa/hsa_api_trace.h>
namespace rocprofiler
{
namespace pc_sampling
{
namespace code_object
{
void
initialize(HsaApiTable* table);
void
finalize();
} // namespace code_object
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,378 @@
// MIT License
//
// Copyright (c) 2023 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.
#include "lib/rocprofiler-sdk/pc_sampling/hsa_adapter.hpp"
#include "lib/common/logging.hpp"
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/hsa/hsa.hpp"
#include "lib/rocprofiler-sdk/hsa/queue_controller.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/pc_record_interface.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/types.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/utils.hpp"
#include <hsa/hsa.h>
#include <hsa/hsa_ext_amd.h>
#include <hsa/hsa_ven_amd_pc_sampling.h>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <stdexcept>
namespace rocprofiler
{
namespace pc_sampling
{
namespace hsa
{
namespace
{
const PCSAgentSession*
get_pcs_session_of(hsa_agent_t hsa_agent)
{
// TODO: optimize this
auto* service = get_configured_pc_sampling_service().load();
for(const auto& [_, agent_session] : service->agent_sessions)
{
if(agent_session->hsa_agent->handle == hsa_agent.handle)
{
return agent_session.get();
}
}
return nullptr;
}
// Called just before the dispatch packet is put inside the real hardware queue.
void
amd_intercept_marker_handler_callback(const struct amd_aql_intercept_marker_s* packet,
hsa_queue_t* queue,
uint64_t packet_id)
{
auto* ext_table_ = rocprofiler::hsa::get_table().amd_ext_;
hsa_agent_t hsa_agent;
if(ext_table_->hsa_amd_queue_get_info_fn(queue, HSA_AMD_QUEUE_INFO_AGENT, &hsa_agent) !=
HSA_STATUS_SUCCESS)
{
throw std::runtime_error("Cannot map hsa_queue_t* to hsa_agent_t");
}
uint64_t doorbell_id = 0;
if(ext_table_->hsa_amd_queue_get_info_fn(queue, HSA_AMD_QUEUE_INFO_DOORBELL_ID, &doorbell_id) !=
HSA_STATUS_SUCCESS)
{
throw std::runtime_error("Cannot map hsa_queue_t* to doorbell_id");
}
auto internal_correlation = packet->user_data[0];
auto external_correlation = rocprofiler_user_data_t{.value = packet->user_data[1]};
auto const* pcs_session = get_pcs_session_of(hsa_agent);
assert(pcs_session);
dispatch_pkt_id_t dispatch_pkt;
dispatch_pkt.type = (pcs_session->method == ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP)
? AMD_HOST_TRAP_V1
: AMD_SNAPSHOT_V1;
// Use rocp_agent handle to uniquely identify the GPU device
dispatch_pkt.device = device_handle{static_cast<uint32_t>(pcs_session->agent->id.handle)};
dispatch_pkt.doorbell_id = doorbell_id;
dispatch_pkt.queue_size = queue->size;
dispatch_pkt.write_index = packet_id;
dispatch_pkt.correlation_id = {.internal = internal_correlation,
.external = external_correlation};
auto* parser = pcs_session->parser.get();
if(parser->shouldFlipRocrBuffer(dispatch_pkt))
{
rocprofiler::hsa::get_table().pc_sampling_ext_->hsa_ven_amd_pcs_flush_fn(
pcs_session->hsa_pc_sampling);
}
parser->newDispatch(dispatch_pkt);
}
/**
* Callback called by HSA interceptor when the kernel has completed.
*/
void
kernel_completion_cb(const rocprofiler_agent_t* rocp_agent,
rocprofiler::hsa::rocprofiler_packet& /*kernel_pkt*/,
const rocprofiler::hsa::Queue::queue_info_session_t& session)
{
// No internal correlation IDs, meaning there is no need to call CID manager.
if(!session.correlation_id) return;
// Check if the PC sampling service is configured on this agent.
if(!is_pc_sample_service_configured(rocp_agent->id)) return;
auto* service = get_configured_pc_sampling_service().load();
assert(service);
auto* agent_session = service->agent_sessions.at(rocp_agent->id).get();
// Mark the correlation ID as completed
agent_session->cid_manager->cid_async_activity_completed(session.correlation_id);
}
void
data_ready_callback(void* client_callback_data,
size_t data_size,
size_t lost_sample_count,
hsa_ven_amd_pcs_data_copy_callback_t data_copy_callback,
void* hsa_callback_data)
{
(void) lost_sample_count; // TODO: How is this exposed to the tool?
auto* agent_session = static_cast<pc_sampling::PCSAgentSession*>(client_callback_data);
// Wrap around the logic for copying PC samples from ROCr's buffer to the SDK's
// PC sampling buffer inside the lambda function called by the CID manager,
// a component responsible for managing the PC sampling related part of the
// process of retiring correlation IDs.
agent_session->cid_manager->manage_cids_implicit([&]() {
size_t samples_num = data_size / sizeof(packet_union_t);
// allocate a temporary buffer for copying PC samples
// TODO: think about how to optimize this (e.g., introduce a buffer pool)
auto buff = std::make_unique<packet_union_t[]>(samples_num);
// copy all the data
data_copy_callback(hsa_callback_data, data_size, buff.get());
upcoming_samples_t upc;
// rocp_agent handle uniquely identifies the device
upc.device = device_handle{static_cast<uint32_t>(agent_session->agent->id.handle)};
upc.which_sample_type = (agent_session->method == ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP)
? AMD_HOST_TRAP_V1
: AMD_SNAPSHOT_V1;
upc.num_samples = samples_num;
// TODO: how about using std::future
std::condition_variable cv;
auto gfx_major = ((agent_session->agent->gfx_target_version / 10000) % 100);
auto pcs_parser_status = agent_session->parser->parse(
upc, reinterpret_cast<const generic_sample_t*>(buff.get()), gfx_major, cv, false);
if(pcs_parser_status != PCSAMPLE_STATUS_SUCCESS)
{
// TODO: should we end program here or somehow report an error to the user and continue?
throw std::runtime_error("Error while parsing PC samples");
}
});
}
} // namespace
rocprofiler::hsa::rocprofiler_packet
generate_marker_packet_for_kernel(
context::correlation_id* correlation_id,
const tracing::external_correlation_id_map_t& external_correlation_ids)
{
// This function executes for each kernel dispatched to the agent on which
// the PC sampling service is configured.
// By doing this, we allow the following scenario to happen:
// A tool configures PC sampling on an agent and offloads some kernels on that agent.
// In the middle of the kernel execution, a tool starts/activates PC sampling service
// to collect samples. Although the PC sampling service was not started/activated
// at the moment of dispatching kernels, the configured PC sampling service is aware of all
// kernels dispatched on the agent and can recreate their correlation IDs.
// The disadvantage of this approach is that it introduces overhead when PC sampling
// service is inactive/stopped.
amd_aql_intercept_marker_t marker_pkt;
marker_pkt.header = HSA_PACKET_TYPE_VENDOR_SPECIFIC;
marker_pkt.format = AMD_AQL_FORMAT_INTERCEPT_MARKER;
marker_pkt.callback = amd_intercept_marker_handler_callback;
if(correlation_id != nullptr)
{
correlation_id->add_ref_count();
// Use the internal correlation ID generated by the tracing service.
marker_pkt.user_data[0] = correlation_id->internal;
// Find a context that holds PC sampling service.
auto contexts = context::get_registered_contexts(
[](const auto* ctx) { return ctx->pc_sampler != nullptr; });
assert(contexts.size() == 1);
const auto* pcs_context = contexts.at(0);
// Get an external correlation that corresponds to the context
// enclosing PC sampling service.
auto external_corr = tracing::empty_user_data;
auto external_corr_it = external_correlation_ids.find(pcs_context);
if(external_corr_it != external_correlation_ids.end())
external_corr = external_corr_it->second;
marker_pkt.user_data[1] = external_corr.value;
}
else
{
marker_pkt.user_data[0] = 0;
// No external correlation ID
marker_pkt.user_data[1] = 0;
}
return rocprofiler::hsa::rocprofiler_packet(marker_pkt);
}
void
pc_sampling_service_start(context::pc_sampling_service* service)
{
auto* pc_sampling_table_ = rocprofiler::hsa::get_table().pc_sampling_ext_;
for(const auto& [_, agent_session] : service->agent_sessions)
{
// If the agent has been hidden by the ROCR_VISIBLE_DEVICES, no need to start PC sampling.
// Please check `pc_sampling_service_finish_configuration` for more information.
if(!agent_session->hsa_agent.has_value()) continue;
if(pc_sampling_table_->hsa_ven_amd_pcs_start_fn(agent_session->hsa_pc_sampling) !=
HSA_STATUS_SUCCESS)
{
// Two concurrent calls to the pc_sampling::start_service are invoked on the same
// service. The "faster" one succeeds and starts the PC sampling service on the HSA
// level. Although the "slower fails", the service is started.
ROCP_ERROR << "HSA runtime failed to start PC sampling on the agent "
<< agent_session->agent->id.handle << "\n";
}
}
}
void
pc_sampling_service_stop(context::pc_sampling_service* service)
{
auto* pc_sampling_table_ = rocprofiler::hsa::get_table().pc_sampling_ext_;
for(const auto& [_, agent_session] : service->agent_sessions)
{
// If the agent has been hidden by the ROCR_VISIBLE_DEVICES, no need to stop PC sampling.
// Please check `pc_sampling_service_finish_configuration` for more information.
if(!agent_session->hsa_agent.has_value()) continue;
if(pc_sampling_table_->hsa_ven_amd_pcs_stop_fn(agent_session->hsa_pc_sampling) !=
HSA_STATUS_SUCCESS)
{
// Two concurrent calls to the pc_sampling::stop_serivce are invoked on the same
// service. The "faster" one succeeds and stops the PC sampling service on the HSA
// level. Although the "slower fails", the service is stopped. The "slower" continues,
// while the "faster" tries flushing the ROCr's buffer below.
ROCP_ERROR << "HSA runtime failed to stop PC sampling on the agent "
<< agent_session->agent->id.handle << "\n";
continue;
};
// Flush internal PC sampling buffers (ROCr + 2nd level trap handler buffers)
flush_internal_agent_buffers(agent_session.get());
}
}
void
pc_sampling_service_finish_configuration(context::pc_sampling_service* service)
{
// This function is executed once by a single thread.
// No synchronization needed.
auto* pc_sampling_table_ = rocprofiler::hsa::get_table().pc_sampling_ext_;
for(const auto& [_, agent_session] : service->agent_sessions)
{
// Get the HSA agent handle
agent_session->hsa_agent = rocprofiler::agent::get_hsa_agent(agent_session->agent);
// Check if HSA agent corresponding to the KFD node id is hidden via ROCR_VISIBLE_DEVICES,
// If so, we cannot finish the configuration on the ROCr level.
// Consequently, no PC samples will be delivered for this device.
if(!agent_session->hsa_agent.has_value()) continue;
// Create PC sampling session on the ROCr level.
// ROCr reuses IOCTL session with `agent_session->ioctl_pcs_id`.
hsa_status_t status = pc_sampling_table_->hsa_ven_amd_pcs_create_from_id_fn(
agent_session->ioctl_pcs_id,
agent_session->hsa_agent.value(),
pc_sampling::utils::get_matching_hsa_pcs_method(agent_session->method),
pc_sampling::utils::get_matching_hsa_pcs_units(agent_session->unit),
agent_session->interval,
pc_sampling::utils::get_hsa_pcs_latency(),
pc_sampling::utils::get_hsa_pcs_buffer_size(),
data_ready_callback,
agent_session.get(),
&agent_session->hsa_pc_sampling);
if(status != HSA_STATUS_SUCCESS)
{
ROCP_ERROR << "HSA runtime failed to finish configuring PC sampling service"
<< " on the agent with id: " << agent_session->agent->id.handle << "\n";
std::runtime_error("PC sampling config on the HSA/ROCr level failed");
}
// TODO: any better way of informing the parser about what buffer is used for a
// specific agent?
if(!agent_session->parser->register_buffer_for_agent(agent_session->buffer_id,
agent_session->agent->id))
{
std::runtime_error("PCS parser does not accept buffer");
}
}
// Register callbacks for the HSA's queue interceptor.
// TODO: should we store callback ID in the service?
rocprofiler::hsa::get_queue_controller()->add_callback(
std::nullopt,
[](const rocprofiler::hsa::Queue&,
const rocprofiler::hsa::rocprofiler_packet&,
rocprofiler_kernel_id_t /*kernel_id*/,
rocprofiler_dispatch_id_t /*dispatch_id*/,
rocprofiler_user_data_t*,
const rocprofiler::hsa::Queue::queue_info_session_t::external_corr_id_map_t&,
const context::correlation_id*) { return nullptr; },
// Completion CB
[](const rocprofiler::hsa::Queue& q,
rocprofiler::hsa::rocprofiler_packet kern_pkt,
const rocprofiler::hsa::Queue::queue_info_session_t& session,
rocprofiler::hsa::inst_pkt_t&) {
kernel_completion_cb(q.get_agent().get_rocp_agent(), kern_pkt, session);
});
}
rocprofiler_status_t
flush_internal_agent_buffers(const PCSAgentSession* agent_session)
{
// If the agent has been hidden by the ROCR_VISIBLE_DEVICES,
// there is no ROCr internal buffers to flush.
if(!agent_session->hsa_agent.has_value()) return ROCPROFILER_STATUS_SUCCESS;
auto* pc_sampling_table_ = rocprofiler::hsa::get_table().pc_sampling_ext_;
// HSA table has not been loaded, so ROCr buffers does not exist yet.
if(!pc_sampling_table_->hsa_ven_amd_pcs_flush_fn)
return ROCPROFILER_STATUS_ERROR_HSA_NOT_LOADED;
auto hsa_pcs_handle = agent_session->hsa_pc_sampling;
// Explicitly flush ROCr's buffers and sync completed CIDs.
agent_session->cid_manager->manage_cids_explicit([=]() {
// TODO: investigate whether the ROCr should maintain an extra buffer
// beyond the 2nd level trap handler buffers.
if(pc_sampling_table_->hsa_ven_amd_pcs_flush_fn(hsa_pcs_handle) != HSA_STATUS_SUCCESS)
{
// TODO: Think if it is possible to recover from this error.
std::runtime_error("Fail to flush ROCr's buffer explicitly");
}
});
return ROCPROFILER_STATUS_SUCCESS;
}
} // namespace hsa
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,56 @@
// MIT License
//
// Copyright (c) 2023 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.
#pragma once
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/hsa/queue.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/types.hpp"
#include "lib/rocprofiler-sdk/tracing/fwd.hpp"
#include <hsa/hsa_api_trace.h>
namespace rocprofiler
{
namespace pc_sampling
{
namespace hsa
{
rocprofiler::hsa::rocprofiler_packet
generate_marker_packet_for_kernel(
context::correlation_id* correlation_id,
const tracing::external_correlation_id_map_t& external_correlation_ids);
void
pc_sampling_service_start(context::pc_sampling_service* service);
void
pc_sampling_service_stop(context::pc_sampling_service* service);
void
pc_sampling_service_finish_configuration(context::pc_sampling_service* service);
rocprofiler_status_t
flush_internal_agent_buffers(const PCSAgentSession* agent_session);
} // namespace hsa
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,6 @@
set(ROCPROFILER_PC_SAMPLING_IOCTL_SOURCES ioctl_adapter.cpp)
set(ROCPROFILER_PC_SAMPLING_IOCTL_HEADERS ioctl_adapter.hpp ioctl_adapter_types.hpp)
target_sources(
rocprofiler-object-library PRIVATE ${ROCPROFILER_PC_SAMPLING_IOCTL_SOURCES}
${ROCPROFILER_PC_SAMPLING_IOCTL_HEADERS})
@@ -0,0 +1,383 @@
// MIT License
//
// Copyright (c) 2023 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.
#include "lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.hpp"
#include "lib/rocprofiler-sdk/details/kfd_ioctl.h"
#include "lib/common/logging.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter_types.hpp"
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <mutex>
#include <shared_mutex>
#include <stdexcept>
#include <vector>
namespace rocprofiler
{
namespace pc_sampling
{
namespace ioctl
{
// forward declaration
rocprofiler_ioctl_version_info_t&
get_ioctl_version();
// IOCTL 1.16 is the first one supporting PC sampling.
#define CHECK_IOCTL_VERSION \
do \
{ \
auto ioctl_version = get_ioctl_version(); \
if(ioctl_version.major_version < 1 || ioctl_version.minor_version < 16) \
{ \
LOG(ERROR) << "PC sampling unavailable\n"; \
return ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL; \
} \
} while(0)
int
kfd_open()
{
int fd = -1;
static const char kfd_device_name[] = "/dev/kfd";
fd = open(kfd_device_name, O_RDWR | O_CLOEXEC);
if(fd == -1)
{
throw std::runtime_error("Cannot open /dev/kfd");
}
return fd;
}
int
get_kfd_fd()
{
static auto _v = kfd_open();
return _v;
}
/** Call ioctl, restarting if it is interrupted
* Taken from libhsakmt.c
*/
int
ioctl(int fd, unsigned long request, void* arg)
{
int ret;
do
{
ret = ::ioctl(fd, request, arg);
} while(ret == -1 && (errno == EINTR || errno == EAGAIN));
if(ret == -1 && errno == EBADF)
{
/* In case pthread_atfork didn't catch it, this will
* make any subsequent hsaKmt calls fail in CHECK_KFD_OPEN.
*/
printf("Invalid KFD descriptor: %d\n", fd);
}
return ret * errno;
}
// More or less taken from the HsaKmt
rocprofiler_ioctl_version_info_t
query_ioctl_version(void)
{
rocprofiler_ioctl_version_info_t ioctl_version;
ioctl_version.minor_version = 0;
ioctl_version.major_version = 0;
// If querying the IOCTL version fails, return major_version/minor_version = 0;
struct kfd_ioctl_get_version_args args = {.major_version = 0, .minor_version = 0};
if(ioctl(get_kfd_fd(), AMDKFD_IOC_GET_VERSION, &args) == 0)
{
ioctl_version.major_version = args.major_version;
ioctl_version.minor_version = args.minor_version;
}
return ioctl_version;
}
rocprofiler_ioctl_version_info_t&
get_ioctl_version()
{
static auto v = query_ioctl_version();
return v;
}
/**
* @kfd_gpu_id represents the gpu identifier read from the content of the
* /sys/class/kfd/kfd/topology/nodes/<node-id>/gpu_id.
*/
ROCPROFILER_IOCTL_STATUS
ioctl_query_pc_sampling_capabilities(uint32_t kfd_gpu_id,
void* sample_info,
uint32_t sample_info_sz,
uint32_t* size)
{
int ret;
struct kfd_ioctl_pc_sample_args args;
assert(sizeof(rocprofiler_ioctl_pc_sampling_info_t) == sizeof(struct kfd_pc_sample_info));
ret = ROCPROFILER_IOCTL_STATUS_SUCCESS;
args.op = KFD_IOCTL_PCS_OP_QUERY_CAPABILITIES;
args.gpu_id = kfd_gpu_id;
args.sample_info_ptr = (uint64_t) sample_info;
args.num_sample_info = sample_info_sz;
args.flags = 0;
ret = ioctl(get_kfd_fd(), AMDKFD_IOC_PC_SAMPLE, &args);
if(ret != 0)
{
if(ret == -EBUSY)
{
// Querying PC sampling capabilities is requsted from within the ROCgdb
// which is not supported.
return ROCPROFILER_IOCTL_STATUS_UNAVAILABLE;
}
ROCP_ERROR << "IOCTL failed to query PC sampling configs: " << ret << "\n";
}
*size = args.num_sample_info;
return (ret == -ENOSPC) ? ROCPROFILER_IOCTL_STATUS_BUFFER_TOO_SMALL
: (ret != 0) ? ROCPROFILER_IOCTL_STATUS_ERROR
: ROCPROFILER_IOCTL_STATUS_SUCCESS;
}
rocprofiler_status_t
convert_ioctl_pcs_config_to_rocp(const rocprofiler_ioctl_pc_sampling_info_t& ioctl_pcs_config,
rocprofiler_pc_sampling_configuration_t& rocp_pcs_config)
{
// Sometimes, the KFD returns 0 for `method` and `units` as an error.
// Note: the 0 is not of the matching enumeration.
// Thus, the default case remains here to indicate that KFD edge case
// and prevents failures inside rocprofiler.
switch(ioctl_pcs_config.method)
{
case ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND_HOSTTRAP_V1:
rocp_pcs_config.method = ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP;
break;
case ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND_STOCHASTIC_V1:
rocp_pcs_config.method = ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC;
break;
default:
// Sampling method unsupported, return the error
return ROCPROFILER_STATUS_ERROR;
}
switch(ioctl_pcs_config.units)
{
case ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_MICROSECONDS:
rocp_pcs_config.unit = ROCPROFILER_PC_SAMPLING_UNIT_TIME;
break;
case ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_CYCLES:
rocp_pcs_config.unit = ROCPROFILER_PC_SAMPLING_UNIT_CYCLES;
break;
case ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_INSTRUCTIONS:
rocp_pcs_config.unit = ROCPROFILER_PC_SAMPLING_UNIT_INSTRUCTIONS;
break;
default:
// Sampling unit unsupported, return error
return ROCPROFILER_STATUS_ERROR;
}
if(ioctl_pcs_config.interval != 0)
{
// The pc sampling is configured on the corresponding device.
// The `interval` contains the value of the interval used for deliverying samples.
// Values of `interval_min` and `interval_max` are irrelevant.
rocp_pcs_config.min_interval = ioctl_pcs_config.interval;
rocp_pcs_config.max_interval = ioctl_pcs_config.interval;
}
else
{
// No one configured PC sampling on the corresponding device.
// Read the values of min and max interval provided by the KFD
rocp_pcs_config.min_interval = ioctl_pcs_config.interval_min;
rocp_pcs_config.max_interval = ioctl_pcs_config.interval_max;
}
rocp_pcs_config.flags = ioctl_pcs_config.flags;
return ROCPROFILER_STATUS_SUCCESS;
}
rocprofiler_status_t
ioctl_query_pcs_configs(const rocprofiler_agent_t* agent, rocp_pcs_cfgs_vec_t& rocp_configs)
{
// Assert the IOCTL version
CHECK_IOCTL_VERSION;
uint32_t kfd_gpu_id = agent->gpu_id;
const size_t ioctl_configs_num = 10;
uint32_t size = 0;
std::vector<rocprofiler_ioctl_pc_sampling_info_t> ioctl_configs(ioctl_configs_num);
auto ret = ioctl_query_pc_sampling_capabilities(
kfd_gpu_id, ioctl_configs.data(), ioctl_configs.size(), &size);
if(ret == ROCPROFILER_IOCTL_STATUS_BUFFER_TOO_SMALL)
{
ioctl_configs.resize(size);
ret = ioctl_query_pc_sampling_capabilities(
kfd_gpu_id, ioctl_configs.data(), ioctl_configs.size(), &size);
}
if(ret == ROCPROFILER_IOCTL_STATUS_UNAVAILABLE)
{
// The PC sampling is accessed from within the ROCgdb which is not supported.
return ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE;
}
else if(ret != ROCPROFILER_IOCTL_STATUS_SUCCESS)
{
ROCP_ERROR << "......... Failed while iterating over PC sampling configurations\n";
return ROCPROFILER_STATUS_ERROR;
}
for(auto const& ioctl_cfg : ioctl_configs)
{
// FIXME: Why this happens?
if(ioctl_cfg.method == 0) continue;
auto rocp_cfg = common::init_public_api_struct(rocprofiler_pc_sampling_configuration_t{});
auto rocp_ret = convert_ioctl_pcs_config_to_rocp(ioctl_cfg, rocp_cfg);
if(rocp_ret != ROCPROFILER_STATUS_SUCCESS)
{
// This should never happened, unless the KFD is broken.
continue;
}
rocp_configs.emplace_back(rocp_cfg);
}
return ROCPROFILER_STATUS_SUCCESS;
}
rocprofiler_status_t
create_ioctl_pcs_config_from_rocp(rocprofiler_ioctl_pc_sampling_info_t& ioctl_cfg,
rocprofiler_pc_sampling_method_t method,
rocprofiler_pc_sampling_unit_t unit,
uint64_t interval)
{
switch(method)
{
case ROCPROFILER_PC_SAMPLING_METHOD_NONE: return ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT;
case ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC:
ioctl_cfg.method = ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND_STOCHASTIC_V1;
break;
case ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP:
ioctl_cfg.method = ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND_HOSTTRAP_V1;
break;
case ROCPROFILER_PC_SAMPLING_METHOD_LAST: return ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT;
}
switch(unit)
{
case ROCPROFILER_PC_SAMPLING_UNIT_NONE: return ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT;
case ROCPROFILER_PC_SAMPLING_UNIT_INSTRUCTIONS:
ioctl_cfg.units = ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_INSTRUCTIONS;
break;
case ROCPROFILER_PC_SAMPLING_UNIT_CYCLES:
ioctl_cfg.units = ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_CYCLES;
break;
case ROCPROFILER_PC_SAMPLING_UNIT_TIME:
ioctl_cfg.units = ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_MICROSECONDS;
break;
case ROCPROFILER_PC_SAMPLING_UNIT_LAST: return ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT;
}
ioctl_cfg.interval = interval;
// TODO: Is it possible to use flags for interval values that are power of 2
// when specifying stochastic on MI300?
ioctl_cfg.flags = 0;
ioctl_cfg.interval_min = 0;
ioctl_cfg.interval_max = 0;
return ROCPROFILER_STATUS_SUCCESS;
}
/**
* @brief Reserve PC sampling service on the device
* @param[out] ioctl_pcs_id - If the return value is ROCPROFILER_STATUS_SUCCESS,
* contains the id that uniquely identifies PC sampling session within IOCTL.
*/
rocprofiler_status_t
ioctl_pcs_create(const rocprofiler_agent_t* agent,
rocprofiler_pc_sampling_method_t method,
rocprofiler_pc_sampling_unit_t unit,
uint64_t interval,
uint32_t* ioctl_pcs_id)
{
// Assert the IOCTL version
CHECK_IOCTL_VERSION;
rocprofiler_ioctl_pc_sampling_info_t ioctl_cfg;
auto ret = create_ioctl_pcs_config_from_rocp(ioctl_cfg, method, unit, interval);
if(ret != ROCPROFILER_STATUS_SUCCESS)
{
return ret;
}
struct kfd_ioctl_pc_sample_args args;
if(!ioctl_pcs_id) return ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT;
*ioctl_pcs_id = INVALID_TRACE_ID;
args.op = KFD_IOCTL_PCS_OP_CREATE;
args.gpu_id = agent->gpu_id;
args.sample_info_ptr = (uint64_t)(&ioctl_cfg);
args.num_sample_info = 1;
args.trace_id = INVALID_TRACE_ID;
auto ioctl_ret = ioctl(get_kfd_fd(), AMDKFD_IOC_PC_SAMPLE, &args);
*ioctl_pcs_id = args.trace_id;
if(ioctl_ret != 0 && (errno == EBUSY || errno == EEXIST))
{
// Currently, KFD uses EBUSY when e.g., PC sampling create is requested from
// withing the ROCgdb.
// On the other hand, EEXIST is used when one tries to create a PC sampling
// with a configuration different than the one already active.
return ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE;
}
else if(ioctl_ret != 0)
{
return ROCPROFILER_STATUS_ERROR;
}
return ROCPROFILER_STATUS_SUCCESS;
}
} // namespace ioctl
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,50 @@
// MIT License
//
// Copyright (c) 2023 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.
#include <rocprofiler-sdk/fwd.h>
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/types.hpp"
#include <vector>
namespace rocprofiler
{
namespace pc_sampling
{
namespace ioctl
{
using rocp_pcs_cfgs_vec_t = std::vector<rocprofiler_pc_sampling_configuration_t>;
rocprofiler_status_t
ioctl_query_pcs_configs(const rocprofiler_agent_t* agent, rocp_pcs_cfgs_vec_t& rocp_configs);
rocprofiler_status_t
ioctl_pcs_create(const rocprofiler_agent_t* agent,
rocprofiler_pc_sampling_method_t method,
rocprofiler_pc_sampling_unit_t unit,
uint64_t interval,
uint32_t* ioctl_pcs_id);
} // namespace ioctl
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,108 @@
// MIT License
//
// Copyright (c) 2023 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.
#include <rocprofiler-sdk/fwd.h>
#include "lib/rocprofiler-sdk/context/context.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/types.hpp"
#include <vector>
namespace rocprofiler
{
namespace pc_sampling
{
namespace ioctl
{
#define INVALID_TRACE_ID 0x0
// The data structure copied from the HsaKmt
// Currently, we are using the following status codes:
// 1. ROCPROFILER_IOCTL_STATUS_SUCCESS
// 2. ROCPROFILER_IOCTL_STATUS_ERROR
// 3. ROCPROFILER_IOCTL_STATUS_BUFFER_TOO_SMALL
// 4. ROCPROFILER_IOCTL_STATUS_UNAVAILABLE
// We might replace 1, 2, and 4 with rocprofiler_status_t, but still lacking a counterpart
// for the ROCPROFILER_IOCTL_STATUS_BUFFER_TOO_SMALL
typedef enum _ROCPROFILER_IOCTL_STATUS
{
ROCPROFILER_IOCTL_STATUS_SUCCESS = 0, /// Operation successful // USED
ROCPROFILER_IOCTL_STATUS_ERROR = 1, /// General error return if not otherwise specified // USED
ROCPROFILER_IOCTL_STATUS_DRIVER_MISMATCH =
2, /// User mode component is not compatible with kernel HSA driver
ROCPROFILER_IOCTL_STATUS_INVALID_NODE_UNIT =
5, /// KFD identifies node or unit parameter invalid
ROCPROFILER_IOCTL_STATUS_NO_MEMORY =
6, /// No memory available (when allocating queues or memory)
ROCPROFILER_IOCTL_STATUS_BUFFER_TOO_SMALL =
7, /// A buffer needed to handle a request is too small //USED
ROCPROFILER_IOCTL_STATUS_NOT_IMPLEMENTED =
10, /// KFD function is not implemented for this set of paramters
ROCPROFILER_IOCTL_STATUS_UNAVAILABLE = 12, /// KFD function is not available currently on this
/// // USED node (but may be at a later time)
ROCPROFILER_IOCTL_STATUS_OUT_OF_RESOURCES =
13, /// KFD function request exceeds the resources currently available.
ROCPROFILER_IOCTL_STATUS_KERNEL_COMMUNICATION_ERROR =
21, /// user-kernel mode communication failure
ROCPROFILER_IOCTL_STATUS_KERNEL_ALREADY_OPENED = 22, /// KFD driver path already opened
ROCPROFILER_IOCTL_STATUS_HSAMMU_UNAVAILABLE =
23, /// ATS/PRI 1.1 (Address Translation Services) not available
/// (IOMMU driver not installed or not-available)
ROCPROFILER_IOCTL_STATUS_WAIT_FAILURE = 30, /// The wait operation failed
ROCPROFILER_IOCTL_STATUS_WAIT_TIMEOUT = 31, /// The wait operation timed out
ROCPROFILER_IOCTL_STATUS_MEMORY_ALREADY_REGISTERED = 35, /// Memory buffer already registered
ROCPROFILER_IOCTL_STATUS_MEMORY_NOT_REGISTERED = 36, /// Memory buffer not registered
ROCPROFILER_IOCTL_STATUS_MEMORY_ALIGNMENT = 37, /// Memory parameter not aligned
} ROCPROFILER_IOCTL_STATUS;
typedef struct rocprofiler_ioctl_version_info_s
{
uint32_t major_version; /// supported IOCTL interface major version
uint32_t minor_version; /// supported IOCTL interface minor version
} rocprofiler_ioctl_version_info_t;
typedef enum _ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND
{
ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND_HOSTTRAP_V1 = 1,
ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND_STOCHASTIC_V1,
} ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND;
typedef enum _ROCPROFILER_IOCTL_PC_SAMPLING_UNITS
{
ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_MICROSECONDS,
ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_CYCLES,
ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL_INSTRUCTIONS,
} ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL;
typedef struct rocprofiler_ioctl_pc_sampling_info_s
{
uint64_t interval;
uint64_t interval_min;
uint64_t interval_max;
uint64_t flags;
ROCPROFILER_IOCTL_PC_SAMPLING_METHOD_KIND method;
ROCPROFILER_IOCTL_PC_SAMPLING_UNIT_INTERVAL units;
} rocprofiler_ioctl_pc_sampling_info_t;
} // namespace ioctl
} // namespace pc_sampling
} // namespace rocprofiler
@@ -184,17 +184,20 @@ add_upcoming_samples(const device_handle device,
const generic_sample_t* buffer,
const size_t available_samples,
Parser::CorrelationMap* corr_map,
rocprofiler_pc_sampling_record_s* samples)
rocprofiler_pc_sampling_record_t* samples)
{
pcsample_status_t status = PCSAMPLE_STATUS_SUCCESS;
for(uint64_t p = 0; p < available_samples; p++)
{
const auto* snap = reinterpret_cast<const perf_sample_snapshot_v1*>(buffer + p);
samples[p] = copySample<bHostTrap, GFXIP>((const void*) (buffer + p));
samples[p].size = 0; // pc sampling record with size 0 will indicate invalid sample
try
{
Parser::trap_correlation_id_t trap{.raw = snap->correlation_id};
samples[p].correlation_id = corr_map->get(device, trap);
samples[p].size = sizeof(rocprofiler_pc_sampling_record_t);
// set size after corr_map->get which may throw
} catch(std::exception& e)
{
status = PCSAMPLE_STATUS_PARSER_ERROR;
@@ -240,7 +243,7 @@ _parse_buffer(generic_sample_t* buffer,
while(pkt_counter > 0)
{
rocprofiler_pc_sampling_record_s* samples = nullptr;
rocprofiler_pc_sampling_record_t* samples = nullptr;
uint64_t available_samples = callback(&samples, pkt_counter, userdata);
if(available_samples == 0 || available_samples > pkt_counter)
@@ -87,7 +87,7 @@ union pcsample_header_v1_t
uint8_t raw;
};
typedef uint64_t (*user_callback_t)(rocprofiler_pc_sampling_record_s**, uint64_t, void*);
typedef uint64_t (*user_callback_t)(rocprofiler_pc_sampling_record_t**, uint64_t, void*);
/**
* The types of errors to be returned by parse_buffer.
@@ -23,7 +23,7 @@
#include "lib/rocprofiler-sdk/pc_sampling/parser/pc_record_interface.hpp"
uint64_t
PCSamplingParserContext::alloc(rocprofiler_pc_sampling_record_s** buffer, uint64_t size)
PCSamplingParserContext::alloc(rocprofiler_pc_sampling_record_t** buffer, uint64_t size)
{
std::unique_lock<std::shared_mutex> lock(mut);
assert(buffer != nullptr);
@@ -97,3 +97,21 @@ PCSamplingParserContext::shouldFlipRocrBuffer(const dispatch_pkt_id_t& pkt) cons
std::shared_lock<std::shared_mutex> lock(mut);
return corr_map->checkDispatch(pkt);
}
void
PCSamplingParserContext::generate_upcoming_pc_record(
uint64_t agent_id_handle,
const rocprofiler_pc_sampling_record_t* samples,
size_t num_samples)
{
auto buff_id = _agent_buffers.at(rocprofiler_agent_id_t{agent_id_handle});
rocprofiler::buffer::instance* buff = rocprofiler::buffer::get_buffer(buff_id);
if(!buff)
throw std::runtime_error(fmt::format("Buffer with id: {} does not exists", buff_id.handle));
for(size_t i = 0; i < num_samples; i++)
buff->emplace(ROCPROFILER_BUFFER_CATEGORY_PC_SAMPLING,
ROCPROFILER_PC_SAMPLING_RECORD_SAMPLE,
samples[i]);
};
@@ -22,23 +22,32 @@
#pragma once
#include "lib/rocprofiler-sdk/buffer.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/correlation.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/parser_types.h"
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/cxx/hash.hpp>
#include <rocprofiler-sdk/cxx/operators.hpp>
#include <fmt/core.h>
#include <sys/types.h>
#include <cassert>
#include <condition_variable>
#include <cstdint>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <unordered_set>
#include "lib/rocprofiler-sdk/pc_sampling/parser/correlation.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/parser_types.h"
struct PCSamplingData
{
PCSamplingData(size_t size)
: samples(size){};
PCSamplingData& operator=(PCSamplingData&) = delete;
std::vector<rocprofiler_pc_sampling_record_s> samples;
std::vector<rocprofiler_pc_sampling_record_t> samples;
};
class PCSamplingParserContext
@@ -52,7 +61,7 @@ public:
* @param[in] size Number of samples requested.
* @returns Number of samples actually allocated on *buffer.
*/
uint64_t alloc(rocprofiler_pc_sampling_record_s** buffer, uint64_t size);
uint64_t alloc(rocprofiler_pc_sampling_record_t** buffer, uint64_t size);
/**
* @brief Parses a chunk of samples.
@@ -95,6 +104,24 @@ public:
*/
bool shouldFlipRocrBuffer(const dispatch_pkt_id_t& pkt) const;
bool register_buffer_for_agent(rocprofiler_buffer_id_t buffer_id,
rocprofiler_agent_id_t agent_id)
{
std::unique_lock<std::shared_mutex> lock(mut);
// Single buffer per agent is allowed
if(_agent_buffers.count(agent_id) > 0) return false;
_agent_buffers.emplace(agent_id, buffer_id);
return true;
}
void unregister_buffer_from_agent(rocprofiler_agent_id_t agent_id)
{
std::unique_lock<std::shared_mutex> lock(mut);
_agent_buffers.erase(agent_id);
}
protected:
/**
* @brief Parses the given input data and generates pc sampling records.
@@ -103,7 +130,7 @@ protected:
template <typename GFX>
pcsample_status_t _parse(const upcoming_samples_t& upcoming, const generic_sample_t* data_)
{
std::shared_lock<std::shared_mutex> lock(mut);
// std::shared_lock<std::shared_mutex> lock(mut);
pcsample_status_t status = PCSAMPLE_STATUS_SUCCESS;
uint64_t pkt_counter = upcoming.num_samples;
@@ -112,7 +139,7 @@ protected:
while(pkt_counter > 0)
{
rocprofiler_pc_sampling_record_s* samples = nullptr;
rocprofiler_pc_sampling_record_t* samples = nullptr;
uint64_t memsize = alloc(&samples, pkt_counter);
if(memsize == 0 || memsize > pkt_counter) return PCSAMPLE_STATUS_CALLBACK_ERROR;
@@ -125,7 +152,7 @@ protected:
data_ += memsize;
pkt_counter -= memsize;
generate_upcoming_pc_record(samples, memsize);
generate_upcoming_pc_record(dev.handle, samples, memsize);
}
return status;
@@ -137,12 +164,9 @@ protected:
*/
pcsample_status_t flushForgetList();
static void generate_id_completion_record(const dispatch_pkt_id_t& pkt) { (void) pkt; };
static void generate_upcoming_pc_record(const rocprofiler_pc_sampling_record_s* samples,
size_t num_samples)
{
(void) samples;
(void) num_samples;
};
void generate_upcoming_pc_record(uint64_t agent_id_handle,
const rocprofiler_pc_sampling_record_t* samples,
size_t num_samples);
//! Maps doorbells and dispatch_index to correlation_id
std::unique_ptr<Parser::CorrelationMap> corr_map;
@@ -156,4 +180,7 @@ protected:
std::unordered_set<uint64_t> forget_list;
mutable std::shared_mutex mut;
private:
std::unordered_map<rocprofiler_agent_id_t, rocprofiler_buffer_id_t> _agent_buffers;
};
@@ -56,8 +56,8 @@ Benchmark(bool bWarmup)
for(size_t i = 0; i < SAMPLE_PER_DISPATCH; i++)
MockWave(dispatch).genPCSample();
std::pair<rocprofiler_pc_sampling_record_s*, size_t> userdata;
userdata.first = new rocprofiler_pc_sampling_record_s[TOTAL_NUM_SAMPLES];
std::pair<rocprofiler_pc_sampling_record_t*, size_t> userdata;
userdata.first = new rocprofiler_pc_sampling_record_t[TOTAL_NUM_SAMPLES];
userdata.second = TOTAL_NUM_SAMPLES;
auto t0 = std::chrono::system_clock::now();
@@ -65,9 +65,9 @@ Benchmark(bool bWarmup)
(generic_sample_t*) buffer->packets.data(),
buffer->packets.size(),
GFXIP_MAJOR,
[](rocprofiler_pc_sampling_record_s** sample, uint64_t size, void* userdata_) {
[](rocprofiler_pc_sampling_record_t** sample, uint64_t size, void* userdata_) {
auto* pair =
reinterpret_cast<std::pair<rocprofiler_pc_sampling_record_s*, size_t>*>(userdata_);
reinterpret_cast<std::pair<rocprofiler_pc_sampling_record_t*, size_t>*>(userdata_);
assert(TOTAL_NUM_SAMPLES == pair->second);
*sample = pair->first;
return size;
@@ -80,7 +80,7 @@ Benchmark(bool bWarmup)
{
std::cout << "Benchmark: Parsed " << int(samples_per_us * 1E3f + 0.5f) * 1E-3f
<< " Msample/s (";
std::cout << int(sizeof(rocprofiler_pc_sampling_record_s) * samples_per_us) << " MB/s)"
std::cout << int(sizeof(rocprofiler_pc_sampling_record_t) * samples_per_us) << " MB/s)"
<< std::endl;
}
@@ -33,14 +33,14 @@ std::mt19937 rdgen(1);
/**
* Sample user memory allocation callback.
* It expects userdata to be cast-able to a pointer to
* std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>>
* std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>>
*/
static uint64_t
alloc_callback(rocprofiler_pc_sampling_record_s** buffer, uint64_t size, void* userdata)
alloc_callback(rocprofiler_pc_sampling_record_t** buffer, uint64_t size, void* userdata)
{
*buffer = new rocprofiler_pc_sampling_record_s[size];
*buffer = new rocprofiler_pc_sampling_record_t[size];
auto& vector =
*reinterpret_cast<std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>>*>(
*reinterpret_cast<std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>>*>(
userdata);
vector.push_back({*buffer, size});
return size;
@@ -51,7 +51,7 @@ alloc_callback(rocprofiler_pc_sampling_record_s** buffer, uint64_t size, void* u
* the reconstructed correlation_id.
*/
static bool
check_samples(rocprofiler_pc_sampling_record_s* samples, uint64_t size)
check_samples(rocprofiler_pc_sampling_record_t* samples, uint64_t size)
{
for(size_t i = 0; i < size; i++)
if(samples[i].correlation_id.internal != samples[i].pc) return false;
@@ -71,7 +71,7 @@ TEST(pcs_parser, hello_world)
MockWave(dispatch).genPCSample();
MockWave(dispatch).genPCSample();
std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>> all_allocations;
std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>> all_allocations;
CHECK_PARSER(parse_buffer((generic_sample_t*) buffer->packets.data(),
buffer->packets.size(),
@@ -114,7 +114,7 @@ TEST(pcs_parser, reverse_wave_order)
for(auto it = dispatches.begin(); it != dispatches.end(); it++)
MockWave(*it).genPCSample();
std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>> all_allocations;
std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>> all_allocations;
CHECK_PARSER(parse_buffer((generic_sample_t*) buffer->packets.data(),
buffer->packets.size(),
@@ -150,7 +150,7 @@ TEST(pcs_parser, dispatch_wrapping)
MockWave(dispatch).genPCSample();
}
std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>> all_allocations;
std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>> all_allocations;
CHECK_PARSER(parse_buffer((generic_sample_t*) buffer->packets.data(),
buffer->packets.size(),
@@ -197,7 +197,7 @@ TEST(pcs_parser, random_samples)
for(int i = 0; i < num_samples; i++)
MockWave(dispatches[rdgen() % dispatches.size()]).genPCSample();
std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>> all_allocations;
std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>> all_allocations;
CHECK_PARSER(parse_buffer((generic_sample_t*) buffer->packets.data(),
buffer->packets.size(),
@@ -290,7 +290,7 @@ TEST(pcs_parser, queue_hammer)
<< std::endl;
std::cout << "Max queue occupancy: " << max_q_occupancy << "\n\n" << std::endl;
std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>> all_allocations;
std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>> all_allocations;
CHECK_PARSER(parse_buffer((generic_sample_t*) buffer->packets.data(),
buffer->packets.size(),
@@ -302,7 +302,7 @@ TEST(pcs_parser, queue_hammer)
NUM_ACTIONS); // QueueHammer test: Incorrect number of callbacks
for(auto sb = 0ul; sb < all_allocations.size(); sb++)
{
rocprofiler_pc_sampling_record_s* samples = all_allocations[sb].first;
rocprofiler_pc_sampling_record_t* samples = all_allocations[sb].first;
size_t num_samples = all_allocations[sb].second;
EXPECT_EQ(num_samples, NUM_QUEUES); // QueueHammer: Incorrect number of samples
@@ -329,7 +329,7 @@ TEST(pcs_parser, multi_buffer)
const auto& packets = firstBuffer->packets;
secondBuffer->packets = std::vector<packet_union_t>(packets.begin() + 2, packets.end());
std::vector<std::pair<rocprofiler_pc_sampling_record_s*, uint64_t>> all_allocations;
std::vector<std::pair<rocprofiler_pc_sampling_record_t*, uint64_t>> all_allocations;
CHECK_PARSER(parse_buffer((generic_sample_t*) firstBuffer->packets.data(),
firstBuffer->packets.size(),
@@ -24,13 +24,15 @@
# undef NDEBUG
#endif
#include "lib/rocprofiler-sdk/pc_sampling/parser/pc_record_interface.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/tests/mocks.hpp"
#include <rocprofiler-sdk/cxx/operators.hpp>
#include <gtest/gtest.h>
#include <cassert>
#include <cstddef>
#include "lib/rocprofiler-sdk/pc_sampling/parser/pc_record_interface.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/tests/mocks.hpp"
#define GFXIP_MAJOR 9
#define TYPECHECK(x) \
@@ -295,7 +297,7 @@ class WaveIssueAndErrorTest : public WaveSnapTest
void genPCSample(bool valid, bool issued, bool dual, bool error)
{
rocprofiler_pc_sampling_record_s sample;
rocprofiler_pc_sampling_record_t sample;
::memset(&sample, 0, sizeof(sample));
sample.pc = dispatch->unique_id;
sample.correlation_id.internal = dispatch->getMockId().raw;
@@ -320,7 +322,7 @@ class WaveIssueAndErrorTest : public WaveSnapTest
dispatch->submit(std::move(pss));
};
std::vector<rocprofiler_pc_sampling_record_s> compare;
std::vector<rocprofiler_pc_sampling_record_t> compare;
};
class WaveOtherFieldsTest : public WaveSnapTest
@@ -347,9 +349,7 @@ class WaveOtherFieldsTest : public WaveSnapTest
assert(parsed[0][i].flags.reserved == false);
assert(compare[i].exec_mask == parsed[0][i].exec_mask);
assert(compare[i].workgroup_id_x == parsed[0][i].workgroup_id_x);
assert(compare[i].workgroup_id_y == parsed[0][i].workgroup_id_y);
assert(compare[i].workgroup_id_z == parsed[0][i].workgroup_id_z);
assert(compare[i].workgroup_id == parsed[0][i].workgroup_id);
assert(compare[i].chiplet == parsed[0][i].chiplet);
assert(compare[i].wave_id == parsed[0][i].wave_id);
@@ -360,13 +360,13 @@ class WaveOtherFieldsTest : public WaveSnapTest
void genPCSample(int pc, int exec, int blkx, int blky, int blkz, int chip, int wave, int hwid)
{
rocprofiler_pc_sampling_record_s sample;
rocprofiler_pc_sampling_record_t sample;
::memset(&sample, 0, sizeof(sample));
sample.exec_mask = exec;
sample.workgroup_id_x = blkx;
sample.workgroup_id_y = blky;
sample.workgroup_id_z = blkz;
sample.workgroup_id.x = blkx;
sample.workgroup_id.y = blky;
sample.workgroup_id.z = blkz;
sample.chiplet = chip;
sample.wave_id = wave;
@@ -392,7 +392,7 @@ class WaveOtherFieldsTest : public WaveSnapTest
(void) pc;
};
std::vector<rocprofiler_pc_sampling_record_s> compare;
std::vector<rocprofiler_pc_sampling_record_t> compare;
};
TEST(pcs_parser, gfx9_test)
@@ -65,7 +65,7 @@ public:
submit(uni);
}
std::vector<std::vector<rocprofiler_pc_sampling_record_s>> get_parsed_buffer(int GFXIP_MAJOR)
std::vector<std::vector<rocprofiler_pc_sampling_record_t>> get_parsed_buffer(int GFXIP_MAJOR)
{
parsed_data = {};
@@ -78,18 +78,18 @@ public:
return parsed_data;
}
static uint64_t alloc_parse_memory(rocprofiler_pc_sampling_record_s** sample,
static uint64_t alloc_parse_memory(rocprofiler_pc_sampling_record_t** sample,
uint64_t req_size,
void* userdata)
{
auto* buffer = reinterpret_cast<MockRuntimeBuffer*>(userdata);
buffer->parsed_data.push_back(std::vector<rocprofiler_pc_sampling_record_s>(req_size));
buffer->parsed_data.push_back(std::vector<rocprofiler_pc_sampling_record_t>(req_size));
*sample = buffer->parsed_data.back().data();
return req_size;
}
std::vector<packet_union_t> packets;
std::vector<std::vector<rocprofiler_pc_sampling_record_s>> parsed_data;
std::vector<std::vector<rocprofiler_pc_sampling_record_t>> parsed_data;
};
/**
@@ -32,18 +32,18 @@
#include "lib/rocprofiler-sdk/pc_sampling/parser/rocr.h"
template <typename SType>
inline rocprofiler_pc_sampling_record_s
inline rocprofiler_pc_sampling_record_t
copySampleHeader(const SType& sample)
{
rocprofiler_pc_sampling_record_s ret;
rocprofiler_pc_sampling_record_t ret;
ret.flags = pcsample_header_v1_t{.raw = 0}.flags;
ret.flags.type = AMD_SNAPSHOT_V1;
ret.pc = sample.pc;
ret.exec_mask = sample.exec_mask;
ret.workgroup_id_x = sample.workgroup_id_x;
ret.workgroup_id_y = sample.workgroup_id_y;
ret.workgroup_id_z = sample.workgroup_id_z;
ret.workgroup_id.x = sample.workgroup_id_x;
ret.workgroup_id.y = sample.workgroup_id_y;
ret.workgroup_id.z = sample.workgroup_id_z;
ret.chiplet = sample.chiplet_and_wave_id >> 8;
ret.wave_id = sample.chiplet_and_wave_id & 0x3F;
@@ -52,23 +52,23 @@ copySampleHeader(const SType& sample)
return ret;
}
inline rocprofiler_pc_sampling_record_s
inline rocprofiler_pc_sampling_record_t
copyHostTrapSample(const perf_sample_host_trap_v1& sample)
{
rocprofiler_pc_sampling_record_s ret = copySampleHeader<perf_sample_host_trap_v1>(sample);
rocprofiler_pc_sampling_record_t ret = copySampleHeader<perf_sample_host_trap_v1>(sample);
ret.flags.type = AMD_HOST_TRAP_V1;
return ret;
}
template <typename gfx>
inline rocprofiler_pc_sampling_record_s
inline rocprofiler_pc_sampling_record_t
copyStochasticSample(const perf_sample_snapshot_v1& sample);
template <>
inline rocprofiler_pc_sampling_record_s
inline rocprofiler_pc_sampling_record_t
copyStochasticSample<GFX9>(const perf_sample_snapshot_v1& sample)
{
rocprofiler_pc_sampling_record_s ret = copySampleHeader<perf_sample_snapshot_v1>(sample);
rocprofiler_pc_sampling_record_t ret = copySampleHeader<perf_sample_snapshot_v1>(sample);
ret.flags.valid = sample.perf_snapshot_data & (~sample.perf_snapshot_data >> 26) & 0x1;
// Check wave_id matches snapshot_wave_id
@@ -88,10 +88,10 @@ copyStochasticSample<GFX9>(const perf_sample_snapshot_v1& sample)
}
template <>
inline rocprofiler_pc_sampling_record_s
inline rocprofiler_pc_sampling_record_t
copyStochasticSample<GFX11>(const perf_sample_snapshot_v1& sample)
{
rocprofiler_pc_sampling_record_s ret = copySampleHeader<perf_sample_snapshot_v1>(sample);
rocprofiler_pc_sampling_record_t ret = copySampleHeader<perf_sample_snapshot_v1>(sample);
ret.flags.valid = sample.perf_snapshot_data & (~sample.perf_snapshot_data >> 23) & 0x1;
// Check wave_id matches snapshot_wave_id
@@ -195,12 +195,12 @@ translate_inst(int in)
#undef LUTOVERLOAD
template <bool HostTrap, typename GFX>
inline rocprofiler_pc_sampling_record_s
inline rocprofiler_pc_sampling_record_t
copySample(const void* sample)
{
if(HostTrap) return copyHostTrapSample(*(const perf_sample_host_trap_v1*) sample);
rocprofiler_pc_sampling_record_s ret =
rocprofiler_pc_sampling_record_t ret =
copyStochasticSample<GFX>(*(const perf_sample_snapshot_v1*) sample);
ret.snapshot.inst_type = translate_inst<GFX>(ret.snapshot.inst_type);
@@ -0,0 +1,268 @@
// MIT License
//
// Copyright (c) 2023 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.
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include "lib/common/logging.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/hsa_adapter.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/ioctl/ioctl_adapter.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/utils.hpp"
namespace rocprofiler
{
namespace pc_sampling
{
using hsa_initialized_t = std::atomic<bool>;
hsa_initialized_t&
is_hsa_initialized()
{
static auto _v = hsa_initialized_t{false};
return _v;
}
// The function returns the atomic pointer to the active PC sampling service.
// The nullptr means the PC sampling service is inactive.
atomic_pc_sampling_service_t&
get_active_pc_sampling_service()
{
static auto _v = atomic_pc_sampling_service_t{nullptr};
return _v;
}
// The function returns the atomic pointer to the configured pc sampling service.
// The nullptr means the PC sampling service is not configured.
atomic_pc_sampling_service_t&
get_configured_pc_sampling_service()
{
static auto _v = atomic_pc_sampling_service_t{nullptr};
return _v;
}
rocprofiler_status_t
start_service(const context::context* ctx)
{
auto* service = ctx->pc_sampler.get();
context::pc_sampling_service* _expected = nullptr;
// If there is no active pc_sampling_service, mark `service` as activated.
bool success = get_active_pc_sampling_service().compare_exchange_strong(_expected, service);
if(!success)
{
// Some other context is active at the moment.
return ROCPROFILER_STATUS_ERROR;
}
if(is_hsa_initialized().load())
{
hsa::pc_sampling_service_start(service);
}
return ROCPROFILER_STATUS_SUCCESS;
}
rocprofiler_status_t
stop_service(const context::context* ctx)
{
auto* service = ctx->pc_sampler.get();
if(get_active_pc_sampling_service().load() != service)
{
// Some other service is activated at the moment.
return ROCPROFILER_STATUS_ERROR;
}
if(is_hsa_initialized().load())
{
hsa::pc_sampling_service_stop(service);
}
// No active PC sampling services
bool success = get_active_pc_sampling_service().compare_exchange_strong(service, nullptr);
return (success) ? ROCPROFILER_STATUS_SUCCESS : ROCPROFILER_STATUS_ERROR;
}
void
post_hsa_init_start_active_service()
{
// Called as part of the registration of the HSA table
if(is_hsa_initialized().load())
{
// If there is a guarantee that the `rocprofiler_set_api_table`
// can be called only once for the HSA, then this condition is redundant.
return;
}
// If the PC sampling service is not configured on any of the agents, return.
if(!get_configured_pc_sampling_service().load()) return;
static auto _once = std::once_flag{};
std::call_once(_once, []() {
// Configure PC sampling on the ROCr level only once.
hsa::pc_sampling_service_finish_configuration(get_configured_pc_sampling_service().load());
});
// Theoretically, the remainder of the function
// can execute concurrently with start_context/stop_context.
context::pc_sampling_service* _expected = nullptr;
void* invalid_ptr = reinterpret_cast<void*>(0xDEADBEEF);
context::pc_sampling_service* pseudo_sevice =
static_cast<context::pc_sampling_service*>(invalid_ptr);
if(get_active_pc_sampling_service().compare_exchange_strong(_expected, pseudo_sevice))
{
// At this point, we prevented any `start_context` instance from activating the service.
is_hsa_initialized().store(true);
// Now, allow `start_context` to active the service.
get_active_pc_sampling_service().compare_exchange_strong(pseudo_sevice, nullptr);
}
else
{
// Someone already called `start_context` that activated service.
// The pointer to this service is written inside `_expected`.
// Start PC sampling service on the HSA level in the name of the
// `start_context` caller.
hsa::pc_sampling_service_start(_expected);
// Although the caller of the `start_context` might try calling the hsa_start,
// it will fail, which is fine, since the service is eventually started.
is_hsa_initialized().store(true);
}
}
rocprofiler_status_t
configure_pc_sampling_service(context::context* ctx,
const rocprofiler_agent_t* agent,
rocprofiler_pc_sampling_method_t method,
rocprofiler_pc_sampling_unit_t unit,
uint64_t interval,
rocprofiler_buffer_id_t buffer_id)
{
if(!ctx->pc_sampler)
{
ctx->pc_sampler = std::make_unique<context::pc_sampling_service>();
}
if(ctx->pc_sampler->agent_sessions.count(agent->id) > 0)
{
// The service has already been configured for this agent.
return ROCPROFILER_STATUS_ERROR_SERVICE_ALREADY_CONFIGURED;
}
// The restriction we agreed at the moment is that at most one context
// can have PC sampling service configured, meaning
// at most one instance of the `context::pc_sampling_service` can be configured
// This `pc_sampling_service` contains at most one configuration per agent.
context::pc_sampling_service* expected = nullptr;
// Try registering the new instance of the `pc_sampling_service`.
if(!get_configured_pc_sampling_service().compare_exchange_strong(expected,
ctx->pc_sampler.get()))
{
// A `pc_sampling_service` instance has already been configured.
// Note: the `expected` contains the pointer to the configured `pc_sampling_service`
// instance.
if(expected != ctx->pc_sampler.get())
{
// Someone tried configuring a new `pc_sampling_service instance`, which we do not
// allow. Invalidate the `pc_sampling_service` from the `ctx` and return an error.
ctx->pc_sampler = nullptr;
// TODO: new status code needed
return ROCPROFILER_STATUS_ERROR;
}
// Someone is trying to enable PC sampling on another agent, and we allow registering
// new agent inside `pc_sampling_service` instance.
}
// calling KFD to check if the configuration is actually supported at the moment
uint32_t ioctl_pcs_id;
auto ioctl_status = ioctl::ioctl_pcs_create(agent, method, unit, interval, &ioctl_pcs_id);
if(ioctl_status != ROCPROFILER_STATUS_SUCCESS) return ioctl_status;
ctx->pc_sampler->agent_sessions[agent->id] = std::make_unique<PCSAgentSession>();
auto* session = ctx->pc_sampler->agent_sessions[agent->id].get();
session->agent = agent;
session->method = method;
session->unit = unit;
session->interval = interval;
session->buffer_id = buffer_id;
session->ioctl_pcs_id = ioctl_pcs_id;
session->parser = std::make_unique<PCSamplingParserContext>();
session->cid_manager = std::make_unique<PCSCIDManager>(session->parser.get());
ROCP_ERROR << "PC sampling session with id: " << session->ioctl_pcs_id
<< " hsa been created!\n";
return ROCPROFILER_STATUS_SUCCESS;
}
bool
is_pc_sample_service_configured(rocprofiler_agent_id_t agent_id)
{
auto* service = get_configured_pc_sampling_service().load();
if(service)
{
// If the agent_id is in the service->agent_sessions map,
// then the PC sampling service is configured on this agent.
return service->agent_sessions.find(agent_id) != service->agent_sessions.end();
}
// The PC sampling service is not configured on this agent
return false;
}
rocprofiler_status_t
flush_internal_agent_buffers(rocprofiler_buffer_id_t buffer_id)
{
// checking if the buffer is registered
auto const* buff = rocprofiler::buffer::get_buffer(buffer_id);
if(!buff) return ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND;
// Checking if the context is registered
const auto* ctx = rocprofiler::context::get_registered_context(
rocprofiler_context_id_t{.handle = buff->context_id});
if(!ctx) return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
auto* service = get_configured_pc_sampling_service().load();
if(service && ctx->pc_sampler.get() == service)
{
// The context `ctx` (that holds the buffer with `buffer_id`)
// is the one containing PC sampling service.
// The HSA interception table is registered.
for(const auto& [_, agent_session] : service->agent_sessions)
{
// Find the agent that fills the buffer with `buffer_id`
if(agent_session->buffer_id.handle == buffer_id.handle)
{
// Flush internal PC sampling buffers filled by the agent
return hsa::flush_internal_agent_buffers(agent_session.get());
}
}
}
// PC sampling service not configured.
return ROCPROFILER_STATUS_SUCCESS;
}
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,66 @@
// MIT License
//
// Copyright (c) 2023 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.
#pragma once
#include "lib/rocprofiler-sdk/context/context.hpp"
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/pc_sampling.h>
#include <hsa/hsa_api_trace.h>
#include <atomic>
namespace rocprofiler
{
namespace pc_sampling
{
using atomic_pc_sampling_service_t = std::atomic<context::pc_sampling_service*>;
atomic_pc_sampling_service_t&
get_configured_pc_sampling_service();
rocprofiler_status_t
start_service(const context::context* ctx);
rocprofiler_status_t
stop_service(const context::context* ctx);
void
post_hsa_init_start_active_service();
rocprofiler_status_t
configure_pc_sampling_service(context::context* ctx,
const rocprofiler_agent_t* agent,
rocprofiler_pc_sampling_method_t method,
rocprofiler_pc_sampling_unit_t unit,
uint64_t interval,
rocprofiler_buffer_id_t buffer_id);
bool
is_pc_sample_service_configured(rocprofiler_agent_id_t agent_id);
rocprofiler_status_t
flush_internal_agent_buffers(rocprofiler_buffer_id_t buffer_id);
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,29 @@
rocprofiler_deactivate_clang_tidy()
include(GoogleTest)
set(ROCPROFILER_LIB_PC_SAMPLING_TEST_SOURCES
configure_service.cpp
# samples_processing.cpp
query_configuration.cpp)
set(ROCPROFILER_LIB_PC_SAMPLING_TEST_HEADERS pc_sampling_internals.hpp)
add_executable(pcs-test)
target_sources(pcs-test PRIVATE ${ROCPROFILER_LIB_PC_SAMPLING_TEST_SOURCES}
${ROCPROFILER_LIB_PC_SAMPLING_TEST_HEADERS})
target_link_libraries(
pcs-test
PRIVATE rocprofiler::rocprofiler-common-library
rocprofiler::rocprofiler-static-library GTest::gtest GTest::gtest_main)
gtest_add_tests(
TARGET pcs-test
SOURCES ${ROCPROFILER_LIB_COUNTER_TEST_SOURCES}
TEST_LIST pcs-tests_TESTS
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_tests_properties(
${pcs-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests;pc-sampling"
SKIP_REGULAR_EXPRESSION "PC sampling unavailable")
@@ -0,0 +1,453 @@
// MIT License
//
// Copyright (c) 2023 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.
#include <rocprofiler-sdk/buffer.h>
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/internal_threading.h>
#include <rocprofiler-sdk/registration.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include "lib/common/utility.hpp"
#include <gtest/gtest.h>
#include <cstddef>
namespace
{
constexpr size_t BUFFER_SIZE_BYTES = 8192;
constexpr size_t WATERMARK = (BUFFER_SIZE_BYTES / 4);
#define ROCPROFILER_CALL(ARG, MSG) \
{ \
auto _status = (ARG); \
EXPECT_EQ(_status, ROCPROFILER_STATUS_SUCCESS) << MSG << " :: " << #ARG; \
}
struct callback_data
{
rocprofiler_client_id_t* client_id = nullptr;
rocprofiler_client_finalize_t client_fini_func = nullptr;
rocprofiler_context_id_t client_ctx = {};
rocprofiler_buffer_id_t client_buffer = {};
rocprofiler_callback_thread_t client_thread = {};
uint64_t client_workflow_count = {};
uint64_t client_callback_count = {};
int64_t current_depth = 0;
int64_t max_depth = 0;
std::map<uint64_t, rocprofiler_user_data_t> client_correlation = {};
std::vector<const rocprofiler_agent_t*> gpu_pcs_agents = {};
};
struct agent_data
{
uint64_t agent_count = 0;
std::vector<hsa_device_type_t> agents = {};
};
bool
is_pc_sampling_supported(rocprofiler_agent_id_t agent_id)
{
auto cb = [](const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data) {
auto* avail_configs =
static_cast<std::vector<rocprofiler_pc_sampling_configuration_t>*>(user_data);
// printf("The agent with the id: %lu supports the %lu configurations: \n",
// agent_id_.handle, num_config);
for(size_t i = 0; i < num_config; i++)
{
avail_configs->emplace_back(configs[i]);
}
return ROCPROFILER_STATUS_SUCCESS;
};
std::vector<rocprofiler_pc_sampling_configuration_t> configs;
auto status = rocprofiler_query_pc_sampling_agent_configurations(agent_id, cb, &configs);
if(status != ROCPROFILER_STATUS_SUCCESS)
{
// PC sampling is not supported
return false;
}
else if(configs.size() > 0)
{
return true;
}
else
{
return false;
}
}
rocprofiler_status_t
find_all_gpu_agents_supporting_pc_sampling_impl(rocprofiler_agent_version_t version,
const void** agents,
size_t num_agents,
void* user_data)
{
EXPECT_EQ(version, ROCPROFILER_AGENT_INFO_VERSION_0);
// user_data represent the pointer to the array where gpu_agent will be stored
if(!user_data) return ROCPROFILER_STATUS_ERROR;
auto* _out_agents = static_cast<std::vector<const rocprofiler_agent_t*>*>(user_data);
auto* _agents = reinterpret_cast<const rocprofiler_agent_t**>(agents);
for(size_t i = 0; i < num_agents; i++)
{
if(_agents[i]->type == ROCPROFILER_AGENT_TYPE_GPU)
{
if(is_pc_sampling_supported(_agents[i]->id)) _out_agents->push_back(_agents[i]);
printf("[%s] %s :: id=%zu, type=%i\n",
__FUNCTION__,
_agents[i]->name,
_agents[i]->id.handle,
_agents[i]->type);
}
else
{
printf("[%s] %s :: id=%zu, type=%i\n",
__FUNCTION__,
_agents[i]->name,
_agents[i]->id.handle,
_agents[i]->type);
}
}
return ROCPROFILER_STATUS_SUCCESS;
}
const rocprofiler_pc_sampling_configuration_t
extract_pc_sampling_config_prefer_stochastic(rocprofiler_agent_id_t agent_id)
{
auto cb = [](const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data) {
auto* avail_configs =
static_cast<std::vector<rocprofiler_pc_sampling_configuration_t>*>(user_data);
// printf("The agent with the id: %lu supports the %lu configurations: \n",
// agent_id_.handle, num_config);
for(size_t i = 0; i < num_config; i++)
{
avail_configs->emplace_back(configs[i]);
}
return ROCPROFILER_STATUS_SUCCESS;
};
std::vector<rocprofiler_pc_sampling_configuration_t> configs;
ROCPROFILER_CALL(rocprofiler_query_pc_sampling_agent_configurations(agent_id, cb, &configs),
"Failed to query available configurations");
const rocprofiler_pc_sampling_configuration_t* first_host_trap_config = nullptr;
const rocprofiler_pc_sampling_configuration_t* first_stochastic_config = nullptr;
// Search until encountering on the stochastic configuration, if any.
// Otherwise, use the host trap config
for(auto const& cfg : configs)
{
if(cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC)
{
first_stochastic_config = &cfg;
break;
}
else if(!first_host_trap_config && cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP)
{
first_host_trap_config = &cfg;
}
}
// Check if the stochastic config is found. Use host trap config otherwise.
const rocprofiler_pc_sampling_configuration_t* picked_cfg =
(first_stochastic_config != nullptr) ? first_stochastic_config : first_host_trap_config;
return *picked_cfg;
}
void
rocprofiler_pc_sampling_callback(rocprofiler_context_id_t /*context_id*/,
rocprofiler_buffer_id_t /*buffer_id*/,
rocprofiler_record_header_t** /*headers*/,
size_t /*num_headers*/,
void* /*data*/,
uint64_t /*drop_count*/)
{}
void
test_fail_because_of_wrong_agent(const callback_data* cb_data,
const rocprofiler_pc_sampling_configuration_t* pcs_config)
{
auto not_existing_agent = rocprofiler_agent_id_t{0xDEADBEEF};
EXPECT_EQ(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
not_existing_agent,
pcs_config->method,
pcs_config->unit,
pcs_config->min_interval,
cb_data->client_buffer),
ROCPROFILER_STATUS_ERROR_AGENT_NOT_FOUND);
}
void
test_fail_because_of_wrong_context(const callback_data* cb_data,
rocprofiler_agent_id_t agent_id,
const rocprofiler_pc_sampling_configuration_t* pcs_config)
{
auto not_existing_ctx = rocprofiler_context_id_t{0xDEADBEEF};
EXPECT_EQ(rocprofiler_configure_pc_sampling_service(not_existing_ctx,
agent_id,
pcs_config->method,
pcs_config->unit,
pcs_config->min_interval,
cb_data->client_buffer),
ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND);
}
void
test_fail_because_of_wrong_buffer(const callback_data* cb_data,
rocprofiler_agent_id_t agent_id,
const rocprofiler_pc_sampling_configuration_t* pcs_config)
{
auto not_existing_buffer_id = rocprofiler_buffer_id_t{0xDEADBEEF};
EXPECT_EQ(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent_id,
pcs_config->method,
pcs_config->unit,
pcs_config->min_interval,
not_existing_buffer_id),
ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND);
}
void
test_fail_because_of_unsupported_configuration(
const callback_data* cb_data,
rocprofiler_agent_id_t agent_id,
const rocprofiler_pc_sampling_configuration_t* pcs_config)
{
auto less_than_min_interval = pcs_config->min_interval - 1;
auto greater_than_max_interval = pcs_config->max_interval + 1;
auto wrong_method = ROCPROFILER_PC_SAMPLING_METHOD_LAST;
auto wrong_unit = ROCPROFILER_PC_SAMPLING_UNIT_NONE;
EXPECT_NE(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent_id,
pcs_config->method,
pcs_config->unit,
less_than_min_interval,
cb_data->client_buffer),
ROCPROFILER_STATUS_SUCCESS);
EXPECT_NE(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent_id,
pcs_config->method,
pcs_config->unit,
greater_than_max_interval,
cb_data->client_buffer),
ROCPROFILER_STATUS_SUCCESS);
EXPECT_NE(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent_id,
wrong_method,
pcs_config->unit,
pcs_config->max_interval,
cb_data->client_buffer),
ROCPROFILER_STATUS_SUCCESS);
EXPECT_NE(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent_id,
pcs_config->method,
wrong_unit,
pcs_config->max_interval,
cb_data->client_buffer),
ROCPROFILER_STATUS_SUCCESS);
}
void
test_fail_because_service_is_already_configured(
const callback_data* cb_data,
rocprofiler_agent_id_t agent_id,
const rocprofiler_pc_sampling_configuration_t* pcs_config)
{
EXPECT_EQ(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent_id,
pcs_config->method,
pcs_config->unit,
pcs_config->min_interval,
cb_data->client_buffer),
ROCPROFILER_STATUS_ERROR_SERVICE_ALREADY_CONFIGURED);
}
} // namespace
TEST(pc_sampling, rocprofiler_configure_pc_sampling_service)
{
using init_func_t = int (*)(rocprofiler_client_finalize_t, void*);
using fini_func_t = void (*)(void*);
// using hsa_iterate_agents_cb_t = hsa_status_t (*)(hsa_agent_t, void*);
auto cmd_line = rocprofiler::common::read_command_line(getpid());
ASSERT_FALSE(cmd_line.empty());
static init_func_t tool_init = [](rocprofiler_client_finalize_t fini_func,
void* client_data) -> int {
auto* cb_data = static_cast<callback_data*>(client_data);
cb_data->client_workflow_count++;
cb_data->client_fini_func = fini_func;
// This function returns the all gpu agents supporting some kind of PC sampling
ROCPROFILER_CALL(
rocprofiler_query_available_agents(ROCPROFILER_AGENT_INFO_VERSION_0,
&find_all_gpu_agents_supporting_pc_sampling_impl,
sizeof(rocprofiler_agent_t),
static_cast<void*>(&cb_data->gpu_pcs_agents)),
"Failed to find GPU agents");
// TODO-VLAINDIC: Can we dynamically skip the test if the underlying
// HW does not support PC sampling
if(cb_data->gpu_pcs_agents.size() == 0) exit(0);
ROCPROFILER_CALL(rocprofiler_create_context(&cb_data->client_ctx),
"failed to create context");
ROCPROFILER_CALL(rocprofiler_create_buffer(cb_data->client_ctx,
BUFFER_SIZE_BYTES,
WATERMARK,
ROCPROFILER_BUFFER_POLICY_LOSSLESS,
rocprofiler_pc_sampling_callback,
client_data,
&cb_data->client_buffer),
"buffer creation failed");
// We will create another context and try configuring pc sampling inside it,
// that is supposed to fail.
rocprofiler_context_id_t another_ctx;
ROCPROFILER_CALL(rocprofiler_create_context(&another_ctx), "failed to create context");
rocprofiler_buffer_id_t another_buff;
ROCPROFILER_CALL(rocprofiler_create_buffer(another_ctx,
4096,
2048,
ROCPROFILER_BUFFER_POLICY_LOSSLESS,
rocprofiler_pc_sampling_callback,
nullptr,
&another_buff),
"buffer creation failed");
for(const auto* agent : cb_data->gpu_pcs_agents)
{
const auto agent_id = agent->id;
const auto pcs_config = extract_pc_sampling_config_prefer_stochastic(agent_id);
test_fail_because_of_wrong_agent(cb_data, &pcs_config);
test_fail_because_of_wrong_context(cb_data, agent_id, &pcs_config);
test_fail_because_of_wrong_buffer(cb_data, agent_id, &pcs_config);
test_fail_because_of_unsupported_configuration(cb_data, agent_id, &pcs_config);
size_t interval = pcs_config.max_interval;
// This calls succeeds
ROCPROFILER_CALL(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent_id,
pcs_config.method,
pcs_config.unit,
interval,
cb_data->client_buffer),
"Failed to configure PC sampling service");
test_fail_because_service_is_already_configured(cb_data, agent_id, &pcs_config);
// Cannot create PC sampling service in context different than the `cb_data->client_ctx`
EXPECT_EQ(rocprofiler_configure_pc_sampling_service(another_ctx,
agent_id,
pcs_config.method,
pcs_config.unit,
interval,
another_buff),
ROCPROFILER_STATUS_ERROR);
}
ROCPROFILER_CALL(rocprofiler_create_callback_thread(&cb_data->client_thread),
"failure creating callback thread");
ROCPROFILER_CALL(
rocprofiler_assign_callback_thread(cb_data->client_buffer, cb_data->client_thread),
"failed to assign thread for buffer");
int valid_ctx = 0;
ROCPROFILER_CALL(rocprofiler_context_is_valid(cb_data->client_ctx, &valid_ctx),
"failure checking context validity");
EXPECT_EQ(valid_ctx, 1);
ROCPROFILER_CALL(rocprofiler_start_context(cb_data->client_ctx),
"rocprofiler context start failed");
// no errors
return 0;
};
static fini_func_t tool_fini = [](void* client_data) -> void {
auto* cb_data = static_cast<callback_data*>(client_data);
ROCPROFILER_CALL(rocprofiler_stop_context(cb_data->client_ctx),
"rocprofiler context stop failed");
static_cast<callback_data*>(client_data)->client_workflow_count++;
};
static auto cb_data = callback_data{};
static auto cfg_result =
rocprofiler_tool_configure_result_t{sizeof(rocprofiler_tool_configure_result_t),
tool_init,
tool_fini,
static_cast<void*>(&cb_data)};
static rocprofiler_configure_func_t rocp_init =
[](uint32_t version,
const char* runtime_version,
uint32_t prio,
rocprofiler_client_id_t* client_id) -> rocprofiler_tool_configure_result_t* {
auto expected_version = ROCPROFILER_VERSION;
EXPECT_EQ(expected_version, version);
EXPECT_EQ(std::string_view{runtime_version}, std::string_view{ROCPROFILER_VERSION_STRING});
EXPECT_EQ(prio, 0);
EXPECT_EQ(client_id->name, nullptr);
cb_data.client_id = client_id;
cb_data.client_id->name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
return &cfg_result;
};
EXPECT_EQ(rocprofiler_force_configure(rocp_init), ROCPROFILER_STATUS_SUCCESS);
// Further tests assumes the existence of at least one GPU agent supporting
if(cb_data.gpu_pcs_agents.size() == 0) return;
const auto* agent = cb_data.gpu_pcs_agents.at(0);
EXPECT_EQ(rocprofiler_configure_pc_sampling_service(cb_data.client_ctx,
agent->id,
ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP,
ROCPROFILER_PC_SAMPLING_UNIT_TIME,
1,
cb_data.client_buffer),
ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED);
}
@@ -0,0 +1,63 @@
// MIT License
//
// Copyright (c) 2023 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.
#pragma once
#include "lib/rocprofiler-sdk/pc_sampling/hsa_adapter.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/pc_record_interface.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
namespace rocprofiler
{
namespace pc_sampling
{
void
post_hsa_init_start_active_service();
namespace hsa
{
extern void
amd_intercept_marker_handler_callback(const struct amd_aql_intercept_marker_s* packet,
hsa_queue_t* queue,
uint64_t packet_id);
extern void
kernel_completion_cb(const std::shared_ptr<rocprofiler::counters::counter_callback_info>& info,
const rocprofiler_agent_t* rocp_agent,
rocprofiler::hsa::ClientID client_id,
const rocprofiler::hsa::rocprofiler_packet& kernel_pkt,
const rocprofiler::hsa::Queue::queue_info_session_t& session,
std::unique_ptr<rocprofiler::hsa::AQLPacket> pkt);
extern void
data_ready_callback(void* client_callback_data,
size_t data_size,
size_t lost_sample_count,
hsa_ven_amd_pcs_data_copy_callback_t data_copy_callback,
void* hsa_callback_data);
extern atomic_pc_sampling_service_t&
get_active_pc_sampling_service();
} // namespace hsa
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,364 @@
// MIT License
//
// Copyright (c) 2023 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.
#include <gtest/gtest.h>
#include <rocprofiler-sdk/buffer.h>
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/internal_threading.h>
#include <rocprofiler-sdk/registration.h>
#include <rocprofiler-sdk/rocprofiler.h>
namespace
{
#define USER_DATA_VAL 33
constexpr size_t BUFFER_SIZE_BYTES = 8192;
constexpr size_t WATERMARK = (BUFFER_SIZE_BYTES / 4);
#define ROCPROFILER_CALL(ARG, MSG) \
{ \
auto _status = (ARG); \
EXPECT_EQ(_status, ROCPROFILER_STATUS_SUCCESS) << MSG << " :: " << #ARG; \
}
struct callback_data
{
rocprofiler_client_id_t* client_id = nullptr;
rocprofiler_client_finalize_t client_fini_func = nullptr;
rocprofiler_context_id_t client_ctx = {};
rocprofiler_buffer_id_t client_buffer = {};
rocprofiler_callback_thread_t client_thread = {};
uint64_t client_workflow_count = {};
uint64_t client_callback_count = {};
int64_t current_depth = 0;
int64_t max_depth = 0;
std::map<uint64_t, rocprofiler_user_data_t> client_correlation = {};
std::vector<const rocprofiler_agent_t*> gpu_pcs_agents = {};
};
struct agent_data
{
uint64_t agent_count = 0;
std::vector<hsa_device_type_t> agents = {};
};
bool
is_pc_sampling_supported(rocprofiler_agent_id_t agent_id)
{
auto cb = [](const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data) {
auto* avail_configs =
static_cast<std::vector<rocprofiler_pc_sampling_configuration_t>*>(user_data);
// printf("The agent with the id: %lu supports the %lu configurations: \n",
// agent_id_.handle, num_config);
for(size_t i = 0; i < num_config; i++)
{
avail_configs->emplace_back(configs[i]);
}
return ROCPROFILER_STATUS_SUCCESS;
};
std::vector<rocprofiler_pc_sampling_configuration_t> configs;
auto status = rocprofiler_query_pc_sampling_agent_configurations(agent_id, cb, &configs);
if(status != ROCPROFILER_STATUS_SUCCESS)
{
// PC sampling is not supported
return false;
}
else if(configs.size() > 0)
{
return true;
}
else
{
return false;
}
}
rocprofiler_status_t
find_all_gpu_agents_supporting_pc_sampling_impl(rocprofiler_agent_version_t version,
const void** agents,
size_t num_agents,
void* user_data)
{
EXPECT_EQ(version, ROCPROFILER_AGENT_INFO_VERSION_0);
// user_data represent the pointer to the array where gpu_agent will be stored
if(!user_data) return ROCPROFILER_STATUS_ERROR;
auto* _out_agents = static_cast<std::vector<const rocprofiler_agent_t*>*>(user_data);
auto* _agents = reinterpret_cast<const rocprofiler_agent_t**>(agents);
for(size_t i = 0; i < num_agents; i++)
{
if(_agents[i]->type == ROCPROFILER_AGENT_TYPE_GPU)
{
if(is_pc_sampling_supported(_agents[i]->id)) _out_agents->push_back(_agents[i]);
printf("[%s] %s :: id=%zu, type=%i\n",
__FUNCTION__,
_agents[i]->name,
_agents[i]->id.handle,
_agents[i]->type);
}
else
{
printf("[%s] %s :: id=%zu, type=%i\n",
__FUNCTION__,
_agents[i]->name,
_agents[i]->id.handle,
_agents[i]->type);
}
}
return ROCPROFILER_STATUS_SUCCESS;
}
rocprofiler_pc_sampling_configuration_t
extract_pc_sampling_config_prefer_stochastic(rocprofiler_agent_id_t agent_id)
{
auto cb = [](const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data) {
auto* avail_configs =
static_cast<std::vector<rocprofiler_pc_sampling_configuration_t>*>(user_data);
// printf("The agent with the id: %lu supports the %lu configurations: \n",
// agent_id_.handle, num_config);
for(size_t i = 0; i < num_config; i++)
{
avail_configs->emplace_back(configs[i]);
}
return ROCPROFILER_STATUS_SUCCESS;
};
std::vector<rocprofiler_pc_sampling_configuration_t> configs;
ROCPROFILER_CALL(rocprofiler_query_pc_sampling_agent_configurations(agent_id, cb, &configs),
"Failed to query available configurations");
const rocprofiler_pc_sampling_configuration_t* first_host_trap_config = nullptr;
const rocprofiler_pc_sampling_configuration_t* first_stochastic_config = nullptr;
// Search until encountering on the stochastic configuration, if any.
// Otherwise, use the host trap config
for(auto const& cfg : configs)
{
if(cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC)
{
first_stochastic_config = &cfg;
break;
}
else if(!first_host_trap_config && cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP)
{
first_host_trap_config = &cfg;
}
}
// Check if the stochastic config is found. Use host trap config otherwise.
const rocprofiler_pc_sampling_configuration_t* picked_cfg =
(first_stochastic_config != nullptr) ? first_stochastic_config : first_host_trap_config;
return *picked_cfg;
}
void
rocprofiler_pc_sampling_callback(rocprofiler_context_id_t /*context_id*/,
rocprofiler_buffer_id_t /*buffer_id*/,
rocprofiler_record_header_t** /*headers*/,
size_t /*num_headers*/,
void* /*data*/,
uint64_t /*drop_count*/)
{}
rocprofiler_status_t
check_all_configs_cb(const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data)
{
auto* val = reinterpret_cast<int*>(user_data);
EXPECT_EQ(*val, USER_DATA_VAL);
if(num_config == 0) return ROCPROFILER_STATUS_ERROR;
for(size_t i = 0; i < num_config; i++)
{
const auto* cfg = &configs[i];
EXPECT_LT(ROCPROFILER_PC_SAMPLING_METHOD_NONE, cfg->method);
EXPECT_LT(cfg->method, ROCPROFILER_PC_SAMPLING_METHOD_LAST);
EXPECT_LT(ROCPROFILER_PC_SAMPLING_UNIT_NONE, cfg->unit);
EXPECT_LT(cfg->unit, ROCPROFILER_PC_SAMPLING_UNIT_LAST);
}
return ROCPROFILER_STATUS_SUCCESS;
};
} // namespace
// TODO: change according to the actual implementation
TEST(pc_sampling, query_configs_agent_does_not_exists)
{
int cb_data = USER_DATA_VAL;
// The agent does not exists
EXPECT_EQ(rocprofiler_query_pc_sampling_agent_configurations(
rocprofiler_agent_id_t{.handle = 0xDEADBEEF}, check_all_configs_cb, &cb_data),
ROCPROFILER_STATUS_ERROR_AGENT_NOT_FOUND);
}
TEST(pc_sampling, query_configs_after_service_setup)
{
using init_func_t = int (*)(rocprofiler_client_finalize_t, void*);
using fini_func_t = void (*)(void*);
// TODO: configure PC sampling and query if the configuration is listed
static init_func_t tool_init = [](rocprofiler_client_finalize_t fini_func,
void* client_data) -> int {
auto* cb_data = static_cast<callback_data*>(client_data);
cb_data->client_workflow_count++;
cb_data->client_fini_func = fini_func;
// This function returns the all gpu agents supporting some kind of PC sampling
ROCPROFILER_CALL(
rocprofiler_query_available_agents(ROCPROFILER_AGENT_INFO_VERSION_0,
&find_all_gpu_agents_supporting_pc_sampling_impl,
sizeof(rocprofiler_agent_t),
static_cast<void*>(&cb_data->gpu_pcs_agents)),
"Failed to find GPU agents");
// TODO-VLAINDIC: Can we dynamically skip the test if the underlying
// HW does not support PC sampling
if(cb_data->gpu_pcs_agents.size() == 0) exit(0);
int query_cb_data = USER_DATA_VAL;
const auto* agent = cb_data->gpu_pcs_agents.at(0);
const auto agent_id = agent->id;
auto status = rocprofiler_query_pc_sampling_agent_configurations(
agent_id, check_all_configs_cb, &query_cb_data);
if(status != ROCPROFILER_STATUS_SUCCESS)
{
// The agent does not support PC sampling
return -1;
}
ROCPROFILER_CALL(rocprofiler_create_context(&cb_data->client_ctx),
"failed to create context");
ROCPROFILER_CALL(rocprofiler_create_buffer(cb_data->client_ctx,
BUFFER_SIZE_BYTES,
WATERMARK,
ROCPROFILER_BUFFER_POLICY_LOSSLESS,
rocprofiler_pc_sampling_callback,
client_data,
&cb_data->client_buffer),
"buffer creation failed");
auto pcs_config = extract_pc_sampling_config_prefer_stochastic(agent_id);
size_t interval = pcs_config.max_interval;
// This calls succeeds
ROCPROFILER_CALL(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent->id,
pcs_config.method,
pcs_config.unit,
interval,
cb_data->client_buffer),
"Failed to configure PC sampling service");
// query configuration and expect to see `pcs_config->max_interval` as the `interval`
auto post_setup_conf_cb = [](const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data) {
const rocprofiler_pc_sampling_configuration_t* picked_cfg =
static_cast<rocprofiler_pc_sampling_configuration_t*>(user_data);
EXPECT_EQ(num_config, 1);
const auto* cfg = &configs[0];
EXPECT_EQ(cfg->method, picked_cfg->method);
EXPECT_EQ(cfg->unit, picked_cfg->unit);
// Min and max interval are equeal when PC sampling is enabled
EXPECT_EQ(cfg->min_interval, cfg->max_interval);
// When set up the PC sampling, we used the max_interval of the picked_cfg
EXPECT_EQ(cfg->max_interval, picked_cfg->max_interval);
return ROCPROFILER_STATUS_SUCCESS;
};
EXPECT_EQ(rocprofiler_query_pc_sampling_agent_configurations(
agent_id, post_setup_conf_cb, &pcs_config),
ROCPROFILER_STATUS_SUCCESS);
ROCPROFILER_CALL(rocprofiler_create_callback_thread(&cb_data->client_thread),
"failure creating callback thread");
ROCPROFILER_CALL(
rocprofiler_assign_callback_thread(cb_data->client_buffer, cb_data->client_thread),
"failed to assign thread for buffer");
int valid_ctx = 0;
ROCPROFILER_CALL(rocprofiler_context_is_valid(cb_data->client_ctx, &valid_ctx),
"failure checking context validity");
EXPECT_EQ(valid_ctx, 1);
ROCPROFILER_CALL(rocprofiler_start_context(cb_data->client_ctx),
"rocprofiler context start failed");
// no errors
return 0;
};
static fini_func_t tool_fini = [](void* client_data) -> void {
auto* cb_data = static_cast<callback_data*>(client_data);
ROCPROFILER_CALL(rocprofiler_stop_context(cb_data->client_ctx),
"rocprofiler context stop failed");
cb_data->client_workflow_count++;
};
static auto cb_data = callback_data{};
static auto cfg_result =
rocprofiler_tool_configure_result_t{sizeof(rocprofiler_tool_configure_result_t),
tool_init,
tool_fini,
static_cast<void*>(&cb_data)};
static rocprofiler_configure_func_t rocp_init =
[](uint32_t version,
const char* runtime_version,
uint32_t prio,
rocprofiler_client_id_t* client_id) -> rocprofiler_tool_configure_result_t* {
auto expected_version = ROCPROFILER_VERSION;
EXPECT_EQ(expected_version, version);
EXPECT_EQ(std::string_view{runtime_version}, std::string_view{ROCPROFILER_VERSION_STRING});
EXPECT_EQ(prio, 0);
EXPECT_EQ(client_id->name, nullptr);
cb_data.client_id = client_id;
cb_data.client_id->name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
return &cfg_result;
};
EXPECT_EQ(rocprofiler_force_configure(rocp_init), ROCPROFILER_STATUS_SUCCESS);
}
@@ -0,0 +1,437 @@
// MIT License
//
// Copyright (c) 2023 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.
#include <hsa/hsa.h>
#include <rocprofiler-sdk/buffer.h>
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/internal_threading.h>
#include <rocprofiler-sdk/registration.h>
#include <rocprofiler-sdk/rocprofiler.h>
#include "lib/common/utility.hpp"
#include "lib/rocprofiler-sdk/hsa/agent_cache.hpp"
#include "lib/rocprofiler-sdk/hsa/hsa.hpp"
#include "lib/rocprofiler-sdk/hsa/queue.hpp"
#include "lib/rocprofiler-sdk/hsa/queue_controller.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/rocr.h"
#include "lib/rocprofiler-sdk/pc_sampling/tests/pc_sampling_internals.hpp"
#include "pc_sampling_internals.hpp"
#include <gtest/gtest.h>
#include <cstddef>
#include <cstdint>
constexpr size_t BUFFER_SIZE_BYTES = 8192;
constexpr size_t WATERMARK = (BUFFER_SIZE_BYTES / 4);
#define ROCPROFILER_CALL(ARG, MSG) \
{ \
auto _status = (ARG); \
EXPECT_EQ(_status, ROCPROFILER_STATUS_SUCCESS) << MSG << " :: " << #ARG; \
}
namespace
{
#define NUM_SAMPLES 5
#define TRAP_ID 0
struct callback_data
{
rocprofiler_client_id_t* client_id = nullptr;
rocprofiler_client_finalize_t client_fini_func = nullptr;
rocprofiler_context_id_t client_ctx = {};
rocprofiler_buffer_id_t client_buffer = {};
rocprofiler_callback_thread_t client_thread = {};
uint64_t client_workflow_count = {};
uint64_t client_callback_count = {};
int64_t current_depth = 0;
int64_t max_depth = 0;
std::map<uint64_t, rocprofiler_user_data_t> client_correlation = {};
std::vector<const rocprofiler_agent_t*> gpu_pcs_agents = {};
};
struct agent_data
{
uint64_t agent_count = 0;
std::vector<hsa_device_type_t> agents = {};
};
rocprofiler_status_t
find_all_gpu_agents_supporting_pc_sampling_impl(const rocprofiler_agent_t** agents,
size_t num_agents,
void* user_data)
{
// user_data represent the pointer to the array where gpu_agent will be stored
if(!user_data) return ROCPROFILER_STATUS_ERROR;
auto* _out_agents = static_cast<std::vector<const rocprofiler_agent_t*>*>(user_data);
// find the first GPU agent
for(size_t i = 0; i < num_agents; i++)
{
if(agents[i]->type == ROCPROFILER_AGENT_TYPE_GPU)
{
// Skip GPU agents not supporting PC sampling
// Vladimir: The assumption is that if a GPU agent does not support PC sampling,
// the size is 0.
if(agents[i]->num_pc_sampling_configs == 0) continue;
_out_agents->push_back(agents[i]);
printf("[%s] %s :: id=%zu, type=%i, num pc sample configs=%zu\n",
__FUNCTION__,
agents[i]->name,
agents[i]->id.handle,
agents[i]->type,
agents[i]->num_pc_sampling_configs);
}
else
{
printf("[%s] %s :: id=%zu, type=%i, num pc sample configs=%zu\n",
__FUNCTION__,
agents[i]->name,
agents[i]->id.handle,
agents[i]->type,
agents[i]->num_pc_sampling_configs);
}
}
return !_out_agents->empty() ? ROCPROFILER_STATUS_SUCCESS : ROCPROFILER_STATUS_ERROR;
}
const rocprofiler_pc_sampling_configuration_t
extract_pc_sampling_config_prefer_stochastic(rocprofiler_agent_id_t agent_id)
{
auto cb = [](const rocprofiler_pc_sampling_configuration_t* configs,
size_t num_config,
void* user_data) {
auto* avail_configs =
static_cast<std::vector<rocprofiler_pc_sampling_configuration_t>*>(user_data);
// printf("The agent with the id: %lu supports the %lu configurations: \n",
// agent_id_.handle, num_config);
for(size_t i = 0; i < num_config; i++)
{
avail_configs->emplace_back(configs[i]);
}
return ROCPROFILER_STATUS_SUCCESS;
};
std::vector<rocprofiler_pc_sampling_configuration_t> configs;
ROCPROFILER_CALL(rocprofiler_query_pc_sampling_agent_configurations(agent_id, cb, &configs),
"Failed to query available configurations");
const rocprofiler_pc_sampling_configuration_t* first_host_trap_config = nullptr;
const rocprofiler_pc_sampling_configuration_t* first_stochastic_config = nullptr;
// Search until encountering on the stochastic configuration, if any.
// Otherwise, use the host trap config
for(auto const& cfg : configs)
{
if(cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC)
{
first_stochastic_config = &cfg;
break;
}
else if(!first_host_trap_config && cfg.method == ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP)
{
first_host_trap_config = &cfg;
}
}
// Check if the stochastic config is found. Use host trap config otherwise.
const rocprofiler_pc_sampling_configuration_t* picked_cfg =
(first_stochastic_config != nullptr) ? first_stochastic_config : first_host_trap_config;
return *picked_cfg;
}
void
rocprofiler_pc_sampling_callback(rocprofiler_context_id_t /*context_id*/,
rocprofiler_buffer_id_t /*buffer_id*/,
rocprofiler_record_header_t** headers,
size_t num_headers,
void* /*data*/,
uint64_t drop_count)
{
EXPECT_EQ(drop_count, 0);
for(size_t i = 0; i < num_headers; i++)
{
auto* cur_header = headers[i];
if(cur_header == nullptr)
{
throw std::runtime_error{
"rocprofiler provided a null pointer to header. this should never happen"};
}
else if(cur_header->hash !=
rocprofiler_record_header_compute_hash(cur_header->category, cur_header->kind))
{
throw std::runtime_error{"rocprofiler_record_header_t (category | kind) != hash"};
}
else if(cur_header->category == ROCPROFILER_BUFFER_CATEGORY_PC_SAMPLING)
{
auto* pc_sample = static_cast<rocprofiler_pc_sampling_record_t*>(cur_header->payload);
// FIXME: find the cause why this fails
// EXPECT_EQ(pc_sample->correlation_id.internal, 1);
EXPECT_EQ(pc_sample->pc, i + 1);
EXPECT_EQ(pc_sample->timestamp, i + 33);
EXPECT_EQ(pc_sample->hw_id, 0);
}
else
{
throw std::runtime_error{"unexpected rocprofiler_record_header_t category + kind"};
}
}
}
} // namespace
TEST(pc_sampling, processing_pc_samples)
{
using init_func_t = int (*)(rocprofiler_client_finalize_t, void*);
using fini_func_t = void (*)(void*);
// using hsa_iterate_agents_cb_t = hsa_status_t (*)(hsa_agent_t, void*);
auto cmd_line = rocprofiler::common::read_command_line(getpid());
ASSERT_FALSE(cmd_line.empty());
static init_func_t tool_init = [](rocprofiler_client_finalize_t fini_func,
void* client_data) -> int {
auto* cb_data = static_cast<callback_data*>(client_data);
cb_data->client_workflow_count++;
cb_data->client_fini_func = fini_func;
// This function returns the all gpu agents supporting some kind of PC sampling
ROCPROFILER_CALL(
rocprofiler_query_available_agents(&find_all_gpu_agents_supporting_pc_sampling_impl,
sizeof(rocprofiler_agent_t),
static_cast<void*>(&cb_data->gpu_pcs_agents)),
"Failed to find GPU agents");
ROCPROFILER_CALL(rocprofiler_create_context(&cb_data->client_ctx),
"failed to create context");
ROCPROFILER_CALL(rocprofiler_create_buffer(cb_data->client_ctx,
4096,
2048,
ROCPROFILER_BUFFER_POLICY_LOSSLESS,
rocprofiler_pc_sampling_callback,
client_data,
&cb_data->client_buffer),
"buffer creation failed");
const auto* agent = cb_data->gpu_pcs_agents.at(0);
const auto agent_id = agent->id;
const auto pcs_config = extract_pc_sampling_config_prefer_stochastic(agent_id);
size_t interval = pcs_config.max_interval;
// This calls succeeds
ROCPROFILER_CALL(rocprofiler_configure_pc_sampling_service(cb_data->client_ctx,
agent->id,
pcs_config.method,
pcs_config.unit,
interval,
cb_data->client_buffer),
"Failed to configure PC sampling service");
ROCPROFILER_CALL(rocprofiler_create_callback_thread(&cb_data->client_thread),
"failure creating callback thread");
ROCPROFILER_CALL(
rocprofiler_assign_callback_thread(cb_data->client_buffer, cb_data->client_thread),
"failed to assign thread for buffer");
int valid_ctx = 0;
ROCPROFILER_CALL(rocprofiler_context_is_valid(cb_data->client_ctx, &valid_ctx),
"failure checking context validity");
EXPECT_EQ(valid_ctx, 1);
ROCPROFILER_CALL(rocprofiler_start_context(cb_data->client_ctx),
"rocprofiler context start failed");
// no errors
return 0;
};
static fini_func_t tool_fini = [](void* client_data) -> void {
auto* cb_data = static_cast<callback_data*>(client_data);
// FIXME: for some reason, this returns context not found
// ROCPROFILER_CALL(rocprofiler_stop_context(cb_data->client_ctx),
// "rocprofiler context stop failed");
cb_data->client_workflow_count++;
};
static auto cb_data = callback_data{};
static auto cfg_result =
rocprofiler_tool_configure_result_t{sizeof(rocprofiler_tool_configure_result_t),
tool_init,
tool_fini,
static_cast<void*>(&cb_data)};
static rocprofiler_configure_func_t rocp_init =
[](uint32_t version,
const char* runtime_version,
uint32_t prio,
rocprofiler_client_id_t* client_id) -> rocprofiler_tool_configure_result_t* {
auto expected_version = ROCPROFILER_VERSION;
EXPECT_EQ(expected_version, version);
EXPECT_EQ(std::string_view{runtime_version}, std::string_view{ROCPROFILER_VERSION_STRING});
EXPECT_EQ(prio, 0);
EXPECT_EQ(client_id->name, nullptr);
cb_data.client_id = client_id;
cb_data.client_id->name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
return &cfg_result;
};
EXPECT_EQ(rocprofiler_force_configure(rocp_init), ROCPROFILER_STATUS_SUCCESS);
// Further tests assumes the existence of at least one GPU agent supporting
if(cb_data.gpu_pcs_agents.size() == 0) return;
auto& hsa_table = rocprofiler::hsa::get_table();
auto* pc_sampling_table_ = hsa_table.pc_sampling_ext_;
EXPECT_NE(pc_sampling_table_, nullptr);
pc_sampling_table_->hsa_ven_amd_pcs_create_from_id_fn =
[](uint32_t /*ioctl_pcs_id*/,
hsa_agent_t /*agent*/,
hsa_ven_amd_pcs_method_kind_t /*method*/,
hsa_ven_amd_pcs_units_t /*units*/,
size_t /*interval*/,
size_t /*latency*/,
size_t /*buffer_size*/,
hsa_ven_amd_pcs_data_ready_callback_t /*data_ready_callback*/,
void* /*client_callback_data*/,
hsa_ven_amd_pcs_t* /*pc_sampling*/) { return HSA_STATUS_SUCCESS; };
pc_sampling_table_->hsa_ven_amd_pcs_flush_fn = [](hsa_ven_amd_pcs_t /*pc_sampling*/) {
return HSA_STATUS_SUCCESS;
};
auto* ext_table_ = hsa_table.amd_ext_;
EXPECT_NE(ext_table_, nullptr);
ext_table_->hsa_amd_queue_get_info_fn =
[](hsa_queue_t* queue, hsa_queue_info_attribute_t attribute, void* value) {
(void) queue;
switch(attribute)
{
case HSA_AMD_QUEUE_INFO_AGENT:
*(reinterpret_cast<hsa_agent_t*>(value)) = hsa_agent_t{.handle = 1};
break;
case HSA_AMD_QUEUE_INFO_DOORBELL_ID:
*(reinterpret_cast<uint64_t*>(value)) = 0;
break;
default: return HSA_STATUS_ERROR_INVALID_ARGUMENT;
}
return HSA_STATUS_SUCCESS;
};
#if 1
// Set the HSA agent for the active PCSamplingConfiguration,
// The reason for setting HSA agent manually follows.
// The test links against rocporifler static library.
// Hence, the rocprofiler_set_api_table is not called.
auto* service = rocprofiler::pc_sampling::get_active_pc_sampling_service().load();
EXPECT_NE(service, nullptr);
const auto* rocp_agent = cb_data.gpu_pcs_agents.at(0);
auto agent_session = service->agent_sessions.at(rocp_agent->id).get();
hsa_agent_t pseudo_hsa_agent = {.handle = 1};
agent_session->hsa_agent = std::make_unique<hsa_agent_t>(pseudo_hsa_agent);
// TODO: We need to register the agent inside the parser
rocprofiler::pc_sampling::hsa::get_pc_sampling_parser().register_buffer_for_agent(
cb_data.client_buffer.handle, rocp_agent->id.handle);
// The following test calls some segments of internal PC sampling API implementation
// by mimicking the HIP and ROCr
// Generate dispatch and marker packet
rocprofiler::hsa::rocprofiler_packet dispatch_pkt;
auto marker_pkt =
rocprofiler::pc_sampling::hsa::generate_marker_packet_for_kernel(&dispatch_pkt);
// create a pseudo hsa queue
hsa_queue_t queue;
queue.size = 1024;
// Mimic the ROCr and notify the pc sampling service that the marker packet has been encoutered.
rocprofiler::pc_sampling::hsa::amd_intercept_marker_handler_callback(
&marker_pkt.marker, &queue, 0);
// We need to generate some samples and send them via data_ready_calllback.
size_t num_samples = NUM_SAMPLES;
auto samples_data_size = num_samples * sizeof(packet_union_t);
static hsa_ven_amd_pcs_data_copy_callback_t hsa_mock_data_copy_callback =
[](void* hsa_callback_data, size_t data_size, void* destination) {
(void) hsa_callback_data;
(void) data_size;
using rocr_buffer_t = std::vector<packet_union_t>;
auto samples_buff = rocr_buffer_t{};
for(size_t i = 0; i < NUM_SAMPLES; i++)
{
perf_sample_host_trap_v1 hs;
hs.pc = i + 1;
hs.exec_mask = 0xF;
hs.workgroup_id_x = 1;
hs.workgroup_id_y = 2;
hs.workgroup_id_z = 3;
hs.chiplet_and_wave_id = 0;
hs.hw_id = 0;
hs.timestamp = 33 + i;
hs.correlation_id = TRAP_ID;
samples_buff.push_back(packet_union_t{.host = hs});
}
// copy the data
std::memcpy(destination, samples_buff.data(), NUM_SAMPLES * sizeof(packet_union_t));
// clear the data
return HSA_STATUS_SUCCESS;
};
// calling data_ready_callback that will result in copying the data from above
// to the client buffer via PC sampling parser
rocprofiler::pc_sampling::PCSAgentSession pcs_agent_session;
pcs_agent_session.agent = rocp_agent;
pcs_agent_session.method = ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP;
size_t lost_samples = 0;
rocprofiler::pc_sampling::hsa::data_ready_callback(
&pcs_agent_session, samples_data_size, lost_samples, hsa_mock_data_copy_callback, nullptr);
rocprofiler::pc_sampling::hsa::kernel_completion_cb(
nullptr, rocp_agent, static_cast<int64_t>(1), dispatch_pkt, nullptr);
// Flush the buffer explicitly
ROCPROFILER_CALL(rocprofiler_flush_buffer(cb_data.client_buffer),
"rocprofiler flush buffer failed");
// Stop the context
ROCPROFILER_CALL(rocprofiler_stop_context(cb_data.client_ctx),
"rocprofiler context stop failed");
#endif
}
@@ -0,0 +1,44 @@
#pragma once
#include "lib/rocprofiler-sdk/hsa/queue.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/cid_manager.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/parser/pc_record_interface.hpp"
#include <rocprofiler-sdk/agent.h>
#include <rocprofiler-sdk/fwd.h>
#include <hsa/hsa.h>
#include <hsa/hsa_ven_amd_pc_sampling.h>
#include <memory>
namespace rocprofiler
{
namespace pc_sampling
{
// forward declaration to avoid circular dependency
class PCSCIDManager;
struct PCSAgentSession
{
const rocprofiler_agent_t* agent;
rocprofiler_pc_sampling_method_t method;
rocprofiler_pc_sampling_unit_t unit;
uint64_t interval;
rocprofiler_buffer_id_t buffer_id;
// hsa relevant information
std::optional<hsa_agent_t> hsa_agent = std::nullopt;
hsa_ven_amd_pcs_t hsa_pc_sampling;
hsa::ClientID intercept_cb_id{-1};
// ioctl relevant information
uint32_t ioctl_pcs_id;
// PC sampling parser
std::unique_ptr<PCSamplingParserContext> parser;
// Manager responsible for retiring CIDs
std::unique_ptr<PCSCIDManager> cid_manager;
};
// TODO static assertions
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,79 @@
// MIT License
//
// Copyright (c) 2023 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.
#include "lib/rocprofiler-sdk/pc_sampling/utils.hpp"
#include "lib/rocprofiler-sdk/agent.hpp"
#include <hsa/hsa_ext_amd.h>
#include <hsa/hsa_ven_amd_pc_sampling.h>
#include <fmt/core.h>
#include <glog/logging.h>
#include <vector>
namespace rocprofiler
{
namespace pc_sampling
{
namespace utils
{
hsa_ven_amd_pcs_method_kind_t
get_matching_hsa_pcs_method(rocprofiler_pc_sampling_method_t method)
{
switch(method)
{
case ROCPROFILER_PC_SAMPLING_METHOD_STOCHASTIC: return HSA_VEN_AMD_PCS_METHOD_STOCHASTIC_V1;
case ROCPROFILER_PC_SAMPLING_METHOD_HOST_TRAP: return HSA_VEN_AMD_PCS_METHOD_HOSTTRAP_V1;
default: throw std::runtime_error("Illegal pc sampling method\n");
}
}
hsa_ven_amd_pcs_units_t
get_matching_hsa_pcs_units(rocprofiler_pc_sampling_unit_t unit)
{
switch(unit)
{
case ROCPROFILER_PC_SAMPLING_UNIT_NONE: break;
case ROCPROFILER_PC_SAMPLING_UNIT_INSTRUCTIONS:
return HSA_VEN_AMD_PCS_INTERVAL_UNITS_INSTRUCTIONS;
case ROCPROFILER_PC_SAMPLING_UNIT_CYCLES:
return HSA_VEN_AMD_PCS_INTERVAL_UNITS_CLOCK_CYCLES;
case ROCPROFILER_PC_SAMPLING_UNIT_TIME: return HSA_VEN_AMD_PCS_INTERVAL_UNITS_MICRO_SECONDS;
case ROCPROFILER_PC_SAMPLING_UNIT_LAST: break;
}
throw std::runtime_error("Illegal pc sampling units\n");
}
uint64_t
get_unique_correlation_id()
{
// TODO: Remove once we confirmed it is unnecessary.
// Also, update the PC sampling parser not to decode correlation ID
// (or always 0 for both internal/external correlation IDs)
static auto _cnt = std::atomic<int>{0};
return ++_cnt;
}
} // namespace utils
} // namespace pc_sampling
} // namespace rocprofiler
@@ -0,0 +1,62 @@
// MIT License
//
// Copyright (c) 2023 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.
#include <rocprofiler-sdk/fwd.h>
#include <rocprofiler-sdk/pc_sampling.h>
#include <hsa/hsa.h>
#include <hsa/hsa_ven_amd_pc_sampling.h>
#include <cstdint>
#include <optional>
namespace rocprofiler
{
namespace pc_sampling
{
namespace utils
{
hsa_ven_amd_pcs_method_kind_t
get_matching_hsa_pcs_method(rocprofiler_pc_sampling_method_t method);
hsa_ven_amd_pcs_units_t
get_matching_hsa_pcs_units(rocprofiler_pc_sampling_unit_t unit);
inline constexpr size_t
get_hsa_pcs_latency()
{
// TODO: Check with David about the default value in the hsa-runtime
return 1000;
}
inline constexpr size_t
get_hsa_pcs_buffer_size()
{
// TODO: Find the minimum size of all buffers and use that.
return 1024 * sizeof(perf_sample_hosttrap_v1_t);
}
uint64_t
get_unique_correlation_id();
} // namespace utils
} // namespace pc_sampling
} // namespace rocprofiler
@@ -39,6 +39,8 @@
#include "lib/rocprofiler-sdk/internal_threading.hpp"
#include "lib/rocprofiler-sdk/marker/marker.hpp"
#include "lib/rocprofiler-sdk/page_migration/page_migration.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/code_object.hpp"
#include "lib/rocprofiler-sdk/pc_sampling/service.hpp"
#include <rocprofiler-sdk/context.h>
#include <rocprofiler-sdk/fwd.h>
@@ -601,6 +603,10 @@ finalize()
hsa::async_copy_fini();
hsa::queue_controller_fini();
page_migration::finalize();
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
// WARNING: this must precede `code_object::finalize()`
pc_sampling::code_object::finalize();
#endif
code_object::finalize();
if(get_init_status() > 0)
{
@@ -753,6 +759,9 @@ rocprofiler_set_api_table(const char* name,
rocprofiler::hsa::async_copy_init(hsa_api_table, lib_instance);
rocprofiler::code_object::initialize(hsa_api_table);
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
rocprofiler::pc_sampling::code_object::initialize(hsa_api_table);
#endif
// install rocprofiler API wrappers
rocprofiler::hsa::update_table(hsa_api_table->core_, lib_instance);
@@ -761,6 +770,11 @@ rocprofiler_set_api_table(const char* name,
rocprofiler::hsa::update_table(hsa_api_table->finalizer_ext_, lib_instance);
rocprofiler::hsa::update_table(hsa_api_table->tools_, lib_instance);
#if ROCPROFILER_SDK_HSA_PC_SAMPLING > 0
// Initialize PC sampling service if configured
rocprofiler::pc_sampling::post_hsa_init_start_active_service();
#endif
// allow tools to install API wrappers
rocprofiler::intercept_table::notify_intercept_table_registration(
ROCPROFILER_HSA_TABLE, lib_version, lib_instance, std::make_tuple(hsa_api_table));
@@ -816,6 +830,7 @@ rocprofiler_set_api_table(const char* name,
return 0;
}
// #if 0
bool
OnLoad(HsaApiTable* table,
uint64_t runtime_version,
@@ -843,4 +858,5 @@ OnUnload()
::rocprofiler::registration::finalize();
ROCP_INFO << "Finalization complete.";
}
// #endif
}
@@ -107,6 +107,11 @@ ROCPROFILER_STATUS_STRING(ROCPROFILER_STATUS_ERROR_NO_HARDWARE_COUNTERS,
"Counter set does not include any hardware counters")
ROCPROFILER_STATUS_STRING(ROCPROFILER_STATUS_ERROR_AGENT_MISMATCH,
"Counter profile agent does not match the agent in the context")
ROCPROFILER_STATUS_STRING(ROCPROFILER_STATUS_ERROR_NOT_AVAILABLE,
"The service is not available."
"Please refer to API functions that return this status code"
"for more information.")
template <size_t Idx, size_t... Tail>
const char*
get_status_name(rocprofiler_status_t status, std::index_sequence<Idx, Tail...>)
@@ -101,13 +101,11 @@ TEST(rocprofiler_lib, agent_abi)
EXPECT_EQ(offsetof(rocprofiler_agent_t, vendor_name), 256) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, product_name), 264) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, model_name), 272) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, num_pc_sampling_configs), 280) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, pc_sampling_configs), 288) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, node_id), 296) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, logical_node_id), 300) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, node_id), 280) << msg;
EXPECT_EQ(offsetof(rocprofiler_agent_t, logical_node_id), 284) << msg;
// Add test for offset of new field above this. Do NOT change any existing values!
constexpr auto expected_rocp_agent_size = 304;
constexpr auto expected_rocp_agent_size = 288;
// If a new field is added, increase this value by the size of the new field(s)
EXPECT_EQ(sizeof(rocprofiler_agent_t), expected_rocp_agent_size)
<< "ABI break. If you added a new field, make sure that this is the only new check that "
@@ -21,7 +21,7 @@
// SOFTWARE.
#include "lib/common/defines.hpp"
#include "lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h"
#include "lib/rocprofiler-sdk/details/kfd_ioctl.h"
#include "lib/rocprofiler-sdk/page_migration/utils.hpp"
#include <rocprofiler-sdk/fwd.h>