From 1afaf37f786b98b8f689b39692195ac02dddcaf5 Mon Sep 17 00:00:00 2001 From: Kacper Nowak Date: Wed, 2 Aug 2023 19:33:48 +0200 Subject: [PATCH] refactor: Unify GTPin initialization logic between APIs Add support for new GTPin loading logic in OCL path, similar to existing in L0 - invoking exposed, dedicated API call (OpenGTPinOCL). - Move logic to shared, including unit tests - Check whether instrumentation is required on context creation and if yes, make a call to OpenGTPinOCL function. Handle potential errors gracefully without exiting. Signed-off-by: Kacper Nowak --- level_zero/core/source/driver/driver.cpp | 7 ++- level_zero/tools/source/pin/CMakeLists.txt | 14 ----- .../tools/source/pin/linux/CMakeLists.txt | 18 ------ .../tools/source/pin/windows/CMakeLists.txt | 17 ------ .../test/unit_tests/sources/pin/test_pin.cpp | 56 ----------------- opencl/source/api/api.cpp | 2 + opencl/source/gtpin/gtpin_callbacks.cpp | 9 +++ opencl/source/gtpin/gtpin_init.cpp | 2 +- opencl/source/gtpin/gtpin_notify.h | 2 + opencl/source/platform/platform.cpp | 13 ++++ opencl/source/platform/platform.h | 2 + opencl/test/unit_test/gtpin/gtpin_tests.cpp | 60 +++++++++++++++++++ shared/source/CMakeLists.txt | 10 ++++ shared/source/pin/CMakeLists.txt | 14 +++++ shared/source/pin/linux/CMakeLists.txt | 13 ++++ .../source/pin/linux/os_pin.h | 9 +-- .../tools => shared}/source/pin/pin.cpp | 23 +++---- {level_zero/tools => shared}/source/pin/pin.h | 9 +-- shared/source/pin/windows/CMakeLists.txt | 13 ++++ .../source/pin/windows/os_pin.h | 9 +-- .../test/unit_test}/pin/CMakeLists.txt | 2 +- shared/test/unit_test/pin/test_pin.cpp | 56 +++++++++++++++++ 22 files changed, 218 insertions(+), 142 deletions(-) delete mode 100644 level_zero/tools/source/pin/CMakeLists.txt delete mode 100644 level_zero/tools/source/pin/linux/CMakeLists.txt delete mode 100644 level_zero/tools/source/pin/windows/CMakeLists.txt delete mode 100644 level_zero/tools/test/unit_tests/sources/pin/test_pin.cpp create mode 100644 shared/source/pin/CMakeLists.txt create mode 100644 shared/source/pin/linux/CMakeLists.txt rename {level_zero/tools => shared}/source/pin/linux/os_pin.h (69%) rename {level_zero/tools => shared}/source/pin/pin.cpp (65%) rename {level_zero/tools => shared}/source/pin/pin.h (75%) create mode 100644 shared/source/pin/windows/CMakeLists.txt rename {level_zero/tools => shared}/source/pin/windows/os_pin.h (70%) rename {level_zero/tools/test/unit_tests/sources => shared/test/unit_test}/pin/CMakeLists.txt (83%) create mode 100644 shared/test/unit_test/pin/test_pin.cpp diff --git a/level_zero/core/source/driver/driver.cpp b/level_zero/core/source/driver/driver.cpp index bd310641da..6a983702e8 100644 --- a/level_zero/core/source/driver/driver.cpp +++ b/level_zero/core/source/driver/driver.cpp @@ -11,12 +11,12 @@ #include "shared/source/execution_environment/execution_environment.h" #include "shared/source/os_interface/debug_env_reader.h" #include "shared/source/os_interface/device_factory.h" +#include "shared/source/pin/pin.h" #include "level_zero/core/source/device/device.h" #include "level_zero/core/source/driver/driver_handle_imp.h" #include "level_zero/core/source/driver/driver_imp.h" #include "level_zero/tools/source/metrics/metric.h" -#include "level_zero/tools/source/pin/pin.h" #include #include @@ -81,8 +81,9 @@ void DriverImp::initialize(ze_result_t *result) { } if ((*result == ZE_RESULT_SUCCESS) && envVariables.pin) { - *result = PinContext::init(); - if (*result != ZE_RESULT_SUCCESS) { + std::string gtpinFuncName{"OpenGTPin"}; + if (false == NEO::PinContext::init(gtpinFuncName)) { + *result = ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE; delete GlobalDriver; GlobalDriverHandle = nullptr; GlobalDriver = nullptr; diff --git a/level_zero/tools/source/pin/CMakeLists.txt b/level_zero/tools/source/pin/CMakeLists.txt deleted file mode 100644 index 5b66de486c..0000000000 --- a/level_zero/tools/source/pin/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -# -# Copyright (C) 2020-2023 Intel Corporation -# -# SPDX-License-Identifier: MIT -# - -target_sources(${L0_STATIC_LIB_NAME} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}/pin.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/pin.h -) - -add_subdirectories() diff --git a/level_zero/tools/source/pin/linux/CMakeLists.txt b/level_zero/tools/source/pin/linux/CMakeLists.txt deleted file mode 100644 index 53182bb5c4..0000000000 --- a/level_zero/tools/source/pin/linux/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -# -# Copyright (C) 2020-2023 Intel Corporation -# -# SPDX-License-Identifier: MIT -# - -if(UNIX) - target_sources(${L0_STATIC_LIB_NAME} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}/os_pin.h - ) - target_include_directories(${L0_STATIC_LIB_NAME} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR} - ) -endif() - diff --git a/level_zero/tools/source/pin/windows/CMakeLists.txt b/level_zero/tools/source/pin/windows/CMakeLists.txt deleted file mode 100644 index 432016da30..0000000000 --- a/level_zero/tools/source/pin/windows/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -# -# Copyright (C) 2020-2023 Intel Corporation -# -# SPDX-License-Identifier: MIT -# - -if(WIN32) - target_sources(${L0_STATIC_LIB_NAME} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - ${CMAKE_CURRENT_SOURCE_DIR}/os_pin.h - ) - target_include_directories(${L0_STATIC_LIB_NAME} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR} - ) -endif() diff --git a/level_zero/tools/test/unit_tests/sources/pin/test_pin.cpp b/level_zero/tools/test/unit_tests/sources/pin/test_pin.cpp deleted file mode 100644 index 3c5cc9ad6f..0000000000 --- a/level_zero/tools/test/unit_tests/sources/pin/test_pin.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2022-2023 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "shared/test/common/mocks/mock_os_library.h" - -#include "level_zero/tools/source/pin/pin.h" - -#include "gtest/gtest.h" - -namespace ult { - -TEST(PinInitializationTest, GivenValidLibraryPinContextInitSucceeds) { - uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { return 0; }; - auto newPtr = new MockOsLibrary(reinterpret_cast(openPinHandler), false); - MockOsLibrary::loadLibraryNewObject = newPtr; - L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load; - EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_SUCCESS); - L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load; - MockOsLibrary::loadLibraryNewObject = nullptr; - delete newPtr; -} - -TEST(PinInitializationTest, GivenBadLibraryNamePinContextInitFAILS) { - MockOsLibrary::loadLibraryNewObject = nullptr; - L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load; - EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE); - L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load; - MockOsLibrary::loadLibraryNewObject = nullptr; -} - -TEST(PinInitializationTest, GivenBadProcAddressPinContextInitFAILS) { - auto newPtr = new MockOsLibrary(nullptr, false); - MockOsLibrary::loadLibraryNewObject = newPtr; - L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load; - EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE); - L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load; - MockOsLibrary::loadLibraryNewObject = nullptr; - delete newPtr; -} - -TEST(PinInitializationTest, GivenBadPinHandlerPinContextInitFAILS) { - uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { return 1; }; - auto newPtr = new MockOsLibrary(reinterpret_cast(openPinHandler), false); - MockOsLibrary::loadLibraryNewObject = newPtr; - L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load; - EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE); - L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load; - MockOsLibrary::loadLibraryNewObject = nullptr; - delete newPtr; -} - -} // namespace ult diff --git a/opencl/source/api/api.cpp b/opencl/source/api/api.cpp index 4339b7c91b..21c335fd15 100644 --- a/opencl/source/api/api.cpp +++ b/opencl/source/api/api.cpp @@ -400,6 +400,7 @@ cl_context CL_API_CALL clCreateContext(const cl_context_properties *properties, size_t, void *), void *userData, cl_int *errcodeRet) { + gtPinTryNotifyInit(); TRACING_ENTER(ClCreateContext, &properties, &numDevices, &devices, &funcNotify, &userData, &errcodeRet); cl_int retVal = CL_SUCCESS; @@ -457,6 +458,7 @@ cl_context CL_API_CALL clCreateContextFromType(const cl_context_properties *prop size_t, void *), void *userData, cl_int *errcodeRet) { + gtPinTryNotifyInit(); TRACING_ENTER(ClCreateContextFromType, &properties, &deviceType, &funcNotify, &userData, &errcodeRet); cl_int retVal = CL_SUCCESS; API_ENTER(&retVal); diff --git a/opencl/source/gtpin/gtpin_callbacks.cpp b/opencl/source/gtpin/gtpin_callbacks.cpp index 6888f655f5..5821228255 100644 --- a/opencl/source/gtpin/gtpin_callbacks.cpp +++ b/opencl/source/gtpin/gtpin_callbacks.cpp @@ -21,6 +21,7 @@ #include "opencl/source/kernel/kernel.h" #include "opencl/source/kernel/multi_device_kernel.h" #include "opencl/source/mem_obj/buffer.h" +#include "opencl/source/platform/platform.h" #include "opencl/source/program/program.h" #include "CL/cl.h" @@ -281,4 +282,12 @@ void gtpinRemoveCommandQueue(void *pCmdQueue) { } } +void gtPinTryNotifyInit() { + if (platformsImpl->empty()) { + return; + } + auto &pPlatform = *(*platformsImpl)[0]; + pPlatform.tryNotifyGtpinInit(); +} + } // namespace NEO diff --git a/opencl/source/gtpin/gtpin_init.cpp b/opencl/source/gtpin/gtpin_init.cpp index 7846b864de..c7db845688 100644 --- a/opencl/source/gtpin/gtpin_init.cpp +++ b/opencl/source/gtpin/gtpin_init.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 Intel Corporation + * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * diff --git a/opencl/source/gtpin/gtpin_notify.h b/opencl/source/gtpin/gtpin_notify.h index 4e95a1da88..78517ae06f 100644 --- a/opencl/source/gtpin/gtpin_notify.h +++ b/opencl/source/gtpin/gtpin_notify.h @@ -28,4 +28,6 @@ inline bool gtpinIsGTPinInitialized() { return isGTPinInitialized; } void *gtpinGetIgcInit(); void gtpinSetIgcInit(void *pIgcInitPtr); void gtpinRemoveCommandQueue(void *pCmdQueue); + +void gtPinTryNotifyInit(); } // namespace NEO diff --git a/opencl/source/platform/platform.cpp b/opencl/source/platform/platform.cpp index c901ea61a6..067faebc13 100644 --- a/opencl/source/platform/platform.cpp +++ b/opencl/source/platform/platform.cpp @@ -17,6 +17,8 @@ #include "shared/source/helpers/debug_helpers.h" #include "shared/source/helpers/get_info.h" #include "shared/source/helpers/hw_info.h" +#include "shared/source/os_interface/debug_env_reader.h" +#include "shared/source/pin/pin.h" #include "shared/source/source_level_debugger/source_level_debugger.h" #include "opencl/source/api/api.h" @@ -185,6 +187,17 @@ bool Platform::initialize(std::vector> devices) { return true; } +void Platform::tryNotifyGtpinInit() { + auto notifyGTPin = []() { + EnvironmentVariableReader envReader; + if (envReader.getSetting("ZET_ENABLE_PROGRAM_INSTRUMENTATION", false)) { + const std::string gtpinFuncName{"OpenGTPinOCL"}; + PinContext::init(gtpinFuncName); + } + }; + std::call_once(this->oclInitGTPinOnce, notifyGTPin); +} + void Platform::fillGlobalDispatchTable() { sharingFactory.fillGlobalDispatchTable(); } diff --git a/opencl/source/platform/platform.h b/opencl/source/platform/platform.h index 35de9d4778..132e99387d 100644 --- a/opencl/source/platform/platform.h +++ b/opencl/source/platform/platform.h @@ -48,6 +48,7 @@ class Platform : public BaseObject<_cl_platform_id> { MOCKABLE_VIRTUAL bool initialize(std::vector> devices); bool isInitialized(); + void tryNotifyGtpinInit(); size_t getNumDevices() const; ClDevice **getClDevices(); @@ -71,6 +72,7 @@ class Platform : public BaseObject<_cl_platform_id> { ClDeviceVector clDevices; ExecutionEnvironment &executionEnvironment; std::once_flag initializeExtensionsWithVersionOnce; + std::once_flag oclInitGTPinOnce; }; extern std::vector> *platformsImpl; diff --git a/opencl/test/unit_test/gtpin/gtpin_tests.cpp b/opencl/test/unit_test/gtpin/gtpin_tests.cpp index b622009017..e03654791f 100644 --- a/opencl/test/unit_test/gtpin/gtpin_tests.cpp +++ b/opencl/test/unit_test/gtpin/gtpin_tests.cpp @@ -14,6 +14,7 @@ #include "shared/source/memory_manager/surface.h" #include "shared/source/memory_manager/unified_memory_manager.h" #include "shared/source/os_interface/os_context.h" +#include "shared/source/pin/pin.h" #include "shared/test/common/device_binary_format/patchtokens_tests.h" #include "shared/test/common/fixtures/memory_management_fixture.h" #include "shared/test/common/helpers/debug_manager_state_restore.h" @@ -22,8 +23,10 @@ #include "shared/test/common/helpers/variable_backup.h" #include "shared/test/common/mocks/mock_cpu_page_fault_manager.h" #include "shared/test/common/mocks/mock_device.h" +#include "shared/test/common/mocks/mock_io_functions.h" #include "shared/test/common/mocks/mock_memory_manager.h" #include "shared/test/common/mocks/mock_modules_zebin.h" +#include "shared/test/common/mocks/mock_os_library.h" #include "shared/test/common/test_macros/hw_test.h" #include "opencl/source/api/api.h" @@ -2503,4 +2506,61 @@ TEST_F(GTPinTests, givenInitializedGTPinInterfaceWhenGtpinRemoveCommandQueueIsCa EXPECT_EQ(0u, kernelExecQueue.size()); } +uint32_t gtpinInitTimesCalled = 0u; + +TEST(GTPinInitNotifyTests, givenAvailablePlatformsAndNoEnvironmentVariableSetWhenTryingToNotifyGtpinInitializationThenGtpinDoesNotGetNotified) { + platformsImpl->clear(); + constructPlatform(); + ASSERT_FALSE(platformsImpl->empty()); + + VariableBackup gtpinCounterBackup(>pinInitTimesCalled, 0u); + uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { gtpinInitTimesCalled++; return 0; }; + MockOsLibrary mockLibraryObject(reinterpret_cast(openPinHandler), false); + MockOsLibrary::loadLibraryNewObject = &mockLibraryObject; + NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load; + + gtPinTryNotifyInit(); + EXPECT_EQ(0u, gtpinInitTimesCalled); + platformsImpl->clear(); +} + +TEST(GTPinInitNotifyTests, givenNoPlatformsAvailableAndEnvironmentVariableSetWhenTryingToNotifyGtpinInitializationThenGtpinDoesNotGetNotified) { + platformsImpl->clear(); + ASSERT_TRUE(platformsImpl->empty()); + + VariableBackup gtpinCounterBackup(>pinInitTimesCalled, 0u); + VariableBackup mockGetenvCalledBackup(&IoFunctions::mockGetenvCalled, 0); + std::unordered_map mockableEnvs = {{"ZET_ENABLE_PROGRAM_INSTRUMENTATION", "1"}}; + VariableBackup *> mockableEnvValuesBackup(&IoFunctions::mockableEnvValues, &mockableEnvs); + + uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { gtpinInitTimesCalled++; return 0; }; + MockOsLibrary mockLibraryObject(reinterpret_cast(openPinHandler), false); + MockOsLibrary::loadLibraryNewObject = &mockLibraryObject; + NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load; + + gtPinTryNotifyInit(); + EXPECT_EQ(0u, gtpinInitTimesCalled); + platformsImpl->clear(); +} + +TEST(GTPinInitNotifyTests, givenAvailablePlatformsAndEnvironmentVariableSetWhenTryingToNotifyGtpinInitializationThenGtpinGetsNotified) { + platformsImpl->clear(); + constructPlatform(); + ASSERT_FALSE(platformsImpl->empty()); + + VariableBackup gtpinCounterBackup(>pinInitTimesCalled, 0u); + VariableBackup mockGetenvCalledBackup(&IoFunctions::mockGetenvCalled, 0); + std::unordered_map mockableEnvs = {{"ZET_ENABLE_PROGRAM_INSTRUMENTATION", "1"}}; + VariableBackup *> mockableEnvValuesBackup(&IoFunctions::mockableEnvValues, &mockableEnvs); + + uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { gtpinInitTimesCalled++; return 0; }; + MockOsLibrary mockLibraryObject(reinterpret_cast(openPinHandler), false); + MockOsLibrary::loadLibraryNewObject = &mockLibraryObject; + NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load; + + gtPinTryNotifyInit(); + EXPECT_EQ(1u, gtpinInitTimesCalled); + platformsImpl->clear(); +} + } // namespace ULT diff --git a/shared/source/CMakeLists.txt b/shared/source/CMakeLists.txt index 4392253163..c6d6f71d29 100644 --- a/shared/source/CMakeLists.txt +++ b/shared/source/CMakeLists.txt @@ -143,6 +143,7 @@ append_sources_from_properties(CORE_SOURCES NEO_DEVICE_BINARY_FORMAT NEO_UNIFIED_MEMORY NEO_CORE_RELEASE_HELPER + NEO_GTPIN ) if(WIN32) @@ -205,4 +206,13 @@ get_property(NEO_SRCS_ENABLE_CORE GLOBAL PROPERTY NEO_SRCS_ENABLE_CORE) target_sources(ocloc_lib PRIVATE ${NEO_SRCS_ENABLE_CORE}) if(UNIX) set_property(GLOBAL APPEND PROPERTY NEO_CORE_SRCS_LINK ${CORE_SRCS_LINK_LINUX}) + target_include_directories(${NEO_SHARED_RELEASE_LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/pin/linux) + if(NOT NEO_SKIP_UNIT_TESTS) + target_include_directories(${NEO_SHARED_MOCKABLE_LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/pin/linux) + endif() +else() + target_include_directories(${NEO_SHARED_RELEASE_LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/pin/windows) + if(NOT NEO_SKIP_UNIT_TESTS) + target_include_directories(${NEO_SHARED_MOCKABLE_LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/pin/windows) + endif() endif() diff --git a/shared/source/pin/CMakeLists.txt b/shared/source/pin/CMakeLists.txt new file mode 100644 index 0000000000..3188a64b99 --- /dev/null +++ b/shared/source/pin/CMakeLists.txt @@ -0,0 +1,14 @@ +# +# Copyright (C) 2020-2023 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +set(NEO_GTPIN + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/pin.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pin.h +) + +set_property(GLOBAL PROPERTY NEO_GTPIN ${NEO_GTPIN}) +add_subdirectories() diff --git a/shared/source/pin/linux/CMakeLists.txt b/shared/source/pin/linux/CMakeLists.txt new file mode 100644 index 0000000000..9084ce4762 --- /dev/null +++ b/shared/source/pin/linux/CMakeLists.txt @@ -0,0 +1,13 @@ +# +# Copyright (C) 2020-2023 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +if(UNIX) + list(APPEND NEO_GTPIN + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/os_pin.h + ) + include_directories(${NEO_SHARED_DIRECTORY}/pin/) +endif() \ No newline at end of file diff --git a/level_zero/tools/source/pin/linux/os_pin.h b/shared/source/pin/linux/os_pin.h similarity index 69% rename from level_zero/tools/source/pin/linux/os_pin.h rename to shared/source/pin/linux/os_pin.h index e68b88b80a..0cb66a6618 100644 --- a/level_zero/tools/source/pin/linux/os_pin.h +++ b/shared/source/pin/linux/os_pin.h @@ -5,14 +5,11 @@ * */ -#pragma once +#include "shared/source/pin/pin.h" -#include "level_zero/tools/source/pin/pin.h" - -namespace L0 { +namespace NEO { typedef uint32_t (*OpenGTPin_fn)(void *gtPinInit); - const std::string PinContext::gtPinLibraryFilename = "libgtpin.so"; -} // namespace L0 +} // namespace NEO \ No newline at end of file diff --git a/level_zero/tools/source/pin/pin.cpp b/shared/source/pin/pin.cpp similarity index 65% rename from level_zero/tools/source/pin/pin.cpp rename to shared/source/pin/pin.cpp index 1f7825c5eb..1a86ed847a 100644 --- a/level_zero/tools/source/pin/pin.cpp +++ b/shared/source/pin/pin.cpp @@ -9,39 +9,34 @@ #include "shared/source/debug_settings/debug_settings_manager.h" -#include "level_zero/source/inc/ze_intel_gpu.h" - #include "os_pin.h" -const std::string gtPinOpenFunctionName = "OpenGTPin"; - -namespace L0 { +namespace NEO { PinContext::OsLibraryLoadPtr PinContext::osLibraryLoadFunction(NEO::OsLibrary::load); -ze_result_t PinContext::init() { +bool PinContext::init(const std::string >PinOpenFunctionName) { NEO::OsLibrary *hGtPinLibrary = nullptr; - hGtPinLibrary = PinContext::osLibraryLoadFunction(gtPinLibraryFilename.c_str()); + hGtPinLibrary = PinContext::osLibraryLoadFunction(PinContext::gtPinLibraryFilename.c_str()); if (hGtPinLibrary == nullptr) { - PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Unable to find gtpin library %s\n", gtPinLibraryFilename.c_str()); - return ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE; + PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Unable to find gtpin library %s\n", PinContext::gtPinLibraryFilename.c_str()); + return false; } OpenGTPin_fn openGTPin = reinterpret_cast(hGtPinLibrary->getProcAddress(gtPinOpenFunctionName.c_str())); if (openGTPin == nullptr) { PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Unable to find gtpin library open function symbol %s\n", gtPinOpenFunctionName.c_str()); - return ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE; + return false; } uint32_t openResult = openGTPin(nullptr); if (openResult != 0) { PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "gtpin library open %s failed with status %u\n", gtPinOpenFunctionName.c_str(), openResult); - return ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE; + return false; } - - return ZE_RESULT_SUCCESS; + return true; } -} // namespace L0 +} // namespace NEO diff --git a/level_zero/tools/source/pin/pin.h b/shared/source/pin/pin.h similarity index 75% rename from level_zero/tools/source/pin/pin.h rename to shared/source/pin/pin.h index 4f1032bc97..cb48abaee7 100644 --- a/level_zero/tools/source/pin/pin.h +++ b/shared/source/pin/pin.h @@ -8,14 +8,11 @@ #pragma once #include "shared/source/os_interface/os_library.h" -#include -#include - -namespace L0 { +namespace NEO { class PinContext { public: - static ze_result_t init(); + static bool init(const std::string >PinOpenFunctionName); using OsLibraryLoadPtr = std::add_pointer::type; static OsLibraryLoadPtr osLibraryLoadFunction; @@ -23,4 +20,4 @@ class PinContext { static const std::string gtPinLibraryFilename; }; -} // namespace L0 +} // namespace NEO diff --git a/shared/source/pin/windows/CMakeLists.txt b/shared/source/pin/windows/CMakeLists.txt new file mode 100644 index 0000000000..3e6c8bd24e --- /dev/null +++ b/shared/source/pin/windows/CMakeLists.txt @@ -0,0 +1,13 @@ +# +# Copyright (C) 2020-2023 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +if(WIN32) + list(APPEND NEO_GTPIN + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/os_pin.h + ) + include_directories(${NEO_SHARED_DIRECTORY}/pin/) +endif() \ No newline at end of file diff --git a/level_zero/tools/source/pin/windows/os_pin.h b/shared/source/pin/windows/os_pin.h similarity index 70% rename from level_zero/tools/source/pin/windows/os_pin.h rename to shared/source/pin/windows/os_pin.h index e02b849bd1..8f2673d8bd 100644 --- a/level_zero/tools/source/pin/windows/os_pin.h +++ b/shared/source/pin/windows/os_pin.h @@ -5,14 +5,11 @@ * */ -#pragma once +#include "shared/source/pin/pin.h" -#include "level_zero/tools/source/pin/pin.h" - -namespace L0 { +namespace NEO { typedef uint32_t(__fastcall *OpenGTPin_fn)(void *gtPinInit); - const std::string PinContext::gtPinLibraryFilename = "gtpin.dll"; -} // namespace L0 +} // namespace NEO \ No newline at end of file diff --git a/level_zero/tools/test/unit_tests/sources/pin/CMakeLists.txt b/shared/test/unit_test/pin/CMakeLists.txt similarity index 83% rename from level_zero/tools/test/unit_tests/sources/pin/CMakeLists.txt rename to shared/test/unit_test/pin/CMakeLists.txt index 2122f0504d..bb76e465da 100644 --- a/level_zero/tools/test/unit_tests/sources/pin/CMakeLists.txt +++ b/shared/test/unit_test/pin/CMakeLists.txt @@ -4,7 +4,7 @@ # SPDX-License-Identifier: MIT # -target_sources(${TARGET_NAME} PRIVATE +target_sources(neo_shared_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt ${CMAKE_CURRENT_SOURCE_DIR}/test_pin.cpp ) diff --git a/shared/test/unit_test/pin/test_pin.cpp b/shared/test/unit_test/pin/test_pin.cpp new file mode 100644 index 0000000000..b816440c21 --- /dev/null +++ b/shared/test/unit_test/pin/test_pin.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2022-2023 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/source/pin/pin.h" +#include "shared/test/common/mocks/mock_os_library.h" + +#include "gtest/gtest.h" + +namespace ULT { + +TEST(PinInitializationTest, GivenValidLibraryPinContextInitSucceeds) { + uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { return 0; }; + MockOsLibrary mockLibraryObject(reinterpret_cast(openPinHandler), false); + MockOsLibrary::loadLibraryNewObject = &mockLibraryObject; + NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load; + std::string mockGTPinFunctionName{"aaa"}; + EXPECT_TRUE(NEO::PinContext::init(mockGTPinFunctionName)); + + MockOsLibrary::loadLibraryNewObject = nullptr; +} + +TEST(PinInitializationTest, GivenBadLibraryNamePinContextInitFAILS) { + MockOsLibrary::loadLibraryNewObject = nullptr; + NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load; + std::string mockGTPinFunctionName{"aaa"}; + EXPECT_FALSE(NEO::PinContext::init(mockGTPinFunctionName)); + + MockOsLibrary::loadLibraryNewObject = nullptr; +} + +TEST(PinInitializationTest, GivenBadProcAddressPinContextInitFAILS) { + MockOsLibrary mockLibraryObject(nullptr, false); + MockOsLibrary::loadLibraryNewObject = &mockLibraryObject; + NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load; + std::string mockGTPinFunctionName{"aaa"}; + EXPECT_FALSE(NEO::PinContext::init(mockGTPinFunctionName)); + + MockOsLibrary::loadLibraryNewObject = nullptr; +} + +TEST(PinInitializationTest, GivenBadPinHandlerPinContextInitFAILS) { + uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { return 1; }; + MockOsLibrary mockLibraryObject(reinterpret_cast(openPinHandler), false); + MockOsLibrary::loadLibraryNewObject = &mockLibraryObject; + NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load; + std::string mockGTPinFunctionName{"aaa"}; + EXPECT_FALSE(NEO::PinContext::init(mockGTPinFunctionName)); + + MockOsLibrary::loadLibraryNewObject = nullptr; +} + +} // namespace ULT