Skip to content

Commit d7c43d0

Browse files
Improve GtPin init error status reporting
Related-To: LOCI-1286 Signed-off-by: davidoli <[email protected]>
1 parent a931f16 commit d7c43d0

File tree

8 files changed

+109
-13
lines changed

8 files changed

+109
-13
lines changed

level_zero/tools/source/metrics/metric_oa_source.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class OaMetricSourceImp : public MetricSource {
4848
uint32_t getSubDeviceIndex();
4949
bool isImplicitScalingCapable() const;
5050
const MetricDeviceContext &getMetricDeviceContext() const { return metricDeviceContext; }
51-
static bool checkDependencies();
5251
static std::unique_ptr<OaMetricSourceImp> create(const MetricDeviceContext &metricDeviceContext);
5352
using OsLibraryLoadPtr = std::add_pointer<NEO::OsLibrary *(const std::string &)>::type;
5453
static OsLibraryLoadPtr osLibraryLoadFunction;

level_zero/tools/source/pin/pin.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,28 @@ const std::string gtPinOpenFunctionName = "OpenGTPin";
1818

1919
namespace L0 {
2020

21+
PinContext::OsLibraryLoadPtr PinContext::osLibraryLoadFunction(NEO::OsLibrary::load);
22+
2123
ze_result_t PinContext::init() {
22-
std::unique_ptr<NEO::OsLibrary> hGtPinLibrary = nullptr;
24+
NEO::OsLibrary *hGtPinLibrary = nullptr;
25+
26+
hGtPinLibrary = PinContext::osLibraryLoadFunction(gtPinLibraryFilename.c_str());
2327

24-
hGtPinLibrary.reset(NEO::OsLibrary::load(gtPinLibraryFilename.c_str()));
25-
if (hGtPinLibrary.get() == nullptr) {
28+
if (hGtPinLibrary == nullptr) {
2629
PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Unable to find gtpin library %s\n", gtPinLibraryFilename.c_str());
27-
return ZE_RESULT_ERROR_UNKNOWN;
30+
return ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE;
2831
}
2932

3033
OpenGTPin_fn openGTPin = reinterpret_cast<OpenGTPin_fn>(hGtPinLibrary->getProcAddress(gtPinOpenFunctionName.c_str()));
3134
if (openGTPin == nullptr) {
32-
hGtPinLibrary.reset(nullptr);
3335
PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "Unable to find gtpin library open function symbol %s\n", gtPinOpenFunctionName.c_str());
34-
return ZE_RESULT_ERROR_UNKNOWN;
36+
return ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE;
3537
}
3638

3739
uint32_t openResult = openGTPin(nullptr);
3840
if (openResult != 0) {
39-
hGtPinLibrary.reset(nullptr);
4041
PRINT_DEBUG_STRING(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr, "gtpin library open %s failed with status %u\n", gtPinOpenFunctionName.c_str(), openResult);
41-
return ZE_RESULT_ERROR_UNKNOWN;
42+
return ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE;
4243
}
4344

4445
return ZE_RESULT_SUCCESS;

level_zero/tools/source/pin/pin.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2021 Intel Corporation
2+
* Copyright (C) 2020-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -16,6 +16,8 @@ namespace L0 {
1616
class PinContext {
1717
public:
1818
static ze_result_t init();
19+
using OsLibraryLoadPtr = std::add_pointer<NEO::OsLibrary *(const std::string &)>::type;
20+
static OsLibraryLoadPtr osLibraryLoadFunction;
1921

2022
private:
2123
static const std::string gtPinLibraryFilename;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# Copyright (C) 2022 Intel Corporation
3+
#
4+
# SPDX-License-Identifier: MIT
5+
#
6+
7+
target_sources(${TARGET_NAME} PRIVATE
8+
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
9+
${CMAKE_CURRENT_SOURCE_DIR}/test_pin.cpp
10+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2022 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "shared/test/common/mocks/mock_os_library.h"
9+
10+
#include "level_zero/tools/source/pin/pin.h"
11+
12+
#include "gtest/gtest.h"
13+
14+
namespace ult {
15+
16+
TEST(PinInitializationTest, GivenValidLibraryPinContextInitSucceeds) {
17+
uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { return 0; };
18+
auto newPtr = new MockOsLibrary(reinterpret_cast<void *>(openPinHandler), false);
19+
MockOsLibrary::loadLibraryNewObject = newPtr;
20+
L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load;
21+
EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_SUCCESS);
22+
L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load;
23+
MockOsLibrary::loadLibraryNewObject = nullptr;
24+
delete newPtr;
25+
}
26+
27+
TEST(PinInitializationTest, GivenBadLibraryNamePinContextInitFAILS) {
28+
MockOsLibrary::loadLibraryNewObject = nullptr;
29+
L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load;
30+
EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE);
31+
L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load;
32+
MockOsLibrary::loadLibraryNewObject = nullptr;
33+
}
34+
35+
TEST(PinInitializationTest, GivenBadProcAddressPinContextInitFAILS) {
36+
auto newPtr = new MockOsLibrary(nullptr, false);
37+
MockOsLibrary::loadLibraryNewObject = newPtr;
38+
L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load;
39+
EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE);
40+
L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load;
41+
MockOsLibrary::loadLibraryNewObject = nullptr;
42+
delete newPtr;
43+
}
44+
45+
TEST(PinInitializationTest, GivenBadPinHandlerPinContextInitFAILS) {
46+
uint32_t (*openPinHandler)(void *) = [](void *arg) -> uint32_t { return 1; };
47+
auto newPtr = new MockOsLibrary(reinterpret_cast<void *>(openPinHandler), false);
48+
MockOsLibrary::loadLibraryNewObject = newPtr;
49+
L0::PinContext::osLibraryLoadFunction = MockOsLibrary::load;
50+
EXPECT_EQ(L0::PinContext::init(), ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE);
51+
L0::PinContext::osLibraryLoadFunction = NEO::OsLibrary::load;
52+
MockOsLibrary::loadLibraryNewObject = nullptr;
53+
delete newPtr;
54+
}
55+
56+
} // namespace ult

shared/test/common/mocks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ set(NEO_CORE_tests_mocks
7676
${CMAKE_CURRENT_SOURCE_DIR}/mock_multi_graphics_allocation.h
7777
${CMAKE_CURRENT_SOURCE_DIR}/mock_os_context.h
7878
${CMAKE_CURRENT_SOURCE_DIR}/mock_os_library.h
79+
${CMAKE_CURRENT_SOURCE_DIR}/mock_os_library.cpp
7980
${CMAKE_CURRENT_SOURCE_DIR}/mock_ostime.h
8081
${CMAKE_CURRENT_SOURCE_DIR}/mock_physical_address_allocator.h
8182
${CMAKE_CURRENT_SOURCE_DIR}/mock_sip.cpp
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (C) 2022 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "shared/test/common/mocks/mock_os_library.h"
9+
10+
NEO::OsLibrary *MockOsLibrary::loadLibraryNewObject = nullptr;

shared/test/common/mocks/mock_os_library.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 Intel Corporation
2+
* Copyright (C) 2021-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -11,10 +11,27 @@
1111

1212
class MockOsLibrary : public NEO::OsLibrary {
1313
public:
14+
MockOsLibrary(void *procAddress, bool isLoaded) : getProcAddressReturn{procAddress}, isLoadedReturn{isLoaded} {}
15+
MockOsLibrary() {}
16+
17+
std::string lastRequestedProcName;
18+
void *getProcAddressReturn = nullptr;
19+
1420
void *getProcAddress(const std::string &procName) override {
15-
return nullptr;
21+
lastRequestedProcName = procName;
22+
return getProcAddressReturn;
1623
}
24+
25+
bool isLoadedReturn = false;
26+
1727
bool isLoaded() override {
18-
return false;
28+
return isLoadedReturn;
29+
}
30+
31+
static OsLibrary *loadLibraryNewObject;
32+
33+
static OsLibrary *load(const std::string &name) {
34+
OsLibrary *ptr = loadLibraryNewObject;
35+
return ptr;
1936
}
2037
};

0 commit comments

Comments
 (0)