From 366a12e3cea4c9ed5c148af7e7f58e5a987537c2 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Fri, 20 Jul 2018 09:01:58 +0200 Subject: [PATCH] Move setting external memory managers to mock layers. Change-Id: I335fd35269da9093c9e744c2735215e8d561098c --- runtime/device/device.cpp | 2 +- runtime/device/device.h | 15 ++++++--- .../execution_environment.cpp | 12 +++---- .../execution_environment.h | 2 +- .../execution_environment_tests.cpp | 10 +----- unit_tests/mocks/mock_device.cpp | 9 ++++-- unit_tests/mocks/mock_device.h | 32 ++++++++++++------- 7 files changed, 45 insertions(+), 37 deletions(-) diff --git a/runtime/device/device.cpp b/runtime/device/device.cpp index a0688f8674..2f43ce72ba 100644 --- a/runtime/device/device.cpp +++ b/runtime/device/device.cpp @@ -132,7 +132,7 @@ bool Device::createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice) { return false; } - executionEnvironment->initializeMemoryManager(outDevice.executionEnvironment->memoryManager.get(), outDevice.getEnabled64kbPages()); + executionEnvironment->initializeMemoryManager(outDevice.getEnabled64kbPages()); CommandStreamReceiver *commandStreamReceiver = executionEnvironment->commandStreamReceiver.get(); if (!commandStreamReceiver->initializeTagAllocation()) { diff --git a/runtime/device/device.h b/runtime/device/device.h index 619b2e7705..11a38bb696 100644 --- a/runtime/device/device.h +++ b/runtime/device/device.h @@ -53,11 +53,7 @@ class Device : public BaseObject<_cl_device_id> { static T *create(const HardwareInfo *pHwInfo, ExecutionEnvironment *execEnv) { pHwInfo = getDeviceInitHwInfo(pHwInfo); T *device = new T(*pHwInfo, execEnv); - if (false == createDeviceImpl(pHwInfo, *device)) { - delete device; - return nullptr; - } - return device; + return createDeviceInternals(pHwInfo, device); } Device &operator=(const Device &) = delete; @@ -139,6 +135,15 @@ class Device : public BaseObject<_cl_device_id> { Device() = delete; Device(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment); + template + static T *createDeviceInternals(const HardwareInfo *pHwInfo, T *device) { + if (false == createDeviceImpl(pHwInfo, *device)) { + delete device; + return nullptr; + } + return device; + } + static bool createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice); static const HardwareInfo *getDeviceInitHwInfo(const HardwareInfo *pHwInfoIn); MOCKABLE_VIRTUAL void initializeCaps(); diff --git a/runtime/execution_environment/execution_environment.cpp b/runtime/execution_environment/execution_environment.cpp index a48683db7b..f0c8b2afe1 100644 --- a/runtime/execution_environment/execution_environment.cpp +++ b/runtime/execution_environment/execution_environment.cpp @@ -45,17 +45,15 @@ bool ExecutionEnvironment::initializeCommandStreamReceiver(const HardwareInfo *p this->commandStreamReceiver.reset(commandStreamReceiver); return true; } -void ExecutionEnvironment::initializeMemoryManager(MemoryManager *externalMemoryManager, bool enable64KBpages) { +void ExecutionEnvironment::initializeMemoryManager(bool enable64KBpages) { if (this->memoryManager) { commandStreamReceiver->setMemoryManager(this->memoryManager.get()); return; } - if (!externalMemoryManager) { - memoryManager.reset(commandStreamReceiver->createMemoryManager(enable64KBpages)); - commandStreamReceiver->setMemoryManager(memoryManager.get()); - } else { - commandStreamReceiver->setMemoryManager(externalMemoryManager); - } + + memoryManager.reset(commandStreamReceiver->createMemoryManager(enable64KBpages)); + commandStreamReceiver->setMemoryManager(memoryManager.get()); + DEBUG_BREAK_IF(!this->memoryManager); } diff --git a/runtime/execution_environment/execution_environment.h b/runtime/execution_environment/execution_environment.h index 405e0be605..e68d3c8e1d 100644 --- a/runtime/execution_environment/execution_environment.h +++ b/runtime/execution_environment/execution_environment.h @@ -40,7 +40,7 @@ class ExecutionEnvironment : public ReferenceTrackedObject ~ExecutionEnvironment() override; void initGmm(const HardwareInfo *hwInfo); bool initializeCommandStreamReceiver(const HardwareInfo *pHwInfo); - void initializeMemoryManager(MemoryManager *externalMemoryManager, bool enable64KBpages); + void initializeMemoryManager(bool enable64KBpages); std::unique_ptr memoryManager; std::unique_ptr commandStreamReceiver; }; diff --git a/unit_tests/execution_environment/execution_environment_tests.cpp b/unit_tests/execution_environment/execution_environment_tests.cpp index 5779b0a971..8136778caa 100644 --- a/unit_tests/execution_environment/execution_environment_tests.cpp +++ b/unit_tests/execution_environment/execution_environment_tests.cpp @@ -106,18 +106,10 @@ TEST(ExecutionEnvironment, givenExecutionEnvironmentWhenInitializeCommandStreamR TEST(ExecutionEnvironment, givenExecutionEnvironmentWhenInitializeMemoryManagerIsCalledThenItIsInitalized) { std::unique_ptr executionEnvironment(new ExecutionEnvironment); executionEnvironment->initializeCommandStreamReceiver(platformDevices[0]); - executionEnvironment->initializeMemoryManager(nullptr, false); + executionEnvironment->initializeMemoryManager(false); EXPECT_NE(nullptr, executionEnvironment->memoryManager); } -TEST(ExecutionEnvironment, givenExecutionEnvironmentWhenInitializeMemoryManagerWithExternalMemoryManagerIsCalledThenItIsSetToExternal) { - std::unique_ptr memoryManager(new OsAgnosticMemoryManager); - std::unique_ptr executionEnvironment(new ExecutionEnvironment); - executionEnvironment->initializeCommandStreamReceiver(platformDevices[0]); - executionEnvironment->initializeMemoryManager(memoryManager.get(), false); - EXPECT_EQ(memoryManager.get(), executionEnvironment->commandStreamReceiver->getMemoryManager()); -} - auto destructorId = 0u; static_assert(sizeof(ExecutionEnvironment) == (is64bit ? 48 : 28), "New members detected in ExecutionEnvironment, please ensure that destruction sequence of objects is correct"); diff --git a/unit_tests/mocks/mock_device.cpp b/unit_tests/mocks/mock_device.cpp index 0f80d2cd50..703da08068 100644 --- a/unit_tests/mocks/mock_device.cpp +++ b/unit_tests/mocks/mock_device.cpp @@ -34,17 +34,22 @@ MockDevice::MockDevice(const HardwareInfo &hwInfo) : MockDevice(hwInfo, new ExecutionEnvironment) { CommandStreamReceiver *commandStreamReceiver = createCommandStream(&hwInfo); executionEnvironment->commandStreamReceiver.reset(commandStreamReceiver); + commandStreamReceiver->setMemoryManager(this->mockMemoryManager.get()); + this->executionEnvironment->memoryManager = std::move(this->mockMemoryManager); } OCLRT::MockDevice::MockDevice(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment) : Device(hwInfo, executionEnvironment) { - this->executionEnvironment->memoryManager.reset(new OsAgnosticMemoryManager); + this->mockMemoryManager.reset(new OsAgnosticMemoryManager); this->osTime = MockOSTime::create(); mockWaTable = *hwInfo.pWaTable; } void MockDevice::setMemoryManager(MemoryManager *memoryManager) { executionEnvironment->memoryManager.reset(memoryManager); + if (executionEnvironment->commandStreamReceiver) { + executionEnvironment->commandStreamReceiver->setMemoryManager(memoryManager); + } } void MockDevice::setOSTime(OSTime *osTime) { @@ -87,5 +92,5 @@ OCLRT::FailMemoryManager::FailMemoryManager(int32_t fail) : MockMemoryManager() } MockAlignedMallocManagerDevice::MockAlignedMallocManagerDevice(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment) : MockDevice(hwInfo, executionEnvironment) { - executionEnvironment->memoryManager.reset(new MockAllocSysMemAgnosticMemoryManager()); + this->mockMemoryManager.reset(new MockAllocSysMemAgnosticMemoryManager()); } diff --git a/unit_tests/mocks/mock_device.h b/unit_tests/mocks/mock_device.h index e23567f937..13540975bf 100644 --- a/unit_tests/mocks/mock_device.h +++ b/unit_tests/mocks/mock_device.h @@ -106,6 +106,7 @@ class MockDevice : public Device { void setSourceLevelDebuggerActive(bool active) { this->deviceInfo.sourceLevelDebuggerActive = active; } + template static T *createWithMemoryManager(const HardwareInfo *pHwInfo, MemoryManager *memManager) { @@ -114,15 +115,16 @@ class MockDevice : public Device { if (memManager) { device->setMemoryManager(memManager); } - if (false == createDeviceImpl(pHwInfo, *device)) { - delete device; - return nullptr; - } - return device; + return createDeviceInternals(pHwInfo, device); } + template static T *createWithNewExecutionEnvironment(const HardwareInfo *pHwInfo) { - return Device::create(pHwInfo, new ExecutionEnvironment); + auto executionEnvironment = new ExecutionEnvironment; + pHwInfo = getDeviceInitHwInfo(pHwInfo); + T *device = new T(*pHwInfo, executionEnvironment); + executionEnvironment->memoryManager = std::move(device->mockMemoryManager); + return createDeviceInternals(pHwInfo, device); } void allocatePreemptionAllocationIfNotPresent() { @@ -136,6 +138,7 @@ class MockDevice : public Device { } } } + std::unique_ptr mockMemoryManager; private: bool forceWhitelistedRegs = false; @@ -143,6 +146,11 @@ class MockDevice : public Device { WorkaroundTable mockWaTable = {}; }; +template <> +inline Device *MockDevice::createWithNewExecutionEnvironment(const HardwareInfo *pHwInfo) { + return Device::create(pHwInfo, new ExecutionEnvironment); +} + class FailMemoryManager : public MockMemoryManager { public: FailMemoryManager(); @@ -207,19 +215,19 @@ class FailMemoryManager : public MockMemoryManager { std::vector allocations; }; -class FailDevice : public Device { +class FailDevice : public MockDevice { public: FailDevice(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment) - : Device(hwInfo, executionEnvironment) { - this->executionEnvironment->memoryManager.reset(new FailMemoryManager); + : MockDevice(hwInfo, executionEnvironment) { + this->mockMemoryManager.reset(new FailMemoryManager); } }; -class FailDeviceAfterOne : public Device { +class FailDeviceAfterOne : public MockDevice { public: FailDeviceAfterOne(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment) - : Device(hwInfo, executionEnvironment) { - this->executionEnvironment->memoryManager.reset(new FailMemoryManager(1)); + : MockDevice(hwInfo, executionEnvironment) { + this->mockMemoryManager.reset(new FailMemoryManager(1)); } };