| | 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.Reflection; |
| | 10 | | using System.Text; |
| | 11 | | using GDX.Developer.Reports.Resource; |
| | 12 | | using GDX.Developer.Reports.Resource.Objects; |
| | 13 | | using GDX.Developer.Reports.Resource.Sections; |
| | 14 | | using UnityEngine; |
| | 15 | | using UnityEngine.Profiling; |
| | 16 | | using Object = UnityEngine.Object; |
| | 17 | |
|
| | 18 | | namespace GDX.Developer.Reports |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// An audit of loaded <see cref="UnityEngine.Object" /> for queried types. |
| | 22 | | /// </summary> |
| | 23 | | /// <remarks> |
| | 24 | | /// Information is referenced to the target objects by a modified weak reference (<see cref="TransientReference" |
| | 25 | | /// thus this will not prevent garbage collection. |
| | 26 | | /// </remarks> |
| | 27 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 28 | | public class ResourcesAuditReport : ResourceReport |
| | 29 | | { |
| 6 | 30 | | public readonly ApplicationSection ApplicationContext = ApplicationSection.Get(); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// A collection of known (loaded in memory) <see cref="UnityEngine.Object" /> keyed by type. |
| | 34 | | /// </summary> |
| 6 | 35 | | public readonly Dictionary<Type, Dictionary<TransientReference, ObjectInfo>> KnownObjects = |
| | 36 | | new Dictionary<Type, Dictionary<TransientReference, ObjectInfo>>(); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// A collection of known <see cref="UnityEngine.Object" /> types in memory and their total usage. |
| | 40 | | /// </summary> |
| 6 | 41 | | public readonly Dictionary<Type, long> KnownUsage = new Dictionary<Type, long>(); |
| | 42 | |
|
| 6 | 43 | | public readonly MemorySection MemoryContext = MemorySection.Get(); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// The last time that the <see cref="ResourcesAuditReport" /> has had a query of types. |
| | 47 | | /// </summary> |
| 6 | 48 | | public DateTime LastTouched = DateTime.Now; |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// The total number of objects which are known to the <see cref="ResourcesAuditReport" />. |
| | 52 | | /// </summary> |
| | 53 | | public int ObjectCount; |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Process to destroy a <see cref="ResourcesAuditReport" />. |
| | 57 | | /// </summary> |
| | 58 | | ~ResourcesAuditReport() |
| 0 | 59 | | { |
| 0 | 60 | | KnownObjects.Clear(); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <inheritdoc /> |
| | 64 | | public override bool Output(StringBuilder builder, ResourceReportContext context = null) |
| 4 | 65 | | { |
| | 66 | | // We need to make the context if its not provided |
| 4 | 67 | | context ??= new ResourceReportContext(); |
| | 68 | |
|
| | 69 | | // Create header |
| 4 | 70 | | builder.AppendLine(CreateHeader(context, "START: Resources Audit Report")); |
| | 71 | |
|
| | 72 | | // Add standard report information |
| 4 | 73 | | ApplicationContext.Output(context, builder); |
| | 74 | |
|
| | 75 | | // Custom header information |
| 4 | 76 | | builder.AppendLine(CreateKeyValuePair(context, "Last Touched", |
| | 77 | | LastTouched.ToString(Localization.LocalTimestampFormat))); |
| 4 | 78 | | builder.AppendLine(CreateKeyValuePair(context, "Total Objects", ObjectCount.ToString())); |
| | 79 | |
|
| 4 | 80 | | builder.AppendLine(); |
| | 81 | |
|
| | 82 | | // Add memory information |
| 4 | 83 | | MemoryContext.Output(context, builder); |
| 4 | 84 | | builder.AppendLine(); |
| | 85 | |
|
| | 86 | | // We iterate over each defined type in the order they were added to the known objects |
| 80 | 87 | | foreach (KeyValuePair<Type, Dictionary<TransientReference, ObjectInfo>> knownObject in KnownObjects) |
| 34 | 88 | | { |
| 34 | 89 | | int count = knownObject.Value.Count; |
| | 90 | |
|
| 34 | 91 | | builder.AppendLine(CreateHeader(context, knownObject.Key.ToString(), '-')); |
| 34 | 92 | | builder.AppendLine(CreateKeyValuePair(context, "Count", count.ToString())); |
| 34 | 93 | | builder.AppendLine(CreateKeyValuePair(context, "Total Size", |
| | 94 | | Localization.GetHumanReadableFileSize(KnownUsage[knownObject.Key]))); |
| 34 | 95 | | builder.AppendLine(); |
| | 96 | |
|
| | 97 | | // Sort the known objects based on size as that's the most useful context to have them listed |
| 34 | 98 | | List<ObjectInfo> newList = new List<ObjectInfo>(count); |
| 19990 | 99 | | foreach (KeyValuePair<TransientReference, ObjectInfo> objectInfo in knownObject.Value) |
| 9944 | 100 | | { |
| 9944 | 101 | | newList.Add(objectInfo.Value); |
| 9944 | 102 | | } |
| | 103 | |
|
| 34 | 104 | | newList.Sort(); |
| | 105 | |
|
| | 106 | | // Output each item |
| 19956 | 107 | | for (int i = 0; i < count; i++) |
| 9944 | 108 | | { |
| 9944 | 109 | | newList[i].Output(context, builder); |
| 9944 | 110 | | } |
| | 111 | |
|
| 34 | 112 | | builder.AppendLine(); |
| 34 | 113 | | } |
| | 114 | |
|
| | 115 | | // Footer |
| 4 | 116 | | builder.AppendLine(CreateHeader(context, "END: Resources Audit Report")); |
| | 117 | |
|
| 4 | 118 | | return true; |
| 4 | 119 | | } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Identify loaded <see cref="UnityEngine.Object" /> of the description provided in <paramref cref="query" |
| | 123 | | /// </summary> |
| | 124 | | /// <remarks> |
| | 125 | | /// This method of querying uses reflection to allow for dynamic developer console calls, |
| | 126 | | /// <see cref="QueryForType{TType,TObjectInfo}" /> for a much faster typed operation. |
| | 127 | | /// </remarks> |
| | 128 | | /// <param name="query">Description of <see cref="UnityEngine.Object" /> type to search for.</param> |
| | 129 | | public void Query(ResourcesQuery query) |
| 2 | 130 | | { |
| | 131 | | // Attempt to try and make a type based on the full name (+namespace), and the assembly. |
| | 132 | | // ex: UnityEngine.Texture2D,UnityEngine |
| 2 | 133 | | Type typeActual = Type.GetType(query.TypeDefinition, false); |
| | 134 | |
|
| | 135 | | // If we actually got a valid type |
| 2 | 136 | | if (typeActual == null) |
| 0 | 137 | | { |
| 0 | 138 | | return; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | // Create our ObjectInfo type, defaulting if invalid |
| 2 | 142 | | Type objectInfoActual = null; |
| 2 | 143 | | if (query.ObjectInfoTypeDefinition != null) |
| 1 | 144 | | { |
| 1 | 145 | | objectInfoActual = Type.GetType(query.ObjectInfoTypeDefinition, false); |
| 1 | 146 | | } |
| | 147 | |
|
| 2 | 148 | | objectInfoActual ??= ObjectInfoFactory.GetObjectInfoType(typeActual); |
| | 149 | |
|
| | 150 | |
|
| | 151 | | // Build out using reflection (yes bad, but you choose this). |
| 2 | 152 | | MethodInfo method = typeof(ResourcesAuditReport).GetMethod(nameof(QueryForType)); |
| | 153 | |
|
| | 154 | | // Did we find the method? |
| 2 | 155 | | if (method is null) |
| 0 | 156 | | { |
| 0 | 157 | | return; |
| | 158 | | } |
| | 159 | |
|
| 2 | 160 | | MethodInfo generic = method.MakeGenericMethod(typeActual, objectInfoActual); |
| | 161 | |
|
| | 162 | | // Invoke the method on our container |
| 2 | 163 | | generic.Invoke(this, new object[] { query.NameFilter }); |
| | 164 | |
|
| 2 | 165 | | LastTouched = DateTime.Now; |
| 2 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Identify loaded <typeparamref name="TType" />, using <typeparamref name="TObjectInfo" /> for report gene |
| | 170 | | /// </summary> |
| | 171 | | /// <typeparam name="TType">The object type to query for.</typeparam> |
| | 172 | | /// <typeparam name="TObjectInfo">The <see cref="ObjectInfo" /> used to generate report entries.</typeparam> |
| | 173 | | public void QueryForType<TType, TObjectInfo>(string nameFilter = null) |
| | 174 | | where TType : Object |
| | 175 | | where TObjectInfo : ObjectInfo, new() |
| 37 | 176 | | { |
| | 177 | | // Find any matching resources |
| 37 | 178 | | TType[] foundLoadedObjects = Resources.FindObjectsOfTypeAll<TType>(); |
| 37 | 179 | | Type typeClass = typeof(TType); |
| | 180 | |
|
| | 181 | | // Make sure the category exists |
| 37 | 182 | | if (!KnownObjects.ContainsKey(typeClass)) |
| 37 | 183 | | { |
| 37 | 184 | | KnownObjects.Add(typeClass, new Dictionary<TransientReference, ObjectInfo>()); |
| 37 | 185 | | } |
| | 186 | |
|
| 37 | 187 | | if (!KnownUsage.ContainsKey(typeClass)) |
| 37 | 188 | | { |
| 37 | 189 | | KnownUsage.Add(typeClass, 0); |
| 37 | 190 | | } |
| | 191 | |
|
| | 192 | | // Get reference to the dictionary for the specified category |
| 37 | 193 | | Dictionary<TransientReference, ObjectInfo> typeObjects = KnownObjects[typeClass]; |
| | 194 | |
|
| 37 | 195 | | bool evaluateNames = !string.IsNullOrEmpty(nameFilter); |
| 37 | 196 | | int count = foundLoadedObjects.Length; |
| 36018 | 197 | | for (int i = 0; i < count; i++) |
| 17972 | 198 | | { |
| 17972 | 199 | | Object foundObject = foundLoadedObjects[i]; |
| | 200 | |
|
| | 201 | | // If we have a provided filter, and our objects name doesnt contain the filter, skip |
| 17972 | 202 | | if (evaluateNames && !foundObject.name.Contains(nameFilter)) |
| 0 | 203 | | { |
| 0 | 204 | | continue; |
| | 205 | | } |
| | 206 | |
|
| 17972 | 207 | | TransientReference pseudoWeakReference = new TransientReference(foundObject); |
| | 208 | |
|
| | 209 | | // We can use the hashcode of a specific type at this level to determine duplication |
| 17972 | 210 | | if (typeObjects.ContainsKey(pseudoWeakReference)) |
| 0 | 211 | | { |
| 0 | 212 | | ObjectInfo foundObjectInfo = typeObjects[pseudoWeakReference]; |
| | 213 | |
|
| | 214 | | // Increment copy count |
| 0 | 215 | | foundObjectInfo.CopyCount++; |
| | 216 | |
|
| | 217 | | // We actively not going to use the existing size, in case the copy is different. |
| 0 | 218 | | long usage = Profiler.GetRuntimeMemorySizeLong(foundObject); |
| 0 | 219 | | foundObjectInfo.TotalMemoryUsage += usage; |
| 0 | 220 | | KnownUsage[typeClass] += usage; |
| 0 | 221 | | } |
| | 222 | | else |
| 17972 | 223 | | { |
| 17972 | 224 | | TObjectInfo objectInfo = new TObjectInfo(); |
| 17972 | 225 | | objectInfo.Populate(foundObject, pseudoWeakReference); |
| 17972 | 226 | | typeObjects.Add(pseudoWeakReference, objectInfo); |
| | 227 | |
|
| | 228 | | // Add to size |
| 17972 | 229 | | KnownUsage[typeClass] += objectInfo.MemoryUsage; |
| 17972 | 230 | | ObjectCount++; |
| 17972 | 231 | | } |
| 17972 | 232 | | } |
| | 233 | |
|
| 37 | 234 | | LastTouched = DateTime.Now; |
| 37 | 235 | | } |
| | 236 | |
|
| | 237 | | /// <summary> |
| | 238 | | /// Remove all information regarding a specific <paramref name="type" /> from the <see cref="KnownObjects" / |
| | 239 | | /// </summary> |
| | 240 | | /// <param name="type">The type to remove from the <see cref="KnownObjects" />.</param> |
| | 241 | | public void Remove(Type type) |
| 0 | 242 | | { |
| 0 | 243 | | if (KnownObjects.ContainsKey(type)) |
| 0 | 244 | | { |
| | 245 | | // Decrement the object count |
| 0 | 246 | | ObjectCount -= KnownObjects.Count; |
| | 247 | |
|
| | 248 | | // Remove the known objects type |
| 0 | 249 | | KnownObjects.Remove(type); |
| 0 | 250 | | } |
| | 251 | |
|
| | 252 | | // Remove the size data |
| 0 | 253 | | if (KnownUsage.ContainsKey(type)) |
| 0 | 254 | | { |
| 0 | 255 | | KnownUsage.Remove(type); |
| 0 | 256 | | } |
| 0 | 257 | | } |
| | 258 | |
|
| | 259 | | /// <summary> |
| | 260 | | /// Generate a <see cref="ResourcesAuditReport" /> for the provided <see cref="ResourcesQuery" /> array. |
| | 261 | | /// </summary> |
| | 262 | | /// <remarks> |
| | 263 | | /// This method uses reflection to generate the necessary typed parameters, its often more efficient to |
| | 264 | | /// create your own custom reports like in <see cref="GetCommon" />. |
| | 265 | | /// </remarks> |
| | 266 | | /// <param name="queries">A list of <see cref="ResourcesQuery" /> to generate a report from.</param> |
| | 267 | | /// <returns>A <see cref="ResourcesAuditReport" /> containing the outlined types.</returns> |
| | 268 | | public static ResourcesAuditReport Get(ResourcesQuery[] queries) |
| 1 | 269 | | { |
| | 270 | | // Create our collection object, this is going to effect memory based on its size |
| 1 | 271 | | ResourcesAuditReport resourcesAuditReport = new ResourcesAuditReport(); |
| | 272 | |
|
| | 273 | | // Types to actual? |
| 1 | 274 | | int count = queries.Length; |
| 6 | 275 | | for (int i = 0; i < count; i++) |
| 2 | 276 | | { |
| 2 | 277 | | resourcesAuditReport.Query(queries[i]); |
| 2 | 278 | | } |
| | 279 | |
|
| 1 | 280 | | return resourcesAuditReport; |
| 1 | 281 | | } |
| | 282 | |
|
| | 283 | | /// <summary> |
| | 284 | | /// Get a <see cref="ResourcesAuditReport" /> of all <see cref="UnityEngine.Object" />. |
| | 285 | | /// </summary> |
| | 286 | | /// <returns>A <see cref="ResourcesAuditReport" /> of all objects.</returns> |
| | 287 | | public static ResourcesAuditReport GetAll() |
| 2 | 288 | | { |
| 2 | 289 | | ResourcesAuditReport resourcesAuditReport = new ResourcesAuditReport(); |
| 2 | 290 | | resourcesAuditReport.QueryForType<Object, ObjectInfo>(); |
| 2 | 291 | | return resourcesAuditReport; |
| 2 | 292 | | } |
| | 293 | |
|
| | 294 | | /// <summary> |
| | 295 | | /// Get a <see cref="ResourcesAuditReport" /> of a common subset of data which usually eats a large portion |
| | 296 | | /// memory, and often can reveal memory leaks. |
| | 297 | | /// </summary> |
| | 298 | | /// <returns>A <see cref="ResourcesAuditReport" /> of textures, shaders, materials and animations.</returns> |
| | 299 | | public static ResourcesAuditReport GetCommon() |
| 3 | 300 | | { |
| | 301 | | // Create our collection object, this is going to effect memory based on its size |
| 3 | 302 | | ResourcesAuditReport resourcesAuditReport = new ResourcesAuditReport(); |
| | 303 | |
|
| | 304 | | // Sections |
| 3 | 305 | | resourcesAuditReport.QueryForType<RenderTexture, TextureObjectInfo>(); |
| 3 | 306 | | resourcesAuditReport.QueryForType<Texture3D, TextureObjectInfo>(); |
| 3 | 307 | | resourcesAuditReport.QueryForType<Texture2D, TextureObjectInfo>(); |
| 3 | 308 | | resourcesAuditReport.QueryForType<Texture2DArray, TextureObjectInfo>(); |
| 3 | 309 | | resourcesAuditReport.QueryForType<Cubemap, TextureObjectInfo>(); |
| 3 | 310 | | resourcesAuditReport.QueryForType<CubemapArray, TextureObjectInfo>(); |
| 3 | 311 | | resourcesAuditReport.QueryForType<Shader, ShaderObjectInfo>(); |
| 3 | 312 | | resourcesAuditReport.QueryForType<Material, ObjectInfo>(); |
| 3 | 313 | | resourcesAuditReport.QueryForType<Mesh, MeshObjectInfo>(); |
| 3 | 314 | | resourcesAuditReport.QueryForType<AnimationClip, ObjectInfo>(); |
| 3 | 315 | | resourcesAuditReport.QueryForType<AssetBundle, AssetBundleObjectInfo>(); |
| | 316 | |
|
| 3 | 317 | | return resourcesAuditReport; |
| 3 | 318 | | } |
| | 319 | |
|
| | 320 | | /// <summary> |
| | 321 | | /// A structure that defines the string inputs necessary to query for loaded resources of a specific type. |
| | 322 | | /// </summary> |
| | 323 | | /// <remarks> |
| | 324 | | /// This forces the path through reflection when querying; there are faster methods available. These queries |
| | 325 | | /// built ideally to support dynamic developer console calls. |
| | 326 | | /// </remarks> |
| | 327 | | public readonly struct ResourcesQuery |
| | 328 | | { |
| | 329 | | /// <summary> |
| | 330 | | /// The fully qualified type that is going to be evaluated. |
| | 331 | | /// </summary> |
| | 332 | | /// <example> |
| | 333 | | /// UnityEngine.Texture2D,UnityEngine |
| | 334 | | /// </example> |
| | 335 | | public readonly string TypeDefinition; |
| | 336 | |
|
| | 337 | | /// <summary> |
| | 338 | | /// The fully qualified type that is going to be used to generate a report on the type. |
| | 339 | | /// </summary> |
| | 340 | | /// <example> |
| | 341 | | /// GDX.Developer.Reports.ObjectInfo,GDX |
| | 342 | | /// </example> |
| | 343 | | public readonly string ObjectInfoTypeDefinition; |
| | 344 | |
|
| | 345 | |
|
| | 346 | | /// <summary> |
| | 347 | | /// A <see cref="string" /> check against a <see cref="UnityEngine.Object" /> name. |
| | 348 | | /// </summary> |
| | 349 | | /// <example> |
| | 350 | | /// Armor |
| | 351 | | /// </example> |
| | 352 | | public readonly string NameFilter; |
| | 353 | |
|
| | 354 | | /// <summary> |
| | 355 | | /// Create a <see cref="ResourcesQuery" /> for the given <paramref name="typeDefinition" />, while |
| | 356 | | /// attempting to use the provided <paramref name="objectInfoTypeDefinition" /> for report generation. |
| | 357 | | /// </summary> |
| | 358 | | /// <remarks> |
| | 359 | | /// Uses the default <see cref="ObjectInfo" /> for report generation if |
| | 360 | | /// <paramref name="objectInfoTypeDefinition" /> fails to qualify. |
| | 361 | | /// </remarks> |
| | 362 | | /// <param name="typeDefinition">The fully qualified type that is going to be evaluated.</param> |
| | 363 | | /// <param name="objectInfoTypeDefinition"> |
| | 364 | | /// The fully qualified type that is going to be used to generate a report on the |
| | 365 | | /// type. If left null, system will attempt to find an appropriate info generator. |
| | 366 | | /// </param> |
| | 367 | | /// <param name="nameFilter"> |
| | 368 | | /// A string that must be contained in an objects name for it to be valid in the query. |
| | 369 | | /// </param> |
| | 370 | | /// <example> |
| | 371 | | /// var queryTexture2D = new ResourcesQuery("UnityEngine.Texture2D,UnityEngine", |
| | 372 | | /// "GDX.Developer.Reports.ObjectInfos.TextureObjectInfo,GDX", "Armor"); |
| | 373 | | /// </example> |
| | 374 | | public ResourcesQuery(string typeDefinition, string objectInfoTypeDefinition = null, |
| | 375 | | string nameFilter = null) |
| 2 | 376 | | { |
| 2 | 377 | | TypeDefinition = typeDefinition; |
| 2 | 378 | | ObjectInfoTypeDefinition = objectInfoTypeDefinition; |
| 2 | 379 | | NameFilter = nameFilter; |
| 2 | 380 | | } |
| | 381 | | } |
| | 382 | | } |
| | 383 | | } |
| | 384 | | #endif // !UNITY_DOTSRUNTIME |