From 581805cc88d2515d5c4b9cc5da98ca44c87dcba8 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Wed, 12 Sep 2018 07:47:55 +0200 Subject: [PATCH] Move OsContext id setting to constructor. Change-Id: I1b809befc02536257800e3667307b8deabd5c95d --- runtime/device/device.cpp | 2 +- runtime/memory_manager/memory_manager.cpp | 7 +++++-- .../os_interface/linux/os_context_linux.cpp | 2 +- runtime/os_interface/os_context.h | 3 +-- .../os_interface/windows/os_context_win.cpp | 2 +- ...and_stream_receiver_with_aub_dump_tests.cpp | 4 ++-- .../memory_manager/memory_manager_tests.cpp | 18 ++++++++++++++---- .../linux/drm_command_stream_tests.cpp | 2 +- .../linux/os_interface_linux_tests.cpp | 4 ++-- .../windows/device_command_stream_tests.cpp | 4 ++-- .../windows/os_interface_win_tests.cpp | 6 +++--- .../os_interface/windows/wddm23_tests.cpp | 2 +- unit_tests/os_interface/windows/wddm_fixture.h | 4 ++-- .../windows/wddm_memory_manager_tests.h | 4 ++-- .../windows/wddm_preemption_tests.cpp | 2 +- 15 files changed, 39 insertions(+), 27 deletions(-) diff --git a/runtime/device/device.cpp b/runtime/device/device.cpp index a6f42ad8c3..a0474bbc6d 100644 --- a/runtime/device/device.cpp +++ b/runtime/device/device.cpp @@ -131,7 +131,7 @@ bool Device::createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice) { } executionEnvironment->initializeMemoryManager(outDevice.getEnabled64kbPages(), outDevice.getHardwareCapabilities().localMemorySupported, outDevice.getDeviceIndex()); - outDevice.osContext = new OsContext(executionEnvironment->osInterface.get()); + outDevice.osContext = new OsContext(executionEnvironment->osInterface.get(), outDevice.getDeviceIndex()); executionEnvironment->memoryManager->registerOsContext(outDevice.osContext); outDevice.commandStreamReceiver = executionEnvironment->commandStreamReceivers[outDevice.getDeviceIndex()].get(); diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index f3e0d7b72d..def11a9b3e 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -387,9 +387,12 @@ RequirementsStatus MemoryManager::checkAllocationsForOverlapping(AllocationRequi } void MemoryManager::registerOsContext(OsContext *contextToRegister) { + auto contextId = contextToRegister->getContextId(); + if (contextId + 1 > registeredOsContexts.size()) { + registeredOsContexts.resize(contextId + 1); + } contextToRegister->incRefInternal(); - contextToRegister->setContextId(static_cast(registeredOsContexts.size())); - registeredOsContexts.push_back(contextToRegister); + registeredOsContexts[contextToRegister->getContextId()] = contextToRegister; } bool MemoryManager::getAllocationData(AllocationData &allocationData, bool allocateMemory, const void *hostPtr, size_t size, GraphicsAllocation::AllocationType type) { diff --git a/runtime/os_interface/linux/os_context_linux.cpp b/runtime/os_interface/linux/os_context_linux.cpp index ea5858c0ee..dc330353f9 100644 --- a/runtime/os_interface/linux/os_context_linux.cpp +++ b/runtime/os_interface/linux/os_context_linux.cpp @@ -24,7 +24,7 @@ namespace OCLRT { class OsContext::OsContextImpl {}; -OsContext::OsContext(OSInterface *osInterface) { +OsContext::OsContext(OSInterface *osInterface, uint32_t contextId) : contextId(contextId) { osContextImpl = std::make_unique(); } diff --git a/runtime/os_interface/os_context.h b/runtime/os_interface/os_context.h index e4f9ca8c17..ddae7fbf7c 100644 --- a/runtime/os_interface/os_context.h +++ b/runtime/os_interface/os_context.h @@ -29,13 +29,12 @@ class OSInterface; class OsContext : public ReferenceTrackedObject { public: class OsContextImpl; - OsContext(OSInterface *osInterface); + OsContext(OSInterface *osInterface, uint32_t contextId); ~OsContext() override; OsContextImpl *get() const { return osContextImpl.get(); }; - void setContextId(uint32_t inputContextId) { contextId = inputContextId; } uint32_t getContextId() { return contextId; } protected: diff --git a/runtime/os_interface/windows/os_context_win.cpp b/runtime/os_interface/windows/os_context_win.cpp index 6a28072928..8d9f04e38d 100644 --- a/runtime/os_interface/windows/os_context_win.cpp +++ b/runtime/os_interface/windows/os_context_win.cpp @@ -53,7 +53,7 @@ void OsContextWin::resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cp monitoredFence.gpuAddress = gpuAddress; } -OsContext::OsContext(OSInterface *osInterface) { +OsContext::OsContext(OSInterface *osInterface, uint32_t contextId) : contextId(contextId) { if (osInterface) { osContextImpl = std::make_unique(*osInterface->get()->getWddm()); } diff --git a/unit_tests/command_stream/command_stream_receiver_with_aub_dump_tests.cpp b/unit_tests/command_stream/command_stream_receiver_with_aub_dump_tests.cpp index de4d43dba7..c01bfac5e3 100644 --- a/unit_tests/command_stream/command_stream_receiver_with_aub_dump_tests.cpp +++ b/unit_tests/command_stream/command_stream_receiver_with_aub_dump_tests.cpp @@ -147,7 +147,7 @@ HWTEST_P(CommandStreamReceiverWithAubDumpTest, givenCommandStreamReceiverWithAub BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 0, nullptr, false, false, QueueThrottle::MEDIUM, cs.getUsed(), &cs}; auto engineType = OCLRT::ENGINE_RCS; - OsContext osContext(nullptr); + OsContext osContext(nullptr, 0u); ResidencyContainer allocationsForResidency; FlushStamp flushStamp = csrWithAubDump->flush(batchBuffer, engineType, &allocationsForResidency, osContext); @@ -190,7 +190,7 @@ HWTEST_P(CommandStreamReceiverWithAubDumpTest, givenCommandStreamReceiverWithAub ASSERT_NE(nullptr, gfxAllocation); ResidencyContainer allocationsForResidency = {gfxAllocation}; - OsContext osContext(nullptr); + OsContext osContext(nullptr, 0u); csrWithAubDump->processResidency(&allocationsForResidency, osContext); EXPECT_TRUE(csrWithAubDump->processResidencyParameterization.wasCalled); diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index edbd72c134..cd38e7f76c 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -1825,13 +1825,24 @@ TEST(GraphicsAllocation, givenSharedHandleBasedConstructorWhenGraphicsAllocation } TEST(ResidencyDataTest, givenOsContextWhenItIsRegisteredToMemoryManagerThenRefCountIncreases) { - auto osContext = new OsContext(nullptr); + auto osContext = new OsContext(nullptr, 0u); OsAgnosticMemoryManager memoryManager; memoryManager.registerOsContext(osContext); EXPECT_EQ(1u, memoryManager.getOsContextCount()); EXPECT_EQ(1, osContext->getRefInternalCount()); } +TEST(ResidencyDataTest, givenTwoOsContextsWhenTheyAreRegistredFromHigherToLowerThenProperSizeIsReturned) { + auto osContext2 = new OsContext(nullptr, 1u); + auto osContext = new OsContext(nullptr, 0u); + OsAgnosticMemoryManager memoryManager; + memoryManager.registerOsContext(osContext2); + memoryManager.registerOsContext(osContext); + EXPECT_EQ(2u, memoryManager.getOsContextCount()); + EXPECT_EQ(1, osContext->getRefInternalCount()); + EXPECT_EQ(1, osContext2->getRefInternalCount()); +} + TEST(ResidencyDataTest, givenResidencyDataWhenUpdateCompletionDataIsCalledThenItIsProperlyUpdated) { struct mockResidencyData : public ResidencyData { using ResidencyData::completionData; @@ -1839,9 +1850,8 @@ TEST(ResidencyDataTest, givenResidencyDataWhenUpdateCompletionDataIsCalledThenIt mockResidencyData residency; - OsContext osContext(nullptr); - OsContext osContext2(nullptr); - osContext2.setContextId(1u); + OsContext osContext(nullptr, 0u); + OsContext osContext2(nullptr, 1u); auto lastFenceValue = 45llu; auto lastFenceValue2 = 23llu; diff --git a/unit_tests/os_interface/linux/drm_command_stream_tests.cpp b/unit_tests/os_interface/linux/drm_command_stream_tests.cpp index 53d5851a45..35892aea4d 100644 --- a/unit_tests/os_interface/linux/drm_command_stream_tests.cpp +++ b/unit_tests/os_interface/linux/drm_command_stream_tests.cpp @@ -57,7 +57,7 @@ class DrmCommandStreamFixture { const int mockFd = 33; void SetUp() { - osContext = std::make_unique(nullptr); + osContext = std::make_unique(nullptr, 0u); this->dbgState = new DebugManagerStateRestore(); //make sure this is disabled, we don't want test this now DebugManager.flags.EnableForcePin.set(false); diff --git a/unit_tests/os_interface/linux/os_interface_linux_tests.cpp b/unit_tests/os_interface/linux/os_interface_linux_tests.cpp index f17b0dcd37..0160a5b680 100644 --- a/unit_tests/os_interface/linux/os_interface_linux_tests.cpp +++ b/unit_tests/os_interface/linux/os_interface_linux_tests.cpp @@ -37,7 +37,7 @@ TEST(OsInterfaceTest, GivenLinuxOsInterfaceWhenDeviceHandleQueriedthenZeroIsRetu TEST(OsContextTest, WhenOsContextIsCreatedThenImplIsAvailable) { OSInterface osInterface; - auto osContext = std::make_unique(&osInterface); + auto osContext = std::make_unique(&osInterface, 0u); EXPECT_NE(nullptr, osContext->get()); } -} // namespace OCLRT \ No newline at end of file +} // namespace OCLRT diff --git a/unit_tests/os_interface/windows/device_command_stream_tests.cpp b/unit_tests/os_interface/windows/device_command_stream_tests.cpp index ea4767d37d..4a0a1310dd 100644 --- a/unit_tests/os_interface/windows/device_command_stream_tests.cpp +++ b/unit_tests/os_interface/windows/device_command_stream_tests.cpp @@ -279,7 +279,7 @@ TEST(WddmPreemptionHeaderTests, givenWddmCommandStreamReceiverWhenPreemptionIsOf LinearStream cs(commandBuffer); BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 0, nullptr, false, false, QueueThrottle::MEDIUM, cs.getUsed(), &cs}; - OsContext *osContext = new OsContext(executionEnvironment.osInterface.get()); + OsContext *osContext = new OsContext(executionEnvironment.osInterface.get(), 0u); osContext->incRefInternal(); executionEnvironment.commandStreamReceivers[0u]->flush(batchBuffer, EngineType::ENGINE_RCS, &executionEnvironment.commandStreamReceivers[0u]->getResidencyAllocations(), *osContext); auto commandHeader = wddm->submitResult.commandHeaderSubmitted; @@ -306,7 +306,7 @@ TEST(WddmPreemptionHeaderTests, givenWddmCommandStreamReceiverWhenPreemptionIsOn LinearStream cs(commandBuffer); BatchBuffer batchBuffer{cs.getGraphicsAllocation(), 0, 0, nullptr, false, false, QueueThrottle::MEDIUM, cs.getUsed(), &cs}; - OsContext *osContext = new OsContext(executionEnvironment.osInterface.get()); + OsContext *osContext = new OsContext(executionEnvironment.osInterface.get(), 0u); osContext->incRefInternal(); executionEnvironment.commandStreamReceivers[0u]->flush(batchBuffer, EngineType::ENGINE_RCS, &executionEnvironment.commandStreamReceivers[0u]->getResidencyAllocations(), *osContext); auto commandHeader = wddm->submitResult.commandHeaderSubmitted; diff --git a/unit_tests/os_interface/windows/os_interface_win_tests.cpp b/unit_tests/os_interface/windows/os_interface_win_tests.cpp index 22797ff069..08a3d859dc 100644 --- a/unit_tests/os_interface/windows/os_interface_win_tests.cpp +++ b/unit_tests/os_interface/windows/os_interface_win_tests.cpp @@ -44,7 +44,7 @@ TEST(OsContextTest, givenWddmWhenCreateOsContextBeforeInitWddmThenOsContextIsNot auto wddm = new WddmMock; OSInterface osInterface; osInterface.get()->setWddm(wddm); - EXPECT_THROW(auto osContext = std::make_unique(&osInterface), std::exception); + EXPECT_THROW(auto osContext = std::make_unique(&osInterface, 0u), std::exception); } TEST(OsContextTest, givenWddmWhenCreateOsContextAfterInitWddmThenOsContextIsInitialized) { @@ -52,13 +52,13 @@ TEST(OsContextTest, givenWddmWhenCreateOsContextAfterInitWddmThenOsContextIsInit OSInterface osInterface; osInterface.get()->setWddm(wddm); wddm->init(); - auto osContext = std::make_unique(&osInterface); + auto osContext = std::make_unique(&osInterface, 0u); EXPECT_NE(nullptr, osContext->get()); EXPECT_TRUE(osContext->get()->isInitialized()); EXPECT_EQ(osContext->get()->getWddm(), wddm); } TEST(OsContextTest, whenCreateOsContextWithoutOsInterfaceThenOsContextImplIsNotAvailable) { - auto osContext = std::make_unique(nullptr); + auto osContext = std::make_unique(nullptr, 0u); EXPECT_EQ(nullptr, osContext->get()); } diff --git a/unit_tests/os_interface/windows/wddm23_tests.cpp b/unit_tests/os_interface/windows/wddm23_tests.cpp index b1da1f32f2..3160459c5b 100644 --- a/unit_tests/os_interface/windows/wddm23_tests.cpp +++ b/unit_tests/os_interface/windows/wddm23_tests.cpp @@ -49,7 +49,7 @@ struct Wddm23TestsWithoutWddmInit : public ::testing::Test, GdiDllFixture, publi void init() { EXPECT_TRUE(wddm->init()); - osContext = std::make_unique(osInterface.get()); + osContext = std::make_unique(osInterface.get(), 0u); osContextWin = osContext->get(); } diff --git a/unit_tests/os_interface/windows/wddm_fixture.h b/unit_tests/os_interface/windows/wddm_fixture.h index be23e22fbc..8712df0376 100644 --- a/unit_tests/os_interface/windows/wddm_fixture.h +++ b/unit_tests/os_interface/windows/wddm_fixture.h @@ -41,7 +41,7 @@ struct WddmFixture : public GmmEnvironmentFixture { gdi = new MockGdi(); wddm->gdi.reset(gdi); wddm->init(); - osContext = std::make_unique(osInterface.get()); + osContext = std::make_unique(osInterface.get(), 0u); osContextWin = osContext->get(); ASSERT_TRUE(wddm->isInitialized()); } @@ -69,7 +69,7 @@ struct WddmFixtureWithMockGdiDll : public GmmEnvironmentFixture, public GdiDllFi void init() { EXPECT_TRUE(wddm->init()); - osContext = std::make_unique(osInterface.get()); + osContext = std::make_unique(osInterface.get(), 0u); osContextWin = osContext->get(); ASSERT_TRUE(wddm->isInitialized()); } diff --git a/unit_tests/os_interface/windows/wddm_memory_manager_tests.h b/unit_tests/os_interface/windows/wddm_memory_manager_tests.h index de756226c1..1625d6b7b1 100644 --- a/unit_tests/os_interface/windows/wddm_memory_manager_tests.h +++ b/unit_tests/os_interface/windows/wddm_memory_manager_tests.h @@ -64,7 +64,7 @@ class MockWddmMemoryManagerFixture : public GmmEnvironmentFixture { gdi = new MockGdi(); wddm->gdi.reset(gdi); EXPECT_TRUE(wddm->init()); - osContext = new OsContext(osInterface.get()); + osContext = new OsContext(osInterface.get(), 0u); osContext->incRefInternal(); uint64_t heap32Base = (uint64_t)(0x800000000000); if (sizeof(uintptr_t) == 4) { @@ -130,7 +130,7 @@ class WddmMemoryManagerFixtureWithGmockWddm : public GmmEnvironmentFixture { ASSERT_NE(nullptr, wddm); EXPECT_TRUE(wddm->init()); osInterface->get()->setWddm(wddm); - osContext = new OsContext(osInterface.get()); + osContext = new OsContext(osInterface.get(), 0u); osContext->incRefInternal(); wddm->init(); memoryManager = new (std::nothrow) MockWddmMemoryManager(wddm); diff --git a/unit_tests/os_interface/windows/wddm_preemption_tests.cpp b/unit_tests/os_interface/windows/wddm_preemption_tests.cpp index 2db64dc13f..b68b75afba 100644 --- a/unit_tests/os_interface/windows/wddm_preemption_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_preemption_tests.cpp @@ -51,7 +51,7 @@ class WddmPreemptionTests : public Test { PreemptionMode preemptionMode = PreemptionHelper::getDefaultPreemptionMode(hwInfoTest); wddm->setPreemptionMode(preemptionMode); wddm->init(); - osContext = std::make_unique(osInterface.get()); + osContext = std::make_unique(osInterface.get(), 0u); osContextWin = osContext->get(); }