From 1909d43dcc424c84452a7f8e70f350a0eec996e0 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 23 Nov 2021 17:57:45 -0800 Subject: [PATCH 01/20] initial version ported from an old implementation Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Tests/Memory/AllocatorBenchmarks.cpp | 483 ++++++++++++++++++ .../Memory/AllocatorBenchmarks_Linux.cpp | 31 ++ .../Platform/Linux/platform_linux_files.cmake | 1 + .../Tests/Memory/AllocatorBenchmarks_Mac.cpp | 30 ++ .../Platform/Mac/platform_mac_files.cmake | 1 + .../Memory/AllocatorBenchmarks_Windows.cpp | 74 +++ .../Windows/platform_windows_files.cmake | 1 + .../AzCore/Tests/azcoretests_files.cmake | 1 + 8 files changed, 622 insertions(+) create mode 100644 Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp create mode 100644 Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp create mode 100644 Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp create mode 100644 Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp new file mode 100644 index 0000000000..2bac12fe83 --- /dev/null +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -0,0 +1,483 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#if defined(HAVE_BENCHMARK) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace Benchmark +{ + namespace Platform + { + size_t GetProcessMemoryUsageBytes(); + size_t GetMemorySize(void* memory); + } + + static AZ::Debug::DrillerManager* s_drillerManager = nullptr; + + template + class TestAllocator : public TAllocator + { + public: + TestAllocator() + : TAllocator() + { + } + + static void SetUp() + { + AZ::AllocatorInstance::Create(); + + s_drillerManager = AZ::Debug::DrillerManager::Create(); + s_drillerManager->Register(aznew AZ::Debug::MemoryDriller); + } + + static void TearDown() + { + AZ::Debug::DrillerManager::Destroy(s_drillerManager); + s_drillerManager = nullptr; + + AZ::AllocatorInstance::Destroy(); + } + + typename TAllocator::pointer_type Allocate( + typename TAllocator::size_type byteSize, + typename TAllocator::size_type alignment, + int = 0, + const char* = nullptr, + const char* = nullptr, + int = 0, + unsigned int = 0) override + { + return AZ::AllocatorInstance::Get().Allocate(byteSize, alignment); + } + + void DeAllocate( + typename TAllocator::pointer_type ptr, + typename TAllocator::size_type byteSize = 0, + typename TAllocator::size_type = 0) override + { + AZ::AllocatorInstance::Get().DeAllocate(ptr, byteSize); + } + + typename TAllocator::pointer_type ReAllocate( + typename TAllocator::pointer_type ptr, + typename TAllocator::size_type newSize, + typename TAllocator::size_type newAlignment) override + { + return AZ::AllocatorInstance::Get().ReAllocate(ptr, newSize, newAlignment); + } + + typename TAllocator::size_type Resize(typename TAllocator::pointer_type ptr, typename TAllocator::size_type newSize) override + { + return AZ::AllocatorInstance::Get().Resize(ptr, newSize); + } + + void GarbageCollect() override + { + AZ::AllocatorInstance::Get().GarbageCollect(); + } + + typename TAllocator::size_type NumAllocatedBytes() const override + { + return AZ::AllocatorInstance::Get().NumAllocatedBytes() + + AZ::AllocatorInstance::Get().GetUnAllocatedMemory(); + } + }; +} + +namespace AZ +{ + AZ_TYPE_INFO_TEMPLATE(Benchmark::TestAllocator, "{ACE2D6E5-4EB8-4DD2-AE95-6BDFD0476801}", AZ_TYPE_INFO_CLASS); +} + +namespace Benchmark +{ + class TestRawMallocAllocator {}; + + template <> + class TestAllocator + : public TestRawMallocAllocator + { + public: + struct Descriptor {}; + + TestAllocator() + {} + + static void SetUp() + { + s_numAllocatedBytes = 0; + } + + static void TearDown() + {} + + void* Allocate( + size_t byteSize, + size_t alignment, + int = 0, + const char* = nullptr, + const char* = nullptr, + int = 0, + unsigned int = 0) + { + s_numAllocatedBytes += byteSize; + if (alignment) + { + return AZ_OS_MALLOC(byteSize, alignment); + } + else + { + return AZ_OS_MALLOC(byteSize, 1); + } + } + + static void DeAllocate(void* ptr, size_t = 0) + { + s_numAllocatedBytes -= Platform::GetMemorySize(ptr); + AZ_OS_FREE(ptr); + } + + static void* ReAllocate(void* ptr, size_t newSize, size_t newAlignment) + { + s_numAllocatedBytes -= Platform::GetMemorySize(ptr); + AZ_OS_FREE(ptr); + + s_numAllocatedBytes += newSize; + if (newAlignment) + { + return AZ_OS_MALLOC(newSize, newAlignment); + } + else + { + return AZ_OS_MALLOC(newSize, 1); + } + } + + static size_t Resize(void* ptr, size_t newSize) + { + AZ_UNUSED(ptr); + AZ_UNUSED(newSize); + + return 0; + } + + static void GarbageCollect() + {} + + static size_t NumAllocatedBytes() + { + return s_numAllocatedBytes; + } + + private: + static size_t s_numAllocatedBytes; + }; + + size_t TestAllocator::s_numAllocatedBytes = 0; + + // Here we require to implement this to be able to configure a name for the allocator, otherswise the AllocatorManager crashes when trying to configure the overrides + class TestMallocSchemaAllocator : public AZ::SimpleSchemaAllocator + { + public: + AZ_TYPE_INFO(TestMallocSchemaAllocator, "{3E68224F-E676-402C-8276-CE4B49C05E89}"); + + TestMallocSchemaAllocator() + : AZ::SimpleSchemaAllocator("TestMallocSchemaAllocator", "") + {} + }; + + class TestHeapSchemaAllocator : public AZ::SimpleSchemaAllocator + { + public: + AZ_TYPE_INFO(TestHeapSchemaAllocator, "{456E6C30-AA84-488F-BE47-5C1E6AF636B7}"); + + TestHeapSchemaAllocator() + : AZ::SimpleSchemaAllocator("TestHeapSchemaAllocator", "") + {} + }; + + class TestHphaSchemaAllocator : public AZ::SimpleSchemaAllocator + { + public: + AZ_TYPE_INFO(TestHphaSchemaAllocator, "{6563AB4B-A68E-4499-8C98-D61D640D1F7F}"); + + TestHphaSchemaAllocator() + : AZ::SimpleSchemaAllocator("TestHphaSchemaAllocator", "") + {} + }; + + class TestSystemAllocator : public AZ::SystemAllocator + { + public: + AZ_TYPE_INFO(TestSystemAllocator, "{360D4DAA-D65D-4D5C-A6FA-1A4C5261C35C}"); + + TestSystemAllocator() + : AZ::SystemAllocator() + {} + }; +} + +namespace AZ +{ + AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{1065B446-4873-4B3E-9CB1-069E148D4DF6}"); + AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{92CFDF86-02EE-4247-9809-884EE9F7BA18}"); + AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{67DA01DF-9232-493A-B11C-6952FEDEB2A9}"); + AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{47384CB4-6729-43A9-B0CE-402E3A7AEFB2}"); + AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{096423BC-DC36-48D2-8E89-8F1D600F488A}"); +} + +namespace Benchmark +{ + // Allocated bytes reported by the allocator / actually requested bytes + static const char* s_counterAllocatorMemoryRatio = "Allocator_MemoryRatio"; + + // Allocated bytes reported by the process / actually requested bytes + static const char* s_counterProcessMemoryRatio = "Process_MemoryRatio"; + + enum AllocationSize + { + SMALL, + BIG, + MIXED, + COUNT + }; + + static const size_t s_kiloByte = 1024; + static const size_t s_megaByte = s_kiloByte * s_kiloByte; + using AllocationSizeArray = AZStd::array; + static const AZStd::array s_allocationSizes = { + /* SMALL */ AllocationSizeArray{ 2, 16, 20, 59, 100, 128, 160, 250, 300, 512 }, + /* BIG */ AllocationSizeArray{ 513, s_kiloByte, 2 * s_kiloByte, 4 * s_kiloByte, 10 * s_kiloByte, 64 * s_kiloByte, 128 * s_kiloByte, 200 * s_kiloByte, s_megaByte, 2 * s_megaByte }, + /* MIXED */ AllocationSizeArray{ 2, s_kiloByte, 59, 4 * s_kiloByte, 128, 200 * s_kiloByte, 250, s_megaByte, 512, 2 * s_megaByte } + }; + + template + class AllocatorBenchmarkFixture + : public ::benchmark::Fixture + { + protected: + using TestAllocatorType = TestAllocator; + + virtual void internalSetUp(const ::benchmark::State&) + { + TestAllocatorType::SetUp(); + } + + virtual void internalTearDown(const ::benchmark::State&) + { + TestAllocatorType::TearDown(); + } + + public: + void SetUp(const ::benchmark::State& state) override + { + internalSetUp(state); + } + void SetUp(::benchmark::State& state) override + { + internalSetUp(state); + } + + void TearDown(const ::benchmark::State& state) override + { + internalTearDown(state); + } + void TearDown(::benchmark::State& state) override + { + internalTearDown(state); + } + }; + + template + class AllocationBenchmarkFixture + : public AllocatorBenchmarkFixture + { + using TestAllocatorType = AllocatorBenchmarkFixture::TestAllocatorType; + + void internalSetUp(const ::benchmark::State& state) override + { + AllocatorBenchmarkFixture::SetUp(state); + + m_allocations.resize(state.range_x(), nullptr); + } + + void internalTearDown(const ::benchmark::State& state) override + { + m_allocations.clear(); + m_allocations.shrink_to_fit(); + + AllocatorBenchmarkFixture::TearDown(state); + } + + public: + void Benchmark(benchmark::State& state) + { + TestAllocatorType allocatorType; + + for (auto _ : state) + { + const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); + + const size_t numberOfAllocations = m_allocations.size(); + for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) + { + state.PauseTiming(); + const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; + const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; + + state.ResumeTiming(); + m_allocations[allocationIndex] = allocatorType.Allocate(allocationSize, 0); + } + + state.PauseTiming(); + state.counters[s_counterAllocatorMemoryRatio] = + benchmark::Counter(static_cast(allocatorType.NumAllocatedBytes()), benchmark::Counter::kDefaults); + state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( + static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), + benchmark::Counter::kDefaults); + + for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) + { + const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; + const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; + allocatorType.DeAllocate(m_allocations[allocationIndex], allocationSize); + m_allocations[allocationIndex] = nullptr; + } + allocatorType.GarbageCollect(); + + state.SetItemsProcessed(numberOfAllocations); + state.ResumeTiming(); + } + } + + private: + AZStd::vector m_allocations; + }; + + template + class DeAllocationBenchmarkFixture + : public AllocatorBenchmarkFixture + { + using TestAllocatorType = AllocatorBenchmarkFixture::TestAllocatorType; + + void internalSetUp(const ::benchmark::State& state) override + { + AllocatorBenchmarkFixture::SetUp(state); + + m_allocations.resize(state.range_x(), nullptr); + } + + void internalTearDown(const ::benchmark::State& state) override + { + m_allocations.clear(); + m_allocations.shrink_to_fit(); + + AllocatorBenchmarkFixture::TearDown(state); + } + public: + void Benchmark(benchmark::State& state) + { + TestAllocatorType allocatorType; + + for (auto _ : state) + { + state.PauseTiming(); + const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); + + const size_t numberOfAllocations = m_allocations.size(); + for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) + { + const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; + const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; + m_allocations[allocationIndex] = allocatorType.Allocate(allocationSize, 0); + } + + for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) + { + const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; + const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; + state.ResumeTiming(); + allocatorType.DeAllocate(m_allocations[allocationIndex], allocationSize); + state.PauseTiming(); + m_allocations[allocationIndex] = nullptr; + } + + state.counters[s_counterAllocatorMemoryRatio] = + benchmark::Counter(static_cast(allocatorType.NumAllocatedBytes()), benchmark::Counter::kDefaults); + state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( + static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), + benchmark::Counter::kDefaults); + + state.SetItemsProcessed(numberOfAllocations); + + allocatorType.GarbageCollect(); + + state.ResumeTiming(); + } + } + + private: + AZStd::vector m_allocations; + }; + + static void RunRanges(benchmark::internal::Benchmark* b) + { + for (int i = 0; i < 6; ++i) + { + b->Arg((1 << i) * 1000); + } + } + +#define BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME, ...) \ + BENCHMARK_TEMPLATE_DEFINE_F(FIXTURE, TESTNAME, __VA_ARGS__)(benchmark::State& state) { Benchmark(state); } \ + BENCHMARK_REGISTER_F(FIXTURE, TESTNAME) + +#define BM_REGISTER_SIZE_FIXTURES(FIXTURE, TESTNAME, ALLOCATORTYPE) \ + BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_SMALL, ALLOCATORTYPE, SMALL)->Apply(RunRanges); \ + BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_BIG, ALLOCATORTYPE, BIG)->Apply(RunRanges); \ + BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED, ALLOCATORTYPE, MIXED)->Apply(RunRanges); + +#define BM_REGISTER_ALLOCATOR(TESTNAME, ALLOCATORTYPE) \ + namespace TESTNAME \ + { \ + BM_REGISTER_SIZE_FIXTURES(AllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE) \ + BM_REGISTER_SIZE_FIXTURES(DeAllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE) \ + } + + BM_REGISTER_ALLOCATOR(RawMallocAllocator, TestRawMallocAllocator); + BM_REGISTER_ALLOCATOR(MallocSchemaAllocator, TestMallocSchemaAllocator); + BM_REGISTER_ALLOCATOR(HeapSchemaAllocator, TestHeapSchemaAllocator); + BM_REGISTER_ALLOCATOR(HphaSchemaAllocator, TestHphaSchemaAllocator); + BM_REGISTER_ALLOCATOR(SystemAllocator, TestSystemAllocator); + + //BM_REGISTER_SCHEMA(BestFitExternalMapSchema); // Requires to implement AZ::IAllocatorAllocate + //BM_REGISTER_SCHEMA(PoolSchema); // Requires special alignment requests while allocating + +#undef BM_REGISTER_ALLOCATOR +#undef BM_REGISTER_SIZE_FIXTURES +#undef BM_REGISTER_TEMPLATE + +} // Benchmark + +#endif // HAVE_BENCHMARK diff --git a/Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp b/Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp new file mode 100644 index 0000000000..49ecefda49 --- /dev/null +++ b/Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include + +#include +#include + +namespace Benchmark +{ + namespace Platform + { + size_t GetProcessMemoryUsageBytes() + { + struct rusage rusage; + getrusage(RUSAGE_SELF, &rusage); + return rusage.ru_maxrss * 1024L; + } + + size_t GetMemorySize(void* memory) + { + return memory ? _aligned_msize(memory, 1, 0) : 0; + } + } +} diff --git a/Code/Framework/AzCore/Tests/Platform/Linux/platform_linux_files.cmake b/Code/Framework/AzCore/Tests/Platform/Linux/platform_linux_files.cmake index 844b621e05..953dbb7791 100644 --- a/Code/Framework/AzCore/Tests/Platform/Linux/platform_linux_files.cmake +++ b/Code/Framework/AzCore/Tests/Platform/Linux/platform_linux_files.cmake @@ -9,4 +9,5 @@ set(FILES Tests/UtilsTests_Linux.cpp ../Common/UnixLike/Tests/UtilsTests_UnixLike.cpp + Tests/Memory/AllocatorBenchmarks_Linux.cpp ) diff --git a/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp b/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp new file mode 100644 index 0000000000..374d9f81f7 --- /dev/null +++ b/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include + +#include + +namespace Benchmark +{ + namespace Platform + { + size_t GetProcessMemoryUsageBytes() + { + struct rusage rusage; + getrusage(RUSAGE_SELF, &rusage); + return rusage.ru_maxrss; + } + + size_t GetMemorySize(void* memory) + { + return memory ? _aligned_msize(memory, 1, 0) : 0; + } + } +} diff --git a/Code/Framework/AzCore/Tests/Platform/Mac/platform_mac_files.cmake b/Code/Framework/AzCore/Tests/Platform/Mac/platform_mac_files.cmake index 93d2daf2b8..14e39d47f4 100644 --- a/Code/Framework/AzCore/Tests/Platform/Mac/platform_mac_files.cmake +++ b/Code/Framework/AzCore/Tests/Platform/Mac/platform_mac_files.cmake @@ -9,4 +9,5 @@ set(FILES ../Common/Apple/Tests/UtilsTests_Apple.cpp ../Common/UnixLike/Tests/UtilsTests_UnixLike.cpp + Tests/Memory/AllocatorBenchmarks_Mac.cpp ) diff --git a/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp b/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp new file mode 100644 index 0000000000..9f4efd7a2c --- /dev/null +++ b/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include + +#include +#include + +namespace Benchmark +{ + namespace Platform + { + size_t GetProcessMemoryUsageBytes() + { + EmptyWorkingSet(GetCurrentProcess()); + + //PROCESS_MEMORY_COUNTERS_EX pmc; + //GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&pmc, sizeof(pmc)); + //return pmc.PrivateUsage; + //return pmc.WorkingSetSize; + + //size_t memoryUsage = 0; + //HANDLE defaultProcessHeap = GetProcessHeap(); + //PROCESS_HEAP_ENTRY heapEntry; + //if (HeapLock(defaultProcessHeap) == FALSE) + //{ + // AZ_Error("Benchmark", false, "Could not lock process' heap, error: %d", GetLastError()); + // return memoryUsage; + //} + + //heapEntry.lpData = NULL; + //while (HeapWalk(defaultProcessHeap, &heapEntry) != FALSE) + //{ + // memoryUsage += heapEntry.cbData; + //} + + //DWORD lastError = GetLastError(); + //if (lastError != ERROR_NO_MORE_ITEMS) + //{ + // AZ_Error("Benchmark", false, "HeapWalk failed with LastError %d", lastError); + //} + + //if (HeapUnlock(defaultProcessHeap) == FALSE) + //{ + // AZ_Error("Benchmark", false, "Failed to unlock heap with LastError %d", GetLastError()); + //} + + //return memoryUsage; + + size_t memoryUsage = 0; + + MEMORY_BASIC_INFORMATION mbi = { 0 }; + unsigned char* pEndRegion = NULL; + while (sizeof(mbi) == VirtualQuery(pEndRegion, &mbi, sizeof(mbi))) { + pEndRegion += mbi.RegionSize; + if ((mbi.AllocationProtect & PAGE_READWRITE) && (mbi.State & MEM_COMMIT)) { + memoryUsage += mbi.RegionSize; + } + } + return memoryUsage; + } + + size_t GetMemorySize(void* memory) + { + return memory ? _aligned_msize(memory, 1, 0) : 0; + } + } +} diff --git a/Code/Framework/AzCore/Tests/Platform/Windows/platform_windows_files.cmake b/Code/Framework/AzCore/Tests/Platform/Windows/platform_windows_files.cmake index 0a96dad34e..97b12b28e6 100644 --- a/Code/Framework/AzCore/Tests/Platform/Windows/platform_windows_files.cmake +++ b/Code/Framework/AzCore/Tests/Platform/Windows/platform_windows_files.cmake @@ -9,6 +9,7 @@ set(FILES ../Common/WinAPI/Tests/UtilsTests_WinAPI.cpp Tests/IO/Streamer/StorageDriveTests_Windows.cpp + Tests/Memory/AllocatorBenchmarks_Windows.cpp Tests/Memory/OverrunDetectionAllocator_Windows.cpp Tests/Serialization_Windows.cpp ) diff --git a/Code/Framework/AzCore/Tests/azcoretests_files.cmake b/Code/Framework/AzCore/Tests/azcoretests_files.cmake index d39595c45e..6762e07d0a 100644 --- a/Code/Framework/AzCore/Tests/azcoretests_files.cmake +++ b/Code/Framework/AzCore/Tests/azcoretests_files.cmake @@ -170,6 +170,7 @@ set(FILES Math/Vector3Tests.cpp Math/Vector4PerformanceTests.cpp Math/Vector4Tests.cpp + Memory/AllocatorBenchmarks.cpp Memory/AllocatorManager.cpp Memory/HphaSchema.cpp Memory/HphaSchemaErrorDetection.cpp From 35c1751694ce245b2a033c195a83a094140ec1a0 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 23 Nov 2021 18:31:00 -0800 Subject: [PATCH 02/20] simplification of code Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Tests/Memory/AllocatorBenchmarks.cpp | 189 +++++++++--------- 1 file changed, 95 insertions(+), 94 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index 2bac12fe83..af87584fc6 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -34,15 +34,15 @@ namespace Benchmark static AZ::Debug::DrillerManager* s_drillerManager = nullptr; + /// + /// Test allocator wrapper that redirects the calls to the passed TAllocator by using AZ::AllocatorInstance. + /// It also creates/destroys the TAllocator type and connects the driller (to reflect what happens at runtime) + /// + /// Allocator type to wrap template - class TestAllocator : public TAllocator + class TestAllocatorWrapper { public: - TestAllocator() - : TAllocator() - { - } - static void SetUp() { AZ::AllocatorInstance::Create(); @@ -59,89 +59,81 @@ namespace Benchmark AZ::AllocatorInstance::Destroy(); } - typename TAllocator::pointer_type Allocate( - typename TAllocator::size_type byteSize, - typename TAllocator::size_type alignment, - int = 0, - const char* = nullptr, - const char* = nullptr, - int = 0, - unsigned int = 0) override + static void* Allocate(size_t byteSize, size_t alignment) { return AZ::AllocatorInstance::Get().Allocate(byteSize, alignment); } - void DeAllocate( - typename TAllocator::pointer_type ptr, - typename TAllocator::size_type byteSize = 0, - typename TAllocator::size_type = 0) override + static void DeAllocate(void* ptr, size_t byteSize = 0) { AZ::AllocatorInstance::Get().DeAllocate(ptr, byteSize); } - typename TAllocator::pointer_type ReAllocate( - typename TAllocator::pointer_type ptr, - typename TAllocator::size_type newSize, - typename TAllocator::size_type newAlignment) override + static void* ReAllocate(void* ptr, size_t newSize, size_t newAlignment) { return AZ::AllocatorInstance::Get().ReAllocate(ptr, newSize, newAlignment); } - typename TAllocator::size_type Resize(typename TAllocator::pointer_type ptr, typename TAllocator::size_type newSize) override + static size_t Resize(void* ptr, size_t newSize) { return AZ::AllocatorInstance::Get().Resize(ptr, newSize); } - void GarbageCollect() override + static void GarbageCollect() { AZ::AllocatorInstance::Get().GarbageCollect(); } - typename TAllocator::size_type NumAllocatedBytes() const override + static size_t NumAllocatedBytes() { return AZ::AllocatorInstance::Get().NumAllocatedBytes() + AZ::AllocatorInstance::Get().GetUnAllocatedMemory(); } }; -} -namespace AZ -{ - AZ_TYPE_INFO_TEMPLATE(Benchmark::TestAllocator, "{ACE2D6E5-4EB8-4DD2-AE95-6BDFD0476801}", AZ_TYPE_INFO_CLASS); -} - -namespace Benchmark -{ - class TestRawMallocAllocator {}; - - template <> - class TestAllocator - : public TestRawMallocAllocator + /// + /// Basic allocator used as a baseline. This allocator is the most basic allocation possible with the OS (AZ_OS_MALLOC). + /// MallocSchema cannot be used here because it has extra logic that we don't want to use as a baseline. + /// + class TestRawMallocAllocator + : public AZ::AllocatorBase + , public AZ::IAllocatorAllocate { public: + AZ_TYPE_INFO(TestMallocSchemaAllocator, "{08EB400A-D723-46C6-808E-D0844C8DE206}"); + struct Descriptor {}; - TestAllocator() - {} - - static void SetUp() + TestRawMallocAllocator() + : AllocatorBase(this, "TestRawMallocAllocator", "") { - s_numAllocatedBytes = 0; + m_numAllocatedBytes = 0; } - static void TearDown() - {} - - void* Allocate( - size_t byteSize, - size_t alignment, - int = 0, - const char* = nullptr, - const char* = nullptr, - int = 0, - unsigned int = 0) + bool Create(const Descriptor&) { - s_numAllocatedBytes += byteSize; + m_numAllocatedBytes = 0; + return true; + } + + // IAllocator + void Destroy() override + { + m_numAllocatedBytes = 0; + } + AZ::AllocatorDebugConfig GetDebugConfig() override + { + return AZ::AllocatorDebugConfig(); + } + AZ::IAllocatorAllocate* GetSchema() override + { + return nullptr; + } + + // IAllocatorAllocate + void* Allocate(size_t byteSize, size_t alignment, int = 0, const char* = 0, const char* = 0, int = 0, unsigned int = 0) override + { + m_numAllocatedBytes += byteSize; if (alignment) { return AZ_OS_MALLOC(byteSize, alignment); @@ -152,18 +144,18 @@ namespace Benchmark } } - static void DeAllocate(void* ptr, size_t = 0) + void DeAllocate(void* ptr, size_t = 0, size_type = 0) override { - s_numAllocatedBytes -= Platform::GetMemorySize(ptr); + m_numAllocatedBytes -= Platform::GetMemorySize(ptr); AZ_OS_FREE(ptr); } - static void* ReAllocate(void* ptr, size_t newSize, size_t newAlignment) + void* ReAllocate(void* ptr, size_t newSize, size_t newAlignment) override { - s_numAllocatedBytes -= Platform::GetMemorySize(ptr); + m_numAllocatedBytes -= Platform::GetMemorySize(ptr); AZ_OS_FREE(ptr); - s_numAllocatedBytes += newSize; + m_numAllocatedBytes += newSize; if (newAlignment) { return AZ_OS_MALLOC(newSize, newAlignment); @@ -174,7 +166,7 @@ namespace Benchmark } } - static size_t Resize(void* ptr, size_t newSize) + size_t Resize(void* ptr, size_t newSize) override { AZ_UNUSED(ptr); AZ_UNUSED(newSize); @@ -182,20 +174,45 @@ namespace Benchmark return 0; } - static void GarbageCollect() - {} - - static size_t NumAllocatedBytes() + size_t AllocationSize(void* ptr) override { - return s_numAllocatedBytes; + return Platform::GetMemorySize(ptr); + } + + void GarbageCollect() override {} + + size_t NumAllocatedBytes() const override + { + return m_numAllocatedBytes; + } + + size_t Capacity() const override + { + return AZ_CORE_MAX_ALLOCATOR_SIZE; // unused + } + + size_t GetMaxAllocationSize() const override + { + return AZ_CORE_MAX_ALLOCATOR_SIZE; // unused + } + + size_t GetMaxContiguousAllocationSize() const override + { + return AZ_CORE_MAX_ALLOCATOR_SIZE; // unused + } + size_t GetUnAllocatedMemory(bool = false) const override + { + return 0; // unused + } + IAllocatorAllocate* GetSubAllocator() override + { + return nullptr; // unused } private: - static size_t s_numAllocatedBytes; + size_t m_numAllocatedBytes; }; - size_t TestAllocator::s_numAllocatedBytes = 0; - // Here we require to implement this to be able to configure a name for the allocator, otherswise the AllocatorManager crashes when trying to configure the overrides class TestMallocSchemaAllocator : public AZ::SimpleSchemaAllocator { @@ -236,19 +253,7 @@ namespace Benchmark : AZ::SystemAllocator() {} }; -} -namespace AZ -{ - AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{1065B446-4873-4B3E-9CB1-069E148D4DF6}"); - AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{92CFDF86-02EE-4247-9809-884EE9F7BA18}"); - AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{67DA01DF-9232-493A-B11C-6952FEDEB2A9}"); - AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{47384CB4-6729-43A9-B0CE-402E3A7AEFB2}"); - AZ_TYPE_INFO_SPECIALIZE(Benchmark::TestAllocator, "{096423BC-DC36-48D2-8E89-8F1D600F488A}"); -} - -namespace Benchmark -{ // Allocated bytes reported by the allocator / actually requested bytes static const char* s_counterAllocatorMemoryRatio = "Allocator_MemoryRatio"; @@ -277,7 +282,7 @@ namespace Benchmark : public ::benchmark::Fixture { protected: - using TestAllocatorType = TestAllocator; + using TestAllocatorType = TestAllocatorWrapper; virtual void internalSetUp(const ::benchmark::State&) { @@ -333,8 +338,6 @@ namespace Benchmark public: void Benchmark(benchmark::State& state) { - TestAllocatorType allocatorType; - for (auto _ : state) { const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); @@ -347,12 +350,12 @@ namespace Benchmark const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; state.ResumeTiming(); - m_allocations[allocationIndex] = allocatorType.Allocate(allocationSize, 0); + m_allocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); } state.PauseTiming(); state.counters[s_counterAllocatorMemoryRatio] = - benchmark::Counter(static_cast(allocatorType.NumAllocatedBytes()), benchmark::Counter::kDefaults); + benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); @@ -361,10 +364,10 @@ namespace Benchmark { const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; - allocatorType.DeAllocate(m_allocations[allocationIndex], allocationSize); + TestAllocatorType::DeAllocate(m_allocations[allocationIndex], allocationSize); m_allocations[allocationIndex] = nullptr; } - allocatorType.GarbageCollect(); + TestAllocatorType::GarbageCollect(); state.SetItemsProcessed(numberOfAllocations); state.ResumeTiming(); @@ -398,8 +401,6 @@ namespace Benchmark public: void Benchmark(benchmark::State& state) { - TestAllocatorType allocatorType; - for (auto _ : state) { state.PauseTiming(); @@ -410,7 +411,7 @@ namespace Benchmark { const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; - m_allocations[allocationIndex] = allocatorType.Allocate(allocationSize, 0); + m_allocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); } for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) @@ -418,20 +419,20 @@ namespace Benchmark const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; state.ResumeTiming(); - allocatorType.DeAllocate(m_allocations[allocationIndex], allocationSize); + TestAllocatorType::DeAllocate(m_allocations[allocationIndex], allocationSize); state.PauseTiming(); m_allocations[allocationIndex] = nullptr; } state.counters[s_counterAllocatorMemoryRatio] = - benchmark::Counter(static_cast(allocatorType.NumAllocatedBytes()), benchmark::Counter::kDefaults); + benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); state.SetItemsProcessed(numberOfAllocations); - allocatorType.GarbageCollect(); + TestAllocatorType::GarbageCollect(); state.ResumeTiming(); } From 3273b7621d10dc98d54864ef41b59284161f5c0d Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 23 Nov 2021 18:37:24 -0800 Subject: [PATCH 03/20] Fixes a recursive loop Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index af87584fc6..f27c92dfb2 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -322,7 +322,7 @@ namespace Benchmark void internalSetUp(const ::benchmark::State& state) override { - AllocatorBenchmarkFixture::SetUp(state); + AllocatorBenchmarkFixture::internalSetUp(state); m_allocations.resize(state.range_x(), nullptr); } @@ -332,7 +332,7 @@ namespace Benchmark m_allocations.clear(); m_allocations.shrink_to_fit(); - AllocatorBenchmarkFixture::TearDown(state); + AllocatorBenchmarkFixture::internalTearDown(state); } public: @@ -386,7 +386,7 @@ namespace Benchmark void internalSetUp(const ::benchmark::State& state) override { - AllocatorBenchmarkFixture::SetUp(state); + AllocatorBenchmarkFixture::internalSetUp(state); m_allocations.resize(state.range_x(), nullptr); } @@ -396,7 +396,7 @@ namespace Benchmark m_allocations.clear(); m_allocations.shrink_to_fit(); - AllocatorBenchmarkFixture::TearDown(state); + AllocatorBenchmarkFixture::internalTearDown(state); } public: void Benchmark(benchmark::State& state) From 160235e86f940a63dd7a06ccb12af39622a0eff8 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Mon, 29 Nov 2021 09:03:14 -0800 Subject: [PATCH 04/20] Removing commented code of different options for getting memory usage of a process Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Memory/AllocatorBenchmarks_Windows.cpp | 34 ------------------- 1 file changed, 34 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp b/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp index 9f4efd7a2c..c8532687e8 100644 --- a/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp +++ b/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp @@ -20,41 +20,7 @@ namespace Benchmark { EmptyWorkingSet(GetCurrentProcess()); - //PROCESS_MEMORY_COUNTERS_EX pmc; - //GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&pmc, sizeof(pmc)); - //return pmc.PrivateUsage; - //return pmc.WorkingSetSize; - - //size_t memoryUsage = 0; - //HANDLE defaultProcessHeap = GetProcessHeap(); - //PROCESS_HEAP_ENTRY heapEntry; - //if (HeapLock(defaultProcessHeap) == FALSE) - //{ - // AZ_Error("Benchmark", false, "Could not lock process' heap, error: %d", GetLastError()); - // return memoryUsage; - //} - - //heapEntry.lpData = NULL; - //while (HeapWalk(defaultProcessHeap, &heapEntry) != FALSE) - //{ - // memoryUsage += heapEntry.cbData; - //} - - //DWORD lastError = GetLastError(); - //if (lastError != ERROR_NO_MORE_ITEMS) - //{ - // AZ_Error("Benchmark", false, "HeapWalk failed with LastError %d", lastError); - //} - - //if (HeapUnlock(defaultProcessHeap) == FALSE) - //{ - // AZ_Error("Benchmark", false, "Failed to unlock heap with LastError %d", GetLastError()); - //} - - //return memoryUsage; - size_t memoryUsage = 0; - MEMORY_BASIC_INFORMATION mbi = { 0 }; unsigned char* pEndRegion = NULL; while (sizeof(mbi) == VirtualQuery(pEndRegion, &mbi, sizeof(mbi))) { From febaebc386225a091dc785e72d6bd99a2b3de196 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 30 Nov 2021 13:51:08 -0800 Subject: [PATCH 05/20] PR comment (NULL->nullptr) Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp b/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp index c8532687e8..e9571a7e5b 100644 --- a/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp +++ b/Code/Framework/AzCore/Tests/Platform/Windows/Tests/Memory/AllocatorBenchmarks_Windows.cpp @@ -22,7 +22,7 @@ namespace Benchmark size_t memoryUsage = 0; MEMORY_BASIC_INFORMATION mbi = { 0 }; - unsigned char* pEndRegion = NULL; + unsigned char* pEndRegion = nullptr; while (sizeof(mbi) == VirtualQuery(pEndRegion, &mbi, sizeof(mbi))) { pEndRegion += mbi.RegionSize; if ((mbi.AllocationProtect & PAGE_READWRITE) && (mbi.State & MEM_COMMIT)) { From 215cb9b52ffd0ee1f40325421f93d977754ed2ff Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 30 Nov 2021 16:06:26 -0800 Subject: [PATCH 06/20] Adds mulit-threaded tests Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Tests/Memory/AllocatorBenchmarks.cpp | 82 +++++++++++-------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index f27c92dfb2..7cd7e7ce0a 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -284,14 +284,34 @@ namespace Benchmark protected: using TestAllocatorType = TestAllocatorWrapper; - virtual void internalSetUp(const ::benchmark::State&) + virtual void internalSetUp(const ::benchmark::State& state) { - TestAllocatorType::SetUp(); + if (state.thread_index == 0) // Only setup in the first thread + { + TestAllocatorType::SetUp(); + + m_allocations.resize(state.threads); + for (auto& perThreadAllocations : m_allocations) + { + perThreadAllocations.resize(state.range_x(), nullptr); + } + } } - virtual void internalTearDown(const ::benchmark::State&) + virtual void internalTearDown(const ::benchmark::State& state) { - TestAllocatorType::TearDown(); + if (state.thread_index == 0) // Only setup in the first thread + { + m_allocations.clear(); + m_allocations.shrink_to_fit(); + + TestAllocatorType::TearDown(); + } + } + + AZStd::vector& GetPerThreadAllocations(size_t threadIndex) + { + return m_allocations[threadIndex]; } public: @@ -312,26 +332,25 @@ namespace Benchmark { internalTearDown(state); } + + private: + AZStd::vector> m_allocations; }; template class AllocationBenchmarkFixture : public AllocatorBenchmarkFixture { - using TestAllocatorType = AllocatorBenchmarkFixture::TestAllocatorType; + using base = AllocatorBenchmarkFixture; + using TestAllocatorType = base::TestAllocatorType; void internalSetUp(const ::benchmark::State& state) override { AllocatorBenchmarkFixture::internalSetUp(state); - - m_allocations.resize(state.range_x(), nullptr); } void internalTearDown(const ::benchmark::State& state) override { - m_allocations.clear(); - m_allocations.shrink_to_fit(); - AllocatorBenchmarkFixture::internalTearDown(state); } @@ -340,17 +359,19 @@ namespace Benchmark { for (auto _ : state) { + state.PauseTiming(); const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); - const size_t numberOfAllocations = m_allocations.size(); + AZStd::vector& perThreadAllocations = base::GetPerThreadAllocations(state.thread_index); + const size_t numberOfAllocations = perThreadAllocations.size(); for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) { - state.PauseTiming(); const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; state.ResumeTiming(); - m_allocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); + perThreadAllocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); + state.PauseTiming(); } state.PauseTiming(); @@ -364,8 +385,8 @@ namespace Benchmark { const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; - TestAllocatorType::DeAllocate(m_allocations[allocationIndex], allocationSize); - m_allocations[allocationIndex] = nullptr; + TestAllocatorType::DeAllocate(perThreadAllocations[allocationIndex], allocationSize); + perThreadAllocations[allocationIndex] = nullptr; } TestAllocatorType::GarbageCollect(); @@ -373,29 +394,22 @@ namespace Benchmark state.ResumeTiming(); } } - - private: - AZStd::vector m_allocations; }; template class DeAllocationBenchmarkFixture : public AllocatorBenchmarkFixture { - using TestAllocatorType = AllocatorBenchmarkFixture::TestAllocatorType; + using base = AllocatorBenchmarkFixture; + using TestAllocatorType = base::TestAllocatorType; void internalSetUp(const ::benchmark::State& state) override { AllocatorBenchmarkFixture::internalSetUp(state); - - m_allocations.resize(state.range_x(), nullptr); } void internalTearDown(const ::benchmark::State& state) override { - m_allocations.clear(); - m_allocations.shrink_to_fit(); - AllocatorBenchmarkFixture::internalTearDown(state); } public: @@ -404,14 +418,15 @@ namespace Benchmark for (auto _ : state) { state.PauseTiming(); + AZStd::vector& perThreadAllocations = base::GetPerThreadAllocations(state.thread_index); const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); - const size_t numberOfAllocations = m_allocations.size(); + const size_t numberOfAllocations = perThreadAllocations.size(); for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) { const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; - m_allocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); + perThreadAllocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); } for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) @@ -419,9 +434,9 @@ namespace Benchmark const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; state.ResumeTiming(); - TestAllocatorType::DeAllocate(m_allocations[allocationIndex], allocationSize); + TestAllocatorType::DeAllocate(perThreadAllocations[allocationIndex], allocationSize); state.PauseTiming(); - m_allocations[allocationIndex] = nullptr; + perThreadAllocations[allocationIndex] = nullptr; } state.counters[s_counterAllocatorMemoryRatio] = @@ -437,9 +452,6 @@ namespace Benchmark state.ResumeTiming(); } } - - private: - AZStd::vector m_allocations; }; static void RunRanges(benchmark::internal::Benchmark* b) @@ -450,14 +462,20 @@ namespace Benchmark } } + // Test under and over-subscription of threads vs the amount of CPUs available + static const unsigned int MaxThreadRange = 2 * AZStd::thread::hardware_concurrency(); + #define BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME, ...) \ BENCHMARK_TEMPLATE_DEFINE_F(FIXTURE, TESTNAME, __VA_ARGS__)(benchmark::State& state) { Benchmark(state); } \ BENCHMARK_REGISTER_F(FIXTURE, TESTNAME) + // We test small/big/mixed allocations in single-threaded environments. For multi-threaded environments, we test mixed since + // the multi threaded fixture will run multiple passes (1, 2, 4, ... until 2*hardware_concurrency) #define BM_REGISTER_SIZE_FIXTURES(FIXTURE, TESTNAME, ALLOCATORTYPE) \ BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_SMALL, ALLOCATORTYPE, SMALL)->Apply(RunRanges); \ BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_BIG, ALLOCATORTYPE, BIG)->Apply(RunRanges); \ - BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED, ALLOCATORTYPE, MIXED)->Apply(RunRanges); + BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED, ALLOCATORTYPE, MIXED)->Apply(RunRanges); \ + BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED_THREADED, ALLOCATORTYPE, MIXED)->ThreadRange(1, MaxThreadRange)->Apply(RunRanges); #define BM_REGISTER_ALLOCATOR(TESTNAME, ALLOCATORTYPE) \ namespace TESTNAME \ From f062b563c89e88b1facf32c6660bdca6cfac9057 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 30 Nov 2021 17:30:55 -0800 Subject: [PATCH 07/20] Improving runtime and making the whole duration manageable Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Tests/Memory/AllocatorBenchmarks.cpp | 44 +++++++--- .../AzCore/Tests/Memory/HphaSchema.cpp | 88 ------------------- 2 files changed, 31 insertions(+), 101 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index 7cd7e7ce0a..c1fdd4ca32 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -364,21 +364,27 @@ namespace Benchmark AZStd::vector& perThreadAllocations = base::GetPerThreadAllocations(state.thread_index); const size_t numberOfAllocations = perThreadAllocations.size(); + size_t totalAllocationSize = 0; for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) { const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; + totalAllocationSize += allocationSize; state.ResumeTiming(); perThreadAllocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); state.PauseTiming(); } - state.PauseTiming(); - state.counters[s_counterAllocatorMemoryRatio] = - benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); + // In allocation cases, s_counterAllocatorMemoryRatio is measuring how much over-allocation our allocators + // are doing to keep track of the memory and because of fragmentation/under-use of blocks. A ratio over 1 means + // that we are using more memory than requested. Ideally we would approximate to a ratio of 1. + state.counters[s_counterAllocatorMemoryRatio] = benchmark::Counter( + static_cast(TestAllocatorType::NumAllocatedBytes()) / static_cast(totalAllocationSize), + benchmark::Counter::kDefaults); + // s_counterProcessMemoryRatio is measuring the same ratio but using the OS to measure the used process memory state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( - static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), + static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline) / static_cast(totalAllocationSize), benchmark::Counter::kDefaults); for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) @@ -391,7 +397,6 @@ namespace Benchmark TestAllocatorType::GarbageCollect(); state.SetItemsProcessed(numberOfAllocations); - state.ResumeTiming(); } } }; @@ -422,10 +427,12 @@ namespace Benchmark const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); const size_t numberOfAllocations = perThreadAllocations.size(); + size_t totalAllocationSize = 0; for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) { const AllocationSizeArray& allocationArray = s_allocationSizes[TAllocationSize]; const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; + totalAllocationSize += allocationSize; perThreadAllocations[allocationIndex] = TestAllocatorType::Allocate(allocationSize, 0); } @@ -439,29 +446,40 @@ namespace Benchmark perThreadAllocations[allocationIndex] = nullptr; } - state.counters[s_counterAllocatorMemoryRatio] = - benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); + // In deallocation cases, s_counterAllocatorMemoryRatio is measuring how much "left-over" memory our allocators + // have after deallocations happen. This is memory that is not returned to the operative system. A ratio of 1 means + // that no memory was returned to the OS. A ratio over 1 means that we are holding more memory than requested. A ratio + // lower than 1 means that we have returned some memory. + state.counters[s_counterAllocatorMemoryRatio] = benchmark::Counter( + static_cast(TestAllocatorType::NumAllocatedBytes()) / static_cast(totalAllocationSize), + benchmark::Counter::kDefaults); + // s_counterProcessMemoryRatio is measuring the same ratio but using the OS to measure the used process memory state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( - static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), + static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline) / static_cast(totalAllocationSize), benchmark::Counter::kDefaults); state.SetItemsProcessed(numberOfAllocations); TestAllocatorType::GarbageCollect(); - - state.ResumeTiming(); } } }; + // For non-threaded ranges, run 100, 400, 1600 amounts static void RunRanges(benchmark::internal::Benchmark* b) { - for (int i = 0; i < 6; ++i) + for (int i = 0; i < 6; i += 2) { - b->Arg((1 << i) * 1000); + b->Arg((1 << i) * 100); } } + // For threaded ranges, run just 200, multi-threaded will already multiply by thread + static void ThreadedRunRanges(benchmark::internal::Benchmark* b) + { + b->Arg(100); + } + // Test under and over-subscription of threads vs the amount of CPUs available static const unsigned int MaxThreadRange = 2 * AZStd::thread::hardware_concurrency(); @@ -475,7 +493,7 @@ namespace Benchmark BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_SMALL, ALLOCATORTYPE, SMALL)->Apply(RunRanges); \ BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_BIG, ALLOCATORTYPE, BIG)->Apply(RunRanges); \ BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED, ALLOCATORTYPE, MIXED)->Apply(RunRanges); \ - BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED_THREADED, ALLOCATORTYPE, MIXED)->ThreadRange(1, MaxThreadRange)->Apply(RunRanges); + BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED_THREADED, ALLOCATORTYPE, MIXED)->ThreadRange(2, MaxThreadRange)->Apply(ThreadedRunRanges); #define BM_REGISTER_ALLOCATOR(TESTNAME, ALLOCATORTYPE) \ namespace TESTNAME \ diff --git a/Code/Framework/AzCore/Tests/Memory/HphaSchema.cpp b/Code/Framework/AzCore/Tests/Memory/HphaSchema.cpp index 85dd79931d..08b84416e6 100644 --- a/Code/Framework/AzCore/Tests/Memory/HphaSchema.cpp +++ b/Code/Framework/AzCore/Tests/Memory/HphaSchema.cpp @@ -10,10 +10,6 @@ #include #include -#if defined(HAVE_BENCHMARK) -#include -#endif // HAVE_BENCHMARK - class HphaSchema_TestAllocator : public AZ::SimpleSchemaAllocator { @@ -112,87 +108,3 @@ namespace UnitTest HphaSchemaTestFixture, ::testing::ValuesIn(s_mixedInstancesParameters)); } - - -#if defined(HAVE_BENCHMARK) -namespace Benchmark -{ - class HphaSchemaBenchmarkFixture - : public ::benchmark::Fixture - { - void internalSetUp() - { - AZ::AllocatorInstance::Create(); - } - - void internalTearDown() - { - AZ::AllocatorInstance::Destroy(); - } - - public: - void SetUp(const benchmark::State&) override - { - internalSetUp(); - } - void SetUp(benchmark::State&) override - { - internalSetUp(); - } - void TearDown(const benchmark::State&) override - { - internalTearDown(); - } - void TearDown(benchmark::State&) override - { - internalTearDown(); - } - - static void BM_Allocations(benchmark::State& state, const AllocationSizeArray& allocationArray) - { - AZStd::vector allocations; - while (state.KeepRunning()) - { - state.PauseTiming(); - const size_t allocationIndex = allocations.size(); - const size_t allocationSize = allocationArray[allocationIndex % allocationArray.size()]; - - state.ResumeTiming(); - void* allocation = AZ::AllocatorInstance::Get().Allocate(allocationSize, 0); - - state.PauseTiming(); - allocations.emplace_back(allocation); - - state.ResumeTiming(); - } - - const size_t numberOfAllocations = allocations.size(); - state.SetItemsProcessed(numberOfAllocations); - - for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) - { - AZ::AllocatorInstance::Get().DeAllocate(allocations[allocationIndex], allocationArray[allocationIndex % allocationArray.size()]); - } - AZ::AllocatorInstance::Get().GarbageCollect(); - } - }; - - // Small allocations, these are allocations that are going to end up in buckets in the HphaSchema - BENCHMARK_F(HphaSchemaBenchmarkFixture, SmallAllocations)(benchmark::State& state) - { - BM_Allocations(state, s_smallAllocationSizes); - } - - BENCHMARK_F(HphaSchemaBenchmarkFixture, BigAllocations)(benchmark::State& state) - { - BM_Allocations(state, s_bigAllocationSizes); - } - - BENCHMARK_F(HphaSchemaBenchmarkFixture, MixedAllocations)(benchmark::State& state) - { - BM_Allocations(state, s_mixedAllocationSizes); - } - - -} // Benchmark -#endif // HAVE_BENCHMARK From 0f5cb54a38d32abacc29f1bc23d58d6032384bb6 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 30 Nov 2021 18:11:08 -0800 Subject: [PATCH 08/20] Fixes Linux build Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/Tests/Memory/AllocatorBenchmarks.cpp | 10 +++------- .../Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp | 4 ++-- .../Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp | 5 +++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index c1fdd4ca32..dfaefd4c0f 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -248,10 +248,6 @@ namespace Benchmark { public: AZ_TYPE_INFO(TestSystemAllocator, "{360D4DAA-D65D-4D5C-A6FA-1A4C5261C35C}"); - - TestSystemAllocator() - : AZ::SystemAllocator() - {} }; // Allocated bytes reported by the allocator / actually requested bytes @@ -293,7 +289,7 @@ namespace Benchmark m_allocations.resize(state.threads); for (auto& perThreadAllocations : m_allocations) { - perThreadAllocations.resize(state.range_x(), nullptr); + perThreadAllocations.resize(state.range(0), nullptr); } } } @@ -342,7 +338,7 @@ namespace Benchmark : public AllocatorBenchmarkFixture { using base = AllocatorBenchmarkFixture; - using TestAllocatorType = base::TestAllocatorType; + using TestAllocatorType = typename base::TestAllocatorType; void internalSetUp(const ::benchmark::State& state) override { @@ -406,7 +402,7 @@ namespace Benchmark : public AllocatorBenchmarkFixture { using base = AllocatorBenchmarkFixture; - using TestAllocatorType = base::TestAllocatorType; + using TestAllocatorType = typename base::TestAllocatorType; void internalSetUp(const ::benchmark::State& state) override { diff --git a/Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp b/Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp index 49ecefda49..636d5519d8 100644 --- a/Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp +++ b/Code/Framework/AzCore/Tests/Platform/Linux/Tests/Memory/AllocatorBenchmarks_Linux.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include namespace Benchmark { @@ -25,7 +25,7 @@ namespace Benchmark size_t GetMemorySize(void* memory) { - return memory ? _aligned_msize(memory, 1, 0) : 0; + return memory ? malloc_usable_size(memory) : 0; } } } diff --git a/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp b/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp index 374d9f81f7..303b7efbb4 100644 --- a/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp +++ b/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp @@ -9,7 +9,8 @@ #include #include -#include +#include +#include namespace Benchmark { @@ -24,7 +25,7 @@ namespace Benchmark size_t GetMemorySize(void* memory) { - return memory ? _aligned_msize(memory, 1, 0) : 0; + return memory ? malloc_usable_size(memory) : 0; } } } From fbd2d60fc1cfc91a49331b304abb797524ae0270 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 30 Nov 2021 18:15:22 -0800 Subject: [PATCH 09/20] Fixes for mac Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp b/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp index 303b7efbb4..932252985a 100644 --- a/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp +++ b/Code/Framework/AzCore/Tests/Platform/Mac/Tests/Memory/AllocatorBenchmarks_Mac.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include namespace Benchmark @@ -25,7 +25,7 @@ namespace Benchmark size_t GetMemorySize(void* memory) { - return memory ? malloc_usable_size(memory) : 0; + return memory ? malloc_size(memory) : 0; } } } From 72f338a6897ff952ae0bfc564fc19fae072fbb1c Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Thu, 2 Dec 2021 11:55:36 -0800 Subject: [PATCH 10/20] Fixes for HeapSchema to get a default block if none is passed Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp | 12 +----------- Code/Framework/AzCore/AzCore/Memory/HeapSchema.h | 14 ++++---------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp index aceafa1b28..1f0fc59a97 100644 --- a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp @@ -115,6 +115,7 @@ namespace AZ m_ownMemoryBlock[i] = false; } + AZ_Assert(m_desc.m_numMemoryBlocks > 0, "At least one memory block is required"); for (int i = 0; i < m_desc.m_numMemoryBlocks; ++i) { if (m_desc.m_memoryBlocks[i] == nullptr) // Allocate memory block if requested! @@ -131,17 +132,6 @@ namespace AZ m_capacity += m_desc.m_memoryBlocksByteSize[i]; } - - if (m_desc.m_numMemoryBlocks == 0) - { - // Create default memory space if we can to serve for default allocations - m_memSpaces[0] = AZDLMalloc::create_mspace(0, m_desc.m_isMultithreadAlloc); - if (m_memSpaces[0]) - { - AZDLMalloc::mspace_az_set_expandable(m_memSpaces[0], true); - m_capacity = Platform::GetHeapCapacity(); - } - } } HeapSchema::~HeapSchema() diff --git a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h index f72ae31057..3a7716a127 100644 --- a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h +++ b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h @@ -32,17 +32,11 @@ namespace AZ */ struct Descriptor { - Descriptor() - : m_numMemoryBlocks(0) - , m_isMultithreadAlloc(true) - {} - - static const int m_memoryBlockAlignment = 64 * 1024; static const int m_maxNumBlocks = 5; - int m_numMemoryBlocks; ///< Number of memory blocks to use. - void* m_memoryBlocks[m_maxNumBlocks]; ///< Pointers to provided memory blocks or NULL if you want the system to allocate them for you with the System Allocator. - size_t m_memoryBlocksByteSize[m_maxNumBlocks]; ///< Sizes of different memory blocks, if m_memoryBlock is 0 the block will be allocated for you with the System Allocator. - bool m_isMultithreadAlloc; ///< Set to true to enable multi threading safe allocation. + int m_numMemoryBlocks = 1; ///< Number of memory blocks to use. + void* m_memoryBlocks[m_maxNumBlocks] = {}; ///< Pointers to provided memory blocks or NULL if you want the system to allocate them for you with the System Allocator. + size_t m_memoryBlocksByteSize[m_maxNumBlocks] = {4 * 1024}; ///< Sizes of different memory blocks, if m_memoryBlock is 0 the block will be allocated for you with the System Allocator. + bool m_isMultithreadAlloc = true; ///< Set to true to enable multi threading safe allocation. }; HeapSchema(const Descriptor& desc); From cf9aab991104d0b59697b4826d25024add70f752 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Mon, 6 Dec 2021 18:34:52 -0800 Subject: [PATCH 11/20] Adds recording functionality (disabled) and a benchmark that can run recordings Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/AzCore/Memory/AllocatorBase.cpp | 81 +++++ .../Tests/Memory/AllocatorBenchmarks.cpp | 344 ++++++++++++------ 2 files changed, 309 insertions(+), 116 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp index e450f12bcf..40ddff0fc3 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp @@ -12,6 +12,49 @@ using namespace AZ; +#define RECORDING_ENABLED 0 + +#if RECORDING_ENABLED + +struct AllocatorOperation +{ + enum OperationType : unsigned int + { + ALLOCATE, + DEALLOCATE, + REALLOCATE, + RESIZE + }; + OperationType m_operationType : 2; + size_t m_size : 46; + size_t m_alignment : 16; + void* m_ptr; + void* m_newptr; // required for resize +}; +static constexpr size_t s_allocationOperationCount = 5 * 1024; +static AZStd::array s_operations = {}; +static uint64_t s_operationCounter = 0; +static AZStd::mutex s_operationsMutex; + +AllocatorOperation& GetNextAllocatorOperation() +{ + AZStd::scoped_lock lock(s_operationsMutex); + if (s_operationCounter == s_allocationOperationCount) + { + FILE* file = nullptr; + fopen_s(&file, "memoryrecordings.bin", "ab"); + if (file) + { + fwrite(&s_operations, sizeof(AllocatorOperation), s_allocationOperationCount, file); + fclose(file); + } + s_operationCounter = 0; + } + return s_operations[s_operationCounter++]; +} + +#endif + AllocatorBase::AllocatorBase(IAllocatorAllocate* allocationSource, const char* name, const char* desc) : IAllocator(allocationSource), m_name(name), @@ -136,6 +179,16 @@ void AllocatorBase::ProfileAllocation(void* ptr, size_t byteSize, size_t alignme EBUS_EVENT(AZ::Debug::MemoryDrillerBus, RegisterAllocation, this, ptr, byteSize, alignment, name, fileName, lineNum, suppressStackRecord); #endif } + +#if RECORDING_ENABLED + { + AllocatorOperation& op = GetNextAllocatorOperation(); + op.m_operationType = AllocatorOperation::ALLOCATE; + op.m_size = byteSize; + op.m_alignment = alignment; + op.m_ptr = ptr; + } +#endif } void AllocatorBase::ProfileDeallocation(void* ptr, size_t byteSize, size_t alignment, Debug::AllocationInfo* info) @@ -148,6 +201,15 @@ void AllocatorBase::ProfileDeallocation(void* ptr, size_t byteSize, size_t align EBUS_EVENT(AZ::Debug::MemoryDrillerBus, UnregisterAllocation, this, ptr, byteSize, alignment, info); #endif } +#if RECORDING_ENABLED + { + AllocatorOperation& op = GetNextAllocatorOperation(); + op.m_operationType = AllocatorOperation::DEALLOCATE; + op.m_size = byteSize; + op.m_alignment = alignment; + op.m_ptr = ptr; + } +#endif } void AllocatorBase::ProfileReallocationBegin(void* ptr, size_t newSize) @@ -174,6 +236,16 @@ void AllocatorBase::ProfileReallocationEnd(void* ptr, void* newPtr, size_t newSi EBUS_EVENT(AZ::Debug::MemoryDrillerBus, ReallocateAllocation, this, ptr, newPtr, newSize, newAlignment); #endif } +#if RECORDING_ENABLED + { + AllocatorOperation& op = GetNextAllocatorOperation(); + op.m_operationType = AllocatorOperation::REALLOCATE; + op.m_size = newSize; + op.m_alignment = newAlignment; + op.m_ptr = ptr; + op.m_newptr = newPtr; + } +#endif } void AllocatorBase::ProfileReallocation(void* ptr, void* newPtr, size_t newSize, size_t newAlignment) @@ -187,6 +259,15 @@ void AllocatorBase::ProfileResize(void* ptr, size_t newSize) { EBUS_EVENT(AZ::Debug::MemoryDrillerBus, ResizeAllocation, this, ptr, newSize); } +#if RECORDING_ENABLED + { + AllocatorOperation& op = GetNextAllocatorOperation(); + op.m_operationType = AllocatorOperation::RESIZE; + op.m_size = newSize; + op.m_alignment = 0; + op.m_ptr = ptr; + } +#endif } bool AllocatorBase::OnOutOfMemory(size_t byteSize, size_t alignment, int flags, const char* name, const char* fileName, int lineNum) diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index dfaefd4c0f..76c3316009 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -32,7 +32,7 @@ namespace Benchmark size_t GetMemorySize(void* memory); } - static AZ::Debug::DrillerManager* s_drillerManager = nullptr; + //static AZ::Debug::DrillerManager* s_drillerManager = nullptr; /// /// Test allocator wrapper that redirects the calls to the passed TAllocator by using AZ::AllocatorInstance. @@ -47,14 +47,14 @@ namespace Benchmark { AZ::AllocatorInstance::Create(); - s_drillerManager = AZ::Debug::DrillerManager::Create(); - s_drillerManager->Register(aznew AZ::Debug::MemoryDriller); + /*s_drillerManager = AZ::Debug::DrillerManager::Create(); + s_drillerManager->Register(aznew AZ::Debug::MemoryDriller);*/ } static void TearDown() { - AZ::Debug::DrillerManager::Destroy(s_drillerManager); - s_drillerManager = nullptr; + /*AZ::Debug::DrillerManager::Destroy(s_drillerManager); + s_drillerManager = nullptr;*/ AZ::AllocatorInstance::Destroy(); } @@ -89,51 +89,41 @@ namespace Benchmark return AZ::AllocatorInstance::Get().NumAllocatedBytes() + AZ::AllocatorInstance::Get().GetUnAllocatedMemory(); } + + static size_t GetSize(void* ptr) + { + return AZ::AllocatorInstance::Get().AllocationSize(ptr); + } }; /// /// Basic allocator used as a baseline. This allocator is the most basic allocation possible with the OS (AZ_OS_MALLOC). /// MallocSchema cannot be used here because it has extra logic that we don't want to use as a baseline. /// - class TestRawMallocAllocator - : public AZ::AllocatorBase - , public AZ::IAllocatorAllocate + class TestRawMallocAllocator {}; + + template<> + class TestAllocatorWrapper { public: - AZ_TYPE_INFO(TestMallocSchemaAllocator, "{08EB400A-D723-46C6-808E-D0844C8DE206}"); - - struct Descriptor {}; - - TestRawMallocAllocator() - : AllocatorBase(this, "TestRawMallocAllocator", "") + TestAllocatorWrapper() { - m_numAllocatedBytes = 0; + s_numAllocatedBytes = 0; } - bool Create(const Descriptor&) + static void SetUp() { - m_numAllocatedBytes = 0; - return true; + s_numAllocatedBytes = 0; } - // IAllocator - void Destroy() override + static void TearDown() { - m_numAllocatedBytes = 0; - } - AZ::AllocatorDebugConfig GetDebugConfig() override - { - return AZ::AllocatorDebugConfig(); - } - AZ::IAllocatorAllocate* GetSchema() override - { - return nullptr; } // IAllocatorAllocate - void* Allocate(size_t byteSize, size_t alignment, int = 0, const char* = 0, const char* = 0, int = 0, unsigned int = 0) override + static void* Allocate(size_t byteSize, size_t alignment) { - m_numAllocatedBytes += byteSize; + s_numAllocatedBytes += byteSize; if (alignment) { return AZ_OS_MALLOC(byteSize, alignment); @@ -144,18 +134,18 @@ namespace Benchmark } } - void DeAllocate(void* ptr, size_t = 0, size_type = 0) override + static void DeAllocate(void* ptr, size_t = 0) { - m_numAllocatedBytes -= Platform::GetMemorySize(ptr); + s_numAllocatedBytes -= Platform::GetMemorySize(ptr); AZ_OS_FREE(ptr); } - void* ReAllocate(void* ptr, size_t newSize, size_t newAlignment) override + static void* ReAllocate(void* ptr, size_t newSize, size_t newAlignment) { - m_numAllocatedBytes -= Platform::GetMemorySize(ptr); + s_numAllocatedBytes -= Platform::GetMemorySize(ptr); AZ_OS_FREE(ptr); - m_numAllocatedBytes += newSize; + s_numAllocatedBytes += newSize; if (newAlignment) { return AZ_OS_MALLOC(newSize, newAlignment); @@ -166,7 +156,7 @@ namespace Benchmark } } - size_t Resize(void* ptr, size_t newSize) override + static size_t Resize(void* ptr, size_t newSize) { AZ_UNUSED(ptr); AZ_UNUSED(newSize); @@ -174,45 +164,24 @@ namespace Benchmark return 0; } - size_t AllocationSize(void* ptr) override + static void GarbageCollect() {} + + static size_t NumAllocatedBytes() + { + return s_numAllocatedBytes; + } + + static size_t GetSize(void* ptr) { return Platform::GetMemorySize(ptr); } - void GarbageCollect() override {} - - size_t NumAllocatedBytes() const override - { - return m_numAllocatedBytes; - } - - size_t Capacity() const override - { - return AZ_CORE_MAX_ALLOCATOR_SIZE; // unused - } - - size_t GetMaxAllocationSize() const override - { - return AZ_CORE_MAX_ALLOCATOR_SIZE; // unused - } - - size_t GetMaxContiguousAllocationSize() const override - { - return AZ_CORE_MAX_ALLOCATOR_SIZE; // unused - } - size_t GetUnAllocatedMemory(bool = false) const override - { - return 0; // unused - } - IAllocatorAllocate* GetSubAllocator() override - { - return nullptr; // unused - } - private: - size_t m_numAllocatedBytes; + static size_t s_numAllocatedBytes; }; + size_t TestAllocatorWrapper::s_numAllocatedBytes = 0; + // Here we require to implement this to be able to configure a name for the allocator, otherswise the AllocatorManager crashes when trying to configure the overrides class TestMallocSchemaAllocator : public AZ::SimpleSchemaAllocator { @@ -250,11 +219,14 @@ namespace Benchmark AZ_TYPE_INFO(TestSystemAllocator, "{360D4DAA-D65D-4D5C-A6FA-1A4C5261C35C}"); }; - // Allocated bytes reported by the allocator / actually requested bytes - static const char* s_counterAllocatorMemoryRatio = "Allocator_MemoryRatio"; + // Allocated bytes reported by the allocator + static const char* s_counterAllocatorMemory = "Allocator_Memory"; - // Allocated bytes reported by the process / actually requested bytes - static const char* s_counterProcessMemoryRatio = "Process_MemoryRatio"; + // Allocated bytes reported by the process + static const char* s_counterProcessMemory = "Process_Memory"; + + // Allocated bytes as counted by the benchmark + static const char* s_counterBenchmarkMemory = "Benchmark_Memory"; enum AllocationSize { @@ -340,16 +312,6 @@ namespace Benchmark using base = AllocatorBenchmarkFixture; using TestAllocatorType = typename base::TestAllocatorType; - void internalSetUp(const ::benchmark::State& state) override - { - AllocatorBenchmarkFixture::internalSetUp(state); - } - - void internalTearDown(const ::benchmark::State& state) override - { - AllocatorBenchmarkFixture::internalTearDown(state); - } - public: void Benchmark(benchmark::State& state) { @@ -372,16 +334,9 @@ namespace Benchmark state.PauseTiming(); } - // In allocation cases, s_counterAllocatorMemoryRatio is measuring how much over-allocation our allocators - // are doing to keep track of the memory and because of fragmentation/under-use of blocks. A ratio over 1 means - // that we are using more memory than requested. Ideally we would approximate to a ratio of 1. - state.counters[s_counterAllocatorMemoryRatio] = benchmark::Counter( - static_cast(TestAllocatorType::NumAllocatedBytes()) / static_cast(totalAllocationSize), - benchmark::Counter::kDefaults); - // s_counterProcessMemoryRatio is measuring the same ratio but using the OS to measure the used process memory - state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( - static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline) / static_cast(totalAllocationSize), - benchmark::Counter::kDefaults); + state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); + state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); + state.counters[s_counterBenchmarkMemory] = benchmark::Counter(static_cast(totalAllocationSize), benchmark::Counter::kDefaults); for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) { @@ -404,15 +359,6 @@ namespace Benchmark using base = AllocatorBenchmarkFixture; using TestAllocatorType = typename base::TestAllocatorType; - void internalSetUp(const ::benchmark::State& state) override - { - AllocatorBenchmarkFixture::internalSetUp(state); - } - - void internalTearDown(const ::benchmark::State& state) override - { - AllocatorBenchmarkFixture::internalTearDown(state); - } public: void Benchmark(benchmark::State& state) { @@ -442,17 +388,9 @@ namespace Benchmark perThreadAllocations[allocationIndex] = nullptr; } - // In deallocation cases, s_counterAllocatorMemoryRatio is measuring how much "left-over" memory our allocators - // have after deallocations happen. This is memory that is not returned to the operative system. A ratio of 1 means - // that no memory was returned to the OS. A ratio over 1 means that we are holding more memory than requested. A ratio - // lower than 1 means that we have returned some memory. - state.counters[s_counterAllocatorMemoryRatio] = benchmark::Counter( - static_cast(TestAllocatorType::NumAllocatedBytes()) / static_cast(totalAllocationSize), - benchmark::Counter::kDefaults); - // s_counterProcessMemoryRatio is measuring the same ratio but using the OS to measure the used process memory - state.counters[s_counterProcessMemoryRatio] = benchmark::Counter( - static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline) / static_cast(totalAllocationSize), - benchmark::Counter::kDefaults); + state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); + state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); + state.counters[s_counterBenchmarkMemory] = benchmark::Counter(static_cast(totalAllocationSize), benchmark::Counter::kDefaults); state.SetItemsProcessed(numberOfAllocations); @@ -460,6 +398,175 @@ namespace Benchmark } } }; + + template + class RecordedAllocationBenchmarkFixture : public AllocatorBenchmarkFixture + { + using base = AllocatorBenchmarkFixture; + using TestAllocatorType = typename base::TestAllocatorType; + + struct AllocatorOperation + { + enum OperationType : unsigned int + { + ALLOCATE, + DEALLOCATE, + REALLOCATE, + RESIZE + }; + OperationType m_operationType : 2; + size_t m_size : 46; + size_t m_alignment : 16; + void* m_ptr; + void* m_newptr; // required for resize + }; + + public: + void Benchmark(benchmark::State& state) + { + for (auto _ : state) + { + state.PauseTiming(); + + AZStd::unordered_map pointerRemapping; + AZStd::unordered_map allocationSize; + constexpr size_t allocationOperationCount = 5 * 1024; + AZStd::array m_operations = {}; + + FILE* file = nullptr; + fopen_s(&file, "memoryrecordings.bin", "rb"); + if (!file) + { + return; + } + size_t elementsRead = fread(&m_operations, sizeof(AllocatorOperation), allocationOperationCount, file); + size_t totalElementsRead = elementsRead; + const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); + size_t totalAllocationSize = 0; + + while (elementsRead > 0) + { + for (size_t operationIndex = 0; operationIndex < elementsRead; ++operationIndex) + { + const AllocatorOperation& operation = m_operations[operationIndex]; + switch (operation.m_operationType) + { + case AllocatorOperation::ALLOCATE: + { + if (operation.m_ptr) + { + const auto it = pointerRemapping.emplace(operation.m_ptr, nullptr); + if (it.second) // otherwise already allocated + { + state.ResumeTiming(); + void* ptr = TestAllocatorType::Allocate(operation.m_size, operation.m_alignment); + state.PauseTiming(); + totalAllocationSize += operation.m_size; + it.first->second = ptr; + allocationSize[ptr] = operation.m_size; + } + else + { + //AZ_Warning("RecordedAllocationBenchmarkFixture", false, "Allocation on %p was already made", operation.m_ptr); + } + } + break; + } + case AllocatorOperation::DEALLOCATE: + { + if (operation.m_ptr) // some deallocate(nullptr) are recorded + { + const auto ptrIt = pointerRemapping.find(operation.m_ptr); + if (ptrIt != pointerRemapping.end()) + { + totalAllocationSize -= allocationSize[ptrIt->second]; + state.ResumeTiming(); + TestAllocatorType::DeAllocate(ptrIt->second, /*operation.m_size*/ 0); // size is not correct after a resize, a 0 size deals with it + state.PauseTiming(); + pointerRemapping.erase(ptrIt); + } + } + else + { + // Just to account of the call of deallocate(nullptr); + // totalAllocationSize -= 0; // No real deallocation happened + state.ResumeTiming(); + TestAllocatorType::DeAllocate(operation.m_ptr, /*operation.m_size*/ 0); + state.PauseTiming(); + } + break; + } + case AllocatorOperation::REALLOCATE: + { + void* ptr = nullptr; + if (operation.m_ptr) + { + AZ_Assert(operation.m_newptr, "Need to consider other cases?"); + const auto ptrIt = pointerRemapping.find(operation.m_ptr); + AZ_Assert(ptrIt != pointerRemapping.end(), "Missing allocation for reallocation"); // In case the recording didnt catch something + ptr = ptrIt->second; + pointerRemapping.erase(ptrIt); + } + AZ_Assert(operation.m_newptr != nullptr, "Reallocation failed in the game"); + const auto it = pointerRemapping.emplace(operation.m_newptr, nullptr); + if (it.second) + { + totalAllocationSize -= allocationSize[ptr]; + state.ResumeTiming(); + void* newPtr = TestAllocatorType::ReAllocate(ptr, operation.m_size, operation.m_alignment); + state.PauseTiming(); + totalAllocationSize += operation.m_size; + it.first->second = newPtr; + allocationSize[newPtr] = operation.m_size; + } + else + { + totalAllocationSize -= allocationSize[ptr]; + state.ResumeTiming(); + TestAllocatorType::DeAllocate(ptr); + state.PauseTiming(); + } + break; + } + case AllocatorOperation::RESIZE: + { + const auto ptrIt = pointerRemapping.find(operation.m_ptr); + AZ_Assert(ptrIt != pointerRemapping.end(), "Missing allocation for resize"); // In case the recording didnt catch something + totalAllocationSize -= allocationSize[ptrIt->second]; + state.ResumeTiming(); + TestAllocatorType::Resize(ptrIt->second, operation.m_size); + state.PauseTiming(); + totalAllocationSize += operation.m_size; + if (operation.m_size == 0) + { + pointerRemapping.erase(ptrIt); + } + break; + } + } + } + + elementsRead = fread(&m_operations, sizeof(AllocatorOperation), allocationOperationCount, file); + totalElementsRead += elementsRead; + } + fclose(file); + + state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); + state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); + state.counters[s_counterBenchmarkMemory] = benchmark::Counter(static_cast(totalAllocationSize), benchmark::Counter::kDefaults); + + state.SetItemsProcessed(totalElementsRead); + + // Deallocate the remainder (since we stopped the recording middle-game)(there are leaks as well) + for (const auto& pointerMapping : pointerRemapping) + { + TestAllocatorType::DeAllocate(pointerMapping.second); + } + pointerRemapping.clear(); + TestAllocatorType::GarbageCollect(); + } + } + }; // For non-threaded ranges, run 100, 400, 1600 amounts static void RunRanges(benchmark::internal::Benchmark* b) @@ -469,6 +576,10 @@ namespace Benchmark b->Arg((1 << i) * 100); } } + static void RecordedRunRanges(benchmark::internal::Benchmark* b) + { + b->Arg(1); + } // For threaded ranges, run just 200, multi-threaded will already multiply by thread static void ThreadedRunRanges(benchmark::internal::Benchmark* b) @@ -494,16 +605,17 @@ namespace Benchmark #define BM_REGISTER_ALLOCATOR(TESTNAME, ALLOCATORTYPE) \ namespace TESTNAME \ { \ - BM_REGISTER_SIZE_FIXTURES(AllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE) \ - BM_REGISTER_SIZE_FIXTURES(DeAllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE) \ + BM_REGISTER_SIZE_FIXTURES(AllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE); \ + BM_REGISTER_SIZE_FIXTURES(DeAllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE); \ + BM_REGISTER_TEMPLATE(RecordedAllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE)->Apply(RecordedRunRanges); \ } BM_REGISTER_ALLOCATOR(RawMallocAllocator, TestRawMallocAllocator); BM_REGISTER_ALLOCATOR(MallocSchemaAllocator, TestMallocSchemaAllocator); - BM_REGISTER_ALLOCATOR(HeapSchemaAllocator, TestHeapSchemaAllocator); BM_REGISTER_ALLOCATOR(HphaSchemaAllocator, TestHphaSchemaAllocator); BM_REGISTER_ALLOCATOR(SystemAllocator, TestSystemAllocator); + //BM_REGISTER_ALLOCATOR(HeapSchemaAllocator, TestHeapSchemaAllocator); // Requires to pre-allocate blocks and cannot work as a general-purpose allocator //BM_REGISTER_SCHEMA(BestFitExternalMapSchema); // Requires to implement AZ::IAllocatorAllocate //BM_REGISTER_SCHEMA(PoolSchema); // Requires special alignment requests while allocating From 4b07665496f060459020f3f6c250807f26d0a80f Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Mon, 6 Dec 2021 18:35:39 -0800 Subject: [PATCH 12/20] Removes Heap allocator from being possible to use as a SystemAllocator since it doesnt allow dynamic allocating (only works with pre-allocated blocks) Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/AzCore/Memory/SystemAllocator.cpp | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp index 8c84338fd0..9ce681ef2d 100644 --- a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp @@ -19,7 +19,6 @@ #define AZCORE_SYSTEM_ALLOCATOR_HPHA 1 #define AZCORE_SYSTEM_ALLOCATOR_MALLOC 2 -#define AZCORE_SYSTEM_ALLOCATOR_HEAP 3 #if !defined(AZCORE_SYSTEM_ALLOCATOR) // define the default @@ -30,8 +29,6 @@ #include #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC #include -#elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HEAP - #include #else #error "Invalid allocator selected for SystemAllocator" #endif @@ -46,8 +43,6 @@ static bool g_isSystemSchemaUsed = false; static AZStd::aligned_storage::value>::type g_systemSchema; #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC static AZStd::aligned_storage::value>::type g_systemSchema; -#elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HEAP - static AZStd::aligned_storage::value>::type g_systemSchema; #endif ////////////////////////////////////////////////////////////////////////// @@ -121,11 +116,6 @@ SystemAllocator::Create(const Descriptor& desc) heapDesc.m_systemChunkSize = desc.m_heap.m_systemChunkSize; #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC MallocSchema::Descriptor heapDesc; -#elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HEAP - HeapSchema::Descriptor heapDesc; - memcpy(heapDesc.m_memoryBlocks, desc.m_heap.m_memoryBlocks, sizeof(heapDesc.m_memoryBlocks)); - memcpy(heapDesc.m_memoryBlocksByteSize, desc.m_heap.m_memoryBlocksByteSize, sizeof(heapDesc.m_memoryBlocksByteSize)); - heapDesc.m_numMemoryBlocks = desc.m_heap.m_numMemoryBlocks; #endif if (&AllocatorInstance::Get() == this) // if we are the system allocator { @@ -135,8 +125,6 @@ SystemAllocator::Create(const Descriptor& desc) m_allocator = new(&g_systemSchema)HphaSchema(heapDesc); #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC m_allocator = new(&g_systemSchema)MallocSchema(heapDesc); -#elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HEAP - m_allocator = new(&g_systemSchema)HeapSchema(heapDesc); #endif g_isSystemSchemaUsed = true; isReady = true; @@ -150,8 +138,6 @@ SystemAllocator::Create(const Descriptor& desc) m_allocator = azcreate(HphaSchema, (heapDesc), SystemAllocator); #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC m_allocator = azcreate(MallocSchema, (heapDesc), SystemAllocator); -#elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HEAP - m_allocator = azcreate(HeapSchema, (heapDesc), SystemAllocator); #endif if (m_allocator == nullptr) { @@ -188,8 +174,6 @@ SystemAllocator::Destroy() static_cast(m_allocator)->~HphaSchema(); #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC static_cast(m_allocator)->~MallocSchema(); -#elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HEAP - static_cast(m_allocator)->~HeapSchema(); #endif g_isSystemSchemaUsed = false; } From f96a466212c1acc526e2a1e41e6849fe854e3a1d Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 7 Dec 2021 17:58:49 -0800 Subject: [PATCH 13/20] WIP trying to use SystemAllocator instead of raw reads Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/AzCore/Memory/AllocatorBase.cpp | 193 ++++++++++++------ Code/Framework/AzCore/CMakeLists.txt | 5 + .../Memory/AllocatorBenchmarkRecordings.bin | 3 + .../Tests/Memory/AllocatorBenchmarks.cpp | 145 ++++--------- 4 files changed, 181 insertions(+), 165 deletions(-) create mode 100644 Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp index 7bba1b1d20..32ccd43f9a 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp @@ -11,47 +11,142 @@ using namespace AZ; -#define RECORDING_ENABLED 0 +#define RECORDING_ENABLED 1 #if RECORDING_ENABLED -struct AllocatorOperation -{ - enum OperationType : unsigned int - { - ALLOCATE, - DEALLOCATE, - REALLOCATE, - RESIZE - }; - OperationType m_operationType : 2; - size_t m_size : 46; - size_t m_alignment : 16; - void* m_ptr; - void* m_newptr; // required for resize -}; -static constexpr size_t s_allocationOperationCount = 5 * 1024; -static AZStd::array s_operations = {}; -static uint64_t s_operationCounter = 0; -static AZStd::mutex s_operationsMutex; +#include +#include +#include +#include -AllocatorOperation& GetNextAllocatorOperation() +namespace { - AZStd::scoped_lock lock(s_operationsMutex); - if (s_operationCounter == s_allocationOperationCount) + class DebugAllocator { - FILE* file = nullptr; - fopen_s(&file, "memoryrecordings.bin", "ab"); - if (file) + public: + typedef void* pointer_type; + typedef AZStd::size_t size_type; + typedef AZStd::ptrdiff_t difference_type; + typedef AZStd::false_type allow_memory_leaks; ///< Regular allocators should not leak. + + AZ_FORCE_INLINE pointer_type allocate(size_t byteSize, size_t alignment, int = 0) { - fwrite(&s_operations, sizeof(AllocatorOperation), s_allocationOperationCount, file); - fclose(file); + return AZ_OS_MALLOC(byteSize, alignment); } - s_operationCounter = 0; - } - return s_operations[s_operationCounter++]; -} + AZ_FORCE_INLINE size_type resize(pointer_type, size_type) + { + return 0; + } + AZ_FORCE_INLINE void deallocate(pointer_type ptr, size_type, size_type) + { + AZ_OS_FREE(ptr); + } + }; + struct AllocatorOperation + { + enum OperationType : unsigned int + { + ALLOCATE, + DEALLOCATE + }; + OperationType m_type: 1; + unsigned int m_size : 28; // Can represent up to 256Mb requests + unsigned int m_alignment : 7; // Can represent up to 128 alignment + unsigned int m_recordId : 28; // Can represent up to 256M simultaneous requests, we reuse ids + }; + static AZStd::mutex s_operationsMutex = {}; + + static constexpr size_t s_maxNumberOfAllocationsToRecord = 16384; + static size_t s_numberOfAllocationsRecorded = 0; + static constexpr size_t s_allocationOperationCount = 5 * 1024; + static AZStd::array s_operations = {}; + static uint64_t s_operationCounter = 0; + + static unsigned int s_nextRecordId = 1; + using AllocatorOperationByAddress = AZStd::map, DebugAllocator>; + static AllocatorOperationByAddress s_allocatorOperationByAddress; + using AvailableRecordIds = AZStd::vector; + AvailableRecordIds s_availableRecordIds; + + void RecordAllocatorOperation(AllocatorOperation::OperationType type, void* ptr, size_t size = 0, size_t alignment = 0) + { + AZStd::scoped_lock lock(s_operationsMutex); + if (s_operationCounter == s_allocationOperationCount) + { + AZ::IO::SystemFile file; + file.Open("memoryrecordings.bin", AZ::IO::SystemFile::OpenMode::SF_OPEN_APPEND); + if (file.IsOpen()) + { + file.Write(&s_operations, sizeof(AllocatorOperation) * s_allocationOperationCount); + file.Close(); + } + s_operationCounter = 0; + } + AllocatorOperation& operation = s_operations[s_operationCounter++]; + operation.m_type = type; + if (type == AllocatorOperation::OperationType::ALLOCATE) + { + if (s_numberOfAllocationsRecorded > s_maxNumberOfAllocationsToRecord) + { + // reached limit of allocations, dont record anymore + --s_operationCounter; + return; + } + ++s_numberOfAllocationsRecorded; + operation.m_size = size; + operation.m_alignment = alignment; + unsigned int recordId = 0; + if (!s_availableRecordIds.empty()) + { + recordId = s_availableRecordIds.back(); + s_availableRecordIds.pop_back(); + } + else + { + recordId = s_nextRecordId; + ++s_nextRecordId; + } + operation.m_recordId = recordId; + auto it = s_allocatorOperationByAddress.emplace(ptr, operation); + if (!it.second) + { + // double alloc or resize, leave the current record and return the id + operation = it.first->second; + s_availableRecordIds.emplace_back(recordId); + } + } + else + { + if (ptr == nullptr) + { + // common scenario, just record the operation + operation.m_size = 0; + operation.m_alignment = 0; + operation.m_recordId = 0; // recordId = 0 will flag this case + } + else + { + auto it = s_allocatorOperationByAddress.find(ptr); + if (it != s_allocatorOperationByAddress.end()) + { + operation.m_size = it->second.m_size; + operation.m_alignment = it->second.m_alignment; + operation.m_recordId = it->second.m_recordId; + s_availableRecordIds.push_back(it->second.m_recordId); + s_allocatorOperationByAddress.erase(it); + } + else + { + // just dont record this operation + --s_operationCounter; + } + } + } + + } +} #endif AllocatorBase::AllocatorBase(IAllocatorAllocate* allocationSource, const char* name, const char* desc) : @@ -188,13 +283,7 @@ void AllocatorBase::ProfileAllocation(void* ptr, size_t byteSize, size_t alignme } #if RECORDING_ENABLED - { - AllocatorOperation& op = GetNextAllocatorOperation(); - op.m_operationType = AllocatorOperation::ALLOCATE; - op.m_size = byteSize; - op.m_alignment = alignment; - op.m_ptr = ptr; - } + RecordAllocatorOperation(AllocatorOperation::ALLOCATE, ptr, byteSize, alignment); #endif } @@ -209,13 +298,7 @@ void AllocatorBase::ProfileDeallocation(void* ptr, size_t byteSize, size_t align } } #if RECORDING_ENABLED - { - AllocatorOperation& op = GetNextAllocatorOperation(); - op.m_operationType = AllocatorOperation::DEALLOCATE; - op.m_size = byteSize; - op.m_alignment = alignment; - op.m_ptr = ptr; - } + RecordAllocatorOperation(AllocatorOperation::DEALLOCATE, ptr, byteSize, alignment); #endif } @@ -232,14 +315,8 @@ void AllocatorBase::ProfileReallocationEnd(void* ptr, void* newPtr, size_t newSi ProfileAllocation(newPtr, newSize, newAlignment, info.m_name, info.m_fileName, info.m_lineNum, 0); } #if RECORDING_ENABLED - { - AllocatorOperation& op = GetNextAllocatorOperation(); - op.m_operationType = AllocatorOperation::REALLOCATE; - op.m_size = newSize; - op.m_alignment = newAlignment; - op.m_ptr = ptr; - op.m_newptr = newPtr; - } + RecordAllocatorOperation(AllocatorOperation::DEALLOCATE, ptr); + RecordAllocatorOperation(AllocatorOperation::ALLOCATE, newPtr, newSize, newAlignment); #endif } @@ -259,13 +336,7 @@ void AllocatorBase::ProfileResize(void* ptr, size_t newSize) } } #if RECORDING_ENABLED - { - AllocatorOperation& op = GetNextAllocatorOperation(); - op.m_operationType = AllocatorOperation::RESIZE; - op.m_size = newSize; - op.m_alignment = 0; - op.m_ptr = ptr; - } + RecordAllocatorOperation(AllocatorOperation::ALLOCATE, ptr, newSize); #endif } diff --git a/Code/Framework/AzCore/CMakeLists.txt b/Code/Framework/AzCore/CMakeLists.txt index 96ed838ccc..838142f0df 100644 --- a/Code/Framework/AzCore/CMakeLists.txt +++ b/Code/Framework/AzCore/CMakeLists.txt @@ -146,6 +146,11 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) PROPERTY COMPILE_DEFINITIONS VALUES AZCORETEST_DLL_NAME=\"$\" ) + ly_add_target_files( + TARGETS AzCore.Tests + FILES ${CMAKE_CURRENT_SOURCE_DIR}/Tests/Memory/AllocatorBenchmarkRecordings.bin + OUTPUT_SUBDIRECTORY Tests/AzCore/Memory + ) endif() diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin new file mode 100644 index 0000000000..2b587a2304 --- /dev/null +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d0f148441ce120b303896618cec364b5afb6f8b911b4785ec6358cfe8467cf7a +size 368640 diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index 76c3316009..f5a728018c 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -9,9 +9,8 @@ #if defined(HAVE_BENCHMARK) #include +#include #include -#include -#include #include #include #include @@ -21,6 +20,7 @@ #include #include #include +#include #include @@ -32,11 +32,9 @@ namespace Benchmark size_t GetMemorySize(void* memory); } - //static AZ::Debug::DrillerManager* s_drillerManager = nullptr; - /// /// Test allocator wrapper that redirects the calls to the passed TAllocator by using AZ::AllocatorInstance. - /// It also creates/destroys the TAllocator type and connects the driller (to reflect what happens at runtime) + /// It also creates/destroys the TAllocator type (to reflect what happens at runtime) /// /// Allocator type to wrap template @@ -46,16 +44,10 @@ namespace Benchmark static void SetUp() { AZ::AllocatorInstance::Create(); - - /*s_drillerManager = AZ::Debug::DrillerManager::Create(); - s_drillerManager->Register(aznew AZ::Debug::MemoryDriller);*/ } static void TearDown() { - /*AZ::Debug::DrillerManager::Destroy(s_drillerManager); - s_drillerManager = nullptr;*/ - AZ::AllocatorInstance::Destroy(); } @@ -410,15 +402,12 @@ namespace Benchmark enum OperationType : unsigned int { ALLOCATE, - DEALLOCATE, - REALLOCATE, - RESIZE + DEALLOCATE }; - OperationType m_operationType : 2; - size_t m_size : 46; - size_t m_alignment : 16; - void* m_ptr; - void* m_newptr; // required for resize + OperationType m_type : 1; + unsigned int m_size : 28; // Can represent up to 256Mb requests + unsigned int m_alignment : 7; // Can represent up to 128 alignment + unsigned int m_recordId : 28; // Can represent up to 256M simultaneous requests, we reuse ids }; public: @@ -428,18 +417,18 @@ namespace Benchmark { state.PauseTiming(); - AZStd::unordered_map pointerRemapping; - AZStd::unordered_map allocationSize; + AZStd::unordered_map pointerRemapping; constexpr size_t allocationOperationCount = 5 * 1024; AZStd::array m_operations = {}; - FILE* file = nullptr; - fopen_s(&file, "memoryrecordings.bin", "rb"); - if (!file) + AZ::IO::SystemFile file; + AZ::IO::FixedMaxPathString filePath = AZ::Utils::GetExecutableDirectory(); + filePath += "/Tests/AzCore/Memory/AllocatorBenchmarkRecordings.bin"; + if (!file.Open(filePath.c_str(), AZ::IO::SystemFile::OpenMode::SF_OPEN_READ_ONLY)) { return; } - size_t elementsRead = fread(&m_operations, sizeof(AllocatorOperation), allocationOperationCount, file); + size_t elementsRead = file.Read(sizeof(AllocatorOperation) * allocationOperationCount, &m_operations); size_t totalElementsRead = elementsRead; const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); size_t totalAllocationSize = 0; @@ -449,107 +438,54 @@ namespace Benchmark for (size_t operationIndex = 0; operationIndex < elementsRead; ++operationIndex) { const AllocatorOperation& operation = m_operations[operationIndex]; - switch (operation.m_operationType) + if (operation.m_type == AllocatorOperation::ALLOCATE) { - case AllocatorOperation::ALLOCATE: - { - if (operation.m_ptr) + const auto it = pointerRemapping.emplace(operation.m_recordId, nullptr); + if (it.second) // otherwise already allocated { - const auto it = pointerRemapping.emplace(operation.m_ptr, nullptr); - if (it.second) // otherwise already allocated - { - state.ResumeTiming(); - void* ptr = TestAllocatorType::Allocate(operation.m_size, operation.m_alignment); - state.PauseTiming(); - totalAllocationSize += operation.m_size; - it.first->second = ptr; - allocationSize[ptr] = operation.m_size; - } - else - { - //AZ_Warning("RecordedAllocationBenchmarkFixture", false, "Allocation on %p was already made", operation.m_ptr); - } + state.ResumeTiming(); + void* ptr = TestAllocatorType::Allocate(operation.m_size, operation.m_alignment); + state.PauseTiming(); + totalAllocationSize += operation.m_size; + it.first->second = ptr; } - break; - } - case AllocatorOperation::DEALLOCATE: - { - if (operation.m_ptr) // some deallocate(nullptr) are recorded + else { - const auto ptrIt = pointerRemapping.find(operation.m_ptr); + // Doing a resize, dont account for this memory change, this operation is rare and we dont have + // the size of the previous allocation + state.ResumeTiming(); + TestAllocatorType::Resize(it.first->second, operation.m_size); + state.PauseTiming(); + } + } + else // AllocatorOperation::DEALLOCATE: + { + if (operation.m_recordId) + { + const auto ptrIt = pointerRemapping.find(operation.m_recordId); if (ptrIt != pointerRemapping.end()) { - totalAllocationSize -= allocationSize[ptrIt->second]; + totalAllocationSize -= operation.m_size; state.ResumeTiming(); TestAllocatorType::DeAllocate(ptrIt->second, /*operation.m_size*/ 0); // size is not correct after a resize, a 0 size deals with it state.PauseTiming(); pointerRemapping.erase(ptrIt); } } - else + else // deallocate(nullptr) are recorded { // Just to account of the call of deallocate(nullptr); - // totalAllocationSize -= 0; // No real deallocation happened state.ResumeTiming(); - TestAllocatorType::DeAllocate(operation.m_ptr, /*operation.m_size*/ 0); + TestAllocatorType::DeAllocate(nullptr, /*operation.m_size*/ 0); state.PauseTiming(); } - break; - } - case AllocatorOperation::REALLOCATE: - { - void* ptr = nullptr; - if (operation.m_ptr) - { - AZ_Assert(operation.m_newptr, "Need to consider other cases?"); - const auto ptrIt = pointerRemapping.find(operation.m_ptr); - AZ_Assert(ptrIt != pointerRemapping.end(), "Missing allocation for reallocation"); // In case the recording didnt catch something - ptr = ptrIt->second; - pointerRemapping.erase(ptrIt); - } - AZ_Assert(operation.m_newptr != nullptr, "Reallocation failed in the game"); - const auto it = pointerRemapping.emplace(operation.m_newptr, nullptr); - if (it.second) - { - totalAllocationSize -= allocationSize[ptr]; - state.ResumeTiming(); - void* newPtr = TestAllocatorType::ReAllocate(ptr, operation.m_size, operation.m_alignment); - state.PauseTiming(); - totalAllocationSize += operation.m_size; - it.first->second = newPtr; - allocationSize[newPtr] = operation.m_size; - } - else - { - totalAllocationSize -= allocationSize[ptr]; - state.ResumeTiming(); - TestAllocatorType::DeAllocate(ptr); - state.PauseTiming(); - } - break; - } - case AllocatorOperation::RESIZE: - { - const auto ptrIt = pointerRemapping.find(operation.m_ptr); - AZ_Assert(ptrIt != pointerRemapping.end(), "Missing allocation for resize"); // In case the recording didnt catch something - totalAllocationSize -= allocationSize[ptrIt->second]; - state.ResumeTiming(); - TestAllocatorType::Resize(ptrIt->second, operation.m_size); - state.PauseTiming(); - totalAllocationSize += operation.m_size; - if (operation.m_size == 0) - { - pointerRemapping.erase(ptrIt); - } - break; - } } } - elementsRead = fread(&m_operations, sizeof(AllocatorOperation), allocationOperationCount, file); + elementsRead = file.Read(sizeof(AllocatorOperation) * allocationOperationCount, &m_operations); totalElementsRead += elementsRead; } - fclose(file); + file.Close(); state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); @@ -579,6 +515,7 @@ namespace Benchmark static void RecordedRunRanges(benchmark::internal::Benchmark* b) { b->Arg(1); + b->Iterations(100); } // For threaded ranges, run just 200, multi-threaded will already multiply by thread From 0d66278ef7c466e6457fc8f5cb99ac020022becd Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Wed, 8 Dec 2021 13:44:15 -0800 Subject: [PATCH 14/20] Makes the recorded benchmark more stable Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/AzCore/Memory/AllocatorBase.cpp | 23 ++- .../Memory/AllocatorBenchmarkRecordings.bin | 4 +- .../Tests/Memory/AllocatorBenchmarks.cpp | 186 +++++++++++------- 3 files changed, 135 insertions(+), 78 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp index 32ccd43f9a..48994c4eef 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp @@ -11,7 +11,7 @@ using namespace AZ; -#define RECORDING_ENABLED 1 +#define RECORDING_ENABLED 0 #if RECORDING_ENABLED @@ -44,18 +44,22 @@ namespace } }; - struct AllocatorOperation + #pragma pack(push, 1) + struct alignas(1) AllocatorOperation { - enum OperationType : unsigned int + enum OperationType : size_t { ALLOCATE, DEALLOCATE }; OperationType m_type: 1; - unsigned int m_size : 28; // Can represent up to 256Mb requests - unsigned int m_alignment : 7; // Can represent up to 128 alignment - unsigned int m_recordId : 28; // Can represent up to 256M simultaneous requests, we reuse ids + size_t m_size : 28; // Can represent up to 256Mb requests + size_t m_alignment : 7; // Can represent up to 128 alignment + size_t m_recordId : 28; // Can represent up to 256M simultaneous requests, we reuse ids }; + #pragma pack(pop) + static_assert(sizeof(AllocatorOperation) == 8); + static AZStd::mutex s_operationsMutex = {}; static constexpr size_t s_maxNumberOfAllocationsToRecord = 16384; @@ -76,7 +80,12 @@ namespace if (s_operationCounter == s_allocationOperationCount) { AZ::IO::SystemFile file; - file.Open("memoryrecordings.bin", AZ::IO::SystemFile::OpenMode::SF_OPEN_APPEND); + int mode = AZ::IO::SystemFile::OpenMode::SF_OPEN_APPEND | AZ::IO::SystemFile::OpenMode::SF_OPEN_WRITE_ONLY; + if (!file.Exists("memoryrecordings.bin")) + { + mode |= AZ::IO::SystemFile::OpenMode::SF_OPEN_CREATE; + } + file.Open("memoryrecordings.bin", mode); if (file.IsOpen()) { file.Write(&s_operations, sizeof(AllocatorOperation) * s_allocationOperationCount); diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin index 2b587a2304..ec5de82e83 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarkRecordings.bin @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d0f148441ce120b303896618cec364b5afb6f8b911b4785ec6358cfe8467cf7a -size 368640 +oid sha256:281ba03e79ecba90b313a0b17bdba87c57d76b504b6e38d579b5eabd995902cc +size 245760 diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index f5a728018c..aa1c7f21ab 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -390,115 +390,159 @@ namespace Benchmark } } }; - - template - class RecordedAllocationBenchmarkFixture : public AllocatorBenchmarkFixture - { - using base = AllocatorBenchmarkFixture; - using TestAllocatorType = typename base::TestAllocatorType; - struct AllocatorOperation + template + class RecordedAllocationBenchmarkFixture : public ::benchmark::Fixture + { + using TestAllocatorType = TestAllocatorWrapper; + + virtual void internalSetUp() { - enum OperationType : unsigned int + TestAllocatorType::SetUp(); + } + + void internalTearDown() + { + TestAllocatorType::TearDown(); + } + + #pragma pack(push, 1) + struct alignas(1) AllocatorOperation + { + enum OperationType : size_t { ALLOCATE, DEALLOCATE }; OperationType m_type : 1; - unsigned int m_size : 28; // Can represent up to 256Mb requests - unsigned int m_alignment : 7; // Can represent up to 128 alignment - unsigned int m_recordId : 28; // Can represent up to 256M simultaneous requests, we reuse ids + size_t m_size : 28; // Can represent up to 256Mb requests + size_t m_alignment : 7; // Can represent up to 128 alignment + size_t m_recordId : 28; // Can represent up to 256M simultaneous requests, we reuse ids }; + #pragma pack(pop) + static_assert(sizeof(AllocatorOperation) == 8); public: + void SetUp(const ::benchmark::State&) override + { + internalSetUp(); + } + void SetUp(::benchmark::State&) override + { + internalSetUp(); + } + + void TearDown(const ::benchmark::State&) override + { + internalTearDown(); + } + void TearDown(::benchmark::State&) override + { + internalTearDown(); + } + void Benchmark(benchmark::State& state) { for (auto _ : state) { state.PauseTiming(); - AZStd::unordered_map pointerRemapping; + AZStd::unordered_map pointerRemapping; constexpr size_t allocationOperationCount = 5 * 1024; AZStd::array m_operations = {}; + [[maybe_unused]] const size_t operationSize = sizeof(AllocatorOperation); - AZ::IO::SystemFile file; - AZ::IO::FixedMaxPathString filePath = AZ::Utils::GetExecutableDirectory(); - filePath += "/Tests/AzCore/Memory/AllocatorBenchmarkRecordings.bin"; - if (!file.Open(filePath.c_str(), AZ::IO::SystemFile::OpenMode::SF_OPEN_READ_ONLY)) - { - return; - } - size_t elementsRead = file.Read(sizeof(AllocatorOperation) * allocationOperationCount, &m_operations); - size_t totalElementsRead = elementsRead; const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); size_t totalAllocationSize = 0; + size_t itemsProcessed = 0; - while (elementsRead > 0) + for (size_t i = 0; i < 100; ++i) // replay the recording, this way we can keep a smaller recording { - for (size_t operationIndex = 0; operationIndex < elementsRead; ++operationIndex) + AZ::IO::SystemFile file; + AZ::IO::FixedMaxPathString filePath = AZ::Utils::GetExecutableDirectory(); + filePath += "/Tests/AzCore/Memory/AllocatorBenchmarkRecordings.bin"; + if (!file.Open(filePath.c_str(), AZ::IO::SystemFile::OpenMode::SF_OPEN_READ_ONLY)) { - const AllocatorOperation& operation = m_operations[operationIndex]; - if (operation.m_type == AllocatorOperation::ALLOCATE) + return; + } + size_t elementsRead = + file.Read(sizeof(AllocatorOperation) * allocationOperationCount, &m_operations) / sizeof(AllocatorOperation); + itemsProcessed += elementsRead; + + while (elementsRead > 0) + { + for (size_t operationIndex = 0; operationIndex < elementsRead; ++operationIndex) { - const auto it = pointerRemapping.emplace(operation.m_recordId, nullptr); - if (it.second) // otherwise already allocated + const AllocatorOperation& operation = m_operations[operationIndex]; + if (operation.m_type == AllocatorOperation::ALLOCATE) { - state.ResumeTiming(); - void* ptr = TestAllocatorType::Allocate(operation.m_size, operation.m_alignment); - state.PauseTiming(); - totalAllocationSize += operation.m_size; - it.first->second = ptr; - } - else - { - // Doing a resize, dont account for this memory change, this operation is rare and we dont have - // the size of the previous allocation - state.ResumeTiming(); - TestAllocatorType::Resize(it.first->second, operation.m_size); - state.PauseTiming(); - } - } - else // AllocatorOperation::DEALLOCATE: - { - if (operation.m_recordId) - { - const auto ptrIt = pointerRemapping.find(operation.m_recordId); - if (ptrIt != pointerRemapping.end()) + const auto it = pointerRemapping.emplace(operation.m_recordId, nullptr); + if (it.second) // otherwise already allocated { - totalAllocationSize -= operation.m_size; state.ResumeTiming(); - TestAllocatorType::DeAllocate(ptrIt->second, /*operation.m_size*/ 0); // size is not correct after a resize, a 0 size deals with it + void* ptr = TestAllocatorType::Allocate(operation.m_size, operation.m_alignment); + state.PauseTiming(); + totalAllocationSize += operation.m_size; + it.first->second = ptr; + } + else + { + // Doing a resize, dont account for this memory change, this operation is rare and we dont have + // the size of the previous allocation + state.ResumeTiming(); + TestAllocatorType::Resize(it.first->second, operation.m_size); state.PauseTiming(); - pointerRemapping.erase(ptrIt); } } - else // deallocate(nullptr) are recorded + else // AllocatorOperation::DEALLOCATE: { - // Just to account of the call of deallocate(nullptr); - state.ResumeTiming(); - TestAllocatorType::DeAllocate(nullptr, /*operation.m_size*/ 0); - state.PauseTiming(); + if (operation.m_recordId) + { + const auto ptrIt = pointerRemapping.find(operation.m_recordId); + if (ptrIt != pointerRemapping.end()) + { + totalAllocationSize -= operation.m_size; + state.ResumeTiming(); + TestAllocatorType::DeAllocate( + ptrIt->second, + /*operation.m_size*/ 0); // size is not correct after a resize, a 0 size deals with it + state.PauseTiming(); + pointerRemapping.erase(ptrIt); + } + } + else // deallocate(nullptr) are recorded + { + // Just to account of the call of deallocate(nullptr); + state.ResumeTiming(); + TestAllocatorType::DeAllocate(nullptr, /*operation.m_size*/ 0); + state.PauseTiming(); + } } } - } - elementsRead = file.Read(sizeof(AllocatorOperation) * allocationOperationCount, &m_operations); - totalElementsRead += elementsRead; + elementsRead = + file.Read(sizeof(AllocatorOperation) * allocationOperationCount, &m_operations) / sizeof(AllocatorOperation); + itemsProcessed += elementsRead; + } + file.Close(); + + // Deallocate the remainder (since we stopped the recording middle-game)(there are leaks as well) + for (const auto& pointerMapping : pointerRemapping) + { + state.ResumeTiming(); + TestAllocatorType::DeAllocate(pointerMapping.second); + state.PauseTiming(); + } + itemsProcessed += pointerRemapping.size(); + pointerRemapping.clear(); } - file.Close(); state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); state.counters[s_counterBenchmarkMemory] = benchmark::Counter(static_cast(totalAllocationSize), benchmark::Counter::kDefaults); - state.SetItemsProcessed(totalElementsRead); + state.SetItemsProcessed(itemsProcessed); - // Deallocate the remainder (since we stopped the recording middle-game)(there are leaks as well) - for (const auto& pointerMapping : pointerRemapping) - { - TestAllocatorType::DeAllocate(pointerMapping.second); - } - pointerRemapping.clear(); TestAllocatorType::GarbageCollect(); } } @@ -514,8 +558,7 @@ namespace Benchmark } static void RecordedRunRanges(benchmark::internal::Benchmark* b) { - b->Arg(1); - b->Iterations(100); + b->Iterations(1); } // For threaded ranges, run just 200, multi-threaded will already multiply by thread @@ -547,6 +590,11 @@ namespace Benchmark BM_REGISTER_TEMPLATE(RecordedAllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE)->Apply(RecordedRunRanges); \ } + /// Warm up benchmark used to prepare the OS for allocations. Most OS keep allocations for a process somehow + /// reserved. So the first allocations run always get a bigger impact in a process. This warm up allocator runs + /// all the benchmarks and is just used for the the next allocators to report more consistent results. + BM_REGISTER_ALLOCATOR(WarmUpAllocator, TestRawMallocAllocator); + BM_REGISTER_ALLOCATOR(RawMallocAllocator, TestRawMallocAllocator); BM_REGISTER_ALLOCATOR(MallocSchemaAllocator, TestMallocSchemaAllocator); BM_REGISTER_ALLOCATOR(HphaSchemaAllocator, TestHphaSchemaAllocator); From b96be71c619000a580e964ff51d84c14aa4c2c0f Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Wed, 8 Dec 2021 19:55:55 -0800 Subject: [PATCH 15/20] More stability changes, improvement on type usage within the benchmark, cleanup of unstable stats Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../Tests/Memory/AllocatorBenchmarks.cpp | 93 +++++++------------ 1 file changed, 35 insertions(+), 58 deletions(-) diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index aa1c7f21ab..5cf5176308 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -92,10 +92,10 @@ namespace Benchmark /// Basic allocator used as a baseline. This allocator is the most basic allocation possible with the OS (AZ_OS_MALLOC). /// MallocSchema cannot be used here because it has extra logic that we don't want to use as a baseline. /// - class TestRawMallocAllocator {}; + class RawMallocAllocator {}; template<> - class TestAllocatorWrapper + class TestAllocatorWrapper { public: TestAllocatorWrapper() @@ -113,17 +113,11 @@ namespace Benchmark } // IAllocatorAllocate - static void* Allocate(size_t byteSize, size_t alignment) + static void* Allocate(size_t byteSize, size_t) { s_numAllocatedBytes += byteSize; - if (alignment) - { - return AZ_OS_MALLOC(byteSize, alignment); - } - else - { - return AZ_OS_MALLOC(byteSize, 1); - } + // Don't pass an alignment since we wont be able to get the memory size without also passing the alignment + return AZ_OS_MALLOC(byteSize, 1); } static void DeAllocate(void* ptr, size_t = 0) @@ -132,20 +126,13 @@ namespace Benchmark AZ_OS_FREE(ptr); } - static void* ReAllocate(void* ptr, size_t newSize, size_t newAlignment) + static void* ReAllocate(void* ptr, size_t newSize, size_t) { s_numAllocatedBytes -= Platform::GetMemorySize(ptr); AZ_OS_FREE(ptr); s_numAllocatedBytes += newSize; - if (newAlignment) - { - return AZ_OS_MALLOC(newSize, newAlignment); - } - else - { - return AZ_OS_MALLOC(newSize, 1); - } + return AZ_OS_MALLOC(newSize, 1); } static size_t Resize(void* ptr, size_t newSize) @@ -172,51 +159,47 @@ namespace Benchmark static size_t s_numAllocatedBytes; }; - size_t TestAllocatorWrapper::s_numAllocatedBytes = 0; + size_t TestAllocatorWrapper::s_numAllocatedBytes = 0; - // Here we require to implement this to be able to configure a name for the allocator, otherswise the AllocatorManager crashes when trying to configure the overrides - class TestMallocSchemaAllocator : public AZ::SimpleSchemaAllocator + // Some allocator are not fully declared, those we simply setup from the schema + class MallocSchemaAllocator : public AZ::SimpleSchemaAllocator { public: - AZ_TYPE_INFO(TestMallocSchemaAllocator, "{3E68224F-E676-402C-8276-CE4B49C05E89}"); + AZ_TYPE_INFO(MallocSchemaAllocator, "{3E68224F-E676-402C-8276-CE4B49C05E89}"); - TestMallocSchemaAllocator() - : AZ::SimpleSchemaAllocator("TestMallocSchemaAllocator", "") + MallocSchemaAllocator() + : AZ::SimpleSchemaAllocator("MallocSchemaAllocator", "") {} }; - class TestHeapSchemaAllocator : public AZ::SimpleSchemaAllocator + // We use both this HphaSchemaAllocator and the SystemAllocator configured with Hpha because the SystemAllocator + // has extra things + class HphaSchemaAllocator : public AZ::SimpleSchemaAllocator { public: - AZ_TYPE_INFO(TestHeapSchemaAllocator, "{456E6C30-AA84-488F-BE47-5C1E6AF636B7}"); + AZ_TYPE_INFO(HphaSchemaAllocator, "{6563AB4B-A68E-4499-8C98-D61D640D1F7F}"); - TestHeapSchemaAllocator() - : AZ::SimpleSchemaAllocator("TestHeapSchemaAllocator", "") - {} - }; - - class TestHphaSchemaAllocator : public AZ::SimpleSchemaAllocator - { - public: - AZ_TYPE_INFO(TestHphaSchemaAllocator, "{6563AB4B-A68E-4499-8C98-D61D640D1F7F}"); - - TestHphaSchemaAllocator() + HphaSchemaAllocator() : AZ::SimpleSchemaAllocator("TestHphaSchemaAllocator", "") {} }; + // For the SystemAllocator we inherit so we have a different stack. The SystemAllocator is used globally so we dont want + // to get that data affecting the benchmark class TestSystemAllocator : public AZ::SystemAllocator { public: AZ_TYPE_INFO(TestSystemAllocator, "{360D4DAA-D65D-4D5C-A6FA-1A4C5261C35C}"); + + TestSystemAllocator() + : AZ::SystemAllocator() + { + } }; // Allocated bytes reported by the allocator static const char* s_counterAllocatorMemory = "Allocator_Memory"; - // Allocated bytes reported by the process - static const char* s_counterProcessMemory = "Process_Memory"; - // Allocated bytes as counted by the benchmark static const char* s_counterBenchmarkMemory = "Benchmark_Memory"; @@ -310,8 +293,7 @@ namespace Benchmark for (auto _ : state) { state.PauseTiming(); - const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); - + AZStd::vector& perThreadAllocations = base::GetPerThreadAllocations(state.thread_index); const size_t numberOfAllocations = perThreadAllocations.size(); size_t totalAllocationSize = 0; @@ -327,7 +309,6 @@ namespace Benchmark } state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); - state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); state.counters[s_counterBenchmarkMemory] = benchmark::Counter(static_cast(totalAllocationSize), benchmark::Counter::kDefaults); for (size_t allocationIndex = 0; allocationIndex < numberOfAllocations; ++allocationIndex) @@ -358,7 +339,6 @@ namespace Benchmark { state.PauseTiming(); AZStd::vector& perThreadAllocations = base::GetPerThreadAllocations(state.thread_index); - const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); const size_t numberOfAllocations = perThreadAllocations.size(); size_t totalAllocationSize = 0; @@ -381,7 +361,6 @@ namespace Benchmark } state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); - state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); state.counters[s_counterBenchmarkMemory] = benchmark::Counter(static_cast(totalAllocationSize), benchmark::Counter::kDefaults); state.SetItemsProcessed(numberOfAllocations); @@ -452,11 +431,10 @@ namespace Benchmark AZStd::array m_operations = {}; [[maybe_unused]] const size_t operationSize = sizeof(AllocatorOperation); - const size_t processMemoryBaseline = Platform::GetProcessMemoryUsageBytes(); size_t totalAllocationSize = 0; size_t itemsProcessed = 0; - for (size_t i = 0; i < 100; ++i) // replay the recording, this way we can keep a smaller recording + for (size_t i = 0; i < 100; ++i) // play the recording multiple times to get a good stable sample, this way we can keep a smaller recording { AZ::IO::SystemFile file; AZ::IO::FixedMaxPathString filePath = AZ::Utils::GetExecutableDirectory(); @@ -538,7 +516,6 @@ namespace Benchmark } state.counters[s_counterAllocatorMemory] = benchmark::Counter(static_cast(TestAllocatorType::NumAllocatedBytes()), benchmark::Counter::kDefaults); - state.counters[s_counterProcessMemory] = benchmark::Counter(static_cast(Platform::GetProcessMemoryUsageBytes() - processMemoryBaseline), benchmark::Counter::kDefaults); state.counters[s_counterBenchmarkMemory] = benchmark::Counter(static_cast(totalAllocationSize), benchmark::Counter::kDefaults); state.SetItemsProcessed(itemsProcessed); @@ -583,7 +560,7 @@ namespace Benchmark BM_REGISTER_TEMPLATE(FIXTURE, TESTNAME##_MIXED_THREADED, ALLOCATORTYPE, MIXED)->ThreadRange(2, MaxThreadRange)->Apply(ThreadedRunRanges); #define BM_REGISTER_ALLOCATOR(TESTNAME, ALLOCATORTYPE) \ - namespace TESTNAME \ + namespace BM_##TESTNAME \ { \ BM_REGISTER_SIZE_FIXTURES(AllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE); \ BM_REGISTER_SIZE_FIXTURES(DeAllocationBenchmarkFixture, TESTNAME, ALLOCATORTYPE); \ @@ -593,15 +570,15 @@ namespace Benchmark /// Warm up benchmark used to prepare the OS for allocations. Most OS keep allocations for a process somehow /// reserved. So the first allocations run always get a bigger impact in a process. This warm up allocator runs /// all the benchmarks and is just used for the the next allocators to report more consistent results. - BM_REGISTER_ALLOCATOR(WarmUpAllocator, TestRawMallocAllocator); + BM_REGISTER_ALLOCATOR(WarmUpAllocator, RawMallocAllocator); - BM_REGISTER_ALLOCATOR(RawMallocAllocator, TestRawMallocAllocator); - BM_REGISTER_ALLOCATOR(MallocSchemaAllocator, TestMallocSchemaAllocator); - BM_REGISTER_ALLOCATOR(HphaSchemaAllocator, TestHphaSchemaAllocator); + BM_REGISTER_ALLOCATOR(RawMallocAllocator, RawMallocAllocator); + BM_REGISTER_ALLOCATOR(MallocSchemaAllocator, MallocSchemaAllocator); + BM_REGISTER_ALLOCATOR(HphaSchemaAllocator, HphaSchemaAllocator); BM_REGISTER_ALLOCATOR(SystemAllocator, TestSystemAllocator); - + + //BM_REGISTER_ALLOCATOR(BestFitExternalMapAllocator, BestFitExternalMapAllocator); // Requires to pre-allocate blocks and cannot work as a general-purpose allocator //BM_REGISTER_ALLOCATOR(HeapSchemaAllocator, TestHeapSchemaAllocator); // Requires to pre-allocate blocks and cannot work as a general-purpose allocator - //BM_REGISTER_SCHEMA(BestFitExternalMapSchema); // Requires to implement AZ::IAllocatorAllocate //BM_REGISTER_SCHEMA(PoolSchema); // Requires special alignment requests while allocating #undef BM_REGISTER_ALLOCATOR From 947adc0248d9be1bac2565d1a67d081b0d1677c7 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Thu, 9 Dec 2021 17:13:09 -0800 Subject: [PATCH 16/20] Removal of OverrideShim, AP seems to be crashing Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/Component/ComponentApplication.cpp | 38 +-- .../AzCore/Component/ComponentApplication.h | 8 +- .../AzCore/AzCore/Compression/Compression.h | 4 +- .../AzCore/AzCore/Compression/compression.cpp | 6 +- .../AzCore/Compression/zstd_compression.cpp | 6 +- .../AzCore/Compression/zstd_compression.h | 5 +- .../AzCore/AzCore/IO/CompressorZStd.h | 2 +- .../AzCore/AzCore/IO/IStreamerTypes.cpp | 2 +- .../AzCore/AzCore/IO/IStreamerTypes.h | 4 +- .../AzCore/AzCore/Memory/AllocatorBase.cpp | 31 +- .../AzCore/AzCore/Memory/AllocatorBase.h | 9 +- .../AzCore/AzCore/Memory/AllocatorManager.cpp | 264 +----------------- .../AzCore/AzCore/Memory/AllocatorManager.h | 21 +- .../AzCore/Memory/AllocatorOverrideShim.cpp | 227 --------------- .../AzCore/Memory/AllocatorOverrideShim.h | 102 ------- .../Memory/BestFitExternalMapAllocator.cpp | 13 +- .../Memory/BestFitExternalMapAllocator.h | 7 +- .../Memory/BestFitExternalMapSchema.cpp | 20 +- .../AzCore/Memory/BestFitExternalMapSchema.h | 25 +- .../AzCore/AzCore/Memory/HeapSchema.cpp | 1 - .../AzCore/AzCore/Memory/HeapSchema.h | 5 +- .../AzCore/AzCore/Memory/HphaSchema.cpp | 2 +- .../AzCore/AzCore/Memory/HphaSchema.h | 5 +- .../AzCore/AzCore/Memory/IAllocator.cpp | 15 +- .../AzCore/AzCore/Memory/IAllocator.h | 65 +---- .../AzCore/AzCore/Memory/MallocSchema.cpp | 31 +- .../AzCore/AzCore/Memory/MallocSchema.h | 20 +- Code/Framework/AzCore/AzCore/Memory/Memory.h | 32 +-- .../AzCore/AzCore/Memory/OSAllocator.cpp | 1 - .../AzCore/AzCore/Memory/OSAllocator.h | 8 +- .../Memory/OverrunDetectionAllocator.cpp | 11 - .../AzCore/Memory/OverrunDetectionAllocator.h | 5 +- .../AzCore/AzCore/Memory/PoolAllocator.h | 2 +- .../AzCore/AzCore/Memory/PoolSchema.cpp | 25 +- .../AzCore/AzCore/Memory/PoolSchema.h | 8 +- .../AzCore/Memory/SimpleSchemaAllocator.h | 25 +- .../AzCore/AzCore/Memory/SystemAllocator.cpp | 40 ++- .../AzCore/AzCore/Memory/SystemAllocator.h | 24 +- .../AzCore/AzCore/Script/ScriptContext.cpp | 6 +- .../AzCore/AzCore/Script/ScriptContext.h | 2 +- .../AzCore/Serialization/AZStdContainers.inl | 4 +- .../AzCore/Serialization/SerializeContext.cpp | 2 +- .../AzCore/Serialization/SerializeContext.h | 18 +- .../Serialization/std/VariantReflection.inl | 4 +- .../AzCore/AzCore/azcore_files.cmake | 2 - Code/Framework/AzCore/Tests/AZStd/Hashed.cpp | 4 +- Code/Framework/AzCore/Tests/AZStd/Ordered.cpp | 4 +- Code/Framework/AzCore/Tests/Memory.cpp | 199 +++++++------ .../Tests/Memory/AllocatorBenchmarks.cpp | 2 +- .../AzCore/Tests/Memory/AllocatorManager.cpp | 85 +----- .../AzFramework/Archive/Archive.cpp | 2 +- .../AzFramework/Archive/IArchive.h | 2 +- .../AzFramework/Archive/ZipDirCache.cpp | 6 +- .../AzFramework/Archive/ZipDirCache.h | 4 +- .../AzFramework/Archive/ZipDirList.cpp | 2 +- .../AzFramework/Archive/ZipDirList.h | 4 +- .../AzFramework/Archive/ZipDirStructures.cpp | 8 +- Code/LauncherUnified/Launcher.h | 2 +- Code/Legacy/CryCommon/CryLegacyAllocator.h | 28 +- .../RHI/Code/Include/Atom/RHI/DrawPacket.h | 4 +- .../Code/Include/Atom/RHI/DrawPacketBuilder.h | 6 +- .../RHI/Code/Source/RHI/DrawPacketBuilder.cpp | 2 +- 62 files changed, 299 insertions(+), 1222 deletions(-) delete mode 100644 Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp delete mode 100644 Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.h diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp index 693b2f1648..a760061215 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.cpp @@ -152,8 +152,6 @@ namespace AZ m_reservedDebug = 0; m_recordingMode = Debug::AllocationRecords::RECORD_STACK_IF_NO_FILE_LINE; m_stackRecordLevels = 5; - m_useOverrunDetection = false; - m_useMalloc = false; } bool AppDescriptorConverter(SerializeContext& serialize, SerializeContext::DataElementNode& node) @@ -323,9 +321,6 @@ namespace AZ ->Field("blockSize", &Descriptor::m_memoryBlocksByteSize) ->Field("reservedOS", &Descriptor::m_reservedOS) ->Field("reservedDebug", &Descriptor::m_reservedDebug) - ->Field("useOverrunDetection", &Descriptor::m_useOverrunDetection) - ->Field("useMalloc", &Descriptor::m_useMalloc) - ->Field("allocatorRemappings", &Descriptor::m_allocatorRemappings) ->Field("modules", &Descriptor::m_modules) ; @@ -361,8 +356,6 @@ namespace AZ ->Attribute(Edit::Attributes::Step, &Descriptor::m_pageSize) ->DataElement(Edit::UIHandlers::SpinBox, &Descriptor::m_reservedOS, "OS reserved memory", "System memory reserved for OS (used only when 'Allocate all memory at startup' is true)") ->DataElement(Edit::UIHandlers::SpinBox, &Descriptor::m_reservedDebug, "Memory reserved for debugger", "System memory reserved for Debug allocator, like memory tracking (used only when 'Allocate all memory at startup' is true)") - ->DataElement(Edit::UIHandlers::CheckBox, &Descriptor::m_useOverrunDetection, "Use Overrun Detection", "Use the overrun detection memory manager (only available on some platforms, ignored in Release builds)") - ->DataElement(Edit::UIHandlers::CheckBox, &Descriptor::m_useMalloc, "Use Malloc", "Use malloc for memory allocations (for memory debugging only, ignored in Release builds)") ; } } @@ -879,7 +872,7 @@ namespace AZ AZ::AllocatorInstance::Create(desc); AZ::Debug::Trace::Instance().Init(); - AZ::Debug::AllocationRecords* records = AllocatorInstance::GetAllocator().GetRecords(); + AZ::Debug::AllocationRecords* records = AllocatorInstance::Get().GetRecords(); if (records) { records->SetMode(m_descriptor.m_recordingMode); @@ -891,35 +884,6 @@ namespace AZ m_isSystemAllocatorOwner = true; } - -#ifndef RELEASE - if (m_descriptor.m_useOverrunDetection) - { - OverrunDetectionSchema::Descriptor overrunDesc(false); - s_overrunDetectionSchema = Environment::CreateVariable(AzTypeInfo::Name(), overrunDesc); - OverrunDetectionSchema* schemaPtr = &s_overrunDetectionSchema.Get(); - - AZ::AllocatorManager::Instance().SetOverrideAllocatorSource(schemaPtr); - } - - if (m_descriptor.m_useMalloc) - { - AZ_Printf("Malloc", "WARNING: Malloc override is enabled. Registered allocators will use malloc instead of their normal allocation schemas."); - s_mallocSchema = Environment::CreateVariable(AzTypeInfo::Name()); - MallocSchema* schemaPtr = &s_mallocSchema.Get(); - - AZ::AllocatorManager::Instance().SetOverrideAllocatorSource(schemaPtr); - } -#endif - - AllocatorManager& allocatorManager = AZ::AllocatorManager::Instance(); - - for (const auto& remapping : m_descriptor.m_allocatorRemappings) - { - allocatorManager.AddAllocatorRemapping(remapping.m_from.c_str(), remapping.m_to.c_str()); - } - - allocatorManager.FinalizeConfiguration(); } void ComponentApplication::MergeSettingsToRegistry(SettingsRegistryInterface& registry) diff --git a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h index d53b8e1a4e..a28c58ac6b 100644 --- a/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h +++ b/Code/Framework/AzCore/AzCore/Component/ComponentApplication.h @@ -142,10 +142,6 @@ namespace AZ AZ::u64 m_reservedDebug; //!< Reserved memory for Debugging (allocation,etc.). Used only when m_grabAllMemory is set to true. (default: 0) Debug::AllocationRecords::Mode m_recordingMode; //!< When to record stack traces (default: AZ::Debug::AllocationRecords::RECORD_STACK_IF_NO_FILE_LINE) AZ::u64 m_stackRecordLevels; //!< If stack recording is enabled, how many stack levels to record. (default: 5) - bool m_useOverrunDetection; //!< True to use the overrun detection memory management scheme. Only available on some platforms; greatly increases memory consumption. - bool m_useMalloc; //!< True to use malloc instead of the internal memory manager. Intended for debugging purposes only. - - AllocatorRemappings m_allocatorRemappings; //!< List of remappings of allocators to perform, so that they can alias each other. ModuleDescriptorList m_modules; //!< Dynamic modules used by the application. //!< These will be loaded on startup. @@ -159,7 +155,7 @@ namespace AZ //! If set, this allocator is used to allocate the temporary bootstrap memory, as well as the main \ref SystemAllocator heap. //! If it's left nullptr (default), the \ref OSAllocator will be used. - IAllocatorAllocate* m_allocator = nullptr; + IAllocator* m_allocator = nullptr; //! Callback to create AZ::Modules for the static libraries linked by this application. //! Leave null if the application uses no static AZ::Modules. @@ -372,7 +368,7 @@ namespace AZ bool m_isOSAllocatorOwner{ false }; bool m_ownsConsole{}; void* m_fixedMemoryBlock{ nullptr }; //!< Pointer to the memory block allocator, so we can free it OnDestroy. - IAllocatorAllocate* m_osAllocator{ nullptr }; + IAllocator* m_osAllocator{ nullptr }; EntitySetType m_entities; AZ::SettingsRegistryInterface::NotifyEventHandler m_projectPathChangedHandler; diff --git a/Code/Framework/AzCore/AzCore/Compression/Compression.h b/Code/Framework/AzCore/AzCore/Compression/Compression.h index 855dcf9a6a..105fb9dbe6 100644 --- a/Code/Framework/AzCore/AzCore/Compression/Compression.h +++ b/Code/Framework/AzCore/AzCore/Compression/Compression.h @@ -15,7 +15,7 @@ struct z_stream_s; namespace AZ { class IAllocator; - class IAllocatorAllocate; + class IAllocatorSchema; /** * The most well known and used compression algorithm. It gives the best compression ratios even on level 1, @@ -90,7 +90,7 @@ namespace AZ z_stream_s* m_strDeflate; z_stream_s* m_strInflate; - IAllocatorAllocate* m_workMemoryAllocator; + IAllocatorSchema* m_workMemoryAllocator; }; } diff --git a/Code/Framework/AzCore/AzCore/Compression/compression.cpp b/Code/Framework/AzCore/AzCore/Compression/compression.cpp index 8b6f270b00..b5b775ef5e 100644 --- a/Code/Framework/AzCore/AzCore/Compression/compression.cpp +++ b/Code/Framework/AzCore/AzCore/Compression/compression.cpp @@ -26,7 +26,7 @@ ZLib::ZLib(IAllocator* workMemAllocator) : m_strDeflate(nullptr) , m_strInflate(nullptr) { - m_workMemoryAllocator = workMemAllocator ? workMemAllocator->GetAllocationSource() : nullptr; + m_workMemoryAllocator = workMemAllocator->GetSchema(); if (!m_workMemoryAllocator) { m_workMemoryAllocator = &AllocatorInstance::Get(); @@ -55,7 +55,7 @@ ZLib::~ZLib() //========================================================================= void* ZLib::AllocateMem(void* userData, unsigned int items, unsigned int size) { - IAllocatorAllocate* allocator = reinterpret_cast(userData); + IAllocator* allocator = reinterpret_cast(userData); return allocator->Allocate(items * size, 4, 0, "ZLib", __FILE__, __LINE__); } @@ -65,7 +65,7 @@ void* ZLib::AllocateMem(void* userData, unsigned int items, unsigned int size) //========================================================================= void ZLib::FreeMem(void* userData, void* address) { - IAllocatorAllocate* allocator = reinterpret_cast(userData); + IAllocator* allocator = reinterpret_cast(userData); allocator->DeAllocate(address); } diff --git a/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp b/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp index ecab62d123..95576a275b 100644 --- a/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp +++ b/Code/Framework/AzCore/AzCore/Compression/zstd_compression.cpp @@ -16,7 +16,7 @@ using namespace AZ; -ZStd::ZStd(IAllocatorAllocate* workMemAllocator) +ZStd::ZStd(IAllocator* workMemAllocator) { m_workMemoryAllocator = workMemAllocator; if (!m_workMemoryAllocator) @@ -41,13 +41,13 @@ ZStd::~ZStd() void* ZStd::AllocateMem(void* userData, size_t size) { - IAllocatorAllocate* allocator = reinterpret_cast(userData); + IAllocator* allocator = reinterpret_cast(userData); return allocator->Allocate(size, 4, 0, "ZStandard", __FILE__, __LINE__); } void ZStd::FreeMem(void* userData, void* address) { - IAllocatorAllocate* allocator = reinterpret_cast(userData); + IAllocator* allocator = reinterpret_cast(userData); allocator->DeAllocate(address); } diff --git a/Code/Framework/AzCore/AzCore/Compression/zstd_compression.h b/Code/Framework/AzCore/AzCore/Compression/zstd_compression.h index 3fe6677fff..70d8c37831 100644 --- a/Code/Framework/AzCore/AzCore/Compression/zstd_compression.h +++ b/Code/Framework/AzCore/AzCore/Compression/zstd_compression.h @@ -17,12 +17,11 @@ namespace AZ { class IAllocator; - class IAllocatorAllocate; class ZStd { public: - ZStd(IAllocatorAllocate* workMemAllocator = 0); + ZStd(IAllocator* workMemAllocator = 0); ~ZStd(); enum FlushType @@ -77,7 +76,7 @@ namespace AZ ZSTD_CStream* m_streamCompression; ZSTD_DStream* m_streamDecompression; - IAllocatorAllocate* m_workMemoryAllocator; + IAllocator* m_workMemoryAllocator; ZSTD_inBuffer m_inBuffer; ZSTD_outBuffer m_outBuffer; size_t m_nextBlockSize; diff --git a/Code/Framework/AzCore/AzCore/IO/CompressorZStd.h b/Code/Framework/AzCore/AzCore/IO/CompressorZStd.h index ed6b94fffa..9503f22665 100644 --- a/Code/Framework/AzCore/AzCore/IO/CompressorZStd.h +++ b/Code/Framework/AzCore/AzCore/IO/CompressorZStd.h @@ -48,7 +48,7 @@ namespace AZ public: AZ_CLASS_ALLOCATOR(CompressorZStdData, AZ::SystemAllocator, 0); - CompressorZStdData(IAllocatorAllocate* zstdMemAllocator = 0) + CompressorZStdData(IAllocator* zstdMemAllocator = 0) { m_zstd = zstdMemAllocator; } diff --git a/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.cpp b/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.cpp index 4c8272cfc8..4126d7457d 100644 --- a/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.cpp +++ b/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.cpp @@ -21,7 +21,7 @@ namespace AZ::IO::IStreamerTypes : m_allocator(AZ::AllocatorInstance::Get()) {} - DefaultRequestMemoryAllocator::DefaultRequestMemoryAllocator(AZ::IAllocatorAllocate& allocator) + DefaultRequestMemoryAllocator::DefaultRequestMemoryAllocator(AZ::IAllocator& allocator) : m_allocator(allocator) {} diff --git a/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.h b/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.h index 901fc5e594..2c4dc28518 100644 --- a/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.h +++ b/Code/Framework/AzCore/AzCore/IO/IStreamerTypes.h @@ -137,7 +137,7 @@ namespace AZ::IO::IStreamerTypes public: //! DefaultRequestMemoryAllocator wraps around the AZ::SystemAllocator by default. DefaultRequestMemoryAllocator(); - explicit DefaultRequestMemoryAllocator(AZ::IAllocatorAllocate& allocator); + explicit DefaultRequestMemoryAllocator(AZ::IAllocator& allocator); ~DefaultRequestMemoryAllocator() override; void LockAllocator() override; @@ -151,7 +151,7 @@ namespace AZ::IO::IStreamerTypes private: AZStd::atomic_int m_lockCounter{ 0 }; AZStd::atomic_int m_allocationCounter{ 0 }; - AZ::IAllocatorAllocate& m_allocator; + AZ::IAllocator& m_allocator; }; // The following alignment functions are put here until they're available in AzCore's math library. diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp index 48994c4eef..4da9ab384f 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp @@ -158,10 +158,10 @@ namespace } #endif -AllocatorBase::AllocatorBase(IAllocatorAllocate* allocationSource, const char* name, const char* desc) : - IAllocator(allocationSource), - m_name(name), - m_desc(desc) +AllocatorBase::AllocatorBase(IAllocatorSchema* allocationSchema, const char* name, const char* desc) + : IAllocator(allocationSchema) + , m_name(name) + , m_desc(desc) { } @@ -180,11 +180,6 @@ const char* AllocatorBase::GetDescription() const return m_desc; } -IAllocatorAllocate* AllocatorBase::GetSchema() -{ - return nullptr; -} - Debug::AllocationRecords* AllocatorBase::GetRecords() { return m_records; @@ -201,11 +196,6 @@ bool AllocatorBase::IsReady() const return m_isReady; } -bool AllocatorBase::CanBeOverridden() const -{ - return m_canBeOverridden; -} - void AllocatorBase::PostCreate() { if (m_registrationEnabled) @@ -266,11 +256,6 @@ bool AllocatorBase::IsProfilingActive() const return m_isProfilingActive; } -void AllocatorBase::DisableOverriding() -{ - m_canBeOverridden = false; -} - void AllocatorBase::DisableRegistration() { m_registrationEnabled = false; @@ -278,12 +263,12 @@ void AllocatorBase::DisableRegistration() void AllocatorBase::ProfileAllocation(void* ptr, size_t byteSize, size_t alignment, const char* name, const char* fileName, int lineNum, int suppressStackRecord) { -#if defined(AZ_HAS_VARIADIC_TEMPLATES) && defined(AZ_DEBUG_BUILD) - ++suppressStackRecord; // one more for the fact the ebus is a function -#endif // AZ_HAS_VARIADIC_TEMPLATES - if (m_isProfilingActive) { +#if defined(AZ_HAS_VARIADIC_TEMPLATES) && defined(AZ_DEBUG_BUILD) + ++suppressStackRecord; // one more for the fact the ebus is a function +#endif // AZ_HAS_VARIADIC_TEMPLATES + auto records = GetRecords(); if (records) { diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.h b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.h index 8f8a17e470..f2521087b3 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.h +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.h @@ -22,7 +22,7 @@ namespace AZ class AllocatorBase : public IAllocator { protected: - AllocatorBase(IAllocatorAllocate* allocationSource, const char* name, const char* desc); + AllocatorBase(IAllocatorSchema* allocationSchema, const char* name, const char* desc); ~AllocatorBase(); public: @@ -32,11 +32,9 @@ namespace AZ //--------------------------------------------------------------------- const char* GetName() const override; const char* GetDescription() const override; - IAllocatorAllocate* GetSchema() override; Debug::AllocationRecords* GetRecords() final; void SetRecords(Debug::AllocationRecords* records) final; bool IsReady() const final; - bool CanBeOverridden() const final; void PostCreate() override; void PreDestroy() final; void SetLazilyCreated(bool lazy) final; @@ -68,10 +66,6 @@ namespace AZ return byteSize; } - /// Call to disallow this allocator from being overridden. - /// Only kernel-level allocators where it would be especially problematic for them to be overridden should do this. - void DisableOverriding(); - /// Call to disallow this allocator from being registered with the AllocatorManager. /// Only kernel-level allocators where it would be especially problematic for them to be registered with the AllocatorManager should do this. void DisableRegistration(); @@ -107,7 +101,6 @@ namespace AZ bool m_isLazilyCreated = false; bool m_isProfilingActive = false; bool m_isReady = false; - bool m_canBeOverridden = true; bool m_registrationEnabled = true; }; diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp index 70ac813972..758fa222fe 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.cpp @@ -12,17 +12,12 @@ #include #include -#include #include #include #include #include -#if !defined(RELEASE) && !defined(AZCORE_MEMORY_ENABLE_OVERRIDES) -# define AZCORE_MEMORY_ENABLE_OVERRIDES -#endif - namespace AZ::Internal { struct AMStringHasher @@ -54,18 +49,6 @@ namespace AZ::Internal namespace AZ { -struct AllocatorManager::InternalData -{ - explicit InternalData(const AZStdIAllocator& alloc) - : m_allocatorMap(alloc) - , m_remappings(alloc) - , m_remappingsReverse(alloc) - {} - Internal::AllocatorNameMap m_allocatorMap; - Internal::AllocatorRemappings m_remappings; - Internal::AllocatorRemappings m_remappingsReverse; -}; - static EnvironmentVariable s_allocManager = nullptr; static AllocatorManager* s_allocManagerDebug = nullptr; // For easier viewing in crash dumps @@ -81,16 +64,6 @@ static Internal::PreEnvironmentAttachData& GetPreEnvironmentAttachData() void AllocatorManager::PreRegisterAllocator(IAllocator* allocator) { auto& data = GetPreEnvironmentAttachData(); - -#ifdef AZCORE_MEMORY_ENABLE_OVERRIDES - // All allocators must switch to an OverrideEnabledAllocationSource proxy if they are to support allocator overriding. - if (allocator->CanBeOverridden()) - { - auto shim = Internal::AllocatorOverrideShim::Create(allocator, &data.m_mallocSchema); - allocator->SetAllocationSource(shim); - } -#endif - { AZStd::lock_guard lock(data.m_mutex); AZ_Assert(data.m_unregisteredAllocatorCount < Internal::PreEnvironmentAttachData::MAX_UNREGISTERED_ALLOCATORS, "Too many allocators trying to register before environment attached!"); @@ -175,12 +148,9 @@ AllocatorManager::AllocatorManager() } ) { - m_overrideSource = nullptr; m_numAllocators = 0; m_isAllocatorLeaking = false; - m_configurationFinalized = false; m_defaultTrackingRecordMode = Debug::AllocationRecords::RECORD_NO_RECORDS; - m_data = new (m_mallocSchema->Allocate(sizeof(InternalData), AZStd::alignment_of::value, 0)) InternalData(AZStdIAllocator(m_mallocSchema.get())); } //========================================================================= @@ -210,10 +180,6 @@ AllocatorManager::RegisterAllocator(class IAllocator* alloc) alloc->SetProfilingActive(m_profilingRefcount.load() > 0); m_allocators[m_numAllocators++] = alloc; - -#ifdef AZCORE_MEMORY_ENABLE_OVERRIDES - ConfigureAllocatorOverrides(alloc); -#endif } //========================================================================= @@ -232,81 +198,12 @@ AllocatorManager::InternalDestroy() // Do not actually destroy the lazy allocator as it may have work to do during non-deterministic shutdown } - if (m_data) - { - m_data->~InternalData(); - m_mallocSchema->DeAllocate(m_data); - m_data = nullptr; - } - if (!m_isAllocatorLeaking) { AZ_Assert(m_numAllocators == 0, "There are still %d registered allocators!", m_numAllocators); } } -//========================================================================= -// ConfigureAllocatorOverrides -// [10/14/2018] -//========================================================================= -void -AllocatorManager::ConfigureAllocatorOverrides(IAllocator* alloc) -{ - auto record = m_data->m_allocatorMap.emplace(AZStd::piecewise_construct, AZStd::forward_as_tuple(alloc->GetName(), AZStdIAllocator(m_mallocSchema.get())), AZStd::forward_as_tuple(alloc)); - - // We only need to keep going if the allocator supports overrides. - if (!alloc->CanBeOverridden()) - { - return; - } - - if (!alloc->IsAllocationSourceChanged()) - { - // All allocators must switch to an OverrideEnabledAllocationSource proxy if they are to support allocator overriding. - auto overrideEnabled = Internal::AllocatorOverrideShim::Create(alloc, m_mallocSchema.get()); - alloc->SetAllocationSource(overrideEnabled); - } - - auto itr = m_data->m_remappings.find(record.first->first); - - if (itr != m_data->m_remappings.end()) - { - auto remapTo = m_data->m_allocatorMap.find(itr->second); - - if (remapTo != m_data->m_allocatorMap.end()) - { - static_cast(alloc->GetAllocationSource())->SetOverride(remapTo->second->GetOriginalAllocationSource()); - } - } - - itr = m_data->m_remappingsReverse.find(record.first->first); - - if (itr != m_data->m_remappingsReverse.end()) - { - auto remapFrom = m_data->m_allocatorMap.find(itr->second); - - if (remapFrom != m_data->m_allocatorMap.end()) - { - AZ_Assert(!m_configurationFinalized, "Allocators may only remap to allocators that have been created before configuration finalization"); - static_cast(remapFrom->second->GetAllocationSource())->SetOverride(alloc->GetOriginalAllocationSource()); - } - } - - if (m_overrideSource) - { - static_cast(alloc->GetAllocationSource())->SetOverride(m_overrideSource); - } - - if (m_configurationFinalized) - { - // We can get rid of the intermediary if configuration won't be changing any further. - // (The creation of it at the top of this function was superflous, but it made it easier to set things up going through a single code path.) - auto shim = static_cast(alloc->GetAllocationSource()); - alloc->SetAllocationSource(shim->GetOverride()); - Internal::AllocatorOverrideShim::Destroy(shim); - } -} - //========================================================================= // UnRegisterAllocator // [9/17/2009] @@ -365,7 +262,7 @@ AllocatorManager::GarbageCollect() for (int i = 0; i < m_numAllocators; ++i) { - m_allocators[i]->GetAllocationSource()->GarbageCollect(); + m_allocators[i]->GetSchema()->GarbageCollect(); } } @@ -414,94 +311,6 @@ AllocatorManager::SetTrackingMode(Debug::AllocationRecords::Mode mode) } } -//========================================================================= -// SetOverrideSchema -// [8/17/2018] -//========================================================================= -void -AllocatorManager::SetOverrideAllocatorSource(IAllocatorAllocate* source, bool overrideExistingAllocators) -{ - (void)source; - (void)overrideExistingAllocators; - -#ifdef AZCORE_MEMORY_ENABLE_OVERRIDES - AZ_Assert(!m_configurationFinalized, "You cannot set an allocator source after FinalizeConfiguration() has been called."); - m_overrideSource = source; - - if (overrideExistingAllocators) - { - AZStd::lock_guard lock(m_allocatorListMutex); - for (int i = 0; i < m_numAllocators; ++i) - { - if (m_allocators[i]->CanBeOverridden()) - { - auto shim = static_cast(m_allocators[i]->GetAllocationSource()); - shim->SetOverride(source); - } - } - } -#endif -} - -//========================================================================= -// AddAllocatorRemapping -// [8/27/2018] -//========================================================================= -void -AllocatorManager::AddAllocatorRemapping(const char* fromName, const char* toName) -{ - (void)fromName; - (void)toName; - -#ifdef AZCORE_MEMORY_ENABLE_OVERRIDES - AZ_Assert(!m_configurationFinalized, "You cannot set an allocator remapping after FinalizeConfiguration() has been called."); - m_data->m_remappings.emplace(AZStd::piecewise_construct, AZStd::forward_as_tuple(fromName, m_mallocSchema.get()), AZStd::forward_as_tuple(toName, m_mallocSchema.get())); - m_data->m_remappingsReverse.emplace(AZStd::piecewise_construct, AZStd::forward_as_tuple(toName, m_mallocSchema.get()), AZStd::forward_as_tuple(fromName, m_mallocSchema.get())); -#endif -} - -void -AllocatorManager::FinalizeConfiguration() -{ - if (m_configurationFinalized) - { - return; - } - -#ifdef AZCORE_MEMORY_ENABLE_OVERRIDES - { - AZStd::lock_guard lock(m_allocatorListMutex); - - for (int i = 0; i < m_numAllocators; ++i) - { - if (!m_allocators[i]->CanBeOverridden()) - { - continue; - } - - auto shim = static_cast(m_allocators[i]->GetAllocationSource()); - - if (!shim->IsOverridden()) - { - m_allocators[i]->ResetAllocationSource(); - Internal::AllocatorOverrideShim::Destroy(shim); - } - else if (!shim->HasOrphanedAllocations()) - { - m_allocators[i]->SetAllocationSource(shim->GetOverride()); - Internal::AllocatorOverrideShim::Destroy(shim); - } - else - { - shim->SetFinalizedConfiguration(); - } - } - } -#endif - - m_configurationFinalized = true; -} - void AllocatorManager::EnterProfilingMode() { @@ -545,27 +354,18 @@ AllocatorManager::DumpAllocators() size_t totalConsumedBytes = 0; memset(m_dumpInfo, 0, sizeof(m_dumpInfo)); - void* sourceList[m_maxNumAllocators]; AZ_Printf(TAG, "%d allocators active\n", m_numAllocators); AZ_Printf(TAG, "Index,Name,Used kb,Reserved kb,Consumed kb\n"); for (int i = 0; i < m_numAllocators; i++) { - auto allocator = m_allocators[i]; - auto source = allocator->GetAllocationSource(); + IAllocator* allocator = GetAllocator(i); const char* name = allocator->GetName(); - size_t usedBytes = source->NumAllocatedBytes(); - size_t reservedBytes = source->Capacity(); + size_t usedBytes = allocator->NumAllocatedBytes(); + size_t reservedBytes = allocator->Capacity(); size_t consumedBytes = reservedBytes; - // Very hacky and inefficient check to see if this allocator obtains its memory from another allocator - sourceList[i] = source; - if (AZStd::find(sourceList, sourceList + i, allocator->GetSchema()) != sourceList + i) - { - consumedBytes = 0; - } - totalUsedBytes += usedBytes; totalReservedBytes += reservedBytes; totalConsumedBytes += consumedBytes; @@ -585,61 +385,21 @@ void AllocatorManager::GetAllocatorStats(size_t& allocatedBytes, size_t& capacit AZStd::lock_guard lock(m_allocatorListMutex); const int allocatorCount = GetNumAllocators(); - AZStd::unordered_map existingAllocators; - AZStd::unordered_map sourcesToAllocators; // Build a mapping of original allocator sources to their allocators for (int i = 0; i < allocatorCount; ++i) { IAllocator* allocator = GetAllocator(i); - sourcesToAllocators.emplace(allocator->GetOriginalAllocationSource(), allocator); - } - - for (int i = 0; i < allocatorCount; ++i) - { - IAllocator* allocator = GetAllocator(i); - IAllocatorAllocate* source = allocator->GetAllocationSource(); - IAllocatorAllocate* originalSource = allocator->GetOriginalAllocationSource(); - IAllocatorAllocate* schema = allocator->GetSchema(); - IAllocator* alias = (source != originalSource) ? sourcesToAllocators[source] : nullptr; - - if (schema && !alias) - { - // Check to see if this allocator's source maps to another allocator - // Need to check both the schema and the allocator itself, as either one might be used as the alias depending on how it's implemented - AZStd::array checkAllocators = { { schema, allocator->GetAllocationSource() } }; - - for (IAllocatorAllocate* check : checkAllocators) - { - auto existing = existingAllocators.emplace(check, allocator); - - if (!existing.second) - { - alias = existing.first->second; - // Do not break out of the loop as we need to add to the map for all entries - } - } - } - - static const IAllocator* OS_ALLOCATOR = &AllocatorInstance::GetAllocator(); - size_t sourceAllocatedBytes = source->NumAllocatedBytes(); - size_t sourceCapacityBytes = source->Capacity(); - - if (allocator == OS_ALLOCATOR) - { - // Need to special case the OS allocator because its capacity is a made-up number. Better to just use the allocated amount, it will hopefully be small anyway. - sourceCapacityBytes = sourceAllocatedBytes; - } - + allocatedBytes += allocator->NumAllocatedBytes(); + capacityBytes += allocator->Capacity(); + if (outStats) { - outStats->emplace(outStats->end(), allocator->GetName(), alias ? alias->GetName() : allocator->GetDescription(), sourceAllocatedBytes, sourceCapacityBytes, alias != nullptr); - } - - if (!alias) - { - allocatedBytes += sourceAllocatedBytes; - capacityBytes += sourceCapacityBytes; + outStats->emplace(outStats->end(), + allocator->GetName(), + allocator->GetDescription(), + allocator->NumAllocatedBytes(), + allocator->Capacity()); } } } diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.h b/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.h index 14dec68ad1..0e8be07013 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.h +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorManager.h @@ -84,17 +84,6 @@ namespace AZ /// Especially for great code and engines... void SetAllocatorLeaking(bool allowLeaking) { m_isAllocatorLeaking = allowLeaking; } - /// Set an override allocator - /// All allocators registered with the AllocatorManager will automatically redirect to this allocator - /// if set. - void SetOverrideAllocatorSource(IAllocatorAllocate* source, bool overrideExistingAllocators = true); - - /// Retrieve the override schema - IAllocatorAllocate* GetOverrideAllocatorSource() const { return m_overrideSource; } - - void AddAllocatorRemapping(const char* fromName, const char* toName); - void FinalizeConfiguration(); - /// Enter or exit profiling mode; calls to Enter must be matched with calls to Exit void EnterProfilingMode(); void ExitProfilingMode(); @@ -113,19 +102,17 @@ namespace AZ struct AllocatorStats { - AllocatorStats(const char* name, const char* aliasOrDescription, size_t allocatedBytes, size_t capacityBytes, bool isAlias) + AllocatorStats(const char* name, const char* aliasOrDescription, size_t allocatedBytes, size_t capacityBytes) : m_name(name) , m_aliasOrDescription(aliasOrDescription) , m_allocatedBytes(allocatedBytes) , m_capacityBytes(capacityBytes) - , m_isAlias(isAlias) {} AZStd::string m_name; AZStd::string m_aliasOrDescription; size_t m_allocatedBytes; size_t m_capacityBytes; - bool m_isAlias; }; void GetAllocatorStats(size_t& usedBytes, size_t& reservedBytes, AZStd::vector* outStats = nullptr); @@ -157,7 +144,6 @@ namespace AZ private: void InternalDestroy(); - void ConfigureAllocatorOverrides(IAllocator* alloc); void DebugBreak(void* address, const Debug::AllocationInfo& info); AZ::MallocSchema* CreateMallocSchema(); @@ -172,14 +158,9 @@ namespace AZ MemoryBreak m_memoryBreak[MaxNumMemoryBreaks]; char m_activeBreaks; AZStd::mutex m_allocatorListMutex; - IAllocatorAllocate* m_overrideSource; DumpInfo m_dumpInfo[m_maxNumAllocators]; - struct InternalData; - - InternalData* m_data; - bool m_configurationFinalized; AZStd::atomic m_profilingRefcount; AZ::Debug::AllocationRecords::Mode m_defaultTrackingRecordMode; diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp deleted file mode 100644 index e4928e83c5..0000000000 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.cpp +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ - -#include - -namespace AZ::Internal -{ - AllocatorOverrideShim* AllocatorOverrideShim::Create(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource) - { - void* memory = shimAllocationSource->Allocate(sizeof(AllocatorOverrideShim), AZStd::alignment_of::value, 0); - auto result = new (memory) AllocatorOverrideShim(owningAllocator, shimAllocationSource); - return result; - } - - void AllocatorOverrideShim::Destroy(AllocatorOverrideShim* source) - { - auto shimAllocationSource = source->m_shimAllocationSource; - source->~AllocatorOverrideShim(); - shimAllocationSource->DeAllocate(source); - } - - AllocatorOverrideShim::AllocatorOverrideShim(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource) - : m_owningAllocator(owningAllocator) - , m_source(owningAllocator->GetOriginalAllocationSource()) - , m_overridingSource(owningAllocator->GetOriginalAllocationSource()) - , m_shimAllocationSource(shimAllocationSource) - , m_records(typename AllocationSet::hasher(), typename AllocationSet::key_eq(), StdAllocationSrc(shimAllocationSource)) - { - } - - void AllocatorOverrideShim::SetOverride(IAllocatorAllocate* source) - { - m_overridingSource = source; - } - - IAllocatorAllocate* AllocatorOverrideShim::GetOverride() const - { - return m_overridingSource; - } - - bool AllocatorOverrideShim::IsOverridden() const - { - return m_source != m_overridingSource; - } - - bool AllocatorOverrideShim::HasOrphanedAllocations() const - { - return !m_records.empty(); - } - - void AllocatorOverrideShim::SetFinalizedConfiguration() - { - m_finalizedConfiguration = true; - } - - typename AllocatorOverrideShim::pointer_type AllocatorOverrideShim::Allocate(size_type byteSize, size_type alignment, int flags, const char* name, const char* fileName, int lineNum, unsigned int suppressStackRecord) - { - pointer_type ptr = m_overridingSource->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord); - - if (!IsOverridden()) - { - lock_type lock(m_mutex); - m_records.insert(ptr); // Record in case we need to orphan this allocation later - } - - return ptr; - } - - void AllocatorOverrideShim::DeAllocate(pointer_type ptr, size_type byteSize, size_type alignment) - { - IAllocatorAllocate* source = m_overridingSource; - bool destroy = false; - - { - lock_type lock(m_mutex); - - // Check to see if this came from a prior allocation source - if (m_records.erase(ptr) && IsOverridden()) - { - source = m_source; - - if (m_records.empty() && m_finalizedConfiguration) - { - // All orphaned records are gone; we are no longer needed - m_owningAllocator->SetAllocationSource(m_overridingSource); - destroy = true; // Must destroy outside the lock - } - } - } - - source->DeAllocate(ptr, byteSize, alignment); - - if (destroy) - { - Destroy(this); - } - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::Resize(pointer_type ptr, size_type newSize) - { - IAllocatorAllocate* source = m_overridingSource; - - if (IsOverridden()) - { - // Determine who owns the allocation - lock_type lock(m_mutex); - - if (m_records.count(ptr)) - { - source = m_source; - } - } - - size_t result = source->Resize(ptr, newSize); - - return result; - } - - typename AllocatorOverrideShim::pointer_type AllocatorOverrideShim::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) - { - pointer_type newPtr = nullptr; - bool useOverride = true; - bool destroy = false; - - if (IsOverridden()) - { - lock_type lock(m_mutex); - - if (m_records.erase(ptr)) - { - // An old allocation needs to be transferred to the new, overriding allocator. - useOverride = false; // We'll do the reallocation here - size_t oldSize = m_source->AllocationSize(ptr); - - if (newSize) - { - newPtr = m_overridingSource->Allocate(newSize, newAlignment, 0); - memcpy(newPtr, ptr, AZStd::min(newSize, oldSize)); - } - - m_source->DeAllocate(ptr, oldSize); - - if (m_records.empty() && m_finalizedConfiguration) - { - // All orphaned records are gone; we are no longer needed - m_owningAllocator->SetAllocationSource(m_overridingSource); - destroy = true; // Must destroy outside the lock - } - } - } - - if (useOverride) - { - // Default behavior, we weren't deleting an old allocation - newPtr = m_overridingSource->ReAllocate(ptr, newSize, newAlignment); - - if (!IsOverridden()) - { - // Still need to do bookkeeping if we haven't been overridden yet - lock_type lock(m_mutex); - m_records.erase(ptr); - m_records.insert(newPtr); - } - } - - if (destroy) - { - Destroy(this); - } - - return newPtr; - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::AllocationSize(pointer_type ptr) - { - IAllocatorAllocate* source = m_overridingSource; - - if (IsOverridden()) - { - // Determine who owns the allocation - lock_type lock(m_mutex); - - if (m_records.count(ptr)) - { - source = m_source; - } - } - - return source->AllocationSize(ptr); - } - - void AllocatorOverrideShim::GarbageCollect() - { - m_source->GarbageCollect(); - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::NumAllocatedBytes() const - { - return m_source->NumAllocatedBytes(); - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::Capacity() const - { - return m_source->Capacity(); - } - - typename AllocatorOverrideShim::size_type AllocatorOverrideShim::GetMaxAllocationSize() const - { - return m_source->GetMaxAllocationSize(); - } - - auto AllocatorOverrideShim::GetMaxContiguousAllocationSize() const -> size_type - { - return m_source->GetMaxContiguousAllocationSize(); - } - - IAllocatorAllocate* AllocatorOverrideShim::GetSubAllocator() - { - return m_source->GetSubAllocator(); - } - -} // namespace AZ::Internal diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.h b/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.h deleted file mode 100644 index 3b45b9953e..0000000000 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorOverrideShim.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (c) Contributors to the Open 3D Engine Project. - * For complete copyright and license terms please see the LICENSE at the root of this distribution. - * - * SPDX-License-Identifier: Apache-2.0 OR MIT - * - */ -#pragma once - -#include -#include -#include - -namespace AZ -{ - class AllocatorManager; - - namespace Internal - { - /** - * A shim schema that solves the problem of overriding lazily-created allocators especially, that perform allocations before the override happens. - * - * Any allocator that *might* be overridden at some point must have this shim installed as its allocation source. This is done automatically by - * the AllocationManager; you generally do not have to interact with this shim directly at all. - * - * The shim will keep track of any allocations that occur, and if the allocator gets overridden it will ensure that prior allocations are - * deallocated with the old schema rather than the new one. - * - * There is some performance cost to this intrusion, however, in most cases it's only temporary: - * * Once the application calls FinalizeConfiguration(), any non-overridden allocators will have their shims destroyed. - * * Any overridden allocators that do not have prior allocations will have their shims destroyed. - * * Any overridden allocator will automatically destroy its shim once the last of the prior allocations has been deallocated. - * - * Note that an allocator that gets overridden but has prior allocations that it never intends to deallocate (such as file-level statics that never - * get changed) will keep its shim indefinitely. This is an unfortunate cost but only affects those allocators if they are being overridden. - */ - class AllocatorOverrideShim - : public IAllocatorAllocate - { - friend AllocatorManager; - - public: - //--------------------------------------------------------------------- - // IAllocator implementation - //--------------------------------------------------------------------- - pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = nullptr, const char* fileName = nullptr, int lineNum = 0, unsigned int suppressStackRecord = 0) override; - void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; - size_type Resize(pointer_type ptr, size_type newSize) override; - pointer_type ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) override; - size_type AllocationSize(pointer_type ptr) override; - void GarbageCollect() override; - size_type NumAllocatedBytes() const override; - size_type Capacity() const override; - size_type GetMaxAllocationSize() const override; - size_type GetMaxContiguousAllocationSize() const override; - IAllocatorAllocate* GetSubAllocator() override; - - private: - /// Creates a shim using a custom memory source - static AllocatorOverrideShim* Create(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource); - static void Destroy(AllocatorOverrideShim* source); - - AllocatorOverrideShim(IAllocator* owningAllocator, IAllocatorAllocate* shimAllocationSource); - - /// Overrides the shim's memory source with a different memory source. - void SetOverride(IAllocatorAllocate* source); - - /// Returns the override source. - IAllocatorAllocate* GetOverride() const; - - /// Returns true if the shim has an override source set on it. - bool IsOverridden() const; - - /// Returns true if there are orphaned allocations from before the shim had its override set. - bool HasOrphanedAllocations() const; - - /// Called by the AllocatorManager to signify that the configuration has been finalized by the application. - void SetFinalizedConfiguration(); - - private: - class StdAllocationSrc : public AZStdIAllocator - { - public: - StdAllocationSrc(IAllocatorAllocate* schema = nullptr) : AZStdIAllocator(schema) - { - } - }; - - typedef AZStd::mutex mutex_type; - typedef AZStd::lock_guard lock_type; - typedef AZStd::unordered_set, AZStd::equal_to, StdAllocationSrc> AllocationSet; - - IAllocator* m_owningAllocator; - IAllocatorAllocate* m_source; - IAllocatorAllocate* m_overridingSource; - IAllocatorAllocate* m_shimAllocationSource; - AllocationSet m_records; - mutex_type m_mutex; - bool m_finalizedConfiguration = false; - }; - } -} diff --git a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.cpp b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.cpp index 30b0b78fe5..da960ce3f3 100644 --- a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.cpp @@ -20,8 +20,7 @@ using namespace AZ; // [1/28/2011] //========================================================================= BestFitExternalMapAllocator::BestFitExternalMapAllocator() - : AllocatorBase(this, "BestFitExternalMapAllocator", "Best fit allocator with external tracking storage!") - , m_schema(nullptr) + : AllocatorBase(nullptr, "BestFitExternalMapAllocator", "Best fit allocator with external tracking storage!") {} //========================================================================= @@ -186,13 +185,3 @@ auto BestFitExternalMapAllocator::GetMaxContiguousAllocationSize() const -> size { return m_schema->GetMaxContiguousAllocationSize(); } - -//========================================================================= -// GetSubAllocator -// [1/28/2011] -//========================================================================= -IAllocatorAllocate* -BestFitExternalMapAllocator::GetSubAllocator() -{ - return m_schema->GetSubAllocator(); -} diff --git a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.h b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.h index 17425625b7..2cad404cd0 100644 --- a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapAllocator.h @@ -20,7 +20,6 @@ namespace AZ */ class BestFitExternalMapAllocator : public AllocatorBase - , public IAllocatorAllocate { public: AZ_TYPE_INFO(BestFitExternalMapAllocator, "{36266C8B-9A2C-4E3E-9812-3DB260868A2B}") @@ -38,7 +37,7 @@ namespace AZ static const int m_memoryBlockAlignment = 16; void* m_memoryBlock; ///< Pointer to memory to allocate from. Can be uncached. unsigned int m_memoryBlockByteSize; ///< Sizes if the memory block. - IAllocatorAllocate* m_mapAllocator; ///< Allocator for the free chunks map. If null the SystemAllocator will be used. + IAllocator* m_mapAllocator; ///< Allocator for the free chunks map. If null the SystemAllocator will be used. bool m_allocationRecords; ///< True if we want to track memory allocations, otherwise false. unsigned char m_stackRecordLevels; ///< If stack recording is enabled, how many stack levels to record. @@ -53,7 +52,7 @@ namespace AZ AllocatorDebugConfig GetDebugConfig() override; ////////////////////////////////////////////////////////////////////////// - // IAllocatorAllocate + // IAllocatorSchema pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override; void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; size_type Resize(pointer_type ptr, size_type newSize) override; @@ -64,7 +63,6 @@ namespace AZ size_type Capacity() const override; size_type GetMaxAllocationSize() const override; size_type GetMaxContiguousAllocationSize() const override; - IAllocatorAllocate* GetSubAllocator() override; ////////////////////////////////////////////////////////////////////////// protected: @@ -72,7 +70,6 @@ namespace AZ BestFitExternalMapAllocator& operator=(const BestFitExternalMapAllocator&); Descriptor m_desc; - BestFitExternalMapSchema* m_schema; }; } diff --git a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp index 715ecd221e..75356e85f3 100644 --- a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp @@ -30,6 +30,7 @@ BestFitExternalMapSchema::BestFitExternalMapSchema(const Descriptor& desc) //if( m_desc.m_memoryBlock == NULL) there is no point to automate this cause we need to flag this memory special, otherwise there is no point to use this allocator at all // m_desc.m_memoryBlock = azmalloc(SystemAllocator,m_desc.m_memoryBlockByteSize,16); m_freeChunksMap.insert(AZStd::make_pair(m_desc.m_memoryBlockByteSize, reinterpret_cast(m_desc.m_memoryBlock))); + } //========================================================================= @@ -37,7 +38,7 @@ BestFitExternalMapSchema::BestFitExternalMapSchema(const Descriptor& desc) // [1/28/2011] //========================================================================= BestFitExternalMapSchema::pointer_type -BestFitExternalMapSchema::Allocate(size_type byteSize, size_type alignment, int flags) +BestFitExternalMapSchema::Allocate(size_type byteSize, size_type alignment, int flags, const char*, const char*, int, unsigned int) { (void)flags; char* address = nullptr; @@ -91,8 +92,7 @@ BestFitExternalMapSchema::Allocate(size_type byteSize, size_type alignment, int // DeAllocate // [1/28/2011] //========================================================================= -void -BestFitExternalMapSchema::DeAllocate(pointer_type ptr) +void BestFitExternalMapSchema::DeAllocate(pointer_type ptr, size_type, size_type) { if (ptr == nullptr) { @@ -122,6 +122,20 @@ BestFitExternalMapSchema::AllocationSize(pointer_type ptr) return 0; } +BestFitExternalMapSchema::size_type +BestFitExternalMapSchema::Resize(pointer_type, size_type) +{ + AZ_Assert(false, AZ_FUNCTION_SIGNATURE " unsupported"); + return 0; +} + +BestFitExternalMapSchema::pointer_type +BestFitExternalMapSchema::ReAllocate(pointer_type, size_type, size_type) +{ + AZ_Assert(false, AZ_FUNCTION_SIGNATURE " unsupported"); + return nullptr; +} + //========================================================================= // GetMaxAllocationSize // [1/28/2011] diff --git a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h index eaab614593..63e9027dd2 100644 --- a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h +++ b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h @@ -21,7 +21,7 @@ namespace AZ * External map allows us to use this allocator with uncached memory, * because the tracking node is stored outside the main chunk. */ - class BestFitExternalMapSchema + class BestFitExternalMapSchema : public IAllocatorSchema { public: typedef void* pointer_type; @@ -45,26 +45,27 @@ namespace AZ static const int m_memoryBlockAlignment = 16; void* m_memoryBlock; ///< Pointer to memory to allocate from. Can be uncached. unsigned int m_memoryBlockByteSize; ///< Sizes if the memory block. - IAllocatorAllocate* m_mapAllocator; ///< Allocator for the free chunks map. If null the SystemAllocator will be used. + IAllocator* m_mapAllocator; ///< Allocator for the free chunks map. If null the SystemAllocator will be used. }; BestFitExternalMapSchema(const Descriptor& desc); - pointer_type Allocate(size_type byteSize, size_type alignment, int flags); - void DeAllocate(pointer_type ptr); - size_type AllocationSize(pointer_type ptr); + pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override; + void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; + size_type Resize(pointer_type ptr, size_type newSize) override; + pointer_type ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) override; + size_type AllocationSize(pointer_type ptr) override; - AZ_FORCE_INLINE size_type NumAllocatedBytes() const { return m_used; } - AZ_FORCE_INLINE size_type Capacity() const { return m_desc.m_memoryBlockByteSize; } - size_type GetMaxAllocationSize() const; - size_type GetMaxContiguousAllocationSize() const; - AZ_FORCE_INLINE IAllocatorAllocate* GetSubAllocator() const { return m_desc.m_mapAllocator; } + AZ_FORCE_INLINE size_type NumAllocatedBytes() const override { return m_used; } + AZ_FORCE_INLINE size_type Capacity() const override { return m_desc.m_memoryBlockByteSize; } + size_type GetMaxAllocationSize() const override; + size_type GetMaxContiguousAllocationSize() const override; /** - * Since we don't consolidate chucnks at free time (too expensive) we will do it as we need or when we can't + * Since we don't consolidate chunks at free time (too expensive) we will do it as we need or when we can't * allocate memory. This function is at least O(nlogn) where 'n' are the free chunks. */ - void GarbageCollect(); + void GarbageCollect() override; private: AZ_FORCE_INLINE size_type ChunckSize(pointer_type ptr); diff --git a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp index 1f0fc59a97..98ca1f87ed 100644 --- a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.cpp @@ -107,7 +107,6 @@ namespace AZ m_used = 0; m_desc = desc; - m_subAllocator = nullptr; for (int i = 0; i < Descriptor::m_maxNumBlocks; ++i) { diff --git a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h index 3a7716a127..ea4a4ebdc6 100644 --- a/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h +++ b/Code/Framework/AzCore/AzCore/Memory/HeapSchema.h @@ -17,7 +17,7 @@ namespace AZ * Internally uses use dlmalloc or version of it (nedmalloc, ptmalloc3). */ class HeapSchema - : public IAllocatorAllocate + : public IAllocatorSchema { public: typedef void* pointer_type; @@ -52,7 +52,6 @@ namespace AZ size_type Capacity() const override { return m_capacity; } size_type GetMaxAllocationSize() const override; size_type GetMaxContiguousAllocationSize() const override; - IAllocatorAllocate* GetSubAllocator() override { return m_subAllocator; } void GarbageCollect() override {} private: @@ -62,7 +61,7 @@ namespace AZ Descriptor m_desc; size_type m_capacity; ///< Capacity in bytes. size_type m_used; ///< Number of bytes in use. - IAllocatorAllocate* m_subAllocator; + IAllocatorSchema* m_subAllocator; bool m_ownMemoryBlock[Descriptor::m_maxNumBlocks]; }; } diff --git a/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp index 6e40ccd8cd..6891eb4248 100644 --- a/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/HphaSchema.cpp @@ -1081,7 +1081,7 @@ namespace AZ { const size_t m_treePageAlignment; const size_t m_poolPageSize; bool m_isPoolAllocations; - IAllocatorAllocate* m_subAllocator; + IAllocatorSchema* m_subAllocator; #if !defined (USE_MUTEX_PER_BUCKET) mutable AZStd::mutex m_mutex; diff --git a/Code/Framework/AzCore/AzCore/Memory/HphaSchema.h b/Code/Framework/AzCore/AzCore/Memory/HphaSchema.h index 5ee0205196..27dbd321d2 100644 --- a/Code/Framework/AzCore/AzCore/Memory/HphaSchema.h +++ b/Code/Framework/AzCore/AzCore/Memory/HphaSchema.h @@ -19,7 +19,7 @@ namespace AZ * Heap allocator schema, based on Dimitar Lazarov "High Performance Heap Allocator". */ class HphaSchema - : public IAllocatorAllocate + : public IAllocatorSchema { public: /** @@ -47,7 +47,7 @@ namespace AZ unsigned int m_isPoolAllocations : 1; ///< True to allow allocations from pools, otherwise false. size_t m_fixedMemoryBlockByteSize; ///< Memory block size, if 0 we use the OS memory allocation functions. void* m_fixedMemoryBlock; ///< Can be NULL if so the we will allocate memory from the subAllocator if m_memoryBlocksByteSize is != 0. - IAllocatorAllocate* m_subAllocator; ///< Allocator that m_memoryBlocks memory was allocated from or should be allocated (if NULL). + IAllocatorSchema* m_subAllocator; ///< Allocator that m_memoryBlocks memory was allocated from or should be allocated (if NULL). size_t m_systemChunkSize; ///< Size of chunk to request from the OS when more memory is needed (defaults to m_pageSize) size_t m_capacity; ///< Max size this allocator can grow to }; @@ -68,7 +68,6 @@ namespace AZ size_type GetMaxAllocationSize() const override; size_type GetMaxContiguousAllocationSize() const override; size_type GetUnAllocatedMemory(bool isPrint = false) const override; - IAllocatorAllocate* GetSubAllocator() override { return m_desc.m_subAllocator; } /// Return unused memory to the OS (if we don't use fixed block). Don't call this unless you really need free memory, it is slow. void GarbageCollect() override; diff --git a/Code/Framework/AzCore/AzCore/Memory/IAllocator.cpp b/Code/Framework/AzCore/AzCore/Memory/IAllocator.cpp index 08c567bf84..2d561b45ef 100644 --- a/Code/Framework/AzCore/AzCore/Memory/IAllocator.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/IAllocator.cpp @@ -9,23 +9,12 @@ namespace AZ { - IAllocator::IAllocator(IAllocatorAllocate* allocationSource) - : m_allocationSource(allocationSource) - , m_originalAllocationSource(allocationSource) + IAllocator::IAllocator(IAllocatorSchema* schema) + : m_schema(schema) { } IAllocator::~IAllocator() { } - - void IAllocator::SetAllocationSource(IAllocatorAllocate* allocationSource) - { - m_allocationSource = allocationSource; - } - - void IAllocator::ResetAllocationSource() - { - m_allocationSource = m_originalAllocationSource; - } } diff --git a/Code/Framework/AzCore/AzCore/Memory/IAllocator.h b/Code/Framework/AzCore/AzCore/Memory/IAllocator.h index 532335e50b..319175f9ee 100644 --- a/Code/Framework/AzCore/AzCore/Memory/IAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/IAllocator.h @@ -28,17 +28,16 @@ namespace AZ class AllocatorManager; /** - * Allocator alloc/free basic interface. It is separate because it can be used - * for user provided allocators overrides + * Allocator schema interface */ - class IAllocatorAllocate + class IAllocatorSchema { public: typedef void* pointer_type; typedef size_t size_type; typedef ptrdiff_t difference_type; - virtual ~IAllocatorAllocate() {} + virtual ~IAllocatorSchema() {} virtual pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) = 0; virtual void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) = 0; @@ -70,8 +69,6 @@ namespace AZ * that will be reported. */ virtual size_type GetUnAllocatedMemory(bool isPrint = false) const { (void)isPrint; return 0; } - /// Returns a pointer to a sub-allocator or NULL. - virtual IAllocatorAllocate* GetSubAllocator() = 0; }; /** @@ -100,56 +97,19 @@ namespace AZ /** * Interface class for all allocators. */ - class IAllocator + class IAllocator : public IAllocatorSchema { public: - IAllocator(IAllocatorAllocate* allocationSource); + IAllocator(IAllocatorSchema* schema = nullptr); virtual ~IAllocator(); - // @{ Every system allocator is required to provide name this is how + // Every system allocator is required to provide name this is how // it will be registered with the allocator manager. virtual const char* GetName() const = 0; virtual const char* GetDescription() const = 0; - // @} - //--------------------------------------------------------------------- - // Code releating to the allocation source is made concrete within this - // interface as a performance optimization. - //--------------------------------------------------------------------- - - /// Returns the current allocation source, which may be used to perform memory allocations. - AZ_FORCE_INLINE IAllocatorAllocate* GetAllocationSource() const - { - return m_allocationSource; - } - - /// Returns the original allocation source. Generally only used for debugging purposes. - AZ_FORCE_INLINE IAllocatorAllocate* GetOriginalAllocationSource() const - { - return m_originalAllocationSource; - } - - /// Returns true if the allocation source has changed from its original value. - AZ_FORCE_INLINE bool IsAllocationSourceChanged() const - { - return m_allocationSource != m_originalAllocationSource; - } - - /// Sets the allocation source, effectively overriding the allocator. - /// Be very careful doing this, as existing allocations will be deallocated through the new source, - /// typically leading to unwanted effects (such as crashes). - void SetAllocationSource(IAllocatorAllocate* allocationSource); - - /// Restores the allocation source to its original value. - /// Be very careful doing this, as allocations that came from the new source will now be deallocated - /// through the original source, typically leading to unwanted effects (such as crashes). - void ResetAllocationSource(); - - //--------------------------------------------------------------------- - - /// Returns the schema, if the allocator uses one. Returns nullptr if the allocator does not use a schema. - /// This is mainly used when debugging to determine if allocators alias each other under the hood. - virtual IAllocatorAllocate* GetSchema() = 0; + /// Returns the schema + AZ_FORCE_INLINE IAllocatorSchema* GetSchema() const { return m_schema; }; /// Returns the debug configuration for this allocator. virtual AllocatorDebugConfig GetDebugConfig() = 0; @@ -163,11 +123,6 @@ namespace AZ /// Returns true if this allocator is ready to use. virtual bool IsReady() const = 0; - /// Returns true if this allocator can be overridden with a different source. - /// Almost all allocators should return true. There are very few minor exceptions, such as the OS Allocator, that are required for direct - /// interfacing with the kernel and must never be overridden under any circumstances. - virtual bool CanBeOverridden() const = 0; - /// Returns true if the allocator was lazily created. Exposed primarily for testing systems that need to verify the state of allocators. virtual bool IsLazilyCreated() const = 0; @@ -195,9 +150,7 @@ namespace AZ virtual void Destroy() = 0; protected: - // The allocation source is made a direct member of the interface as a performance optimization. - IAllocatorAllocate * m_allocationSource; - IAllocatorAllocate* m_originalAllocationSource; + IAllocatorSchema* m_schema; template friend class AllocatorStorage::StoragePolicyBase; diff --git a/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp index 9aa31cd8b6..37fa277fab 100644 --- a/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/MallocSchema.cpp @@ -22,31 +22,15 @@ namespace AZ::Internal namespace AZ { + static constexpr size_t DEFAULT_ALIGNMENT = sizeof(void*) * 2; // Default malloc alignment + //--------------------------------------------------------------------- // MallocSchema methods //--------------------------------------------------------------------- - MallocSchema::MallocSchema(const Descriptor& desc) + MallocSchema::MallocSchema(const Descriptor&) : m_bytesAllocated(0) { - if (desc.m_useAZMalloc) - { - static const int DEFAULT_ALIGNMENT = sizeof(void*) * 2; // Default malloc alignment - - m_mallocFn = [](size_t byteSize) - { - return AZ_OS_MALLOC(byteSize, DEFAULT_ALIGNMENT); - }; - m_freeFn = [](void* ptr) - { - AZ_OS_FREE(ptr); - }; - } - else - { - m_mallocFn = &malloc; - m_freeFn = &free; - } } MallocSchema::~MallocSchema() @@ -84,7 +68,7 @@ namespace AZ ((alignment > sizeof(double)) ? alignment : 0); // Malloc will align to a minimum boundary for native objects, so we only pad if aligning to a large value - void* data = (*m_mallocFn)(required); + void* data = AZ_OS_MALLOC(required, DEFAULT_ALIGNMENT); void* result = PointerAlignUp(reinterpret_cast(reinterpret_cast(data) + sizeof(Internal::Header)), alignment); Internal::Header* header = PointerAlignDown( (Internal::Header*)(reinterpret_cast(result) - sizeof(Internal::Header)), AZStd::alignment_of::value); @@ -112,7 +96,7 @@ namespace AZ void* freePtr = reinterpret_cast(reinterpret_cast(ptr) - static_cast(header->offset)); m_bytesAllocated -= header->size; - (*m_freeFn)(freePtr); + AZ_OS_FREE(freePtr); } MallocSchema::pointer_type MallocSchema::ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) @@ -166,11 +150,6 @@ namespace AZ return AZ_CORE_MAX_ALLOCATOR_SIZE; } - IAllocatorAllocate* MallocSchema::GetSubAllocator() - { - return nullptr; - } - void MallocSchema::GarbageCollect() { } diff --git a/Code/Framework/AzCore/AzCore/Memory/MallocSchema.h b/Code/Framework/AzCore/AzCore/Memory/MallocSchema.h index 7a8c4a0366..02559fdb57 100644 --- a/Code/Framework/AzCore/AzCore/Memory/MallocSchema.h +++ b/Code/Framework/AzCore/AzCore/Memory/MallocSchema.h @@ -16,7 +16,7 @@ namespace AZ * Uses malloc internally. Mainly intended for debugging using host operating system features. */ class MallocSchema - : public IAllocatorAllocate + : public IAllocatorSchema { public: AZ_TYPE_INFO("MallocSchema", "{2A21D120-A42A-484C-997C-5735DCCA5FE9}"); @@ -25,21 +25,13 @@ namespace AZ typedef size_t size_type; typedef ptrdiff_t difference_type; - struct Descriptor - { - Descriptor(bool useAZMalloc = true) - : m_useAZMalloc(useAZMalloc) - { - } - - bool m_useAZMalloc; - }; + struct Descriptor {}; MallocSchema(const Descriptor& desc = Descriptor()); virtual ~MallocSchema(); //--------------------------------------------------------------------- - // IAllocatorAllocate + // IAllocatorSchema //--------------------------------------------------------------------- pointer_type Allocate(size_type byteSize, size_type alignment, int flags, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override; void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; @@ -51,15 +43,9 @@ namespace AZ size_type Capacity() const override; size_type GetMaxAllocationSize() const override; size_type GetMaxContiguousAllocationSize() const override; - IAllocatorAllocate* GetSubAllocator() override; void GarbageCollect() override; private: - typedef void* (*MallocFn)(size_t); - typedef void (*FreeFn)(void*); - AZStd::atomic m_bytesAllocated; - MallocFn m_mallocFn; - FreeFn m_freeFn; }; } diff --git a/Code/Framework/AzCore/AzCore/Memory/Memory.h b/Code/Framework/AzCore/AzCore/Memory/Memory.h index 2e08ec1b15..2ecba6ebff 100644 --- a/Code/Framework/AzCore/AzCore/Memory/Memory.h +++ b/Code/Framework/AzCore/AzCore/Memory/Memory.h @@ -141,9 +141,9 @@ void* operator new[](std::size_t, const AZ::Internal::AllocatorDummy*); */ #define azfree(...) AZ_MACRO_SPECIALIZE(azfree_, AZ_VA_NUM_ARGS(__VA_ARGS__), (__VA_ARGS__)) -/// Returns allocation size, based on it's pointer \ref AZ::IAllocatorAllocate::AllocationSize. +/// Returns allocation size, based on it's pointer \ref AZ::IAllocatorSchema::AllocationSize. #define azallocsize(_Ptr, _Allocator) AZ::AllocatorInstance< _Allocator >::Get().AllocationSize(_Ptr) -/// Returns the new expanded size or 0 if NOT supported by the allocator \ref AZ::IAllocatorAllocate::Resize. +/// Returns the new expanded size or 0 if NOT supported by the allocator \ref AZ::IAllocatorSchema::Resize. #define azallocresize(_Ptr, _NewSize, _Allocator) AZ::AllocatorInstance< _Allocator >::Get().Resize(_Ptr, _NewSize) namespace AZ { @@ -734,12 +734,15 @@ namespace AZ public: typedef typename Allocator::Descriptor Descriptor; - AZ_FORCE_INLINE static IAllocatorAllocate& Get() + // Maintained for backwards compatibility, prefer to use Get() instead. + // Get was previously used to get the the schema, however, that bypases what the allocators are doing. + // If the schema is needed, call Get().GetSchema() + AZ_FORCE_INLINE static IAllocator& GetAllocator() { - return *GetAllocator().GetAllocationSource(); + return StoragePolicy::GetAllocator(); } - AZ_FORCE_INLINE static IAllocator& GetAllocator() + AZ_FORCE_INLINE static IAllocator& Get() { return StoragePolicy::GetAllocator(); } @@ -781,7 +784,7 @@ namespace AZ // structure of another allocator template class ChildAllocatorSchema - : public IAllocatorAllocate + : public IAllocatorSchema { public: // No descriptor is necessary, as the parent allocator is expected to already @@ -792,7 +795,7 @@ namespace AZ ChildAllocatorSchema(const Descriptor&) {} //--------------------------------------------------------------------- - // IAllocatorAllocate + // IAllocatorSchema //--------------------------------------------------------------------- pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override { @@ -848,11 +851,6 @@ namespace AZ { return AZ::AllocatorInstance::Get().GetUnAllocatedMemory(isPrint); } - - IAllocatorAllocate* GetSubAllocator() override - { - return AZ::AllocatorInstance::Get().GetSubAllocator(); - } }; /** @@ -873,7 +871,7 @@ namespace AZ { if (AllocatorInstance::IsReady()) { - m_name = AllocatorInstance::GetAllocator().GetName(); + m_name = AllocatorInstance::Get().GetName(); } else { @@ -932,7 +930,7 @@ namespace AZ typedef AZStd::ptrdiff_t difference_type; typedef AZStd::false_type allow_memory_leaks; ///< Regular allocators should not leak. - AZ_FORCE_INLINE AZStdIAllocator(IAllocatorAllocate* allocator, const char* name = "AZ::AZStdIAllocator") + AZ_FORCE_INLINE AZStdIAllocator(IAllocator* allocator, const char* name = "AZ::AZStdIAllocator") : m_allocator(allocator) , m_name(name) { @@ -965,7 +963,7 @@ namespace AZ AZ_FORCE_INLINE bool operator==(const AZStdIAllocator& rhs) const { return m_allocator == rhs.m_allocator; } AZ_FORCE_INLINE bool operator!=(const AZStdIAllocator& rhs) const { return m_allocator != rhs.m_allocator; } private: - IAllocatorAllocate* m_allocator; + IAllocator* m_allocator; const char* m_name; }; @@ -982,8 +980,8 @@ namespace AZ using size_type = AZStd::size_t; using difference_type = AZStd::ptrdiff_t; using allow_memory_leaks = AZStd::false_type; ///< Regular allocators should not leak. - using functor_type = IAllocatorAllocate&(*)(); ///< Function Pointer must return IAllocatorAllocate&. - ///< function pointers do not support covariant return types + using functor_type = IAllocator&(*)(); ///< Function Pointer must return IAllocator&. + ///< function pointers do not support covariant return types constexpr AZStdFunctorAllocator(functor_type allocatorFunctor, const char* name = "AZ::AZStdFunctorAllocator") : m_allocatorFunctor(allocatorFunctor) diff --git a/Code/Framework/AzCore/AzCore/Memory/OSAllocator.cpp b/Code/Framework/AzCore/AzCore/Memory/OSAllocator.cpp index 317df214d6..293cd92354 100644 --- a/Code/Framework/AzCore/AzCore/Memory/OSAllocator.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/OSAllocator.cpp @@ -19,7 +19,6 @@ namespace AZ , m_custom(nullptr) , m_numAllocatedBytes(0) { - DisableOverriding(); } //========================================================================= diff --git a/Code/Framework/AzCore/AzCore/Memory/OSAllocator.h b/Code/Framework/AzCore/AzCore/Memory/OSAllocator.h index 3a8080483b..0bdf7d9fed 100644 --- a/Code/Framework/AzCore/AzCore/Memory/OSAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/OSAllocator.h @@ -24,7 +24,6 @@ namespace AZ */ class OSAllocator : public AllocatorBase - , public IAllocatorAllocate { public: AZ_TYPE_INFO(OSAllocator, "{9F835EE3-F23C-454E-B4E3-011E2F3C8118}") @@ -39,7 +38,7 @@ namespace AZ { Descriptor() : m_custom(0) {} - IAllocatorAllocate* m_custom; ///< You can provide our own allocation scheme. If NULL a HeapScheme will be used with the provided Descriptor. + IAllocatorSchema* m_custom; ///< You can provide our own allocation scheme. If NULL a HeapScheme will be used with the provided Descriptor. }; bool Create(const Descriptor& desc); @@ -51,7 +50,7 @@ namespace AZ AllocatorDebugConfig GetDebugConfig() override; ////////////////////////////////////////////////////////////////////////// - // IAllocatorAllocate + // IAllocatorSchema pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override; void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; size_type Resize(pointer_type ptr, size_type newSize) override { return m_custom ? m_custom->Resize(ptr, newSize) : 0; } @@ -62,13 +61,12 @@ namespace AZ size_type Capacity() const override { return m_custom ? m_custom->Capacity() : AZ_CORE_MAX_ALLOCATOR_SIZE; } // custom size or unlimited size_type GetMaxAllocationSize() const override { return m_custom ? m_custom->GetMaxAllocationSize() : AZ_CORE_MAX_ALLOCATOR_SIZE; } // custom size or unlimited size_type GetMaxContiguousAllocationSize() const override { return m_custom ? m_custom->GetMaxContiguousAllocationSize() : AZ_CORE_MAX_ALLOCATOR_SIZE; } // custom size or unlimited - IAllocatorAllocate* GetSubAllocator() override { return m_custom ? m_custom : NULL; } protected: OSAllocator(const OSAllocator&); OSAllocator& operator=(const OSAllocator&); - IAllocatorAllocate* m_custom; + IAllocatorSchema* m_custom; size_type m_numAllocatedBytes; }; diff --git a/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.cpp b/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.cpp index ed5e0febc2..096fbbac95 100644 --- a/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.cpp @@ -233,7 +233,6 @@ namespace AZ size_type Capacity() const; size_type GetMaxAllocationSize() const; size_type GetMaxContiguousAllocationSize() const; - IAllocatorAllocate* GetSubAllocator(); void GarbageCollect(); private: @@ -680,11 +679,6 @@ auto AZ::OverrunDetectionSchemaImpl::GetMaxContiguousAllocationSize() const -> s return 0; } -AZ::IAllocatorAllocate* AZ::OverrunDetectionSchemaImpl::GetSubAllocator() -{ - return nullptr; -} - void AZ::OverrunDetectionSchemaImpl::GarbageCollect() { } @@ -810,11 +804,6 @@ auto AZ::OverrunDetectionSchema::GetMaxContiguousAllocationSize() const -> size_ return m_impl->GetMaxContiguousAllocationSize(); } -AZ::IAllocatorAllocate* AZ::OverrunDetectionSchema::GetSubAllocator() -{ - return m_impl->GetSubAllocator(); -} - void AZ::OverrunDetectionSchema::GarbageCollect() { m_impl->GarbageCollect(); diff --git a/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.h b/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.h index 9895a5f84f..073b54160b 100644 --- a/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/OverrunDetectionAllocator.h @@ -27,7 +27,7 @@ namespace AZ * the requested memory, plus the trap page). On most platforms this is 8kb (4kb * 2 pages). */ class OverrunDetectionSchema - : public IAllocatorAllocate + : public IAllocatorSchema { public: AZ_TYPE_INFO("OverrunDetectionSchema", "{0DF781AC-1615-40AE-81F7-6CA5841E2914}"); @@ -75,7 +75,7 @@ namespace AZ virtual ~OverrunDetectionSchema(); //--------------------------------------------------------------------- - // IAllocatorAllocate + // IAllocatorSchema //--------------------------------------------------------------------- pointer_type Allocate(size_type byteSize, size_type alignment, int flags, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override; void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; @@ -87,7 +87,6 @@ namespace AZ size_type Capacity() const override; size_type GetMaxAllocationSize() const override; size_type GetMaxContiguousAllocationSize() const override; - IAllocatorAllocate* GetSubAllocator() override; void GarbageCollect() override; private: diff --git a/Code/Framework/AzCore/AzCore/Memory/PoolAllocator.h b/Code/Framework/AzCore/AzCore/Memory/PoolAllocator.h index a03f7b5b92..f55c6251bf 100644 --- a/Code/Framework/AzCore/AzCore/Memory/PoolAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/PoolAllocator.h @@ -102,7 +102,7 @@ namespace AZ } ////////////////////////////////////////////////////////////////////////// - // IAllocatorAllocate + // IAllocatorSchema pointer_type ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) override { (void)ptr; diff --git a/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp index 9167864450..60f4f34f1d 100644 --- a/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/PoolSchema.cpp @@ -163,7 +163,7 @@ namespace AZ } using AllocatorType = PoolAllocation; - IAllocatorAllocate* m_pageAllocator; + IAllocatorSchema* m_pageAllocator; AllocatorType m_allocator; void* m_staticDataBlock; unsigned int m_numStaticPages; @@ -295,7 +295,7 @@ namespace AZ FreePagesType m_freePages; AZStd::vector m_threads; ///< Array with all separate thread data. Used to traverse end free elements. - IAllocatorAllocate* m_pageAllocator; + IAllocatorSchema* m_pageAllocator; void* m_staticDataBlock; size_t m_numStaticPages; size_t m_pageSize; @@ -732,17 +732,6 @@ PoolSchema::Capacity() const return m_impl->m_numStaticPages * m_impl->m_pageSize; } -//========================================================================= -// GetPageAllocator -// [11/17/2010] -//========================================================================= -IAllocatorAllocate* -PoolSchema::GetSubAllocator() -{ - return m_impl->m_pageAllocator; -} - - ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // PollAllocator Implementation @@ -1090,16 +1079,6 @@ ThreadPoolSchema::Capacity() const return m_impl->m_numStaticPages * m_impl->m_pageSize; } -//========================================================================= -// GetPageAllocator -// [11/17/2010] -//========================================================================= -IAllocatorAllocate* -ThreadPoolSchema::GetSubAllocator() -{ - return m_impl->m_pageAllocator; -} - //========================================================================= // ThreadPoolSchemaImpl diff --git a/Code/Framework/AzCore/AzCore/Memory/PoolSchema.h b/Code/Framework/AzCore/AzCore/Memory/PoolSchema.h index cfc5e3ea07..d9faf976b3 100644 --- a/Code/Framework/AzCore/AzCore/Memory/PoolSchema.h +++ b/Code/Framework/AzCore/AzCore/Memory/PoolSchema.h @@ -22,7 +22,7 @@ namespace AZ * use ThreadPool Schema or do the sync yourself. */ class PoolSchema - : public IAllocatorAllocate + : public IAllocatorSchema { public: /** @@ -52,7 +52,7 @@ namespace AZ * this is the minimum number of pages we will have allocated at all times, otherwise the total number of pages supported. */ unsigned int m_numStaticPages; - IAllocatorAllocate* m_pageAllocator; ///< If you provide this interface we will use it for page allocations, otherwise SystemAllocator will be used. + IAllocatorSchema* m_pageAllocator; ///< If you provide this interface we will use it for page allocations, otherwise SystemAllocator will be used. }; PoolSchema(const Descriptor& desc = Descriptor()); @@ -73,7 +73,6 @@ namespace AZ size_type GetMaxContiguousAllocationSize() const override; size_type NumAllocatedBytes() const override; size_type Capacity() const override; - IAllocatorAllocate* GetSubAllocator() override; protected: PoolSchema(const PoolSchema&); @@ -90,7 +89,7 @@ namespace AZ * for each thread. So there will be some memory overhead, especially if you use fixed pool sizes. */ class ThreadPoolSchema - : public IAllocatorAllocate + : public IAllocatorSchema { public: // Functions for getting an instance of a ThreadPoolData when using thread local storage @@ -119,7 +118,6 @@ namespace AZ size_type GetMaxContiguousAllocationSize() const override; size_type NumAllocatedBytes() const override; size_type Capacity() const override; - IAllocatorAllocate* GetSubAllocator() override; protected: ThreadPoolSchema(const ThreadPoolSchema&); diff --git a/Code/Framework/AzCore/AzCore/Memory/SimpleSchemaAllocator.h b/Code/Framework/AzCore/AzCore/Memory/SimpleSchemaAllocator.h index 5fbc890203..76be66786e 100644 --- a/Code/Framework/AzCore/AzCore/Memory/SimpleSchemaAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/SimpleSchemaAllocator.h @@ -22,17 +22,15 @@ namespace AZ template class SimpleSchemaAllocator : public AllocatorBase - , public IAllocatorAllocate { public: using Descriptor = DescriptorType; - using pointer_type = typename IAllocatorAllocate::pointer_type; - using size_type = typename IAllocatorAllocate::size_type; - using difference_type = typename IAllocatorAllocate::difference_type; + using pointer_type = typename Schema::pointer_type; + using size_type = typename Schema::size_type; + using difference_type = typename Schema::difference_type; SimpleSchemaAllocator(const char* name, const char* desc) - : AllocatorBase(this, name, desc) - , m_schema(nullptr) + : AllocatorBase(nullptr, name, desc) { } @@ -65,13 +63,8 @@ namespace AZ return AllocatorDebugConfig(); } - IAllocatorAllocate* GetSchema() override - { - return m_schema; - } - //--------------------------------------------------------------------- - // IAllocatorAllocate + // IAllocatorSchema //--------------------------------------------------------------------- pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = nullptr, const char* fileName = nullptr, int lineNum = 0, unsigned int suppressStackRecord = 0) override { @@ -188,14 +181,6 @@ namespace AZ { return m_schema->GetUnAllocatedMemory(isPrint); } - - IAllocatorAllocate* GetSubAllocator() override - { - return m_schema->GetSubAllocator(); - } - - protected: - IAllocatorAllocate* m_schema; private: typename AZStd::aligned_storage::value>::type m_schemaStorage; diff --git a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp index e56f4f045d..c403df0ed6 100644 --- a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp @@ -51,9 +51,8 @@ static bool g_isSystemSchemaUsed = false; // [9/2/2009] //========================================================================= SystemAllocator::SystemAllocator() - : AllocatorBase(this, "SystemAllocator", "Fundamental generic memory allocator") + : AllocatorBase(nullptr, "SystemAllocator", "Fundamental generic memory allocator") , m_isCustom(false) - , m_allocator(nullptr) , m_ownsOSAllocator(false) { } @@ -93,7 +92,7 @@ SystemAllocator::Create(const Descriptor& desc) if (desc.m_custom) { m_isCustom = true; - m_allocator = desc.m_custom; + m_schema = desc.m_custom; isReady = true; } else @@ -121,9 +120,9 @@ SystemAllocator::Create(const Descriptor& desc) AZ_Assert(!g_isSystemSchemaUsed, "AZ::SystemAllocator MUST be created first! It's the source of all allocations!"); #if AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HPHA - m_allocator = new(&g_systemSchema)HphaSchema(heapDesc); + m_schema = new (&g_systemSchema) HphaSchema(heapDesc); #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC - m_allocator = new(&g_systemSchema)MallocSchema(heapDesc); + m_schema = new (&g_systemSchema) MallocSchema(heapDesc); #endif g_isSystemSchemaUsed = true; isReady = true; @@ -134,11 +133,11 @@ SystemAllocator::Create(const Descriptor& desc) AZ_Assert(AllocatorInstance::IsReady(), "System allocator must be created before any other allocator! They allocate from it."); #if AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HPHA - m_allocator = azcreate(HphaSchema, (heapDesc), SystemAllocator); + m_schema = azcreate(HphaSchema, (heapDesc), SystemAllocator); #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC - m_allocator = azcreate(MallocSchema, (heapDesc), SystemAllocator); + m_schema = azcreate(MallocSchema, (heapDesc), SystemAllocator); #endif - if (m_allocator == nullptr) + if (m_schema == nullptr) { isReady = false; } @@ -167,18 +166,18 @@ SystemAllocator::Destroy() if (!m_isCustom) { - if ((void*)m_allocator == (void*)&g_systemSchema) + if ((void*)m_schema == (void*)&g_systemSchema) { #if AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HPHA - static_cast(m_allocator)->~HphaSchema(); + static_cast(m_schema)->~HphaSchema(); #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC - static_cast(m_allocator)->~MallocSchema(); + static_cast(m_schema)->~MallocSchema(); #endif g_isSystemSchemaUsed = false; } else { - azdestroy(m_allocator); + azdestroy(m_schema); } } @@ -198,11 +197,6 @@ AllocatorDebugConfig SystemAllocator::GetDebugConfig() .ExcludeFromDebugging(!m_desc.m_allocationRecords); } -IAllocatorAllocate* SystemAllocator::GetSchema() -{ - return m_allocator; -} - //========================================================================= // Allocate // [9/2/2009] @@ -218,14 +212,14 @@ SystemAllocator::Allocate(size_type byteSize, size_type alignment, int flags, co AZ_Assert((alignment & (alignment - 1)) == 0, "Alignment must be power of 2!"); byteSize = MemorySizeAdjustedUp(byteSize); - SystemAllocator::pointer_type address = m_allocator->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord + 1); + SystemAllocator::pointer_type address = m_schema->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord + 1); if (address == nullptr) { // Free all memory we can and try again! AllocatorManager::Instance().GarbageCollect(); - address = m_allocator->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord + 1); + address = m_schema->Allocate(byteSize, alignment, flags, name, fileName, lineNum, suppressStackRecord + 1); } if (address == nullptr) @@ -251,7 +245,7 @@ SystemAllocator::DeAllocate(pointer_type ptr, size_type byteSize, size_type alig byteSize = MemorySizeAdjustedUp(byteSize); AZ_PROFILE_MEMORY_FREE(MemoryReserved, ptr); AZ_MEMORY_PROFILE(ProfileDeallocation(ptr, byteSize, alignment, nullptr)); - m_allocator->DeAllocate(ptr, byteSize, alignment); + m_schema->DeAllocate(ptr, byteSize, alignment); } //========================================================================= @@ -265,7 +259,7 @@ SystemAllocator::ReAllocate(pointer_type ptr, size_type newSize, size_type newAl AZ_MEMORY_PROFILE(ProfileReallocationBegin(ptr, newSize)); AZ_PROFILE_MEMORY_FREE(MemoryReserved, ptr); - pointer_type newAddress = m_allocator->ReAllocate(ptr, newSize, newAlignment); + pointer_type newAddress = m_schema->ReAllocate(ptr, newSize, newAlignment); AZ_PROFILE_MEMORY_ALLOC(MemoryReserved, newAddress, newSize, "SystemAllocator realloc"); AZ_MEMORY_PROFILE(ProfileReallocationEnd(ptr, newAddress, newSize, newAlignment)); @@ -280,7 +274,7 @@ SystemAllocator::size_type SystemAllocator::Resize(pointer_type ptr, size_type newSize) { newSize = MemorySizeAdjustedUp(newSize); - size_type resizedSize = m_allocator->Resize(ptr, newSize); + size_type resizedSize = m_schema->Resize(ptr, newSize); AZ_MEMORY_PROFILE(ProfileResize(ptr, resizedSize)); @@ -294,7 +288,7 @@ SystemAllocator::Resize(pointer_type ptr, size_type newSize) SystemAllocator::size_type SystemAllocator::AllocationSize(pointer_type ptr) { - size_type allocSize = MemorySizeAdjustedDown(m_allocator->AllocationSize(ptr)); + size_type allocSize = MemorySizeAdjustedDown(m_schema->AllocationSize(ptr)); return allocSize; } diff --git a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.h b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.h index c02ada5843..0ea4251b8a 100644 --- a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.h @@ -26,7 +26,6 @@ namespace AZ */ class SystemAllocator : public AllocatorBase - , public IAllocatorAllocate { public: AZ_TYPE_INFO(SystemAllocator, "{424C94D8-85CF-4E89-8CD6-AB5EC173E875}") @@ -39,7 +38,7 @@ namespace AZ * we will allocate system memory using system calls. You can * provide arenas (spaces) with pre-allocated memory, and use the * flag to specify which arena you want to allocate from. - * You are also allowed to supply IAllocatorAllocate, but if you do + * You are also allowed to supply IAllocatorSchema, but if you do * so you will need to take care of all allocations, we will not use * the default HeapSchema. * \ref HeapSchema::Descriptor @@ -51,7 +50,7 @@ namespace AZ , m_allocationRecords(true) , m_stackRecordLevels(5) {} - IAllocatorAllocate* m_custom; ///< You can provide our own allocation scheme. If NULL a HeapScheme will be used with the provided Descriptor. + IAllocatorSchema* m_custom; ///< You can provide our own allocation scheme. If NULL a HeapScheme will be used with the provided Descriptor. struct Heap { @@ -73,7 +72,7 @@ namespace AZ int m_numFixedMemoryBlocks; ///< Number of memory blocks to use. void* m_fixedMemoryBlocks[m_maxNumFixedBlocks]; ///< Pointers to provided memory blocks or NULL if you want the system to allocate them for you with the System Allocator. size_t m_fixedMemoryBlocksByteSize[m_maxNumFixedBlocks]; ///< Sizes of different memory blocks (MUST be multiple of m_pageSize), if m_memoryBlock is 0 the block will be allocated for you with the System Allocator. - IAllocatorAllocate* m_subAllocator; ///< Allocator that m_memoryBlocks memory was allocated from or should be allocated (if NULL). + IAllocatorSchema* m_subAllocator; ///< Allocator that m_memoryBlocks memory was allocated from or should be allocated (if NULL). size_t m_systemChunkSize; ///< Size of chunk to request from the OS when more memory is needed (defaults to m_pageSize) } m_heap; bool m_allocationRecords; ///< True if we want to track memory allocations, otherwise false. @@ -87,25 +86,23 @@ namespace AZ ////////////////////////////////////////////////////////////////////////// // IAllocator AllocatorDebugConfig GetDebugConfig() override; - IAllocatorAllocate* GetSchema() override; ////////////////////////////////////////////////////////////////////////// - // IAllocatorAllocate + // IAllocatorSchema pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override; void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; pointer_type ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) override; size_type Resize(pointer_type ptr, size_type newSize) override; size_type AllocationSize(pointer_type ptr) override; - void GarbageCollect() override { m_allocator->GarbageCollect(); } + void GarbageCollect() override { GetSchema()->GarbageCollect(); } - size_type NumAllocatedBytes() const override { return m_allocator->NumAllocatedBytes(); } - size_type Capacity() const override { return m_allocator->Capacity(); } + size_type NumAllocatedBytes() const override { return GetSchema()->NumAllocatedBytes(); } + size_type Capacity() const override { return GetSchema()->Capacity(); } /// Keep in mind this operation will execute GarbageCollect to make sure it returns, max allocation. This function WILL be slow. - size_type GetMaxAllocationSize() const override { return m_allocator->GetMaxAllocationSize(); } - size_type GetMaxContiguousAllocationSize() const override { return m_allocator->GetMaxContiguousAllocationSize(); } - size_type GetUnAllocatedMemory(bool isPrint = false) const override { return m_allocator->GetUnAllocatedMemory(isPrint); } - IAllocatorAllocate* GetSubAllocator() override { return m_isCustom ? m_allocator : m_allocator->GetSubAllocator(); } + size_type GetMaxAllocationSize() const override { return GetSchema()->GetMaxAllocationSize(); } + size_type GetMaxContiguousAllocationSize() const override { return GetSchema()->GetMaxContiguousAllocationSize(); } + size_type GetUnAllocatedMemory(bool isPrint = false) const override { return GetSchema()->GetUnAllocatedMemory(isPrint); } ////////////////////////////////////////////////////////////////////////// @@ -115,7 +112,6 @@ namespace AZ Descriptor m_desc; bool m_isCustom; - IAllocatorAllocate* m_allocator; bool m_ownsOSAllocator; }; } diff --git a/Code/Framework/AzCore/AzCore/Script/ScriptContext.cpp b/Code/Framework/AzCore/AzCore/Script/ScriptContext.cpp index 841387f14d..b6f42e79ec 100644 --- a/Code/Framework/AzCore/AzCore/Script/ScriptContext.cpp +++ b/Code/Framework/AzCore/AzCore/Script/ScriptContext.cpp @@ -1456,7 +1456,7 @@ using namespace AZ; static void* LuaMemoryHook(void* userData, void* ptr, size_t osize, size_t nsize) { (void)osize; - IAllocatorAllocate* allocator = reinterpret_cast(userData); + IAllocator* allocator = reinterpret_cast(userData); if (nsize == 0) { if (ptr) @@ -4274,7 +4274,7 @@ LUA_API const Node* lua_getDummyNode() AZ_CLASS_ALLOCATOR(ScriptContextImpl, AZ::SystemAllocator, 0); ////////////////////////////////////////////////////////////////////////// - ScriptContextImpl(ScriptContext* owner, IAllocatorAllocate* allocator, lua_State* nativeContext) + ScriptContextImpl(ScriptContext* owner, IAllocator* allocator, lua_State* nativeContext) : m_owner(owner) , m_context(nullptr) , m_debug(nullptr) @@ -5827,7 +5827,7 @@ LUA_API const Node* lua_getDummyNode() }; } // namespace AZ - ScriptContext::ScriptContext(ScriptContextId id, IAllocatorAllocate* allocator, lua_State* nativeContext) + ScriptContext::ScriptContext(ScriptContextId id, IAllocator* allocator, lua_State* nativeContext) { m_id = id; m_impl = aznew ScriptContextImpl(this, allocator, nativeContext); diff --git a/Code/Framework/AzCore/AzCore/Script/ScriptContext.h b/Code/Framework/AzCore/AzCore/Script/ScriptContext.h index bb63a9368d..8784bf7ca7 100644 --- a/Code/Framework/AzCore/AzCore/Script/ScriptContext.h +++ b/Code/Framework/AzCore/AzCore/Script/ScriptContext.h @@ -822,7 +822,7 @@ namespace AZ CustomFromLua m_fromLua; }; - ScriptContext(ScriptContextId id = ScriptContextIds::DefaultScriptContextId, IAllocatorAllocate* allocator = nullptr, lua_State* nativeContext = nullptr); + ScriptContext(ScriptContextId id = ScriptContextIds::DefaultScriptContextId, IAllocator* allocator = nullptr, lua_State* nativeContext = nullptr); ~ScriptContext(); /// Bind LUA context (VM) a specific behaviorContext diff --git a/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl b/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl index a633af22da..c1ecd3634a 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl +++ b/Code/Framework/AzCore/AzCore/Serialization/AZStdContainers.inl @@ -74,7 +74,7 @@ namespace AZ * But as the AZStdAssociativeContainer instance will not be accessed outside of the module it was * created within then this will return this .dll/.exe module allocator */ - classElement.m_attributes.set_allocator(AZStdFunctorAllocator([]() -> IAllocatorAllocate& { return GetCurrentSerializeContextModule().GetAllocator(); })); + classElement.m_attributes.set_allocator(AZStdFunctorAllocator([]() -> IAllocator& { return GetCurrentSerializeContextModule().GetAllocator(); })); // Flag the field with the EnumType attribute if we're an enumeration type aliased by RemoveEnum const bool isSpecializedEnum = AZStd::is_enum::value && !AzTypeInfo::Uuid().IsNull(); @@ -650,7 +650,7 @@ namespace AZ * But as the AZStdAssociativeContainer instance will not be accessed outside of the module it was * created within then this will return this .dll/.exe module allocator */ - m_classElement.m_attributes.set_allocator(AZStdFunctorAllocator([]() -> IAllocatorAllocate& { return GetCurrentSerializeContextModule().GetAllocator(); })); + m_classElement.m_attributes.set_allocator(AZStdFunctorAllocator([]() -> IAllocator& { return GetCurrentSerializeContextModule().GetAllocator(); })); m_classElement.m_attributes.emplace_back(AZ_CRC("KeyType", 0x15bc5303), CreateModuleAttribute(AZStd::move(uuid))); } diff --git a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp index ff0ad571f7..227ba3dc75 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp +++ b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.cpp @@ -3245,7 +3245,7 @@ namespace AZ return genericClassInfoFoundIt != m_moduleLocalGenericClassInfos.end() ? genericClassInfoFoundIt->second : nullptr; } - AZ::IAllocatorAllocate& SerializeContext::PerModuleGenericClassInfo::GetAllocator() + AZ::IAllocator& SerializeContext::PerModuleGenericClassInfo::GetAllocator() { return m_moduleOSAllocator; } diff --git a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h index bf96bcdb9a..f8daa8c328 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h +++ b/Code/Framework/AzCore/AzCore/Serialization/SerializeContext.h @@ -574,10 +574,10 @@ namespace AZ GenericClassInfo* m_genericClassInfo = nullptr; ///< Valid when the generic class is set. So you don't search for the actual type in the class register. Edit::ElementData* m_editData{}; ///< Pointer to edit data (generated by EditContext). AZStd::vector m_attributes{ - AZStdFunctorAllocator([]() -> IAllocatorAllocate& { return AZ::AllocatorInstance::Get(); }) + AZStdFunctorAllocator([]() -> IAllocator& { return AZ::AllocatorInstance::Get(); }) }; ///< Attributes attached to ClassElement. Lambda is required here as AZStdFunctorAllocator expects a function pointer - ///< that returns an IAllocatorAllocate& and the AZ::AllocatorInstance::Get returns an AZ::SystemAllocator& - /// which while it inherits from IAllocatorAllocate, does not work as function pointers do not support covariant return types + ///< that returns an IAllocator& and the AZ::AllocatorInstance::Get returns an AZ::SystemAllocator& + /// which while it inherits from IAllocator, does not work as function pointers do not support covariant return types AttributeOwnership m_attributeOwnership = AttributeOwnership::Parent; int m_flags{}; ///< }; @@ -639,12 +639,12 @@ namespace AZ DataPatchUpgradeHandler m_dataPatchUpgrader; ///< Attributes for this class type. Lambda is required here as AZStdFunctorAllocator expects a function pointer - ///< that returns an IAllocatorAllocate& and the AZ::AllocatorInstance::Get returns an AZ::SystemAllocator& - /// which while it inherits from IAllocatorAllocate, does not work as function pointers do not support covariant return types + ///< that returns an IAllocator& and the AZ::AllocatorInstance::Get returns an AZ::SystemAllocator& + /// which while it inherits from IAllocator, does not work as function pointers do not support covariant return types AZStd::vector m_attributes{AZStdFunctorAllocator(&GetSystemAllocator) }; private: - static IAllocatorAllocate& GetSystemAllocator() + static IAllocator& GetSystemAllocator() { return AZ::AllocatorInstance::Get(); } @@ -2483,7 +2483,7 @@ namespace AZ PerModuleGenericClassInfo(); ~PerModuleGenericClassInfo(); - AZ::IAllocatorAllocate& GetAllocator(); + AZ::IAllocator& GetAllocator(); void AddGenericClassInfo(AZ::GenericClassInfo* genericClassInfo); void RemoveGenericClassInfo(const AZ::TypeId& canonicalTypeId); @@ -2546,12 +2546,12 @@ namespace AZ template AttributePtr CreateModuleAttribute(T&& attrValue) { - IAllocatorAllocate& moduleAllocator = GetCurrentSerializeContextModule().GetAllocator(); + IAllocator& moduleAllocator = GetCurrentSerializeContextModule().GetAllocator(); void* rawMemory = moduleAllocator.Allocate(sizeof(ContainerType), alignof(ContainerType)); new (rawMemory) ContainerType{ AZStd::forward(attrValue) }; auto attributeDeleter = [](Attribute* attribute) { - IAllocatorAllocate& moduleAllocator = GetCurrentSerializeContextModule().GetAllocator(); + IAllocator& moduleAllocator = GetCurrentSerializeContextModule().GetAllocator(); attribute->~Attribute(); moduleAllocator.DeAllocate(attribute); }; diff --git a/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl b/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl index 06d4f76c80..2712baa859 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl +++ b/Code/Framework/AzCore/AzCore/Serialization/std/VariantReflection.inl @@ -32,7 +32,7 @@ namespace AZ * But as the AZStdAssociativeContainer instance will not be accessed outside of the module it was * created within then this will return this .dll/.exe module allocator */ - classElement.m_attributes.set_allocator(AZStdFunctorAllocator([]() -> IAllocatorAllocate& { return GetCurrentSerializeContextModule().GetAllocator(); })); + classElement.m_attributes.set_allocator(AZStdFunctorAllocator([]() -> IAllocator& { return GetCurrentSerializeContextModule().GetAllocator(); })); } template @@ -429,7 +429,7 @@ namespace AZ // the serialize context dll module allocator has to be used to manage the lifetime of the ClassData attributes within a module // If a module which reflects a variant is unloaded, then the dll module allocator will properly unreflect the variant type from the serialize context // for this particular module - AZStdFunctorAllocator dllAllocator([]() -> IAllocatorAllocate& { return GetCurrentSerializeContextModule().GetAllocator(); }); + AZStdFunctorAllocator dllAllocator([]() -> IAllocator& { return GetCurrentSerializeContextModule().GetAllocator(); }); m_classData.m_attributes.set_allocator(AZStd::move(dllAllocator)); // Create the ObjectStreamWriteOverrideCB in the current module diff --git a/Code/Framework/AzCore/AzCore/azcore_files.cmake b/Code/Framework/AzCore/AzCore/azcore_files.cmake index e8001f7215..b8ea9d3f39 100644 --- a/Code/Framework/AzCore/AzCore/azcore_files.cmake +++ b/Code/Framework/AzCore/AzCore/azcore_files.cmake @@ -362,8 +362,6 @@ set(FILES Memory/AllocatorBase.h Memory/AllocatorManager.cpp Memory/AllocatorManager.h - Memory/AllocatorOverrideShim.cpp - Memory/AllocatorOverrideShim.h Memory/AllocatorWrapper.h Memory/AllocatorScope.h Memory/BestFitExternalMapAllocator.cpp diff --git a/Code/Framework/AzCore/Tests/AZStd/Hashed.cpp b/Code/Framework/AzCore/Tests/AZStd/Hashed.cpp index c982868c4f..dd2597334d 100644 --- a/Code/Framework/AzCore/Tests/AZStd/Hashed.cpp +++ b/Code/Framework/AzCore/Tests/AZStd/Hashed.cpp @@ -1400,7 +1400,7 @@ namespace UnitTest { using ContainerType = ContainerTemplate, AZStd::equal_to, AZ::AZStdIAllocator>; - static ContainerType Create(std::initializer_list intList, AZ::IAllocatorAllocate* allocatorInstance) + static ContainerType Create(std::initializer_list intList, AZ::IAllocator* allocatorInstance) { ContainerType allocatorSet(intList, AZStd::hash{}, AZStd::equal_to{}, AZ::AZStdIAllocator{ allocatorInstance }); return allocatorSet; @@ -1798,7 +1798,7 @@ namespace UnitTest { using ContainerType = ContainerTemplate, AZStd::equal_to, AZ::AZStdIAllocator>; - static ContainerType Create(std::initializer_list intList, AZ::IAllocatorAllocate* allocatorInstance) + static ContainerType Create(std::initializer_list intList, AZ::IAllocator* allocatorInstance) { ContainerType allocatorMap(intList, AZStd::hash{}, AZStd::equal_to{}, AZ::AZStdIAllocator{ allocatorInstance }); return allocatorMap; diff --git a/Code/Framework/AzCore/Tests/AZStd/Ordered.cpp b/Code/Framework/AzCore/Tests/AZStd/Ordered.cpp index 26838eeb63..3f3bc86ed6 100644 --- a/Code/Framework/AzCore/Tests/AZStd/Ordered.cpp +++ b/Code/Framework/AzCore/Tests/AZStd/Ordered.cpp @@ -1082,7 +1082,7 @@ namespace UnitTest { using ContainerType = ContainerTemplate, AZ::AZStdIAllocator>; - static ContainerType Create(std::initializer_list intList, AZ::IAllocatorAllocate* allocatorInstance) + static ContainerType Create(std::initializer_list intList, AZ::IAllocator* allocatorInstance) { ContainerType allocatorSet(intList, AZStd::less{}, AZ::AZStdIAllocator{ allocatorInstance }); return allocatorSet; @@ -1503,7 +1503,7 @@ namespace UnitTest { using ContainerType = ContainerTemplate, AZ::AZStdIAllocator>; - static ContainerType Create(std::initializer_list intList, AZ::IAllocatorAllocate* allocatorInstance) + static ContainerType Create(std::initializer_list intList, AZ::IAllocator* allocatorInstance) { ContainerType allocatorMap(intList, AZStd::less{}, AZ::AZStdIAllocator{ allocatorInstance }); return allocatorMap; diff --git a/Code/Framework/AzCore/Tests/Memory.cpp b/Code/Framework/AzCore/Tests/Memory.cpp index 5287167c8b..0e0103d162 100644 --- a/Code/Framework/AzCore/Tests/Memory.cpp +++ b/Code/Framework/AzCore/Tests/Memory.cpp @@ -87,7 +87,7 @@ namespace UnitTest #endif void* addresses[numAllocations] = {nullptr}; - IAllocatorAllocate& sysAlloc = AllocatorInstance::Get(); + IAllocator& sysAllocator = AllocatorInstance::Get(); ////////////////////////////////////////////////////////////////////////// // Allocate @@ -96,19 +96,19 @@ namespace UnitTest { AZStd::size_t size = AZStd::GetMax(rand() % 256, 1); // supply all debug info, so we don't need to record the stack. - addresses[i] = sysAlloc.Allocate(size, 8, 0, "Test Alloc", __FILE__, __LINE__); + addresses[i] = sysAllocator.Allocate(size, 8, 0, "Test Alloc", __FILE__, __LINE__); memset(addresses[i], 1, size); totalAllocSize += size; } ////////////////////////////////////////////////////////////////////////// - EXPECT_GE(sysAlloc.NumAllocatedBytes(), totalAllocSize); + EXPECT_GE(sysAllocator.NumAllocatedBytes(), totalAllocSize); ////////////////////////////////////////////////////////////////////////// // Deallocate for (int i = numAllocations-1; i >=0; --i) { - sysAlloc.DeAllocate(addresses[i]); + sysAllocator.DeAllocate(addresses[i]); } ////////////////////////////////////////////////////////////////////////// } @@ -122,21 +122,21 @@ namespace UnitTest { AllocatorInstance::Create(); - IAllocatorAllocate& sysAlloc = AllocatorInstance::Get(); + IAllocator& sysAllocator = AllocatorInstance::Get(); for (int i = 0; i < 100; ++i) { - address[i] = sysAlloc.Allocate(1000, 32, 0); + address[i] = sysAllocator.Allocate(1000, 32, 0); EXPECT_NE(nullptr, address[i]); EXPECT_EQ(0, ((size_t)address[i] & 31)); // check alignment - EXPECT_GE(sysAlloc.AllocationSize(address[i]), 1000); // check allocation size + EXPECT_GE(sysAllocator.AllocationSize(address[i]), 1000); // check allocation size } - EXPECT_GE(sysAlloc.NumAllocatedBytes(), 100000); // we requested 100 * 1000 so we should have at least this much allocated + EXPECT_GE(sysAllocator.NumAllocatedBytes(), 100000); // we requested 100 * 1000 so we should have at least this much allocated for (int i = 0; i < 100; ++i) { - sysAlloc.DeAllocate(address[i]); + sysAllocator.DeAllocate(address[i]); } //////////////////////////////////////////////////////////////////////// @@ -168,18 +168,17 @@ namespace UnitTest SystemAllocator::Descriptor descriptor; descriptor.m_stackRecordLevels = 20; AllocatorInstance::Create(descriptor); - IAllocator& sysAllocator = AllocatorInstance::GetAllocator(); - IAllocatorAllocate& sysAlloc = *sysAllocator.GetAllocationSource(); + IAllocator& sysAllocator = AllocatorInstance::Get(); for (int i = 0; i < 100; ++i) { - address[i] = sysAlloc.Allocate(1000, 32, 0); + address[i] = sysAllocator.Allocate(1000, 32, 0); EXPECT_NE(nullptr, address[i]); EXPECT_EQ(0, ((size_t)address[i] & 31)); // check alignment - EXPECT_GE(sysAlloc.AllocationSize(address[i]), 1000); // check allocation size + EXPECT_GE(sysAllocator.AllocationSize(address[i]), 1000); // check allocation size } - EXPECT_TRUE(sysAlloc.NumAllocatedBytes() >= 100000); // we requested 100 * 1000 so we should have at least this much allocated + EXPECT_TRUE(sysAllocator.NumAllocatedBytes() >= 100000); // we requested 100 * 1000 so we should have at least this much allocated // If tracking and recording is enabled, we can verify that the alloc info is valid #if defined(AZ_DEBUG_BUILD) @@ -192,7 +191,7 @@ namespace UnitTest const Debug::AllocationInfo& ai = iter->second; EXPECT_EQ(32, ai.m_alignment); EXPECT_EQ(1000, ai.m_byteSize); - EXPECT_EQ(nullptr, ai.m_fileName); // We did not pass fileName or lineNum to sysAlloc.Allocate() + EXPECT_EQ(nullptr, ai.m_fileName); // We did not pass fileName or lineNum to sysAllocator.Allocate() EXPECT_EQ(0, ai.m_lineNum); // -- " -- # if defined(AZ_PLATFORM_WINDOWS) // if our hardware support stack traces make sure we have them, since we did not provide fileName,lineNum @@ -229,47 +228,47 @@ namespace UnitTest // Free all memory for (int i = 0; i < 100; ++i) { - sysAlloc.DeAllocate(address[i]); + sysAllocator.DeAllocate(address[i]); } - sysAlloc.GarbageCollect(); - EXPECT_LT(sysAlloc.NumAllocatedBytes(), 1024); // We freed everything from a memspace, we should have only a very minor chunk of data + sysAllocator.GarbageCollect(); + EXPECT_LT(sysAllocator.NumAllocatedBytes(), 1024); // We freed everything from a memspace, we should have only a very minor chunk of data ////////////////////////////////////////////////////////////////////////// // realloc test address[0] = nullptr; static const unsigned int checkValue = 0x0badbabe; // create tree (non pool) allocation (we usually pool < 256 bytes) - address[0] = sysAlloc.Allocate(2048, 16); + address[0] = sysAllocator.Allocate(2048, 16); *(unsigned*)(address[0]) = checkValue; // set check value - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 2048, 16); - address[0] = sysAlloc.ReAllocate(address[0], 1024, 16); // test tree big -> tree small + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 2048, 16); + address[0] = sysAllocator.ReAllocate(address[0], 1024, 16); // test tree big -> tree small EXPECT_EQ(checkValue, *(unsigned*)address[0]); - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 1024, 16); - address[0] = sysAlloc.ReAllocate(address[0], 4096, 16); // test tree small -> tree big - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 4096, 16); + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 1024, 16); + address[0] = sysAllocator.ReAllocate(address[0], 4096, 16); // test tree small -> tree big + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 4096, 16); EXPECT_EQ(checkValue, *(unsigned*)address[0]); - address[0] = sysAlloc.ReAllocate(address[0], 128, 16); // test tree -> pool, - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 128, 16); + address[0] = sysAllocator.ReAllocate(address[0], 128, 16); // test tree -> pool, + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 128, 16); EXPECT_EQ(checkValue, *(unsigned*)address[0]); - address[0] = sysAlloc.ReAllocate(address[0], 64, 16); // pool big -> pool small - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 64, 16); + address[0] = sysAllocator.ReAllocate(address[0], 64, 16); // pool big -> pool small + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 64, 16); EXPECT_EQ(checkValue, *(unsigned*)address[0]); - address[0] = sysAlloc.ReAllocate(address[0], 64, 16); // pool sanity check - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 64, 16); + address[0] = sysAllocator.ReAllocate(address[0], 64, 16); // pool sanity check + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 64, 16); EXPECT_EQ(checkValue, *(unsigned*)address[0]); - address[0] = sysAlloc.ReAllocate(address[0], 192, 16); // pool small -> pool big - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 192, 16); + address[0] = sysAllocator.ReAllocate(address[0], 192, 16); // pool small -> pool big + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 192, 16); EXPECT_EQ(checkValue, *(unsigned*)address[0]); - address[0] = sysAlloc.ReAllocate(address[0], 2048, 16); // pool -> tree - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 2048, 16); + address[0] = sysAllocator.ReAllocate(address[0], 2048, 16); // pool -> tree + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 2048, 16); ; EXPECT_EQ(checkValue, *(unsigned*)address[0]); - address[0] = sysAlloc.ReAllocate(address[0], 2048, 16); // tree sanity check - AZ_TEST_ASSERT_CLOSE(sysAlloc.AllocationSize(address[0]), 2048, 16); + address[0] = sysAllocator.ReAllocate(address[0], 2048, 16); // tree sanity check + AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 2048, 16); ; EXPECT_EQ(checkValue, *(unsigned*)address[0]); - sysAlloc.DeAllocate(address[0], 2048, 16); + sysAllocator.DeAllocate(address[0], 2048, 16); // TODO realloc with different alignment tests ////////////////////////////////////////////////////////////////////////// @@ -340,8 +339,7 @@ namespace UnitTest void run() { - IAllocatorAllocate& poolAlloc = AllocatorInstance::Get(); - IAllocator& poolAllocator = AllocatorInstance::GetAllocator(); + IAllocator& poolAllocator = AllocatorInstance::Get(); // 64 should be the max number of different pool sizes we can allocate. void* address[64]; ////////////////////////////////////////////////////////////////////////// @@ -352,12 +350,12 @@ namespace UnitTest int i = 0; for (int size = 8; size <= 256; ++i, size += 8) { - address[i] = poolAlloc.Allocate(size, 8); - EXPECT_GE(poolAlloc.AllocationSize(address[i]), (AZStd::size_t)size); + address[i] = poolAllocator.Allocate(size, 8); + EXPECT_GE(poolAllocator.AllocationSize(address[i]), (AZStd::size_t)size); memset(address[i], 1, size); } - EXPECT_GE(poolAlloc.NumAllocatedBytes(), 4126); + EXPECT_GE(poolAllocator.NumAllocatedBytes(), 4126); if (poolAllocator.GetRecords()) { @@ -369,11 +367,11 @@ namespace UnitTest for (i = 0; address[i] != nullptr; ++i) { - poolAlloc.DeAllocate(address[i]); + poolAllocator.DeAllocate(address[i]); } ////////////////////////////////////////////////////////////////////////// - EXPECT_EQ(0, poolAlloc.NumAllocatedBytes()); + EXPECT_EQ(0, poolAllocator.NumAllocatedBytes()); if (poolAllocator.GetRecords()) { @@ -393,13 +391,13 @@ namespace UnitTest memset(address, 0, AZ_ARRAY_SIZE(address)*sizeof(void*)); for (unsigned int j = 0; j < AZ_ARRAY_SIZE(address); ++j) { - address[j] = poolAlloc.Allocate(256, 8, 0, "Pool Alloc", "This File", 123); - EXPECT_GE(poolAlloc.AllocationSize(address[j]), 256); + address[j] = poolAllocator.Allocate(256, 8, 0, "Pool Alloc", "This File", 123); + EXPECT_GE(poolAllocator.AllocationSize(address[j]), 256); memset(address[j], 1, 256); } // AllocatorManager::Instance().ResetMemoryBreak(0); - EXPECT_GE(poolAlloc.NumAllocatedBytes(), AZ_ARRAY_SIZE(address)*256); + EXPECT_GE(poolAllocator.NumAllocatedBytes(), AZ_ARRAY_SIZE(address)*256); if (poolAllocator.GetRecords()) { @@ -411,11 +409,11 @@ namespace UnitTest for (unsigned int j = 0; j < AZ_ARRAY_SIZE(address); ++j) { - poolAlloc.DeAllocate(address[j]); + poolAllocator.DeAllocate(address[j]); } ////////////////////////////////////////////////////////////////////////// - EXPECT_EQ(0, poolAlloc.NumAllocatedBytes()); + EXPECT_EQ(0, poolAllocator.NumAllocatedBytes()); if (poolAllocator.GetRecords()) { @@ -541,14 +539,14 @@ namespace UnitTest #endif void* addresses[numAllocations] = {nullptr}; - IAllocatorAllocate& poolAlloc = AllocatorInstance::Get(); + IAllocator& poolAllocator = AllocatorInstance::Get(); ////////////////////////////////////////////////////////////////////////// // Allocate for (int i = 0; i < numAllocations; ++i) { AZStd::size_t size = AZStd::GetMax(1, ((i + 1) * 2) % 256); - addresses[i] = poolAlloc.Allocate(size, 8, 0, "Test Alloc", __FILE__, __LINE__); + addresses[i] = poolAllocator.Allocate(size, 8, 0, "Test Alloc", __FILE__, __LINE__); EXPECT_NE(addresses[i], nullptr); memset(addresses[i], 1, size); } @@ -558,7 +556,7 @@ namespace UnitTest // Deallocate for (int i = numAllocations-1; i >=0; --i) { - poolAlloc.DeAllocate(addresses[i]); + poolAllocator.DeAllocate(addresses[i]); } ////////////////////////////////////////////////////////////////////////// } @@ -568,12 +566,12 @@ namespace UnitTest */ void SharedAlloc() { - IAllocatorAllocate& poolAlloc = AllocatorInstance::Get(); + IAllocator& poolAllocator = AllocatorInstance::Get(); for (int i = 0; i < m_numSharedAlloc; ++i) { AZStd::size_t minSize = sizeof(AllocClass); AZStd::size_t size = AZStd::GetMax((AZStd::size_t)(rand() % 256), minSize); - AllocClass* ac = reinterpret_cast(poolAlloc.Allocate(size, AZStd::alignment_of::value, 0, "Shared Alloc", __FILE__, __LINE__)); + AllocClass* ac = reinterpret_cast(poolAllocator.Allocate(size, AZStd::alignment_of::value, 0, "Shared Alloc", __FILE__, __LINE__)); AZStd::lock_guard lock(m_mutex); m_sharedAlloc.push_back(*ac); } @@ -584,7 +582,7 @@ namespace UnitTest */ void SharedDeAlloc() { - IAllocatorAllocate& poolAlloc = AllocatorInstance::Get(); + IAllocator& poolAllocator = AllocatorInstance::Get(); AllocClass* ac; int isDone = 0; while (isDone!=2) @@ -594,7 +592,7 @@ namespace UnitTest { ac = &m_sharedAlloc.front(); m_sharedAlloc.pop_front(); - poolAlloc.DeAllocate(ac); + poolAllocator.DeAllocate(ac); } if (m_doneSharedAlloc) // once we know we don't add more elements, make one last check and exit. @@ -633,8 +631,7 @@ namespace UnitTest void run() { - IAllocatorAllocate& poolAlloc = AllocatorInstance::Get(); - IAllocator& poolAllocator = AllocatorInstance::GetAllocator(); + IAllocator& poolAllocator = AllocatorInstance::Get(); // 64 should be the max number of different pool sizes we can allocate. void* address[64]; ////////////////////////////////////////////////////////////////////////// @@ -645,12 +642,12 @@ namespace UnitTest int j = 0; for (int size = 8; size <= 256; ++j, size += 8) { - address[j] = poolAlloc.Allocate(size, 8); - EXPECT_GE(poolAlloc.AllocationSize(address[j]), (AZStd::size_t)size); + address[j] = poolAllocator.Allocate(size, 8); + EXPECT_GE(poolAllocator.AllocationSize(address[j]), (AZStd::size_t)size); memset(address[j], 1, size); } - EXPECT_GE(poolAlloc.NumAllocatedBytes(), 4126); + EXPECT_GE(poolAllocator.NumAllocatedBytes(), 4126); if (poolAllocator.GetRecords()) { @@ -662,11 +659,11 @@ namespace UnitTest for (int i = 0; address[i] != nullptr; ++i) { - poolAlloc.DeAllocate(address[i]); + poolAllocator.DeAllocate(address[i]); } ////////////////////////////////////////////////////////////////////////// - EXPECT_EQ(0, poolAlloc.NumAllocatedBytes()); + EXPECT_EQ(0, poolAllocator.NumAllocatedBytes()); if (poolAllocator.GetRecords()) { @@ -681,12 +678,12 @@ namespace UnitTest memset(address, 0, AZ_ARRAY_SIZE(address)*sizeof(void*)); for (unsigned int i = 0; i < AZ_ARRAY_SIZE(address); ++i) { - address[i] = poolAlloc.Allocate(256, 8); - EXPECT_GE(poolAlloc.AllocationSize(address[i]), 256); + address[i] = poolAllocator.Allocate(256, 8); + EXPECT_GE(poolAllocator.AllocationSize(address[i]), 256); memset(address[i], 1, 256); } - EXPECT_GE(poolAlloc.NumAllocatedBytes(), AZ_ARRAY_SIZE(address)*256); + EXPECT_GE(poolAllocator.NumAllocatedBytes(), AZ_ARRAY_SIZE(address)*256); if (poolAllocator.GetRecords()) { @@ -698,11 +695,11 @@ namespace UnitTest for (unsigned int i = 0; i < AZ_ARRAY_SIZE(address); ++i) { - poolAlloc.DeAllocate(address[i]); + poolAllocator.DeAllocate(address[i]); } ////////////////////////////////////////////////////////////////////////// - EXPECT_EQ(0, poolAlloc.NumAllocatedBytes()); + EXPECT_EQ(0, poolAllocator.NumAllocatedBytes()); if (poolAllocator.GetRecords()) { @@ -820,7 +817,7 @@ namespace UnitTest desc.m_memoryBlock = azmalloc(desc.m_memoryBlockByteSize, desc.m_memoryBlockAlignment); AllocatorInstance::Create(desc); - IAllocatorAllocate& bfAlloc = AllocatorInstance::Get(); + IAllocator& bfAlloc = AllocatorInstance::Get(); EXPECT_EQ( desc.m_memoryBlockByteSize, bfAlloc.Capacity() ); EXPECT_EQ( 0, bfAlloc.NumAllocatedBytes() ); @@ -882,8 +879,8 @@ namespace UnitTest void run() { - IAllocator& sysAllocator = AllocatorInstance::GetAllocator(); - IAllocator& poolAllocator = AllocatorInstance::GetAllocator(); + IAllocator& sysAllocator = AllocatorInstance::Get(); + IAllocator& poolAllocator = AllocatorInstance::Get(); void* ptr = azmalloc(16*1024, 32, SystemAllocator); EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment @@ -1010,27 +1007,27 @@ namespace UnitTest void run() { - IAllocator& sysAlloc = AllocatorInstance::GetAllocator(); - IAllocator& poolAlloc = AllocatorInstance::GetAllocator(); + IAllocator& sysAllocator = AllocatorInstance::Get(); + IAllocator& poolAllocator = AllocatorInstance::Get(); MyClass* ptr = aznew MyClass(202); /// this should allocate memory from the pool allocator EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment EXPECT_EQ(202, ptr->m_data); // check value - if (poolAlloc.GetRecords()) + if (poolAllocator.GetRecords()) { - AZStd::lock_guard lock(*poolAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = poolAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*poolAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap(); Debug::AllocationRecordsType::const_iterator iter = records.find(ptr); EXPECT_TRUE(iter!=records.end()); // our allocation is in the list EXPECT_STREQ(iter->second.m_name, "MyClass"); } delete ptr; - if (poolAlloc.GetRecords()) + if (poolAllocator.GetRecords()) { - AZStd::lock_guard lock(*poolAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = poolAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*poolAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap(); EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list } @@ -1038,19 +1035,19 @@ namespace UnitTest ptr = azcreate(MyClass, (101), SystemAllocator); EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment EXPECT_EQ(101, ptr->m_data); // check value - if (sysAlloc.GetRecords()) + if (sysAllocator.GetRecords()) { - AZStd::lock_guard lock(*sysAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = sysAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*sysAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap(); Debug::AllocationRecordsType::const_iterator iter = records.find(ptr); EXPECT_TRUE(iter!=records.end()); // our allocation is in the list EXPECT_STREQ(iter->second.m_name, "MyClass"); } azdestroy(ptr, SystemAllocator); - if (sysAlloc.GetRecords()) + if (sysAllocator.GetRecords()) { - AZStd::lock_guard lock(*sysAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = sysAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*sysAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap(); EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list } @@ -1059,19 +1056,19 @@ namespace UnitTest ptr = azcreate(MyClass, (505), SystemAllocator, "MyClassNamed"); EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment EXPECT_EQ(505, ptr->m_data); // check value - if (sysAlloc.GetRecords()) + if (sysAllocator.GetRecords()) { - AZStd::lock_guard lock(*sysAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = sysAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*sysAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap(); Debug::AllocationRecordsType::const_iterator iter = records.find(ptr); EXPECT_TRUE(iter!=records.end()); // our allocation is in the list EXPECT_STREQ(iter->second.m_name, "MyClassNamed"); } azdestroy(ptr); // imply SystemAllocator - if (sysAlloc.GetRecords()) + if (sysAllocator.GetRecords()) { - AZStd::lock_guard lock(*sysAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = sysAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*sysAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap(); EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list } @@ -1080,20 +1077,20 @@ namespace UnitTest EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment EXPECT_EQ(303, ptr->m_data); // check value - if (poolAlloc.GetRecords()) + if (poolAllocator.GetRecords()) { - AZStd::lock_guard lock(*poolAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = poolAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*poolAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap(); Debug::AllocationRecordsType::const_iterator iter = records.find(ptr); EXPECT_TRUE(iter != records.end()); // our allocation is in the list EXPECT_STREQ(iter->second.m_name, "MyClass"); } delete ptr; - if (poolAlloc.GetRecords()) + if (poolAllocator.GetRecords()) { - AZStd::lock_guard lock(*poolAlloc.GetRecords()); - const Debug::AllocationRecordsType& records = poolAlloc.GetRecords()->GetMap(); + AZStd::lock_guard lock(*poolAllocator.GetRecords()); + const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap(); EXPECT_TRUE(records.find(ptr) == records.end()); // our allocation is NOT in the list } } @@ -1140,8 +1137,8 @@ namespace UnitTest return (size_t)1 << (size_t)(MAX_ALIGNMENT_LOG2 * r); } - class DebugSysAlloc - : public AZ::IAllocatorAllocate + class DebugSysAllocSchema + : public AZ::IAllocatorSchema { pointer_type Allocate(size_type byteSize, size_type alignment, int flags, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override { @@ -1176,8 +1173,6 @@ namespace UnitTest size_type GetMaxAllocationSize() const override { return 1 * 1024 * 1024 * 1024; } /// Returns max allocation size of a single contiguous allocation size_type GetMaxContiguousAllocationSize() const override { return 1 * 1024 * 1024 * 1024; } - /// Returns a pointer to a sub-allocator or NULL. - IAllocatorAllocate* GetSubAllocator() override { return NULL; } }; public: void SetUp() override @@ -2565,7 +2560,7 @@ namespace UnitTest printf("\n\t\t\t=======================\n"); printf("\t\t\tSchemas Benchmark Test!\n"); printf("\t\t\t=======================\n"); - DebugSysAlloc da; + DebugSysAllocSchema da; { HphaSchema::Descriptor hphaDesc; hphaDesc.m_fixedMemoryBlockByteSize = AZ_TRAIT_OS_HPHA_MEMORYBLOCKBYTESIZE; diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index 5cf5176308..7870749d24 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -112,7 +112,6 @@ namespace Benchmark { } - // IAllocatorAllocate static void* Allocate(size_t byteSize, size_t) { s_numAllocatedBytes += byteSize; @@ -580,6 +579,7 @@ namespace Benchmark //BM_REGISTER_ALLOCATOR(BestFitExternalMapAllocator, BestFitExternalMapAllocator); // Requires to pre-allocate blocks and cannot work as a general-purpose allocator //BM_REGISTER_ALLOCATOR(HeapSchemaAllocator, TestHeapSchemaAllocator); // Requires to pre-allocate blocks and cannot work as a general-purpose allocator //BM_REGISTER_SCHEMA(PoolSchema); // Requires special alignment requests while allocating + // BM_REGISTER_ALLOCATOR(OSAllocator, OSAllocator); // Requires special treatment to initialize since it will be already initialized, maybe creating a different instance? #undef BM_REGISTER_ALLOCATOR #undef BM_REGISTER_SIZE_FIXTURES diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorManager.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorManager.cpp index dc8ac033c1..bc758c1544 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorManager.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorManager.cpp @@ -7,7 +7,6 @@ */ #include -#include #include #include @@ -47,9 +46,6 @@ namespace UnitTest void RunTests() { - TestAllocatorShimWithDeallocateAfter(); - TestAllocatorShimRemovedAfterFinalization(); - TestAllocatorShimUsedForRealloc(); TearDownAllocatorManagerTest(); } @@ -64,7 +60,7 @@ namespace UnitTest EXPECT_EQ(m_manager->GetNumAllocators(), 0); AllocatorInstance::Create(); EXPECT_EQ(m_manager->GetNumAllocators(), 2); // SystemAllocator creates the OSAllocator if it doesn't exist - m_systemAllocator = &AllocatorInstance::GetAllocator(); + m_systemAllocator = &AllocatorInstance::Get(); } void TearDownAllocatorManagerTest() @@ -83,85 +79,6 @@ namespace UnitTest m_systemAllocator = nullptr; } - void TestAllocatorShimWithDeallocateAfter() - { - SetUpAllocatorManagerTest(); - - // Should begin with a shim installed. If this fails, check that AZCORE_MEMORY_ENABLE_OVERRIDES is enabled in AllocatorManager.cpp. - EXPECT_NE(m_systemAllocator->GetAllocationSource(), m_systemAllocator->GetOriginalAllocationSource()); - - const int testAllocBytes = TEST_ALLOC_BYTES; - - // Allocate from the shim, which should take from the allocator's original source - void* p = m_systemAllocator->GetAllocationSource()->Allocate(testAllocBytes, 0, 0); - EXPECT_NE(p, nullptr); - EXPECT_EQ(m_systemAllocator->GetOriginalAllocationSource()->NumAllocatedBytes(), testAllocBytes); - - // Add the override schema - m_manager->SetOverrideAllocatorSource(&m_mallocSchema); - - // Allocations should go through malloc schema instead of the allocator's regular schema - void* q = m_systemAllocator->GetAllocationSource()->Allocate(testAllocBytes, 0, 0); - EXPECT_NE(q, nullptr); - EXPECT_EQ(m_mallocSchema.NumAllocatedBytes(), testAllocBytes); - EXPECT_EQ(m_systemAllocator->GetOriginalAllocationSource()->NumAllocatedBytes(), testAllocBytes); - - // Finalize configuration, no more shims should be created after this point - m_manager->FinalizeConfiguration(); - - // Deallocating the original orphaned allocation from the SystemAllocator should remove the shim - EXPECT_NE(m_systemAllocator->GetAllocationSource(), &m_mallocSchema); - m_systemAllocator->GetAllocationSource()->DeAllocate(p); - EXPECT_EQ(m_systemAllocator->GetAllocationSource(), &m_mallocSchema); - - // Clean up - m_systemAllocator->GetAllocationSource()->DeAllocate(q); - } - - void TestAllocatorShimRemovedAfterFinalization() - { - SetUpAllocatorManagerTest(); - - // Should begin with a shim installed - EXPECT_NE(m_systemAllocator->GetAllocationSource(), m_systemAllocator->GetOriginalAllocationSource()); - - // Finalizing the configuration should remove the shim if it was unused - m_manager->FinalizeConfiguration(); - EXPECT_EQ(m_systemAllocator->GetAllocationSource(), m_systemAllocator->GetOriginalAllocationSource()); - } - - void TestAllocatorShimUsedForRealloc() - { - SetUpAllocatorManagerTest(); - - // Should begin with a shim installed - EXPECT_NE(m_systemAllocator->GetAllocationSource(), m_systemAllocator->GetOriginalAllocationSource()); - - const int testAllocBytes = TEST_ALLOC_BYTES; - - // Allocate from the shim, which should take from the allocator's original source - void* p = m_systemAllocator->GetAllocationSource()->Allocate(testAllocBytes, 0, 0); - EXPECT_NE(p, nullptr); - EXPECT_EQ(m_systemAllocator->GetOriginalAllocationSource()->NumAllocatedBytes(), testAllocBytes); - - // Add the override schema and finalize - m_manager->SetOverrideAllocatorSource(&m_mallocSchema); - m_manager->FinalizeConfiguration(); - - // Shim should still be present - EXPECT_NE(m_systemAllocator->GetAllocationSource(), &m_mallocSchema); - - // Reallocation should move allocation from the old source to the new source - EXPECT_EQ(m_mallocSchema.NumAllocatedBytes(), 0); - void* q = m_systemAllocator->GetAllocationSource()->ReAllocate(p, testAllocBytes * 2, 0); - EXPECT_NE(p, q); - EXPECT_EQ(m_mallocSchema.NumAllocatedBytes(), testAllocBytes * 2); - EXPECT_EQ(m_systemAllocator->GetOriginalAllocationSource()->NumAllocatedBytes(), 0); - - // Reallocation should also have removed the shim as it was no longer necessary - EXPECT_EQ(m_systemAllocator->GetAllocationSource(), &m_mallocSchema); - } - MallocSchema m_mallocSchema; AllocatorManager* m_manager = nullptr; IAllocator* m_systemAllocator = nullptr; diff --git a/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp b/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp index 04802a8d0e..c2fba5c5a3 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp +++ b/Code/Framework/AzFramework/AzFramework/Archive/Archive.cpp @@ -1975,7 +1975,7 @@ namespace AZ::IO AZ_Error("Archive", false, "OSAllocator is not ready. It cannot be used to allocate a MemoryBlock"); return {}; } - AZ::IAllocatorAllocate* allocator = &AZ::AllocatorInstance::Get(); + AZ::IAllocator* allocator = &AZ::AllocatorInstance::Get(); AZStd::intrusive_ptr memoryBlock{ new (allocator->Allocate(sizeof(AZ::IO::MemoryBlock), alignof(AZ::IO::MemoryBlock))) AZ::IO::MemoryBlock{AZ::IO::MemoryBlockDeleter{ &AZ::AllocatorInstance::Get() }} }; auto CreateFunc = [](size_t byteSize, size_t byteAlignment, const char* name) { diff --git a/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h b/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h index d7eaee6e24..0bbc5aaaab 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h +++ b/Code/Framework/AzFramework/AzFramework/Archive/IArchive.h @@ -36,7 +36,7 @@ namespace AZ::IO struct MemoryBlockDeleter { void operator()(const AZStd::intrusive_refcount* ptr) const; - AZ::IAllocatorAllocate* m_allocator{}; + AZ::IAllocator* m_allocator{}; }; struct MemoryBlock : AZStd::intrusive_refcount diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.cpp b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.cpp index 13d5b0f723..05c773f056 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.cpp +++ b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.cpp @@ -42,7 +42,7 @@ namespace AZ::IO::ZipDir AZ_Error("Archive", false, "OSAllocator is not ready. It cannot be used to allocate a MemoryBlock"); return {}; } - AZ::IAllocatorAllocate* allocator = &AZ::AllocatorInstance::Get(); + AZ::IAllocator* allocator = &AZ::AllocatorInstance::Get(); AZStd::intrusive_ptr memoryBlock{ new (allocator->Allocate(sizeof(AZ::IO::MemoryBlock), alignof(AZ::IO::MemoryBlock))) AZ::IO::MemoryBlock{AZ::IO::MemoryBlockDeleter{ &AZ::AllocatorInstance::Get() }} }; auto CreateFunc = [](size_t byteSize, size_t byteAlignment, const char* name) { @@ -142,14 +142,14 @@ namespace AZ::IO::ZipDir { } - Cache::Cache(AZ::IAllocatorAllocate* allocator) + Cache::Cache(AZ::IAllocator* allocator) : m_fileHandle(AZ::IO::InvalidHandle) , m_nFlags(0) , m_lCDROffset(0) , m_encryptedHeaders(ZipFile::HEADERS_NOT_ENCRYPTED) , m_allocator{ allocator } { - AZ_Assert(allocator, "IAllocatorAllocate object is required in order to allocated memory for the ZipDir Cache operations"); + AZ_Assert(allocator, "IAllocator object is required in order to allocated memory for the ZipDir Cache operations"); } void Cache::Close() diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.h b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.h index ae1e3dfa9c..c8b19ebf33 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.h +++ b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirCache.h @@ -40,7 +40,7 @@ namespace AZ::IO::ZipDir inline static constexpr int compressedBlockHeaderSizeInBytes = 4; //number of bytes we need in front of the compressed block to indicate which compressor was used Cache(); - explicit Cache(AZ::IAllocatorAllocate* allocator); + explicit Cache(AZ::IAllocator* allocator); ~Cache() { @@ -133,7 +133,7 @@ namespace AZ::IO::ZipDir friend class FileEntryTransactionAdd; FileEntryTree m_treeDir; AZ::IO::HandleType m_fileHandle; - AZ::IAllocatorAllocate* m_allocator; + AZ::IAllocator* m_allocator; AZ::IO::Path m_strFilePath; // String Pool for persistently storing paths as long as they reside in the cache diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.cpp b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.cpp index ab9c356d7f..9ad58443a0 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.cpp +++ b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.cpp @@ -39,7 +39,7 @@ namespace AZ::IO::ZipDir { } - auto FileDataRecord::New(const FileRecord& rThat, AZ::IAllocatorAllocate* allocator) -> AZStd::intrusive_ptr + auto FileDataRecord::New(const FileRecord& rThat, AZ::IAllocator* allocator) -> AZStd::intrusive_ptr { auto fileDataRecordAlloc = reinterpret_cast(allocator->Allocate( sizeof(FileDataRecord) + rThat.pFileEntryBase->desc.lSizeCompressed, diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.h b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.h index 1183dbf384..2a4df44f2d 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.h +++ b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirList.h @@ -28,7 +28,7 @@ namespace AZ::IO::ZipDir { void operator()(const AZStd::intrusive_refcount* ptr) const; - AZ::IAllocatorAllocate* m_allocator{}; + AZ::IAllocator* m_allocator{}; }; struct FileDataRecord : public FileRecord @@ -36,7 +36,7 @@ namespace AZ::IO::ZipDir { FileDataRecord(); - static auto New(const FileRecord& rThat, AZ::IAllocatorAllocate* allocator) ->AZStd::intrusive_ptr; + static auto New(const FileRecord& rThat, AZ::IAllocator* allocator) ->AZStd::intrusive_ptr; void* GetData() {return this + 1; } }; diff --git a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirStructures.cpp b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirStructures.cpp index cea517decd..1ac0906c94 100644 --- a/Code/Framework/AzFramework/AzFramework/Archive/ZipDirStructures.cpp +++ b/Code/Framework/AzFramework/AzFramework/Archive/ZipDirStructures.cpp @@ -25,13 +25,13 @@ namespace AZ::IO::ZipDir::ZipDirStructuresInternal { static void* ZlibAlloc(void* userData, uint32_t item, uint32_t size) { - auto allocator = reinterpret_cast(userData); + auto allocator = reinterpret_cast(userData); return allocator->Allocate(item * size, alignof(uint8_t), 0, "ZLibAlloc"); } static void ZlibFree(void* userData, void* ptr) { - auto allocator = reinterpret_cast(userData); + auto allocator = reinterpret_cast(userData); allocator->DeAllocate(ptr); } @@ -296,7 +296,7 @@ namespace AZ::IO::ZipDir::ZipDirStructuresInternal return {}; } - AZ::IAllocatorAllocate* allocator = &AZ::AllocatorInstance::Get(); + AZ::IAllocator* allocator = &AZ::AllocatorInstance::Get(); AZStd::intrusive_ptr memoryBlock{ new (allocator->Allocate(sizeof(AZ::IO::MemoryBlock), alignof(AZ::IO::MemoryBlock))) AZ::IO::MemoryBlock{AZ::IO::MemoryBlockDeleter{ &AZ::AllocatorInstance::Get() }} }; auto CreateFunc = [](size_t byteSize, size_t byteAlignment, const char* name) { @@ -451,7 +451,7 @@ namespace AZ::IO::ZipDir if (GetDiskFreeSpaceA(drive.c_str(),nullptr, &bytesPerSector, nullptr, nullptr)) { m_nSectorSize = bytesPerSector; - AZ::IAllocatorAllocate& allocator = AZ::AllocatorInstance::Get(); + AZ::IAllocator& allocator = AZ::AllocatorInstance::Get(); if (m_pReadTarget) { allocator.DeAllocate(m_pReadTarget); diff --git a/Code/LauncherUnified/Launcher.h b/Code/LauncherUnified/Launcher.h index 7c4c09c19f..eb1cf6479e 100644 --- a/Code/LauncherUnified/Launcher.h +++ b/Code/LauncherUnified/Launcher.h @@ -62,7 +62,7 @@ namespace O3DELauncher ResourceLimitUpdater m_updateResourceLimits = nullptr; //!< callback for updating system resources, if necessary OnPostApplicationStart m_onPostAppStart = nullptr; //!< callback notifying the platform specific entry point that AzGameFramework::GameApplication::Start has been called - AZ::IAllocatorAllocate* m_allocator = nullptr; //!< Used to allocate the temporary bootstrap memory, as well as the main \ref SystemAllocator heap. If null, OSAllocator will be used + AZ::IAllocator* m_allocator = nullptr; //!< Used to allocate the temporary bootstrap memory, as well as the main \ref SystemAllocator heap. If null, OSAllocator will be used const char* m_appResourcesPath = "."; //!< Path to the device specific assets, default is equivalent to blank path in ParseEngineConfig const char* m_appWriteStoragePath = nullptr; //!< Path to writeable storage if different than assets path, used to override userPath and logPath diff --git a/Code/Legacy/CryCommon/CryLegacyAllocator.h b/Code/Legacy/CryCommon/CryLegacyAllocator.h index b0e1e29657..e96c2a9347 100644 --- a/Code/Legacy/CryCommon/CryLegacyAllocator.h +++ b/Code/Legacy/CryCommon/CryLegacyAllocator.h @@ -23,19 +23,9 @@ inline void* CryModuleMallocImpl(size_t size, const char* file, const int line) #define CryModuleFree(ptr) CryModuleFreeImpl(ptr, __FILE__, __LINE__) #define CryModuleMemalignFree(ptr) CryModuleFreeImpl(ptr, __FILE__, __LINE__) -inline void CryModuleFreeImpl(void* ptr, const char* file, const int line) +inline void CryModuleFreeImpl(void* ptr, const char*, const int) { - - AZ::IAllocator& allocator = AZ::AllocatorInstance::GetAllocator(); - - if (allocator.IsAllocationSourceChanged()) - { - allocator.GetAllocationSource()->DeAllocate(ptr); - } - else - { - static_cast(allocator).DeAllocate(ptr, file, line); - } + AZ::AllocatorInstance::Get().DeAllocate(ptr, 0, 0); } #define CryModuleMemalign(size, alignment) CryModuleMemalignImpl(size, alignment, __FILE__, __LINE__) @@ -79,17 +69,5 @@ inline void* CryModuleReallocAlignImpl(void* prev, size_t size, size_t alignment } #endif - AZ::IAllocator& allocator = AZ::AllocatorInstance::GetAllocator(); - void *ptr; - - if (allocator.IsAllocationSourceChanged()) - { - ptr = allocator.GetAllocationSource()->ReAllocate(prev, size, 0); - } - else - { - ptr = static_cast(allocator).ReAllocate(prev, size, 0, file, line); - } - - return ptr; + return AZ::AllocatorInstance::Get().ReAllocate(prev, size, 0); } diff --git a/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacket.h b/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacket.h index d66a3ea1bb..7995002a39 100644 --- a/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacket.h +++ b/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacket.h @@ -13,7 +13,7 @@ namespace AZ { - class IAllocatorAllocate; + class IAllocator; namespace RHI { @@ -66,7 +66,7 @@ namespace AZ DrawPacket() = default; // The allocator used to release the memory when Release() is called. - IAllocatorAllocate* m_allocator = nullptr; + IAllocator* m_allocator = nullptr; // The bit-mask of all active filter tags. DrawListMask m_drawListMask = 0; diff --git a/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacketBuilder.h b/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacketBuilder.h index 021125312b..30e013d486 100644 --- a/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacketBuilder.h +++ b/Gems/Atom/RHI/Code/Include/Atom/RHI/DrawPacketBuilder.h @@ -14,7 +14,7 @@ namespace AZ { - class IAllocatorAllocate; + class IAllocator; namespace RHI { @@ -50,7 +50,7 @@ namespace AZ // NOTE: This is configurable; just used to control the amount of memory held by the builder. static const size_t DrawItemCountMax = 16; - void Begin(IAllocatorAllocate* allocator); + void Begin(IAllocator* allocator); void SetDrawArguments(const DrawArguments& drawArguments); @@ -77,7 +77,7 @@ namespace AZ private: void ClearData(); - IAllocatorAllocate* m_allocator = nullptr; + IAllocator* m_allocator = nullptr; DrawArguments m_drawArguments; DrawListMask m_drawListMask = 0; DrawFilterMask m_drawFilterMask = DrawFilterMaskDefaultValue; diff --git a/Gems/Atom/RHI/Code/Source/RHI/DrawPacketBuilder.cpp b/Gems/Atom/RHI/Code/Source/RHI/DrawPacketBuilder.cpp index ebda9732df..ec9dd1a14f 100644 --- a/Gems/Atom/RHI/Code/Source/RHI/DrawPacketBuilder.cpp +++ b/Gems/Atom/RHI/Code/Source/RHI/DrawPacketBuilder.cpp @@ -18,7 +18,7 @@ namespace AZ { namespace RHI { - void DrawPacketBuilder::Begin(IAllocatorAllocate* allocator) + void DrawPacketBuilder::Begin(IAllocator* allocator) { m_allocator = allocator ? allocator : &AllocatorInstance::Get(); } From d108fe4804071e1f22612e897021d6653f6bea53 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Thu, 16 Dec 2021 18:31:19 -0800 Subject: [PATCH 17/20] Addresses PR comments Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/AzCore/Memory/AllocatorBase.cpp | 345 +++++++++--------- .../AzCore/Memory/BestFitExternalMapSchema.h | 2 +- .../AzCore/AzCore/Memory/IAllocator.h | 4 +- Code/Framework/AzCore/AzCore/Memory/Memory.h | 2 +- .../Tests/Memory/AllocatorBenchmarks.cpp | 4 +- 5 files changed, 181 insertions(+), 176 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp index 4da9ab384f..e877739e29 100644 --- a/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/AllocatorBase.cpp @@ -9,11 +9,10 @@ #include #include -using namespace AZ; +// Only used to create recordings of memory operations to use for memory benchmarks +#define O3DE_RECORDING_ENABLED 0 -#define RECORDING_ENABLED 0 - -#if RECORDING_ENABLED +#if O3DE_RECORDING_ENABLED #include #include @@ -25,10 +24,10 @@ namespace class DebugAllocator { public: - typedef void* pointer_type; - typedef AZStd::size_t size_type; - typedef AZStd::ptrdiff_t difference_type; - typedef AZStd::false_type allow_memory_leaks; ///< Regular allocators should not leak. + using pointer_type = void*; + using size_type = AZStd::size_t; + using difference_type = AZStd::ptrdiff_t; + using allow_memory_leaks = AZStd::false_type; ///< Regular allocators should not leak. AZ_FORCE_INLINE pointer_type allocate(size_t byteSize, size_t alignment, int = 0) { @@ -64,7 +63,7 @@ namespace static constexpr size_t s_maxNumberOfAllocationsToRecord = 16384; static size_t s_numberOfAllocationsRecorded = 0; - static constexpr size_t s_allocationOperationCount = 5 * 1024; + static constexpr size_t s_allocationOperationCount = 8 * 1024; static AZStd::array s_operations = {}; static uint64_t s_operationCounter = 0; @@ -76,11 +75,12 @@ namespace void RecordAllocatorOperation(AllocatorOperation::OperationType type, void* ptr, size_t size = 0, size_t alignment = 0) { - AZStd::scoped_lock lock(s_operationsMutex); + AZStd::scoped_lock lock(s_operationsMutex); if (s_operationCounter == s_allocationOperationCount) { AZ::IO::SystemFile file; int mode = AZ::IO::SystemFile::OpenMode::SF_OPEN_APPEND | AZ::IO::SystemFile::OpenMode::SF_OPEN_WRITE_ONLY; + // memoryrecordings.bin is being output to the current working directory if (!file.Exists("memoryrecordings.bin")) { mode |= AZ::IO::SystemFile::OpenMode::SF_OPEN_CREATE; @@ -158,188 +158,195 @@ namespace } #endif -AllocatorBase::AllocatorBase(IAllocatorSchema* allocationSchema, const char* name, const char* desc) - : IAllocator(allocationSchema) - , m_name(name) - , m_desc(desc) +namespace AZ { -} - -AllocatorBase::~AllocatorBase() -{ - AZ_Assert(!m_isReady, "Allocator %s (%s) is being destructed without first having gone through proper calls to PreDestroy() and Destroy(). Use AllocatorInstance<> for global allocators or AllocatorWrapper<> for local allocators.", m_name, m_desc); -} - -const char* AllocatorBase::GetName() const -{ - return m_name; -} - -const char* AllocatorBase::GetDescription() const -{ - return m_desc; -} - -Debug::AllocationRecords* AllocatorBase::GetRecords() -{ - return m_records; -} - -void AllocatorBase::SetRecords(Debug::AllocationRecords* records) -{ - m_records = records; - m_memoryGuardSize = records ? records->MemoryGuardSize() : 0; -} - -bool AllocatorBase::IsReady() const -{ - return m_isReady; -} - -void AllocatorBase::PostCreate() -{ - if (m_registrationEnabled) + AllocatorBase::AllocatorBase(IAllocatorSchema* allocationSchema, const char* name, const char* desc) + : IAllocator(allocationSchema) + , m_name(name) + , m_desc(desc) { - if (AZ::Environment::IsReady()) + } + + AllocatorBase::~AllocatorBase() + { + AZ_Assert( + !m_isReady, + "Allocator %s (%s) is being destructed without first having gone through proper calls to PreDestroy() and Destroy(). Use " + "AllocatorInstance<> for global allocators or AllocatorWrapper<> for local allocators.", + m_name, m_desc); + } + + const char* AllocatorBase::GetName() const + { + return m_name; + } + + const char* AllocatorBase::GetDescription() const + { + return m_desc; + } + + Debug::AllocationRecords* AllocatorBase::GetRecords() + { + return m_records; + } + + void AllocatorBase::SetRecords(Debug::AllocationRecords* records) + { + m_records = records; + m_memoryGuardSize = records ? records->MemoryGuardSize() : 0; + } + + bool AllocatorBase::IsReady() const + { + return m_isReady; + } + + void AllocatorBase::PostCreate() + { + if (m_registrationEnabled) { - AllocatorManager::Instance().RegisterAllocator(this); + if (AZ::Environment::IsReady()) + { + AllocatorManager::Instance().RegisterAllocator(this); + } + else + { + AllocatorManager::PreRegisterAllocator(this); + } } - else + + const auto debugConfig = GetDebugConfig(); + if (!debugConfig.m_excludeFromDebugging) { - AllocatorManager::PreRegisterAllocator(this); + SetRecords(aznew Debug::AllocationRecords( + (unsigned char)debugConfig.m_stackRecordLevels, debugConfig.m_usesMemoryGuards, debugConfig.m_marksUnallocatedMemory, + GetName())); } + + m_isReady = true; } - const auto debugConfig = GetDebugConfig(); - if (!debugConfig.m_excludeFromDebugging) + void AllocatorBase::PreDestroy() { - SetRecords(aznew Debug::AllocationRecords((unsigned char)debugConfig.m_stackRecordLevels, debugConfig.m_usesMemoryGuards, debugConfig.m_marksUnallocatedMemory, GetName())); - } - - m_isReady = true; -} - -void AllocatorBase::PreDestroy() -{ - Debug::AllocationRecords* allocatorRecords = GetRecords(); - if(allocatorRecords) - { - delete allocatorRecords; - SetRecords(nullptr); - } - - if (m_registrationEnabled && AZ::AllocatorManager::IsReady()) - { - AllocatorManager::Instance().UnRegisterAllocator(this); - } - - m_isReady = false; -} - -void AllocatorBase::SetLazilyCreated(bool lazy) -{ - m_isLazilyCreated = lazy; -} - -bool AllocatorBase::IsLazilyCreated() const -{ - return m_isLazilyCreated; -} - -void AllocatorBase::SetProfilingActive(bool active) -{ - m_isProfilingActive = active; -} - -bool AllocatorBase::IsProfilingActive() const -{ - return m_isProfilingActive; -} - -void AllocatorBase::DisableRegistration() -{ - m_registrationEnabled = false; -} - -void AllocatorBase::ProfileAllocation(void* ptr, size_t byteSize, size_t alignment, const char* name, const char* fileName, int lineNum, int suppressStackRecord) -{ - if (m_isProfilingActive) - { -#if defined(AZ_HAS_VARIADIC_TEMPLATES) && defined(AZ_DEBUG_BUILD) - ++suppressStackRecord; // one more for the fact the ebus is a function -#endif // AZ_HAS_VARIADIC_TEMPLATES - - auto records = GetRecords(); - if (records) + Debug::AllocationRecords* allocatorRecords = GetRecords(); + if (allocatorRecords) { - records->RegisterAllocation(ptr, byteSize, alignment, name, fileName, lineNum, suppressStackRecord + 1); + delete allocatorRecords; + SetRecords(nullptr); } + + if (m_registrationEnabled && AZ::AllocatorManager::IsReady()) + { + AllocatorManager::Instance().UnRegisterAllocator(this); + } + + m_isReady = false; } -#if RECORDING_ENABLED - RecordAllocatorOperation(AllocatorOperation::ALLOCATE, ptr, byteSize, alignment); + void AllocatorBase::SetLazilyCreated(bool lazy) + { + m_isLazilyCreated = lazy; + } + + bool AllocatorBase::IsLazilyCreated() const + { + return m_isLazilyCreated; + } + + void AllocatorBase::SetProfilingActive(bool active) + { + m_isProfilingActive = active; + } + + bool AllocatorBase::IsProfilingActive() const + { + return m_isProfilingActive; + } + + void AllocatorBase::DisableRegistration() + { + m_registrationEnabled = false; + } + + void AllocatorBase::ProfileAllocation( + void* ptr, size_t byteSize, size_t alignment, const char* name, const char* fileName, int lineNum, int suppressStackRecord) + { + if (m_isProfilingActive) + { + auto records = GetRecords(); + if (records) + { + records->RegisterAllocation(ptr, byteSize, alignment, name, fileName, lineNum, suppressStackRecord + 1); + } + } + +#if O3DE_RECORDING_ENABLED + RecordAllocatorOperation(AllocatorOperation::ALLOCATE, ptr, byteSize, alignment); #endif -} + } -void AllocatorBase::ProfileDeallocation(void* ptr, size_t byteSize, size_t alignment, Debug::AllocationInfo* info) -{ - if (m_isProfilingActive) + void AllocatorBase::ProfileDeallocation(void* ptr, size_t byteSize, size_t alignment, Debug::AllocationInfo* info) { - auto records = GetRecords(); - if (records) + if (m_isProfilingActive) { - records->UnregisterAllocation(ptr, byteSize, alignment, info); + auto records = GetRecords(); + if (records) + { + records->UnregisterAllocation(ptr, byteSize, alignment, info); + } } - } -#if RECORDING_ENABLED - RecordAllocatorOperation(AllocatorOperation::DEALLOCATE, ptr, byteSize, alignment); +#if O3DE_RECORDING_ENABLED + RecordAllocatorOperation(AllocatorOperation::DEALLOCATE, ptr, byteSize, alignment); #endif -} - -void AllocatorBase::ProfileReallocationBegin([[maybe_unused]] void* ptr, [[maybe_unused]] size_t newSize) -{ -} - -void AllocatorBase::ProfileReallocationEnd(void* ptr, void* newPtr, size_t newSize, size_t newAlignment) -{ - if (m_isProfilingActive) - { - Debug::AllocationInfo info; - ProfileDeallocation(ptr, 0, 0, &info); - ProfileAllocation(newPtr, newSize, newAlignment, info.m_name, info.m_fileName, info.m_lineNum, 0); } -#if RECORDING_ENABLED - RecordAllocatorOperation(AllocatorOperation::DEALLOCATE, ptr); - RecordAllocatorOperation(AllocatorOperation::ALLOCATE, newPtr, newSize, newAlignment); -#endif -} -void AllocatorBase::ProfileReallocation(void* ptr, void* newPtr, size_t newSize, size_t newAlignment) -{ - ProfileReallocationEnd(ptr, newPtr, newSize, newAlignment); -} - -void AllocatorBase::ProfileResize(void* ptr, size_t newSize) -{ - if (newSize && m_isProfilingActive) + void AllocatorBase::ProfileReallocationBegin([[maybe_unused]] void* ptr, [[maybe_unused]] size_t newSize) { - auto records = GetRecords(); - if (records) + } + + void AllocatorBase::ProfileReallocationEnd(void* ptr, void* newPtr, size_t newSize, size_t newAlignment) + { + if (m_isProfilingActive) { - records->ResizeAllocation(ptr, newSize); + Debug::AllocationInfo info; + ProfileDeallocation(ptr, 0, 0, &info); + ProfileAllocation(newPtr, newSize, newAlignment, info.m_name, info.m_fileName, info.m_lineNum, 0); } - } -#if RECORDING_ENABLED - RecordAllocatorOperation(AllocatorOperation::ALLOCATE, ptr, newSize); +#if O3DE_RECORDING_ENABLED + RecordAllocatorOperation(AllocatorOperation::DEALLOCATE, ptr); + RecordAllocatorOperation(AllocatorOperation::ALLOCATE, newPtr, newSize, newAlignment); #endif -} - -bool AllocatorBase::OnOutOfMemory(size_t byteSize, size_t alignment, int flags, const char* name, const char* fileName, int lineNum) -{ - if (AllocatorManager::IsReady() && AllocatorManager::Instance().m_outOfMemoryListener) - { - AllocatorManager::Instance().m_outOfMemoryListener(this, byteSize, alignment, flags, name, fileName, lineNum); - return true; } - return false; -} + + void AllocatorBase::ProfileReallocation(void* ptr, void* newPtr, size_t newSize, size_t newAlignment) + { + ProfileReallocationEnd(ptr, newPtr, newSize, newAlignment); + } + + void AllocatorBase::ProfileResize(void* ptr, size_t newSize) + { + if (newSize && m_isProfilingActive) + { + auto records = GetRecords(); + if (records) + { + records->ResizeAllocation(ptr, newSize); + } + } +#if O3DE_RECORDING_ENABLED + RecordAllocatorOperation(AllocatorOperation::ALLOCATE, ptr, newSize); +#endif + } + + bool AllocatorBase::OnOutOfMemory(size_t byteSize, size_t alignment, int flags, const char* name, const char* fileName, int lineNum) + { + if (AllocatorManager::IsReady() && AllocatorManager::Instance().m_outOfMemoryListener) + { + AllocatorManager::Instance().m_outOfMemoryListener(this, byteSize, alignment, flags, name, fileName, lineNum); + return true; + } + return false; + } + +} // namespace AZ diff --git a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h index 63e9027dd2..21ce34eb80 100644 --- a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h +++ b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.h @@ -50,7 +50,7 @@ namespace AZ BestFitExternalMapSchema(const Descriptor& desc); - pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) override; + pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = nullptr, const char* fileName = nullptr, int lineNum = 0, unsigned int suppressStackRecord = 0) override; void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) override; size_type Resize(pointer_type ptr, size_type newSize) override; pointer_type ReAllocate(pointer_type ptr, size_type newSize, size_type newAlignment) override; diff --git a/Code/Framework/AzCore/AzCore/Memory/IAllocator.h b/Code/Framework/AzCore/AzCore/Memory/IAllocator.h index 319175f9ee..4612b27249 100644 --- a/Code/Framework/AzCore/AzCore/Memory/IAllocator.h +++ b/Code/Framework/AzCore/AzCore/Memory/IAllocator.h @@ -37,9 +37,9 @@ namespace AZ typedef size_t size_type; typedef ptrdiff_t difference_type; - virtual ~IAllocatorSchema() {} + virtual ~IAllocatorSchema() = default; - virtual pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = 0, const char* fileName = 0, int lineNum = 0, unsigned int suppressStackRecord = 0) = 0; + virtual pointer_type Allocate(size_type byteSize, size_type alignment, int flags = 0, const char* name = nullptr, const char* fileName = nullptr, int lineNum = 0, unsigned int suppressStackRecord = 0) = 0; virtual void DeAllocate(pointer_type ptr, size_type byteSize = 0, size_type alignment = 0) = 0; /// Resize an allocated memory block. Returns the new adjusted size (as close as possible or equal to the requested one) or 0 (if you don't support resize at all). virtual size_type Resize(pointer_type ptr, size_type newSize) = 0; diff --git a/Code/Framework/AzCore/AzCore/Memory/Memory.h b/Code/Framework/AzCore/AzCore/Memory/Memory.h index 2ecba6ebff..c87c9cd303 100644 --- a/Code/Framework/AzCore/AzCore/Memory/Memory.h +++ b/Code/Framework/AzCore/AzCore/Memory/Memory.h @@ -735,7 +735,7 @@ namespace AZ typedef typename Allocator::Descriptor Descriptor; // Maintained for backwards compatibility, prefer to use Get() instead. - // Get was previously used to get the the schema, however, that bypases what the allocators are doing. + // Get was previously used to get the the schema, however, that bypasses what the allocators are doing. // If the schema is needed, call Get().GetSchema() AZ_FORCE_INLINE static IAllocator& GetAllocator() { diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index 7870749d24..0599038006 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -155,11 +155,9 @@ namespace Benchmark } private: - static size_t s_numAllocatedBytes; + inline static size_t s_numAllocatedBytes = 0; }; - size_t TestAllocatorWrapper::s_numAllocatedBytes = 0; - // Some allocator are not fully declared, those we simply setup from the schema class MallocSchemaAllocator : public AZ::SimpleSchemaAllocator { From af6af93dffe2764ccf5160436e64931775cc18ce Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Wed, 5 Jan 2022 14:23:32 -0800 Subject: [PATCH 18/20] Fixes Linux builds Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- .../AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp | 4 ++-- Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp index 9561d1019c..5029e6349f 100644 --- a/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/BestFitExternalMapSchema.cpp @@ -133,13 +133,13 @@ namespace AZ BestFitExternalMapSchema::size_type BestFitExternalMapSchema::Resize(pointer_type, size_type) { - AZ_Assert(false, AZ_FUNCTION_SIGNATURE " unsupported"); + AZ_Assert(false, "%s unsupported", AZ_FUNCTION_SIGNATURE); return 0; } BestFitExternalMapSchema::pointer_type BestFitExternalMapSchema::ReAllocate(pointer_type, size_type, size_type) { - AZ_Assert(false, AZ_FUNCTION_SIGNATURE " unsupported"); + AZ_Assert(false, "%s unsupported", AZ_FUNCTION_SIGNATURE); return nullptr; } diff --git a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp index 0599038006..e027b03a49 100644 --- a/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp +++ b/Code/Framework/AzCore/Tests/Memory/AllocatorBenchmarks.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include From 4a7e00f12534e2f4cbc2a5fc119d9166c4a06c3c Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Thu, 20 Jan 2022 14:02:57 -0800 Subject: [PATCH 19/20] Removes call to DisableOverride which was removed with the OverrideShim Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- Code/Framework/AzCore/AzCore/DOM/DomValue.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Code/Framework/AzCore/AzCore/DOM/DomValue.h b/Code/Framework/AzCore/AzCore/DOM/DomValue.h index d1d3c1745d..ec0a5fb267 100644 --- a/Code/Framework/AzCore/AzCore/DOM/DomValue.h +++ b/Code/Framework/AzCore/AzCore/DOM/DomValue.h @@ -51,7 +51,6 @@ namespace AZ::Dom ValueAllocator() : Base("DomValueAllocator", "Allocator for AZ::Dom::Value") { - DisableOverriding(); } }; From 25157b3fa15cd34e5edc8f908cdf77298922732a Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 25 Jan 2022 16:26:00 -0800 Subject: [PATCH 20/20] PR comments Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp index 0414740f2a..7238a18672 100644 --- a/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp +++ b/Code/Framework/AzCore/AzCore/Memory/SystemAllocator.cpp @@ -118,9 +118,9 @@ namespace AZ AZ_Assert(!g_isSystemSchemaUsed, "AZ::SystemAllocator MUST be created first! It's the source of all allocations!"); #if AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_HPHA - m_schema = new (&g_systemSchema) HphaSchema(heapDesc); + m_schema = new (&g_systemSchema) HphaSchema(heapDesc); #elif AZCORE_SYSTEM_ALLOCATOR == AZCORE_SYSTEM_ALLOCATOR_MALLOC - m_schema = new (&g_systemSchema) MallocSchema(heapDesc); + m_schema = new (&g_systemSchema) MallocSchema(heapDesc); #endif g_isSystemSchemaUsed = true; isReady = true; @@ -292,7 +292,7 @@ namespace AZ //========================================================================= SystemAllocator::size_type SystemAllocator::AllocationSize(pointer_type ptr) { - size_type allocSize = MemorySizeAdjustedDown(m_schema->AllocationSize(ptr)); + size_type allocSize = MemorySizeAdjustedDown(m_schema->AllocationSize(ptr)); return allocSize; }