Memory.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/PlatformIncl.h>
  9. #include <AzCore/Memory/SystemAllocator.h>
  10. #include <AzCore/Memory/PoolAllocator.h>
  11. #include <AzCore/Memory/HphaAllocator.h>
  12. #include <AzCore/Memory/AllocationRecords.h>
  13. #include <AzCore/Debug/StackTracer.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. #include <AzCore/std/parallel/thread.h>
  16. #include <AzCore/std/parallel/containers/lock_free_intrusive_stamped_stack.h>
  17. #include <AzCore/std/parallel/mutex.h>
  18. #include <AzCore/std/parallel/lock.h>
  19. #include <AzCore/std/containers/intrusive_slist.h>
  20. #include <AzCore/std/containers/intrusive_list.h>
  21. #include <AzCore/std/chrono/chrono.h>
  22. #include <AzCore/std/functional.h>
  23. using namespace AZ;
  24. using namespace AZ::Debug;
  25. namespace UnitTest
  26. {
  27. class MemoryTrackingFixture
  28. : public ::testing::Test
  29. {
  30. public:
  31. void SetUp() override
  32. {
  33. AZ::AllocatorManager::Instance().SetDefaultTrackingMode(AZ::Debug::AllocationRecords::Mode::RECORD_FULL);
  34. AZ::AllocatorManager::Instance().SetTrackingMode(AZ::Debug::AllocationRecords::Mode::RECORD_FULL);
  35. AZ::AllocatorManager::Instance().EnterProfilingMode();
  36. AZ::AllocatorManager::Instance().SetDefaultProfilingState(true);
  37. }
  38. void TearDown() override
  39. {
  40. AZ::AllocatorManager::Instance().GarbageCollect();
  41. AZ::AllocatorManager::Instance().ExitProfilingMode();
  42. AZ::AllocatorManager::Instance().SetDefaultProfilingState(false);
  43. AZ::AllocatorManager::Instance().SetTrackingMode(AZ::Debug::AllocationRecords::Mode::RECORD_FULL);
  44. AZ::AllocatorManager::Instance().SetDefaultTrackingMode(AZ::Debug::AllocationRecords::Mode::RECORD_NO_RECORDS);
  45. }
  46. };
  47. class SystemAllocatorTest
  48. : public MemoryTrackingFixture
  49. {
  50. static const int m_threadStackSize = 128 * 1024;
  51. static const unsigned int m_maxNumThreads = 10;
  52. AZStd::thread_desc m_desc[m_maxNumThreads];
  53. public:
  54. void SetUp() override
  55. {
  56. MemoryTrackingFixture::SetUp();
  57. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  58. {
  59. // Set the threads stack size
  60. m_desc[i].m_stackSize = m_threadStackSize;
  61. // Allocate stacks for the platforms that can't do it themself.
  62. }
  63. }
  64. void TearDown() override
  65. {
  66. // Free allocated stacks.
  67. MemoryTrackingFixture::TearDown();
  68. }
  69. void ThreadFunc()
  70. {
  71. #ifdef _DEBUG
  72. static const int numAllocations = 100;
  73. #else
  74. static const int numAllocations = 10000;
  75. #endif
  76. void* addresses[numAllocations] = { nullptr };
  77. IAllocator& sysAllocator = AllocatorInstance<SystemAllocator>::Get();
  78. //////////////////////////////////////////////////////////////////////////
  79. // Allocate
  80. AZStd::size_t totalAllocSize = 0;
  81. for (int i = 0; i < numAllocations; ++i)
  82. {
  83. AZStd::size_t size = AZStd::GetMax(rand() % 256, 1);
  84. // supply all debug info, so we don't need to record the stack.
  85. addresses[i] = sysAllocator.Allocate(size, 8);
  86. memset(addresses[i], 1, size);
  87. totalAllocSize += size;
  88. }
  89. //////////////////////////////////////////////////////////////////////////
  90. EXPECT_GE(sysAllocator.NumAllocatedBytes(), totalAllocSize);
  91. //////////////////////////////////////////////////////////////////////////
  92. // Deallocate
  93. for (int i = numAllocations - 1; i >= 0; --i)
  94. {
  95. sysAllocator.DeAllocate(addresses[i]);
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. }
  99. void run()
  100. {
  101. void* address[100];
  102. #if defined(AZ_PLATFORM_WINDOWS)
  103. // On windows we don't require to preallocate memory to function.
  104. // On most consoles we do!
  105. {
  106. IAllocator& sysAllocator = AllocatorInstance<SystemAllocator>::Get();
  107. for (int i = 0; i < 100; ++i)
  108. {
  109. address[i] = sysAllocator.Allocate(1000, 32);
  110. EXPECT_NE(nullptr, address[i]);
  111. EXPECT_EQ(0, ((size_t)address[i] & 31)); // check alignment
  112. EXPECT_GE(sysAllocator.AllocationSize(address[i]), 1000); // check allocation size
  113. }
  114. EXPECT_GE(sysAllocator.NumAllocatedBytes(), 100000); // we requested 100 * 1000 so we should have at least this much allocated
  115. for (int i = 0; i < 100; ++i)
  116. {
  117. sysAllocator.DeAllocate(address[i]);
  118. }
  119. ////////////////////////////////////////////////////////////////////////
  120. // Create some threads and simulate concurrent allocation and deallocation
  121. {
  122. AZStd::thread m_threads[m_maxNumThreads];
  123. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  124. {
  125. m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&SystemAllocatorTest::ThreadFunc, this));
  126. // give some time offset to the threads so we can test alloc and dealloc at the same time.
  127. //AZStd::this_thread::sleep_for(AZStd::chrono::microseconds(500));
  128. }
  129. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  130. {
  131. m_threads[i].join();
  132. }
  133. }
  134. }
  135. #endif
  136. memset(address, 0, AZ_ARRAY_SIZE(address) * sizeof(void*));
  137. IAllocator& sysAllocator = AllocatorInstance<SystemAllocator>::Get();
  138. for (int i = 0; i < 100; ++i)
  139. {
  140. address[i] = sysAllocator.Allocate(1000, 32);
  141. EXPECT_NE(nullptr, address[i]);
  142. EXPECT_EQ(0, ((size_t)address[i] & 31)); // check alignment
  143. EXPECT_GE(sysAllocator.AllocationSize(address[i]), 1000); // check allocation size
  144. }
  145. EXPECT_TRUE(sysAllocator.NumAllocatedBytes() >= 100000); // we requested 100 * 1000 so we should have at least this much allocated
  146. // If tracking and recording is enabled, we can verify that the alloc info is valid
  147. #if defined(AZ_DEBUG_BUILD)
  148. sysAllocator.GetRecords()->lock();
  149. EXPECT_TRUE(sysAllocator.GetRecords());
  150. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  151. EXPECT_EQ(100, records.size());
  152. for (Debug::AllocationRecordsType::const_iterator iter = records.begin(); iter != records.end(); ++iter)
  153. {
  154. const Debug::AllocationInfo& ai = iter->second;
  155. EXPECT_EQ(32, ai.m_alignment);
  156. EXPECT_EQ(1000, ai.m_byteSize);
  157. EXPECT_EQ(nullptr, ai.m_fileName); // We did not pass fileName or lineNum to sysAllocator.Allocate()
  158. EXPECT_EQ(0, ai.m_lineNum); // -- " --
  159. # if defined(AZ_PLATFORM_WINDOWS)
  160. // if our hardware support stack traces make sure we have them, since we did not provide fileName,lineNum
  161. EXPECT_TRUE(ai.m_stackFrames[0].IsValid()); // We need to have at least one frame
  162. // For windows we should be able to decode the program counters into readable content.
  163. // This is possible on deprecated platforms too, but we would need to load the map file manually and so on... it's tricky.
  164. // Note: depending on where the tests are run from the call stack may differ.
  165. SymbolStorage::StackLine stackLine[20];
  166. auto recordFrameCount = AZ::GetMin(ai.m_stackFramesCount, static_cast<unsigned int>(AZ_ARRAY_SIZE(stackLine)));
  167. SymbolStorage::DecodeFrames(ai.m_stackFrames, recordFrameCount, stackLine);
  168. bool found = false;
  169. int foundIndex = 0; // After finding it for the first time, save the index so it can be reused
  170. for (int idx = foundIndex; idx < AZ_ARRAY_SIZE(stackLine); idx++)
  171. {
  172. if (strstr(stackLine[idx], "SystemAllocatorTest::run"))
  173. {
  174. found = true;
  175. break;
  176. }
  177. else
  178. {
  179. foundIndex++;
  180. }
  181. }
  182. EXPECT_TRUE(found);
  183. # endif // defined(AZ_PLATFORM_WINDOWS)
  184. }
  185. sysAllocator.GetRecords()->unlock();
  186. #endif //#if defined(AZ_DEBUG_BUILD)
  187. // Free all memory
  188. for (int i = 0; i < 100; ++i)
  189. {
  190. sysAllocator.DeAllocate(address[i]);
  191. }
  192. sysAllocator.GarbageCollect();
  193. EXPECT_LT(sysAllocator.NumAllocatedBytes(), 1024); // We freed everything from a memspace, we should have only a very minor chunk of data
  194. //////////////////////////////////////////////////////////////////////////
  195. // realloc test
  196. address[0] = nullptr;
  197. static const unsigned int checkValue = 0x0badbabe;
  198. // create tree (non pool) allocation (we usually pool < 256 bytes)
  199. address[0] = sysAllocator.Allocate(2048, 16);
  200. *(unsigned*)(address[0]) = checkValue; // set check value
  201. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 2048, 16);
  202. address[0] = sysAllocator.ReAllocate(address[0], 1024, 16); // test tree big -> tree small
  203. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  204. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 1024, 16);
  205. address[0] = sysAllocator.ReAllocate(address[0], 4096, 16); // test tree small -> tree big
  206. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 4096, 16);
  207. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  208. address[0] = sysAllocator.ReAllocate(address[0], 128, 16); // test tree -> pool,
  209. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 128, 16);
  210. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  211. address[0] = sysAllocator.ReAllocate(address[0], 64, 16); // pool big -> pool small
  212. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 64, 16);
  213. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  214. address[0] = sysAllocator.ReAllocate(address[0], 64, 16); // pool sanity check
  215. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 64, 16);
  216. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  217. address[0] = sysAllocator.ReAllocate(address[0], 192, 16); // pool small -> pool big
  218. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 192, 16);
  219. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  220. address[0] = sysAllocator.ReAllocate(address[0], 2048, 16); // pool -> tree
  221. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 2048, 16);
  222. ;
  223. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  224. address[0] = sysAllocator.ReAllocate(address[0], 2048, 16); // tree sanity check
  225. AZ_TEST_ASSERT_CLOSE(sysAllocator.AllocationSize(address[0]), 2048, 16);
  226. ;
  227. EXPECT_EQ(checkValue, *(unsigned*)address[0]);
  228. sysAllocator.DeAllocate(address[0], 2048, 16);
  229. // TODO realloc with different alignment tests
  230. //////////////////////////////////////////////////////////////////////////
  231. // run some thread allocations.
  232. //////////////////////////////////////////////////////////////////////////
  233. // Create some threads and simulate concurrent allocation and deallocation
  234. //AZStd::chrono::steady_clock::time_point startTime = AZStd::chrono::steady_clock::now();
  235. {
  236. AZStd::thread m_threads[m_maxNumThreads];
  237. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  238. {
  239. m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&SystemAllocatorTest::ThreadFunc, this));
  240. // give some time offset to the threads so we can test alloc and dealloc at the same time.
  241. AZStd::this_thread::sleep_for(AZStd::chrono::microseconds(500));
  242. }
  243. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  244. {
  245. m_threads[i].join();
  246. }
  247. }
  248. //AZStd::chrono::microseconds exTime = AZStd::chrono::steady_clock::now() - startTime;
  249. //AZ_Printf("UnitTest::SystemAllocatorTest::mspaces","Time: %d Ms\n",exTime.count());
  250. //////////////////////////////////////////////////////////////////////////
  251. }
  252. };
  253. TEST_F(SystemAllocatorTest, Test)
  254. {
  255. run();
  256. }
  257. class PoolAllocatorTest
  258. : public MemoryTrackingFixture
  259. {
  260. protected:
  261. public:
  262. void run(IAllocator& poolAllocator)
  263. {
  264. // 64 should be the max number of different pool sizes we can allocate.
  265. void* address[64];
  266. //////////////////////////////////////////////////////////////////////////
  267. // Allocate different pool sizes
  268. memset(address, 0, AZ_ARRAY_SIZE(address) * sizeof(void*));
  269. // try any size from 8 to 256 (which are supported pool sizes)
  270. int i = 0;
  271. int expectedMinimumAllocationSize = 0;
  272. for (int size = 8; size <= 256; ++i, size += 8)
  273. {
  274. address[i] = poolAllocator.Allocate(size, 8);
  275. EXPECT_GE(poolAllocator.AllocationSize(address[i]), (AZStd::size_t)size);
  276. memset(address[i], 1, size);
  277. expectedMinimumAllocationSize += size;
  278. }
  279. EXPECT_GE(poolAllocator.NumAllocatedBytes(), expectedMinimumAllocationSize);
  280. if (poolAllocator.GetRecords())
  281. {
  282. poolAllocator.GetRecords()->lock();
  283. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  284. EXPECT_EQ(32, records.size());
  285. poolAllocator.GetRecords()->unlock();
  286. }
  287. for (i = 0; address[i] != nullptr; ++i)
  288. {
  289. poolAllocator.DeAllocate(address[i]);
  290. }
  291. //////////////////////////////////////////////////////////////////////////
  292. EXPECT_EQ(0, poolAllocator.NumAllocatedBytes());
  293. if (poolAllocator.GetRecords())
  294. {
  295. poolAllocator.GetRecords()->lock();
  296. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  297. EXPECT_EQ(0, records.size());
  298. poolAllocator.GetRecords()->unlock();
  299. }
  300. //////////////////////////////////////////////////////////////////////////
  301. // Allocate many elements from the same size
  302. // AllocatorManager::MemoryBreak mb;
  303. // mb.fileName = "This File";
  304. // AllocatorManager::Instance().SetMemoryBreak(0,mb);
  305. memset(address, 0, AZ_ARRAY_SIZE(address) * sizeof(void*));
  306. for (unsigned int j = 0; j < AZ_ARRAY_SIZE(address); ++j)
  307. {
  308. address[j] = poolAllocator.Allocate(256, 8);
  309. EXPECT_GE(poolAllocator.AllocationSize(address[j]), 256);
  310. memset(address[j], 1, 256);
  311. }
  312. // AllocatorManager::Instance().ResetMemoryBreak(0);
  313. EXPECT_GE(poolAllocator.NumAllocatedBytes(), AZ_ARRAY_SIZE(address) * 256);
  314. if (poolAllocator.GetRecords())
  315. {
  316. poolAllocator.GetRecords()->lock();
  317. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  318. EXPECT_EQ(AZ_ARRAY_SIZE(address), records.size());
  319. poolAllocator.GetRecords()->unlock();
  320. }
  321. for (unsigned int j = 0; j < AZ_ARRAY_SIZE(address); ++j)
  322. {
  323. poolAllocator.DeAllocate(address[j]);
  324. }
  325. //////////////////////////////////////////////////////////////////////////
  326. EXPECT_EQ(0, poolAllocator.NumAllocatedBytes());
  327. if (poolAllocator.GetRecords())
  328. {
  329. EXPECT_NE(nullptr, poolAllocator.GetRecords());
  330. poolAllocator.GetRecords()->lock();
  331. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  332. EXPECT_EQ(0, records.size());
  333. poolAllocator.GetRecords()->unlock();
  334. }
  335. }
  336. };
  337. TEST_F(PoolAllocatorTest, TestDefaultPoolAllocator)
  338. {
  339. run(AllocatorInstance<PoolAllocator>::Get());
  340. }
  341. constexpr size_t s_testCustomPoolPageSize = 1024;
  342. constexpr size_t s_testCustomPoolMinAllocationSize = 64;
  343. constexpr size_t s_testCustomPoolMaxAllocationSize = 256;
  344. // Define a custom pool allocator
  345. class TestCustomPoolAllocator final : public AZ::Internal::PoolAllocatorHelper<AZ::PoolSchema>
  346. {
  347. public:
  348. AZ_CLASS_ALLOCATOR(TestCustomPoolAllocator, AZ::SystemAllocator, 0);
  349. AZ_TYPE_INFO(TestCustomPoolAllocator, "{3B299C74-7697-4188-A116-32FC69A72E15}");
  350. TestCustomPoolAllocator()
  351. // Invoke the base constructor explicitely to use the override that takes custom page, min, and max allocation sizes
  352. : AZ::Internal::PoolAllocatorHelper<AZ::PoolSchema>(
  353. s_testCustomPoolPageSize, s_testCustomPoolMinAllocationSize, s_testCustomPoolMaxAllocationSize)
  354. {
  355. }
  356. };
  357. TEST_F(PoolAllocatorTest, TestWithCustomPoolAllocator)
  358. {
  359. run(AllocatorInstance<TestCustomPoolAllocator>::Get());
  360. }
  361. TEST_F(PoolAllocatorTest, Allocate_LessThanMinAllocationSize_RoundsUpToMinAllocationSize)
  362. {
  363. // Validate that the minimum allocation size of a custom PoolAllocator is respected
  364. AZ::IAllocator::pointer result =
  365. AZ::AllocatorInstance<TestCustomPoolAllocator>::Get().Allocate(s_testCustomPoolMinAllocationSize - 1, 1);
  366. EXPECT_EQ(AZ::AllocatorInstance<TestCustomPoolAllocator>::Get().NumAllocatedBytes(), s_testCustomPoolMinAllocationSize);
  367. // DeAllocate so the test doesn't leak
  368. AZ::AllocatorInstance<TestCustomPoolAllocator>::Get().DeAllocate(result);
  369. }
  370. TEST_F(PoolAllocatorTest, Allocate_MoreThanMaxAllocationSize_FailsToAllocate)
  371. {
  372. // Validate that the maximum allocation size of a custom PoolAllocator is respected
  373. AZ_TEST_START_TRACE_SUPPRESSION;
  374. AZ::IAllocator::pointer result =
  375. AZ::AllocatorInstance<TestCustomPoolAllocator>::Get().Allocate(s_testCustomPoolMaxAllocationSize + 1, 1);
  376. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  377. EXPECT_EQ(result, nullptr);
  378. }
  379. /**
  380. * Tests ThreadPoolAllocator
  381. */
  382. class ThreadPoolAllocatorTest
  383. : public MemoryTrackingFixture
  384. {
  385. static const int m_threadStackSize = 128 * 1024;
  386. static const unsigned int m_maxNumThreads = 10;
  387. AZStd::thread_desc m_desc[m_maxNumThreads];
  388. //\todo lock free
  389. struct AllocClass
  390. : public AZStd::intrusive_slist_node<AllocClass>
  391. {
  392. int m_data;
  393. };
  394. typedef AZStd::intrusive_slist<AllocClass, AZStd::slist_base_hook<AllocClass> > SharedAllocStackType;
  395. AZStd::mutex m_mutex;
  396. SharedAllocStackType m_sharedAlloc;
  397. #ifdef _DEBUG
  398. static const int m_numSharedAlloc = 100; ///< Number of shared alloc free.
  399. #else
  400. static const int m_numSharedAlloc = 10000; ///< Number of shared alloc free.
  401. #endif
  402. #if (ATOMIC_ADDRESS_LOCK_FREE==2) // or we can use locked atomics
  403. AZStd::atomic_bool m_doneSharedAlloc;
  404. #else
  405. volatile bool m_doneSharedAlloc;
  406. #endif
  407. public:
  408. void SetUp() override
  409. {
  410. MemoryTrackingFixture::SetUp();
  411. m_doneSharedAlloc = false;
  412. #if defined(AZ_PLATFORM_WINDOWS)
  413. SetCriticalSectionSpinCount(m_mutex.native_handle(), 4000);
  414. #endif
  415. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  416. {
  417. m_desc[i].m_stackSize = m_threadStackSize;
  418. }
  419. }
  420. void AllocDeallocFunc()
  421. {
  422. #ifdef _DEBUG
  423. static const int numAllocations = 100;
  424. #else
  425. static const int numAllocations = 10000;
  426. #endif
  427. void* addresses[numAllocations] = { nullptr };
  428. IAllocator& poolAllocator = AllocatorInstance<ThreadPoolAllocator>::Get();
  429. //////////////////////////////////////////////////////////////////////////
  430. // Allocate
  431. for (int i = 0; i < numAllocations; ++i)
  432. {
  433. AZStd::size_t size = AZStd::GetMax(1, ((i + 1) * 2) % 256);
  434. addresses[i] = poolAllocator.Allocate(size, 8);
  435. EXPECT_NE(addresses[i], nullptr);
  436. memset(addresses[i], 1, size);
  437. }
  438. //////////////////////////////////////////////////////////////////////////
  439. //////////////////////////////////////////////////////////////////////////
  440. // Deallocate
  441. for (int i = numAllocations - 1; i >= 0; --i)
  442. {
  443. poolAllocator.DeAllocate(addresses[i]);
  444. }
  445. //////////////////////////////////////////////////////////////////////////
  446. }
  447. /**
  448. * Function that does allocations and pushes them on a lock free stack
  449. */
  450. void SharedAlloc()
  451. {
  452. IAllocator& poolAllocator = AllocatorInstance<ThreadPoolAllocator>::Get();
  453. for (int i = 0; i < m_numSharedAlloc; ++i)
  454. {
  455. AZStd::size_t minSize = sizeof(AllocClass);
  456. AZStd::size_t size = AZStd::GetMax((AZStd::size_t)(rand() % 256), minSize);
  457. AllocClass* ac = reinterpret_cast<AllocClass*>(poolAllocator.Allocate(size, AZStd::alignment_of<AllocClass>::value));
  458. AZStd::lock_guard<AZStd::mutex> lock(m_mutex);
  459. m_sharedAlloc.push_back(*ac);
  460. }
  461. }
  462. /**
  463. * Function that does deallocations from the lock free stack
  464. */
  465. void SharedDeAlloc()
  466. {
  467. IAllocator& poolAllocator = AllocatorInstance<ThreadPoolAllocator>::Get();
  468. AllocClass* ac;
  469. int isDone = 0;
  470. while (isDone != 2)
  471. {
  472. AZStd::lock_guard<AZStd::mutex> lock(m_mutex);
  473. while (!m_sharedAlloc.empty())
  474. {
  475. ac = &m_sharedAlloc.front();
  476. m_sharedAlloc.pop_front();
  477. poolAllocator.DeAllocate(ac);
  478. }
  479. if (m_doneSharedAlloc) // once we know we don't add more elements, make one last check and exit.
  480. {
  481. ++isDone;
  482. }
  483. }
  484. }
  485. class MyThreadPoolAllocator
  486. : public ThreadPoolBase<MyThreadPoolAllocator>
  487. {
  488. public:
  489. AZ_CLASS_ALLOCATOR(MyThreadPoolAllocator, SystemAllocator);
  490. AZ_TYPE_INFO(MyThreadPoolAllocator, "{28D80F96-19B1-4465-8278-B53989C44CF1}");
  491. using Base = ThreadPoolBase<MyThreadPoolAllocator>;
  492. };
  493. void run()
  494. {
  495. IAllocator& poolAllocator = AllocatorInstance<ThreadPoolAllocator>::Get();
  496. // 64 should be the max number of different pool sizes we can allocate.
  497. void* address[64];
  498. //////////////////////////////////////////////////////////////////////////
  499. // Allocate different pool sizes
  500. memset(address, 0, AZ_ARRAY_SIZE(address) * sizeof(void*));
  501. // try any size from 8 to 256 (which are supported pool sizes)
  502. int j = 0;
  503. for (int size = 8; size <= 256; ++j, size += 8)
  504. {
  505. address[j] = poolAllocator.Allocate(size, 8);
  506. EXPECT_GE(poolAllocator.AllocationSize(address[j]), (AZStd::size_t)size);
  507. memset(address[j], 1, size);
  508. }
  509. // Re-enable once https://github.com/o3de/o3de/issues/13263 is fixed
  510. // EXPECT_GE(poolAllocator.NumAllocatedBytes(), 4126);
  511. if (poolAllocator.GetRecords())
  512. {
  513. poolAllocator.GetRecords()->lock();
  514. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  515. EXPECT_EQ(32, records.size());
  516. poolAllocator.GetRecords()->unlock();
  517. }
  518. for (int i = 0; address[i] != nullptr; ++i)
  519. {
  520. poolAllocator.DeAllocate(address[i]);
  521. }
  522. //////////////////////////////////////////////////////////////////////////
  523. // Re-enable once https://github.com/o3de/o3de/issues/13263 is fixed
  524. // EXPECT_EQ(0, poolAllocator.NumAllocatedBytes());
  525. if (poolAllocator.GetRecords())
  526. {
  527. poolAllocator.GetRecords()->lock();
  528. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  529. EXPECT_EQ(0, records.size());
  530. poolAllocator.GetRecords()->unlock();
  531. }
  532. //////////////////////////////////////////////////////////////////////////
  533. // Allocate many elements from the same size
  534. memset(address, 0, AZ_ARRAY_SIZE(address) * sizeof(void*));
  535. for (unsigned int i = 0; i < AZ_ARRAY_SIZE(address); ++i)
  536. {
  537. address[i] = poolAllocator.Allocate(256, 8);
  538. EXPECT_GE(poolAllocator.AllocationSize(address[i]), 256);
  539. memset(address[i], 1, 256);
  540. }
  541. // Re-enable once https://github.com/o3de/o3de/issues/13263 is fixed
  542. // EXPECT_GE(poolAllocator.NumAllocatedBytes(), AZ_ARRAY_SIZE(address)*256);
  543. if (poolAllocator.GetRecords())
  544. {
  545. poolAllocator.GetRecords()->lock();
  546. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  547. EXPECT_EQ(AZ_ARRAY_SIZE(address), records.size());
  548. poolAllocator.GetRecords()->unlock();
  549. }
  550. for (unsigned int i = 0; i < AZ_ARRAY_SIZE(address); ++i)
  551. {
  552. poolAllocator.DeAllocate(address[i]);
  553. }
  554. //////////////////////////////////////////////////////////////////////////
  555. // Re-enable once https://github.com/o3de/o3de/issues/13263 is fixed
  556. // EXPECT_EQ(0, poolAllocator.NumAllocatedBytes());
  557. if (poolAllocator.GetRecords())
  558. {
  559. poolAllocator.GetRecords()->lock();
  560. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  561. EXPECT_EQ(0, records.size());
  562. poolAllocator.GetRecords()->unlock();
  563. }
  564. //////////////////////////////////////////////////////////////////////////
  565. // Create some threads and simulate concurrent allocation and deallocation
  566. //AZStd::chrono::steady_clock::time_point startTime = AZStd::chrono::steady_clock::now();
  567. {
  568. AZStd::thread m_threads[m_maxNumThreads];
  569. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  570. {
  571. m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&ThreadPoolAllocatorTest::AllocDeallocFunc, this));
  572. }
  573. for (unsigned int i = 0; i < m_maxNumThreads; ++i)
  574. {
  575. m_threads[i].join();
  576. }
  577. }
  578. //////////////////////////////////////////////////////////////////////////
  579. //AZStd::chrono::microseconds exTime = AZStd::chrono::steady_clock::now() - startTime;
  580. //AZ_Printf("UnitTest","Time: %d Ms\n",exTime.count());
  581. //////////////////////////////////////////////////////////////////////////
  582. // Spawn some threads that allocate and some that deallocate together
  583. {
  584. AZStd::thread m_threads[m_maxNumThreads];
  585. for (unsigned int i = m_maxNumThreads / 2; i < m_maxNumThreads; ++i)
  586. {
  587. m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&ThreadPoolAllocatorTest::SharedDeAlloc, this));
  588. }
  589. for (unsigned int i = 0; i < m_maxNumThreads / 2; ++i)
  590. {
  591. m_threads[i] = AZStd::thread(m_desc[i], AZStd::bind(&ThreadPoolAllocatorTest::SharedAlloc, this));
  592. }
  593. for (unsigned int i = 0; i < m_maxNumThreads / 2; ++i)
  594. {
  595. m_threads[i].join();
  596. }
  597. m_doneSharedAlloc = true;
  598. for (unsigned int i = m_maxNumThreads / 2; i < m_maxNumThreads; ++i)
  599. {
  600. m_threads[i].join();
  601. }
  602. }
  603. //////////////////////////////////////////////////////////////////////////
  604. void* pooled512 = AZ::AllocatorInstance<MyThreadPoolAllocator>::Get().Allocate(512, 512);
  605. ASSERT_TRUE(pooled512);
  606. AZ::AllocatorInstance<MyThreadPoolAllocator>::Get().DeAllocate(pooled512);
  607. AZ_TEST_START_TRACE_SUPPRESSION;
  608. void* pooled2048 = AZ::AllocatorInstance<MyThreadPoolAllocator>::Get().Allocate(2048, 2048);
  609. (void)pooled2048;
  610. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  611. }
  612. };
  613. }
  614. namespace AZ::Internal
  615. {
  616. // Add implementation of PoolAllocatorHelper Class Template RTTI functions inside
  617. // of this translation unit to as the MyThreadPoolAllocator derived class requires linkage to those functions.
  618. AZ_TYPE_INFO_TEMPLATE_WITH_NAME_IMPL(PoolAllocatorHelper, "PoolAllocatorHelper", PoolAllocatorHelperTemplateId, AZ_TYPE_INFO_CLASS);
  619. AZ_RTTI_NO_TYPE_INFO_IMPL((PoolAllocatorHelper, AZ_TYPE_INFO_CLASS), Base);
  620. }
  621. namespace UnitTest
  622. {
  623. TEST_F(ThreadPoolAllocatorTest, Test)
  624. {
  625. run();
  626. }
  627. /**
  628. * Tests azmalloc,azmallocex/azfree.
  629. */
  630. class AZMallocTest
  631. : public MemoryTrackingFixture
  632. {
  633. public:
  634. void run()
  635. {
  636. IAllocator& sysAllocator = AllocatorInstance<SystemAllocator>::Get();
  637. IAllocator& poolAllocator = AllocatorInstance<PoolAllocator>::Get();
  638. void* ptr = azmalloc(16*1024, 32, SystemAllocator);
  639. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  640. if (sysAllocator.GetRecords())
  641. {
  642. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  643. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  644. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  645. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  646. }
  647. azfree(ptr, SystemAllocator);
  648. if (sysAllocator.GetRecords())
  649. {
  650. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  651. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  652. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  653. }
  654. ptr = azmalloc(16*1024, 32, SystemAllocator);
  655. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  656. if (sysAllocator.GetRecords())
  657. {
  658. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  659. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  660. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  661. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  662. }
  663. azfree(ptr, SystemAllocator);
  664. if (sysAllocator.GetRecords())
  665. {
  666. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  667. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  668. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  669. }
  670. ptr = azmalloc(16, 32, PoolAllocator);
  671. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  672. if (poolAllocator.GetRecords())
  673. {
  674. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  675. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  676. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  677. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  678. }
  679. azfree(ptr, PoolAllocator);
  680. if (poolAllocator.GetRecords())
  681. {
  682. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  683. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  684. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  685. }
  686. }
  687. };
  688. TEST_F(AZMallocTest, Test)
  689. {
  690. run();
  691. }
  692. /**
  693. * Tests aznew/delete, azcreate/azdestroy
  694. */
  695. class AZNewCreateDestroyTest
  696. : public MemoryTrackingFixture
  697. {
  698. class MyClass
  699. {
  700. public:
  701. AZ_CLASS_ALLOCATOR(MyClass, PoolAllocator);
  702. MyClass(int data = 303)
  703. : m_data(data) {}
  704. ~MyClass() {}
  705. alignas(32) int m_data;
  706. };
  707. // Explicitly doesn't have AZ_CLASS_ALLOCATOR
  708. class MyDerivedClass
  709. : public MyClass
  710. {
  711. public:
  712. MyDerivedClass() = default;
  713. };
  714. public:
  715. void run()
  716. {
  717. IAllocator& sysAllocator = AllocatorInstance<SystemAllocator>::Get();
  718. IAllocator& poolAllocator = AllocatorInstance<PoolAllocator>::Get();
  719. MyClass* ptr = aznew MyClass(202); /// this should allocate memory from the pool allocator
  720. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  721. EXPECT_EQ(202, ptr->m_data); // check value
  722. if (poolAllocator.GetRecords())
  723. {
  724. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  725. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  726. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  727. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  728. }
  729. delete ptr;
  730. if (poolAllocator.GetRecords())
  731. {
  732. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  733. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  734. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  735. }
  736. // now use the azcreate to allocate the object wherever we want
  737. ptr = azcreate(MyClass, (101), SystemAllocator);
  738. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  739. EXPECT_EQ(101, ptr->m_data); // check value
  740. if (sysAllocator.GetRecords())
  741. {
  742. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  743. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  744. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  745. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  746. }
  747. azdestroy(ptr, SystemAllocator);
  748. if (sysAllocator.GetRecords())
  749. {
  750. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  751. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  752. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  753. }
  754. // Test creation of derived classes
  755. ptr = aznew MyDerivedClass(); /// this should allocate memory from the pool allocator
  756. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  757. EXPECT_EQ(303, ptr->m_data); // check value
  758. if (poolAllocator.GetRecords())
  759. {
  760. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  761. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  762. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  763. EXPECT_TRUE(iter != records.end()); // our allocation is in the list
  764. }
  765. delete ptr;
  766. if (poolAllocator.GetRecords())
  767. {
  768. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  769. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  770. EXPECT_TRUE(records.find(ptr) == records.end()); // our allocation is NOT in the list
  771. }
  772. }
  773. };
  774. TEST_F(AZNewCreateDestroyTest, Test)
  775. {
  776. run();
  777. }
  778. #if AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE
  779. class PERF_MemoryBenchmark
  780. : public ::testing::Test
  781. {
  782. #if defined(_DEBUG)
  783. static const unsigned N = 1024 * 128;
  784. #else
  785. static const unsigned N = 1024 * 512;
  786. #endif
  787. static const size_t DefaultAlignment = 1;
  788. static const size_t MIN_SIZE = 2;
  789. size_t MAX_SIZE;
  790. static const size_t MAX_ALIGNMENT_LOG2 = 7;
  791. static const size_t MAX_ALIGNMENT = 1 << MAX_ALIGNMENT_LOG2;
  792. struct test_record
  793. {
  794. void* ptr;
  795. size_t size;
  796. size_t alignment;
  797. size_t _padding;
  798. }* tr /*[N]*/;
  799. size_t rand_size()
  800. {
  801. float r = float(rand()) / RAND_MAX;
  802. return MIN_SIZE + (size_t)((MAX_SIZE - MIN_SIZE) * powf(r, 8.0f));
  803. }
  804. size_t rand_alignment()
  805. {
  806. float r = float(rand()) / RAND_MAX;
  807. return (size_t)1 << (size_t)(MAX_ALIGNMENT_LOG2 * r);
  808. }
  809. public:
  810. void SetUp() override
  811. {
  812. tr = (test_record*)AZ_OS_MALLOC(sizeof(test_record)*N, 8);
  813. MAX_SIZE = 4096;
  814. }
  815. void TearDown() override
  816. {
  817. AZ_OS_FREE(tr);
  818. }
  819. //////////////////////////////////////////////////////////////////////////
  820. // default allocator
  821. void defAllocate(unsigned iStart, unsigned iEnd)
  822. {
  823. for (unsigned i = iStart; i < iEnd; i++)
  824. {
  825. size_t size = rand_size();
  826. tr[i].ptr = AZ_OS_MALLOC(size, DefaultAlignment);
  827. }
  828. }
  829. void defFree(unsigned iStart, unsigned iEnd)
  830. {
  831. for (unsigned i = iStart; i < iEnd; i++)
  832. {
  833. unsigned j = i + rand() % (iEnd - i);
  834. AZ_OS_FREE(tr[j].ptr);
  835. tr[j].ptr = tr[i].ptr;
  836. }
  837. }
  838. void defAllocateSize(unsigned iStart, unsigned iEnd)
  839. {
  840. for (unsigned i = iStart; i < iEnd; i++)
  841. {
  842. size_t size = rand_size();
  843. tr[i].ptr = AZ_OS_MALLOC(size, DefaultAlignment);
  844. tr[i].size = size;
  845. }
  846. }
  847. void defFreeSize(unsigned iStart, unsigned iEnd)
  848. {
  849. for (unsigned i = iStart; i < iEnd; i++)
  850. {
  851. unsigned j = i + rand() % (iEnd - i);
  852. AZ_OS_FREE(tr[j].ptr /*, tr[j].size*/);
  853. tr[j].ptr = tr[i].ptr;
  854. tr[j].size = tr[i].size;
  855. }
  856. }
  857. void defAllocateAlignment(unsigned iStart, unsigned iEnd)
  858. {
  859. for (unsigned i = iStart; i < iEnd; i++)
  860. {
  861. size_t size = rand_size();
  862. size_t alignment = rand_alignment();
  863. tr[i].ptr = AZ_OS_MALLOC(size, alignment);
  864. }
  865. }
  866. void defFreeAlignment(unsigned iStart, unsigned iEnd)
  867. {
  868. for (unsigned i = iStart; i < iEnd; i++)
  869. {
  870. unsigned j = i + rand() % (iEnd - i);
  871. AZ_OS_FREE(tr[j].ptr);
  872. tr[j].ptr = tr[i].ptr;
  873. }
  874. }
  875. void defAllocateAlignmentSize(unsigned iStart, unsigned iEnd)
  876. {
  877. for (unsigned i = iStart; i < iEnd; i++)
  878. {
  879. size_t size = rand_size();
  880. size_t alignment = rand_alignment();
  881. tr[i].ptr = AZ_OS_MALLOC(size, alignment);
  882. tr[i].size = size;
  883. tr[i].alignment = alignment;
  884. }
  885. }
  886. void defFreeAlignmentSize(unsigned iStart, unsigned iEnd)
  887. {
  888. for (unsigned i = iStart; i < iEnd; i++)
  889. {
  890. unsigned j = i + rand() % (iEnd - i);
  891. AZ_OS_FREE(tr[j].ptr /*, tr[j].size, tr[j].alignment*/);
  892. tr[j].ptr = tr[i].ptr;
  893. tr[j].size = tr[i].size;
  894. tr[j].alignment = tr[i].alignment;
  895. }
  896. }
  897. //////////////////////////////////////////////////////////////////////////
  898. //////////////////////////////////////////////////////////////////////////
  899. // HPHA
  900. void hphaAllocate(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  901. {
  902. for (unsigned i = iStart; i < iEnd; i++)
  903. {
  904. size_t size = rand_size();
  905. tr[i].ptr = hpha.Allocate(size, DefaultAlignment, 0);
  906. }
  907. }
  908. void hphaFree(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  909. {
  910. for (unsigned i = iStart; i < iEnd; i++)
  911. {
  912. unsigned j = i + rand() % (iEnd - i);
  913. hpha.DeAllocate(tr[j].ptr);
  914. tr[j].ptr = tr[i].ptr;
  915. }
  916. }
  917. void hphaAllocateSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  918. {
  919. for (unsigned i = iStart; i < iEnd; i++)
  920. {
  921. size_t size = rand_size();
  922. tr[i].ptr = hpha.Allocate(size, DefaultAlignment, 0);
  923. tr[i].size = size;
  924. }
  925. }
  926. void hphaFreeSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  927. {
  928. for (unsigned i = iStart; i < iEnd; i++)
  929. {
  930. unsigned j = i + rand() % (iEnd - i);
  931. hpha.DeAllocate(tr[j].ptr, tr[j].size);
  932. tr[j].ptr = tr[i].ptr;
  933. tr[j].size = tr[i].size;
  934. }
  935. }
  936. void hphaAllocateAlignment(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  937. {
  938. for (unsigned i = iStart; i < iEnd; i++)
  939. {
  940. size_t size = rand_size();
  941. size_t alignment = rand_alignment();
  942. tr[i].ptr = hpha.Allocate(size, alignment, 0);
  943. }
  944. }
  945. void hphaFreeAlignment(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  946. {
  947. for (unsigned i = iStart; i < iEnd; i++)
  948. {
  949. unsigned j = i + rand() % (iEnd - i);
  950. hpha.DeAllocate(tr[j].ptr);
  951. tr[j].ptr = tr[i].ptr;
  952. }
  953. }
  954. void hphaAllocateAlignmentSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  955. {
  956. for (unsigned i = iStart; i < iEnd; i++)
  957. {
  958. size_t size = rand_size();
  959. size_t alignment = rand_alignment();
  960. tr[i].ptr = hpha.Allocate(size, alignment, 0);
  961. tr[i].size = size;
  962. tr[i].alignment = alignment;
  963. }
  964. }
  965. void hphaFreeAlignmentSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  966. {
  967. for (unsigned i = iStart; i < iEnd; i++)
  968. {
  969. unsigned j = i + rand() % (iEnd - i);
  970. hpha.DeAllocate(tr[j].ptr, tr[j].size, tr[j].alignment);
  971. tr[j].ptr = tr[i].ptr;
  972. tr[j].size = tr[i].size;
  973. tr[j].alignment = tr[i].alignment;
  974. }
  975. }
  976. //////////////////////////////////////////////////////////////////////////
  977. //////////////////////////////////////////////////////////////////////////
  978. // POOL
  979. void poolAllocate(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  980. {
  981. for (unsigned i = iStart; i < iEnd; i++)
  982. {
  983. size_t size = rand_size();
  984. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  985. }
  986. }
  987. void poolFree(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  988. {
  989. for (unsigned i = iStart; i < iEnd; i++)
  990. {
  991. unsigned j = i + rand() % (iEnd - i);
  992. pool.DeAllocate(tr[j].ptr, 0, 0);
  993. tr[j].ptr = tr[i].ptr;
  994. }
  995. }
  996. void poolAllocateSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  997. {
  998. for (unsigned i = iStart; i < iEnd; i++)
  999. {
  1000. size_t size = rand_size();
  1001. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  1002. tr[i].size = size;
  1003. }
  1004. }
  1005. void poolFreeSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1006. {
  1007. for (unsigned i = iStart; i < iEnd; i++)
  1008. {
  1009. unsigned j = i + rand() % (iEnd - i);
  1010. pool.DeAllocate(tr[j].ptr, tr[j].size, 0);
  1011. tr[j].ptr = tr[i].ptr;
  1012. tr[j].size = tr[i].size;
  1013. }
  1014. }
  1015. void poolAllocateAlignment(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1016. {
  1017. for (unsigned i = iStart; i < iEnd; i++)
  1018. {
  1019. size_t size = rand_size();
  1020. size_t alignment = rand_alignment();
  1021. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1022. }
  1023. }
  1024. void poolFreeAlignment(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1025. {
  1026. for (unsigned i = iStart; i < iEnd; i++)
  1027. {
  1028. unsigned j = i + rand() % (iEnd - i);
  1029. pool.DeAllocate(tr[j].ptr, 0, 0);
  1030. tr[j].ptr = tr[i].ptr;
  1031. }
  1032. }
  1033. void poolAllocateAlignmentSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1034. {
  1035. for (unsigned i = iStart; i < iEnd; i++)
  1036. {
  1037. size_t size = rand_size();
  1038. size_t alignment = rand_alignment();
  1039. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1040. tr[i].size = size;
  1041. tr[i].alignment = alignment;
  1042. }
  1043. }
  1044. void poolFreeAlignmentSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1045. {
  1046. for (unsigned i = iStart; i < iEnd; i++)
  1047. {
  1048. unsigned j = i + rand() % (iEnd - i);
  1049. pool.DeAllocate(tr[j].ptr, tr[j].size, tr[j].alignment);
  1050. tr[j].ptr = tr[i].ptr;
  1051. tr[j].size = tr[i].size;
  1052. tr[j].alignment = tr[i].alignment;
  1053. }
  1054. }
  1055. //////////////////////////////////////////////////////////////////////////
  1056. //////////////////////////////////////////////////////////////////////////
  1057. // THREAD POOL
  1058. void thPoolAllocate(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1059. {
  1060. for (unsigned i = iStart; i < iEnd; i++)
  1061. {
  1062. size_t size = rand_size();
  1063. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  1064. }
  1065. }
  1066. void thPoolFree(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1067. {
  1068. for (unsigned i = iStart; i < iEnd; i++)
  1069. {
  1070. unsigned j = i + rand() % (iEnd - i);
  1071. pool.DeAllocate(tr[j].ptr, 0, 0);
  1072. tr[j].ptr = tr[i].ptr;
  1073. }
  1074. }
  1075. void thPoolAllocateSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1076. {
  1077. for (unsigned i = iStart; i < iEnd; i++)
  1078. {
  1079. size_t size = rand_size();
  1080. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  1081. tr[i].size = size;
  1082. }
  1083. }
  1084. void thPoolFreeSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1085. {
  1086. for (unsigned i = iStart; i < iEnd; i++)
  1087. {
  1088. unsigned j = i + rand() % (iEnd - i);
  1089. pool.DeAllocate(tr[j].ptr, tr[j].size, 0);
  1090. tr[j].ptr = tr[i].ptr;
  1091. tr[j].size = tr[i].size;
  1092. }
  1093. }
  1094. void thPoolAllocateAlignment(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1095. {
  1096. for (unsigned i = iStart; i < iEnd; i++)
  1097. {
  1098. size_t size = rand_size();
  1099. size_t alignment = rand_alignment();
  1100. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1101. }
  1102. }
  1103. void thPoolFreeAlignment(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1104. {
  1105. for (unsigned i = iStart; i < iEnd; i++)
  1106. {
  1107. unsigned j = i + rand() % (iEnd - i);
  1108. pool.DeAllocate(tr[j].ptr, 0, 0);
  1109. tr[j].ptr = tr[i].ptr;
  1110. }
  1111. }
  1112. void thPoolAllocateAlignmentSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1113. {
  1114. for (unsigned i = iStart; i < iEnd; i++)
  1115. {
  1116. size_t size = rand_size();
  1117. size_t alignment = rand_alignment();
  1118. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1119. tr[i].size = size;
  1120. tr[i].alignment = alignment;
  1121. }
  1122. }
  1123. void thPoolFreeAlignmentSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1124. {
  1125. for (unsigned i = iStart; i < iEnd; i++)
  1126. {
  1127. unsigned j = i + rand() % (iEnd - i);
  1128. pool.DeAllocate(tr[j].ptr, tr[j].size, tr[j].alignment);
  1129. tr[j].ptr = tr[i].ptr;
  1130. tr[j].size = tr[i].size;
  1131. tr[j].alignment = tr[i].alignment;
  1132. }
  1133. }
  1134. //////////////////////////////////////////////////////////////////////////
  1135. void allocdealloc(HphaSchema& hpha, PoolSchema& pool, bool isHpha, bool isDefault, bool isPool)
  1136. {
  1137. AZStd::chrono::steady_clock::time_point start;
  1138. AZStd::chrono::duration<float> elapsed;
  1139. printf("MinSize %u MaxSize %u MaxAlignment %u\n", static_cast<unsigned int>(MIN_SIZE), static_cast<unsigned int>(MAX_SIZE), static_cast<unsigned int>(MAX_ALIGNMENT));
  1140. printf("\t\t\t\tHPHA\t\tDL\t\tDEFAULT\t\tPOOL\n");
  1141. printf("ALLOC/FREE:");
  1142. if (isHpha)
  1143. {
  1144. srand(1234);
  1145. start = AZStd::chrono::steady_clock::now();
  1146. hphaAllocate(hpha, 0, N);
  1147. elapsed = AZStd::chrono::steady_clock::now() - start;
  1148. printf("\t\t\t(%.3f/", elapsed.count());
  1149. start = AZStd::chrono::steady_clock::now();
  1150. hphaFree(hpha, 0, N);
  1151. elapsed = AZStd::chrono::steady_clock::now() - start;
  1152. printf("%.3f)", elapsed.count());
  1153. }
  1154. else
  1155. {
  1156. printf("\t\t\t(skip/skip)");
  1157. }
  1158. if (isDefault)
  1159. {
  1160. srand(1234);
  1161. start = AZStd::chrono::steady_clock::now();
  1162. defAllocate(0, N);
  1163. elapsed = AZStd::chrono::steady_clock::now() - start;
  1164. printf("\t(%.3f/", elapsed.count());
  1165. start = AZStd::chrono::steady_clock::now();
  1166. defFree(0, N);
  1167. elapsed = AZStd::chrono::steady_clock::now() - start;
  1168. printf("%.3f)", elapsed.count());
  1169. }
  1170. else
  1171. {
  1172. printf("\t(skip/skip)");
  1173. }
  1174. if (MAX_SIZE<=256)
  1175. {
  1176. if (isPool)
  1177. {
  1178. srand(1234);
  1179. start = AZStd::chrono::steady_clock::now();
  1180. poolAllocate(pool, 0, N);
  1181. elapsed = AZStd::chrono::steady_clock::now() - start;
  1182. printf("\t(%.3f/", elapsed.count());
  1183. start = AZStd::chrono::steady_clock::now();
  1184. poolFree(pool, 0, N);
  1185. elapsed = AZStd::chrono::steady_clock::now() - start;
  1186. printf("%.3f)\n", elapsed.count());
  1187. }
  1188. else
  1189. {
  1190. printf("\t(skip/skip)\n");
  1191. }
  1192. }
  1193. else
  1194. {
  1195. printf("\n");
  1196. }
  1197. printf("ALLOC/FREE(size):");
  1198. if (isHpha)
  1199. {
  1200. srand(1234);
  1201. start = AZStd::chrono::steady_clock::now();
  1202. hphaAllocateSize(hpha, 0, N);
  1203. elapsed = AZStd::chrono::steady_clock::now() - start;
  1204. printf("\t\t(%.3f/", elapsed.count());
  1205. start = AZStd::chrono::steady_clock::now();
  1206. hphaFreeSize(hpha, 0, N);
  1207. elapsed = AZStd::chrono::steady_clock::now() - start;
  1208. printf("%.3f)", elapsed.count());
  1209. }
  1210. else
  1211. {
  1212. printf("\t\t(skip/skip)");
  1213. }
  1214. if (isDefault)
  1215. {
  1216. srand(1234);
  1217. start = AZStd::chrono::steady_clock::now();
  1218. defAllocateSize(0, N);
  1219. elapsed = AZStd::chrono::steady_clock::now() - start;
  1220. printf("\t(%.3f/", elapsed.count());
  1221. start = AZStd::chrono::steady_clock::now();
  1222. defFreeSize(0, N);
  1223. elapsed = AZStd::chrono::steady_clock::now() - start;
  1224. printf("%.3f)", elapsed.count());
  1225. }
  1226. else
  1227. {
  1228. printf("\t(skip/skip)");
  1229. }
  1230. if (MAX_SIZE<=256)
  1231. {
  1232. if (isPool)
  1233. {
  1234. srand(1234);
  1235. start = AZStd::chrono::steady_clock::now();
  1236. poolAllocateSize(pool, 0, N);
  1237. elapsed = AZStd::chrono::steady_clock::now() - start;
  1238. printf("\t(%.3f/", elapsed.count());
  1239. start = AZStd::chrono::steady_clock::now();
  1240. poolFreeSize(pool, 0, N);
  1241. elapsed = AZStd::chrono::steady_clock::now() - start;
  1242. printf("%.3f)\n", elapsed.count());
  1243. }
  1244. else
  1245. {
  1246. printf("\t(skip/skip)\n");
  1247. }
  1248. }
  1249. else
  1250. {
  1251. printf("\n");
  1252. }
  1253. printf("ALLOC(align)/FREE:");
  1254. if (isHpha)
  1255. {
  1256. srand(1234);
  1257. start = AZStd::chrono::steady_clock::now();
  1258. hphaAllocateAlignment(hpha, 0, N);
  1259. elapsed = AZStd::chrono::steady_clock::now() - start;
  1260. printf("\t\t(%.3f/", elapsed.count());
  1261. start = AZStd::chrono::steady_clock::now();
  1262. hphaFreeAlignment(hpha, 0, N);
  1263. elapsed = AZStd::chrono::steady_clock::now() - start;
  1264. printf("%.3f)", elapsed.count());
  1265. }
  1266. else
  1267. {
  1268. printf("\t\t(skip/skip)");
  1269. }
  1270. if (isDefault)
  1271. {
  1272. srand(1234);
  1273. start = AZStd::chrono::steady_clock::now();
  1274. defAllocateAlignment(0, N);
  1275. elapsed = AZStd::chrono::steady_clock::now() - start;
  1276. printf("\t(%.3f/", elapsed.count());
  1277. start = AZStd::chrono::steady_clock::now();
  1278. defFreeAlignment(0, N);
  1279. elapsed = AZStd::chrono::steady_clock::now() - start;
  1280. printf("%.3f)", elapsed.count());
  1281. }
  1282. else
  1283. {
  1284. printf("\t(skip/skip)");
  1285. }
  1286. if (MAX_SIZE<=256)
  1287. {
  1288. if (isPool)
  1289. {
  1290. srand(1234);
  1291. start = AZStd::chrono::steady_clock::now();
  1292. poolAllocateAlignment(pool, 0, N);
  1293. elapsed = AZStd::chrono::steady_clock::now() - start;
  1294. printf("\t(%.3f/", elapsed.count());
  1295. start = AZStd::chrono::steady_clock::now();
  1296. poolFreeAlignment(pool, 0, N);
  1297. elapsed = AZStd::chrono::steady_clock::now() - start;
  1298. printf("%.3f)\n", elapsed.count());
  1299. }
  1300. else
  1301. {
  1302. printf("\t(skip/skip)\n");
  1303. }
  1304. }
  1305. else
  1306. {
  1307. printf("\n");
  1308. }
  1309. printf("ALLOC(align)/FREE(size,align):");
  1310. if (isHpha)
  1311. {
  1312. srand(1234);
  1313. start = AZStd::chrono::steady_clock::now();
  1314. hphaAllocateAlignmentSize(hpha, 0, N);
  1315. elapsed = AZStd::chrono::steady_clock::now() - start;
  1316. printf("\t(%.3f/", elapsed.count());
  1317. start = AZStd::chrono::steady_clock::now();
  1318. hphaFreeAlignmentSize(hpha, 0, N);
  1319. elapsed = AZStd::chrono::steady_clock::now() - start;
  1320. printf("%.3f)", elapsed.count());
  1321. }
  1322. else
  1323. {
  1324. printf("\t(skip/skip)");
  1325. }
  1326. if (isDefault)
  1327. {
  1328. srand(1234);
  1329. start = AZStd::chrono::steady_clock::now();
  1330. defAllocateAlignmentSize(0, N);
  1331. elapsed = AZStd::chrono::steady_clock::now() - start;
  1332. printf("\t(%.3f/", elapsed.count());
  1333. start = AZStd::chrono::steady_clock::now();
  1334. defFreeAlignmentSize(0, N);
  1335. elapsed = AZStd::chrono::steady_clock::now() - start;
  1336. printf("%.3f)", elapsed.count());
  1337. }
  1338. else
  1339. {
  1340. printf("\t(skip/skip)");
  1341. }
  1342. if (MAX_SIZE<=256)
  1343. {
  1344. if (isPool)
  1345. {
  1346. srand(1234);
  1347. start = AZStd::chrono::steady_clock::now();
  1348. poolAllocateAlignmentSize(pool, 0, N);
  1349. elapsed = AZStd::chrono::steady_clock::now() - start;
  1350. printf("\t(%.3f/", elapsed.count());
  1351. start = AZStd::chrono::steady_clock::now();
  1352. poolFreeAlignmentSize(pool, 0, N);
  1353. elapsed = AZStd::chrono::steady_clock::now() - start;
  1354. printf("%.3f)\n", elapsed.count());
  1355. }
  1356. else
  1357. {
  1358. printf("\t(skip/skip)\n");
  1359. }
  1360. }
  1361. else
  1362. {
  1363. printf("\n");
  1364. }
  1365. printf("TEST REALLOC:");
  1366. srand(1234);
  1367. start = AZStd::chrono::steady_clock::now();
  1368. for (unsigned i = 0; i < N; i++) {
  1369. size_t size = rand_size();
  1370. tr[i].ptr = hpha.ReAllocate(nullptr, size, 1);
  1371. }
  1372. for (unsigned i = 0; i < N; i++) {
  1373. size_t size = rand_size();
  1374. tr[i].ptr = hpha.ReAllocate(tr[i].ptr, size, 1);
  1375. }
  1376. for (unsigned i = 0; i < N; i++) {
  1377. unsigned j = i + rand() % (N - i);
  1378. hpha.ReAllocate(tr[j].ptr, 0, 0);
  1379. tr[j].ptr = tr[i].ptr;
  1380. }
  1381. elapsed = AZStd::chrono::steady_clock::now() - start;
  1382. printf("\t\t\t\t\t(%.3f)", elapsed.count());
  1383. srand(1234);
  1384. start = AZStd::chrono::steady_clock::now();
  1385. for (unsigned i = 0; i < N; i++) {
  1386. size_t size = rand_size();
  1387. tr[i].ptr = realloc(NULL, size);
  1388. }
  1389. for (unsigned i = 0; i < N; i++) {
  1390. size_t size = rand_size();
  1391. tr[i].ptr = realloc(tr[i].ptr, size);
  1392. }
  1393. for (unsigned i = 0; i < N; i++) {
  1394. unsigned j = i + rand() % (N - i);
  1395. realloc(tr[j].ptr, 0);
  1396. tr[j].ptr = tr[i].ptr;
  1397. }
  1398. elapsed = AZStd::chrono::steady_clock::now() - start;
  1399. printf("\t(%.3f)\n", elapsed.count());
  1400. printf("TEST REALLOC with ALIGNMENT:");
  1401. srand(1234);
  1402. start = AZStd::chrono::steady_clock::now();
  1403. for (unsigned i = 0; i < N; i++) {
  1404. size_t size = rand_size();
  1405. size_t alignment = rand_alignment();
  1406. tr[i].ptr = hpha.ReAllocate(NULL, size, alignment);
  1407. }
  1408. for (unsigned i = 0; i < N; i++) {
  1409. size_t size = rand_size();
  1410. size_t alignment = rand_alignment();
  1411. tr[i].ptr = hpha.ReAllocate(tr[i].ptr, size, alignment);
  1412. }
  1413. for (unsigned i = 0; i < N; i++) {
  1414. unsigned j = i + rand() % (N - i);
  1415. hpha.ReAllocate(tr[j].ptr, 0, 0);
  1416. tr[j].ptr = tr[i].ptr;
  1417. }
  1418. elapsed = AZStd::chrono::steady_clock::now() - start;
  1419. printf("\t\t\t(%.3f)", elapsed.count());
  1420. srand(1234);
  1421. start = AZStd::chrono::steady_clock::now();
  1422. #ifdef AZ_PLATFORM_WINDOWS // Windows test only
  1423. for (unsigned i = 0; i < N; i++) {
  1424. size_t size = rand_size();
  1425. size_t alignment = rand_alignment();
  1426. tr[i].ptr = _aligned_realloc(NULL, size, alignment);
  1427. }
  1428. for (unsigned i = 0; i < N; i++) {
  1429. size_t size = rand_size();
  1430. size_t alignment = rand_alignment();
  1431. tr[i].ptr = _aligned_realloc(tr[i].ptr, size, alignment);
  1432. }
  1433. for (unsigned i = 0; i < N; i++) {
  1434. unsigned j = i + rand() % (N - i);
  1435. realloc(tr[j].ptr, 0);
  1436. tr[j].ptr = tr[i].ptr;
  1437. }
  1438. #endif // AZ_PLATFORM_WINDOWS
  1439. elapsed = AZStd::chrono::steady_clock::now() - start;
  1440. printf("\t(%.3f)\n", elapsed.count());
  1441. }
  1442. void allocdeallocThread(HphaSchema& hpha, ThreadPoolSchema& thPool, bool isHpha, bool isDefault, bool isPool)
  1443. {
  1444. AZStd::chrono::steady_clock::time_point start;
  1445. AZStd::chrono::duration<float> elapsed;
  1446. printf("MinSize %u MaxSize %u MaxAlignment %u\n", static_cast<unsigned int>(MIN_SIZE), static_cast<unsigned int>(MAX_SIZE), static_cast<unsigned int>(MAX_ALIGNMENT));
  1447. printf("\t\t\t\tHPHA\t\tDL\t\tDEFAULT\t\tPOOL\n");
  1448. static const int TN = N / 4;
  1449. {
  1450. printf("ALLOC/FREE:");
  1451. if (isHpha)
  1452. {
  1453. srand(1234);
  1454. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1455. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1456. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1457. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1458. start = AZStd::chrono::steady_clock::now();
  1459. tr1.join();
  1460. tr2.join();
  1461. tr3.join();
  1462. tr4.join();
  1463. elapsed = AZStd::chrono::steady_clock::now() - start;
  1464. printf("\t\t\t(%.3f/", elapsed.count());
  1465. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1466. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1467. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1468. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1469. start = AZStd::chrono::steady_clock::now();
  1470. tr5.join();
  1471. tr6.join();
  1472. tr7.join();
  1473. tr8.join();
  1474. elapsed = AZStd::chrono::steady_clock::now() - start;
  1475. printf("%.3f)", elapsed.count());
  1476. }
  1477. else
  1478. {
  1479. printf("\t\t\t(skip/skip)");
  1480. }
  1481. }
  1482. {
  1483. if (isDefault)
  1484. {
  1485. srand(1234);
  1486. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 0 * TN, 1 * TN));
  1487. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 1 * TN, 2 * TN));
  1488. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 2 * TN, 3 * TN));
  1489. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 3 * TN, 4 * TN));
  1490. start = AZStd::chrono::steady_clock::now();
  1491. tr1.join();
  1492. tr2.join();
  1493. tr3.join();
  1494. tr4.join();
  1495. elapsed = AZStd::chrono::steady_clock::now() - start;
  1496. printf("\t(%.3f/", elapsed.count());
  1497. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 0 * TN, 1 * TN));
  1498. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 1 * TN, 2 * TN));
  1499. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 2 * TN, 3 * TN));
  1500. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 3 * TN, 4 * TN));
  1501. start = AZStd::chrono::steady_clock::now();
  1502. tr5.join();
  1503. tr6.join();
  1504. tr7.join();
  1505. tr8.join();
  1506. elapsed = AZStd::chrono::steady_clock::now() - start;
  1507. printf("\t(%.3f)", elapsed.count());
  1508. }
  1509. else
  1510. {
  1511. printf("\t(skip/skip)");
  1512. }
  1513. }
  1514. if (MAX_SIZE<=256)
  1515. {
  1516. if (isPool)
  1517. {
  1518. srand(1234);
  1519. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1520. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1521. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1522. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1523. start = AZStd::chrono::steady_clock::now();
  1524. tr1.join();
  1525. tr2.join();
  1526. tr3.join();
  1527. tr4.join();
  1528. elapsed = AZStd::chrono::steady_clock::now() - start;
  1529. printf("\t(%.3f/", elapsed.count());
  1530. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1531. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1532. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1533. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1534. start = AZStd::chrono::steady_clock::now();
  1535. tr5.join();
  1536. tr6.join();
  1537. tr7.join();
  1538. tr8.join();
  1539. elapsed = AZStd::chrono::steady_clock::now() - start;
  1540. printf("%.3f)\n", elapsed.count());
  1541. }
  1542. else
  1543. {
  1544. printf("\t(skip/skip)\n");
  1545. }
  1546. }
  1547. else
  1548. {
  1549. printf("\n");
  1550. }
  1551. {
  1552. printf("ALLOC/FREE(size):");
  1553. if (isHpha)
  1554. {
  1555. srand(1234);
  1556. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1557. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1558. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1559. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1560. start = AZStd::chrono::steady_clock::now();
  1561. tr1.join();
  1562. tr2.join();
  1563. tr3.join();
  1564. tr4.join();
  1565. elapsed = AZStd::chrono::steady_clock::now() - start;
  1566. printf("\t\t(%.3f/", elapsed.count());
  1567. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1568. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1569. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1570. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1571. start = AZStd::chrono::steady_clock::now();
  1572. tr5.join();
  1573. tr6.join();
  1574. tr7.join();
  1575. tr8.join();
  1576. elapsed = AZStd::chrono::steady_clock::now() - start;
  1577. printf("/%.3f)", elapsed.count());
  1578. }
  1579. else
  1580. {
  1581. printf("\t\t(skip/skip)");
  1582. }
  1583. }
  1584. {
  1585. if (isDefault)
  1586. {
  1587. srand(1234);
  1588. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 0 * TN, 1 * TN));
  1589. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 1 * TN, 2 * TN));
  1590. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 2 * TN, 3 * TN));
  1591. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 3 * TN, 4 * TN));
  1592. start = AZStd::chrono::steady_clock::now();
  1593. tr1.join();
  1594. tr2.join();
  1595. tr3.join();
  1596. tr4.join();
  1597. elapsed = AZStd::chrono::steady_clock::now() - start;
  1598. printf("\t(%.3f/", elapsed.count());
  1599. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 0 * TN, 1 * TN));
  1600. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 1 * TN, 2 * TN));
  1601. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 2 * TN, 3 * TN));
  1602. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 3 * TN, 4 * TN));
  1603. start = AZStd::chrono::steady_clock::now();
  1604. tr5.join();
  1605. tr6.join();
  1606. tr7.join();
  1607. tr8.join();
  1608. elapsed = AZStd::chrono::steady_clock::now() - start;
  1609. printf("%.3f)", elapsed.count());
  1610. }
  1611. else
  1612. {
  1613. printf("\t(skip/skip)");
  1614. }
  1615. }
  1616. if (MAX_SIZE<=256)
  1617. {
  1618. if (isPool)
  1619. {
  1620. srand(1234);
  1621. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1622. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1623. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1624. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1625. start = AZStd::chrono::steady_clock::now();
  1626. tr1.join();
  1627. tr2.join();
  1628. tr3.join();
  1629. tr4.join();
  1630. elapsed = AZStd::chrono::steady_clock::now() - start;
  1631. printf("\t(%.3f/", elapsed.count());
  1632. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1633. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1634. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1635. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1636. start = AZStd::chrono::steady_clock::now();
  1637. tr5.join();
  1638. tr6.join();
  1639. tr7.join();
  1640. tr8.join();
  1641. elapsed = AZStd::chrono::steady_clock::now() - start;
  1642. printf("%.3f)\n", elapsed.count());
  1643. }
  1644. else
  1645. {
  1646. printf("\t(skip/skip)\n");
  1647. }
  1648. }
  1649. else
  1650. {
  1651. printf("\n");
  1652. }
  1653. {
  1654. printf("ALLOC(align)/FREE:");
  1655. if (isHpha)
  1656. {
  1657. srand(1234);
  1658. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1659. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1660. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1661. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1662. start = AZStd::chrono::steady_clock::now();
  1663. tr1.join();
  1664. tr2.join();
  1665. tr3.join();
  1666. tr4.join();
  1667. elapsed = AZStd::chrono::steady_clock::now() - start;
  1668. printf("\t\t(%.3f/", elapsed.count());
  1669. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1670. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1671. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1672. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1673. start = AZStd::chrono::steady_clock::now();
  1674. tr5.join();
  1675. tr6.join();
  1676. tr7.join();
  1677. tr8.join();
  1678. elapsed = AZStd::chrono::steady_clock::now() - start;
  1679. printf("%.3f)", elapsed.count());
  1680. }
  1681. else
  1682. {
  1683. printf("\t\t(skip/skip)");
  1684. }
  1685. }
  1686. {
  1687. if (isDefault)
  1688. {
  1689. srand(1234);
  1690. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 0 * TN, 1 * TN));
  1691. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 1 * TN, 2 * TN));
  1692. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 2 * TN, 3 * TN));
  1693. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 3 * TN, 4 * TN));
  1694. start = AZStd::chrono::steady_clock::now();
  1695. tr1.join();
  1696. tr2.join();
  1697. tr3.join();
  1698. tr4.join();
  1699. elapsed = AZStd::chrono::steady_clock::now() - start;
  1700. printf("\t(%.3f/", elapsed.count());
  1701. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 0 * TN, 1 * TN));
  1702. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 1 * TN, 2 * TN));
  1703. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 2 * TN, 3 * TN));
  1704. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 3 * TN, 4 * TN));
  1705. start = AZStd::chrono::steady_clock::now();
  1706. tr5.join();
  1707. tr6.join();
  1708. tr7.join();
  1709. tr8.join();
  1710. elapsed = AZStd::chrono::steady_clock::now() - start;
  1711. printf("%.3f)", elapsed.count());
  1712. }
  1713. else
  1714. {
  1715. printf("\t(skip/skip)");
  1716. }
  1717. }
  1718. if (MAX_SIZE<=256)
  1719. {
  1720. if (isPool)
  1721. {
  1722. srand(1234);
  1723. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1724. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1725. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1726. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1727. start = AZStd::chrono::steady_clock::now();
  1728. tr1.join();
  1729. tr2.join();
  1730. tr3.join();
  1731. tr4.join();
  1732. elapsed = AZStd::chrono::steady_clock::now() - start;
  1733. printf("\t(%.3f/", elapsed.count());
  1734. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1735. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1736. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1737. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1738. start = AZStd::chrono::steady_clock::now();
  1739. tr5.join();
  1740. tr6.join();
  1741. tr7.join();
  1742. tr8.join();
  1743. elapsed = AZStd::chrono::steady_clock::now() - start;
  1744. printf("%.3f)\n", elapsed.count());
  1745. }
  1746. else
  1747. {
  1748. printf("\t(skip/skip)\n");
  1749. }
  1750. }
  1751. else
  1752. {
  1753. printf("\n");
  1754. }
  1755. {
  1756. printf("ALLOC(align)/FREE(size,align):");
  1757. if (isHpha)
  1758. {
  1759. srand(1234);
  1760. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1761. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1762. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1763. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1764. start = AZStd::chrono::steady_clock::now();
  1765. tr1.join();
  1766. tr2.join();
  1767. tr3.join();
  1768. tr4.join();
  1769. elapsed = AZStd::chrono::steady_clock::now() - start;
  1770. printf("\t(%.3f/", elapsed.count());
  1771. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1772. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1773. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1774. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1775. start = AZStd::chrono::steady_clock::now();
  1776. tr5.join();
  1777. tr6.join();
  1778. tr7.join();
  1779. tr8.join();
  1780. elapsed = AZStd::chrono::steady_clock::now() - start;
  1781. printf("%.3f)", elapsed.count());
  1782. }
  1783. else
  1784. {
  1785. printf("\t(skip/skip)");
  1786. }
  1787. }
  1788. {
  1789. if (isDefault)
  1790. {
  1791. srand(1234);
  1792. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 0 * TN, 1 * TN));
  1793. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 1 * TN, 2 * TN));
  1794. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 2 * TN, 3 * TN));
  1795. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 3 * TN, 4 * TN));
  1796. start = AZStd::chrono::steady_clock::now();
  1797. tr1.join();
  1798. tr2.join();
  1799. tr3.join();
  1800. tr4.join();
  1801. elapsed = AZStd::chrono::steady_clock::now() - start;
  1802. printf("\t(%.3f/", elapsed.count());
  1803. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 0 * TN, 1 * TN));
  1804. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 1 * TN, 2 * TN));
  1805. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 2 * TN, 3 * TN));
  1806. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 3 * TN, 4 * TN));
  1807. start = AZStd::chrono::steady_clock::now();
  1808. tr5.join();
  1809. tr6.join();
  1810. tr7.join();
  1811. tr8.join();
  1812. elapsed = AZStd::chrono::steady_clock::now() - start;
  1813. printf("%.3f)", elapsed.count());
  1814. }
  1815. else
  1816. {
  1817. printf("\t(skip/skip)");
  1818. }
  1819. }
  1820. if (MAX_SIZE<=256)
  1821. {
  1822. if (isPool)
  1823. {
  1824. srand(1234);
  1825. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1826. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1827. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1828. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1829. start = AZStd::chrono::steady_clock::now();
  1830. tr1.join();
  1831. tr2.join();
  1832. tr3.join();
  1833. tr4.join();
  1834. elapsed = AZStd::chrono::steady_clock::now() - start;
  1835. printf("\t(%.3f/", elapsed.count());
  1836. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1837. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1838. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1839. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1840. start = AZStd::chrono::steady_clock::now();
  1841. tr5.join();
  1842. tr6.join();
  1843. tr7.join();
  1844. tr8.join();
  1845. elapsed = AZStd::chrono::steady_clock::now() - start;
  1846. printf("%.3f)\n", elapsed.count());
  1847. }
  1848. else
  1849. {
  1850. printf("\t(skip/skip)\n");
  1851. }
  1852. }
  1853. else
  1854. {
  1855. printf("\n");
  1856. }
  1857. }
  1858. void run()
  1859. {
  1860. printf("\n\t\t\t=======================\n");
  1861. printf("\t\t\tSchemas Benchmark Test!\n");
  1862. printf("\t\t\t=======================\n");
  1863. {
  1864. // TODO Switch to using instance of HphaAllocator with a sub allocator of a fixed size
  1865. {
  1866. HphaSchema hpha;
  1867. PoolSchema pool;
  1868. pool.Create();
  1869. ThreadPoolSchemaHelper<nullptr_t> threadPool;
  1870. threadPool.Create();
  1871. printf("---- Single Thread ----\n");
  1872. // any allocations
  1873. MAX_SIZE = 4096;
  1874. allocdealloc(hpha, pool, false, false, true);
  1875. printf("\n");
  1876. // pool allocations
  1877. MAX_SIZE = 256;
  1878. allocdealloc(hpha, pool, false, false, true);
  1879. // threads
  1880. printf("\n---- 4 Threads ----\n");
  1881. // any allocations
  1882. MAX_SIZE = 4096;
  1883. //allocdeallocThread(hpha,threadPool,true,true,true);
  1884. printf("\n");
  1885. // pool allocations
  1886. MAX_SIZE = 256;
  1887. //allocdeallocThread(hpha,threadPool,true,true,true);
  1888. }
  1889. }
  1890. #if AZ_TRAIT_UNITTEST_NON_PREALLOCATED_HPHA_TEST
  1891. printf("\n\t\t\tNO prealocated memory!\n");
  1892. {
  1893. HphaSchema hpha;
  1894. PoolSchema pool;
  1895. pool.Create();
  1896. ThreadPoolSchemaHelper<nullptr_t> threadPool;
  1897. threadPool.Create();
  1898. printf("---- Single Thread ----\n");
  1899. // any allocations
  1900. MAX_SIZE = 4096;
  1901. allocdealloc(hpha,pool,true,true,true);
  1902. printf("\n");
  1903. // pool allocations
  1904. MAX_SIZE = 256;
  1905. allocdealloc(hpha,pool,true,true,true);
  1906. // threads
  1907. printf("\n---- 4 Threads ----\n");
  1908. // any allocations
  1909. MAX_SIZE = 4096;
  1910. allocdeallocThread(hpha,threadPool,true,true,true);
  1911. printf("\n");
  1912. // pool allocations
  1913. MAX_SIZE = 256;
  1914. allocdeallocThread(hpha,threadPool,true,true,true);
  1915. }
  1916. #endif
  1917. }
  1918. };
  1919. #ifdef ENABLE_PERFORMANCE_TEST
  1920. TEST_F(PERF_MemoryBenchmark, Test)
  1921. {
  1922. run();
  1923. }
  1924. #endif // ENABLE_PERFORMANCE_TEST
  1925. #endif // AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE
  1926. }
  1927. // GlobalNewDeleteTest-End