No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BenchmarkAllocator.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using Unity.PerformanceTesting;
  3. using Unity.PerformanceTesting.Benchmark;
  4. using Unity.Burst;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Jobs;
  7. namespace Unity.Collections.PerformanceTests
  8. {
  9. /// <summary>
  10. /// Specifies a class containing performance test methods which should be included in allocator benchmarking.<para />
  11. /// The values specified in this enum are unlikely to be needed in user code, but user code will specify the enum type
  12. /// in a couple places:<para />
  13. /// <c>[Benchmark(typeof(BenchmarkAllocatorType))] // &lt;---- HERE<br />
  14. /// class FooAllocatorPerformanceTestMethods</c><para />
  15. /// and<para />
  16. /// <c>[Test, Performance]<br />
  17. /// public unsafe void AllocatorPerfTestExample(<br />
  18. /// [Values(1, 2, 4, 8)] int workerThreads,<br />
  19. /// [Values(1024, 1024 * 1024)] int allocSize,<br />
  20. /// [Values] BenchmarkAllocatorType type) // &lt;---- HERE<br />
  21. /// {</c><para />
  22. /// Though values may be specified in the performance test method parameter, it is recommended to leave the argument implicitly
  23. /// covering all enum values as seen in the example above.
  24. /// </summary>
  25. [BenchmarkComparison(BenchmarkAllocatorConfig.Persistent, "Persistent (E)")]
  26. [BenchmarkComparisonExternal(BenchmarkAllocatorConfig.TempJob, "TempJob (E)")]
  27. [BenchmarkComparisonExternal(BenchmarkAllocatorConfig.Temp, "Temp (E)")]
  28. [BenchmarkComparisonDisplay(SampleUnit.Microsecond, 1, BenchmarkAllocatorConfig.kRankingStat)]
  29. public enum BenchmarkAllocatorType : int
  30. {
  31. /// <summary>Allocator performance test will execute on a managed (not burst compiled) code path</summary>
  32. [BenchmarkName("{0} (S)")] Managed,
  33. /// <summary>Allocator performance test will execute on a burst compile code path, with safety checks enabled</summary>
  34. [BenchmarkName("{0} (S+B)")] BurstSafety,
  35. /// <summary>Allocator performance test will execute on a burst compile code path, with safety checks disabled</summary>
  36. [BenchmarkName("{0} (B)")] BurstNoSafety,
  37. }
  38. internal static class BenchmarkAllocatorConfig
  39. {
  40. internal const int Temp = -1;
  41. internal const int TempJob = -2;
  42. internal const int Persistent = -3;
  43. internal const BenchmarkRankingStatistic kRankingStat = BenchmarkRankingStatistic.Min;
  44. internal const int kCountWarmup = 5;
  45. internal const int kCountMeasure = 50;
  46. #if UNITY_STANDALONE || UNITY_EDITOR
  47. internal const int kCountAllocations = 150;
  48. #else
  49. // Still allows allocator tests on non-desktop platforms, but with a much lower memory requirement
  50. internal const int kCountAllocations = 25;
  51. #endif
  52. #if UNITY_EDITOR
  53. [UnityEditor.MenuItem("DOTS/Unity.Collections/Generate Allocator Benchmarks")]
  54. #endif
  55. static void RunBenchmarks()
  56. {
  57. BenchmarkGenerator.GenerateMarkdown(
  58. "Allocators",
  59. typeof(BenchmarkAllocatorType),
  60. "../../Packages/com.unity.collections/Documentation~/performance-comparison-allocators.md",
  61. $"The following benchmarks make **{kCountAllocations} consecutive allocations** per sample set."
  62. + $"<br/>Multithreaded benchmarks make the full **{kCountAllocations} consecutive allocations *per worker thread*** per sample set."
  63. + $"<br/>The **{kRankingStat} of {kCountMeasure} sample sets** is compared against the baseline on the far right side of the table."
  64. + $"<br/>{kCountWarmup} extra sample sets are run as warmup."
  65. ,
  66. "Legend",
  67. new string[]
  68. {
  69. "`(S)` = Safety Enabled",
  70. "`(B)` = Burst Compiled *with Safety Disabled*",
  71. "`(S+B)` = Burst Compiled *with Safety Enabled*",
  72. "`(E)` = Engine Provided",
  73. "",
  74. "*`italic`* results are for benchmarking comparison only; these are not included in standard Performance Framework tests",
  75. });
  76. }
  77. }
  78. /// <summary>
  79. /// Interface to implement allocator performance tests which will run using <see cref="BenchmarkAllocatorRunner{T}.Run(BenchmarkAllocatorType, int, int, int[])"/>.
  80. /// Deriving tests from this interface enables both Performance Test Framework and Benchmark Framework to generate and run
  81. /// tests for the contexts described by <see cref="BenchmarkAllocatorType"/>.
  82. /// </summary>
  83. public interface IBenchmarkAllocator
  84. {
  85. /// <summary>
  86. /// Override this to add extra int arguments to a performance test implementation as fields in the implementing type. These arguments
  87. /// are optionally passed in through <see cref="BenchmarkAllocatorRunner{T}.Run(BenchmarkAllocatorType, int, int, int[])"/>.
  88. /// </summary>
  89. /// <param name="args">A variable number of extra arguments to passed through to the test implementation</param>
  90. public void SetParams(params int[] args) { }
  91. /// <summary>
  92. /// Used to create the allocator used in performance testing.
  93. /// </summary>
  94. /// <param name="builtinOverride">When this is <see cref="Allocator.None"/>, create the custom allocator type.
  95. /// Otherwise use the provided <see cref="Allocator"/> enum for allocations in performance testing.</param>
  96. public void CreateAllocator(Allocator builtinOverride);
  97. /// <summary>
  98. /// Used to free memory and destroy the custom allocator if it wasn't allocated with an <see cref="Allocator"/> type.
  99. /// </summary>
  100. public void DestroyAllocator();
  101. /// <summary>
  102. /// Actions performed prior to each measurement of a sample set. Typically used to set up initial state to ensure each sample measured is executed in the same way.
  103. /// </summary>
  104. /// <param name="workers">Number of job workers for this allocation test. Work is duplicated across job workers rather than split across job workers.</param>
  105. /// <param name="size">The base size of each allocation in a single measurement.</param>
  106. /// <param name="allocations">The number of allocations in a single measurement.</param>
  107. public void Setup(int workers, int size, int allocations);
  108. /// <summary>
  109. /// Actions performed following each measurement of a sample set. Typically used to dispose or invalidate the state set up during <see cref="Setup(int, int, int)"/>.
  110. /// </summary>
  111. public void Teardown();
  112. /// <summary>
  113. /// The code which will be executed during performance measurement. This should usually be general enough to work with any allocator, so if making
  114. /// allocations or freeing, the recommendation is to interface through <see cref="AllocatorManager"/>.
  115. /// </summary>
  116. /// <param name="workerI"></param>
  117. public void Measure(int workerI);
  118. }
  119. /// <summary>
  120. /// Provides the API for running allocator based Performance Framework tests and Benchmark Framework measurements.
  121. /// This will typically be the sole call from a performance test. See <see cref="Run(BenchmarkAllocatorType, int, int, int[])"/>
  122. /// for more information.
  123. /// </summary>
  124. /// <typeparam name="T">An implementation conforming to the <see cref="IBenchmarkAllocator"/> interface for running allocator performance tests and benchmarks.</typeparam>
  125. [BurstCompile(CompileSynchronously = true)]
  126. public static class BenchmarkAllocatorRunner<T> where T : unmanaged, IBenchmarkAllocator
  127. {
  128. internal unsafe struct JobST : IJob
  129. {
  130. [NativeDisableUnsafePtrRestriction] public T* methods;
  131. public void Execute() => methods->Measure(0);
  132. }
  133. [BurstCompile(CompileSynchronously = true, DisableSafetyChecks = true)]
  134. internal unsafe struct JobBurstST : IJob
  135. {
  136. [NativeDisableUnsafePtrRestriction] public T* methods;
  137. public void Execute() => methods->Measure(0);
  138. }
  139. [BurstCompile(CompileSynchronously = true, DisableSafetyChecks = false)]
  140. internal unsafe struct JobSafetyBurstST : IJob
  141. {
  142. [NativeDisableUnsafePtrRestriction] public T* methods;
  143. public void Execute() => methods->Measure(0);
  144. }
  145. internal unsafe struct JobMT : IJobParallelFor
  146. {
  147. [NativeDisableUnsafePtrRestriction] public T* methods;
  148. public void Execute(int index) => methods->Measure(index);
  149. }
  150. [BurstCompile(CompileSynchronously = true, DisableSafetyChecks = true)]
  151. internal unsafe struct JobBurstMT : IJobParallelFor
  152. {
  153. [NativeDisableUnsafePtrRestriction] public T* methods;
  154. public void Execute(int index) => methods->Measure(index);
  155. }
  156. [BurstCompile(CompileSynchronously = true, DisableSafetyChecks = false)]
  157. internal unsafe struct JobSafetyBurstMT : IJobParallelFor
  158. {
  159. [NativeDisableUnsafePtrRestriction] public T* methods;
  160. public void Execute(int index) => methods->Measure(index);
  161. }
  162. static unsafe void RunST(BenchmarkAllocatorType type, int baseSize, int allocations, params int[] args)
  163. {
  164. var methods = new T();
  165. methods.SetParams(args);
  166. switch (type)
  167. {
  168. case (BenchmarkAllocatorType)(BenchmarkAllocatorConfig.Temp):
  169. methods.CreateAllocator(Allocator.Temp);
  170. BenchmarkMeasure.Measure(typeof(T),
  171. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  172. () => new JobST { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule().Complete(),
  173. () => methods.Setup(1, baseSize, allocations), () => methods.Teardown());
  174. break;
  175. case (BenchmarkAllocatorType)(BenchmarkAllocatorConfig.TempJob):
  176. methods.CreateAllocator(Allocator.TempJob);
  177. BenchmarkMeasure.Measure(typeof(T),
  178. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  179. () => new JobST { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule().Complete(),
  180. () => methods.Setup(1, baseSize, allocations), () => methods.Teardown());
  181. break;
  182. case (BenchmarkAllocatorType)(BenchmarkAllocatorConfig.Persistent):
  183. methods.CreateAllocator(Allocator.Persistent);
  184. BenchmarkMeasure.Measure(typeof(T),
  185. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  186. () => new JobST { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule().Complete(),
  187. () => methods.Setup(1, baseSize, allocations), () => methods.Teardown());
  188. break;
  189. case BenchmarkAllocatorType.Managed:
  190. methods.CreateAllocator(Allocator.None);
  191. BenchmarkMeasure.Measure(typeof(T),
  192. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  193. () => new JobST { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule().Complete(),
  194. () => methods.Setup(1, baseSize, allocations), () => methods.Teardown());
  195. break;
  196. case BenchmarkAllocatorType.BurstSafety:
  197. methods.CreateAllocator(Allocator.None);
  198. BenchmarkMeasure.Measure(typeof(T),
  199. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  200. () => new JobSafetyBurstST { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Run(),
  201. () => methods.Setup(1, baseSize, allocations), () => methods.Teardown());
  202. break;
  203. case BenchmarkAllocatorType.BurstNoSafety:
  204. methods.CreateAllocator(Allocator.None);
  205. BenchmarkMeasure.Measure(typeof(T),
  206. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  207. () => new JobBurstST { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Run(),
  208. () => methods.Setup(1, baseSize, allocations), () => methods.Teardown());
  209. break;
  210. }
  211. methods.DestroyAllocator();
  212. }
  213. static unsafe void RunMT(BenchmarkAllocatorType type, int baseSize, int allocations, int workers, params int[] args)
  214. {
  215. var methods = new T();
  216. methods.SetParams(args);
  217. switch (type)
  218. {
  219. case (BenchmarkAllocatorType)(BenchmarkAllocatorConfig.Temp):
  220. methods.CreateAllocator(Allocator.Temp);
  221. BenchmarkMeasure.MeasureParallel(typeof(T),
  222. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  223. () => new JobMT { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule(workers, 1).Complete(),
  224. () => methods.Setup(workers, baseSize, allocations), () => methods.Teardown());
  225. break;
  226. case (BenchmarkAllocatorType)(BenchmarkAllocatorConfig.TempJob):
  227. methods.CreateAllocator(Allocator.TempJob);
  228. BenchmarkMeasure.MeasureParallel(typeof(T),
  229. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  230. () => new JobMT { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule(workers, 1).Complete(),
  231. () => methods.Setup(workers, baseSize, allocations), () => methods.Teardown());
  232. break;
  233. case (BenchmarkAllocatorType)(BenchmarkAllocatorConfig.Persistent):
  234. methods.CreateAllocator(Allocator.Persistent);
  235. BenchmarkMeasure.MeasureParallel(typeof(T),
  236. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  237. () => new JobMT { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule(workers, 1).Complete(),
  238. () => methods.Setup(workers, baseSize, allocations), () => methods.Teardown());
  239. break;
  240. case BenchmarkAllocatorType.Managed:
  241. methods.CreateAllocator(Allocator.None);
  242. BenchmarkMeasure.MeasureParallel(typeof(T),
  243. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  244. () => new JobMT { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule(workers, 1).Complete(),
  245. () => methods.Setup(workers, baseSize, allocations), () => methods.Teardown());
  246. break;
  247. case BenchmarkAllocatorType.BurstSafety:
  248. methods.CreateAllocator(Allocator.None);
  249. BenchmarkMeasure.MeasureParallel(typeof(T),
  250. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  251. () => new JobSafetyBurstMT { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule(workers, 1).Complete(),
  252. () => methods.Setup(workers, baseSize, allocations), () => methods.Teardown());
  253. break;
  254. case BenchmarkAllocatorType.BurstNoSafety:
  255. methods.CreateAllocator(Allocator.None);
  256. BenchmarkMeasure.MeasureParallel(typeof(T),
  257. BenchmarkAllocatorConfig.kCountWarmup, BenchmarkAllocatorConfig.kCountMeasure,
  258. () => new JobBurstMT { methods = (T*)UnsafeUtility.AddressOf(ref methods) }.Schedule(workers, 1).Complete(),
  259. () => methods.Setup(workers, baseSize, allocations), () => methods.Teardown());
  260. break;
  261. }
  262. methods.DestroyAllocator();
  263. }
  264. /// <summary>
  265. /// Called from a typical performance test method to provide both Performance Framework measurements as well as
  266. /// Benchmark Framework measurements. A typical usage is similar to:
  267. /// <c>[Test, Performance]<br />
  268. /// [Category("Performance")]<br />
  269. /// [BenchmarkTestFootnote]<br />
  270. /// public unsafe void FixedSize(<br />
  271. /// [Values(1, 2, 4, 8)] int workerThreads,<br />
  272. /// [Values(1024, 1024 * 1024)] int allocSize,<br />
  273. /// [Values] BenchmarkAllocatorType type)<br />
  274. /// {<br />
  275. /// BenchmarkAllocatorRunner&lt;Rewindable_FixedSize&gt;.Run(type, allocSize, workerThreads);<br />
  276. /// }</c>
  277. /// </summary>
  278. /// <param name="type">The benchmark or performance measurement type to run for allocators i.e. <see cref="BenchmarkAllocatorType.Managed"/> etc.</param>
  279. /// <param name="baseSize">The size to base allocations off of, whether fixed for all allocations, increasing in size, or anything else.</param>
  280. /// <param name="workers">The number of job workers to run performance tests on. These are duplicated across workers rather than split across workers.</param>
  281. /// <param name="args">Optional arguments that can be stored in a test implementation class.</param>
  282. public static unsafe void Run(BenchmarkAllocatorType type, int baseSize, int workers, params int[] args)
  283. {
  284. if (workers == 1)
  285. RunST(type, baseSize, BenchmarkAllocatorConfig.kCountAllocations, args);
  286. else
  287. RunMT(type, baseSize, BenchmarkAllocatorConfig.kCountAllocations, workers, args);
  288. }
  289. }
  290. /// <summary>
  291. /// A useful set of functionality commonly found in allocator performance and benchmark tests for most allocator types. Typically
  292. /// wrapped in a separate utility class for a set of tests to a specific allocator type.
  293. /// </summary>
  294. public struct BenchmarkAllocatorUtil
  295. {
  296. /// <summary>
  297. /// [worker][sequential allocation]<para />
  298. /// Used to store the pointer from allocations so it may be freed later.
  299. /// </summary>
  300. public NativeArray<NativeArray<IntPtr>> AllocPtr { get; private set; }
  301. /// <summary>
  302. /// [sequential allocation]<para />
  303. /// Used to store the size of allocations so it may be freed later, as some allocators require the size to be explicitly given when freed.
  304. /// Separate arrays for each worker are not provided because workers duplicate the same work rather than splitting it in some manner.
  305. /// </summary>
  306. public NativeArray<int> AllocSize { get; private set; }
  307. /// <summary>
  308. /// To be called prior to each measurement. Sets up the allocation and size storage used for freeing allocations, whether this happens
  309. /// during teardown following each measurement, or freeing is the functionality being measured itself.
  310. /// </summary>
  311. /// <param name="workers">The number of job workers to run performance tests on. These are duplicated across workers rather than split across workers.</param>
  312. /// <param name="baseSize">The size to base allocations off of, whether fixed for all allocations, increasing in size, or anything else.</param>
  313. /// <param name="growthRate">
  314. /// - If &lt; 0, a performance measurement's allocations start at the largest size and decrease linearly to the `baseSize`.
  315. /// - If &gt; 0, a performance measurement's allocations start at the `baseSize` and increase linearly
  316. /// - If 0, the allocation size is equivalent to the `baseSize` for all of a performance measurement's allocations
  317. /// </param>
  318. /// <param name="allocations">The number of allocations in a single measurement.</param>
  319. public void Setup(int workers, int baseSize, int growthRate, int allocations)
  320. {
  321. var allocStorage = new NativeArray<NativeArray<IntPtr>>(workers, Allocator.Persistent);
  322. for (int i = 0; i < workers; i++)
  323. allocStorage[i] = new NativeArray<IntPtr>(allocations, Allocator.Persistent);
  324. AllocPtr = allocStorage;
  325. var sizeStorage = new NativeArray<int>(allocations, Allocator.Persistent);
  326. for (int i = 0; i < allocations; i++)
  327. {
  328. if (growthRate >= 0)
  329. sizeStorage[i] = baseSize + growthRate * i;
  330. else
  331. sizeStorage[i] = baseSize + (-growthRate * (allocations - 1)) + growthRate * i;
  332. }
  333. AllocSize = sizeStorage;
  334. }
  335. /// <summary>
  336. /// To be called following each measurement. Frees the memory allocated in the <see cref="Setup(int, int, int, int)"/> method.
  337. /// This also frees the memory allocated by the given allocator using the stored information in this class.
  338. /// </summary>
  339. /// <param name="allocator">A handle to the allocator being measured.</param>
  340. unsafe public void Teardown(AllocatorManager.AllocatorHandle allocator)
  341. {
  342. if (AllocPtr.IsCreated)
  343. {
  344. for (int i = 0; i < AllocPtr.Length; i++)
  345. {
  346. var inner = AllocPtr[i];
  347. for (int j = 0; j < inner.Length; j++)
  348. {
  349. AllocatorManager.Free(allocator, (void*)inner[j], AllocSize[j], 0);
  350. inner[j] = IntPtr.Zero;
  351. }
  352. }
  353. }
  354. Teardown();
  355. }
  356. /// <summary>
  357. /// To be called following each measurement. Frees the memory allocated in the <see cref="Setup(int, int, int, int)"/> method.
  358. /// This does not free the memory allocated by a given allocator type used in measurement tests.
  359. /// </summary>
  360. public void Teardown()
  361. {
  362. if (AllocPtr.IsCreated)
  363. {
  364. for (int i = 0; i < AllocPtr.Length; i++)
  365. {
  366. if (AllocPtr[i].IsCreated)
  367. AllocPtr[i].Dispose();
  368. }
  369. AllocPtr.Dispose();
  370. }
  371. if (AllocSize.IsCreated)
  372. AllocSize.Dispose();
  373. }
  374. }
  375. }