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

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

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

@@ -0,0 +1,15 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/pin/pin.h"
namespace NEO {
typedef uint32_t (*OpenGTPin_fn)(void *gtPinInit);
const std::string PinContext::gtPinLibraryFilename = "libgtpin.so";
} // namespace NEO

42
shared/source/pin/pin.cpp Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "pin.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "os_pin.h"
namespace NEO {
PinContext::OsLibraryLoadPtr PinContext::osLibraryLoadFunction(NEO::OsLibrary::load);
bool PinContext::init(const std::string &gtPinOpenFunctionName) {
NEO::OsLibrary *hGtPinLibrary = nullptr;
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", PinContext::gtPinLibraryFilename.c_str());
return false;
}
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 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 false;
}
return true;
}
} // namespace NEO

23
shared/source/pin/pin.h Normal file
View File

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

@@ -0,0 +1,15 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/pin/pin.h"
namespace NEO {
typedef uint32_t(__fastcall *OpenGTPin_fn)(void *gtPinInit);
const std::string PinContext::gtPinLibraryFilename = "gtpin.dll";
} // namespace NEO

View File

@@ -0,0 +1,10 @@
#
# Copyright (C) 2022-2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
target_sources(neo_shared_tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${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