From fd3d97287c4fe84f8947176993d8d46a4d0fe476 Mon Sep 17 00:00:00 2001 From: Mythreya Date: Fri, 12 Apr 2024 13:51:44 -0700 Subject: [PATCH] Page migration reporting (#651) * Page migration reporting support * Page migration: Update parser and reporting Container does not lave latest KFD header, so CI might fail * Add kfd_ioctl.h * Formatting * Update get_key - get key was not used (and shouldn't be), so delete it * clang-tidy fixes * Tests for page migration * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update tests/bin/page-migration/CMakeLists.txt Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update page-migration test app - add hipHostRegister to register mmap'ed allocation with HIP - misc cleanup and reorg - remove HSA_XNACK=1 from test env * Update lib/rocprofiler-sdk/tests/page_migration.cpp - fix compilation error * Minor updates (reorg, rename) * Page migration reporting support * Page migration: Update parser and reporting Container does not lave latest KFD header, so CI might fail * Update page migration tests, fix trigger types * Page Migration Tracing Support Refactoring (#753) * Reorganization * Update page migration init/fini * Formatting * Update page_migration.cpp - change logging severity * Skip test if KFD does not support page migration reporting * Rework skipping test if KFD does not support page migration * Fix event trigger enum values * Fix clang-diagnostic-unused-const-variable --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Jonathan R. Madsen Co-authored-by: Jonathan R. Madsen --- .../include/rocprofiler-sdk/buffer_tracing.h | 97 +- source/include/rocprofiler-sdk/fwd.h | 19 + source/lib/rocprofiler-sdk/CMakeLists.txt | 1 + source/lib/rocprofiler-sdk/buffer_tracing.cpp | 17 +- .../page_migration/CMakeLists.txt | 9 + .../page_migration/defines.hpp | 87 + .../page_migration/details/CMakeLists.txt | 7 + .../page_migration/details/kfd_ioctl.h | 1711 +++++++++++++++++ .../page_migration/page_migration.cpp | 1143 +++++++++++ .../page_migration/page_migration.def.cpp | 74 + .../page_migration/page_migration.hpp | 45 + .../rocprofiler-sdk/page_migration/utils.hpp | 218 +++ source/lib/rocprofiler-sdk/registration.cpp | 2 + source/lib/rocprofiler-sdk/rocprofiler.cpp | 2 + .../lib/rocprofiler-sdk/tests/CMakeLists.txt | 5 +- .../rocprofiler-sdk/tests/page_migration.cpp | 177 ++ source/scripts/format-deps.py | 2 + tests/CMakeLists.txt | 1 + tests/bin/CMakeLists.txt | 1 + tests/bin/page-migration/CMakeLists.txt | 47 + tests/bin/page-migration/page-migration.cpp | 222 +++ tests/common/defines.hpp | 4 +- tests/common/serialization.hpp | 84 + tests/page-migration/CMakeLists.txt | 65 + tests/page-migration/conftest.py | 27 + tests/page-migration/pytest.ini | 4 + tests/page-migration/validate.py | 344 ++++ tests/tools/json-tool.cpp | 53 +- 28 files changed, 4455 insertions(+), 13 deletions(-) create mode 100644 source/lib/rocprofiler-sdk/page_migration/CMakeLists.txt create mode 100644 source/lib/rocprofiler-sdk/page_migration/defines.hpp create mode 100644 source/lib/rocprofiler-sdk/page_migration/details/CMakeLists.txt create mode 100644 source/lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h create mode 100644 source/lib/rocprofiler-sdk/page_migration/page_migration.cpp create mode 100644 source/lib/rocprofiler-sdk/page_migration/page_migration.def.cpp create mode 100644 source/lib/rocprofiler-sdk/page_migration/page_migration.hpp create mode 100644 source/lib/rocprofiler-sdk/page_migration/utils.hpp create mode 100644 source/lib/rocprofiler-sdk/tests/page_migration.cpp mode change 100644 => 100755 source/scripts/format-deps.py create mode 100644 tests/bin/page-migration/CMakeLists.txt create mode 100644 tests/bin/page-migration/page-migration.cpp create mode 100644 tests/page-migration/CMakeLists.txt create mode 100644 tests/page-migration/conftest.py create mode 100644 tests/page-migration/pytest.ini create mode 100644 tests/page-migration/validate.py diff --git a/source/include/rocprofiler-sdk/buffer_tracing.h b/source/include/rocprofiler-sdk/buffer_tracing.h index a330ab2447..90261bd66b 100644 --- a/source/include/rocprofiler-sdk/buffer_tracing.h +++ b/source/include/rocprofiler-sdk/buffer_tracing.h @@ -26,6 +26,8 @@ #include #include +#include + ROCPROFILER_EXTERN_C_INIT /** @@ -35,6 +37,49 @@ ROCPROFILER_EXTERN_C_INIT * @{ */ +/** + * @brief Page migration triggers + * + */ +typedef enum +{ + ROCPROFILER_PAGE_MIGRATION_TRIGGER_NONE = -1, + ROCPROFILER_PAGE_MIGRATION_TRIGGER_PREFETCH, + ROCPROFILER_PAGE_MIGRATION_TRIGGER_PAGEFAULT_GPU, + ROCPROFILER_PAGE_MIGRATION_TRIGGER_PAGEFAULT_CPU, + ROCPROFILER_PAGE_MIGRATION_TRIGGER_TTM_EVICTION, + ROCPROFILER_PAGE_MIGRATION_TRIGGER_LAST, +} rocprofiler_page_migration_trigger_t; + +/** + * @brief Page migration triggers causing the queue to suspend + * + */ +typedef enum +{ + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_NONE = -1, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_SVM, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_USERPTR, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_TTM, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_SUSPEND, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_CRIU_CHECKPOINT, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_CRIU_RESTORE, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_LAST, +} rocprofiler_page_migration_queue_suspend_trigger_t; + +/** + * @brief Page migration triggers causing an unmap from the GPU + * + */ +typedef enum +{ + ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_NONE = -1, + ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_MMU_NOTIFY, + ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_MMU_NOTIFY_MIGRATE, + ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_UNMAP_FROM_CPU, + ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_LAST, +} rocprofiler_page_migration_unmap_from_gpu_trigger_t; + /** * @brief ROCProfiler Buffer HSA API Tracer Record. */ @@ -150,17 +195,63 @@ typedef struct rocprofiler_buffer_tracing_kernel_dispatch_record_t /// @brief runtime grid size } rocprofiler_buffer_tracing_kernel_dispatch_record_t; +typedef struct +{ + uint8_t read_fault : 1; ///< Is the fault due to a read or a write + uint8_t migrated : 1; + uint32_t node_id; ///< GPU or CPU node ID which reports a page fault + uint64_t address; ///< Address access that caused the page fault +} rocprofiler_buffer_tracing_page_migration_page_fault_record_t; + +typedef struct +{ + uint64_t start_addr; ///< Start address of the page being migrated + uint64_t end_addr; ///< End address of the page being migrated + uint32_t from_node; ///< Source node + uint32_t to_node; ///< Destination node + uint32_t prefetch_node; ///< Node from which page was prefetched + uint32_t preferred_node; ///< Preferred destinaion node + rocprofiler_page_migration_trigger_t trigger; ///< Cause of migration +} rocprofiler_buffer_tracing_page_migration_page_migrate_record_t; + +typedef struct +{ + uint8_t rescheduled : 1; + uint32_t node_id; ///< GPU node from which the queue was suspended + rocprofiler_page_migration_queue_suspend_trigger_t trigger; ///< Cause of queue suspension +} rocprofiler_buffer_tracing_page_migration_queue_suspend_record_t; + +typedef struct +{ + uint32_t node_id; ///< Node ID from which page was unmapped + uint64_t start_addr; ///< Start address of unmapped page + uint64_t end_addr; ///< End address of unmapped page + rocprofiler_page_migration_unmap_from_gpu_trigger_t trigger; ///< Cause of unmap +} rocprofiler_buffer_tracing_page_migration_unmap_from_gpu_record_t; + /** - * @brief ROCProfiler Buffer Page Migration Tracer Record. Not implemented. + * @brief ROCProfiler Buffer Page Migration Tracer Record */ typedef struct { uint64_t size; ///< size of this struct rocprofiler_buffer_tracing_kind_t kind; ///< ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION - rocprofiler_correlation_id_t correlation_id; ///< correlation ids for record + rocprofiler_tracing_operation_t operation; rocprofiler_timestamp_t start_timestamp; ///< start time in nanoseconds rocprofiler_timestamp_t end_timestamp; ///< end time in nanoseconds - // Not Sure What is the info needed here? + uint32_t pid; + + union + { + rocprofiler_buffer_tracing_page_migration_page_fault_record_t page_fault; + rocprofiler_buffer_tracing_page_migration_page_migrate_record_t page_migrate; + rocprofiler_buffer_tracing_page_migration_queue_suspend_record_t queue_suspend; + rocprofiler_buffer_tracing_page_migration_unmap_from_gpu_record_t unmap_from_gpu; + struct + { + uint64_t reserved[12]; + }; + }; } rocprofiler_buffer_tracing_page_migration_record_t; /** diff --git a/source/include/rocprofiler-sdk/fwd.h b/source/include/rocprofiler-sdk/fwd.h index bafd8deb15..00e6976050 100644 --- a/source/include/rocprofiler-sdk/fwd.h +++ b/source/include/rocprofiler-sdk/fwd.h @@ -89,6 +89,10 @@ typedef enum // NOLINT(performance-enum-size) ROCPROFILER_STATUS_ERROR_AST_GENERATION_FAILED, ///< AST could not be generated correctly ROCPROFILER_STATUS_ERROR_AST_NOT_FOUND, ///< AST was not found ROCPROFILER_STATUS_ERROR_AQL_NO_EVENT_COORD, ///< Event coordinate was not found by AQL profile + ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL, ///< A service depends on a newer version of KFD + ///< (amdgpu kernel driver). Check logs for + ///< service that report incompatibility + ROCPROFILER_STATUS_LAST, } rocprofiler_status_t; @@ -202,6 +206,21 @@ typedef enum // NOLINT(performance-enum-size) ROCPROFILER_MEMORY_COPY_LAST, } rocprofiler_memory_copy_operation_t; +/** + * @brief Page migration event. + */ +typedef enum // NOLINT(performance-enum-size) +{ + ROCPROFILER_PAGE_MIGRATION_NONE = 0, ///< Unknown event + ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, + ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, + ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, + // Any and all events, from all processes. Requires superuser + // ROCPROFILER_PAGE_MIGRATION_ANY_ALL_PROCESSES, + ROCPROFILER_PAGE_MIGRATION_LAST, +} rocprofiler_page_migration_operation_t; + /** * @brief ROCProfiler Kernel Dispatch Tracing Operation Types. */ diff --git a/source/lib/rocprofiler-sdk/CMakeLists.txt b/source/lib/rocprofiler-sdk/CMakeLists.txt index d2e5ea7ab8..2fc078a9c5 100644 --- a/source/lib/rocprofiler-sdk/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/CMakeLists.txt @@ -45,6 +45,7 @@ add_subdirectory(marker) add_subdirectory(thread_trace) add_subdirectory(tracing) add_subdirectory(kernel_dispatch) +add_subdirectory(page_migration) target_link_libraries( rocprofiler-object-library diff --git a/source/lib/rocprofiler-sdk/buffer_tracing.cpp b/source/lib/rocprofiler-sdk/buffer_tracing.cpp index fad77dbf3f..cb207d4942 100644 --- a/source/lib/rocprofiler-sdk/buffer_tracing.cpp +++ b/source/lib/rocprofiler-sdk/buffer_tracing.cpp @@ -34,12 +34,15 @@ #include "lib/rocprofiler-sdk/hsa/scratch_memory.hpp" #include "lib/rocprofiler-sdk/kernel_dispatch/kernel_dispatch.hpp" #include "lib/rocprofiler-sdk/marker/marker.hpp" +#include "lib/rocprofiler-sdk/page_migration/page_migration.hpp" #include "lib/rocprofiler-sdk/registration.hpp" #include #include #include +#include +#include #include #define RETURN_STATUS_ON_FAIL(...) \ @@ -105,8 +108,7 @@ rocprofiler_configure_buffer_tracing_service(rocprofiler_context_id_t c if(rocprofiler::registration::get_init_status() > -1) return ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED; - static auto unsupported = std::unordered_set{ - ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION}; + static auto unsupported = std::unordered_set{}; if(unsupported.count(kind) > 0) return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED; auto* ctx = rocprofiler::context::get_mutable_registered_context(context_id); @@ -137,6 +139,9 @@ rocprofiler_configure_buffer_tracing_service(rocprofiler_context_id_t c ctx->buffered_tracer->domains, kind, operations[i])); } + if(kind == ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION) + RETURN_STATUS_ON_FAIL(rocprofiler::page_migration::init()); + return ROCPROFILER_STATUS_SUCCESS; } @@ -233,6 +238,10 @@ rocprofiler_query_buffer_tracing_kind_operation_name(rocprofiler_buffer_tracing_ break; } case ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION: + { + val = rocprofiler::page_migration::name_by_id(operation); + break; + } case ROCPROFILER_BUFFER_TRACING_CORRELATION_ID_RETIREMENT: { return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED; @@ -340,6 +349,10 @@ rocprofiler_iterate_buffer_tracing_kind_operations( break; } case ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION: + { + ops = rocprofiler::page_migration::get_ids(); + break; + } case ROCPROFILER_BUFFER_TRACING_CORRELATION_ID_RETIREMENT: { return ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED; diff --git a/source/lib/rocprofiler-sdk/page_migration/CMakeLists.txt b/source/lib/rocprofiler-sdk/page_migration/CMakeLists.txt new file mode 100644 index 0000000000..874d8ae1a8 --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/CMakeLists.txt @@ -0,0 +1,9 @@ +# +# +set(ROCPROFILER_LIB_UVM_SOURCES page_migration.cpp) +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) diff --git a/source/lib/rocprofiler-sdk/page_migration/defines.hpp b/source/lib/rocprofiler-sdk/page_migration/defines.hpp new file mode 100644 index 0000000000..0468c3804b --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/defines.hpp @@ -0,0 +1,87 @@ +// 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. + +#pragma once + +#define KFD_EVENT_PARSE_EVENTS(X, HANDLER) \ + do \ + { \ + const auto find_newline = [&](auto b) { return std::find(b, X.cend(), '\n'); }; \ + \ + const auto* cursor = X.cbegin(); \ + \ + for(const auto* pos = find_newline(cursor); pos != X.cend(); pos = find_newline(cursor)) \ + { \ + size_t char_count = pos - cursor; \ + assert(char_count > 0); \ + std::string_view event_str{cursor, char_count}; \ + \ + LOG(INFO) << fmt::format("KFD event: [{}]", event_str); \ + HANDLER(event_str); \ + \ + cursor = pos + 1; \ + } \ + } while(0) + +#define APPEND_UVM_EVENT(X) ROCPROFILER_UVM_EVENT_##X +#define APPEND_1(X) APPEND_UVM_EVENT(X) +#define CONCAT(X, Y) X##Y +#define APPEND_2(A1, A2) APPEND_1(A1), APPEND_1(A2) +#define APPEND_3(A1, A2, A3) APPEND_2(A1, A2), APPEND_1(A3) +#define APPEND_4(A1, A2, A3, A4) APPEND_3(A1, A2, A3), APPEND_1(A4) +#define APPEND_5(A1, A2, A3, A4, A5) APPEND_4(A1, A2, A3, A4), APPEND_1(A5) + +#define MACRO_N(MACRO, N, ...) CONCAT(MACRO, N)(__VA_ARGS__) +#define APPLY_N(MACRO, ...) MACRO_N(MACRO, IMPL_DETAIL_FOR_EACH_NARG(__VA_ARGS__), __VA_ARGS__) + +#define GET_UVM_ENUMS(...) APPLY_N(APPEND_, __VA_ARGS__) + +// static constexpr size_t uvm_event = UVM_ENUM; +#define SPECIALIZE_UVM_KFD_EVENT(UVM_ENUM, KFD_ENUM, FORMAT_STRING) \ + template <> \ + struct uvm_event_info \ + { \ + static constexpr size_t kfd_event = KFD_ENUM; \ + static constexpr std::string_view format_str{FORMAT_STRING}; \ + }; + +#define SPECIALIZE_PAGE_MIGRATION_INFO(TYPE, ...) \ + template <> \ + struct page_migration_info \ + { \ + static constexpr auto operation_idx = ROCPROFILER_PAGE_MIGRATION_##TYPE; \ + static constexpr auto name = #TYPE; \ + static constexpr size_t uvm_bitmask = \ + bitmask(std::index_sequence()); \ + static constexpr size_t kfd_bitmask = \ + to_kfd_bitmask(std::index_sequence()); \ + } + +#define COPY_FROM_START_1(MEMBER) end.MEMBER = start.MEMBER; +#define COPY_FROM_START_2(UNION_TYPE, MEMBER) end.UNION_TYPE.MEMBER = start.UNION_TYPE.MEMBER; + +#define SPECIALIZE_KFD_IOC_IOCTL(STRUCT, ARG_IOC) \ + template <> \ + struct IOC_event \ + { \ + static constexpr auto value = ARG_IOC; \ + } diff --git a/source/lib/rocprofiler-sdk/page_migration/details/CMakeLists.txt b/source/lib/rocprofiler-sdk/page_migration/details/CMakeLists.txt new file mode 100644 index 0000000000..7e06f953c1 --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/details/CMakeLists.txt @@ -0,0 +1,7 @@ +# +# +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}) diff --git a/source/lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h b/source/lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h new file mode 100644 index 0000000000..86ad6c2aa6 --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h @@ -0,0 +1,1711 @@ +// clang-format off +/* + * Copyright 2014 Advanced Micro Devices, Inc. + * + * 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) 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. + */ + +#ifndef KFD_IOCTL_H_INCLUDED +#define KFD_IOCTL_H_INCLUDED + +#include +#include + +/* + * - 1.1 - initial version + * - 1.3 - Add SMI events support + * - 1.4 - Indicate new SRAM EDC bit in device properties + * - 1.5 - Add SVM API + * - 1.6 - Query clear flags in SVM get_attr API + * - 1.7 - Checkpoint Restore (CRIU) API + * - 1.8 - CRIU - Support for SDMA transfers with GTT BOs + * - 1.9 - Add available memory ioctl + * - 1.10 - Add SMI profiler event log + * - 1.11 - Add unified memory for ctx save/restore area + * - 1.12 - Add DMA buf export ioctl + * - 1.13 - Add debugger API + * - 1.14 - Update kfd_event_data + */ +#define KFD_IOCTL_MAJOR_VERSION 1 +#define KFD_IOCTL_MINOR_VERSION 14 + +struct kfd_ioctl_get_version_args { + __u32 major_version; /* from KFD */ + __u32 minor_version; /* from KFD */ +}; + +/* For kfd_ioctl_create_queue_args.queue_type. */ +#define KFD_IOC_QUEUE_TYPE_COMPUTE 0x0 +#define KFD_IOC_QUEUE_TYPE_SDMA 0x1 +#define KFD_IOC_QUEUE_TYPE_COMPUTE_AQL 0x2 +#define KFD_IOC_QUEUE_TYPE_SDMA_XGMI 0x3 + +#define KFD_MAX_QUEUE_PERCENTAGE 100 +#define KFD_MAX_QUEUE_PRIORITY 15 + +struct kfd_ioctl_create_queue_args { + __u64 ring_base_address; /* to KFD */ + __u64 write_pointer_address; /* from KFD */ + __u64 read_pointer_address; /* from KFD */ + __u64 doorbell_offset; /* from KFD */ + + __u32 ring_size; /* to KFD */ + __u32 gpu_id; /* to KFD */ + __u32 queue_type; /* to KFD */ + __u32 queue_percentage; /* to KFD */ + __u32 queue_priority; /* to KFD */ + __u32 queue_id; /* from KFD */ + + __u64 eop_buffer_address; /* to KFD */ + __u64 eop_buffer_size; /* to KFD */ + __u64 ctx_save_restore_address; /* to KFD */ + __u32 ctx_save_restore_size; /* to KFD */ + __u32 ctl_stack_size; /* to KFD */ +}; + +struct kfd_ioctl_destroy_queue_args { + __u32 queue_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_update_queue_args { + __u64 ring_base_address; /* to KFD */ + + __u32 queue_id; /* to KFD */ + __u32 ring_size; /* to KFD */ + __u32 queue_percentage; /* to KFD */ + __u32 queue_priority; /* to KFD */ +}; + +struct kfd_ioctl_set_cu_mask_args { + __u32 queue_id; /* to KFD */ + __u32 num_cu_mask; /* to KFD */ + __u64 cu_mask_ptr; /* to KFD */ +}; + +struct kfd_ioctl_get_queue_wave_state_args { + __u64 ctl_stack_address; /* to KFD */ + __u32 ctl_stack_used_size; /* from KFD */ + __u32 save_area_used_size; /* from KFD */ + __u32 queue_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_get_available_memory_args { + __u64 available; /* from KFD */ + __u32 gpu_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_dbg_device_info_entry { + __u64 exception_status; + __u64 lds_base; + __u64 lds_limit; + __u64 scratch_base; + __u64 scratch_limit; + __u64 gpuvm_base; + __u64 gpuvm_limit; + __u32 gpu_id; + __u32 location_id; + __u32 vendor_id; + __u32 device_id; + __u32 revision_id; + __u32 subsystem_vendor_id; + __u32 subsystem_device_id; + __u32 fw_version; + __u32 gfx_target_version; + __u32 simd_count; + __u32 max_waves_per_simd; + __u32 array_count; + __u32 simd_arrays_per_engine; + __u32 num_xcc; + __u32 capability; + __u32 debug_prop; +}; + +/* For kfd_ioctl_set_memory_policy_args.default_policy and alternate_policy */ +#define KFD_IOC_CACHE_POLICY_COHERENT 0 +#define KFD_IOC_CACHE_POLICY_NONCOHERENT 1 + +struct kfd_ioctl_set_memory_policy_args { + __u64 alternate_aperture_base; /* to KFD */ + __u64 alternate_aperture_size; /* to KFD */ + + __u32 gpu_id; /* to KFD */ + __u32 default_policy; /* to KFD */ + __u32 alternate_policy; /* to KFD */ + __u32 pad; +}; + +/* + * All counters are monotonic. They are used for profiling of compute jobs. + * The profiling is done by userspace. + * + * In case of GPU reset, the counter should not be affected. + */ + +struct kfd_ioctl_get_clock_counters_args { + __u64 gpu_clock_counter; /* from KFD */ + __u64 cpu_clock_counter; /* from KFD */ + __u64 system_clock_counter; /* from KFD */ + __u64 system_clock_freq; /* from KFD */ + + __u32 gpu_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_process_device_apertures { + __u64 lds_base; /* from KFD */ + __u64 lds_limit; /* from KFD */ + __u64 scratch_base; /* from KFD */ + __u64 scratch_limit; /* from KFD */ + __u64 gpuvm_base; /* from KFD */ + __u64 gpuvm_limit; /* from KFD */ + __u32 gpu_id; /* from KFD */ + __u32 pad; +}; + +/* + * AMDKFD_IOC_GET_PROCESS_APERTURES is deprecated. Use + * AMDKFD_IOC_GET_PROCESS_APERTURES_NEW instead, which supports an + * unlimited number of GPUs. + */ +#define NUM_OF_SUPPORTED_GPUS 7 +struct kfd_ioctl_get_process_apertures_args { + struct kfd_process_device_apertures + process_apertures[NUM_OF_SUPPORTED_GPUS];/* from KFD */ + + /* from KFD, should be in the range [1 - NUM_OF_SUPPORTED_GPUS] */ + __u32 num_of_nodes; + __u32 pad; +}; + +struct kfd_ioctl_get_process_apertures_new_args { + /* User allocated. Pointer to struct kfd_process_device_apertures + * filled in by Kernel + */ + __u64 kfd_process_device_apertures_ptr; + /* to KFD - indicates amount of memory present in + * kfd_process_device_apertures_ptr + * from KFD - Number of entries filled by KFD. + */ + __u32 num_of_nodes; + __u32 pad; +}; + +#define MAX_ALLOWED_NUM_POINTS 100 +#define MAX_ALLOWED_AW_BUFF_SIZE 4096 +#define MAX_ALLOWED_WAC_BUFF_SIZE 128 + +struct kfd_ioctl_dbg_register_args { + __u32 gpu_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_dbg_unregister_args { + __u32 gpu_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_dbg_address_watch_args { + __u64 content_ptr; /* a pointer to the actual content */ + __u32 gpu_id; /* to KFD */ + __u32 buf_size_in_bytes; /*including gpu_id and buf_size */ +}; + +struct kfd_ioctl_dbg_wave_control_args { + __u64 content_ptr; /* a pointer to the actual content */ + __u32 gpu_id; /* to KFD */ + __u32 buf_size_in_bytes; /*including gpu_id and buf_size */ +}; + +#define KFD_INVALID_FD 0xffffffff + +struct kfd_ioctl_dbg_trap_args_deprecated { + __u64 exception_mask; /* to KFD */ + __u64 ptr; /* to KFD */ + __u32 pid; /* to KFD */ + __u32 op; /* to KFD */ + __u32 data1; /* to KFD */ + __u32 data2; /* to KFD */ + __u32 data3; /* to KFD */ + __u32 data4; /* to KFD */ +}; + +/* Matching HSA_EVENTTYPE */ +#define KFD_IOC_EVENT_SIGNAL 0 +#define KFD_IOC_EVENT_NODECHANGE 1 +#define KFD_IOC_EVENT_DEVICESTATECHANGE 2 +#define KFD_IOC_EVENT_HW_EXCEPTION 3 +#define KFD_IOC_EVENT_SYSTEM_EVENT 4 +#define KFD_IOC_EVENT_DEBUG_EVENT 5 +#define KFD_IOC_EVENT_PROFILE_EVENT 6 +#define KFD_IOC_EVENT_QUEUE_EVENT 7 +#define KFD_IOC_EVENT_MEMORY 8 + +#define KFD_IOC_WAIT_RESULT_COMPLETE 0 +#define KFD_IOC_WAIT_RESULT_TIMEOUT 1 +#define KFD_IOC_WAIT_RESULT_FAIL 2 + +#define KFD_SIGNAL_EVENT_LIMIT 4096 + +/* For kfd_event_data.hw_exception_data.reset_type. */ +#define KFD_HW_EXCEPTION_WHOLE_GPU_RESET 0 +#define KFD_HW_EXCEPTION_PER_ENGINE_RESET 1 + +/* For kfd_event_data.hw_exception_data.reset_cause. */ +#define KFD_HW_EXCEPTION_GPU_HANG 0 +#define KFD_HW_EXCEPTION_ECC 1 + +/* For kfd_hsa_memory_exception_data.ErrorType */ +#define KFD_MEM_ERR_NO_RAS 0 +#define KFD_MEM_ERR_SRAM_ECC 1 +#define KFD_MEM_ERR_POISON_CONSUMED 2 +#define KFD_MEM_ERR_GPU_HANG 3 + +struct kfd_ioctl_create_event_args { + __u64 event_page_offset; /* from KFD */ + __u32 event_trigger_data; /* from KFD - signal events only */ + __u32 event_type; /* to KFD */ + __u32 auto_reset; /* to KFD */ + __u32 node_id; /* to KFD - only valid for certain + event types */ + __u32 event_id; /* from KFD */ + __u32 event_slot_index; /* from KFD */ +}; + +struct kfd_ioctl_destroy_event_args { + __u32 event_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_set_event_args { + __u32 event_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_reset_event_args { + __u32 event_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_memory_exception_failure { + __u32 NotPresent; /* Page not present or supervisor privilege */ + __u32 ReadOnly; /* Write access to a read-only page */ + __u32 NoExecute; /* Execute access to a page marked NX */ + __u32 imprecise; /* Can't determine the exact fault address */ +}; + +/* memory exception data */ +struct kfd_hsa_memory_exception_data { + struct kfd_memory_exception_failure failure; + __u64 va; + __u32 gpu_id; + __u32 ErrorType; /* 0 = no RAS error, + * 1 = ECC_SRAM, + * 2 = Link_SYNFLOOD (poison), + * 3 = GPU hang (not attributable to a specific cause), + * other values reserved + */ +}; + +/* hw exception data */ +struct kfd_hsa_hw_exception_data { + __u32 reset_type; + __u32 reset_cause; + __u32 memory_lost; + __u32 gpu_id; +}; + +/* hsa signal event data */ +struct kfd_hsa_signal_event_data { + __u64 last_event_age; /* to and from KFD */ +}; + +/* Event data */ +struct kfd_event_data { + union { + /* From KFD */ + struct kfd_hsa_memory_exception_data memory_exception_data; + struct kfd_hsa_hw_exception_data hw_exception_data; + /* To and From KFD */ + struct kfd_hsa_signal_event_data signal_event_data; + }; + __u64 kfd_event_data_ext; /* pointer to an extension structure + for future exception types */ + __u32 event_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_wait_events_args { + __u64 events_ptr; /* pointed to struct + kfd_event_data array, to KFD */ + __u32 num_events; /* to KFD */ + __u32 wait_for_all; /* to KFD */ + __u32 timeout; /* to KFD */ + __u32 wait_result; /* from KFD */ +}; + +struct kfd_ioctl_set_scratch_backing_va_args { + __u64 va_addr; /* to KFD */ + __u32 gpu_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_get_tile_config_args { + /* to KFD: pointer to tile array */ + __u64 tile_config_ptr; + /* to KFD: pointer to macro tile array */ + __u64 macro_tile_config_ptr; + /* to KFD: array size allocated by user mode + * from KFD: array size filled by kernel + */ + __u32 num_tile_configs; + /* to KFD: array size allocated by user mode + * from KFD: array size filled by kernel + */ + __u32 num_macro_tile_configs; + + __u32 gpu_id; /* to KFD */ + __u32 gb_addr_config; /* from KFD */ + __u32 num_banks; /* from KFD */ + __u32 num_ranks; /* from KFD */ + /* struct size can be extended later if needed + * without breaking ABI compatibility + */ +}; + +struct kfd_ioctl_set_trap_handler_args { + __u64 tba_addr; /* to KFD */ + __u64 tma_addr; /* to KFD */ + __u32 gpu_id; /* to KFD */ + __u32 pad; +}; + +struct kfd_ioctl_acquire_vm_args { + __u32 drm_fd; /* to KFD */ + __u32 gpu_id; /* to KFD */ +}; + +/* Allocation flags: memory types */ +#define KFD_IOC_ALLOC_MEM_FLAGS_VRAM (1 << 0) +#define KFD_IOC_ALLOC_MEM_FLAGS_GTT (1 << 1) +#define KFD_IOC_ALLOC_MEM_FLAGS_USERPTR (1 << 2) +#define KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL (1 << 3) +#define KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP (1 << 4) +/* Allocation flags: attributes/access options */ +#define KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE (1 << 31) +#define KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE (1 << 30) +#define KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC (1 << 29) +#define KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE (1 << 28) +#define KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM (1 << 27) +#define KFD_IOC_ALLOC_MEM_FLAGS_COHERENT (1 << 26) +#define KFD_IOC_ALLOC_MEM_FLAGS_UNCACHED (1 << 25) +#define KFD_IOC_ALLOC_MEM_FLAGS_EXT_COHERENT (1 << 24) + +/* Allocate memory for later SVM (shared virtual memory) mapping. + * + * @va_addr: virtual address of the memory to be allocated + * all later mappings on all GPUs will use this address + * @size: size in bytes + * @handle: buffer handle returned to user mode, used to refer to + * this allocation for mapping, unmapping and freeing + * @mmap_offset: for CPU-mapping the allocation by mmapping a render node + * for userptrs this is overloaded to specify the CPU address + * @gpu_id: device identifier + * @flags: memory type and attributes. See KFD_IOC_ALLOC_MEM_FLAGS above + */ +struct kfd_ioctl_alloc_memory_of_gpu_args { + __u64 va_addr; /* to KFD */ + __u64 size; /* to KFD */ + __u64 handle; /* from KFD */ + __u64 mmap_offset; /* to KFD (userptr), from KFD (mmap offset) */ + __u32 gpu_id; /* to KFD */ + __u32 flags; +}; + +/* Free memory allocated with kfd_ioctl_alloc_memory_of_gpu + * + * @handle: memory handle returned by alloc + */ +struct kfd_ioctl_free_memory_of_gpu_args { + __u64 handle; /* to KFD */ +}; + +/* Map memory to one or more GPUs + * + * @handle: memory handle returned by alloc + * @device_ids_array_ptr: array of gpu_ids (__u32 per device) + * @n_devices: number of devices in the array + * @n_success: number of devices mapped successfully + * + * @n_success returns information to the caller how many devices from + * the start of the array have mapped the buffer successfully. It can + * be passed into a subsequent retry call to skip those devices. For + * the first call the caller should initialize it to 0. + * + * If the ioctl completes with return code 0 (success), n_success == + * n_devices. + */ +struct kfd_ioctl_map_memory_to_gpu_args { + __u64 handle; /* to KFD */ + __u64 device_ids_array_ptr; /* to KFD */ + __u32 n_devices; /* to KFD */ + __u32 n_success; /* to/from KFD */ +}; + +/* Unmap memory from one or more GPUs + * + * same arguments as for mapping + */ +struct kfd_ioctl_unmap_memory_from_gpu_args { + __u64 handle; /* to KFD */ + __u64 device_ids_array_ptr; /* to KFD */ + __u32 n_devices; /* to KFD */ + __u32 n_success; /* to/from KFD */ +}; + +/* Allocate GWS for specific queue + * + * @queue_id: queue's id that GWS is allocated for + * @num_gws: how many GWS to allocate + * @first_gws: index of the first GWS allocated. + * only support contiguous GWS allocation + */ +struct kfd_ioctl_alloc_queue_gws_args { + __u32 queue_id; /* to KFD */ + __u32 num_gws; /* to KFD */ + __u32 first_gws; /* from KFD */ + __u32 pad; +}; + +struct kfd_ioctl_get_dmabuf_info_args { + __u64 size; /* from KFD */ + __u64 metadata_ptr; /* to KFD */ + __u32 metadata_size; /* to KFD (space allocated by user) + * from KFD (actual metadata size) + */ + __u32 gpu_id; /* from KFD */ + __u32 flags; /* from KFD (KFD_IOC_ALLOC_MEM_FLAGS) */ + __u32 dmabuf_fd; /* to KFD */ +}; + +struct kfd_ioctl_import_dmabuf_args { + __u64 va_addr; /* to KFD */ + __u64 handle; /* from KFD */ + __u32 gpu_id; /* to KFD */ + __u32 dmabuf_fd; /* to KFD */ +}; + +struct kfd_ioctl_export_dmabuf_args { + __u64 handle; /* to KFD */ + __u32 flags; /* to KFD */ + __u32 dmabuf_fd; /* from KFD */ +}; + +/* + * KFD SMI(System Management Interface) events + */ +enum kfd_smi_event { + KFD_SMI_EVENT_NONE = 0, /* not used */ + KFD_SMI_EVENT_VMFAULT = 1, /* event start counting at 1 */ + KFD_SMI_EVENT_THERMAL_THROTTLE = 2, + KFD_SMI_EVENT_GPU_PRE_RESET = 3, + KFD_SMI_EVENT_GPU_POST_RESET = 4, + KFD_SMI_EVENT_MIGRATE_START = 5, + KFD_SMI_EVENT_MIGRATE_END = 6, + KFD_SMI_EVENT_PAGE_FAULT_START = 7, + KFD_SMI_EVENT_PAGE_FAULT_END = 8, + KFD_SMI_EVENT_QUEUE_EVICTION = 9, + KFD_SMI_EVENT_QUEUE_RESTORE = 10, + KFD_SMI_EVENT_UNMAP_FROM_GPU = 11, + + /* + * max event number, as a flag bit to get events from all processes, + * this requires super user permission, otherwise will not be able to + * receive event from any process. Without this flag to receive events + * from same process. + */ + KFD_SMI_EVENT_ALL_PROCESS = 64 +}; + +enum KFD_MIGRATE_TRIGGERS { + KFD_MIGRATE_TRIGGER_PREFETCH, + KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU, + KFD_MIGRATE_TRIGGER_PAGEFAULT_CPU, + KFD_MIGRATE_TRIGGER_TTM_EVICTION +}; + +enum KFD_QUEUE_EVICTION_TRIGGERS { + KFD_QUEUE_EVICTION_TRIGGER_SVM, + KFD_QUEUE_EVICTION_TRIGGER_USERPTR, + KFD_QUEUE_EVICTION_TRIGGER_TTM, + KFD_QUEUE_EVICTION_TRIGGER_SUSPEND, + KFD_QUEUE_EVICTION_CRIU_CHECKPOINT, + KFD_QUEUE_EVICTION_CRIU_RESTORE +}; + +enum KFD_SVM_UNMAP_TRIGGERS { + KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY, + KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY_MIGRATE, + KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU +}; + +#define KFD_SMI_EVENT_MASK_FROM_INDEX(i) (1ULL << ((i) - 1)) +#define KFD_SMI_EVENT_MSG_SIZE 96 + +struct kfd_ioctl_smi_events_args { + __u32 gpuid; /* to KFD */ + __u32 anon_fd; /* from KFD */ +}; + +/** + * kfd_ioctl_spm_op - SPM ioctl operations + * + * @KFD_IOCTL_SPM_OP_ACQUIRE: acquire exclusive access to SPM + * @KFD_IOCTL_SPM_OP_RELEASE: release exclusive access to SPM + * @KFD_IOCTL_SPM_OP_SET_DEST_BUF: set or unset destination buffer for SPM streaming + */ +enum kfd_ioctl_spm_op { + KFD_IOCTL_SPM_OP_ACQUIRE, + KFD_IOCTL_SPM_OP_RELEASE, + KFD_IOCTL_SPM_OP_SET_DEST_BUF +}; + +/** + * kfd_ioctl_spm_args - Arguments for SPM ioctl + * + * @op[in]: specifies the operation to perform + * @gpu_id[in]: GPU ID of the GPU to profile + * @dst_buf[in]: used for the address of the destination buffer + * in @KFD_IOCTL_SPM_SET_DEST_BUFFER + * @buf_size[in]: size of the destination buffer + * @timeout[in/out]: [in]: timeout in milliseconds, [out]: amount of time left + * `in the timeout window + * @bytes_copied[out]: amount of data that was copied to the previous dest_buf + * @has_data_loss: boolean indicating whether data was lost + * (e.g. due to a ring-buffer overflow) + * + * This ioctl performs different functions depending on the @op parameter. + * + * KFD_IOCTL_SPM_OP_ACQUIRE + * ------------------------ + * + * Acquires exclusive access of SPM on the specified @gpu_id for the calling process. + * This must be called before using KFD_IOCTL_SPM_OP_SET_DEST_BUF. + * + * KFD_IOCTL_SPM_OP_RELEASE + * ------------------------ + * + * Releases exclusive access of SPM on the specified @gpu_id for the calling process, + * which allows another process to acquire it in the future. + * + * KFD_IOCTL_SPM_OP_SET_DEST_BUF + * ----------------------------- + * + * If @dst_buf is NULL, the destination buffer address is unset and copying of counters + * is stopped. + * + * If @dst_buf is not NULL, it specifies the pointer to a new destination buffer. + * @buf_size specifies the size of the buffer. + * + * If @timeout is non-0, the call will wait for up to @timeout ms for the previous + * buffer to be filled. If previous buffer to be filled before timeout, the @timeout + * will be updated value with the time remaining. If the timeout is exceeded, the function + * copies any partial data available into the previous user buffer and returns success. + * The amount of valid data in the previous user buffer is indicated by @bytes_copied. + * + * If @timeout is 0, the function immediately replaces the previous destination buffer + * without waiting for the previous buffer to be filled. That means the previous buffer + * may only be partially filled, and @bytes_copied will indicate how much data has been + * copied to it. + * + * If data was lost, e.g. due to a ring buffer overflow, @has_data_loss will be non-0. + * + * Returns negative error code on failure, 0 on success. + */ +struct kfd_ioctl_spm_args { + __u64 dest_buf; + __u32 buf_size; + __u32 op; + __u32 timeout; + __u32 gpu_id; + __u32 bytes_copied; + __u32 has_data_loss; +}; + +/************************************************************************************************** + * CRIU IOCTLs (Checkpoint Restore In Userspace) + * + * When checkpointing a process, the userspace application will perform: + * 1. PROCESS_INFO op to determine current process information. This pauses execution and evicts + * all the queues. + * 2. CHECKPOINT op to checkpoint process contents (BOs, queues, events, svm-ranges) + * 3. UNPAUSE op to un-evict all the queues + * + * When restoring a process, the CRIU userspace application will perform: + * + * 1. RESTORE op to restore process contents + * 2. RESUME op to start the process + * + * Note: Queues are forced into an evicted state after a successful PROCESS_INFO. User + * application needs to perform an UNPAUSE operation after calling PROCESS_INFO. + */ + +enum kfd_criu_op { + KFD_CRIU_OP_PROCESS_INFO, + KFD_CRIU_OP_CHECKPOINT, + KFD_CRIU_OP_UNPAUSE, + KFD_CRIU_OP_RESTORE, + KFD_CRIU_OP_RESUME, +}; + +/** + * kfd_ioctl_criu_args - Arguments perform CRIU operation + * @devices: [in/out] User pointer to memory location for devices information. + * This is an array of type kfd_criu_device_bucket. + * @bos: [in/out] User pointer to memory location for BOs information + * This is an array of type kfd_criu_bo_bucket. + * @priv_data: [in/out] User pointer to memory location for private data + * @priv_data_size: [in/out] Size of priv_data in bytes + * @num_devices: [in/out] Number of GPUs used by process. Size of @devices array. + * @num_bos [in/out] Number of BOs used by process. Size of @bos array. + * @num_objects: [in/out] Number of objects used by process. Objects are opaque to + * user application. + * @pid: [in/out] PID of the process being checkpointed + * @op [in] Type of operation (kfd_criu_op) + * + * Return: 0 on success, -errno on failure + */ +struct kfd_ioctl_criu_args { + __u64 devices; /* Used during ops: CHECKPOINT, RESTORE */ + __u64 bos; /* Used during ops: CHECKPOINT, RESTORE */ + __u64 priv_data; /* Used during ops: CHECKPOINT, RESTORE */ + __u64 priv_data_size; /* Used during ops: PROCESS_INFO, RESTORE */ + __u32 num_devices; /* Used during ops: PROCESS_INFO, RESTORE */ + __u32 num_bos; /* Used during ops: PROCESS_INFO, RESTORE */ + __u32 num_objects; /* Used during ops: PROCESS_INFO, RESTORE */ + __u32 pid; /* Used during ops: PROCESS_INFO, RESUME */ + __u32 op; +}; + +struct kfd_criu_device_bucket { + __u32 user_gpu_id; + __u32 actual_gpu_id; + __u32 drm_fd; + __u32 pad; +}; + +struct kfd_criu_bo_bucket { + __u64 addr; + __u64 size; + __u64 offset; + __u64 restored_offset; /* During restore, updated offset for BO */ + __u32 gpu_id; /* This is the user_gpu_id */ + __u32 alloc_flags; + __u32 dmabuf_fd; + __u32 pad; +}; + +/* CRIU IOCTLs - END */ +/**************************************************************************************************/ +/* Register offset inside the remapped mmio page + */ +enum kfd_mmio_remap { + KFD_MMIO_REMAP_HDP_MEM_FLUSH_CNTL = 0, + KFD_MMIO_REMAP_HDP_REG_FLUSH_CNTL = 4, +}; + +struct kfd_ioctl_ipc_export_handle_args { + __u64 handle; /* to KFD */ + __u32 share_handle[4]; /* from KFD */ + __u32 gpu_id; /* to KFD */ + __u32 flags; /* to KFD */ +}; + +struct kfd_ioctl_ipc_import_handle_args { + __u64 handle; /* from KFD */ + __u64 va_addr; /* to KFD */ + __u64 mmap_offset; /* from KFD */ + __u32 share_handle[4]; /* to KFD */ + __u32 gpu_id; /* to KFD */ + __u32 flags; /* from KFD */ +}; + +struct kfd_ioctl_cross_memory_copy_deprecated_args { + /* to KFD: Process ID of the remote process */ + __u32 pid; + /* to KFD: See above definition */ + __u32 flags; + /* to KFD: Source GPU VM range */ + __u64 src_mem_range_array; + /* to KFD: Size of above array */ + __u64 src_mem_array_size; + /* to KFD: Destination GPU VM range */ + __u64 dst_mem_range_array; + /* to KFD: Size of above array */ + __u64 dst_mem_array_size; + /* from KFD: Total amount of bytes copied */ + __u64 bytes_copied; +}; + +/* Guarantee host access to memory */ +#define KFD_IOCTL_SVM_FLAG_HOST_ACCESS 0x00000001 +/* Fine grained coherency between all devices with access */ +#define KFD_IOCTL_SVM_FLAG_COHERENT 0x00000002 +/* Use any GPU in same hive as preferred device */ +#define KFD_IOCTL_SVM_FLAG_HIVE_LOCAL 0x00000004 +/* GPUs only read, allows replication */ +#define KFD_IOCTL_SVM_FLAG_GPU_RO 0x00000008 +/* Allow execution on GPU */ +#define KFD_IOCTL_SVM_FLAG_GPU_EXEC 0x00000010 +/* GPUs mostly read, may allow similar optimizations as RO, but writes fault */ +#define KFD_IOCTL_SVM_FLAG_GPU_READ_MOSTLY 0x00000020 +/* Keep GPU memory mapping always valid as if XNACK is disable */ +#define KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED 0x00000040 +/* Fine grained coherency between all devices using device-scope atomics */ +#define KFD_IOCTL_SVM_FLAG_EXT_COHERENT 0x00000080 + +/** + * kfd_ioctl_svm_op - SVM ioctl operations + * + * @KFD_IOCTL_SVM_OP_SET_ATTR: Modify one or more attributes + * @KFD_IOCTL_SVM_OP_GET_ATTR: Query one or more attributes + */ +enum kfd_ioctl_svm_op { + KFD_IOCTL_SVM_OP_SET_ATTR, + KFD_IOCTL_SVM_OP_GET_ATTR +}; + +/** kfd_ioctl_svm_location - Enum for preferred and prefetch locations + * + * GPU IDs are used to specify GPUs as preferred and prefetch locations. + * Below definitions are used for system memory or for leaving the preferred + * location unspecified. + */ +enum kfd_ioctl_svm_location { + KFD_IOCTL_SVM_LOCATION_SYSMEM = 0, + KFD_IOCTL_SVM_LOCATION_UNDEFINED = 0xffffffff +}; + +/** + * kfd_ioctl_svm_attr_type - SVM attribute types + * + * @KFD_IOCTL_SVM_ATTR_PREFERRED_LOC: gpuid of the preferred location, 0 for + * system memory + * @KFD_IOCTL_SVM_ATTR_PREFETCH_LOC: gpuid of the prefetch location, 0 for + * system memory. Setting this triggers an + * immediate prefetch (migration). + * @KFD_IOCTL_SVM_ATTR_ACCESS: + * @KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE: + * @KFD_IOCTL_SVM_ATTR_NO_ACCESS: specify memory access for the gpuid given + * by the attribute value + * @KFD_IOCTL_SVM_ATTR_SET_FLAGS: bitmask of flags to set (see + * KFD_IOCTL_SVM_FLAG_...) + * @KFD_IOCTL_SVM_ATTR_CLR_FLAGS: bitmask of flags to clear + * @KFD_IOCTL_SVM_ATTR_GRANULARITY: migration granularity + * (log2 num pages) + */ +enum kfd_ioctl_svm_attr_type { + KFD_IOCTL_SVM_ATTR_PREFERRED_LOC, + KFD_IOCTL_SVM_ATTR_PREFETCH_LOC, + KFD_IOCTL_SVM_ATTR_ACCESS, + KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE, + KFD_IOCTL_SVM_ATTR_NO_ACCESS, + KFD_IOCTL_SVM_ATTR_SET_FLAGS, + KFD_IOCTL_SVM_ATTR_CLR_FLAGS, + KFD_IOCTL_SVM_ATTR_GRANULARITY +}; + +/** + * kfd_ioctl_svm_attribute - Attributes as pairs of type and value + * + * The meaning of the @value depends on the attribute type. + * + * @type: attribute type (see enum @kfd_ioctl_svm_attr_type) + * @value: attribute value + */ +struct kfd_ioctl_svm_attribute { + __u32 type; + __u32 value; +}; + +/** + * kfd_ioctl_svm_args - Arguments for SVM ioctl + * + * @op specifies the operation to perform (see enum + * @kfd_ioctl_svm_op). @start_addr and @size are common for all + * operations. + * + * A variable number of attributes can be given in @attrs. + * @nattr specifies the number of attributes. New attributes can be + * added in the future without breaking the ABI. If unknown attributes + * are given, the function returns -EINVAL. + * + * @KFD_IOCTL_SVM_OP_SET_ATTR sets attributes for a virtual address + * range. It may overlap existing virtual address ranges. If it does, + * the existing ranges will be split such that the attribute changes + * only apply to the specified address range. + * + * @KFD_IOCTL_SVM_OP_GET_ATTR returns the intersection of attributes + * over all memory in the given range and returns the result as the + * attribute value. If different pages have different preferred or + * prefetch locations, 0xffffffff will be returned for + * @KFD_IOCTL_SVM_ATTR_PREFERRED_LOC or + * @KFD_IOCTL_SVM_ATTR_PREFETCH_LOC resepctively. For + * @KFD_IOCTL_SVM_ATTR_SET_FLAGS, flags of all pages will be + * aggregated by bitwise AND. That means, a flag will be set in the + * output, if that flag is set for all pages in the range. For + * @KFD_IOCTL_SVM_ATTR_CLR_FLAGS, flags of all pages will be + * aggregated by bitwise NOR. That means, a flag will be set in the + * output, if that flag is clear for all pages in the range. + * The minimum migration granularity throughout the range will be + * returned for @KFD_IOCTL_SVM_ATTR_GRANULARITY. + * + * Querying of accessibility attributes works by initializing the + * attribute type to @KFD_IOCTL_SVM_ATTR_ACCESS and the value to the + * GPUID being queried. Multiple attributes can be given to allow + * querying multiple GPUIDs. The ioctl function overwrites the + * attribute type to indicate the access for the specified GPU. + */ +struct kfd_ioctl_svm_args { + __u64 start_addr; + __u64 size; + __u32 op; + __u32 nattr; + /* Variable length array of attributes */ + struct kfd_ioctl_svm_attribute attrs[]; +}; + +/** + * kfd_ioctl_set_xnack_mode_args - Arguments for set_xnack_mode + * + * @xnack_enabled: [in/out] Whether to enable XNACK mode for this process + * + * @xnack_enabled indicates whether recoverable page faults should be + * enabled for the current process. 0 means disabled, positive means + * enabled, negative means leave unchanged. If enabled, virtual address + * translations on GFXv9 and later AMD GPUs can return XNACK and retry + * the access until a valid PTE is available. This is used to implement + * device page faults. + * + * On output, @xnack_enabled returns the (new) current mode (0 or + * positive). Therefore, a negative input value can be used to query + * the current mode without changing it. + * + * The XNACK mode fundamentally changes the way SVM managed memory works + * in the driver, with subtle effects on application performance and + * functionality. + * + * Enabling XNACK mode requires shader programs to be compiled + * differently. Furthermore, not all GPUs support changing the mode + * per-process. Therefore changing the mode is only allowed while no + * user mode queues exist in the process. This ensure that no shader + * code is running that may be compiled for the wrong mode. And GPUs + * that cannot change to the requested mode will prevent the XNACK + * mode from occurring. All GPUs used by the process must be in the + * same XNACK mode. + * + * GFXv8 or older GPUs do not support 48 bit virtual addresses or SVM. + * Therefore those GPUs are not considered for the XNACK mode switch. + * + * Return: 0 on success, -errno on failure + */ +struct kfd_ioctl_set_xnack_mode_args { + __s32 xnack_enabled; +}; + +/* Wave launch override modes */ +enum kfd_dbg_trap_override_mode { + KFD_DBG_TRAP_OVERRIDE_OR = 0, + KFD_DBG_TRAP_OVERRIDE_REPLACE = 1 +}; + +/* Wave launch overrides */ +enum kfd_dbg_trap_mask { + KFD_DBG_TRAP_MASK_FP_INVALID = 1, + KFD_DBG_TRAP_MASK_FP_INPUT_DENORMAL = 2, + KFD_DBG_TRAP_MASK_FP_DIVIDE_BY_ZERO = 4, + KFD_DBG_TRAP_MASK_FP_OVERFLOW = 8, + KFD_DBG_TRAP_MASK_FP_UNDERFLOW = 16, + KFD_DBG_TRAP_MASK_FP_INEXACT = 32, + KFD_DBG_TRAP_MASK_INT_DIVIDE_BY_ZERO = 64, + KFD_DBG_TRAP_MASK_DBG_ADDRESS_WATCH = 128, + KFD_DBG_TRAP_MASK_DBG_MEMORY_VIOLATION = 256, + KFD_DBG_TRAP_MASK_TRAP_ON_WAVE_START = (1 << 30), + KFD_DBG_TRAP_MASK_TRAP_ON_WAVE_END = (1 << 31) +}; + +/* Wave launch modes */ +enum kfd_dbg_trap_wave_launch_mode { + KFD_DBG_TRAP_WAVE_LAUNCH_MODE_NORMAL = 0, + KFD_DBG_TRAP_WAVE_LAUNCH_MODE_HALT = 1, + KFD_DBG_TRAP_WAVE_LAUNCH_MODE_DEBUG = 3 +}; + +/* Address watch modes */ +enum kfd_dbg_trap_address_watch_mode { + KFD_DBG_TRAP_ADDRESS_WATCH_MODE_READ = 0, + KFD_DBG_TRAP_ADDRESS_WATCH_MODE_NONREAD = 1, + KFD_DBG_TRAP_ADDRESS_WATCH_MODE_ATOMIC = 2, + KFD_DBG_TRAP_ADDRESS_WATCH_MODE_ALL = 3 +}; + +/* Additional wave settings */ +enum kfd_dbg_trap_flags { + KFD_DBG_TRAP_FLAG_SINGLE_MEM_OP = 1, +}; + +/* Trap exceptions */ +enum kfd_dbg_trap_exception_code { + EC_NONE = 0, + /* per queue */ + EC_QUEUE_WAVE_ABORT = 1, + EC_QUEUE_WAVE_TRAP = 2, + EC_QUEUE_WAVE_MATH_ERROR = 3, + EC_QUEUE_WAVE_ILLEGAL_INSTRUCTION = 4, + EC_QUEUE_WAVE_MEMORY_VIOLATION = 5, + EC_QUEUE_WAVE_APERTURE_VIOLATION = 6, + EC_QUEUE_PACKET_DISPATCH_DIM_INVALID = 16, + EC_QUEUE_PACKET_DISPATCH_GROUP_SEGMENT_SIZE_INVALID = 17, + EC_QUEUE_PACKET_DISPATCH_CODE_INVALID = 18, + EC_QUEUE_PACKET_RESERVED = 19, + EC_QUEUE_PACKET_UNSUPPORTED = 20, + EC_QUEUE_PACKET_DISPATCH_WORK_GROUP_SIZE_INVALID = 21, + EC_QUEUE_PACKET_DISPATCH_REGISTER_INVALID = 22, + EC_QUEUE_PACKET_VENDOR_UNSUPPORTED = 23, + EC_QUEUE_PREEMPTION_ERROR = 30, + EC_QUEUE_NEW = 31, + /* per device */ + EC_DEVICE_QUEUE_DELETE = 32, + EC_DEVICE_MEMORY_VIOLATION = 33, + EC_DEVICE_RAS_ERROR = 34, + EC_DEVICE_FATAL_HALT = 35, + EC_DEVICE_NEW = 36, + /* per process */ + EC_PROCESS_RUNTIME = 48, + EC_PROCESS_DEVICE_REMOVE = 49, + EC_MAX +}; + +/* Mask generated by ecode in kfd_dbg_trap_exception_code */ +#define KFD_EC_MASK(ecode) (1ULL << (ecode - 1)) + +/* Masks for exception code type checks below */ +#define KFD_EC_MASK_QUEUE (KFD_EC_MASK(EC_QUEUE_WAVE_ABORT) | \ + KFD_EC_MASK(EC_QUEUE_WAVE_TRAP) | \ + KFD_EC_MASK(EC_QUEUE_WAVE_MATH_ERROR) | \ + KFD_EC_MASK(EC_QUEUE_WAVE_ILLEGAL_INSTRUCTION) | \ + KFD_EC_MASK(EC_QUEUE_WAVE_MEMORY_VIOLATION) | \ + KFD_EC_MASK(EC_QUEUE_WAVE_APERTURE_VIOLATION) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_DIM_INVALID) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_GROUP_SEGMENT_SIZE_INVALID) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_CODE_INVALID) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_RESERVED) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_UNSUPPORTED) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_WORK_GROUP_SIZE_INVALID) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_REGISTER_INVALID) | \ + KFD_EC_MASK(EC_QUEUE_PACKET_VENDOR_UNSUPPORTED) | \ + KFD_EC_MASK(EC_QUEUE_PREEMPTION_ERROR) | \ + KFD_EC_MASK(EC_QUEUE_NEW)) +#define KFD_EC_MASK_DEVICE (KFD_EC_MASK(EC_DEVICE_QUEUE_DELETE) | \ + KFD_EC_MASK(EC_DEVICE_RAS_ERROR) | \ + KFD_EC_MASK(EC_DEVICE_FATAL_HALT) | \ + KFD_EC_MASK(EC_DEVICE_MEMORY_VIOLATION) | \ + KFD_EC_MASK(EC_DEVICE_NEW)) +#define KFD_EC_MASK_PROCESS (KFD_EC_MASK(EC_PROCESS_RUNTIME) | \ + KFD_EC_MASK(EC_PROCESS_DEVICE_REMOVE)) + +/* Checks for exception code types for KFD search */ +#define KFD_DBG_EC_TYPE_IS_QUEUE(ecode) \ + (!!(KFD_EC_MASK(ecode) & KFD_EC_MASK_QUEUE)) +#define KFD_DBG_EC_TYPE_IS_DEVICE(ecode) \ + (!!(KFD_EC_MASK(ecode) & KFD_EC_MASK_DEVICE)) +#define KFD_DBG_EC_TYPE_IS_PROCESS(ecode) \ + (!!(KFD_EC_MASK(ecode) & KFD_EC_MASK_PROCESS)) + + +/* Runtime enable states */ +enum kfd_dbg_runtime_state { + DEBUG_RUNTIME_STATE_DISABLED = 0, + DEBUG_RUNTIME_STATE_ENABLED = 1, + DEBUG_RUNTIME_STATE_ENABLED_BUSY = 2, + DEBUG_RUNTIME_STATE_ENABLED_ERROR = 3 +}; + +/* Runtime enable status */ +struct kfd_runtime_info { + __u64 r_debug; + __u32 runtime_state; + __u32 ttmp_setup; +}; + +/* Enable modes for runtime enable */ +#define KFD_RUNTIME_ENABLE_MODE_ENABLE_MASK 1 +#define KFD_RUNTIME_ENABLE_MODE_TTMP_SAVE_MASK 2 + +/** + * kfd_ioctl_runtime_enable_args - Arguments for runtime enable + * + * Coordinates debug exception signalling and debug device enablement with runtime. + * + * @r_debug - pointer to user struct for sharing information between ROCr and the debuggger + * @mode_mask - mask to set mode + * KFD_RUNTIME_ENABLE_MODE_ENABLE_MASK - enable runtime for debugging, otherwise disable + * KFD_RUNTIME_ENABLE_MODE_TTMP_SAVE_MASK - enable trap temporary setup (ignore on disable) + * @capabilities_mask - mask to notify runtime on what KFD supports + * + * Return - 0 on SUCCESS. + * - EBUSY if runtime enable call already pending. + * - EEXIST if user queues already active prior to call. + * If process is debug enabled, runtime enable will enable debug devices and + * wait for debugger process to send runtime exception EC_PROCESS_RUNTIME + * to unblock - see kfd_ioctl_dbg_trap_args. + * + */ +struct kfd_ioctl_runtime_enable_args { + __u64 r_debug; + __u32 mode_mask; + __u32 capabilities_mask; +}; + +/* Queue information */ +struct kfd_queue_snapshot_entry { + __u64 exception_status; + __u64 ring_base_address; + __u64 write_pointer_address; + __u64 read_pointer_address; + __u64 ctx_save_restore_address; + __u32 queue_id; + __u32 gpu_id; + __u32 ring_size; + __u32 queue_type; + __u32 ctx_save_restore_area_size; + __u32 reserved; +}; + +/* Queue status return for suspend/resume */ +#define KFD_DBG_QUEUE_ERROR_BIT 30 +#define KFD_DBG_QUEUE_INVALID_BIT 31 +#define KFD_DBG_QUEUE_ERROR_MASK (1 << KFD_DBG_QUEUE_ERROR_BIT) +#define KFD_DBG_QUEUE_INVALID_MASK (1 << KFD_DBG_QUEUE_INVALID_BIT) + +/* Context save area header information */ +struct kfd_context_save_area_header { + struct { + __u32 control_stack_offset; + __u32 control_stack_size; + __u32 wave_state_offset; + __u32 wave_state_size; + } wave_state; + __u32 debug_offset; + __u32 debug_size; + __u64 err_payload_addr; + __u32 err_event_id; + __u32 reserved1; +}; + +/* + * Debug operations + * + * For specifics on usage and return values, see documentation per operation + * below. Otherwise, generic error returns apply: + * - ESRCH if the process to debug does not exist. + * + * - EINVAL (with KFD_IOC_DBG_TRAP_ENABLE exempt) if operation + * KFD_IOC_DBG_TRAP_ENABLE has not succeeded prior. + * Also returns this error if GPU hardware scheduling is not supported. + * + * - EPERM (with KFD_IOC_DBG_TRAP_DISABLE exempt) if target process is not + * PTRACE_ATTACHED. KFD_IOC_DBG_TRAP_DISABLE is exempt to allow + * clean up of debug mode as long as process is debug enabled. + * + * - EACCES if any DBG_HW_OP (debug hardware operation) is requested when + * AMDKFD_IOC_RUNTIME_ENABLE has not succeeded prior. + * + * - ENODEV if any GPU does not support debugging on a DBG_HW_OP call. + * + * - Other errors may be returned when a DBG_HW_OP occurs while the GPU + * is in a fatal state. + * + */ +enum kfd_dbg_trap_operations { + KFD_IOC_DBG_TRAP_ENABLE = 0, + KFD_IOC_DBG_TRAP_DISABLE = 1, + KFD_IOC_DBG_TRAP_SEND_RUNTIME_EVENT = 2, + KFD_IOC_DBG_TRAP_SET_EXCEPTIONS_ENABLED = 3, + KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE = 4, /* DBG_HW_OP */ + KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE = 5, /* DBG_HW_OP */ + KFD_IOC_DBG_TRAP_SUSPEND_QUEUES = 6, /* DBG_HW_OP */ + KFD_IOC_DBG_TRAP_RESUME_QUEUES = 7, /* DBG_HW_OP */ + KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH = 8, /* DBG_HW_OP */ + KFD_IOC_DBG_TRAP_CLEAR_NODE_ADDRESS_WATCH = 9, /* DBG_HW_OP */ + KFD_IOC_DBG_TRAP_SET_FLAGS = 10, + KFD_IOC_DBG_TRAP_QUERY_DEBUG_EVENT = 11, + KFD_IOC_DBG_TRAP_QUERY_EXCEPTION_INFO = 12, + KFD_IOC_DBG_TRAP_GET_QUEUE_SNAPSHOT = 13, + KFD_IOC_DBG_TRAP_GET_DEVICE_SNAPSHOT = 14 +}; + +/** + * kfd_ioctl_dbg_trap_enable_args + * + * Arguments for KFD_IOC_DBG_TRAP_ENABLE. + * + * Enables debug session for target process. Call @op KFD_IOC_DBG_TRAP_DISABLE in + * kfd_ioctl_dbg_trap_args to disable debug session. + * + * @exception_mask (IN) - exceptions to raise to the debugger + * @rinfo_ptr (IN) - pointer to runtime info buffer (see kfd_runtime_info) + * @rinfo_size (IN/OUT) - size of runtime info buffer in bytes + * @dbg_fd (IN) - fd the KFD will nofify the debugger with of raised + * exceptions set in exception_mask. + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * Copies KFD saved kfd_runtime_info to @rinfo_ptr on enable. + * Size of kfd_runtime saved by the KFD returned to @rinfo_size. + * - EBADF if KFD cannot get a reference to dbg_fd. + * - EFAULT if KFD cannot copy runtime info to rinfo_ptr. + * - EINVAL if target process is already debug enabled. + * + */ +struct kfd_ioctl_dbg_trap_enable_args { + __u64 exception_mask; + __u64 rinfo_ptr; + __u32 rinfo_size; + __u32 dbg_fd; +}; + +/** + * kfd_ioctl_dbg_trap_send_runtime_event_args + * + * + * Arguments for KFD_IOC_DBG_TRAP_SEND_RUNTIME_EVENT. + * Raises exceptions to runtime. + * + * @exception_mask (IN) - exceptions to raise to runtime + * @gpu_id (IN) - target device id + * @queue_id (IN) - target queue id + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * - ENODEV if gpu_id not found. + * If exception_mask contains EC_PROCESS_RUNTIME, unblocks pending + * AMDKFD_IOC_RUNTIME_ENABLE call - see kfd_ioctl_runtime_enable_args. + * All other exceptions are raised to runtime through err_payload_addr. + * See kfd_context_save_area_header. + */ +struct kfd_ioctl_dbg_trap_send_runtime_event_args { + __u64 exception_mask; + __u32 gpu_id; + __u32 queue_id; +}; + +/** + * kfd_ioctl_dbg_trap_set_exceptions_enabled_args + * + * Arguments for KFD_IOC_SET_EXCEPTIONS_ENABLED + * Set new exceptions to be raised to the debugger. + * + * @exception_mask (IN) - new exceptions to raise the debugger + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + */ +struct kfd_ioctl_dbg_trap_set_exceptions_enabled_args { + __u64 exception_mask; +}; + +/** + * kfd_ioctl_dbg_trap_set_wave_launch_override_args + * + * Arguments for KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE + * Enable HW exceptions to raise trap. + * + * @override_mode (IN) - see kfd_dbg_trap_override_mode + * @enable_mask (IN/OUT) - reference kfd_dbg_trap_mask. + * IN is the override modes requested to be enabled. + * OUT is referenced in Return below. + * @support_request_mask (IN/OUT) - reference kfd_dbg_trap_mask. + * IN is the override modes requested for support check. + * OUT is referenced in Return below. + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * Previous enablement is returned in @enable_mask. + * Actual override support is returned in @support_request_mask. + * - EINVAL if override mode is not supported. + * - EACCES if trap support requested is not actually supported. + * i.e. enable_mask (IN) is not a subset of support_request_mask (OUT). + * Otherwise it is considered a generic error (see kfd_dbg_trap_operations). + */ +struct kfd_ioctl_dbg_trap_set_wave_launch_override_args { + __u32 override_mode; + __u32 enable_mask; + __u32 support_request_mask; + __u32 pad; +}; + +/** + * kfd_ioctl_dbg_trap_set_wave_launch_mode_args + * + * Arguments for KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE + * Set wave launch mode. + * + * @mode (IN) - see kfd_dbg_trap_wave_launch_mode + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + */ +struct kfd_ioctl_dbg_trap_set_wave_launch_mode_args { + __u32 launch_mode; + __u32 pad; +}; + +/** + * kfd_ioctl_dbg_trap_suspend_queues_ags + * + * Arguments for KFD_IOC_DBG_TRAP_SUSPEND_QUEUES + * Suspend queues. + * + * @exception_mask (IN) - raised exceptions to clear + * @queue_array_ptr (IN) - pointer to array of queue ids (u32 per queue id) + * to suspend + * @num_queues (IN) - number of queues to suspend in @queue_array_ptr + * @grace_period (IN) - wave time allowance before preemption + * per 1K GPU clock cycle unit + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Destruction of a suspended queue is blocked until the queue is + * resumed. This allows the debugger to access queue information and + * the its context save area without running into a race condition on + * queue destruction. + * Automatically copies per queue context save area header information + * into the save area base + * (see kfd_queue_snapshot_entry and kfd_context_save_area_header). + * + * Return - Number of queues suspended on SUCCESS. + * . KFD_DBG_QUEUE_ERROR_MASK and KFD_DBG_QUEUE_INVALID_MASK masked + * for each queue id in @queue_array_ptr array reports unsuccessful + * suspend reason. + * KFD_DBG_QUEUE_ERROR_MASK = HW failure. + * KFD_DBG_QUEUE_INVALID_MASK = queue does not exist, is new or + * is being destroyed. + */ +struct kfd_ioctl_dbg_trap_suspend_queues_args { + __u64 exception_mask; + __u64 queue_array_ptr; + __u32 num_queues; + __u32 grace_period; +}; + +/** + * kfd_ioctl_dbg_trap_resume_queues_args + * + * Arguments for KFD_IOC_DBG_TRAP_RESUME_QUEUES + * Resume queues. + * + * @queue_array_ptr (IN) - pointer to array of queue ids (u32 per queue id) + * to resume + * @num_queues (IN) - number of queues to resume in @queue_array_ptr + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - Number of queues resumed on SUCCESS. + * KFD_DBG_QUEUE_ERROR_MASK and KFD_DBG_QUEUE_INVALID_MASK mask + * for each queue id in @queue_array_ptr array reports unsuccessful + * resume reason. + * KFD_DBG_QUEUE_ERROR_MASK = HW failure. + * KFD_DBG_QUEUE_INVALID_MASK = queue does not exist. + */ +struct kfd_ioctl_dbg_trap_resume_queues_args { + __u64 queue_array_ptr; + __u32 num_queues; + __u32 pad; +}; + +/** + * kfd_ioctl_dbg_trap_set_node_address_watch_args + * + * Arguments for KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH + * Sets address watch for device. + * + * @address (IN) - watch address to set + * @mode (IN) - see kfd_dbg_trap_address_watch_mode + * @mask (IN) - watch address mask + * @gpu_id (IN) - target gpu to set watch point + * @id (OUT) - watch id allocated + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * Allocated watch ID returned to @id. + * - ENODEV if gpu_id not found. + * - ENOMEM if watch IDs can be allocated + */ +struct kfd_ioctl_dbg_trap_set_node_address_watch_args { + __u64 address; + __u32 mode; + __u32 mask; + __u32 gpu_id; + __u32 id; +}; + +/** + * kfd_ioctl_dbg_trap_clear_node_address_watch_args + * + * Arguments for KFD_IOC_DBG_TRAP_CLEAR_NODE_ADDRESS_WATCH + * Clear address watch for device. + * + * @gpu_id (IN) - target device to clear watch point + * @id (IN) - allocated watch id to clear + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * - ENODEV if gpu_id not found. + * - EINVAL if watch ID has not been allocated. + */ +struct kfd_ioctl_dbg_trap_clear_node_address_watch_args { + __u32 gpu_id; + __u32 id; +}; + +/** + * kfd_ioctl_dbg_trap_set_flags_args + * + * Arguments for KFD_IOC_DBG_TRAP_SET_FLAGS + * Sets flags for wave behaviour. + * + * @flags (IN/OUT) - IN = flags to enable, OUT = flags previously enabled + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * - EACCESS if any debug device does not allow flag options. + */ +struct kfd_ioctl_dbg_trap_set_flags_args { + __u32 flags; + __u32 pad; +}; + +/** + * kfd_ioctl_dbg_trap_query_debug_event_args + * + * Arguments for KFD_IOC_DBG_TRAP_QUERY_DEBUG_EVENT + * + * Find one or more raised exceptions. This function can return multiple + * exceptions from a single queue or a single device with one call. To find + * all raised exceptions, this function must be called repeatedly until it + * returns -EAGAIN. Returned exceptions can optionally be cleared by + * setting the corresponding bit in the @exception_mask input parameter. + * However, clearing an exception prevents retrieving further information + * about it with KFD_IOC_DBG_TRAP_QUERY_EXCEPTION_INFO. + * + * @exception_mask (IN/OUT) - exception to clear (IN) and raised (OUT) + * @gpu_id (OUT) - gpu id of exceptions raised + * @queue_id (OUT) - queue id of exceptions raised + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on raised exception found + * Raised exceptions found are returned in @exception mask + * with reported source id returned in @gpu_id or @queue_id. + * - EAGAIN if no raised exception has been found + */ +struct kfd_ioctl_dbg_trap_query_debug_event_args { + __u64 exception_mask; + __u32 gpu_id; + __u32 queue_id; +}; + +/** + * kfd_ioctl_dbg_trap_query_exception_info_args + * + * Arguments KFD_IOC_DBG_TRAP_QUERY_EXCEPTION_INFO + * Get additional info on raised exception. + * + * @info_ptr (IN) - pointer to exception info buffer to copy to + * @info_size (IN/OUT) - exception info buffer size (bytes) + * @source_id (IN) - target gpu or queue id + * @exception_code (IN) - target exception + * @clear_exception (IN) - clear raised @exception_code exception + * (0 = false, 1 = true) + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * If @exception_code is EC_DEVICE_MEMORY_VIOLATION, copy @info_size(OUT) + * bytes of memory exception data to @info_ptr. + * If @exception_code is EC_PROCESS_RUNTIME, copy saved + * kfd_runtime_info to @info_ptr. + * Actual required @info_ptr size (bytes) is returned in @info_size. + */ +struct kfd_ioctl_dbg_trap_query_exception_info_args { + __u64 info_ptr; + __u32 info_size; + __u32 source_id; + __u32 exception_code; + __u32 clear_exception; +}; + +/** + * kfd_ioctl_dbg_trap_get_queue_snapshot_args + * + * Arguments KFD_IOC_DBG_TRAP_GET_QUEUE_SNAPSHOT + * Get queue information. + * + * @exception_mask (IN) - exceptions raised to clear + * @snapshot_buf_ptr (IN) - queue snapshot entry buffer (see kfd_queue_snapshot_entry) + * @num_queues (IN/OUT) - number of queue snapshot entries + * The debugger specifies the size of the array allocated in @num_queues. + * KFD returns the number of queues that actually existed. If this is + * larger than the size specified by the debugger, KFD will not overflow + * the array allocated by the debugger. + * + * @entry_size (IN/OUT) - size per entry in bytes + * The debugger specifies sizeof(struct kfd_queue_snapshot_entry) in + * @entry_size. KFD returns the number of bytes actually populated per + * entry. The debugger should use the KFD_IOCTL_MINOR_VERSION to determine, + * which fields in struct kfd_queue_snapshot_entry are valid. This allows + * growing the ABI in a backwards compatible manner. + * Note that entry_size(IN) should still be used to stride the snapshot buffer in the + * event that it's larger than actual kfd_queue_snapshot_entry. + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * Copies @num_queues(IN) queue snapshot entries of size @entry_size(IN) + * into @snapshot_buf_ptr if @num_queues(IN) > 0. + * Otherwise return @num_queues(OUT) queue snapshot entries that exist. + */ +struct kfd_ioctl_dbg_trap_queue_snapshot_args { + __u64 exception_mask; + __u64 snapshot_buf_ptr; + __u32 num_queues; + __u32 entry_size; +}; + +/** + * kfd_ioctl_dbg_trap_get_device_snapshot_args + * + * Arguments for KFD_IOC_DBG_TRAP_GET_DEVICE_SNAPSHOT + * Get device information. + * + * @exception_mask (IN) - exceptions raised to clear + * @snapshot_buf_ptr (IN) - pointer to snapshot buffer (see kfd_dbg_device_info_entry) + * @num_devices (IN/OUT) - number of debug devices to snapshot + * The debugger specifies the size of the array allocated in @num_devices. + * KFD returns the number of devices that actually existed. If this is + * larger than the size specified by the debugger, KFD will not overflow + * the array allocated by the debugger. + * + * @entry_size (IN/OUT) - size per entry in bytes + * The debugger specifies sizeof(struct kfd_dbg_device_info_entry) in + * @entry_size. KFD returns the number of bytes actually populated. The + * debugger should use KFD_IOCTL_MINOR_VERSION to determine, which fields + * in struct kfd_dbg_device_info_entry are valid. This allows growing the + * ABI in a backwards compatible manner. + * Note that entry_size(IN) should still be used to stride the snapshot buffer in the + * event that it's larger than actual kfd_dbg_device_info_entry. + * + * Generic errors apply (see kfd_dbg_trap_operations). + * Return - 0 on SUCCESS. + * Copies @num_devices(IN) device snapshot entries of size @entry_size(IN) + * into @snapshot_buf_ptr if @num_devices(IN) > 0. + * Otherwise return @num_devices(OUT) queue snapshot entries that exist. + */ +struct kfd_ioctl_dbg_trap_device_snapshot_args { + __u64 exception_mask; + __u64 snapshot_buf_ptr; + __u32 num_devices; + __u32 entry_size; +}; + +/** + * kfd_ioctl_dbg_trap_args + * + * Arguments to debug target process. + * + * @pid - target process to debug + * @op - debug operation (see kfd_dbg_trap_operations) + * + * @op determines which union struct args to use. + * Refer to kern docs for each kfd_ioctl_dbg_trap_*_args struct. + */ +struct kfd_ioctl_dbg_trap_args { + __u32 pid; + __u32 op; + + union { + struct kfd_ioctl_dbg_trap_enable_args enable; + struct kfd_ioctl_dbg_trap_send_runtime_event_args send_runtime_event; + struct kfd_ioctl_dbg_trap_set_exceptions_enabled_args set_exceptions_enabled; + struct kfd_ioctl_dbg_trap_set_wave_launch_override_args launch_override; + struct kfd_ioctl_dbg_trap_set_wave_launch_mode_args launch_mode; + struct kfd_ioctl_dbg_trap_suspend_queues_args suspend_queues; + struct kfd_ioctl_dbg_trap_resume_queues_args resume_queues; + struct kfd_ioctl_dbg_trap_set_node_address_watch_args set_node_address_watch; + struct kfd_ioctl_dbg_trap_clear_node_address_watch_args clear_node_address_watch; + struct kfd_ioctl_dbg_trap_set_flags_args set_flags; + struct kfd_ioctl_dbg_trap_query_debug_event_args query_debug_event; + struct kfd_ioctl_dbg_trap_query_exception_info_args query_exception_info; + struct kfd_ioctl_dbg_trap_queue_snapshot_args queue_snapshot; + struct kfd_ioctl_dbg_trap_device_snapshot_args device_snapshot; + }; +}; + +#define AMDKFD_IOCTL_BASE 'K' +#define AMDKFD_IO(nr) _IO(AMDKFD_IOCTL_BASE, nr) +#define AMDKFD_IOR(nr, type) _IOR(AMDKFD_IOCTL_BASE, nr, type) +#define AMDKFD_IOW(nr, type) _IOW(AMDKFD_IOCTL_BASE, nr, type) +#define AMDKFD_IOWR(nr, type) _IOWR(AMDKFD_IOCTL_BASE, nr, type) + +#define AMDKFD_IOC_GET_VERSION \ + AMDKFD_IOR(0x01, struct kfd_ioctl_get_version_args) + +#define AMDKFD_IOC_CREATE_QUEUE \ + AMDKFD_IOWR(0x02, struct kfd_ioctl_create_queue_args) + +#define AMDKFD_IOC_DESTROY_QUEUE \ + AMDKFD_IOWR(0x03, struct kfd_ioctl_destroy_queue_args) + +#define AMDKFD_IOC_SET_MEMORY_POLICY \ + AMDKFD_IOW(0x04, struct kfd_ioctl_set_memory_policy_args) + +#define AMDKFD_IOC_GET_CLOCK_COUNTERS \ + AMDKFD_IOWR(0x05, struct kfd_ioctl_get_clock_counters_args) + +#define AMDKFD_IOC_GET_PROCESS_APERTURES \ + AMDKFD_IOR(0x06, struct kfd_ioctl_get_process_apertures_args) + +#define AMDKFD_IOC_UPDATE_QUEUE \ + AMDKFD_IOW(0x07, struct kfd_ioctl_update_queue_args) + +#define AMDKFD_IOC_CREATE_EVENT \ + AMDKFD_IOWR(0x08, struct kfd_ioctl_create_event_args) + +#define AMDKFD_IOC_DESTROY_EVENT \ + AMDKFD_IOW(0x09, struct kfd_ioctl_destroy_event_args) + +#define AMDKFD_IOC_SET_EVENT \ + AMDKFD_IOW(0x0A, struct kfd_ioctl_set_event_args) + +#define AMDKFD_IOC_RESET_EVENT \ + AMDKFD_IOW(0x0B, struct kfd_ioctl_reset_event_args) + +#define AMDKFD_IOC_WAIT_EVENTS \ + AMDKFD_IOWR(0x0C, struct kfd_ioctl_wait_events_args) + +#define AMDKFD_IOC_DBG_REGISTER_DEPRECATED \ + AMDKFD_IOW(0x0D, struct kfd_ioctl_dbg_register_args) + +#define AMDKFD_IOC_DBG_UNREGISTER_DEPRECATED \ + AMDKFD_IOW(0x0E, struct kfd_ioctl_dbg_unregister_args) + +#define AMDKFD_IOC_DBG_ADDRESS_WATCH_DEPRECATED \ + AMDKFD_IOW(0x0F, struct kfd_ioctl_dbg_address_watch_args) + +#define AMDKFD_IOC_DBG_WAVE_CONTROL_DEPRECATED \ + AMDKFD_IOW(0x10, struct kfd_ioctl_dbg_wave_control_args) + +#define AMDKFD_IOC_SET_SCRATCH_BACKING_VA \ + AMDKFD_IOWR(0x11, struct kfd_ioctl_set_scratch_backing_va_args) + +#define AMDKFD_IOC_GET_TILE_CONFIG \ + AMDKFD_IOWR(0x12, struct kfd_ioctl_get_tile_config_args) + +#define AMDKFD_IOC_SET_TRAP_HANDLER \ + AMDKFD_IOW(0x13, struct kfd_ioctl_set_trap_handler_args) + +#define AMDKFD_IOC_GET_PROCESS_APERTURES_NEW \ + AMDKFD_IOWR(0x14, \ + struct kfd_ioctl_get_process_apertures_new_args) + +#define AMDKFD_IOC_ACQUIRE_VM \ + AMDKFD_IOW(0x15, struct kfd_ioctl_acquire_vm_args) + +#define AMDKFD_IOC_ALLOC_MEMORY_OF_GPU \ + AMDKFD_IOWR(0x16, struct kfd_ioctl_alloc_memory_of_gpu_args) + +#define AMDKFD_IOC_FREE_MEMORY_OF_GPU \ + AMDKFD_IOW(0x17, struct kfd_ioctl_free_memory_of_gpu_args) + +#define AMDKFD_IOC_MAP_MEMORY_TO_GPU \ + AMDKFD_IOWR(0x18, struct kfd_ioctl_map_memory_to_gpu_args) + +#define AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU \ + AMDKFD_IOWR(0x19, struct kfd_ioctl_unmap_memory_from_gpu_args) + +#define AMDKFD_IOC_SET_CU_MASK \ + AMDKFD_IOW(0x1A, struct kfd_ioctl_set_cu_mask_args) + +#define AMDKFD_IOC_GET_QUEUE_WAVE_STATE \ + AMDKFD_IOWR(0x1B, struct kfd_ioctl_get_queue_wave_state_args) + +#define AMDKFD_IOC_GET_DMABUF_INFO \ + AMDKFD_IOWR(0x1C, struct kfd_ioctl_get_dmabuf_info_args) + +#define AMDKFD_IOC_IMPORT_DMABUF \ + AMDKFD_IOWR(0x1D, struct kfd_ioctl_import_dmabuf_args) + +#define AMDKFD_IOC_ALLOC_QUEUE_GWS \ + AMDKFD_IOWR(0x1E, struct kfd_ioctl_alloc_queue_gws_args) + +#define AMDKFD_IOC_SMI_EVENTS \ + AMDKFD_IOWR(0x1F, struct kfd_ioctl_smi_events_args) + +#define AMDKFD_IOC_SVM AMDKFD_IOWR(0x20, struct kfd_ioctl_svm_args) + +#define AMDKFD_IOC_SET_XNACK_MODE \ + AMDKFD_IOWR(0x21, struct kfd_ioctl_set_xnack_mode_args) + +#define AMDKFD_IOC_CRIU_OP \ + AMDKFD_IOWR(0x22, struct kfd_ioctl_criu_args) + +#define AMDKFD_IOC_AVAILABLE_MEMORY \ + AMDKFD_IOWR(0x23, struct kfd_ioctl_get_available_memory_args) + +#define AMDKFD_IOC_EXPORT_DMABUF \ + AMDKFD_IOWR(0x24, struct kfd_ioctl_export_dmabuf_args) + +#define AMDKFD_IOC_RUNTIME_ENABLE \ + AMDKFD_IOWR(0x25, struct kfd_ioctl_runtime_enable_args) + +#define AMDKFD_IOC_DBG_TRAP \ + AMDKFD_IOWR(0x26, struct kfd_ioctl_dbg_trap_args) + +#define AMDKFD_COMMAND_START 0x01 +#define AMDKFD_COMMAND_END 0x27 + +/* non-upstream ioctls */ +#define AMDKFD_IOC_IPC_IMPORT_HANDLE \ + AMDKFD_IOWR(0x80, struct kfd_ioctl_ipc_import_handle_args) + +#define AMDKFD_IOC_IPC_EXPORT_HANDLE \ + AMDKFD_IOWR(0x81, struct kfd_ioctl_ipc_export_handle_args) + +#define AMDKFD_IOC_DBG_TRAP_DEPRECATED \ + AMDKFD_IOWR(0x82, struct kfd_ioctl_dbg_trap_args_deprecated) + +#define AMDKFD_IOC_CROSS_MEMORY_COPY_DEPRECATED \ + AMDKFD_IOWR(0x83, struct kfd_ioctl_cross_memory_copy_deprecated_args) + +#define AMDKFD_IOC_RLC_SPM \ + AMDKFD_IOWR(0x84, struct kfd_ioctl_spm_args) + +#define AMDKFD_COMMAND_START_2 0x80 +#define AMDKFD_COMMAND_END_2 0x85 + +#endif +// clang-format on diff --git a/source/lib/rocprofiler-sdk/page_migration/page_migration.cpp b/source/lib/rocprofiler-sdk/page_migration/page_migration.cpp new file mode 100644 index 0000000000..73b5590eb7 --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/page_migration.cpp @@ -0,0 +1,1143 @@ +// 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 "lib/rocprofiler-sdk/page_migration/page_migration.hpp" +#include "lib/common/mpl.hpp" +#include "lib/common/static_object.hpp" +#include "lib/common/utility.hpp" +#include "lib/rocprofiler-sdk/agent.hpp" +#include "lib/rocprofiler-sdk/buffer.hpp" +#include "lib/rocprofiler-sdk/context/context.hpp" +#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 +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#define ROCPROFILER_LIB_ROCPROFILER_SDK_PAGE_MIGRATION_PAGE_MIGRATION_CPP_IMPL 1 +#include "page_migration.def.cpp" +#undef ROCPROFILER_LIB_ROCPROFILER_SDK_PAGE_MIGRATION_PAGE_MIGRATION_CPP_IMPL + +namespace rocprofiler +{ +namespace page_migration +{ +template +using small_vector = common::container::small_vector; + +using context_t = context::context; +using context_array_t = common::container::small_vector; +using kfd_event_id_t = decltype(KFD_SMI_EVENT_NONE); +using page_migration_record_t = rocprofiler_buffer_tracing_page_migration_record_t; +using migrate_trigger_t = rocprofiler_page_migration_trigger_t; +using qsuspend_trigger_t = rocprofiler_page_migration_queue_suspend_trigger_t; +using unmap_trigger_t = rocprofiler_page_migration_unmap_from_gpu_trigger_t; + +// Parsing and utilities +namespace +{ +using namespace page_migration; + +constexpr auto +page_to_bytes(size_t val) +{ + // each page is 4KB = 4096 bytes + return val << 12; +} + +template +struct page_migration_enum_info; + +template +struct page_migration_bounds; + +#define SPECIALIZE_PM_ENUM_INFO(TYPE, TRIGGER_CATEGORY, NAME) \ + template <> \ + struct page_migration_enum_info \ + { \ + static constexpr auto name = #NAME; \ + }; + +#define SPECIALIZE_PM_ENUM_BOUNDS(TYPE, TRIGGER_CATEGORY) \ + template <> \ + struct page_migration_bounds \ + { \ + static constexpr auto last = ROCPROFILER_PAGE_MIGRATION_##TRIGGER_CATEGORY##_LAST; \ + }; + +using queue_suspend_trigger_t = rocprofiler_page_migration_queue_suspend_trigger_t; +using unmap_from_gpu_trigger_t = rocprofiler_page_migration_unmap_from_gpu_trigger_t; + +SPECIALIZE_PM_ENUM_BOUNDS(rocprofiler_page_migration_trigger_t, TRIGGER) +SPECIALIZE_PM_ENUM_BOUNDS(queue_suspend_trigger_t, QUEUE_SUSPEND_TRIGGER) +SPECIALIZE_PM_ENUM_BOUNDS(unmap_from_gpu_trigger_t, UNMAP_FROM_GPU_TRIGGER) + +SPECIALIZE_PM_ENUM_INFO(rocprofiler_page_migration_trigger_t, TRIGGER, PREFETCH) +SPECIALIZE_PM_ENUM_INFO(rocprofiler_page_migration_trigger_t, TRIGGER, PAGEFAULT_GPU) +SPECIALIZE_PM_ENUM_INFO(rocprofiler_page_migration_trigger_t, TRIGGER, PAGEFAULT_CPU) +SPECIALIZE_PM_ENUM_INFO(rocprofiler_page_migration_trigger_t, TRIGGER, TTM_EVICTION) + +SPECIALIZE_PM_ENUM_INFO(queue_suspend_trigger_t, QUEUE_SUSPEND_TRIGGER, SVM) +SPECIALIZE_PM_ENUM_INFO(queue_suspend_trigger_t, QUEUE_SUSPEND_TRIGGER, USERPTR) +SPECIALIZE_PM_ENUM_INFO(queue_suspend_trigger_t, QUEUE_SUSPEND_TRIGGER, TTM) +SPECIALIZE_PM_ENUM_INFO(queue_suspend_trigger_t, QUEUE_SUSPEND_TRIGGER, SUSPEND) +SPECIALIZE_PM_ENUM_INFO(queue_suspend_trigger_t, QUEUE_SUSPEND_TRIGGER, CRIU_CHECKPOINT) +SPECIALIZE_PM_ENUM_INFO(queue_suspend_trigger_t, QUEUE_SUSPEND_TRIGGER, CRIU_RESTORE) + +SPECIALIZE_PM_ENUM_INFO(unmap_from_gpu_trigger_t, UNMAP_FROM_GPU_TRIGGER, MMU_NOTIFY) +SPECIALIZE_PM_ENUM_INFO(unmap_from_gpu_trigger_t, UNMAP_FROM_GPU_TRIGGER, MMU_NOTIFY_MIGRATE) +SPECIALIZE_PM_ENUM_INFO(unmap_from_gpu_trigger_t, UNMAP_FROM_GPU_TRIGGER, UNMAP_FROM_CPU) + +using trigger_type_list_t = common::mpl::type_list; + +template +std::string_view +to_string_impl(EnumT val, std::index_sequence) +{ + if(val == Idx) return page_migration_enum_info::name; + if constexpr(sizeof...(IdxTail) > 0) + return to_string_impl(val, std::index_sequence{}); + else + return std::string_view{}; +} + +template +std::string_view +to_string(EnumT val, + std::enable_if_t::value && + common::mpl::is_one_of::value, + int> = 0) +{ + constexpr auto last = page_migration_bounds::last; + return to_string_impl(val, std::make_index_sequence{}); +} + +template +page_migration_record_t parse_uvm_event(std::string_view) +{ + LOG_IF(FATAL, false) << uvm_event_info::format_str; + return {}; +} + +template <> +page_migration_record_t +parse_uvm_event(std::string_view str) +{ + page_migration_record_t rec{}; + auto& e = rec.page_fault; + uint32_t kind{}; + + char fault; + std::sscanf(str.data(), + uvm_event_info::format_str.data(), + &kind, + &rec.start_timestamp, + &rec.pid, + &e.address, + &e.node_id, + &fault); + + e.read_fault = (fault == 'R'); + e.address = page_to_bytes(e.address); + + LOG(INFO) << fmt::format("Page fault start [ ts: {} pid: {} addr: 0x{:X} node: {} ] \n", + rec.start_timestamp, + rec.pid, + e.address, + e.node_id); + + return rec; +} + +template <> +page_migration_record_t +parse_uvm_event(std::string_view str) +{ + page_migration_record_t rec{}; + auto& e = rec.page_fault; + uint32_t kind{}; + + char migrated; + std::sscanf(str.data(), + uvm_event_info::format_str.data(), + &kind, + &rec.end_timestamp, + &rec.pid, + &e.address, + &e.node_id, + &migrated); + + // M or U -> migrated / unmigrated? + if(migrated == 'M') + e.migrated = true; + else if(migrated == 'U') + e.migrated = false; + // else + // throw std::runtime_error("Invalid SVM memory migrate type"); + e.address = page_to_bytes(e.address); + + LOG(INFO) << fmt::format( + "Page fault end [ ts: {} pid: {} addr: 0x{:X} node: {} migrated: {} ] \n", + rec.end_timestamp, + rec.pid, + e.address, + e.node_id, + migrated); + + return rec; +} + +template <> +page_migration_record_t +parse_uvm_event(std::string_view str) +{ + page_migration_record_t rec{}; + auto& e = rec.page_migrate; + uint32_t kind{}; + uint32_t trigger{}; + + std::sscanf(str.data(), + uvm_event_info::format_str.data(), + &kind, + &rec.start_timestamp, + &rec.pid, + &e.start_addr, + &e.end_addr, + &e.from_node, + &e.to_node, + &e.prefetch_node, + &e.preferred_node, + &trigger); + + e.end_addr += e.start_addr; + e.trigger = static_cast(trigger); + e.start_addr = page_to_bytes(e.start_addr); + e.end_addr = page_to_bytes(e.end_addr) - 1; + + LOG(INFO) << fmt::format( + "Page migrate start [ ts: {} pid: {} addr s: 0x{:X} addr " + "e: 0x{:X} size: {}B from node: {} to node: {} prefetch node: {} preferred node: {} " + "trigger: {} ] \n", + rec.start_timestamp, + rec.pid, + e.start_addr, + e.end_addr, + (e.end_addr - e.start_addr), + e.from_node, + e.to_node, + e.prefetch_node, + e.preferred_node, + to_string(e.trigger)); + + return rec; +} + +template <> +page_migration_record_t +parse_uvm_event(std::string_view str) +{ + page_migration_record_t rec{}; + auto& e = rec.page_migrate; + uint32_t kind{}; + uint32_t trigger{}; + + std::sscanf(str.data(), + uvm_event_info::format_str.data(), + &kind, + &rec.end_timestamp, + &rec.pid, + &e.start_addr, + &e.end_addr, + &e.from_node, + &e.to_node, + &trigger); + + e.end_addr += e.start_addr; + e.trigger = static_cast(trigger); + e.start_addr = page_to_bytes(e.start_addr); + e.end_addr = page_to_bytes(e.end_addr) - 1; + + LOG(INFO) << fmt::format("Page migrate end [ ts: {} pid: {} addr s: 0x{:X} addr e: " + "0x{:X} from node: {} to node: {} trigger: {} ] \n", + rec.end_timestamp, + rec.pid, + e.start_addr, + e.end_addr, + e.from_node, + e.to_node, + to_string(e.trigger)); + + return rec; +} + +template <> +page_migration_record_t +parse_uvm_event(std::string_view str) +{ + page_migration_record_t rec{}; + auto& e = rec.queue_suspend; + uint32_t kind{}; + uint32_t trigger{}; + + std::sscanf(str.data(), + uvm_event_info::format_str.data(), + &kind, + &rec.start_timestamp, + &rec.pid, + &e.node_id, + &trigger); + + rec.queue_suspend.trigger = static_cast(trigger); + + LOG(INFO) << fmt::format("Queue evict [ ts: {} pid: {} node: {} trigger: {} ] \n", + rec.start_timestamp, + rec.pid, + e.node_id, + to_string(e.trigger)); + + return rec; +} + +template <> +page_migration_record_t +parse_uvm_event(std::string_view str) +{ + page_migration_record_t rec{}; + auto& e = rec.queue_suspend; + uint32_t kind{}; + + std::sscanf(str.data(), + uvm_event_info::format_str.data(), + &kind, + &rec.end_timestamp, + &rec.pid, + &e.node_id); + // check if we have a valid char at the end. -1 has \0 + if(str[str.size() - 2] == 'R') + e.rescheduled = true; + else + e.rescheduled = false; + + LOG(INFO) << fmt::format( + "Queue restore [ ts: {} pid: {} node: {} ] \n", rec.end_timestamp, rec.pid, e.node_id); + + return rec; +} + +template <> +page_migration_record_t +parse_uvm_event(std::string_view str) +{ + page_migration_record_t rec{}; + auto& e = rec.unmap_from_gpu; + uint32_t kind{}; + uint32_t trigger{}; + + std::sscanf(str.data(), + uvm_event_info::format_str.data(), + &kind, + &rec.start_timestamp, + &rec.pid, + &e.start_addr, + &e.end_addr, + &e.node_id, + &trigger); + + e.end_addr += e.start_addr; + rec.end_timestamp = rec.start_timestamp; + rec.unmap_from_gpu.trigger = static_cast(trigger); + e.start_addr = page_to_bytes(e.start_addr); + e.end_addr = page_to_bytes(e.end_addr); + + LOG(INFO) << fmt::format("Unmap from GPU [ ts: {} pid: {} start addr: 0x{:X} end addr: 0x{:X} " + "node: {} trigger {} ] \n", + rec.start_timestamp, + rec.pid, + e.start_addr, + e.end_addr, + e.node_id, + to_string(e.trigger)); + + return rec; +} + +template +page_migration_record_t +parse_uvm_event(uvm_event_id_t event_id, + std::string_view strn, + std::index_sequence) +{ + if(OpInx == static_cast(event_id)) + { + auto rec = parse_uvm_event(strn); + rec.size = sizeof(page_migration_record_t); + rec.kind = ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION; + rec.operation = to_rocprof_op(OpInx); + return rec; + } + else if constexpr(sizeof...(OpInxs) > 0) + return parse_uvm_event(event_id, strn, std::index_sequence{}); + else + return page_migration_record_t{}; +} + +/* -----------------------------------------------------------------------------------*/ + +template +void +update_end(const page_migration_record_t& start, page_migration_record_t& end); + +template <> +void +update_end(const page_migration_record_t& start, + page_migration_record_t& end) +{ + CHECK(start.pid == end.pid); + CHECK(start.page_fault.address == end.page_fault.address); + CHECK(start.page_fault.node_id == end.page_fault.node_id); + COPY_FROM_START_1(start_timestamp); + COPY_FROM_START_2(page_fault, migrated); +} + +template <> +void +update_end(const page_migration_record_t& start, + page_migration_record_t& end) +{ + CHECK(start.pid == end.pid); + CHECK(start.page_migrate.start_addr == end.page_migrate.start_addr); + CHECK(start.page_migrate.end_addr == end.page_migrate.end_addr); + CHECK(start.page_migrate.from_node == end.page_migrate.from_node); + CHECK(start.page_migrate.to_node == end.page_migrate.to_node); + CHECK(start.page_migrate.trigger == end.page_migrate.trigger); + COPY_FROM_START_1(start_timestamp); + COPY_FROM_START_2(page_migrate, prefetch_node); + COPY_FROM_START_2(page_migrate, preferred_node); +} + +template <> +void +update_end(const page_migration_record_t& start, + page_migration_record_t& end) +{ + CHECK(start.pid == end.pid); + CHECK(start.queue_suspend.node_id == end.queue_suspend.node_id); + COPY_FROM_START_1(start_timestamp); + COPY_FROM_START_2(queue_suspend, trigger); +} + +/* -----------------------------------------------------------------------------------*/ + +template +uint64_t +get_key(const rocprofiler_buffer_tracing_page_migration_record_t& rec) = delete; + +template <> +uint64_t +get_key( + const rocprofiler_buffer_tracing_page_migration_record_t& rec) +{ + // page migrate, use address as identifier + return rec.page_migrate.start_addr; +} + +template <> +uint64_t +get_key( + const rocprofiler_buffer_tracing_page_migration_record_t& rec) +{ + // page fault, use address as identifier + return rec.page_fault.address; +} + +template <> +uint64_t +get_key( + const rocprofiler_buffer_tracing_page_migration_record_t& rec) +{ + // Queue suspend/evict. Node ID and pid are sufficient as in kfd, + // eviction is reference-counted per process-device. + uint64_t node_id = rec.queue_suspend.node_id; + return (node_id << 32) | rec.pid; +} + +/* -----------------------------------------------------------------------------------*/ + +template <> +page_migration_record_t parse_uvm_event<0>(std::string_view) +{ + throw std::runtime_error("None Op for parsing UVM events should not happen"); +} + +template <> +void +update_end(const page_migration_record_t&, page_migration_record_t&) +{ + throw std::runtime_error("None Op for parsing UVM events should not happen"); +} + +template <> +uint64_t +get_key(const page_migration_record_t&) +{ + throw std::runtime_error("None Op for parsing UVM events should not happen"); +} + +/* -----------------------------------------------------------------------------------*/ + +template +void +update_end(uvm_event_id_t event_id, + const page_migration_record_t& start, + page_migration_record_t& end, + std::index_sequence) +{ + if(OpInx == static_cast(event_id)) + update_end(start, end); + else if constexpr(sizeof...(OpInxs) > 0) + update_end(event_id, start, end, std::index_sequence{}); + else + return; +} + +template +uint64_t +get_key(uvm_event_id_t event_id, + const page_migration_record_t& record, + std::index_sequence) +{ + if constexpr(OpInx == ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU) + return {}; + else if(is_rocprof_uvm_map(event_id)) + return get_key::operation_idx>(record); + else if constexpr(sizeof...(OpInxs) > 0) + return get_key(event_id, record, std::index_sequence{}); + else + return {}; +} + +void +update_end(uvm_event_id_t event_id, + const page_migration_record_t& start, + page_migration_record_t& end) +{ + update_end(event_id, + start, + end, + std::index_sequence{}); +} +} // namespace + +// Event capture and reporting +namespace +{ +// Support seems to have been added in kfdv > 1.10 +static_assert(KFD_IOCTL_MAJOR_VERSION == 1, "KFD API major version changed"); +static_assert(KFD_IOCTL_MINOR_VERSION >= 10, "KFD SMI support missing in kfd_ioctl.h"); + +// Convert from public events to KFD enum config + +template +constexpr size_t +kfd_bitmask_impl(size_t uvm_event_id, std::index_sequence) +{ + if(uvm_event_id == OpInx) return page_migration_info::kfd_bitmask; + if constexpr(sizeof...(OpInxs) > 0) + return kfd_bitmask_impl(uvm_event_id, std::index_sequence{}); + else + return 0; +} + +template +constexpr auto +kfd_bitmask(const small_vector& rocprof_event_ids, std::index_sequence) +{ + uint64_t m{}; + for(const size_t& event_id : rocprof_event_ids) + { + m |= kfd_bitmask_impl(event_id, std::index_sequence{}); + } + return m; +} + +template +constexpr size_t +to_uvm_op_impl(size_t kfd_id, std::index_sequence) +{ + // if(kfd_id == uvm_event_info::kfd_event) return uvm_event_info::uvm_event; + if(kfd_id == uvm_event_info::kfd_event) return OpInx; + if constexpr(sizeof...(OpInxs) > 0) + return to_uvm_op_impl(kfd_id, std::index_sequence{}); + else + return 0; +} + +constexpr uvm_event_id_t +kfd_to_uvm_op(kfd_event_id_t kfd_id) +{ + return static_cast( + to_uvm_op_impl(kfd_id, std::make_index_sequence{})); +} + +struct buffered_context_data +{ + const context::context* ctx = nullptr; +}; + +void +populate_contexts(int operation_idx, std::vector& buffered_contexts) +{ + buffered_contexts.clear(); + + auto active_contexts = context::context_array_t{}; + for(const auto* itr : context::get_active_contexts(active_contexts)) + { + if(itr->buffered_tracer) + { + // if the given domain + op is not enabled, skip this context + if(itr->buffered_tracer->domains(ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION, + operation_idx)) + buffered_contexts.emplace_back(buffered_context_data{itr}); + } + } +} + +void +remove_events(events_cache_t& events, size_t timestamp) +{ + for(auto map : events) + { + for(auto i = map.begin(); i != map.end(); ++i) + { + if(i->second.start_timestamp < timestamp) map.erase(i); + } + } +} + +bool +report_event(uvm_event_id_t event_id, + rocprofiler_buffer_tracing_page_migration_record_t& end_record) +{ + using rocprofiler_page_migr_seq = std::make_index_sequence; + static thread_local events_cache_t EVENTS_CACHE{}; + + auto& events_map = EVENTS_CACHE[to_rocprof_op(event_id)]; + + switch(static_cast(event_id)) + { + case ROCPROFILER_UVM_EVENT_MIGRATE_START: [[fallthrough]]; + case ROCPROFILER_UVM_EVENT_PAGE_FAULT_START: [[fallthrough]]; + case ROCPROFILER_UVM_EVENT_QUEUE_EVICTION: + { + // insert into map + auto key = get_key(event_id, end_record, rocprofiler_page_migr_seq{}); + events_map[key] = end_record; + return false; + } + // End events. Pair up and report + case ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU: + { + return true; + } + case ROCPROFILER_UVM_EVENT_MIGRATE_END: [[fallthrough]]; + case ROCPROFILER_UVM_EVENT_PAGE_FAULT_END: [[fallthrough]]; + case ROCPROFILER_UVM_EVENT_QUEUE_RESTORE: + { + auto key = get_key(event_id, end_record, rocprofiler_page_migr_seq{}); + if(auto start_rec = events_map.find(key); start_rec != events_map.end()) + { + update_end(event_id, start_rec->second, end_record); + } + else + { + // we got an end record and can't find the start record + // drop everything in the map before this timestamp + remove_events(EVENTS_CACHE, end_record.end_timestamp); + } + return true; + } + default: throw std::runtime_error("Invalid page migration event"); + } +} + +void +handle_reporting(std::string_view event_data) +{ + uint32_t kfd_event_id; + std::sscanf(event_data.data(), "%x ", &kfd_event_id); + std::vector buffered_contexts{}; + + auto uvm_event_op = kfd_to_uvm_op(static_cast(kfd_event_id)); + + populate_contexts(uvm_event_op, buffered_contexts); + if(buffered_contexts.empty()) return; + + // Parse and process the event + auto record = parse_uvm_event( + uvm_event_op, event_data, std::make_index_sequence{}); + + // pair up start and end and only then insert it into the buffer + if(report_event(uvm_event_op, record)) + { + for(const auto& itr : buffered_contexts) + { + auto* _buffer = buffer::get_buffer(itr.ctx->buffered_tracer->buffer_data.at( + ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION)); + CHECK_NOTNULL(_buffer)->emplace(ROCPROFILER_BUFFER_CATEGORY_TRACING, + ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION, + record); + } + } +} + +} // namespace + +// KFD utils +namespace kfd +{ +void +poll_events(small_vector, bool); + +using fd_flags_t = decltype(EFD_NONBLOCK); +using fd_t = decltype(pollfd::fd); +constexpr auto KFD_DEVICE_PATH{"/dev/kfd"}; + +SPECIALIZE_KFD_IOC_IOCTL(kfd_ioctl_get_version_args, AMDKFD_IOC_GET_VERSION); +SPECIALIZE_KFD_IOC_IOCTL(kfd_ioctl_smi_events_args, AMDKFD_IOC_SMI_EVENTS); + +namespace +{ +template +auto +ioctl(int kfd_fd, T& args) +{ + // from hsaKmt library (hsakmt/src/libhsakmt.c) + int exit_code{}; + + do + { + exit_code = ::ioctl(kfd_fd, IOC_event::value, static_cast(&args)); + } while(exit_code == -1 && (errno == EINTR || errno == EAGAIN)); + + if(exit_code == -1 && errno == EBADF) + { + /* In case pthread_atfork didn't catch it, this will + * make any subsequent hsaKmt calls fail in CHECK_KFD_OPEN. + */ + CHECK(true && "KFD file descriptor not valid in this process\n"); + } + return exit_code; +} + +struct kfd_device_fd +{ + fd_t fd{-1}; + + kfd_device_fd() + { + fd = ::open(KFD_DEVICE_PATH, O_RDWR | O_CLOEXEC); + LOG_IF(FATAL, fd == -1) << "Error opening KFD handle @ " << KFD_DEVICE_PATH; + } + + ~kfd_device_fd() + { + if(fd >= 0) close(fd); + } +}; + +const kfd_ioctl_get_version_args +get_version() +{ + static kfd_ioctl_get_version_args version = [&]() { + auto args = kfd_ioctl_get_version_args{0, 0}; + kfd_device_fd kfd_fd{}; + + if(ioctl(kfd_fd.fd, args) != -1) + LOG(INFO) << fmt::format("KFD v{}.{}", args.major_version, args.minor_version); + else + LOG(ERROR) << fmt::format("Could not determine KFD version"); + return args; + }(); + + return version; +} + +struct poll_kfd_t +{ + static constexpr auto DEFAULT_FLAGS{EFD_CLOEXEC}; + + struct gpu_fd_t + { + unsigned int node_id{}; + fd_t fd{}; + const rocprofiler_agent_t* agent{}; + }; + + kfd_device_fd kfd_fd{}; + small_vector file_handles{}; + pollfd thread_notify{}; + std::thread bg_thread; + bool active{false}; + + poll_kfd_t() = default; + + poll_kfd_t(const small_vector& rprof_ev, bool non_blocking) + : kfd_fd{kfd_device_fd{}} + { + const auto kfd_flags = + kfd_bitmask(rprof_ev, std::make_index_sequence{}); + + LOG(INFO) << fmt::format("Setting KFD flags to [0b{:b}] \n", kfd_flags); + + // Create fd for notifying thread when we want to wake it up, and an eventfd for any events + // to this thread + file_handles.emplace_back(pollfd{ + .fd = eventfd(0, DEFAULT_FLAGS), + }); + + fd_t thread_pipes[2]{}; + + [&]() { + const auto retcode = pipe2(&thread_pipes[0], DEFAULT_FLAGS); + + if(retcode != 0) + throw std::runtime_error{ + fmt::format("Pipe creation for thread notify failed with {} code\n", retcode)}; + }(); + + thread_notify = pollfd{ + .fd = thread_pipes[1], + .events = POLLIN, + .revents = 0, + }; + + // add pipe listening end to fds to watch + file_handles.emplace_back(pollfd{thread_pipes[0], POLLIN, 0}); + + // get FD, start thread, and then enable events + for(const auto& agent : agent::get_agents()) + { + if(agent->type == ROCPROFILER_AGENT_TYPE_GPU) + { + auto gpu_event_fd = get_node_fd(agent->gpu_id); + file_handles.emplace_back(pollfd{gpu_event_fd, POLLIN, 0}); + LOG(INFO) << fmt::format( + "GPU node {} with fd {} added\n", agent->gpu_id, gpu_event_fd); + } + } + + // Enable KFD masked events by writing flags to kfd fd + for(size_t i = 2; i < file_handles.size(); ++i) + { + auto& fd = file_handles[i]; + auto write_size = write(fd.fd, &kfd_flags, sizeof(kfd_flags)); + LOG(INFO) << fmt::format( + "Writing {} to GPU fd {} ({} bytes)\n", kfd_flags, fd.fd, write_size); + CHECK(write_size == sizeof(kfd_flags)); + } + + // start bg thread + internal_threading::notify_pre_internal_thread_create(ROCPROFILER_LIBRARY); + bg_thread = std::thread{poll_events, file_handles, non_blocking}; + internal_threading::notify_post_internal_thread_create(ROCPROFILER_LIBRARY); + + active = true; + } + + inline static auto get_event_id(const std::string_view& strn) + { + uint32_t event_id{std::numeric_limits::max()}; + std::sscanf(strn.data(), "%x ", &event_id); + + CHECK(event_id <= KFD_SMI_EVENT_ALL_PROCESS); + } + + poll_kfd_t(const poll_kfd_t&) = delete; + poll_kfd_t& operator=(const poll_kfd_t&) = delete; + + poll_kfd_t(poll_kfd_t&&) = default; + poll_kfd_t& operator=(poll_kfd_t&&) = default; + + ~poll_kfd_t(); + + node_fd_t get_node_fd(int gpu_node_id) const + { + kfd_ioctl_smi_events_args args{}; + args.gpuid = gpu_node_id; + + if(auto ret = ioctl(kfd_fd.fd, args); ret == -1) + LOG(ERROR) << fmt::format( + "Could not get GPU node {} file descriptor (exit code: {})", gpu_node_id, ret); + return args.anon_fd; + } +}; + +// for all contexts +struct page_migration_config +{ + bool should_exit() const { return m_should_exit.load(); } + void set_exit(bool val) { m_should_exit.store(val); } + + uint64_t enabled_events = 0; + kfd::poll_kfd_t* kfd_handle = nullptr; + +private: + std::atomic m_should_exit = false; +}; + +page_migration_config& +get_config() +{ + static auto& state = *common::static_object::construct(); + return state; +} + +kfd::poll_kfd_t::~poll_kfd_t() +{ + LOG(INFO) << fmt::format("Terminating poll_kfd\n"); + if(!active) return; + + // wake thread up + kfd::get_config().set_exit(true); + auto bytes_written{-1}; + do + { + bytes_written = write(thread_notify.fd, "E", 1); + } while(bytes_written == -1 && (errno == EINTR || errno == EAGAIN)); + + if(bg_thread.joinable()) bg_thread.join(); + LOG(INFO) << fmt::format("Background thread terminated\n"); + + for(const auto& f : file_handles) + close(f.fd); +} +} // namespace + +void +poll_events(small_vector file_handles, bool non_blocking) +{ + // storage to write records to, 1MB + constexpr size_t PREALLOCATE_ELEMENT_COUNT{1024 * 128}; + std::string scratch_buffer(PREALLOCATE_ELEMENT_COUNT, '\0'); + auto& exitfd = file_handles[1]; + const auto timeout_val = non_blocking == true ? 0 : -1; + + // Wait or spin on events. + // 0 -> return immediately even if no events + // -1 -> wait indefinitely + + LOG(INFO) << fmt::format("{} polling = {}, polling with timeout = {}", + non_blocking ? "Non-blocking" : "Blocking", + non_blocking, + timeout_val); + + pthread_setname_np(pthread_self(), "bg:pagemigr"); + + for(auto& fd : file_handles) + { + LOG(INFO) << fmt::format( + "Handle = {}, events = {}, revents = {}\n", fd.fd, fd.events, fd.revents); + } + + while(!kfd::get_config().should_exit()) + { + auto poll_ret = poll(file_handles.data(), file_handles.size(), timeout_val); + + if(poll_ret == -1) + throw std::runtime_error{"Background thread file descriptors are invalid"}; + + if((exitfd.revents & POLLIN) != 0) + { + LOG(INFO) << "Terminating background thread\n"; + return; + } + + using namespace std::chrono_literals; + + for(size_t i = 2; i < file_handles.size(); ++i) + { + auto& fd = file_handles[i]; + + // We have data to read, perhaps multiple events + if((fd.revents & POLLIN) != 0) + { + size_t status_size = read(fd.fd, scratch_buffer.data(), scratch_buffer.size()); + + // LOG(INFO) << fmt::format( + // "status_size: {} size {}\n", status_size, scratch_buffer.size()); + std::string_view event_strings{scratch_buffer.data(), status_size}; + + // LOG(INFO) << fmt::format("Raw KFD string [({})]\n", + // event_strings.data()); + KFD_EVENT_PARSE_EVENTS(event_strings, handle_reporting); + } + fd.revents = 0; + } + } +} +} // namespace kfd + +template +const char* +name_by_id(const uint32_t id, std::index_sequence) +{ + if(Idx == id) return page_migration_info::name; + if constexpr(sizeof...(IdxTail) > 0) + return name_by_id(id, std::index_sequence{}); + else + return nullptr; +} + +template +void +get_ids(std::vector& _id_list, std::index_sequence) +{ + auto _emplace = [](auto& _vec, uint32_t _v) { + if(_v < static_cast(ROCPROFILER_HSA_AMD_EXT_API_ID_LAST)) _vec.emplace_back(_v); + }; + + (_emplace(_id_list, page_migration_info::operation_idx), ...); +} + +bool +context_filter(const context::context* ctx) +{ + return (ctx->buffered_tracer && + (ctx->buffered_tracer->domains(ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION))); +} + +template +void +to_bitmask(small_vector& _id_list, std::index_sequence) +{ + auto _emplace = [](auto& _vec, uint32_t _v) { + if(_v < static_cast(ROCPROFILER_HSA_AMD_EXT_API_ID_LAST)) _vec.emplace_back(_v); + }; + + (_emplace(_id_list, page_migration_info::operation_idx), ...); +} + +namespace +{ +rocprofiler_status_t +init(const small_vector& event_ids, bool non_blocking) +{ + // Check if version is more than 1.11 + auto ver = kfd::get_version(); + if(ver.major_version * 1000 + ver.minor_version > 1011) + { + if(!context::get_registered_contexts(context_filter).empty()) + { + if(!kfd::get_config().kfd_handle) + kfd::get_config().kfd_handle = new kfd::poll_kfd_t{event_ids, non_blocking}; + } + return ROCPROFILER_STATUS_SUCCESS; + } + else + { + // Add a buffer record with this info + LOG(ERROR) << fmt::format( + "KFD does not support SVM event reporting in v{}.{} (requires v1.11)", + ver.major_version, + ver.minor_version); + return ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL; + } +} +} // namespace + +rocprofiler_status_t +init() +{ + // Testing page migration + return init({ROCPROFILER_PAGE_MIGRATION_NONE, + ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, + ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, + ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, + ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU}, + rocprofiler::common::get_env("ROCPROF_PAGE_MIGRATION_NON_BLOCKING", false)); +} + +void +finalize() +{ + if(kfd::get_config().kfd_handle) + { + kfd::poll_kfd_t* _handle = nullptr; + std::swap(kfd::get_config().kfd_handle, _handle); + delete _handle; + } +} + +const char* +name_by_id(uint32_t id) +{ + return name_by_id(id, std::make_index_sequence{}); +} + +std::vector +get_ids() +{ + auto _data = std::vector{}; + _data.reserve(ROCPROFILER_PAGE_MIGRATION_LAST); + get_ids(_data, std::make_index_sequence{}); + return _data; +} +} // namespace page_migration +} // namespace rocprofiler diff --git a/source/lib/rocprofiler-sdk/page_migration/page_migration.def.cpp b/source/lib/rocprofiler-sdk/page_migration/page_migration.def.cpp new file mode 100644 index 0000000000..ace2dae896 --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/page_migration.def.cpp @@ -0,0 +1,74 @@ +// 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 "lib/rocprofiler-sdk/page_migration/defines.hpp" +#include "lib/rocprofiler-sdk/page_migration/page_migration.hpp" + +#if defined(ROCPROFILER_LIB_ROCPROFILER_SDK_PAGE_MIGRATION_PAGE_MIGRATION_CPP_IMPL) && \ + ROCPROFILER_LIB_ROCPROFILER_SDK_PAGE_MIGRATION_PAGE_MIGRATION_CPP_IMPL == 1 + +namespace rocprofiler +{ +namespace page_migration +{ +// clang-format off +// Map ROCPROF UVM enums to KFD enums +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_NONE, KFD_SMI_EVENT_NONE, "Error: Invalid UVM event from KFD" ); +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_MIGRATE_START, KFD_SMI_EVENT_MIGRATE_START, "%x %ld -%d @%lx(%lx) %x->%x %x:%x %d\n" ); +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_MIGRATE_END, KFD_SMI_EVENT_MIGRATE_END, "%x %ld -%d @%lx(%lx) %x->%x %d\n" ); +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_PAGE_FAULT_START, KFD_SMI_EVENT_PAGE_FAULT_START, "%x %ld -%d @%lx(%x) %c\n" ); +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_PAGE_FAULT_END, KFD_SMI_EVENT_PAGE_FAULT_END, "%x %ld -%d @%lx(%x) %c\n" ); +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_QUEUE_EVICTION, KFD_SMI_EVENT_QUEUE_EVICTION, "%x %ld -%d %x %d\n" ); +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_QUEUE_RESTORE, KFD_SMI_EVENT_QUEUE_RESTORE, "%x %ld -%d %x\n" ); +SPECIALIZE_UVM_KFD_EVENT(ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU, KFD_SMI_EVENT_UNMAP_FROM_GPU, "%x %ld -%d @%lx(%lx) %x %d\n" ); +// clang-format on +# undef SPECIALIZE_UVM_KFD_EVENT + +SPECIALIZE_PAGE_MIGRATION_INFO(NONE, NONE); +SPECIALIZE_PAGE_MIGRATION_INFO(PAGE_MIGRATE, MIGRATE_START, MIGRATE_END); +SPECIALIZE_PAGE_MIGRATION_INFO(PAGE_FAULT, PAGE_FAULT_START, PAGE_FAULT_END); +SPECIALIZE_PAGE_MIGRATION_INFO(QUEUE_SUSPEND, QUEUE_EVICTION, QUEUE_RESTORE); +SPECIALIZE_PAGE_MIGRATION_INFO(UNMAP_FROM_GPU, UNMAP_FROM_GPU); + +template +constexpr size_t to_rocprof_op_impl(std::index_sequence) +{ + return ((((bitmask(UvmOpInx) & page_migration_info::uvm_bitmask) != 0) * OpInxs) + ...); +} + +template +constexpr auto _to_rocprof_op_impl(std::index_sequence) +{ + return std::array{ + to_rocprof_op_impl(std::make_index_sequence{})...}; +} + +constexpr auto +to_rocprof_op(size_t pos) +{ + using rop = rocprofiler_page_migration_operation_t; + return static_cast( + _to_rocprof_op_impl(std::make_index_sequence{})[pos]); +} +} // namespace page_migration +} // namespace rocprofiler +#endif diff --git a/source/lib/rocprofiler-sdk/page_migration/page_migration.hpp b/source/lib/rocprofiler-sdk/page_migration/page_migration.hpp new file mode 100644 index 0000000000..9e4c28653f --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/page_migration.hpp @@ -0,0 +1,45 @@ +// 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. + +#pragma once + +#include "lib/common/container/small_vector.hpp" + +#include + +namespace rocprofiler +{ +namespace page_migration +{ +const char* +name_by_id(uint32_t id); + +std::vector +get_ids(); + +rocprofiler_status_t +init(); + +void +finalize(); +} // namespace page_migration +} // namespace rocprofiler diff --git a/source/lib/rocprofiler-sdk/page_migration/utils.hpp b/source/lib/rocprofiler-sdk/page_migration/utils.hpp new file mode 100644 index 0000000000..589e31ca4e --- /dev/null +++ b/source/lib/rocprofiler-sdk/page_migration/utils.hpp @@ -0,0 +1,218 @@ +// 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. + +#pragma once + +#include "lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h" + +#include +#include + +#include +#include +#include + +namespace rocprofiler +{ +namespace page_migration +{ +// serves as an overview of what events we capture and report +enum fault_type_t +{ + NONE, + READ, + WRITE, +}; + +struct uvm_event_page_fault_start_t +{ + int kind; + uint64_t start_timestamp; + int pid; + int node_id; + uint64_t address; + fault_type_t fault; +}; + +struct uvm_event_page_fault_end_t +{ + int kind; + uint64_t end_timestamp; + uint32_t pid; + int node_id; + uint64_t address; + bool migrated; +}; + +struct uvm_event_migrate_start_t +{ + int kind; + uint64_t start_timestamp; + uint32_t pid; + uint64_t start; + uint64_t end_offset; + uint32_t from; + uint32_t to; + uint32_t prefetch_node; // last prefetch location, 0 for CPU, or GPU id + uint32_t preferred_node; // perferred location, 0 for CPU, or GPU id + uint32_t trigger; +}; + +struct uvm_event_migrate_end_t +{ + int kind; + uint64_t end_timestamp; + uint32_t pid; + uint64_t start; + uint64_t end_offset; + uint32_t from; + uint32_t to; + uint32_t trigger; +}; + +struct uvm_event_queue_eviction_t +{ + int kind; + uint64_t start_timestamp; + uint32_t pid; + int node_id; + uint32_t trigger; +}; + +struct uvm_event_queue_restore_t +{ + int kind; + uint64_t end_timestamp; + uint32_t pid; + int node_id; + bool rescheduled; +}; + +struct uvm_event_unmap_from_gpu_t +{ + int kind; + uint64_t timestamp; + uint32_t pid; + uint64_t address; + uint64_t size; + int node_id; + uint32_t trigger; +}; + +template +struct uvm_event_info; + +template +struct page_migration_info; + +namespace kfd +{ +template +struct IOC_event; +} // namespace kfd + +constexpr size_t +bitmask(size_t num) +{ + if(num == 0) + return 0; + else + return (1ULL << (num - 1)); +} + +template +constexpr size_t bitmask(std::index_sequence) +{ + return (bitmask(Args) | ...); +} + +enum uvm_event_id_t +{ + ROCPROFILER_UVM_EVENT_NONE, + ROCPROFILER_UVM_EVENT_MIGRATE_START, + ROCPROFILER_UVM_EVENT_MIGRATE_END, + ROCPROFILER_UVM_EVENT_PAGE_FAULT_START, + ROCPROFILER_UVM_EVENT_PAGE_FAULT_END, + ROCPROFILER_UVM_EVENT_QUEUE_EVICTION, + ROCPROFILER_UVM_EVENT_QUEUE_RESTORE, + ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU, + ROCPROFILER_UVM_EVENT_LAST, +}; + +static_assert(KFD_SMI_EVENT_NONE == 0); +static_assert(KFD_SMI_EVENT_MIGRATE_START == 5); +static_assert(KFD_SMI_EVENT_MIGRATE_END == 6); +static_assert(KFD_SMI_EVENT_PAGE_FAULT_START == 7); +static_assert(KFD_SMI_EVENT_PAGE_FAULT_END == 8); +static_assert(KFD_SMI_EVENT_QUEUE_EVICTION == 9); +static_assert(KFD_SMI_EVENT_QUEUE_RESTORE == 10); +static_assert(KFD_SMI_EVENT_UNMAP_FROM_GPU == 11); +static_assert(KFD_SMI_EVENT_ALL_PROCESS == 64); + +using rocprof_buffer_op_t = rocprofiler_page_migration_operation_t; + +using node_fd_t = int; + +using event_map_t = + std::unordered_map; +using events_cache_t = std::array; + +template +constexpr size_t to_kfd_bitmask(std::index_sequence) +{ + return bitmask(std::index_sequence::kfd_event...>()); +} + +template +constexpr size_t to_uvm_bitmask(std::index_sequence) +{ + return bitmask(std::index_sequence(Ops)...>()); +} + +template +constexpr bool +is_rocprof_uvm_map() +{ + return page_migration_info::uvm_bitmask & bitmask(UvmOpIdx); +} + +template +constexpr bool +_is_rocprof_uvm_map(size_t uvm_event, std::index_sequence) +{ + if(OpInx == uvm_event) + return is_rocprof_uvm_map(); + else if constexpr(sizeof...(OpInxs) > 0) + return _is_rocprof_uvm_map(uvm_event, std::index_sequence{}); + else + return false; +} + +template +constexpr bool +is_rocprof_uvm_map(size_t uvm_event) +{ + return _is_rocprof_uvm_map( + uvm_event, std::make_index_sequence{}); +} +} // namespace page_migration +} // namespace rocprofiler diff --git a/source/lib/rocprofiler-sdk/registration.cpp b/source/lib/rocprofiler-sdk/registration.cpp index f9b1d0ebaa..38af6c9f7b 100644 --- a/source/lib/rocprofiler-sdk/registration.cpp +++ b/source/lib/rocprofiler-sdk/registration.cpp @@ -38,6 +38,7 @@ #include "lib/rocprofiler-sdk/intercept_table.hpp" #include "lib/rocprofiler-sdk/internal_threading.hpp" #include "lib/rocprofiler-sdk/marker/marker.hpp" +#include "lib/rocprofiler-sdk/page_migration/page_migration.hpp" #include #include @@ -600,6 +601,7 @@ finalize() set_fini_status(-1); hsa::async_copy_fini(); hsa::queue_controller_fini(); + page_migration::finalize(); hsa::code_object_shutdown(); if(get_init_status() > 0) { diff --git a/source/lib/rocprofiler-sdk/rocprofiler.cpp b/source/lib/rocprofiler-sdk/rocprofiler.cpp index 69bc37487d..a55ecd9525 100644 --- a/source/lib/rocprofiler-sdk/rocprofiler.cpp +++ b/source/lib/rocprofiler-sdk/rocprofiler.cpp @@ -87,6 +87,8 @@ ROCPROFILER_STATUS_STRING(ROCPROFILER_STATUS_ERROR_AST_NOT_FOUND, "AST was not f ROCPROFILER_STATUS_STRING( ROCPROFILER_STATUS_ERROR_AQL_NO_EVENT_COORD, "AQL Profiler was not able to find event coordinates for defined counters") +ROCPROFILER_STATUS_STRING(ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL, + "A service depends on a newer version of KFD (amdgpu kernel driver)") template const char* get_status_name(rocprofiler_status_t status, std::index_sequence) diff --git a/source/lib/rocprofiler-sdk/tests/CMakeLists.txt b/source/lib/rocprofiler-sdk/tests/CMakeLists.txt index 73d44e08e2..28353aa26e 100644 --- a/source/lib/rocprofiler-sdk/tests/CMakeLists.txt +++ b/source/lib/rocprofiler-sdk/tests/CMakeLists.txt @@ -36,8 +36,9 @@ set_tests_properties(${lib_TESTS} PROPERTIES TIMEOUT 30 LABELS "unittests") # # -------------------------------------------------------------------------------------- # -set(rocprofiler_shared_lib_sources external_correlation.cpp intercept_table.cpp - registration.cpp roctx.cpp status.cpp) +set(rocprofiler_shared_lib_sources + external_correlation.cpp intercept_table.cpp page_migration.cpp registration.cpp + roctx.cpp status.cpp) add_executable(rocprofiler-lib-tests-shared) target_sources(rocprofiler-lib-tests-shared PRIVATE ${rocprofiler_shared_lib_sources}) diff --git a/source/lib/rocprofiler-sdk/tests/page_migration.cpp b/source/lib/rocprofiler-sdk/tests/page_migration.cpp new file mode 100644 index 0000000000..84cdad9b05 --- /dev/null +++ b/source/lib/rocprofiler-sdk/tests/page_migration.cpp @@ -0,0 +1,177 @@ +// 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 "lib/common/defines.hpp" +#include "lib/rocprofiler-sdk/page_migration/details/kfd_ioctl.h" +#include "lib/rocprofiler-sdk/page_migration/utils.hpp" + +#include +#include + +#include +#include +#include + +#include +#include + +#define ROCPROFILER_LIB_ROCPROFILER_SDK_PAGE_MIGRATION_PAGE_MIGRATION_CPP_IMPL 1 +#include "lib/rocprofiler-sdk/page_migration/page_migration.def.cpp" +#undef ROCPROFILER_LIB_ROCPROFILER_SDK_PAGE_MIGRATION_PAGE_MIGRATION_CPP_IMPL + +#define ASSERT_SAME(A, B) static_assert(static_cast(A) == static_cast(B)) + +namespace +{ +constexpr std::string_view MULTILINE_STRING = "This is 0 Line 0\n" + "This is 10 Line 1\n" + "This is 20 Line 2\n" + "This is 30 Line 3\n" + "This is 40 Line 4\n"; +} + +void +return_line(const std::string_view line) +{ + static int line_no = 0; + std::stringstream strs{}; + strs << fmt::format("This is {} Line {}", line_no * 10, line_no); + EXPECT_EQ(strs.str(), line); + line_no++; +} + +auto +parse_lines() +{ + KFD_EVENT_PARSE_EVENTS(MULTILINE_STRING, return_line); +} + +TEST(page_migration, readlines) +{ + // Ensure all lines are read + parse_lines(); +} + +TEST(page_migration, parse_kvm_events) +{ + // Ensure all lines are read + parse_lines(); +} + +TEST(page_migtation, rocprof_kfd_map) +{ + using namespace ::rocprofiler::page_migration; + + // clang-format off + static_assert( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, ROCPROFILER_UVM_EVENT_PAGE_FAULT_START >() ); + static_assert( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, ROCPROFILER_UVM_EVENT_PAGE_FAULT_END >() ); + static_assert( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, ROCPROFILER_UVM_EVENT_MIGRATE_START >() ); + static_assert( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, ROCPROFILER_UVM_EVENT_MIGRATE_END >() ); + static_assert( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, ROCPROFILER_UVM_EVENT_QUEUE_EVICTION >() ); + static_assert( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, ROCPROFILER_UVM_EVENT_QUEUE_RESTORE >() ); + static_assert( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU >() ); + + EXPECT_TRUE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_START) ); + EXPECT_TRUE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_END ) ); + EXPECT_TRUE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE >(ROCPROFILER_UVM_EVENT_MIGRATE_START ) ); + EXPECT_TRUE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE >(ROCPROFILER_UVM_EVENT_MIGRATE_END ) ); + EXPECT_TRUE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND >(ROCPROFILER_UVM_EVENT_QUEUE_EVICTION ) ); + EXPECT_TRUE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND >(ROCPROFILER_UVM_EVENT_QUEUE_RESTORE ) ); + EXPECT_TRUE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU >(ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU ) ); + + + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, ROCPROFILER_UVM_EVENT_QUEUE_EVICTION >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, ROCPROFILER_UVM_EVENT_QUEUE_RESTORE >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, ROCPROFILER_UVM_EVENT_PAGE_FAULT_START >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE, ROCPROFILER_UVM_EVENT_PAGE_FAULT_END >() ); + + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, ROCPROFILER_UVM_EVENT_MIGRATE_START >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, ROCPROFILER_UVM_EVENT_MIGRATE_END >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, ROCPROFILER_UVM_EVENT_QUEUE_EVICTION >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, ROCPROFILER_UVM_EVENT_QUEUE_RESTORE >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT, ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU >() ); + + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, ROCPROFILER_UVM_EVENT_MIGRATE_START >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, ROCPROFILER_UVM_EVENT_MIGRATE_END >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, ROCPROFILER_UVM_EVENT_PAGE_FAULT_START >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, ROCPROFILER_UVM_EVENT_PAGE_FAULT_END >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND, ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU >() ); + + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, ROCPROFILER_UVM_EVENT_MIGRATE_START >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, ROCPROFILER_UVM_EVENT_MIGRATE_END >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, ROCPROFILER_UVM_EVENT_PAGE_FAULT_START >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, ROCPROFILER_UVM_EVENT_PAGE_FAULT_END >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, ROCPROFILER_UVM_EVENT_QUEUE_EVICTION >() ); + static_assert( ! is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU, ROCPROFILER_UVM_EVENT_QUEUE_RESTORE >() ); + + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE >(ROCPROFILER_UVM_EVENT_QUEUE_EVICTION ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE >(ROCPROFILER_UVM_EVENT_QUEUE_RESTORE ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE >(ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_START) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_END ) ); + + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT >(ROCPROFILER_UVM_EVENT_MIGRATE_START ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT >(ROCPROFILER_UVM_EVENT_MIGRATE_END ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT >(ROCPROFILER_UVM_EVENT_QUEUE_EVICTION ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT >(ROCPROFILER_UVM_EVENT_QUEUE_RESTORE ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT >(ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU ) ); + + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND >(ROCPROFILER_UVM_EVENT_MIGRATE_START ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND >(ROCPROFILER_UVM_EVENT_MIGRATE_END ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_START) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_END ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND >(ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU ) ); + + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU >(ROCPROFILER_UVM_EVENT_MIGRATE_START ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU >(ROCPROFILER_UVM_EVENT_MIGRATE_END ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_START) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU >(ROCPROFILER_UVM_EVENT_PAGE_FAULT_END ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU >(ROCPROFILER_UVM_EVENT_QUEUE_EVICTION ) ); + EXPECT_FALSE( is_rocprof_uvm_map < ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU >(ROCPROFILER_UVM_EVENT_QUEUE_RESTORE ) ); + + static_assert(to_rocprof_op(ROCPROFILER_UVM_EVENT_MIGRATE_START) == ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE ); + static_assert(to_rocprof_op(ROCPROFILER_UVM_EVENT_PAGE_FAULT_END) == ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT ); + static_assert(to_rocprof_op(ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU) == ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU ); + static_assert(to_rocprof_op(ROCPROFILER_UVM_EVENT_QUEUE_EVICTION) == ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND ); + + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_TRIGGER_PREFETCH, KFD_MIGRATE_TRIGGER_PREFETCH ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_TRIGGER_PAGEFAULT_GPU, KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_TRIGGER_PAGEFAULT_CPU, KFD_MIGRATE_TRIGGER_PAGEFAULT_CPU ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_TRIGGER_TTM_EVICTION, KFD_MIGRATE_TRIGGER_TTM_EVICTION ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_SVM, KFD_QUEUE_EVICTION_TRIGGER_SVM ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_USERPTR, KFD_QUEUE_EVICTION_TRIGGER_USERPTR ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_TTM, KFD_QUEUE_EVICTION_TRIGGER_TTM ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_SUSPEND, KFD_QUEUE_EVICTION_TRIGGER_SUSPEND ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_CRIU_CHECKPOINT, KFD_QUEUE_EVICTION_CRIU_CHECKPOINT ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND_TRIGGER_CRIU_RESTORE, KFD_QUEUE_EVICTION_CRIU_RESTORE ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_MMU_NOTIFY, KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY ); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_MMU_NOTIFY_MIGRATE, KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY_MIGRATE); + ASSERT_SAME(ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU_TRIGGER_UNMAP_FROM_CPU, KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU ); + + static_assert(to_kfd_bitmask(std::index_sequence< + ROCPROFILER_UVM_EVENT_PAGE_FAULT_START, ROCPROFILER_UVM_EVENT_UNMAP_FROM_GPU>()) == + (KFD_SMI_EVENT_MASK_FROM_INDEX(KFD_SMI_EVENT_PAGE_FAULT_START) + | KFD_SMI_EVENT_MASK_FROM_INDEX(KFD_SMI_EVENT_UNMAP_FROM_GPU))); + + // clang-format on +} diff --git a/source/scripts/format-deps.py b/source/scripts/format-deps.py old mode 100644 new mode 100755 index 4c319ac314..cb49eacd13 --- a/source/scripts/format-deps.py +++ b/source/scripts/format-deps.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import argparse import os import sys diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 72f8fc997a..10408a1229 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -51,6 +51,7 @@ add_subdirectory(kernel-tracing) add_subdirectory(async-copy-tracing) add_subdirectory(scratch-memory-tracing) add_subdirectory(c-tool) +add_subdirectory(page-migration) # rocprofv3 validation tests add_subdirectory(rocprofv3) diff --git a/tests/bin/CMakeLists.txt b/tests/bin/CMakeLists.txt index daef293032..49faa6217c 100644 --- a/tests/bin/CMakeLists.txt +++ b/tests/bin/CMakeLists.txt @@ -21,4 +21,5 @@ add_subdirectory(multistream) add_subdirectory(vector-operations) add_subdirectory(hip-in-libraries) add_subdirectory(scratch-memory) +add_subdirectory(page-migration) add_subdirectory(hsa-queue-dependency) diff --git a/tests/bin/page-migration/CMakeLists.txt b/tests/bin/page-migration/CMakeLists.txt new file mode 100644 index 0000000000..1406a9e580 --- /dev/null +++ b/tests/bin/page-migration/CMakeLists.txt @@ -0,0 +1,47 @@ +# +# +# +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-tests-bin-page-migration 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() + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_HIP_STANDARD 17) +set(CMAKE_HIP_EXTENSIONS OFF) +set(CMAKE_HIP_STANDARD_REQUIRED ON) + +set_source_files_properties(page-migration.cpp PROPERTIES LANGUAGE HIP) +add_executable(page-migration) +target_sources(page-migration PRIVATE page-migration.cpp) +target_compile_options(page-migration PRIVATE -W -Wall -Wextra -Wpedantic -Wshadow + -Werror) + +find_package(Threads REQUIRED) +target_link_libraries(page-migration PRIVATE Threads::Threads) + +install( + TARGETS page-migration + DESTINATION bin + COMPONENT tests) diff --git a/tests/bin/page-migration/page-migration.cpp b/tests/bin/page-migration/page-migration.cpp new file mode 100644 index 0000000000..f422a388aa --- /dev/null +++ b/tests/bin/page-migration/page-migration.cpp @@ -0,0 +1,222 @@ +// 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 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#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"); \ + } \ + } + +using auto_lock_t = std::unique_lock; +auto print_lock = std::mutex{}; + +__global__ void +kernel(size_t* __restrict__ data, int size) +{ + int x = hipBlockDim_x * hipBlockIdx_x + hipThreadIdx_x; + int stride = hipBlockDim_x * hipGridDim_x; + + for(int i = x; i < size; i += stride) + { + data[i] *= 2; + } +} + +struct mmap_allocator +{ + explicit mmap_allocator(size_t num_pages) + { + m_size = num_pages * sysconf(_SC_PAGE_SIZE); + void* ret = mmap(nullptr, // addr: null. Kernel gives us page-aligned memory + m_size, // length: num bytes to allocate + PROT_WRITE | PROT_READ, // mem_prot: Allow read/write + MAP_ANONYMOUS | MAP_PRIVATE, // flags: No file handle + -1, // no fd, use memory "MAP_ANONYMOUS" + 0); // offset into fd + if(ret == ((void*) -1)) // NOLINT performance-no-int-to-ptr + { + auto ecode = errno; + fprintf(stderr, "mmap error %d: %s", ecode, strerror(ecode)); + throw std::runtime_error("mmap failed"); + } + else + { + m_addr = ret; + ::memset(m_addr, 0, m_size); + } + } + + ~mmap_allocator() + { + auto ret = munmap(m_addr, m_size); + if(ret != 0) perror("munmap failed"); + } + + mmap_allocator(const mmap_allocator&) = delete; + mmap_allocator(mmap_allocator&&) noexcept = default; + mmap_allocator& operator=(const mmap_allocator&) = delete; + mmap_allocator& operator=(mmap_allocator&&) noexcept = default; + + template + Up* get() const + { + static_assert(!std::is_pointer::value, "must not be pointer type"); + return static_cast(m_addr); + } + +private: + size_t m_size = 0; + void* m_addr = nullptr; +}; + +int +main() +{ + using namespace std::chrono_literals; + + static constexpr auto NUM_PAGES = 16; + const auto PAGE_SIZE_BYTES = ::sysconf(_SC_PAGE_SIZE); + + size_t elem_count = (NUM_PAGES * PAGE_SIZE_BYTES) / sizeof(size_t); // one page? + + auto alloc = mmap_allocator(NUM_PAGES); + void* data_v = alloc.get(); + auto* data = alloc.get(); + + for(size_t i = 0; i < elem_count; ++i) + if(data[i] != 0) throw std::runtime_error{"bad init"}; + + printf("Allocated size: %lu bytes (%lu KB), (%lu MB), %zu elements @ %p\n", + sizeof(size_t) * elem_count, + sizeof(size_t) * elem_count / 1024, + sizeof(size_t) * elem_count / 1024 / 1024, + elem_count, + data_v); + + HIP_API_CALL(hipHostRegister(data, elem_count * sizeof(size_t), hipHostRegisterDefault)); + + char maps[1024 * 1024]; + std::memset(maps, '\0', 1024 * 1024); + auto fd = open("/proc/self/maps", O_RDONLY | O_CLOEXEC); + + if(fd == -1) + { + auto ecode = errno; + fprintf(stderr, "mmap error %d: %s", ecode, strerror(ecode)); + exit(-1); + } + + auto bytes = read(fd, maps, 1024 * 1024 - 1); + if(bytes == -1) + { + auto ecode = errno; + fprintf(stderr, "mmap error %d: %s", ecode, strerror(ecode)); + exit(-1); + } + close(fd); + + std::string_view maps_data{maps, static_cast(bytes)}; + std::cout << "------------\n"; + std::cout << maps_data; + std::cout << "------------\n"; + std::istringstream maps_stream{maps_data.data()}; + std::string line(1024, '\0'); + + while(std::getline(maps_stream, line)) + { + char __[1024]; + int _{}; + + void* start{}; + void* end{}; + + auto ret = + std::sscanf(line.data(), "%p-%p %s %d %d:%d %d\n", &start, &end, __, &_, &_, &_, &_); + if(ret > 0 && (start == data_v)) + { + size_t ptr_diff = ((size_t) end - (size_t) start); + printf("Found match: %zu %zu KB, %zu 4K > %s\n", + ptr_diff, + ptr_diff / 1024, + ptr_diff / 4096, + line.data()); + } + } + + for(int iter = 0; iter < 1000; ++iter) + { + for(size_t i = 0; i < elem_count; ++i) + data[i] = i; + + // std::cout << "launching..." << std::endl; + hipLaunchKernelGGL(kernel, 128, 64, 0, 0, data, elem_count); + + // std::cout << "syncing..." << std::endl; + HIP_API_CALL(hipDeviceSynchronize()); + + // std::cout << "checking..." << std::endl; + for(size_t i = 0; i < elem_count; ++i) + { + if(data[i] != (i * 2)) + { + auto msg = std::stringstream{}; + msg << "GPU computed value at " << i << " in iteration " << iter + << " is incorrect. Expected " << (i * 2) << ", found " << data[i]; + throw std::runtime_error{msg.str()}; + } + } + + std::cout << "Iteration " << std::setw(2) << iter << ": correct\n" << std::flush; + } + + HIP_API_CALL(hipDeviceSynchronize()); + + return 0; +} diff --git a/tests/common/defines.hpp b/tests/common/defines.hpp index 36c7c597ab..26c2e55fe4 100644 --- a/tests/common/defines.hpp +++ b/tests/common/defines.hpp @@ -33,8 +33,8 @@ << " failed with error code " << status_name << " (" << CHECKSTATUS \ << "): " << status_msg << std::endl; \ std::stringstream errmsg{}; \ - errmsg << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] " << msg " failure (" \ - << status_name << ": " << status_msg << ")"; \ + errmsg << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] " << msg \ + << " failure (" << status_name << ": " << status_msg << ")"; \ throw std::runtime_error(errmsg.str()); \ } \ } diff --git a/tests/common/serialization.hpp b/tests/common/serialization.hpp index 33d7177002..10d3aab622 100644 --- a/tests/common/serialization.hpp +++ b/tests/common/serialization.hpp @@ -391,6 +391,90 @@ save(ArchiveT& ar, rocprofiler_buffer_tracing_memory_copy_record_t data) SAVE_DATA_FIELD(src_agent_id); } +template +void +save(ArchiveT& ar, const rocprofiler_buffer_tracing_page_migration_record_t& data) +{ + SAVE_DATA_FIELD(size); + SAVE_DATA_FIELD(kind); + SAVE_DATA_FIELD(operation); + SAVE_DATA_FIELD(start_timestamp); + SAVE_DATA_FIELD(end_timestamp); + SAVE_DATA_FIELD(pid); + + switch(data.operation) + { + case ROCPROFILER_PAGE_MIGRATION_PAGE_FAULT: + { + ar(make_nvp("page_fault", data.page_fault)); + break; + } + case ROCPROFILER_PAGE_MIGRATION_PAGE_MIGRATE: + { + ar(make_nvp("page_migrate", data.page_migrate)); + break; + } + case ROCPROFILER_PAGE_MIGRATION_QUEUE_SUSPEND: + { + ar(make_nvp("queue_suspend", data.queue_suspend)); + break; + } + case ROCPROFILER_PAGE_MIGRATION_UNMAP_FROM_GPU: + { + ar(make_nvp("unmap_from_gpu", data.unmap_from_gpu)); + break; + } + case ROCPROFILER_PAGE_MIGRATION_NONE: + case ROCPROFILER_PAGE_MIGRATION_LAST: + { + throw std::runtime_error{"unsupported page migration operation type"}; + break; + } + } +} + +template +void +save(ArchiveT& ar, const rocprofiler_buffer_tracing_page_migration_page_fault_record_t& data) +{ + SAVE_DATA_FIELD(node_id); + SAVE_DATA_FIELD(address); + SAVE_DATA_FIELD(read_fault); + SAVE_DATA_FIELD(migrated); +} + +template +void +save(ArchiveT& ar, const rocprofiler_buffer_tracing_page_migration_page_migrate_record_t& data) +{ + SAVE_DATA_FIELD(start_addr); + SAVE_DATA_FIELD(end_addr); + SAVE_DATA_FIELD(from_node); + SAVE_DATA_FIELD(to_node); + SAVE_DATA_FIELD(prefetch_node); + SAVE_DATA_FIELD(preferred_node); + SAVE_DATA_FIELD(trigger); +} + +template +void +save(ArchiveT& ar, const rocprofiler_buffer_tracing_page_migration_queue_suspend_record_t& data) +{ + SAVE_DATA_FIELD(node_id); + SAVE_DATA_FIELD(trigger); + SAVE_DATA_FIELD(rescheduled); +} + +template +void +save(ArchiveT& ar, const rocprofiler_buffer_tracing_page_migration_unmap_from_gpu_record_t& data) +{ + SAVE_DATA_FIELD(node_id); + SAVE_DATA_FIELD(start_addr); + SAVE_DATA_FIELD(end_addr); + SAVE_DATA_FIELD(trigger); +} + template void save(ArchiveT& ar, rocprofiler_buffer_tracing_scratch_memory_record_t data) diff --git a/tests/page-migration/CMakeLists.txt b/tests/page-migration/CMakeLists.txt new file mode 100644 index 0000000000..accbd909b6 --- /dev/null +++ b/tests/page-migration/CMakeLists.txt @@ -0,0 +1,65 @@ +# +# +# +cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR) + +project( + rocprofiler-tests-page-migration + LANGUAGES CXX + VERSION 0.0.0) + +find_package(rocprofiler-sdk REQUIRED) + +if(ROCPROFILER_MEMCHECK_PRELOAD_ENV) + set(PRELOAD_ENV + "${ROCPROFILER_MEMCHECK_PRELOAD_ENV}:$") +else() + set(PRELOAD_ENV "LD_PRELOAD=$") +endif() + +add_test(NAME test-page-migration-execute COMMAND $) + +set(page-migration-env + "${PRELOAD_ENV}" + "ROCPROFILER_TOOL_OUTPUT_FILE=page-migration-test.json" + "LD_LIBRARY_PATH=$:$ENV{LD_LIBRARY_PATH}" + ) + +set_tests_properties( + test-page-migration-execute + PROPERTIES TIMEOUT + 45 + LABELS + "integration-tests" + ENVIRONMENT + "${page-migration-env}" + FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}" + SKIP_REGULAR_EXPRESSION + "KFD does not support SVM event reporting" + WORKING_DIRECTORY + ${CMAKE_CURRENT_BINARY_DIR}) + +foreach(FILENAME validate.py pytest.ini conftest.py) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME} + ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME} COPYONLY) +endforeach() + +add_test(NAME test-page-migration-validate + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/validate.py --input + ${CMAKE_CURRENT_BINARY_DIR}/page-migration-test.json) + +set_tests_properties( + test-page-migration-validate + PROPERTIES TIMEOUT + 45 + LABELS + "integration-tests" + DEPENDS + test-page-migration-execute + FAIL_REGULAR_EXPRESSION + "${ROCPROFILER_DEFAULT_FAIL_REGEX}" + SKIP_REGULAR_EXPRESSION + "KFD does not support SVM event reporting" + WORKING_DIRECTORY + ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/tests/page-migration/conftest.py b/tests/page-migration/conftest.py new file mode 100644 index 0000000000..15b059dc42 --- /dev/null +++ b/tests/page-migration/conftest.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import json +import pytest + + +def pytest_addoption(parser): + parser.addoption( + "--input", + action="store", + default="page-migration-test.json", + help="Input JSON", + ) + + +@pytest.fixture +def input_data(request): + filename = request.config.getoption("--input") + data = None + with open(filename, "r") as inp: + data = json.load(inp) + + if data["rocprofiler-sdk-json-tool"]["metadata"]["validate_page_migration"] is False: + return pytest.skip( + "Skipping test because KFD does not support SVM event reporting" + ) + return data diff --git a/tests/page-migration/pytest.ini b/tests/page-migration/pytest.ini new file mode 100644 index 0000000000..eb3f82f5cd --- /dev/null +++ b/tests/page-migration/pytest.ini @@ -0,0 +1,4 @@ + +[pytest] +addopts = --durations=20 -rA -s -vv +testpaths = validate.py diff --git a/tests/page-migration/validate.py b/tests/page-migration/validate.py new file mode 100644 index 0000000000..1f831ea313 --- /dev/null +++ b/tests/page-migration/validate.py @@ -0,0 +1,344 @@ +#!/usr/bin/env python3 + +from collections import defaultdict +import os +import sys +import pytest + + +# helper function +def node_exists(name, data, min_len=1): + assert name in data + assert data[name] is not None + if isinstance(data[name], (list, tuple, dict, set)): + assert len(data[name]) >= min_len + + +def to_dict(key_values): + a = defaultdict() + for kv in key_values: + a[kv["key"]] = kv["value"] + return a + + +def op_name(op_name, record): + found_op = False + op_key = None + + for kind_node in record["names"]["kind_names"]: + if kind_node["value"] == op_name: + op_key = kind_node["key"] + + for op_node in record["names"]["operation_names"]: + if op_node["key"] == op_key: + return op_key, to_dict(op_node["value"]) + + +def dict_from_value_key(d): + ret_d = defaultdict() + + for k, v in d.items(): + assert v not in ret_d + ret_d[v] = k + return ret_d + + +def sort_by_timestamp(lines): + timestamp_line_map = {} + + for log_line in lines: + timestamp = log_line.split(" ")[1] + timestamp_line_map[timestamp] = log_line + + timestamps_sorted = sorted([l.split(" ")[1] for l in lines]) + return timestamps_sorted, timestamp_line_map + + +# ------------------------------ Tests ------------------------------ # + + +def test_data_structure(input_data): + """verify minimum amount of expected data is present""" + data = input_data + + node_exists("rocprofiler-sdk-json-tool", data) + + sdk_data = data["rocprofiler-sdk-json-tool"] + + node_exists("metadata", sdk_data) + node_exists("pid", sdk_data["metadata"]) + node_exists("main_tid", sdk_data["metadata"]) + node_exists("init_time", sdk_data["metadata"]) + node_exists("fini_time", sdk_data["metadata"]) + node_exists("validate_page_migration", sdk_data["metadata"]) + + assert sdk_data["metadata"]["validate_page_migration"] is True + + node_exists("agents", sdk_data) + node_exists("call_stack", sdk_data) + node_exists("callback_records", sdk_data) + node_exists("buffer_records", sdk_data) + + node_exists("names", sdk_data["callback_records"]) + node_exists("code_objects", sdk_data["callback_records"]) + node_exists("kernel_symbols", sdk_data["callback_records"]) + node_exists("hsa_api_traces", sdk_data["callback_records"]) + node_exists("hip_api_traces", sdk_data["callback_records"], 0) + node_exists("marker_api_traces", sdk_data["callback_records"], 0) + + node_exists("names", sdk_data["buffer_records"]) + node_exists("kernel_dispatches", sdk_data["buffer_records"]) + node_exists("memory_copies", sdk_data["buffer_records"], 0) + node_exists("hsa_api_traces", sdk_data["buffer_records"]) + node_exists("hip_api_traces", sdk_data["buffer_records"], 0) + node_exists("marker_api_traces", sdk_data["buffer_records"], 0) + node_exists("retired_correlation_ids", sdk_data["buffer_records"]) + node_exists("page_migration", sdk_data["buffer_records"]) + + +def test_timestamps(input_data): + data = input_data + sdk_data = data["rocprofiler-sdk-json-tool"] + + cb_start = {} + cb_end = {} + for titr in ["hsa_api_traces", "marker_api_traces", "hip_api_traces"]: + for itr in sdk_data["callback_records"][titr]: + cid = itr["record"]["correlation_id"]["internal"] + phase = itr["record"]["phase"] + if phase == 1: + cb_start[cid] = itr["timestamp"] + elif phase == 2: + cb_end[cid] = itr["timestamp"] + assert cb_start[cid] <= itr["timestamp"] + else: + assert phase == 1 or phase == 2 + + for itr in sdk_data["buffer_records"][titr]: + assert itr["start_timestamp"] <= itr["end_timestamp"] + + for titr in ["kernel_dispatches", "memory_copies"]: + for itr in sdk_data["buffer_records"][titr]: + assert itr["start_timestamp"] < itr["end_timestamp"] + assert itr["correlation_id"]["internal"] > 0 + assert itr["correlation_id"]["external"] > 0 + assert sdk_data["metadata"]["init_time"] < itr["start_timestamp"] + assert sdk_data["metadata"]["init_time"] < itr["end_timestamp"] + assert sdk_data["metadata"]["fini_time"] > itr["start_timestamp"] + assert sdk_data["metadata"]["fini_time"] > itr["end_timestamp"] + + # api_start = cb_start[itr["correlation_id"]["internal"]] + # api_end = cb_end[itr["correlation_id"]["internal"]] + # assert api_start < itr["start_timestamp"] + # assert api_end <= itr["end_timestamp"] + + +def test_internal_correlation_ids(input_data): + data = input_data + sdk_data = data["rocprofiler-sdk-json-tool"] + + api_corr_ids = [] + for titr in ["hsa_api_traces", "marker_api_traces", "hip_api_traces"]: + for itr in sdk_data["callback_records"][titr]: + api_corr_ids.append(itr["record"]["correlation_id"]["internal"]) + + for itr in sdk_data["buffer_records"][titr]: + api_corr_ids.append(itr["correlation_id"]["internal"]) + + api_corr_ids_sorted = sorted(api_corr_ids) + api_corr_ids_unique = list(set(api_corr_ids)) + + for itr in sdk_data["buffer_records"]["kernel_dispatches"]: + assert itr["correlation_id"]["internal"] in api_corr_ids_unique + + for itr in sdk_data["buffer_records"]["memory_copies"]: + assert itr["correlation_id"]["internal"] in api_corr_ids_unique + + len_corr_id_unq = len(api_corr_ids_unique) + assert len(api_corr_ids) != len_corr_id_unq + assert max(api_corr_ids_sorted) == len_corr_id_unq + + +def test_external_correlation_ids(input_data): + data = input_data + sdk_data = data["rocprofiler-sdk-json-tool"] + + extern_corr_ids = [] + for titr in ["hsa_api_traces", "marker_api_traces", "hip_api_traces"]: + for itr in sdk_data["callback_records"][titr]: + assert itr["record"]["correlation_id"]["external"] > 0 + assert ( + itr["record"]["thread_id"] == itr["record"]["correlation_id"]["external"] + ) + extern_corr_ids.append(itr["record"]["correlation_id"]["external"]) + + extern_corr_ids = list(set(sorted(extern_corr_ids))) + for titr in ["hsa_api_traces", "marker_api_traces", "hip_api_traces"]: + for itr in sdk_data["buffer_records"][titr]: + assert itr["correlation_id"]["external"] > 0 + assert itr["thread_id"] == itr["correlation_id"]["external"] + assert itr["thread_id"] in extern_corr_ids + assert itr["correlation_id"]["external"] in extern_corr_ids + + for itr in sdk_data["buffer_records"]["kernel_dispatches"]: + assert itr["correlation_id"]["external"] > 0 + assert itr["correlation_id"]["external"] in extern_corr_ids + + for itr in sdk_data["buffer_records"]["memory_copies"]: + assert itr["correlation_id"]["external"] > 0 + assert itr["correlation_id"]["external"] in extern_corr_ids + + +def test_kernel_ids(input_data): + data = input_data + sdk_data = data["rocprofiler-sdk-json-tool"] + + symbol_info = {} + for itr in sdk_data["callback_records"]["kernel_symbols"]: + phase = itr["record"]["phase"] + payload = itr["payload"] + kern_id = payload["kernel_id"] + + assert phase == 1 or phase == 2 + assert kern_id > 0 + if phase == 1: + assert len(payload["kernel_name"]) > 0 + symbol_info[kern_id] = payload + elif phase == 2: + assert payload["kernel_id"] in symbol_info.keys() + assert payload["kernel_name"] == symbol_info[kern_id]["kernel_name"] + + for itr in sdk_data["buffer_records"]["kernel_dispatches"]: + assert itr["kernel_id"] in symbol_info.keys() + + +def test_retired_correlation_ids(input_data): + data = input_data + sdk_data = data["rocprofiler-sdk-json-tool"] + + def _sort_dict(inp): + return dict(sorted(inp.items())) + + api_corr_ids = {} + for titr in ["hsa_api_traces", "marker_api_traces", "hip_api_traces"]: + for itr in sdk_data["buffer_records"][titr]: + corr_id = itr["correlation_id"]["internal"] + assert corr_id not in api_corr_ids.keys() + api_corr_ids[corr_id] = itr + + async_corr_ids = {} + for titr in ["kernel_dispatches", "memory_copies"]: + for itr in sdk_data["buffer_records"][titr]: + corr_id = itr["correlation_id"]["internal"] + assert corr_id not in async_corr_ids.keys() + async_corr_ids[corr_id] = itr + + retired_corr_ids = {} + for itr in sdk_data["buffer_records"]["retired_correlation_ids"]: + corr_id = itr["internal_correlation_id"] + assert corr_id not in retired_corr_ids.keys() + retired_corr_ids[corr_id] = itr + + api_corr_ids = _sort_dict(api_corr_ids) + async_corr_ids = _sort_dict(async_corr_ids) + retired_corr_ids = _sort_dict(retired_corr_ids) + + for cid, itr in async_corr_ids.items(): + assert cid in retired_corr_ids.keys() + ts = retired_corr_ids[cid]["timestamp"] + assert (ts - itr["end_timestamp"]) > 0, f"correlation-id: {cid}, data: {itr}" + + for cid, itr in api_corr_ids.items(): + assert cid in retired_corr_ids.keys() + ts = retired_corr_ids[cid]["timestamp"] + assert (ts - itr["end_timestamp"]) > 0, f"correlation-id: {cid}, data: {itr}" + + assert len(api_corr_ids.keys()) == (len(retired_corr_ids.keys())) + + +def get_allocated_pages(callback_records): + # Get how many pages we allocated + hip_api_traces = callback_records["hip_api_traces"] + _, op_dict = op_name("HIP_RUNTIME_API", callback_records) + op_key = [k for k, v in op_dict.items() if v == "hipHostRegister"][0] + + host_register_record = [] + for r in hip_api_traces: + if ( + r["record"]["operation"] == op_key + and "sizeBytes" in r["args"] + and "hostPtr" in r["args"] + ): + host_register_record.append(r) + + assert len(host_register_record) == 1 + alloc_size = int(host_register_record[0]["args"]["sizeBytes"], 10) + start_addr = int(host_register_record[0]["args"]["hostPtr"], 16) + end_addr = start_addr + alloc_size + + return start_addr, end_addr, alloc_size + + +def test_page_migration_data(input_data): + data = input_data + sdk_data = data["rocprofiler-sdk-json-tool"] + buffer_records = sdk_data["buffer_records"] + callback_records = sdk_data["callback_records"] + page_migtation_buffers = buffer_records["page_migration"] + + bf_op_id, bf_op_names = op_name("PAGE_MIGRATION", buffer_records) + assert bf_op_names[0] == "NONE" + assert "PAGE_MIGRATE" in str(bf_op_names) + assert len(bf_op_names) == 5 + + node_ids = set(x["gpu_id"] for x in sdk_data["agents"]) + start_addr, end_addr, alloc_size = get_allocated_pages(callback_records) + + assert start_addr < end_addr and start_addr + alloc_size == end_addr + assert int(alloc_size) == 16 * 4096 # We allocated 16 pages in the test + + # PID must be same + assert len(set(r["pid"] for r in page_migtation_buffers)) == 1 + + for r in page_migtation_buffers: + op = r["operation"] + + assert r["size"] == 136 + assert r["kind"] == bf_op_id + assert op != 0 and bf_op_names[op] != "NONE" + assert bf_op_names[op].lower() in r + + if "page_migrate" in r: + assert r["page_migrate"]["from_node"] in node_ids + assert r["page_migrate"]["to_node"] in node_ids + assert r["page_migrate"]["prefetch_node"] in node_ids + assert r["page_migrate"]["preferred_node"] in node_ids + assert r["page_migrate"]["trigger"] >= 0 + + if "queue_suspend" in r: + assert r["queue_suspend"]["trigger"] >= 0 + assert r["queue_suspend"]["node_id"] in node_ids + + if "unmap_from_gpu" in r: + assert r["unmap_from_gpu"]["trigger"] >= 0 + # unmap is "instantaneous" + assert 0 < r["start_timestamp"] == r["end_timestamp"] + else: + assert 0 < r["start_timestamp"] < r["end_timestamp"] + + # Check for events with our page + for r in page_migtation_buffers: + + if "page_migrate" in r and r["page_migrate"]["start_addr"] == start_addr: + assert end_addr == r["page_migrate"]["end_addr"] + + if "unmap_from_gpu" in r and r["unmap_from_gpu"]["start_addr"] == start_addr: + assert end_addr == r["unmap_from_gpu"]["end_addr"] + + # TODO: Check if a migrate a->b is paired up with b->a + # It may not always be reported towards app finalization + + +if __name__ == "__main__": + exit_code = pytest.main(["-x", __file__] + sys.argv[1:]) + sys.exit(exit_code) diff --git a/tests/tools/json-tool.cpp b/tests/tools/json-tool.cpp index b0d75c1a36..9cdd65dca7 100644 --- a/tests/tools/json-tool.cpp +++ b/tests/tools/json-tool.cpp @@ -312,6 +312,7 @@ get_buffer_tracing_names() ROCPROFILER_BUFFER_TRACING_MARKER_NAME_API, ROCPROFILER_BUFFER_TRACING_MEMORY_COPY, ROCPROFILER_BUFFER_TRACING_SCRATCH_MEMORY, + ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION, }; auto cb_name_info = buffer_name_info{}; @@ -758,6 +759,7 @@ auto hip_api_bf_records = std::deque{}; auto memory_copy_records = std::deque{}; auto scratch_memory_records = std::deque{}; +auto page_migration_records = std::deque{}; auto corr_id_retire_records = std::deque{}; @@ -842,6 +844,13 @@ tool_tracing_buffered(rocprofiler_context_id_t /*context*/, scratch_memory_records.emplace_back(*record); } + else if(header->kind == ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION) + { + auto* record = static_cast( + header->payload); + + page_migration_records.emplace_back(*record); + } else if(header->kind == ROCPROFILER_BUFFER_TRACING_CORRELATION_ID_RETIREMENT) { auto* record = @@ -927,12 +936,14 @@ rocprofiler_context_id_t scratch_memory_ctx = {}; rocprofiler_context_id_t corr_id_retire_ctx = {}; rocprofiler_context_id_t kernel_dispatch_callback_ctx = {}; rocprofiler_context_id_t kernel_dispatch_buffered_ctx = {}; +rocprofiler_context_id_t page_migration_ctx = {}; // buffers rocprofiler_buffer_id_t hsa_api_buffered_buffer = {}; rocprofiler_buffer_id_t hip_api_buffered_buffer = {}; rocprofiler_buffer_id_t marker_api_buffered_buffer = {}; rocprofiler_buffer_id_t kernel_dispatch_buffer = {}; rocprofiler_buffer_id_t memory_copy_buffer = {}; +rocprofiler_buffer_id_t page_migration_buffer = {}; rocprofiler_buffer_id_t counter_collection_buffer = {}; rocprofiler_buffer_id_t scratch_memory_buffer = {}; rocprofiler_buffer_id_t corr_id_retire_buffer = {}; @@ -948,25 +959,28 @@ auto contexts = std::unordered_map{ {"MARKER_API_BUFFERED", &marker_api_buffered_ctx}, {"KERNEL_DISPATCH_BUFFERED", &kernel_dispatch_buffered_ctx}, {"MEMORY_COPY", &memory_copy_ctx}, + {"PAGE_MIGRATION", &page_migration_ctx}, {"COUNTER_COLLECTION", &counter_collection_ctx}, {"SCRATCH_MEMORY", &scratch_memory_ctx}, {"CORRELATION_ID_RETIREMENT", &corr_id_retire_ctx}, }; -auto buffers = std::array{&hsa_api_buffered_buffer, +auto buffers = std::array{&hsa_api_buffered_buffer, &hip_api_buffered_buffer, &marker_api_buffered_buffer, &kernel_dispatch_buffer, &memory_copy_buffer, &scratch_memory_buffer, + &page_migration_buffer, &counter_collection_buffer, &corr_id_retire_buffer}; auto agents = std::vector{}; -rocprofiler_timestamp_t init_time = 0; -rocprofiler_timestamp_t fini_time = 0; -rocprofiler_thread_id_t main_tid = 0; +rocprofiler_timestamp_t init_time = 0; +rocprofiler_timestamp_t fini_time = 0; +rocprofiler_thread_id_t main_tid = 0; +auto page_migration_status = ROCPROFILER_STATUS_SUCCESS; int tool_init(rocprofiler_client_finalize_t fini_func, void* tool_data) @@ -1142,6 +1156,15 @@ tool_init(rocprofiler_client_finalize_t fini_func, void* tool_data) &scratch_memory_buffer), "buffer creation"); + ROCPROFILER_CALL(rocprofiler_create_buffer(page_migration_ctx, + buffer_size, + watermark, + ROCPROFILER_BUFFER_POLICY_LOSSLESS, + tool_tracing_buffered, + tool_data, + &page_migration_buffer), + "buffer creation"); + ROCPROFILER_CALL(rocprofiler_create_buffer(corr_id_retire_ctx, buffer_size, watermark, @@ -1226,6 +1249,23 @@ tool_init(rocprofiler_client_finalize_t fini_func, void* tool_data) scratch_memory_buffer), "buffer tracing service for scratch memory configure"); + { + page_migration_status = + rocprofiler_configure_buffer_tracing_service(page_migration_ctx, + ROCPROFILER_BUFFER_TRACING_PAGE_MIGRATION, + nullptr, + 0, + page_migration_buffer); + + constexpr auto message = "page migration service for memory copy configure"; + if(page_migration_status == ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL) + std::cerr << message + << " failed: " << rocprofiler_get_status_string(page_migration_status) + << std::endl; + else + ROCPROFILER_CALL(page_migration_status, message); + } + ROCPROFILER_CALL(rocprofiler_configure_buffer_tracing_service( corr_id_retire_ctx, ROCPROFILER_BUFFER_TRACING_CORRELATION_ID_RETIREMENT, @@ -1382,6 +1422,7 @@ tool_fini(void* tool_data) << ", kernel_dispatch_bf_records=" << kernel_dispatch_bf_records.size() << ", memory_copy_records=" << memory_copy_records.size() << ", scratch_memory_records=" << scratch_memory_records.size() + << ", page_migration=" << page_migration_records.size() << ", hsa_api_bf_records=" << hsa_api_bf_records.size() << ", hip_api_bf_records=" << hip_api_bf_records.size() << ", marker_api_bf_records=" << marker_api_bf_records.size() @@ -1444,6 +1485,8 @@ write_json(call_stack_t* _call_stack) auto json_ar = JSONOutputArchive{*ofs, json_opts}; auto buffer_name_info = get_buffer_tracing_names(); auto callback_name_info = get_callback_tracing_names(); + auto validate_page_migration = + (page_migration_status != ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_KERNEL); json_ar.setNextName("rocprofiler-sdk-json-tool"); json_ar.startNode(); @@ -1454,6 +1497,7 @@ write_json(call_stack_t* _call_stack) json_ar(cereal::make_nvp("main_tid", main_tid)); json_ar(cereal::make_nvp("init_time", init_time)); json_ar(cereal::make_nvp("fini_time", fini_time)); + json_ar(cereal::make_nvp("validate_page_migration", validate_page_migration)); json_ar.finishNode(); json_ar(cereal::make_nvp("agents", agents)); @@ -1487,6 +1531,7 @@ write_json(call_stack_t* _call_stack) json_ar(cereal::make_nvp("kernel_dispatches", kernel_dispatch_bf_records)); json_ar(cereal::make_nvp("memory_copies", memory_copy_records)); json_ar(cereal::make_nvp("scratch_memory_traces", scratch_memory_records)); + json_ar(cereal::make_nvp("page_migration", page_migration_records)); json_ar(cereal::make_nvp("hsa_api_traces", hsa_api_bf_records)); json_ar(cereal::make_nvp("hip_api_traces", hip_api_bf_records)); json_ar(cereal::make_nvp("marker_api_traces", marker_api_bf_records));