refactor: Cache context settings during init

- cache context related settings once to reuse in subsequent calls

Signed-off-by: Aravind Gopalakrishnan <aravind.gopalakrishnan@intel.com>
This commit is contained in:
Aravind Gopalakrishnan
2025-07-10 05:05:47 +00:00
committed by Compute-Runtime-Automation
parent 5840dc09cd
commit c1c8eb59c0
5 changed files with 119 additions and 15 deletions

View File

@@ -64,6 +64,26 @@ DriverHandle *ContextImp::getDriverHandle() {
ContextImp::ContextImp(DriverHandle *driverHandle) {
this->driverHandle = static_cast<DriverHandleImp *>(driverHandle);
this->contextExt = createContextExt(driverHandle);
bool platformSupportSvmHeapReservation = true;
for (auto &device : this->driverHandle->devices) {
auto &productHelper = device->getNEODevice()->getProductHelper();
if (!productHelper.isSvmHeapReservationSupported()) {
platformSupportSvmHeapReservation = false;
break;
}
}
contextSettings.enableSvmHeapReservation = platformSupportSvmHeapReservation;
bool pidfdOrSocket = true;
for (auto &device : this->driverHandle->devices) {
auto &productHelper = device->getNEODevice()->getProductHelper();
if (!productHelper.isPidFdOrSocketForIpcSupported()) {
pidfdOrSocket = false;
break;
}
}
contextSettings.enablePidfdOrSockets = pidfdOrSocket;
}
ContextImp::~ContextImp() {
@@ -779,14 +799,7 @@ ze_result_t ContextImp::getIpcMemHandlesImpl(const void *ptr,
ipcType = InternalIpcMemoryType::hostUnifiedMemory;
}
bool pidfdOrSocket = true;
for (auto &device : this->driverHandle->devices) {
auto &productHelper = device->getNEODevice()->getProductHelper();
if (!productHelper.isPidFdOrSocketForIpcSupported()) {
pidfdOrSocket = false;
break;
}
}
bool pidfdOrSocket = contextSettings.enablePidfdOrSockets;
uint32_t loopCount = numIpcHandles ? *numIpcHandles : 1u;
for (uint32_t i = 0u; i < loopCount; i++) {
uint64_t handle = 0;
@@ -1192,12 +1205,7 @@ ze_result_t ContextImp::reserveVirtualMem(const void *pStart,
reserveOnSvmHeap = true;
}
bool platformSupportSvmHeapReservation = true;
for (auto &device : this->driverHandle->devices) {
auto &productHelper = device->getNEODevice()->getProductHelper();
platformSupportSvmHeapReservation &= productHelper.isSvmHeapReservationSupported();
}
reserveOnSvmHeap &= platformSupportSvmHeapReservation;
reserveOnSvmHeap &= contextSettings.enableSvmHeapReservation;
reserveOnSvmHeap &= NEO::debugManager.flags.EnableReservingInSvmRange.get();
NEO::AddressRange addressRange{};

View File

@@ -26,6 +26,11 @@ struct IpcCounterBasedEventData;
ContextExt *createContextExt(DriverHandle *driverHandle);
void destroyContextExt(ContextExt *ctxExt);
struct ContextSettings {
bool enablePidfdOrSockets = true;
bool enableSvmHeapReservation = true;
};
struct ContextImp : Context, NEO::NonCopyableAndNonMovableClass {
ContextImp(DriverHandle *driverHandle);
~ContextImp() override;
@@ -166,6 +171,7 @@ struct ContextImp : Context, NEO::NonCopyableAndNonMovableClass {
RootDeviceIndicesContainer rootDeviceIndices;
std::map<uint32_t, NEO::DeviceBitfield> deviceBitfields;
ContextSettings contextSettings;
bool isDeviceDefinedForThisContext(Device *inDevice);
bool isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice) override;

View File

@@ -1174,6 +1174,54 @@ TEST_F(ContextTest, whenGettingDriverThenDriverIsRetrievedSuccessfully) {
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
}
TEST_F(ContextTest, whenCreatingContextWithPidfdApproachTrueThenContextSettingsSetCorrectly) {
DebugManagerStateRestore restore;
debugManager.flags.EnablePidFdOrSocketsForIpc.set(1);
ze_context_handle_t hContext;
ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};
ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext);
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
ContextImp *contextImp = static_cast<ContextImp *>(L0::Context::fromHandle(hContext));
EXPECT_TRUE(contextImp->contextSettings.enablePidfdOrSockets);
res = contextImp->destroy();
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
}
TEST_F(ContextTest, whenCreatingContextWithPidfdApproachFalseThenContextSettingsSetCorrectly) {
DebugManagerStateRestore restore;
debugManager.flags.EnablePidFdOrSocketsForIpc.set(0);
ze_context_handle_t hContext;
ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};
ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext);
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
ContextImp *contextImp = static_cast<ContextImp *>(L0::Context::fromHandle(hContext));
EXPECT_FALSE(contextImp->contextSettings.enablePidfdOrSockets);
res = contextImp->destroy();
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
}
HWTEST2_F(ContextTest, whenCreatingContextWithSvmHeapReservationTrueThenContextSettingsSetCorrectly, IsNotMTL) {
ze_context_handle_t hContext;
ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};
ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext);
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
ContextImp *contextImp = static_cast<ContextImp *>(L0::Context::fromHandle(hContext));
EXPECT_TRUE(contextImp->contextSettings.enableSvmHeapReservation);
res = contextImp->destroy();
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
}
TEST_F(ContextTest, whenCallingVirtualMemInterfacesThenSuccessIsReturned) {
ze_context_handle_t hContext;
ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Intel Corporation
* Copyright (C) 2023-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -11,6 +11,7 @@
#include "shared/test/common/mocks/mock_device.h"
#include "shared/test/common/mocks/mock_driver_model.h"
#include "shared/test/common/mocks/mock_memory_operations_handler.h"
#include "shared/test/common/test_macros/hw_test.h"
#include "shared/test/common/test_macros/test.h"
#include "level_zero/core/source/context/context_imp.h"
@@ -59,5 +60,23 @@ TEST_F(ContextGetVirtualAddressSpaceTests, givenWddmDriverModelWhenCallingPutVir
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
}
using ContextTestWindows = Test<DeviceFixture>;
HWTEST2_F(ContextTestWindows, whenCreatingContextWithSvmHeapDisabledThenContextSettingsSetCorrectly, IsMTL) {
neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new NEO::OSInterface());
neoDevice->executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::make_unique<NEO::MockDriverModelWDDM>());
ze_context_handle_t hContext;
ze_context_desc_t desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};
ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext);
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
ContextImp *contextImp = static_cast<ContextImp *>(L0::Context::fromHandle(hContext));
EXPECT_FALSE(contextImp->contextSettings.enableSvmHeapReservation);
res = contextImp->destroy();
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
}
} // namespace ult
} // namespace L0

View File

@@ -1076,5 +1076,28 @@ TEST_F(MemoryDrmIpcPidfdTests,
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
}
TEST_F(MemoryDrmIpcPidfdTests,
givenCallToGetIpcHandleWithDeviceAllocationWithPidFdSocketMethodFalseAndMockDrmThenIpcHandleIsReturned) {
DebugManagerStateRestore restorer;
debugManager.flags.EnablePidFdOrSocketsForIpc.set(0);
size_t size = 10;
size_t alignment = 1u;
void *ptr = nullptr;
ze_device_mem_alloc_desc_t deviceDesc = {};
ze_result_t result = context->allocDeviceMem(device->toHandle(),
&deviceDesc,
size, alignment, &ptr);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_NE(nullptr, ptr);
ze_ipc_mem_handle_t ipcHandle;
result = context->getIpcMemHandle(ptr, &ipcHandle);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
result = context->freeMem(ptr);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
}
} // namespace ult
} // namespace L0