using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Threading; using HarmonyLib; using Newtonsoft.Json.Linq; namespace XFE.SeAgent.Plugin.Performance { // Diagnostic-only instrumentation: remove all probes when a capture finishes. // Durations are inclusive wall time, not CPU time. Parallel sums may exceed elapsed time. internal sealed class EngineStages : IDisposable { private const string Id = "studio.xfe.seagent.stages"; private readonly Harmony harmony = new Harmony(Id); private static EngineStages active; private readonly ConcurrentDictionary counters = new ConcurrentDictionary(); private readonly List unavailable = new List(); private readonly ConcurrentDictionary threads = new ConcurrentDictionary(); private readonly int gameThread = Thread.CurrentThread.ManagedThreadId; private readonly ActorMetrics actorMetrics = new ActorMetrics(); private readonly ConcurrentQueue slowCalls = new ConcurrentQueue(); private long started = Stopwatch.GetTimestamp(), slowCallCount; private sealed class Counter { public string Name; public long Calls, MainCalls, Ticks, MainTicks, Maximum; } private struct Call { public Counter Counter; public long Start; public bool Main; } private sealed class SlowCall { public string Name; public double StartMs, DurationMs; public bool Main; public int Thread; } public EngineStages() { active = this; ActorBatching.Metrics = actorMetrics; Add("Sandbox.MySandboxGame", "Update", "ProcessInvoke", "ProcessRenderOutput", "UpdateSound"); Add("Sandbox.Game.World.MySession", "Update"); Add("Sandbox.Graphics.GUI.MyGuiSandbox", "Update", "HandleInput", "HandleInputAfterSimulation"); Add("VRage.GameServices.MyGameService", "Update"); // Include our own sampling/transport work so diagnostic overhead cannot // silently be attributed to the game or garbage collection. Add("XFE.SeAgent.Plugin.AgentPlugin", "Update"); Add("XFE.SeAgent.Plugin.Game.GameDebugApi", "PerformanceStatus"); Add("Sandbox.Engine.Physics.MyPhysics", "ExecuteParallelRayCasts", "StepWorlds", "UpdateActiveRigidBodies", "ProcessCollisionFilterRefreshes"); Add("Sandbox.Engine.Physics.MyPhysicsBody", "OnMotionDynamic", "UpdateCluster"); Add("VRage.Game.Components.MyPositionComponent", "UpdateChildren"); Add("Sandbox.Game.Entities.MyParallelEntityUpdateOrchestrator", "PerformParallelUpdate", "ProcessInvokeLater", "UpdateBeforeSimulation", "UpdateBeforeSimulation10", "UpdateBeforeSimulation100", "UpdateAfterSimulation", "UpdateAfterSimulation10", "UpdateAfterSimulation100"); Add("Sandbox.Game.Entities.MyCubeGrid", "Dispatch"); Add("Sandbox.Game.GameSystems.MyGridTerminalSystem", "UpdateHud", "UpdateGridBlocksOwnership"); Add("Sandbox.Game.Entities.Blocks.MyProgrammableBlock", "RunSandboxedProgramAction"); Add("Sandbox.Game.EntityComponents.MyResourceDistributorComponent", "UpdateBeforeSimulation"); Add("Sandbox.Game.GameSystems.MyEntityThrustComponent", "UpdateBeforeSimulation"); Add("Sandbox.Game.GameSystems.MyGridGyroSystem", "UpdateGyros", "UpdateOverriddenGyros"); Add("VRageRender.MyRender11", "FullDraw", "ProcessUpdates", "UpdateGameScene", "PrepareGameScene", "DrawGameScene", "Present"); Add("VRage.Render11.Render.MyRenderScheduler", "Init", "Execute", "Done"); Add("VRage.Render11.Culling.Frustum.MyFrustumCullingWork", "ExecuteCulling"); Add("VRageRender.MyGeometryRendererOld", "ProcessCullProxies", "Prepare", "Render"); Add("VRage.Render11.GeometryStage2.Rendering.MyGeometryRenderer", "Preprocess", "UpdateMatrices", "UpdateLods", "Prepare", "PreprocessTransparentPass", "Render"); Add("VRageRender.MyTransparentRendering", "DoWork"); Add("VRage.Render11.Culling.Occlusion.MyOcclusionTask", "DoWork"); Add("VRage.Render11.Culling.Occlusion.MyFlareOcclusionRenderer", "Render"); Add("VRage.Render11.Culling.Occlusion.MyActorOcclusionRenderer", "Render"); Add("VRage.Render11.GBufferResolve.MyGBufferResolver", "DoWork"); Add("VRage.Render11.Scene.MyScene11", "Update"); Add("VRage.Render.Scene.MyActorUpdater", "Update", "UpdateDelayedCalls"); Add("XFE.SeAgent.Plugin.Performance.ActorBatching", "Run"); Add("VRage.Render.Scene.MyActor", "OnUpdateBeforeDraw", "UpdateProxy", "MoveProxy"); Add("VRage.Render.Scene.MyActor", "UpdateBeforeDraw"); Add("VRageRender.MyMeshes", "Load"); Add("VRageRender.MyMeshMaterials1", "OnResourcesGathering"); Add("VRageRender.MyVoxelMaterials", "OnResourcesGather"); Add("VRage.Render11.GeometryStage.Geometry.MyAlphaTransition", "Update"); Add("VRage.Render11.Scene.Components.MyBigMeshTable", "MoveToGPU"); } public void StartClock() { started = Stopwatch.GetTimestamp(); } private void Add(string typeName, params string[] methods) { var type = AccessTools.TypeByName(typeName); foreach (var name in methods) { try { var method = type == null ? null : AccessTools.Method(type, name); if (method == null) { unavailable.Add(typeName + "." + name); continue; } counters[method] = new Counter { Name = type.Name + "." + name }; harmony.Patch(method, prefix: new HarmonyMethod(typeof(EngineStages), nameof(Begin)), finalizer: new HarmonyMethod(typeof(EngineStages), nameof(End))); } catch (Exception error) { unavailable.Add(typeName + "." + name + ": " + error.Message); } } } private static void Begin(MethodBase __originalMethod, ref Call __state) { var self = active; if (self == null || !self.counters.TryGetValue(__originalMethod, out var counter)) return; int thread = Thread.CurrentThread.ManagedThreadId; self.threads.TryAdd(thread, 0); __state = new Call { Counter = counter, Start = Stopwatch.GetTimestamp(), Main = thread == self.gameThread }; } private static void End(Call __state) { if (__state.Counter == null) return; long ticks = Stopwatch.GetTimestamp() - __state.Start; var c = __state.Counter; Interlocked.Increment(ref c.Calls); Interlocked.Add(ref c.Ticks, ticks); if (__state.Main) { Interlocked.Increment(ref c.MainCalls); Interlocked.Add(ref c.MainTicks, ticks); } long previous; do { previous = Interlocked.Read(ref c.Maximum); if (previous >= ticks) break; } while (Interlocked.CompareExchange(ref c.Maximum, ticks, previous) != previous); var self = active; if (self != null && ticks >= Stopwatch.Frequency / 20 && Interlocked.Increment(ref self.slowCallCount) <= 256) self.slowCalls.Enqueue(new SlowCall { Name = c.Name, StartMs = (__state.Start - self.started) * 1000.0 / Stopwatch.Frequency, DurationMs = ticks * 1000.0 / Stopwatch.Frequency, Main = __state.Main, Thread = Thread.CurrentThread.ManagedThreadId }); } public JObject Report() => new JObject { ["measurement"] = "Inclusive wall-clock time; nested stages overlap and parallel totals cannot be added to main-thread time.", ["observedThreads"] = threads.Count, ["unavailable"] = new JArray(unavailable), ["actorComponents"] = actorMetrics.Report(), ["slowCallThresholdMs"] = 50, ["slowCallCount"] = Interlocked.Read(ref slowCallCount), ["slowCalls"] = JArray.FromObject(slowCalls.OrderBy(c => c.StartMs).ToArray()), ["stages"] = new JArray(counters.Values.OrderByDescending(c => c.MainTicks).Select(c => new JObject { ["name"] = c.Name, ["calls"] = c.Calls, ["mainCalls"] = c.MainCalls, ["totalMs"] = c.Ticks * 1000.0 / Stopwatch.Frequency, ["mainMs"] = c.MainTicks * 1000.0 / Stopwatch.Frequency, ["maxMs"] = c.Maximum * 1000.0 / Stopwatch.Frequency })) }; public void Dispose() { if (ReferenceEquals(ActorBatching.Metrics, actorMetrics)) ActorBatching.Metrics = null; harmony.UnpatchAll(Id); if (ReferenceEquals(active, this)) active = null; } } }