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

@@ -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,18 +0,0 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "level_zero/tools/source/pin/pin.h"
namespace L0 {
typedef uint32_t (*OpenGTPin_fn)(void *gtPinInit);
const std::string PinContext::gtPinLibraryFilename = "libgtpin.so";
} // namespace L0

View File

@@ -1,47 +0,0 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "pin.h"
#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 {
PinContext::OsLibraryLoadPtr PinContext::osLibraryLoadFunction(NEO::OsLibrary::load);
ze_result_t PinContext::init() {
NEO::OsLibrary *hGtPinLibrary = nullptr;
hGtPinLibrary = PinContext::osLibraryLoadFunction(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;
}
OpenGTPin_fn openGTPin = reinterpret_cast<OpenGTPin_fn>(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;
}
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 ZE_RESULT_SUCCESS;
}
} // namespace L0

View File

@@ -1,26 +0,0 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/os_interface/os_library.h"
#include <level_zero/ze_api.h>
#include <level_zero/zet_api.h>
namespace L0 {
class PinContext {
public:
static ze_result_t init();
using OsLibraryLoadPtr = std::add_pointer<NEO::OsLibrary *(const std::string &)>::type;
static OsLibraryLoadPtr osLibraryLoadFunction;
private:
static const std::string gtPinLibraryFilename;
};
} // namespace L0

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,18 +0,0 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "level_zero/tools/source/pin/pin.h"
namespace L0 {
typedef uint32_t(__fastcall *OpenGTPin_fn)(void *gtPinInit);
const std::string PinContext::gtPinLibraryFilename = "gtpin.dll";
} // namespace L0

View File

@@ -1,10 +0,0 @@
#
# Copyright (C) 2022-2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
target_sources(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/test_pin.cpp
)

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