using System; using System.Diagnostics; using System.Reflection; using Blacklight.Core; namespace Konis.Testbed { /// /// What each pass of a frame costs, averaged over a window. The frame interval alone says little under vsync, so /// the passes are timed apart. /// public class FrameStats { /// /// How long a window runs before its averages are published. /// public const double WindowSeconds = 0.5; // This project targets netstandard2.0, which predates GC.GetAllocatedBytesForCurrentThread, so it is bound by // reflection. A runtime without it reports zero. private static readonly Func allocatedOnThisThread = BindAllocationCounter(); private readonly Stopwatch clock = Stopwatch.StartNew(); private TimeSpan? frameStartedAt; private TimeSpan passStartedAt; private TimeSpan interval; private bool hasInterval; private long allocatedAtStart; private int gen0AtStart; private int gen1AtStart; private int gen2AtStart; private TimeSpan simulation; private TimeSpan interfaceUpdate; private TimeSpan draw; private TimeSpan simulationInWindow; private TimeSpan interfaceInWindow; private TimeSpan drawInWindow; private TimeSpan submitInWindow; private TimeSpan gpuWaitInWindow; private TimeSpan vsyncWaitInWindow; private TimeSpan intervalInWindow; private long allocatedInWindow; private int gen0InWindow; private int gen1InWindow; private int gen2InWindow; private int framesInWindow; private static Func BindAllocationCounter() { MethodInfo counter = typeof(GC).GetMethod("GetAllocatedBytesForCurrentThread", BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null); if (counter == null) { return () => 0; } return (Func)counter.CreateDelegate(typeof(Func)); } public FrameStats(VerticalSyncMode verticalSync) { VerticalSync = verticalSync; } /// /// Gets the vsync mode the window presents with. /// public VerticalSyncMode VerticalSync { get; } /// /// Gets whether the surface should time how long the cpu waits on the gpu. With vsync off the wait would only /// slow the frame it measures. /// public bool MeasuresGpuWait => VerticalSync != VerticalSyncMode.Off; /// /// Gets how many windows have finished, which changes whenever the averages do. /// public int Windows { get; private set; } /// /// Gets the average milliseconds a frame spent advancing the particle world. /// public double SimulationMilliseconds { get; private set; } /// /// Gets the average milliseconds a frame spent updating the UI. /// public double InterfaceMilliseconds { get; private set; } /// /// Gets the average milliseconds a frame spent queueing draws. /// public double DrawMilliseconds { get; private set; } /// /// Gets the average milliseconds a frame spent handing its draws to the driver. /// public double SubmitMilliseconds { get; private set; } /// /// Gets the average milliseconds a frame spent waiting on the gpu. /// public double GpuWaitMilliseconds { get; private set; } /// /// Gets the average milliseconds a frame spent waiting on the display. With vsync off it is the bare present. /// public double VSyncWaitMilliseconds { get; private set; } /// /// Gets the average milliseconds between the starts of two frames. /// public double IntervalMilliseconds { get; private set; } /// /// Gets the average bytes a frame allocated. /// public double AllocatedBytes { get; private set; } /// /// Gets how many generation 0 collections ran in the window. /// public int Gen0Collections { get; private set; } /// /// Gets how many generation 1 collections ran in the window. /// public int Gen1Collections { get; private set; } /// /// Gets how many generation 2 collections ran in the window. /// public int Gen2Collections { get; private set; } private TimeSpan EndPass() { TimeSpan now = clock.Elapsed; TimeSpan taken = now - passStartedAt; passStartedAt = now; return taken; } private static double Average(TimeSpan total, int frames) { return total.TotalMilliseconds / frames; } private void Publish() { SimulationMilliseconds = Average(simulationInWindow, framesInWindow); InterfaceMilliseconds = Average(interfaceInWindow, framesInWindow); DrawMilliseconds = Average(drawInWindow, framesInWindow); SubmitMilliseconds = Average(submitInWindow, framesInWindow); GpuWaitMilliseconds = Average(gpuWaitInWindow, framesInWindow); VSyncWaitMilliseconds = Average(vsyncWaitInWindow, framesInWindow); IntervalMilliseconds = Average(intervalInWindow, framesInWindow); AllocatedBytes = (double)allocatedInWindow / framesInWindow; Gen0Collections = gen0InWindow; Gen1Collections = gen1InWindow; Gen2Collections = gen2InWindow; Windows++; simulationInWindow = TimeSpan.Zero; interfaceInWindow = TimeSpan.Zero; drawInWindow = TimeSpan.Zero; submitInWindow = TimeSpan.Zero; gpuWaitInWindow = TimeSpan.Zero; vsyncWaitInWindow = TimeSpan.Zero; intervalInWindow = TimeSpan.Zero; allocatedInWindow = 0; gen0InWindow = 0; gen1InWindow = 0; gen2InWindow = 0; framesInWindow = 0; } /// /// Starts timing a frame. Call it first, then each pass's end in the order the passes run. /// public void BeginFrame() { TimeSpan now = clock.Elapsed; hasInterval = frameStartedAt.HasValue; if (hasInterval) { interval = now - frameStartedAt.Value; } frameStartedAt = now; passStartedAt = now; allocatedAtStart = allocatedOnThisThread(); gen0AtStart = GC.CollectionCount(0); gen1AtStart = GC.CollectionCount(1); gen2AtStart = GC.CollectionCount(2); } /// /// Ends the pass that advanced the particle world. /// public void EndSimulation() { simulation = EndPass(); } /// /// Ends the pass that updated the UI. /// public void EndInterfaceUpdate() { interfaceUpdate = EndPass(); } /// /// Ends the pass that queued the draws. /// public void EndDraw() { draw = EndPass(); } /// /// Ends the frame and takes what submitting it cost. The first frame has nothing to measure an interval /// against and costs more than a steady one, so it is dropped. /// public void EndFrame(FrameCost submitCost) { if (!hasInterval) { return; } simulationInWindow += simulation; interfaceInWindow += interfaceUpdate; drawInWindow += draw; submitInWindow += submitCost.Submit; gpuWaitInWindow += submitCost.GpuWait; vsyncWaitInWindow += submitCost.VSyncWait; intervalInWindow += interval; allocatedInWindow += allocatedOnThisThread() - allocatedAtStart; gen0InWindow += GC.CollectionCount(0) - gen0AtStart; gen1InWindow += GC.CollectionCount(1) - gen1AtStart; gen2InWindow += GC.CollectionCount(2) - gen2AtStart; framesInWindow++; // Averaged over a window, since a single frame jitters too much to read. if (intervalInWindow.TotalSeconds >= WindowSeconds) { Publish(); } } } }