From e34c47271b6dd0ac493caed77cabbd6559cb4c89 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Fri, 13 Jul 2018 07:42:18 +0200 Subject: [PATCH] Fix destruction sequence in execution environment. - Gmm needs to be closed after memory manager. Change-Id: I608fc328034012ce52b7e791afd9ad2ff2f0cd1a --- .../execution_environment.h | 6 ++-- runtime/gmm_helper/gmm_helper.h | 2 +- .../execution_environment_tests.cpp | 33 +++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/runtime/execution_environment/execution_environment.h b/runtime/execution_environment/execution_environment.h index 7c09c83620..6b71b1ce8f 100644 --- a/runtime/execution_environment/execution_environment.h +++ b/runtime/execution_environment/execution_environment.h @@ -32,14 +32,14 @@ class ExecutionEnvironment : public ReferenceTrackedObject private: DeviceFactoryCleaner cleaner; + protected: + std::unique_ptr gmmHelper; + public: ExecutionEnvironment(); ~ExecutionEnvironment() override; void initGmm(const HardwareInfo *hwInfo); std::unique_ptr commandStreamReceiver; std::unique_ptr memoryManager; - - protected: - std::unique_ptr gmmHelper; }; } // namespace OCLRT diff --git a/runtime/gmm_helper/gmm_helper.h b/runtime/gmm_helper/gmm_helper.h index e6fa6f8343..c27b526ade 100644 --- a/runtime/gmm_helper/gmm_helper.h +++ b/runtime/gmm_helper/gmm_helper.h @@ -41,7 +41,7 @@ class GmmHelper { public: GmmHelper() = delete; GmmHelper(const HardwareInfo *hwInfo); - ~GmmHelper(); + MOCKABLE_VIRTUAL ~GmmHelper(); static constexpr uint32_t cacheDisabledIndex = 0; static constexpr uint32_t cacheEnabledIndex = 4; static constexpr uint32_t maxPossiblePitch = 2147483648; diff --git a/unit_tests/execution_environment/execution_environment_tests.cpp b/unit_tests/execution_environment/execution_environment_tests.cpp index a03a58cc52..de67ced6ee 100644 --- a/unit_tests/execution_environment/execution_environment_tests.cpp +++ b/unit_tests/execution_environment/execution_environment_tests.cpp @@ -22,7 +22,10 @@ #include "test.h" #include "runtime/device/device.h" +#include "runtime/gmm_helper/gmm_helper.h" #include "runtime/execution_environment/execution_environment.h" +#include "runtime/memory_manager/os_agnostic_memory_manager.h" +#include "runtime/helpers/options.h" #include "runtime/platform/platform.h" using namespace OCLRT; @@ -91,4 +94,34 @@ TEST(ExecutionEnvironment, givenDeviceWhenItIsDestroyedThenMemoryManagerIsStillA std::unique_ptr device(Device::create(nullptr, executionEnvironment.get())); device.reset(nullptr); EXPECT_NE(nullptr, executionEnvironment->memoryManager); +} + +auto destructorId = 0u; +static_assert(sizeof(ExecutionEnvironment) == (is64bit ? 48 : 28), "New members detected in ExecutionEnvironment, please ensure that destruction sequence of objects is correct"); + +TEST(ExecutionEnvironment, givenExecutionEnvironmentWithVariousMembersWhenItIsDestroyedThenDeleteSequenceIsSpecified) { + destructorId = 0u; + struct GmmHelperMock : public GmmHelper { + using GmmHelper::GmmHelper; + ~GmmHelperMock() override { + EXPECT_EQ(destructorId, 1u); + destructorId++; + } + }; + struct MemoryMangerMock : public OsAgnosticMemoryManager { + ~MemoryMangerMock() override { + EXPECT_EQ(destructorId, 0u); + destructorId++; + } + }; + struct MockExecutionEnvironment : ExecutionEnvironment { + using ExecutionEnvironment::gmmHelper; + }; + + std::unique_ptr executionEnvironment(new MockExecutionEnvironment); + executionEnvironment->gmmHelper.reset(new GmmHelperMock(platformDevices[0])); + executionEnvironment->memoryManager.reset(new MemoryMangerMock); + + executionEnvironment.reset(nullptr); + EXPECT_EQ(2u, destructorId); } \ No newline at end of file