| | 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; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.IO; |
| | 10 | | using UnityEngine.Profiling; |
| | 11 | | #if UNITY_2022_2_OR_NEWER |
| | 12 | | using Unity.Profiling.Memory; |
| | 13 | |
|
| | 14 | | #else |
| | 15 | | using UnityEngine.Profiling.Memory.Experimental; |
| | 16 | | #endif // UNITY_2022_2_OR_NEWER |
| | 17 | |
|
| | 18 | | namespace GDX.Developer |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// A set of functionality useful for creating profiling data to reason about the performance of an application. |
| | 22 | | /// </summary> |
| | 23 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 24 | | public class Profiling |
| | 25 | | { |
| | 26 | | /// <summary> |
| | 27 | | /// The prefix to use with all capture files. |
| | 28 | | /// </summary> |
| | 29 | | const string k_MemoryCaptureFilePrefix = "MemCap-"; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// The number of memory captures to keep in the output folder. |
| | 33 | | /// </summary> |
| | 34 | | const int k_MemoryCapturesToKeep = 10; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// The number of profile captures to keep in the output folder. |
| | 38 | | /// </summary> |
| | 39 | | const int k_ProfilesToKeep = 10; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// The prefix to use with all binary profile files. |
| | 43 | | /// </summary> |
| | 44 | | const string k_ProfileFilePrefix = "Profile-"; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// The default flags (all) used when capturing memory. |
| | 48 | | /// </summary> |
| | 49 | | const CaptureFlags k_AllCaptureFlags = CaptureFlags.ManagedObjects | CaptureFlags.NativeAllocations | |
| | 50 | | CaptureFlags.NativeObjects | CaptureFlags.NativeAllocationSites | |
| | 51 | | CaptureFlags.NativeStackTraces; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Take a memory snapshot and save it to <see cref="Platform.GetOutputFolder" />. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="prefix">Override the default prefix <see cref="k_MemoryCaptureFilePrefix" />.</param> |
| | 57 | | /// <param name="finishCallback">Optional callback action once the memory capture has been made.</param> |
| | 58 | | /// <param name="captureFlags">Override of the memory capture flags, defaults to <see cref="k_AllCaptureFlags" / |
| | 59 | | /// <param name="manageCaptures">Should the number of captures found in the output folder be managed?</param> |
| | 60 | | public static void TakeMemorySnapshot(string prefix = null, Action<string, bool> finishCallback = null, |
| | 61 | | CaptureFlags captureFlags = k_AllCaptureFlags, bool manageCaptures = true) |
| 0 | 62 | | { |
| 0 | 63 | | string outputFolder = Platform.GetOutputFolder(); |
| 0 | 64 | | if (manageCaptures) |
| 0 | 65 | | { |
| 0 | 66 | | string[] files = Directory.GetFiles(outputFolder, |
| | 67 | | prefix == null ? $"{k_MemoryCaptureFilePrefix}*" : $"{k_MemoryCaptureFilePrefix}{prefix}-*", |
| | 68 | | SearchOption.TopDirectoryOnly); |
| 0 | 69 | | int filesToRemove = files.Length - (k_MemoryCapturesToKeep - 1); |
| | 70 | |
|
| 0 | 71 | | if (filesToRemove > 0) |
| 0 | 72 | | { |
| 0 | 73 | | List<string> fileList = new List<string>(files.Length); |
| 0 | 74 | | fileList.AddRange(files); |
| 0 | 75 | | fileList.Sort(); |
| | 76 | |
|
| 0 | 77 | | for (int i = 0; i < filesToRemove; i++) |
| 0 | 78 | | { |
| 0 | 79 | | Platform.ForceDeleteFile(fileList[i]); |
| 0 | 80 | | } |
| 0 | 81 | | } |
| 0 | 82 | | } |
| | 83 | |
|
| 0 | 84 | | string path = Path.Combine(outputFolder, |
| | 85 | | prefix != null |
| | 86 | | ? $"{k_MemoryCaptureFilePrefix}{prefix}-{DateTime.Now:GDX.Platform.FilenameTimestampFormat}.snap" |
| | 87 | | : $"{k_MemoryCaptureFilePrefix}{DateTime.Now:GDX.Platform.FilenameTimestampFormat}.raw"); |
| 0 | 88 | | MemoryProfiler.TakeSnapshot(path, finishCallback, captureFlags); |
| 0 | 89 | | UnityEngine.Debug.Log($"[MemorySnapshot] {path}"); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Setup a profiling session used during an import. This will create a binary file when finished profiling. |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="prefix">Optional descriptor for profile run used in filename.</param> |
| | 96 | | /// <param name="manageProfiles">Should the number of profiles be managed.</param> |
| | 97 | | public static void StartProfiling(string prefix = null, bool manageProfiles = true) |
| 0 | 98 | | { |
| | 99 | | // Make sure it is off |
| 0 | 100 | | Profiler.enabled = false; |
| | 101 | |
|
| 0 | 102 | | string outputFolder = Platform.GetOutputFolder(); |
| 0 | 103 | | if (manageProfiles) |
| 0 | 104 | | { |
| 0 | 105 | | string[] files = Directory.GetFiles(outputFolder, |
| | 106 | | prefix == null ? $"{k_ProfileFilePrefix}*" : $"{k_ProfileFilePrefix}{prefix}-*", |
| | 107 | | SearchOption.TopDirectoryOnly); |
| | 108 | | // If we have 15 files, and our max is 15, we need to remove just one. |
| 0 | 109 | | int filesToRemove = files.Length - (k_ProfilesToKeep - 1); |
| | 110 | |
|
| 0 | 111 | | if (filesToRemove > 0) |
| 0 | 112 | | { |
| 0 | 113 | | List<string> fileList = new List<string>(files.Length); |
| 0 | 114 | | fileList.AddRange(files); |
| 0 | 115 | | fileList.Sort(); |
| | 116 | |
|
| 0 | 117 | | for (int i = 0; i < filesToRemove; i++) |
| 0 | 118 | | { |
| 0 | 119 | | Platform.ForceDeleteFile(fileList[i]); |
| 0 | 120 | | } |
| 0 | 121 | | } |
| 0 | 122 | | } |
| | 123 | |
|
| 0 | 124 | | string path = Path.Combine(outputFolder, |
| | 125 | | prefix != null |
| | 126 | | ? $"{k_ProfileFilePrefix}{prefix}-{Platform.FilenameTimestampFormat}.raw" |
| | 127 | | : $"{k_ProfileFilePrefix}{Platform.FilenameTimestampFormat}.raw"); |
| 0 | 128 | | UnityEngine.Debug.Log($"[Profiling Started] {path}"); |
| 0 | 129 | | Profiler.logFile = path; |
| 0 | 130 | | Profiler.enableBinaryLog = true; |
| 0 | 131 | | Profiler.enabled = true; |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// Finalize a profiling session during an import. |
| | 136 | | /// </summary> |
| | 137 | | public static void StopProfiling() |
| 0 | 138 | | { |
| 0 | 139 | | UnityEngine.Debug.Log($"[Profiling Stopped] {Profiler.logFile}"); |
| 0 | 140 | | Profiler.enabled = false; |
| 0 | 141 | | Profiler.logFile = ""; |
| 0 | 142 | | } |
| | 143 | | } |
| | 144 | | } |
| | 145 | | #endif // !UNITY_DOTSRUNTIME |