diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index 9c345e9756..d61dee3035 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -594,27 +594,19 @@ ze_result_t EventPool::closeIpcHandle() { } ze_result_t EventPool::getIpcHandle(ze_ipc_event_pool_handle_t *ipcHandle) { - // L0 uses a vector of ZE_MAX_IPC_HANDLE_SIZE bytes to send the IPC handle, i.e. - // char data[ZE_MAX_IPC_HANDLE_SIZE]; - // First four bytes (which is of size sizeof(int)) of it contain the file descriptor - // associated with the dma-buf, - // Rest is payload to communicate extra info to the other processes. - // For the event pool, this contains: - // - the number of events the pool has. - // - the id for the device used during pool creation if (!this->isShareableEventMemory) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } IpcEventPoolData &poolData = *reinterpret_cast(ipcHandle->data); poolData = {}; - this->eventPoolAllocations->getDefaultGraphicsAllocation()->peekInternalHandle(this->context->getDriverHandle()->getMemoryManager(), poolData.handle); poolData.numEvents = this->numEvents; poolData.rootDeviceIndex = this->getDevice()->getRootDeviceIndex(); poolData.isDeviceEventPoolAllocation = this->isDeviceEventPoolAllocation; poolData.isHostVisibleEventPoolAllocation = this->isHostVisibleEventPoolAllocation; - return ZE_RESULT_SUCCESS; + int retCode = this->eventPoolAllocations->getDefaultGraphicsAllocation()->peekInternalHandle(this->context->getDriverHandle()->getMemoryManager(), poolData.handle); + return retCode == 0 ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY; } ze_result_t ContextImp::openEventPoolIpcHandle(const ze_ipc_event_pool_handle_t &ipcEventPoolHandle, diff --git a/level_zero/core/test/unit_tests/sources/event/test_event.cpp b/level_zero/core/test/unit_tests/sources/event/test_event.cpp index 8ed37d45c4..5b0a8c9a61 100644 --- a/level_zero/core/test/unit_tests/sources/event/test_event.cpp +++ b/level_zero/core/test/unit_tests/sources/event/test_event.cpp @@ -292,12 +292,22 @@ TEST_F(EventPoolCreate, GivenDeviceThenEventPoolIsCreated) { } eventPool->destroy(); } +struct EventPoolIpcMockGraphicsAllocation : public NEO::MockGraphicsAllocation { + using NEO::MockGraphicsAllocation::MockGraphicsAllocation; -class MemoryManagerEventPoolIPCMock : public NEO::MockMemoryManager { + int peekInternalHandle(MemoryManager *memoryManager, uint64_t &handle) override { + handle = peekInternalHandleValue; + return peekInternalHandleRetCode; + } + + int peekInternalHandleRetCode = 0; + uint64_t peekInternalHandleValue = 0; +}; +class MemoryManagerEventPoolIpcMock : public NEO::MockMemoryManager { public: - MemoryManagerEventPoolIPCMock(NEO::ExecutionEnvironment &executionEnvironment) : NEO::MockMemoryManager(executionEnvironment) {} + MemoryManagerEventPoolIpcMock(NEO::ExecutionEnvironment &executionEnvironment) : NEO::MockMemoryManager(executionEnvironment) {} void *createMultiGraphicsAllocationInSystemMemoryPool(RootDeviceIndicesContainer &rootDeviceIndices, AllocationProperties &properties, NEO::MultiGraphicsAllocation &multiGraphicsAllocation) override { - alloc = new NEO::MockGraphicsAllocation(&buffer, sizeof(buffer)); + alloc = new EventPoolIpcMockGraphicsAllocation(&buffer, sizeof(buffer)); alloc->isShareableHostMemory = true; multiGraphicsAllocation.addAllocation(alloc); return reinterpret_cast(alloc->getUnderlyingBuffer()); @@ -306,13 +316,22 @@ class MemoryManagerEventPoolIPCMock : public NEO::MockMemoryManager { if (callParentCreateGraphicsAllocationFromSharedHandle) { return NEO::MockMemoryManager::createGraphicsAllocationFromSharedHandle(handle, properties, requireSpecificBitness, isHostIpcAllocation, reuseSharedAllocation); } - alloc = new NEO::MockGraphicsAllocation(&buffer, sizeof(buffer)); + alloc = new EventPoolIpcMockGraphicsAllocation(&buffer, sizeof(buffer)); + alloc->isShareableHostMemory = true; + return alloc; + } + GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties) override { + if (callParentAllocateGraphicsMemoryWithProperties) { + return NEO::MockMemoryManager::allocateGraphicsMemoryWithProperties(properties); + } + alloc = new EventPoolIpcMockGraphicsAllocation(&buffer, sizeof(buffer)); alloc->isShareableHostMemory = true; return alloc; } char buffer[256]; - NEO::MockGraphicsAllocation *alloc; + EventPoolIpcMockGraphicsAllocation *alloc; bool callParentCreateGraphicsAllocationFromSharedHandle = true; + bool callParentAllocateGraphicsMemoryWithProperties = true; }; using EventPoolIPCHandleTests = Test; @@ -328,7 +347,7 @@ TEST_F(EventPoolIPCHandleTests, whenGettingIpcHandleForEventPoolThenHandleAndNum auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto curMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -360,7 +379,7 @@ TEST_F(EventPoolIPCHandleTests, whenGettingIpcHandleForEventPoolThenHandleAndIsH auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto curMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -393,7 +412,7 @@ TEST_F(EventPoolIPCHandleTests, whenGettingIpcHandleForEventPoolWithDeviceAllocT auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto curMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -468,7 +487,7 @@ TEST_F(EventPoolIPCHandleTests, whenOpeningIpcHandleForEventPoolThenEventPoolIsC auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto curMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -507,7 +526,7 @@ TEST_F(EventPoolIPCHandleTests, whenOpeningIpcHandleForEventPoolWithHostVisibleT auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto curMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -546,7 +565,7 @@ TEST_F(EventPoolIPCHandleTests, whenOpeningIpcHandleForEventPoolWithDeviceAllocT auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto curMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -630,7 +649,7 @@ TEST_F(EventPoolIPCHandleTests, GivenIpcEventPoolWhenCreatingEventFromIpcPoolThe auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto curMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); mockMemoryManager->callParentCreateGraphicsAllocationFromSharedHandle = false; driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); @@ -677,6 +696,38 @@ TEST_F(EventPoolIPCHandleTests, GivenIpcEventPoolWhenCreatingEventFromIpcPoolThe driverHandle->setMemoryManager(curMemoryManager); } +TEST_F(EventPoolIPCHandleTests, givenIpcEventPoolWhenGettingIpcHandleAndFailingToGetThenCorrectErrorIsReturned) { + uint32_t numEvents = 1; + ze_event_pool_desc_t eventPoolDesc = { + ZE_STRUCTURE_TYPE_EVENT_POOL_DESC, + nullptr, + ZE_EVENT_POOL_FLAG_HOST_VISIBLE | ZE_EVENT_POOL_FLAG_IPC, + numEvents}; + + auto deviceHandle = device->toHandle(); + ze_result_t result = ZE_RESULT_SUCCESS; + auto curMemoryManager = driverHandle->getMemoryManager(); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); + mockMemoryManager->callParentAllocateGraphicsMemoryWithProperties = false; + driverHandle->setMemoryManager(mockMemoryManager); + auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, eventPool); + + auto alloc = static_cast(eventPool->getAllocation().getGraphicsAllocation(device->getRootDeviceIndex())); + EXPECT_EQ(mockMemoryManager->alloc, alloc); + alloc->peekInternalHandleRetCode = -1; + + ze_ipc_event_pool_handle_t ipcHandle = {}; + ze_result_t res = eventPool->getIpcHandle(&ipcHandle); + EXPECT_EQ(res, ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY); + + res = eventPool->destroy(); + EXPECT_EQ(res, ZE_RESULT_SUCCESS); + delete mockMemoryManager; + driverHandle->setMemoryManager(curMemoryManager); +} + using EventPoolOpenIPCHandleFailTests = Test; TEST_F(EventPoolOpenIPCHandleFailTests, givenFailureToAllocateMemoryWhenOpeningIpcHandleForEventPoolThenInvalidArgumentIsReturned) { @@ -690,7 +741,7 @@ TEST_F(EventPoolOpenIPCHandleFailTests, givenFailureToAllocateMemoryWhenOpeningI auto deviceHandle = device->toHandle(); ze_result_t result = ZE_RESULT_SUCCESS; auto originalMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -761,7 +812,7 @@ TEST_F(MultiDeviceEventPoolOpenIPCHandleFailTests, ze_result_t result = ZE_RESULT_SUCCESS; NEO::MockDevice *neoDevice = static_cast(driverHandle->devices[0]->getNEODevice()); auto originalMemoryManager = driverHandle->getMemoryManager(); - MemoryManagerEventPoolIPCMock *mockMemoryManager = new MemoryManagerEventPoolIPCMock(*neoDevice->executionEnvironment); + MemoryManagerEventPoolIpcMock *mockMemoryManager = new MemoryManagerEventPoolIpcMock(*neoDevice->executionEnvironment); driverHandle->setMemoryManager(mockMemoryManager); auto eventPool = EventPool::create(driverHandle.get(), context, 1, &deviceHandle, &eventPoolDesc, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result);