Memory.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  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 UnitTest
  615. {
  616. TEST_F(ThreadPoolAllocatorTest, Test)
  617. {
  618. run();
  619. }
  620. /**
  621. * Tests azmalloc,azmallocex/azfree.
  622. */
  623. class AZMallocTest
  624. : public MemoryTrackingFixture
  625. {
  626. public:
  627. void run()
  628. {
  629. IAllocator& sysAllocator = AllocatorInstance<SystemAllocator>::Get();
  630. IAllocator& poolAllocator = AllocatorInstance<PoolAllocator>::Get();
  631. void* ptr = azmalloc(16*1024, 32, SystemAllocator);
  632. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  633. if (sysAllocator.GetRecords())
  634. {
  635. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  636. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  637. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  638. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  639. }
  640. azfree(ptr, SystemAllocator);
  641. if (sysAllocator.GetRecords())
  642. {
  643. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  644. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  645. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  646. }
  647. ptr = azmalloc(16*1024, 32, SystemAllocator);
  648. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  649. if (sysAllocator.GetRecords())
  650. {
  651. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  652. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  653. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  654. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  655. }
  656. azfree(ptr, SystemAllocator);
  657. if (sysAllocator.GetRecords())
  658. {
  659. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  660. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  661. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  662. }
  663. ptr = azmalloc(16, 32, PoolAllocator);
  664. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  665. if (poolAllocator.GetRecords())
  666. {
  667. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  668. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  669. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  670. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  671. }
  672. azfree(ptr, PoolAllocator);
  673. if (poolAllocator.GetRecords())
  674. {
  675. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  676. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  677. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  678. }
  679. }
  680. };
  681. TEST_F(AZMallocTest, Test)
  682. {
  683. run();
  684. }
  685. /**
  686. * Tests aznew/delete, azcreate/azdestroy
  687. */
  688. class AZNewCreateDestroyTest
  689. : public MemoryTrackingFixture
  690. {
  691. class MyClass
  692. {
  693. public:
  694. AZ_CLASS_ALLOCATOR(MyClass, PoolAllocator);
  695. MyClass(int data = 303)
  696. : m_data(data) {}
  697. ~MyClass() {}
  698. alignas(32) int m_data;
  699. };
  700. // Explicitly doesn't have AZ_CLASS_ALLOCATOR
  701. class MyDerivedClass
  702. : public MyClass
  703. {
  704. public:
  705. MyDerivedClass() = default;
  706. };
  707. public:
  708. void run()
  709. {
  710. IAllocator& sysAllocator = AllocatorInstance<SystemAllocator>::Get();
  711. IAllocator& poolAllocator = AllocatorInstance<PoolAllocator>::Get();
  712. MyClass* ptr = aznew MyClass(202); /// this should allocate memory from the pool allocator
  713. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  714. EXPECT_EQ(202, ptr->m_data); // check value
  715. if (poolAllocator.GetRecords())
  716. {
  717. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  718. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  719. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  720. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  721. }
  722. delete ptr;
  723. if (poolAllocator.GetRecords())
  724. {
  725. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  726. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  727. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  728. }
  729. // now use the azcreate to allocate the object wherever we want
  730. ptr = azcreate(MyClass, (101), SystemAllocator);
  731. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  732. EXPECT_EQ(101, ptr->m_data); // check value
  733. if (sysAllocator.GetRecords())
  734. {
  735. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  736. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  737. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  738. EXPECT_TRUE(iter!=records.end()); // our allocation is in the list
  739. }
  740. azdestroy(ptr, SystemAllocator);
  741. if (sysAllocator.GetRecords())
  742. {
  743. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*sysAllocator.GetRecords());
  744. const Debug::AllocationRecordsType& records = sysAllocator.GetRecords()->GetMap();
  745. EXPECT_TRUE(records.find(ptr)==records.end()); // our allocation is NOT in the list
  746. }
  747. // Test creation of derived classes
  748. ptr = aznew MyDerivedClass(); /// this should allocate memory from the pool allocator
  749. EXPECT_EQ(0, ((size_t)ptr & 31)); // check alignment
  750. EXPECT_EQ(303, ptr->m_data); // check value
  751. if (poolAllocator.GetRecords())
  752. {
  753. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  754. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  755. Debug::AllocationRecordsType::const_iterator iter = records.find(ptr);
  756. EXPECT_TRUE(iter != records.end()); // our allocation is in the list
  757. }
  758. delete ptr;
  759. if (poolAllocator.GetRecords())
  760. {
  761. AZStd::lock_guard<AZ::Debug::AllocationRecords> lock(*poolAllocator.GetRecords());
  762. const Debug::AllocationRecordsType& records = poolAllocator.GetRecords()->GetMap();
  763. EXPECT_TRUE(records.find(ptr) == records.end()); // our allocation is NOT in the list
  764. }
  765. }
  766. };
  767. TEST_F(AZNewCreateDestroyTest, Test)
  768. {
  769. run();
  770. }
  771. #if AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE
  772. class PERF_MemoryBenchmark
  773. : public ::testing::Test
  774. {
  775. #if defined(_DEBUG)
  776. static const unsigned N = 1024 * 128;
  777. #else
  778. static const unsigned N = 1024 * 512;
  779. #endif
  780. static const size_t DefaultAlignment = 1;
  781. static const size_t MIN_SIZE = 2;
  782. size_t MAX_SIZE;
  783. static const size_t MAX_ALIGNMENT_LOG2 = 7;
  784. static const size_t MAX_ALIGNMENT = 1 << MAX_ALIGNMENT_LOG2;
  785. struct test_record
  786. {
  787. void* ptr;
  788. size_t size;
  789. size_t alignment;
  790. size_t _padding;
  791. }* tr /*[N]*/;
  792. size_t rand_size()
  793. {
  794. float r = float(rand()) / RAND_MAX;
  795. return MIN_SIZE + (size_t)((MAX_SIZE - MIN_SIZE) * powf(r, 8.0f));
  796. }
  797. size_t rand_alignment()
  798. {
  799. float r = float(rand()) / RAND_MAX;
  800. return (size_t)1 << (size_t)(MAX_ALIGNMENT_LOG2 * r);
  801. }
  802. public:
  803. void SetUp() override
  804. {
  805. tr = (test_record*)AZ_OS_MALLOC(sizeof(test_record)*N, 8);
  806. MAX_SIZE = 4096;
  807. }
  808. void TearDown() override
  809. {
  810. AZ_OS_FREE(tr);
  811. }
  812. //////////////////////////////////////////////////////////////////////////
  813. // default allocator
  814. void defAllocate(unsigned iStart, unsigned iEnd)
  815. {
  816. for (unsigned i = iStart; i < iEnd; i++)
  817. {
  818. size_t size = rand_size();
  819. tr[i].ptr = AZ_OS_MALLOC(size, DefaultAlignment);
  820. }
  821. }
  822. void defFree(unsigned iStart, unsigned iEnd)
  823. {
  824. for (unsigned i = iStart; i < iEnd; i++)
  825. {
  826. unsigned j = i + rand() % (iEnd - i);
  827. AZ_OS_FREE(tr[j].ptr);
  828. tr[j].ptr = tr[i].ptr;
  829. }
  830. }
  831. void defAllocateSize(unsigned iStart, unsigned iEnd)
  832. {
  833. for (unsigned i = iStart; i < iEnd; i++)
  834. {
  835. size_t size = rand_size();
  836. tr[i].ptr = AZ_OS_MALLOC(size, DefaultAlignment);
  837. tr[i].size = size;
  838. }
  839. }
  840. void defFreeSize(unsigned iStart, unsigned iEnd)
  841. {
  842. for (unsigned i = iStart; i < iEnd; i++)
  843. {
  844. unsigned j = i + rand() % (iEnd - i);
  845. AZ_OS_FREE(tr[j].ptr /*, tr[j].size*/);
  846. tr[j].ptr = tr[i].ptr;
  847. tr[j].size = tr[i].size;
  848. }
  849. }
  850. void defAllocateAlignment(unsigned iStart, unsigned iEnd)
  851. {
  852. for (unsigned i = iStart; i < iEnd; i++)
  853. {
  854. size_t size = rand_size();
  855. size_t alignment = rand_alignment();
  856. tr[i].ptr = AZ_OS_MALLOC(size, alignment);
  857. }
  858. }
  859. void defFreeAlignment(unsigned iStart, unsigned iEnd)
  860. {
  861. for (unsigned i = iStart; i < iEnd; i++)
  862. {
  863. unsigned j = i + rand() % (iEnd - i);
  864. AZ_OS_FREE(tr[j].ptr);
  865. tr[j].ptr = tr[i].ptr;
  866. }
  867. }
  868. void defAllocateAlignmentSize(unsigned iStart, unsigned iEnd)
  869. {
  870. for (unsigned i = iStart; i < iEnd; i++)
  871. {
  872. size_t size = rand_size();
  873. size_t alignment = rand_alignment();
  874. tr[i].ptr = AZ_OS_MALLOC(size, alignment);
  875. tr[i].size = size;
  876. tr[i].alignment = alignment;
  877. }
  878. }
  879. void defFreeAlignmentSize(unsigned iStart, unsigned iEnd)
  880. {
  881. for (unsigned i = iStart; i < iEnd; i++)
  882. {
  883. unsigned j = i + rand() % (iEnd - i);
  884. AZ_OS_FREE(tr[j].ptr /*, tr[j].size, tr[j].alignment*/);
  885. tr[j].ptr = tr[i].ptr;
  886. tr[j].size = tr[i].size;
  887. tr[j].alignment = tr[i].alignment;
  888. }
  889. }
  890. //////////////////////////////////////////////////////////////////////////
  891. //////////////////////////////////////////////////////////////////////////
  892. // HPHA
  893. void hphaAllocate(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  894. {
  895. for (unsigned i = iStart; i < iEnd; i++)
  896. {
  897. size_t size = rand_size();
  898. tr[i].ptr = hpha.Allocate(size, DefaultAlignment, 0);
  899. }
  900. }
  901. void hphaFree(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  902. {
  903. for (unsigned i = iStart; i < iEnd; i++)
  904. {
  905. unsigned j = i + rand() % (iEnd - i);
  906. hpha.DeAllocate(tr[j].ptr);
  907. tr[j].ptr = tr[i].ptr;
  908. }
  909. }
  910. void hphaAllocateSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  911. {
  912. for (unsigned i = iStart; i < iEnd; i++)
  913. {
  914. size_t size = rand_size();
  915. tr[i].ptr = hpha.Allocate(size, DefaultAlignment, 0);
  916. tr[i].size = size;
  917. }
  918. }
  919. void hphaFreeSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  920. {
  921. for (unsigned i = iStart; i < iEnd; i++)
  922. {
  923. unsigned j = i + rand() % (iEnd - i);
  924. hpha.DeAllocate(tr[j].ptr, tr[j].size);
  925. tr[j].ptr = tr[i].ptr;
  926. tr[j].size = tr[i].size;
  927. }
  928. }
  929. void hphaAllocateAlignment(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  930. {
  931. for (unsigned i = iStart; i < iEnd; i++)
  932. {
  933. size_t size = rand_size();
  934. size_t alignment = rand_alignment();
  935. tr[i].ptr = hpha.Allocate(size, alignment, 0);
  936. }
  937. }
  938. void hphaFreeAlignment(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  939. {
  940. for (unsigned i = iStart; i < iEnd; i++)
  941. {
  942. unsigned j = i + rand() % (iEnd - i);
  943. hpha.DeAllocate(tr[j].ptr);
  944. tr[j].ptr = tr[i].ptr;
  945. }
  946. }
  947. void hphaAllocateAlignmentSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  948. {
  949. for (unsigned i = iStart; i < iEnd; i++)
  950. {
  951. size_t size = rand_size();
  952. size_t alignment = rand_alignment();
  953. tr[i].ptr = hpha.Allocate(size, alignment, 0);
  954. tr[i].size = size;
  955. tr[i].alignment = alignment;
  956. }
  957. }
  958. void hphaFreeAlignmentSize(HphaSchema& hpha, unsigned iStart, unsigned iEnd)
  959. {
  960. for (unsigned i = iStart; i < iEnd; i++)
  961. {
  962. unsigned j = i + rand() % (iEnd - i);
  963. hpha.DeAllocate(tr[j].ptr, tr[j].size, tr[j].alignment);
  964. tr[j].ptr = tr[i].ptr;
  965. tr[j].size = tr[i].size;
  966. tr[j].alignment = tr[i].alignment;
  967. }
  968. }
  969. //////////////////////////////////////////////////////////////////////////
  970. //////////////////////////////////////////////////////////////////////////
  971. // POOL
  972. void poolAllocate(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  973. {
  974. for (unsigned i = iStart; i < iEnd; i++)
  975. {
  976. size_t size = rand_size();
  977. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  978. }
  979. }
  980. void poolFree(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  981. {
  982. for (unsigned i = iStart; i < iEnd; i++)
  983. {
  984. unsigned j = i + rand() % (iEnd - i);
  985. pool.DeAllocate(tr[j].ptr, 0, 0);
  986. tr[j].ptr = tr[i].ptr;
  987. }
  988. }
  989. void poolAllocateSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  990. {
  991. for (unsigned i = iStart; i < iEnd; i++)
  992. {
  993. size_t size = rand_size();
  994. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  995. tr[i].size = size;
  996. }
  997. }
  998. void poolFreeSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  999. {
  1000. for (unsigned i = iStart; i < iEnd; i++)
  1001. {
  1002. unsigned j = i + rand() % (iEnd - i);
  1003. pool.DeAllocate(tr[j].ptr, tr[j].size, 0);
  1004. tr[j].ptr = tr[i].ptr;
  1005. tr[j].size = tr[i].size;
  1006. }
  1007. }
  1008. void poolAllocateAlignment(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1009. {
  1010. for (unsigned i = iStart; i < iEnd; i++)
  1011. {
  1012. size_t size = rand_size();
  1013. size_t alignment = rand_alignment();
  1014. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1015. }
  1016. }
  1017. void poolFreeAlignment(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1018. {
  1019. for (unsigned i = iStart; i < iEnd; i++)
  1020. {
  1021. unsigned j = i + rand() % (iEnd - i);
  1022. pool.DeAllocate(tr[j].ptr, 0, 0);
  1023. tr[j].ptr = tr[i].ptr;
  1024. }
  1025. }
  1026. void poolAllocateAlignmentSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1027. {
  1028. for (unsigned i = iStart; i < iEnd; i++)
  1029. {
  1030. size_t size = rand_size();
  1031. size_t alignment = rand_alignment();
  1032. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1033. tr[i].size = size;
  1034. tr[i].alignment = alignment;
  1035. }
  1036. }
  1037. void poolFreeAlignmentSize(PoolSchema& pool, unsigned iStart, unsigned iEnd)
  1038. {
  1039. for (unsigned i = iStart; i < iEnd; i++)
  1040. {
  1041. unsigned j = i + rand() % (iEnd - i);
  1042. pool.DeAllocate(tr[j].ptr, tr[j].size, tr[j].alignment);
  1043. tr[j].ptr = tr[i].ptr;
  1044. tr[j].size = tr[i].size;
  1045. tr[j].alignment = tr[i].alignment;
  1046. }
  1047. }
  1048. //////////////////////////////////////////////////////////////////////////
  1049. //////////////////////////////////////////////////////////////////////////
  1050. // THREAD POOL
  1051. void thPoolAllocate(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1052. {
  1053. for (unsigned i = iStart; i < iEnd; i++)
  1054. {
  1055. size_t size = rand_size();
  1056. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  1057. }
  1058. }
  1059. void thPoolFree(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1060. {
  1061. for (unsigned i = iStart; i < iEnd; i++)
  1062. {
  1063. unsigned j = i + rand() % (iEnd - i);
  1064. pool.DeAllocate(tr[j].ptr, 0, 0);
  1065. tr[j].ptr = tr[i].ptr;
  1066. }
  1067. }
  1068. void thPoolAllocateSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1069. {
  1070. for (unsigned i = iStart; i < iEnd; i++)
  1071. {
  1072. size_t size = rand_size();
  1073. tr[i].ptr = pool.Allocate(size, DefaultAlignment, 0, nullptr, nullptr, 0, 0);
  1074. tr[i].size = size;
  1075. }
  1076. }
  1077. void thPoolFreeSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1078. {
  1079. for (unsigned i = iStart; i < iEnd; i++)
  1080. {
  1081. unsigned j = i + rand() % (iEnd - i);
  1082. pool.DeAllocate(tr[j].ptr, tr[j].size, 0);
  1083. tr[j].ptr = tr[i].ptr;
  1084. tr[j].size = tr[i].size;
  1085. }
  1086. }
  1087. void thPoolAllocateAlignment(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1088. {
  1089. for (unsigned i = iStart; i < iEnd; i++)
  1090. {
  1091. size_t size = rand_size();
  1092. size_t alignment = rand_alignment();
  1093. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1094. }
  1095. }
  1096. void thPoolFreeAlignment(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1097. {
  1098. for (unsigned i = iStart; i < iEnd; i++)
  1099. {
  1100. unsigned j = i + rand() % (iEnd - i);
  1101. pool.DeAllocate(tr[j].ptr, 0, 0);
  1102. tr[j].ptr = tr[i].ptr;
  1103. }
  1104. }
  1105. void thPoolAllocateAlignmentSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1106. {
  1107. for (unsigned i = iStart; i < iEnd; i++)
  1108. {
  1109. size_t size = rand_size();
  1110. size_t alignment = rand_alignment();
  1111. tr[i].ptr = pool.Allocate(size, alignment, 0, nullptr, nullptr, 0, 0);
  1112. tr[i].size = size;
  1113. tr[i].alignment = alignment;
  1114. }
  1115. }
  1116. void thPoolFreeAlignmentSize(ThreadPoolSchema& pool, unsigned iStart, unsigned iEnd)
  1117. {
  1118. for (unsigned i = iStart; i < iEnd; i++)
  1119. {
  1120. unsigned j = i + rand() % (iEnd - i);
  1121. pool.DeAllocate(tr[j].ptr, tr[j].size, tr[j].alignment);
  1122. tr[j].ptr = tr[i].ptr;
  1123. tr[j].size = tr[i].size;
  1124. tr[j].alignment = tr[i].alignment;
  1125. }
  1126. }
  1127. //////////////////////////////////////////////////////////////////////////
  1128. void allocdealloc(HphaSchema& hpha, PoolSchema& pool, bool isHpha, bool isDefault, bool isPool)
  1129. {
  1130. AZStd::chrono::steady_clock::time_point start;
  1131. AZStd::chrono::duration<float> elapsed;
  1132. 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));
  1133. printf("\t\t\t\tHPHA\t\tDL\t\tDEFAULT\t\tPOOL\n");
  1134. printf("ALLOC/FREE:");
  1135. if (isHpha)
  1136. {
  1137. srand(1234);
  1138. start = AZStd::chrono::steady_clock::now();
  1139. hphaAllocate(hpha, 0, N);
  1140. elapsed = AZStd::chrono::steady_clock::now() - start;
  1141. printf("\t\t\t(%.3f/", elapsed.count());
  1142. start = AZStd::chrono::steady_clock::now();
  1143. hphaFree(hpha, 0, N);
  1144. elapsed = AZStd::chrono::steady_clock::now() - start;
  1145. printf("%.3f)", elapsed.count());
  1146. }
  1147. else
  1148. {
  1149. printf("\t\t\t(skip/skip)");
  1150. }
  1151. if (isDefault)
  1152. {
  1153. srand(1234);
  1154. start = AZStd::chrono::steady_clock::now();
  1155. defAllocate(0, N);
  1156. elapsed = AZStd::chrono::steady_clock::now() - start;
  1157. printf("\t(%.3f/", elapsed.count());
  1158. start = AZStd::chrono::steady_clock::now();
  1159. defFree(0, N);
  1160. elapsed = AZStd::chrono::steady_clock::now() - start;
  1161. printf("%.3f)", elapsed.count());
  1162. }
  1163. else
  1164. {
  1165. printf("\t(skip/skip)");
  1166. }
  1167. if (MAX_SIZE<=256)
  1168. {
  1169. if (isPool)
  1170. {
  1171. srand(1234);
  1172. start = AZStd::chrono::steady_clock::now();
  1173. poolAllocate(pool, 0, N);
  1174. elapsed = AZStd::chrono::steady_clock::now() - start;
  1175. printf("\t(%.3f/", elapsed.count());
  1176. start = AZStd::chrono::steady_clock::now();
  1177. poolFree(pool, 0, N);
  1178. elapsed = AZStd::chrono::steady_clock::now() - start;
  1179. printf("%.3f)\n", elapsed.count());
  1180. }
  1181. else
  1182. {
  1183. printf("\t(skip/skip)\n");
  1184. }
  1185. }
  1186. else
  1187. {
  1188. printf("\n");
  1189. }
  1190. printf("ALLOC/FREE(size):");
  1191. if (isHpha)
  1192. {
  1193. srand(1234);
  1194. start = AZStd::chrono::steady_clock::now();
  1195. hphaAllocateSize(hpha, 0, N);
  1196. elapsed = AZStd::chrono::steady_clock::now() - start;
  1197. printf("\t\t(%.3f/", elapsed.count());
  1198. start = AZStd::chrono::steady_clock::now();
  1199. hphaFreeSize(hpha, 0, N);
  1200. elapsed = AZStd::chrono::steady_clock::now() - start;
  1201. printf("%.3f)", elapsed.count());
  1202. }
  1203. else
  1204. {
  1205. printf("\t\t(skip/skip)");
  1206. }
  1207. if (isDefault)
  1208. {
  1209. srand(1234);
  1210. start = AZStd::chrono::steady_clock::now();
  1211. defAllocateSize(0, N);
  1212. elapsed = AZStd::chrono::steady_clock::now() - start;
  1213. printf("\t(%.3f/", elapsed.count());
  1214. start = AZStd::chrono::steady_clock::now();
  1215. defFreeSize(0, N);
  1216. elapsed = AZStd::chrono::steady_clock::now() - start;
  1217. printf("%.3f)", elapsed.count());
  1218. }
  1219. else
  1220. {
  1221. printf("\t(skip/skip)");
  1222. }
  1223. if (MAX_SIZE<=256)
  1224. {
  1225. if (isPool)
  1226. {
  1227. srand(1234);
  1228. start = AZStd::chrono::steady_clock::now();
  1229. poolAllocateSize(pool, 0, N);
  1230. elapsed = AZStd::chrono::steady_clock::now() - start;
  1231. printf("\t(%.3f/", elapsed.count());
  1232. start = AZStd::chrono::steady_clock::now();
  1233. poolFreeSize(pool, 0, N);
  1234. elapsed = AZStd::chrono::steady_clock::now() - start;
  1235. printf("%.3f)\n", elapsed.count());
  1236. }
  1237. else
  1238. {
  1239. printf("\t(skip/skip)\n");
  1240. }
  1241. }
  1242. else
  1243. {
  1244. printf("\n");
  1245. }
  1246. printf("ALLOC(align)/FREE:");
  1247. if (isHpha)
  1248. {
  1249. srand(1234);
  1250. start = AZStd::chrono::steady_clock::now();
  1251. hphaAllocateAlignment(hpha, 0, N);
  1252. elapsed = AZStd::chrono::steady_clock::now() - start;
  1253. printf("\t\t(%.3f/", elapsed.count());
  1254. start = AZStd::chrono::steady_clock::now();
  1255. hphaFreeAlignment(hpha, 0, N);
  1256. elapsed = AZStd::chrono::steady_clock::now() - start;
  1257. printf("%.3f)", elapsed.count());
  1258. }
  1259. else
  1260. {
  1261. printf("\t\t(skip/skip)");
  1262. }
  1263. if (isDefault)
  1264. {
  1265. srand(1234);
  1266. start = AZStd::chrono::steady_clock::now();
  1267. defAllocateAlignment(0, N);
  1268. elapsed = AZStd::chrono::steady_clock::now() - start;
  1269. printf("\t(%.3f/", elapsed.count());
  1270. start = AZStd::chrono::steady_clock::now();
  1271. defFreeAlignment(0, N);
  1272. elapsed = AZStd::chrono::steady_clock::now() - start;
  1273. printf("%.3f)", elapsed.count());
  1274. }
  1275. else
  1276. {
  1277. printf("\t(skip/skip)");
  1278. }
  1279. if (MAX_SIZE<=256)
  1280. {
  1281. if (isPool)
  1282. {
  1283. srand(1234);
  1284. start = AZStd::chrono::steady_clock::now();
  1285. poolAllocateAlignment(pool, 0, N);
  1286. elapsed = AZStd::chrono::steady_clock::now() - start;
  1287. printf("\t(%.3f/", elapsed.count());
  1288. start = AZStd::chrono::steady_clock::now();
  1289. poolFreeAlignment(pool, 0, N);
  1290. elapsed = AZStd::chrono::steady_clock::now() - start;
  1291. printf("%.3f)\n", elapsed.count());
  1292. }
  1293. else
  1294. {
  1295. printf("\t(skip/skip)\n");
  1296. }
  1297. }
  1298. else
  1299. {
  1300. printf("\n");
  1301. }
  1302. printf("ALLOC(align)/FREE(size,align):");
  1303. if (isHpha)
  1304. {
  1305. srand(1234);
  1306. start = AZStd::chrono::steady_clock::now();
  1307. hphaAllocateAlignmentSize(hpha, 0, N);
  1308. elapsed = AZStd::chrono::steady_clock::now() - start;
  1309. printf("\t(%.3f/", elapsed.count());
  1310. start = AZStd::chrono::steady_clock::now();
  1311. hphaFreeAlignmentSize(hpha, 0, N);
  1312. elapsed = AZStd::chrono::steady_clock::now() - start;
  1313. printf("%.3f)", elapsed.count());
  1314. }
  1315. else
  1316. {
  1317. printf("\t(skip/skip)");
  1318. }
  1319. if (isDefault)
  1320. {
  1321. srand(1234);
  1322. start = AZStd::chrono::steady_clock::now();
  1323. defAllocateAlignmentSize(0, N);
  1324. elapsed = AZStd::chrono::steady_clock::now() - start;
  1325. printf("\t(%.3f/", elapsed.count());
  1326. start = AZStd::chrono::steady_clock::now();
  1327. defFreeAlignmentSize(0, N);
  1328. elapsed = AZStd::chrono::steady_clock::now() - start;
  1329. printf("%.3f)", elapsed.count());
  1330. }
  1331. else
  1332. {
  1333. printf("\t(skip/skip)");
  1334. }
  1335. if (MAX_SIZE<=256)
  1336. {
  1337. if (isPool)
  1338. {
  1339. srand(1234);
  1340. start = AZStd::chrono::steady_clock::now();
  1341. poolAllocateAlignmentSize(pool, 0, N);
  1342. elapsed = AZStd::chrono::steady_clock::now() - start;
  1343. printf("\t(%.3f/", elapsed.count());
  1344. start = AZStd::chrono::steady_clock::now();
  1345. poolFreeAlignmentSize(pool, 0, N);
  1346. elapsed = AZStd::chrono::steady_clock::now() - start;
  1347. printf("%.3f)\n", elapsed.count());
  1348. }
  1349. else
  1350. {
  1351. printf("\t(skip/skip)\n");
  1352. }
  1353. }
  1354. else
  1355. {
  1356. printf("\n");
  1357. }
  1358. printf("TEST REALLOC:");
  1359. srand(1234);
  1360. start = AZStd::chrono::steady_clock::now();
  1361. for (unsigned i = 0; i < N; i++) {
  1362. size_t size = rand_size();
  1363. tr[i].ptr = hpha.ReAllocate(nullptr, size, 1);
  1364. }
  1365. for (unsigned i = 0; i < N; i++) {
  1366. size_t size = rand_size();
  1367. tr[i].ptr = hpha.ReAllocate(tr[i].ptr, size, 1);
  1368. }
  1369. for (unsigned i = 0; i < N; i++) {
  1370. unsigned j = i + rand() % (N - i);
  1371. hpha.ReAllocate(tr[j].ptr, 0, 0);
  1372. tr[j].ptr = tr[i].ptr;
  1373. }
  1374. elapsed = AZStd::chrono::steady_clock::now() - start;
  1375. printf("\t\t\t\t\t(%.3f)", elapsed.count());
  1376. srand(1234);
  1377. start = AZStd::chrono::steady_clock::now();
  1378. for (unsigned i = 0; i < N; i++) {
  1379. size_t size = rand_size();
  1380. tr[i].ptr = realloc(NULL, size);
  1381. }
  1382. for (unsigned i = 0; i < N; i++) {
  1383. size_t size = rand_size();
  1384. tr[i].ptr = realloc(tr[i].ptr, size);
  1385. }
  1386. for (unsigned i = 0; i < N; i++) {
  1387. unsigned j = i + rand() % (N - i);
  1388. realloc(tr[j].ptr, 0);
  1389. tr[j].ptr = tr[i].ptr;
  1390. }
  1391. elapsed = AZStd::chrono::steady_clock::now() - start;
  1392. printf("\t(%.3f)\n", elapsed.count());
  1393. printf("TEST REALLOC with ALIGNMENT:");
  1394. srand(1234);
  1395. start = AZStd::chrono::steady_clock::now();
  1396. for (unsigned i = 0; i < N; i++) {
  1397. size_t size = rand_size();
  1398. size_t alignment = rand_alignment();
  1399. tr[i].ptr = hpha.ReAllocate(NULL, size, alignment);
  1400. }
  1401. for (unsigned i = 0; i < N; i++) {
  1402. size_t size = rand_size();
  1403. size_t alignment = rand_alignment();
  1404. tr[i].ptr = hpha.ReAllocate(tr[i].ptr, size, alignment);
  1405. }
  1406. for (unsigned i = 0; i < N; i++) {
  1407. unsigned j = i + rand() % (N - i);
  1408. hpha.ReAllocate(tr[j].ptr, 0, 0);
  1409. tr[j].ptr = tr[i].ptr;
  1410. }
  1411. elapsed = AZStd::chrono::steady_clock::now() - start;
  1412. printf("\t\t\t(%.3f)", elapsed.count());
  1413. srand(1234);
  1414. start = AZStd::chrono::steady_clock::now();
  1415. #ifdef AZ_PLATFORM_WINDOWS // Windows test only
  1416. for (unsigned i = 0; i < N; i++) {
  1417. size_t size = rand_size();
  1418. size_t alignment = rand_alignment();
  1419. tr[i].ptr = _aligned_realloc(NULL, size, alignment);
  1420. }
  1421. for (unsigned i = 0; i < N; i++) {
  1422. size_t size = rand_size();
  1423. size_t alignment = rand_alignment();
  1424. tr[i].ptr = _aligned_realloc(tr[i].ptr, size, alignment);
  1425. }
  1426. for (unsigned i = 0; i < N; i++) {
  1427. unsigned j = i + rand() % (N - i);
  1428. realloc(tr[j].ptr, 0);
  1429. tr[j].ptr = tr[i].ptr;
  1430. }
  1431. #endif // AZ_PLATFORM_WINDOWS
  1432. elapsed = AZStd::chrono::steady_clock::now() - start;
  1433. printf("\t(%.3f)\n", elapsed.count());
  1434. }
  1435. void allocdeallocThread(HphaSchema& hpha, ThreadPoolSchema& thPool, bool isHpha, bool isDefault, bool isPool)
  1436. {
  1437. AZStd::chrono::steady_clock::time_point start;
  1438. AZStd::chrono::duration<float> elapsed;
  1439. 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));
  1440. printf("\t\t\t\tHPHA\t\tDL\t\tDEFAULT\t\tPOOL\n");
  1441. static const int TN = N / 4;
  1442. {
  1443. printf("ALLOC/FREE:");
  1444. if (isHpha)
  1445. {
  1446. srand(1234);
  1447. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1448. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1449. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1450. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocate, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1451. start = AZStd::chrono::steady_clock::now();
  1452. tr1.join();
  1453. tr2.join();
  1454. tr3.join();
  1455. tr4.join();
  1456. elapsed = AZStd::chrono::steady_clock::now() - start;
  1457. printf("\t\t\t(%.3f/", elapsed.count());
  1458. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1459. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1460. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1461. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFree, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1462. start = AZStd::chrono::steady_clock::now();
  1463. tr5.join();
  1464. tr6.join();
  1465. tr7.join();
  1466. tr8.join();
  1467. elapsed = AZStd::chrono::steady_clock::now() - start;
  1468. printf("%.3f)", elapsed.count());
  1469. }
  1470. else
  1471. {
  1472. printf("\t\t\t(skip/skip)");
  1473. }
  1474. }
  1475. {
  1476. if (isDefault)
  1477. {
  1478. srand(1234);
  1479. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 0 * TN, 1 * TN));
  1480. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 1 * TN, 2 * TN));
  1481. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 2 * TN, 3 * TN));
  1482. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocate, this, 3 * TN, 4 * TN));
  1483. start = AZStd::chrono::steady_clock::now();
  1484. tr1.join();
  1485. tr2.join();
  1486. tr3.join();
  1487. tr4.join();
  1488. elapsed = AZStd::chrono::steady_clock::now() - start;
  1489. printf("\t(%.3f/", elapsed.count());
  1490. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 0 * TN, 1 * TN));
  1491. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 1 * TN, 2 * TN));
  1492. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 2 * TN, 3 * TN));
  1493. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFree, this, 3 * TN, 4 * TN));
  1494. start = AZStd::chrono::steady_clock::now();
  1495. tr5.join();
  1496. tr6.join();
  1497. tr7.join();
  1498. tr8.join();
  1499. elapsed = AZStd::chrono::steady_clock::now() - start;
  1500. printf("\t(%.3f)", elapsed.count());
  1501. }
  1502. else
  1503. {
  1504. printf("\t(skip/skip)");
  1505. }
  1506. }
  1507. if (MAX_SIZE<=256)
  1508. {
  1509. if (isPool)
  1510. {
  1511. srand(1234);
  1512. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1513. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1514. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1515. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocate, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1516. start = AZStd::chrono::steady_clock::now();
  1517. tr1.join();
  1518. tr2.join();
  1519. tr3.join();
  1520. tr4.join();
  1521. elapsed = AZStd::chrono::steady_clock::now() - start;
  1522. printf("\t(%.3f/", elapsed.count());
  1523. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1524. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1525. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1526. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFree, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1527. start = AZStd::chrono::steady_clock::now();
  1528. tr5.join();
  1529. tr6.join();
  1530. tr7.join();
  1531. tr8.join();
  1532. elapsed = AZStd::chrono::steady_clock::now() - start;
  1533. printf("%.3f)\n", elapsed.count());
  1534. }
  1535. else
  1536. {
  1537. printf("\t(skip/skip)\n");
  1538. }
  1539. }
  1540. else
  1541. {
  1542. printf("\n");
  1543. }
  1544. {
  1545. printf("ALLOC/FREE(size):");
  1546. if (isHpha)
  1547. {
  1548. srand(1234);
  1549. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1550. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1551. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1552. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1553. start = AZStd::chrono::steady_clock::now();
  1554. tr1.join();
  1555. tr2.join();
  1556. tr3.join();
  1557. tr4.join();
  1558. elapsed = AZStd::chrono::steady_clock::now() - start;
  1559. printf("\t\t(%.3f/", elapsed.count());
  1560. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1561. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1562. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1563. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1564. start = AZStd::chrono::steady_clock::now();
  1565. tr5.join();
  1566. tr6.join();
  1567. tr7.join();
  1568. tr8.join();
  1569. elapsed = AZStd::chrono::steady_clock::now() - start;
  1570. printf("/%.3f)", elapsed.count());
  1571. }
  1572. else
  1573. {
  1574. printf("\t\t(skip/skip)");
  1575. }
  1576. }
  1577. {
  1578. if (isDefault)
  1579. {
  1580. srand(1234);
  1581. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 0 * TN, 1 * TN));
  1582. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 1 * TN, 2 * TN));
  1583. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 2 * TN, 3 * TN));
  1584. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocateSize, this, 3 * TN, 4 * TN));
  1585. start = AZStd::chrono::steady_clock::now();
  1586. tr1.join();
  1587. tr2.join();
  1588. tr3.join();
  1589. tr4.join();
  1590. elapsed = AZStd::chrono::steady_clock::now() - start;
  1591. printf("\t(%.3f/", elapsed.count());
  1592. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 0 * TN, 1 * TN));
  1593. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 1 * TN, 2 * TN));
  1594. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 2 * TN, 3 * TN));
  1595. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFreeSize, this, 3 * TN, 4 * TN));
  1596. start = AZStd::chrono::steady_clock::now();
  1597. tr5.join();
  1598. tr6.join();
  1599. tr7.join();
  1600. tr8.join();
  1601. elapsed = AZStd::chrono::steady_clock::now() - start;
  1602. printf("%.3f)", elapsed.count());
  1603. }
  1604. else
  1605. {
  1606. printf("\t(skip/skip)");
  1607. }
  1608. }
  1609. if (MAX_SIZE<=256)
  1610. {
  1611. if (isPool)
  1612. {
  1613. srand(1234);
  1614. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1615. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1616. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1617. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1618. start = AZStd::chrono::steady_clock::now();
  1619. tr1.join();
  1620. tr2.join();
  1621. tr3.join();
  1622. tr4.join();
  1623. elapsed = AZStd::chrono::steady_clock::now() - start;
  1624. printf("\t(%.3f/", elapsed.count());
  1625. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1626. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1627. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1628. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1629. start = AZStd::chrono::steady_clock::now();
  1630. tr5.join();
  1631. tr6.join();
  1632. tr7.join();
  1633. tr8.join();
  1634. elapsed = AZStd::chrono::steady_clock::now() - start;
  1635. printf("%.3f)\n", elapsed.count());
  1636. }
  1637. else
  1638. {
  1639. printf("\t(skip/skip)\n");
  1640. }
  1641. }
  1642. else
  1643. {
  1644. printf("\n");
  1645. }
  1646. {
  1647. printf("ALLOC(align)/FREE:");
  1648. if (isHpha)
  1649. {
  1650. srand(1234);
  1651. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1652. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1653. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1654. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignment, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1655. start = AZStd::chrono::steady_clock::now();
  1656. tr1.join();
  1657. tr2.join();
  1658. tr3.join();
  1659. tr4.join();
  1660. elapsed = AZStd::chrono::steady_clock::now() - start;
  1661. printf("\t\t(%.3f/", elapsed.count());
  1662. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1663. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1664. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1665. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignment, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1666. start = AZStd::chrono::steady_clock::now();
  1667. tr5.join();
  1668. tr6.join();
  1669. tr7.join();
  1670. tr8.join();
  1671. elapsed = AZStd::chrono::steady_clock::now() - start;
  1672. printf("%.3f)", elapsed.count());
  1673. }
  1674. else
  1675. {
  1676. printf("\t\t(skip/skip)");
  1677. }
  1678. }
  1679. {
  1680. if (isDefault)
  1681. {
  1682. srand(1234);
  1683. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 0 * TN, 1 * TN));
  1684. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 1 * TN, 2 * TN));
  1685. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 2 * TN, 3 * TN));
  1686. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignment, this, 3 * TN, 4 * TN));
  1687. start = AZStd::chrono::steady_clock::now();
  1688. tr1.join();
  1689. tr2.join();
  1690. tr3.join();
  1691. tr4.join();
  1692. elapsed = AZStd::chrono::steady_clock::now() - start;
  1693. printf("\t(%.3f/", elapsed.count());
  1694. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 0 * TN, 1 * TN));
  1695. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 1 * TN, 2 * TN));
  1696. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 2 * TN, 3 * TN));
  1697. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignment, this, 3 * TN, 4 * TN));
  1698. start = AZStd::chrono::steady_clock::now();
  1699. tr5.join();
  1700. tr6.join();
  1701. tr7.join();
  1702. tr8.join();
  1703. elapsed = AZStd::chrono::steady_clock::now() - start;
  1704. printf("%.3f)", elapsed.count());
  1705. }
  1706. else
  1707. {
  1708. printf("\t(skip/skip)");
  1709. }
  1710. }
  1711. if (MAX_SIZE<=256)
  1712. {
  1713. if (isPool)
  1714. {
  1715. srand(1234);
  1716. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1717. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1718. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1719. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignment, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1720. start = AZStd::chrono::steady_clock::now();
  1721. tr1.join();
  1722. tr2.join();
  1723. tr3.join();
  1724. tr4.join();
  1725. elapsed = AZStd::chrono::steady_clock::now() - start;
  1726. printf("\t(%.3f/", elapsed.count());
  1727. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1728. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1729. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1730. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignment, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1731. start = AZStd::chrono::steady_clock::now();
  1732. tr5.join();
  1733. tr6.join();
  1734. tr7.join();
  1735. tr8.join();
  1736. elapsed = AZStd::chrono::steady_clock::now() - start;
  1737. printf("%.3f)\n", elapsed.count());
  1738. }
  1739. else
  1740. {
  1741. printf("\t(skip/skip)\n");
  1742. }
  1743. }
  1744. else
  1745. {
  1746. printf("\n");
  1747. }
  1748. {
  1749. printf("ALLOC(align)/FREE(size,align):");
  1750. if (isHpha)
  1751. {
  1752. srand(1234);
  1753. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1754. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1755. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1756. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::hphaAllocateAlignmentSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1757. start = AZStd::chrono::steady_clock::now();
  1758. tr1.join();
  1759. tr2.join();
  1760. tr3.join();
  1761. tr4.join();
  1762. elapsed = AZStd::chrono::steady_clock::now() - start;
  1763. printf("\t(%.3f/", elapsed.count());
  1764. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 0 * TN, 1 * TN));
  1765. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 1 * TN, 2 * TN));
  1766. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 2 * TN, 3 * TN));
  1767. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::hphaFreeAlignmentSize, this, AZStd::ref(hpha), 3 * TN, 4 * TN));
  1768. start = AZStd::chrono::steady_clock::now();
  1769. tr5.join();
  1770. tr6.join();
  1771. tr7.join();
  1772. tr8.join();
  1773. elapsed = AZStd::chrono::steady_clock::now() - start;
  1774. printf("%.3f)", elapsed.count());
  1775. }
  1776. else
  1777. {
  1778. printf("\t(skip/skip)");
  1779. }
  1780. }
  1781. {
  1782. if (isDefault)
  1783. {
  1784. srand(1234);
  1785. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 0 * TN, 1 * TN));
  1786. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 1 * TN, 2 * TN));
  1787. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 2 * TN, 3 * TN));
  1788. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::defAllocateAlignmentSize, this, 3 * TN, 4 * TN));
  1789. start = AZStd::chrono::steady_clock::now();
  1790. tr1.join();
  1791. tr2.join();
  1792. tr3.join();
  1793. tr4.join();
  1794. elapsed = AZStd::chrono::steady_clock::now() - start;
  1795. printf("\t(%.3f/", elapsed.count());
  1796. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 0 * TN, 1 * TN));
  1797. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 1 * TN, 2 * TN));
  1798. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 2 * TN, 3 * TN));
  1799. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::defFreeAlignmentSize, this, 3 * TN, 4 * TN));
  1800. start = AZStd::chrono::steady_clock::now();
  1801. tr5.join();
  1802. tr6.join();
  1803. tr7.join();
  1804. tr8.join();
  1805. elapsed = AZStd::chrono::steady_clock::now() - start;
  1806. printf("%.3f)", elapsed.count());
  1807. }
  1808. else
  1809. {
  1810. printf("\t(skip/skip)");
  1811. }
  1812. }
  1813. if (MAX_SIZE<=256)
  1814. {
  1815. if (isPool)
  1816. {
  1817. srand(1234);
  1818. AZStd::thread tr1(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1819. AZStd::thread tr2(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1820. AZStd::thread tr3(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1821. AZStd::thread tr4(AZStd::bind(&PERF_MemoryBenchmark::thPoolAllocateAlignmentSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1822. start = AZStd::chrono::steady_clock::now();
  1823. tr1.join();
  1824. tr2.join();
  1825. tr3.join();
  1826. tr4.join();
  1827. elapsed = AZStd::chrono::steady_clock::now() - start;
  1828. printf("\t(%.3f/", elapsed.count());
  1829. AZStd::thread tr5(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 0 * TN, 1 * TN));
  1830. AZStd::thread tr6(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 1 * TN, 2 * TN));
  1831. AZStd::thread tr7(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 2 * TN, 3 * TN));
  1832. AZStd::thread tr8(AZStd::bind(&PERF_MemoryBenchmark::thPoolFreeAlignmentSize, this, AZStd::ref(thPool), 3 * TN, 4 * TN));
  1833. start = AZStd::chrono::steady_clock::now();
  1834. tr5.join();
  1835. tr6.join();
  1836. tr7.join();
  1837. tr8.join();
  1838. elapsed = AZStd::chrono::steady_clock::now() - start;
  1839. printf("%.3f)\n", elapsed.count());
  1840. }
  1841. else
  1842. {
  1843. printf("\t(skip/skip)\n");
  1844. }
  1845. }
  1846. else
  1847. {
  1848. printf("\n");
  1849. }
  1850. }
  1851. void run()
  1852. {
  1853. printf("\n\t\t\t=======================\n");
  1854. printf("\t\t\tSchemas Benchmark Test!\n");
  1855. printf("\t\t\t=======================\n");
  1856. {
  1857. // TODO Switch to using instance of HphaAllocator with a sub allocator of a fixed size
  1858. {
  1859. HphaSchema hpha;
  1860. PoolSchema pool;
  1861. pool.Create();
  1862. ThreadPoolSchemaHelper<nullptr_t> threadPool;
  1863. threadPool.Create();
  1864. printf("---- Single Thread ----\n");
  1865. // any allocations
  1866. MAX_SIZE = 4096;
  1867. allocdealloc(hpha, pool, false, false, true);
  1868. printf("\n");
  1869. // pool allocations
  1870. MAX_SIZE = 256;
  1871. allocdealloc(hpha, pool, false, false, true);
  1872. // threads
  1873. printf("\n---- 4 Threads ----\n");
  1874. // any allocations
  1875. MAX_SIZE = 4096;
  1876. //allocdeallocThread(hpha,threadPool,true,true,true);
  1877. printf("\n");
  1878. // pool allocations
  1879. MAX_SIZE = 256;
  1880. //allocdeallocThread(hpha,threadPool,true,true,true);
  1881. }
  1882. }
  1883. #if AZ_TRAIT_UNITTEST_NON_PREALLOCATED_HPHA_TEST
  1884. printf("\n\t\t\tNO prealocated memory!\n");
  1885. {
  1886. HphaSchema hpha;
  1887. PoolSchema pool;
  1888. pool.Create();
  1889. ThreadPoolSchemaHelper<nullptr_t> threadPool;
  1890. threadPool.Create();
  1891. printf("---- Single Thread ----\n");
  1892. // any allocations
  1893. MAX_SIZE = 4096;
  1894. allocdealloc(hpha,pool,true,true,true);
  1895. printf("\n");
  1896. // pool allocations
  1897. MAX_SIZE = 256;
  1898. allocdealloc(hpha,pool,true,true,true);
  1899. // threads
  1900. printf("\n---- 4 Threads ----\n");
  1901. // any allocations
  1902. MAX_SIZE = 4096;
  1903. allocdeallocThread(hpha,threadPool,true,true,true);
  1904. printf("\n");
  1905. // pool allocations
  1906. MAX_SIZE = 256;
  1907. allocdeallocThread(hpha,threadPool,true,true,true);
  1908. }
  1909. #endif
  1910. }
  1911. };
  1912. #ifdef ENABLE_PERFORMANCE_TEST
  1913. TEST_F(PERF_MemoryBenchmark, Test)
  1914. {
  1915. run();
  1916. }
  1917. #endif // ENABLE_PERFORMANCE_TEST
  1918. #endif // AZ_TRAIT_PERF_MEMORYBENCHMARK_IS_AVAILABLE
  1919. }
  1920. // GlobalNewDeleteTest-End