From 87ffbd27f4121f71e5c8e59b19b4e932eb309e8a Mon Sep 17 00:00:00 2001 From: Laurent Morichetti Date: Thu, 8 Sep 2022 18:30:54 -0700 Subject: [PATCH] Fix hsa_support::timestamp_ns if HSA is not yet initialized Default to the HSA runtime's hsa_system_get_info if the saved HSA functions table is not yet initialized. Change-Id: I3659095a5ad662f7ca8b0d92bd035901c6d66bb0 --- src/roctracer/hsa_support.cpp | 23 +++-- test/CMakeLists.txt | 8 ++ test/directed/dlopen.cpp | 94 +++++++++++++++++++ test/golden_traces/tests_trace_cmp_levels.txt | 1 + test/run.sh | 1 + 5 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 test/directed/dlopen.cpp diff --git a/src/roctracer/hsa_support.cpp b/src/roctracer/hsa_support.cpp index c92e273516..e0e6bd1a17 100644 --- a/src/roctracer/hsa_support.cpp +++ b/src/roctracer/hsa_support.cpp @@ -490,21 +490,24 @@ hsa_status_t MemoryASyncCopyRectIntercept(const hsa_pitched_ptr_t* dst, } // namespace roctracer_timestamp_t timestamp_ns() { + // If the HSA intercept is installed, then use the "original" 'hsa_system_get_info' function to + // avoid reporting calls for internal use of the HSA API by the tracer. + auto hsa_system_get_info_fn = saved_core_api.hsa_system_get_info_fn; + + // If the HSA intercept is not installed, use the default 'hsa_system_get_info'. + if (hsa_system_get_info_fn == nullptr) hsa_system_get_info_fn = hsa_system_get_info; + uint64_t sysclock; - - assert(saved_core_api.hsa_system_get_info_fn != nullptr && "HSA intercept is not active"); - - if (hsa_status_t status = - saved_core_api.hsa_system_get_info_fn(HSA_SYSTEM_INFO_TIMESTAMP, &sysclock); + if (hsa_status_t status = hsa_system_get_info_fn(HSA_SYSTEM_INFO_TIMESTAMP, &sysclock); status == HSA_STATUS_ERROR_NOT_INITIALIZED) return 0; else if (status != HSA_STATUS_SUCCESS) fatal("hsa_system_get_info failed"); - static uint64_t sysclock_period = []() { + static uint64_t sysclock_period = [&]() { uint64_t sysclock_hz = 0; - if (hsa_status_t status = saved_core_api.hsa_system_get_info_fn( - HSA_SYSTEM_INFO_TIMESTAMP_FREQUENCY, &sysclock_hz); + if (hsa_status_t status = + hsa_system_get_info_fn(HSA_SYSTEM_INFO_TIMESTAMP_FREQUENCY, &sysclock_hz); status != HSA_STATUS_SUCCESS) fatal("hsa_system_get_info failed"); @@ -575,6 +578,10 @@ void Finalize() { if (hsa_status_t status = saved_amd_ext_api.hsa_amd_profiling_async_copy_enable_fn(false); status != HSA_STATUS_SUCCESS) assert(!"hsa_amd_profiling_async_copy_enable failed"); + + memset(&saved_core_api, '\0', sizeof(saved_core_api)); + memset(&saved_amd_ext_api, '\0', sizeof(saved_amd_ext_api)); + memset(&hsa_loader_api, '\0', sizeof(hsa_loader_api)); } const char* GetApiName(uint32_t id) { return detail::GetApiName(id); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 287a7e3a65..c7c5903b88 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -20,6 +20,8 @@ ## IN THE SOFTWARE. ################################################################################ +get_property(HSA_RUNTIME_INCLUDE_DIRECTORIES TARGET hsa-runtime64::hsa-runtime64 PROPERTY INTERFACE_INCLUDE_DIRECTORIES) + # Set the HIP language runtime link flags as FindHIP does not set them. set(CMAKE_EXECUTABLE_RUNTIME_HIP_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG}) set(CMAKE_EXECUTABLE_RUNTIME_HIP_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP}) @@ -139,6 +141,12 @@ hip_add_executable(multi_pool_activities directed/multi_pool_activities.cpp) target_link_libraries(multi_pool_activities roctracer) add_dependencies(mytest multi_pool_activities) +## Build the dlopen test +add_executable(dlopen directed/dlopen.cpp) +target_include_directories(dlopen PRIVATE ${PROJECT_SOURCE_DIR}/inc ${HSA_RUNTIME_INCLUDE_DIRECTORIES}) +target_link_libraries(dlopen dl) +add_dependencies(mytest dlopen) + ## Copy the golden traces and test scripts configure_file(run.sh ${PROJECT_BINARY_DIR} COPYONLY) execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink run.sh ${PROJECT_BINARY_DIR}/run_ci.sh) diff --git a/test/directed/dlopen.cpp b/test/directed/dlopen.cpp new file mode 100644 index 0000000000..7a112c52ac --- /dev/null +++ b/test/directed/dlopen.cpp @@ -0,0 +1,94 @@ +/* Copyright (c) 2022 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 + 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 "roctracer.h" + +#include +#include + +#include + +using get_timestamp_t = decltype(roctracer_get_timestamp); +using hsa_init_t = decltype(hsa_init); +using hsa_shut_down_t = decltype(hsa_shut_down); + +int main() { + // CASE 1: HSA is not loaded. + // + { + void* tracer_library = dlopen("libroctracer64.so", RTLD_LAZY); + assert(tracer_library != nullptr); + + auto* get_timestamp = + reinterpret_cast(dlsym(tracer_library, "roctracer_get_timestamp")); + assert(get_timestamp != nullptr); + + roctracer_timestamp_t timestamp; + (*get_timestamp)(×tamp); + dlclose(tracer_library); + } + + // CASE 2 Load the roctracer after hsa_init(). + // + void* hsa_library = dlopen("libhsa-runtime64.so.1", RTLD_LAZY); + assert(hsa_library != nullptr); + + auto* hsa_init = reinterpret_cast(dlsym(hsa_library, "hsa_init")); + auto* hsa_shut_down = reinterpret_cast(dlsym(hsa_library, "hsa_shut_down")); + assert(hsa_init != nullptr && hsa_shut_down != nullptr); + + { + (*hsa_init)(); + + void* tracer_library = dlopen("libroctracer64.so", RTLD_LAZY); + assert(tracer_library != nullptr); + + auto* get_timestamp = + reinterpret_cast(dlsym(tracer_library, "roctracer_get_timestamp")); + assert(get_timestamp != nullptr); + + roctracer_timestamp_t timestamp; + (*get_timestamp)(×tamp); + + dlclose(tracer_library); + (*hsa_shut_down)(); + } + + // CASE 3: Load and use the roctracer before hsa_init(). + // + { + void* tracer_library = dlopen("libroctracer64.so", RTLD_LAZY); + assert(tracer_library != nullptr); + + auto* get_timestamp = + reinterpret_cast(dlsym(tracer_library, "roctracer_get_timestamp")); + assert(get_timestamp != nullptr); + + roctracer_timestamp_t timestamp; + (*get_timestamp)(×tamp); + + (*hsa_init)(); + (*hsa_shut_down)(); + dlclose(tracer_library); + } + + return 0; +} \ No newline at end of file diff --git a/test/golden_traces/tests_trace_cmp_levels.txt b/test/golden_traces/tests_trace_cmp_levels.txt index 5d191021bd..f6ceb02c68 100644 --- a/test/golden_traces/tests_trace_cmp_levels.txt +++ b/test/golden_traces/tests_trace_cmp_levels.txt @@ -21,3 +21,4 @@ activity_and_callback_trace --check-order .* multi_pool_activities_trace --check-order .* roctx_test_trace --check-count .* backward_compat_test_trace --check-none +dlopen --check-none \ No newline at end of file diff --git a/test/run.sh b/test/run.sh index f6f2e5d27c..7cd1cbd5d2 100755 --- a/test/run.sh +++ b/test/run.sh @@ -181,6 +181,7 @@ eval_test "directed TraceBuffer test" ./test/trace_buffer trace_buffer eval_test "directed MemoryPool test" ./test/memory_pool memory_pool eval_test "enable/disable callbacks and activities test" ./test/activity_and_callback activity_and_callback_trace eval_test "use multiple memory pools in HIP activities test" ./test/multi_pool_activities multi_pool_activities_trace +eval_test "Dynamically load the tracer library test" ./test/dlopen dlopen eval_test "backward compatibility tests" ./test/backward_compat_test backward_compat_test_trace