| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | #if !UNITY_DOTSRUNTIME |
| | 6 | |
|
| | 7 | | using System.Text; |
| | 8 | | using UnityEngine.Profiling; |
| | 9 | |
|
| | 10 | | namespace GDX.Developer.Reports.Resource.Sections |
| | 11 | | { |
| | 12 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 13 | | public readonly struct MemorySection |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// The size of the Mono heap when the <see cref="ResourcesAuditReport" /> was created. |
| | 17 | | /// </summary> |
| | 18 | | /// <remarks>This is cached so that the <see cref="ResourcesAuditReport" /> does not effect this value.</remarks |
| | 19 | | public readonly long MonoHeapSize; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// The amount of the Mono heap used when the <see cref="ResourcesAuditReport" /> was created. |
| | 23 | | /// </summary> |
| | 24 | | /// <remarks>This is cached so that the <see cref="ResourcesAuditReport" /> does not effect this value.</remarks |
| | 25 | | public readonly long MonoUsedSize; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Unity's allocated native memory for the graphics driver (in bytes). |
| | 29 | | /// </summary> |
| | 30 | | public readonly long UnityGraphicsDriverAllocatedMemory; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Unity's total allocated memory (in bytes). |
| | 34 | | /// </summary> |
| | 35 | | public readonly long UnityTotalAllocatedMemory; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Unity's total reserved memory (in bytes). |
| | 39 | | /// </summary> |
| | 40 | | public readonly long UnityTotalReservedMemory; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Unity's total unused reserved memory (in bytes). |
| | 44 | | /// </summary> |
| | 45 | | public readonly long UnityTotalUnusedReservedMemory; |
| | 46 | |
|
| | 47 | | public MemorySection(long monoHeapSize, long monoUsedSize, long unityTotalAllocatedMemory, |
| | 48 | | long unityTotalReservedMemory, long unityTotalUnusedReservedMemory, long unityGraphicsDriverAllocatedMemory) |
| 6 | 49 | | { |
| 6 | 50 | | MonoHeapSize = monoHeapSize; |
| 6 | 51 | | MonoUsedSize = monoUsedSize; |
| 6 | 52 | | UnityTotalAllocatedMemory = unityTotalAllocatedMemory; |
| 6 | 53 | | UnityTotalReservedMemory = unityTotalReservedMemory; |
| 6 | 54 | | UnityTotalUnusedReservedMemory = unityTotalUnusedReservedMemory; |
| 6 | 55 | | UnityGraphicsDriverAllocatedMemory = unityGraphicsDriverAllocatedMemory; |
| 6 | 56 | | } |
| | 57 | |
|
| | 58 | | public static MemorySection Get() |
| 6 | 59 | | { |
| 6 | 60 | | return new MemorySection(Profiler.GetMonoHeapSizeLong(), Profiler.GetMonoUsedSizeLong(), |
| | 61 | | Profiler.GetTotalAllocatedMemoryLong(), Profiler.GetTotalReservedMemoryLong(), |
| | 62 | | Profiler.GetTotalUnusedReservedMemoryLong(), Profiler.GetAllocatedMemoryForGraphicsDriver()); |
| 6 | 63 | | } |
| | 64 | |
|
| | 65 | | public void Output(ResourceReportContext context, StringBuilder builder, |
| | 66 | | bool detailed = true) |
| 4 | 67 | | { |
| 4 | 68 | | builder.AppendLine(ResourceReport.CreateKeyValuePair(context, "Total Mono Heap", |
| | 69 | | Localization.GetHumanReadableFileSize(MonoHeapSize))); |
| 4 | 70 | | builder.AppendLine(ResourceReport.CreateKeyValuePair(context, "Used Mono Heap", |
| | 71 | | Localization.GetHumanReadableFileSize(MonoUsedSize))); |
| | 72 | |
|
| 4 | 73 | | if (detailed) |
| 4 | 74 | | { |
| 4 | 75 | | builder.AppendLine(ResourceReport.CreateKeyValuePair(context, "GFX Driver Allocated Memory", |
| | 76 | | Localization.GetHumanReadableFileSize(UnityGraphicsDriverAllocatedMemory))); |
| 4 | 77 | | builder.AppendLine(ResourceReport.CreateKeyValuePair(context, "Total Reserved Memory", |
| | 78 | | Localization.GetHumanReadableFileSize(UnityTotalReservedMemory))); |
| 4 | 79 | | builder.AppendLine(ResourceReport.CreateKeyValuePair(context, "Total Allocated Memory", |
| | 80 | | Localization.GetHumanReadableFileSize(UnityTotalAllocatedMemory))); |
| 4 | 81 | | builder.AppendLine(ResourceReport.CreateKeyValuePair(context, "Total Unused Reserved Memory", |
| | 82 | | Localization.GetHumanReadableFileSize(UnityTotalUnusedReservedMemory))); |
| 4 | 83 | | } |
| 4 | 84 | | } |
| | 85 | | } |
| | 86 | | } |
| | 87 | | #endif // !UNITY_DOTSRUNTIME |