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 <kacper.nowak@intel.com>
This commit is contained in:
Kacper Nowak 2023-08-02 19:33:48 +02:00 committed by Compute-Runtime-Automation
parent c43b827702
commit 1afaf37f78
22 changed files with 218 additions and 142 deletions

View File

@ -11,12 +11,12 @@
#include "shared/source/execution_environment/execution_environment.h" #include "shared/source/execution_environment/execution_environment.h"
#include "shared/source/os_interface/debug_env_reader.h" #include "shared/source/os_interface/debug_env_reader.h"
#include "shared/source/os_interface/device_factory.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/device/device.h"
#include "level_zero/core/source/driver/driver_handle_imp.h" #include "level_zero/core/source/driver/driver_handle_imp.h"
#include "level_zero/core/source/driver/driver_imp.h" #include "level_zero/core/source/driver/driver_imp.h"
#include "level_zero/tools/source/metrics/metric.h" #include "level_zero/tools/source/metrics/metric.h"
#include "level_zero/tools/source/pin/pin.h"
#include <memory> #include <memory>
#include <thread> #include <thread>
@ -81,8 +81,9 @@ void DriverImp::initialize(ze_result_t *result) {
} }
if ((*result == ZE_RESULT_SUCCESS) && envVariables.pin) { if ((*result == ZE_RESULT_SUCCESS) && envVariables.pin) {
*result = PinContext::init(); std::string gtpinFuncName{"OpenGTPin"};
if (*result != ZE_RESULT_SUCCESS) { if (false == NEO::PinContext::init(gtpinFuncName)) {
*result = ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE;
delete GlobalDriver; delete GlobalDriver;
GlobalDriverHandle = nullptr; GlobalDriverHandle = nullptr;
GlobalDriver = nullptr; GlobalDriver = nullptr;

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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<void *>(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<void *>(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

View File

@ -400,6 +400,7 @@ cl_context CL_API_CALL clCreateContext(const cl_context_properties *properties,
size_t, void *), size_t, void *),
void *userData, void *userData,
cl_int *errcodeRet) { cl_int *errcodeRet) {
gtPinTryNotifyInit();
TRACING_ENTER(ClCreateContext, &properties, &numDevices, &devices, &funcNotify, &userData, &errcodeRet); TRACING_ENTER(ClCreateContext, &properties, &numDevices, &devices, &funcNotify, &userData, &errcodeRet);
cl_int retVal = CL_SUCCESS; cl_int retVal = CL_SUCCESS;
@ -457,6 +458,7 @@ cl_context CL_API_CALL clCreateContextFromType(const cl_context_properties *prop
size_t, void *), size_t, void *),
void *userData, void *userData,
cl_int *errcodeRet) { cl_int *errcodeRet) {
gtPinTryNotifyInit();
TRACING_ENTER(ClCreateContextFromType, &properties, &deviceType, &funcNotify, &userData, &errcodeRet); TRACING_ENTER(ClCreateContextFromType, &properties, &deviceType, &funcNotify, &userData, &errcodeRet);
cl_int retVal = CL_SUCCESS; cl_int retVal = CL_SUCCESS;
API_ENTER(&retVal); API_ENTER(&retVal);

View File

@ -21,6 +21,7 @@
#include "opencl/source/kernel/kernel.h" #include "opencl/source/kernel/kernel.h"
#include "opencl/source/kernel/multi_device_kernel.h" #include "opencl/source/kernel/multi_device_kernel.h"
#include "opencl/source/mem_obj/buffer.h" #include "opencl/source/mem_obj/buffer.h"
#include "opencl/source/platform/platform.h"
#include "opencl/source/program/program.h" #include "opencl/source/program/program.h"
#include "CL/cl.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 } // namespace NEO

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2018-2022 Intel Corporation * Copyright (C) 2018-2023 Intel Corporation
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* *

View File

@ -28,4 +28,6 @@ inline bool gtpinIsGTPinInitialized() { return isGTPinInitialized; }
void *gtpinGetIgcInit(); void *gtpinGetIgcInit();
void gtpinSetIgcInit(void *pIgcInitPtr); void gtpinSetIgcInit(void *pIgcInitPtr);
void gtpinRemoveCommandQueue(void *pCmdQueue); void gtpinRemoveCommandQueue(void *pCmdQueue);
void gtPinTryNotifyInit();
} // namespace NEO } // namespace NEO

View File

@ -17,6 +17,8 @@
#include "shared/source/helpers/debug_helpers.h" #include "shared/source/helpers/debug_helpers.h"
#include "shared/source/helpers/get_info.h" #include "shared/source/helpers/get_info.h"
#include "shared/source/helpers/hw_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 "shared/source/source_level_debugger/source_level_debugger.h"
#include "opencl/source/api/api.h" #include "opencl/source/api/api.h"
@ -185,6 +187,17 @@ bool Platform::initialize(std::vector<std::unique_ptr<Device>> devices) {
return true; 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() { void Platform::fillGlobalDispatchTable() {
sharingFactory.fillGlobalDispatchTable(); sharingFactory.fillGlobalDispatchTable();
} }

View File

@ -48,6 +48,7 @@ class Platform : public BaseObject<_cl_platform_id> {
MOCKABLE_VIRTUAL bool initialize(std::vector<std::unique_ptr<Device>> devices); MOCKABLE_VIRTUAL bool initialize(std::vector<std::unique_ptr<Device>> devices);
bool isInitialized(); bool isInitialized();
void tryNotifyGtpinInit();
size_t getNumDevices() const; size_t getNumDevices() const;
ClDevice **getClDevices(); ClDevice **getClDevices();
@ -71,6 +72,7 @@ class Platform : public BaseObject<_cl_platform_id> {
ClDeviceVector clDevices; ClDeviceVector clDevices;
ExecutionEnvironment &executionEnvironment; ExecutionEnvironment &executionEnvironment;
std::once_flag initializeExtensionsWithVersionOnce; std::once_flag initializeExtensionsWithVersionOnce;
std::once_flag oclInitGTPinOnce;
}; };
extern std::vector<std::unique_ptr<Platform>> *platformsImpl; extern std::vector<std::unique_ptr<Platform>> *platformsImpl;

View File

@ -14,6 +14,7 @@
#include "shared/source/memory_manager/surface.h" #include "shared/source/memory_manager/surface.h"
#include "shared/source/memory_manager/unified_memory_manager.h" #include "shared/source/memory_manager/unified_memory_manager.h"
#include "shared/source/os_interface/os_context.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/device_binary_format/patchtokens_tests.h"
#include "shared/test/common/fixtures/memory_management_fixture.h" #include "shared/test/common/fixtures/memory_management_fixture.h"
#include "shared/test/common/helpers/debug_manager_state_restore.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/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_cpu_page_fault_manager.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_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_memory_manager.h"
#include "shared/test/common/mocks/mock_modules_zebin.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 "shared/test/common/test_macros/hw_test.h"
#include "opencl/source/api/api.h" #include "opencl/source/api/api.h"
@ -2503,4 +2506,61 @@ TEST_F(GTPinTests, givenInitializedGTPinInterfaceWhenGtpinRemoveCommandQueueIsCa
EXPECT_EQ(0u, kernelExecQueue.size()); EXPECT_EQ(0u, kernelExecQueue.size());
} }
uint32_t gtpinInitTimesCalled = 0u;
TEST(GTPinInitNotifyTests, givenAvailablePlatformsAndNoEnvironmentVariableSetWhenTryingToNotifyGtpinInitializationThenGtpinDoesNotGetNotified) {
platformsImpl->clear();
constructPlatform();
ASSERT_FALSE(platformsImpl->empty());
VariableBackup<uint32_t> gtpinCounterBackup(&gtpinInitTimesCalled, 0u);
uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { gtpinInitTimesCalled++; return 0; };
MockOsLibrary mockLibraryObject(reinterpret_cast<void *>(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<uint32_t> gtpinCounterBackup(&gtpinInitTimesCalled, 0u);
VariableBackup<uint32_t> mockGetenvCalledBackup(&IoFunctions::mockGetenvCalled, 0);
std::unordered_map<std::string, std::string> mockableEnvs = {{"ZET_ENABLE_PROGRAM_INSTRUMENTATION", "1"}};
VariableBackup<std::unordered_map<std::string, std::string> *> mockableEnvValuesBackup(&IoFunctions::mockableEnvValues, &mockableEnvs);
uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { gtpinInitTimesCalled++; return 0; };
MockOsLibrary mockLibraryObject(reinterpret_cast<void *>(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<uint32_t> gtpinCounterBackup(&gtpinInitTimesCalled, 0u);
VariableBackup<uint32_t> mockGetenvCalledBackup(&IoFunctions::mockGetenvCalled, 0);
std::unordered_map<std::string, std::string> mockableEnvs = {{"ZET_ENABLE_PROGRAM_INSTRUMENTATION", "1"}};
VariableBackup<std::unordered_map<std::string, std::string> *> mockableEnvValuesBackup(&IoFunctions::mockableEnvValues, &mockableEnvs);
uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { gtpinInitTimesCalled++; return 0; };
MockOsLibrary mockLibraryObject(reinterpret_cast<void *>(openPinHandler), false);
MockOsLibrary::loadLibraryNewObject = &mockLibraryObject;
NEO::PinContext::osLibraryLoadFunction = MockOsLibrary::load;
gtPinTryNotifyInit();
EXPECT_EQ(1u, gtpinInitTimesCalled);
platformsImpl->clear();
}
} // namespace ULT } // namespace ULT

View File

@ -143,6 +143,7 @@ append_sources_from_properties(CORE_SOURCES
NEO_DEVICE_BINARY_FORMAT NEO_DEVICE_BINARY_FORMAT
NEO_UNIFIED_MEMORY NEO_UNIFIED_MEMORY
NEO_CORE_RELEASE_HELPER NEO_CORE_RELEASE_HELPER
NEO_GTPIN
) )
if(WIN32) 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}) target_sources(ocloc_lib PRIVATE ${NEO_SRCS_ENABLE_CORE})
if(UNIX) if(UNIX)
set_property(GLOBAL APPEND PROPERTY NEO_CORE_SRCS_LINK ${CORE_SRCS_LINK_LINUX}) 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() endif()

View File

@ -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()

View File

@ -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()

View File

@ -5,14 +5,11 @@
* *
*/ */
#pragma once #include "shared/source/pin/pin.h"
#include "level_zero/tools/source/pin/pin.h" namespace NEO {
namespace L0 {
typedef uint32_t (*OpenGTPin_fn)(void *gtPinInit); typedef uint32_t (*OpenGTPin_fn)(void *gtPinInit);
const std::string PinContext::gtPinLibraryFilename = "libgtpin.so"; const std::string PinContext::gtPinLibraryFilename = "libgtpin.so";
} // namespace L0 } // namespace NEO

View File

@ -9,39 +9,34 @@
#include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/debug_settings/debug_settings_manager.h"
#include "level_zero/source/inc/ze_intel_gpu.h"
#include "os_pin.h" #include "os_pin.h"
const std::string gtPinOpenFunctionName = "OpenGTPin"; namespace NEO {
namespace L0 {
PinContext::OsLibraryLoadPtr PinContext::osLibraryLoadFunction(NEO::OsLibrary::load); PinContext::OsLibraryLoadPtr PinContext::osLibraryLoadFunction(NEO::OsLibrary::load);
ze_result_t PinContext::init() { bool PinContext::init(const std::string &gtPinOpenFunctionName) {
NEO::OsLibrary *hGtPinLibrary = nullptr; NEO::OsLibrary *hGtPinLibrary = nullptr;
hGtPinLibrary = PinContext::osLibraryLoadFunction(gtPinLibraryFilename.c_str()); hGtPinLibrary = PinContext::osLibraryLoadFunction(PinContext::gtPinLibraryFilename.c_str());
if (hGtPinLibrary == nullptr) { if (hGtPinLibrary == nullptr) {
PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Unable to find gtpin library %s\n", gtPinLibraryFilename.c_str()); PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Unable to find gtpin library %s\n", PinContext::gtPinLibraryFilename.c_str());
return ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE; return false;
} }
OpenGTPin_fn openGTPin = reinterpret_cast<OpenGTPin_fn>(hGtPinLibrary->getProcAddress(gtPinOpenFunctionName.c_str())); OpenGTPin_fn openGTPin = reinterpret_cast<OpenGTPin_fn>(hGtPinLibrary->getProcAddress(gtPinOpenFunctionName.c_str()));
if (openGTPin == nullptr) { 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()); 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); uint32_t openResult = openGTPin(nullptr);
if (openResult != 0) { 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); 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 true;
return ZE_RESULT_SUCCESS;
} }
} // namespace L0 } // namespace NEO

View File

@ -8,14 +8,11 @@
#pragma once #pragma once
#include "shared/source/os_interface/os_library.h" #include "shared/source/os_interface/os_library.h"
#include <level_zero/ze_api.h> namespace NEO {
#include <level_zero/zet_api.h>
namespace L0 {
class PinContext { class PinContext {
public: public:
static ze_result_t init(); static bool init(const std::string &gtPinOpenFunctionName);
using OsLibraryLoadPtr = std::add_pointer<NEO::OsLibrary *(const std::string &)>::type; using OsLibraryLoadPtr = std::add_pointer<NEO::OsLibrary *(const std::string &)>::type;
static OsLibraryLoadPtr osLibraryLoadFunction; static OsLibraryLoadPtr osLibraryLoadFunction;
@ -23,4 +20,4 @@ class PinContext {
static const std::string gtPinLibraryFilename; static const std::string gtPinLibraryFilename;
}; };
} // namespace L0 } // namespace NEO

View File

@ -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()

View File

@ -5,14 +5,11 @@
* *
*/ */
#pragma once #include "shared/source/pin/pin.h"
#include "level_zero/tools/source/pin/pin.h" namespace NEO {
namespace L0 {
typedef uint32_t(__fastcall *OpenGTPin_fn)(void *gtPinInit); typedef uint32_t(__fastcall *OpenGTPin_fn)(void *gtPinInit);
const std::string PinContext::gtPinLibraryFilename = "gtpin.dll"; const std::string PinContext::gtPinLibraryFilename = "gtpin.dll";
} // namespace L0 } // namespace NEO

View File

@ -4,7 +4,7 @@
# SPDX-License-Identifier: MIT # 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}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/test_pin.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_pin.cpp
) )

View File

@ -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<void *>(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<void *>(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