mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-27 05:28:25 +01:00
SOURCE DUMP v1
This commit is contained in:
parent
bd62d51ec9
commit
fb20299e9d
2 changed files with 768360 additions and 0 deletions
141
brutal.texture/Brutal.Texture/Resources/ResourceTracker.cs
Normal file
141
brutal.texture/Brutal.Texture/Resources/ResourceTracker.cs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
|
||||
namespace Brutal.TextureApi;
|
||||
|
||||
/// <summary>
|
||||
/// Provides basic functionality for tracking resources and their disposal. Call Track() to record an instance
|
||||
/// of a resource and Untrack() to stop tracking.
|
||||
/// <para/>
|
||||
/// If you wish to see what resources are still being tracked, along with a formatted stacktrace of when they
|
||||
/// were tracked you can call <see cref="CurrentResources"/>.
|
||||
/// <para/>
|
||||
/// Tracking and untracking resource is threadsafe and can be done from different threads. ResourceTracker uses a
|
||||
/// <see cref="ConcurrentDictionary{TKey, TValue}" /> underneath and Interlocked calls to manage internal counts.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// ARKB: This is a very rudimentary implementation just so I could see the usage of textures in KSA. This class is internal
|
||||
/// for a reason and not directly usable outside Brutal for now.
|
||||
/// Making this a CriticalFinalizerObject means its finaliser will run after normal finalisers, giving
|
||||
/// a chance for things to Untrack first.</remarks>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
internal class ResourceTracker<T> : CriticalFinalizerObject
|
||||
where T : notnull
|
||||
{
|
||||
public readonly struct TrackedResource
|
||||
{
|
||||
public T Instance { get; init; }
|
||||
public StackTrace? StackTrace { get; init; }
|
||||
}
|
||||
|
||||
public ResourceTracker()
|
||||
{
|
||||
_trackedInstances = new(new Comparer());
|
||||
#if RecordTrackingOverhead
|
||||
_stopwatch = new Stopwatch();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Interlocked.Exchange(ref _totalResourcesTracked, 0);
|
||||
_trackedInstances.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The total number of resource items tracked since the tracker was
|
||||
/// created.
|
||||
/// </summary>
|
||||
public ulong TotalTrackedCount => Interlocked.Read(ref _totalResourcesTracked);
|
||||
|
||||
/// <summary>
|
||||
/// The number of resources currently being tracked. This is the difference of
|
||||
/// TotalTrackedCount and the number of successfully Untrack()'ed resources.
|
||||
/// </summary>
|
||||
public ulong CurrentTrackedCount => (ulong)_trackedInstances.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Track a new resource instance. The resource must NOT already be tracked.
|
||||
/// </summary>
|
||||
/// <param name="resource">The object to track</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the resource is already being tracked</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public void Track(in T resource)
|
||||
{
|
||||
#if RecordTrackingOverhead
|
||||
var start = Stopwatch.GetTimestamp();
|
||||
#endif
|
||||
if (!_trackedInstances.TryAdd(new() { Instance = resource, StackTrace = new StackTrace(1, true) }, 0))
|
||||
throw new InvalidOperationException($"Object of type {typeof(T).Name} already being tracked");
|
||||
Interlocked.Increment(ref _totalResourcesTracked);
|
||||
#if RecordTrackingOverhead
|
||||
_totalTimeSpentTracking += Stopwatch.GetTimestamp() - start;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop tracking a resource.
|
||||
/// </summary>
|
||||
/// <param name="resource">The resource to stop tracking</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if the resource isn't being tracked</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public void Untrack(in T resource)
|
||||
{
|
||||
#if RecordTrackingOverhead
|
||||
var start = Stopwatch.GetTimestamp();
|
||||
#endif
|
||||
if (!_trackedInstances.TryRemove(new() { Instance = resource }, out _))
|
||||
throw new InvalidOperationException($"Object {resource} not found in tracked instances.");
|
||||
#if RecordTrackingOverhead
|
||||
_totalTimeSpentTracking += Stopwatch.GetTimestamp() - start;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a collection of TrackedResource objects.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IReadOnlyCollection<TrackedResource> CurrentResources()
|
||||
{
|
||||
return _trackedInstances.AsReadOnly().Keys;
|
||||
}
|
||||
|
||||
public class Comparer : IEqualityComparer<TrackedResource>
|
||||
{
|
||||
public bool Equals(TrackedResource x, TrackedResource y)
|
||||
{
|
||||
return ReferenceEquals(x.Instance, y.Instance);
|
||||
}
|
||||
|
||||
public int GetHashCode(TrackedResource obj)
|
||||
{
|
||||
return RuntimeHelpers.GetHashCode(obj.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
#if RecordTrackingOverhead
|
||||
public void LogUnreleasedResources(int stackFrameCount)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Overhead: {_totalTimeSpentTracking / (float)Stopwatch.Frequency / _totalResourcesTracked * 1000000}us"
|
||||
);
|
||||
|
||||
if (_trackedInstances.IsEmpty)
|
||||
return;
|
||||
foreach (var instance in _trackedInstances.Keys)
|
||||
Console.WriteLine(
|
||||
$"Allocated here: {(instance.StackTrace is not null ? StackTraceToString(instance.StackTrace, stackFrameCount) : "no trace")}"
|
||||
);
|
||||
}
|
||||
#endif
|
||||
|
||||
private ulong _totalResourcesTracked = 0;
|
||||
private readonly ConcurrentDictionary<TrackedResource, byte> _trackedInstances;
|
||||
|
||||
#if RecordTrackingOverhead
|
||||
private readonly Stopwatch _stopwatch;
|
||||
public long _totalTimeSpentTracking = 0;
|
||||
#endif
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
using System.Diagnostics;
|
||||
|
||||
namespace Brutal.TextureApi
|
||||
{
|
||||
internal static class StackTraceFormatting
|
||||
{
|
||||
/// <summary>
|
||||
/// Takes a StackTrace and generates a string of the last X number of relevant stack frames.
|
||||
/// <para />
|
||||
/// The output starts at the first non-Brutal namespace stack frame.
|
||||
/// System functions (Those in System or Microsoft namespaces) are removed.
|
||||
/// </summary>
|
||||
/// <param name="stackTrace">The StackTrace to format</param>
|
||||
/// <param name="stackFrameCount">The number of relevant StackFrames to return in the string.</param>
|
||||
/// <returns></returns>
|
||||
public static IList<string> FriendlyStackTraceStrings(StackTrace? stackTrace, int stackFrameCount)
|
||||
{
|
||||
if (stackTrace is null)
|
||||
return ["No stack trace"];
|
||||
|
||||
var stackIndent = "";
|
||||
var stackTraceEntries = new List<string>();
|
||||
var startStackFrameIndex = GetFirstClientStackFrameIndex(stackTrace);
|
||||
|
||||
for (var i = startStackFrameIndex; i < stackTrace.FrameCount; i++)
|
||||
{
|
||||
var stackFrame = stackTrace.GetFrame(i)!;
|
||||
if (IsIgnoredStackFrame(stackFrame))
|
||||
continue;
|
||||
|
||||
stackTraceEntries.Add(BuildStackFrameString(stackIndent, stackFrame));
|
||||
if (--stackFrameCount == 0)
|
||||
break;
|
||||
|
||||
stackIndent += " ";
|
||||
}
|
||||
return stackTraceEntries;
|
||||
}
|
||||
|
||||
#region Private
|
||||
|
||||
private static string BuildStackFrameString(string stackIndent, StackFrame stackFrame)
|
||||
{
|
||||
return $"{stackIndent}{stackFrame.GetMethod()!.Name} : {stackFrame.GetFileName()} : {stackFrame.GetFileLineNumber()}";
|
||||
}
|
||||
|
||||
private static int GetFirstClientStackFrameIndex(StackTrace stackTrace)
|
||||
{
|
||||
for (var i = 0; i < stackTrace.FrameCount; i++)
|
||||
{
|
||||
var stackFrameNamespace = stackTrace.GetFrame(i)?.GetMethod()?.DeclaringType?.Namespace;
|
||||
if (stackFrameNamespace is null)
|
||||
continue;
|
||||
|
||||
if (!stackFrameNamespace.StartsWith("Brutal")) // This is a temporary lame filter
|
||||
return i;
|
||||
}
|
||||
return stackTrace.FrameCount;
|
||||
}
|
||||
|
||||
private static bool IsIgnoredStackFrame(StackFrame stackFrame)
|
||||
{
|
||||
if (!stackFrame.HasSource() || !stackFrame.HasMethod())
|
||||
return true;
|
||||
|
||||
var name = stackFrame.GetMethod()?.DeclaringType?.Assembly.FullName;
|
||||
return name is null || name.StartsWith("Microsoft") || name.StartsWith("System");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue