XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFE.SpaceEngineers.AgentBridge

【SpaceEngineer】AI调试插件

公开
关注 0 Fork 0 Star 0
UTF-8
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Threading;
using HarmonyLib;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sandbox;
using Sandbox.Engine.Physics;
using Sandbox.Engine.Utils;
using Sandbox.Game.Entities;
using Sandbox.Game.GameSystems;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.World;
using VRage;
using VRage.Library.Utils;
using VRageRender.ExternalApp;

namespace XFE.SeAgent.Plugin.Performance
{
    /// <summary>Reversible, opt-in engine tuning. No simulation step or ship content is changed.</summary>
    internal sealed class PerformanceTuning : IDisposable
    {
        private const string PatchId = "studio.xfe.seagent.performance";
        private readonly Harmony harmony = new Harmony(PatchId);
        private readonly Action<string> log;
        private readonly string path;
        private readonly int gameThread;
        private readonly CacheCoreAffinity cacheCoreAffinity = new CacheCoreAffinity();
        private readonly GcLatencyTuning gcLatency = new GcLatencyTuning();
        private static PerformanceTuning current;
        private ConditionalWeakTable<MyGridTerminalSystem, AccessStamp> stamps = new ConditionalWeakTable<MyGridTerminalSystem, AccessStamp>();
        private readonly List<MethodBase> cacheMethods = new List<MethodBase>();
        private WaitForTargetFrameRate renderWaiter;
        private MyRenderThread affinityRenderThread;
        private bool originalRenderMaxSpeed;
        private bool uncapped, cacheEnabled, validate, disableVsync, actorBatching, lockFreeLoops, idleRotation, texturePriority, hierarchyUpdates;
        private readonly bool originalLockFreeLoops = ParallelTasks.Parallel.IsMutexSlow;
        private MethodBase presentMethod;
        private MethodBase actorUpdateMethod;
        private MethodBase entityUpdateMethod;
        private bool entityBatching;
        private MethodBase rotationMethod;
        private MethodBase texturePriorityMethod;
        private MethodBase hierarchyMethod;
        private int physicsWorkers;
        private long generation, lookups, hits, validations, mismatches;
        private string patchError;
        private static readonly FieldInfo RenderWaiter = AccessTools.Field(typeof(MyRenderThread), "m_waiter");

        public PerformanceTuning(Action<string> logger)
        {
            log = logger; gameThread = Thread.CurrentThread.ManagedThreadId; current = this;
            ActorBatching.Affinity=cacheCoreAffinity;
            path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "XFE", "SpaceEngineersAgent", "performance.json");
            // This is the engine's supported pool sizing input; never replace a live native pool.
            try
            {
                var platformGetter = AccessTools.PropertyGetter(MyVRage.Platform.System.GetType(), "OptimalHavokThreadCount");
                if (platformGetter == null) throw new MissingMemberException("OptimalHavokThreadCount");
                harmony.Patch(platformGetter, postfix: new HarmonyMethod(typeof(PerformanceTuning), nameof(PhysicsWorkersPostfix)));
            }
            catch { Dispose(); throw; }
            if (File.Exists(path))
            {
                try
                {
                    if (new FileInfo(path).Length > 16384) throw new InvalidDataException("Performance configuration exceeds 16 KiB.");
                    Configure(JObject.Parse(File.ReadAllText(path)), false);
                }
                catch (Exception error) { patchError = error.Message; log("Performance configuration not fully applied: " + error.Message); }
            }
        }

        public JObject Configure(JObject args, bool mayPersist = true)
        {
            bool nextUncapped = Boolean(args, "renderUncapped", uncapped), nextCache = Boolean(args, "terminalAccessCache", cacheEnabled);
            bool nextValidate = Boolean(args, "validateCache", validate);
            bool nextDisableVsync = Boolean(args, "disableVsync", disableVsync);
            bool nextActorBatching = Boolean(args, "actorBatching", actorBatching);
            bool nextEntityBatching = Boolean(args, "entityBatching", entityBatching);
            int nextEntityChunk = Integer(args, "entityBatchSize", EntityBatching.ChunkSize, 1, 128);
            bool nextLockFreeLoops = Boolean(args, "lockFreeParallelLoops", lockFreeLoops);
            bool nextCacheCores = Boolean(args, "preferLargestL3", cacheCoreAffinity.Enabled);
            bool nextMainThreadOnly = Boolean(args, "mainThreadL3", cacheCoreAffinity.MainThreadOnly);
            bool nextRenderAffinity = Boolean(args, "pinRenderWorkers", cacheCoreAffinity.PinRenderWorkers);
            bool nextIdleRotation = Boolean(args, "idleRotation", idleRotation);
            bool nextTexturePriority = Boolean(args, "texturePriority", texturePriority);
            bool nextHierarchy = Boolean(args, "hierarchyUpdates", hierarchyUpdates);
            bool nextLowLatency = Boolean(args, "lowLatencyGc", gcLatency.Enabled);
            int nextChunk = Integer(args, "actorBatchSize", ActorBatching.ChunkSize, 16, 1024);
            int nextActorWorkers = Integer(args, "actorWorkers", ActorBatching.Workers, 0, Environment.ProcessorCount);
            int nextWorkers = physicsWorkers;
            if (args["physicsWorkers"] != null)
            {
                if (args["physicsWorkers"].Type != JTokenType.Integer) throw new ArgumentException("physicsWorkers must be an integer.");
                nextWorkers = (int)args["physicsWorkers"];
                if (nextWorkers != 0 && (nextWorkers < 2 || nextWorkers > Math.Min(64, Environment.ProcessorCount)))
                    throw new ArgumentException("physicsWorkers must be 0 (game default), or 2..logicalProcessors (maximum 64).");
            }
            bool persist = Boolean(args, "persist", false);
            if (nextCache != cacheEnabled) SetCache(nextCache);
            if (nextDisableVsync != disableVsync) SetVsyncOverride(nextDisableVsync);
            if (nextActorBatching != actorBatching) SetActorBatching(nextActorBatching);
            if (nextEntityBatching != entityBatching) SetEntityBatching(nextEntityBatching);
            EntityBatching.ChunkSize = nextEntityChunk;
            if (nextIdleRotation != idleRotation) SetIdleRotation(nextIdleRotation);
            if (nextTexturePriority != texturePriority) SetTexturePriority(nextTexturePriority);
            if (nextHierarchy != hierarchyUpdates) SetHierarchyUpdates(nextHierarchy);
            ActorBatching.ChunkSize = nextChunk; ActorBatching.Workers = nextActorWorkers;
            gcLatency.Set(nextLowLatency);
            cacheCoreAffinity.Set(nextCacheCores,nextMainThreadOnly,nextRenderAffinity);
            lockFreeLoops = nextLockFreeLoops;
            ParallelTasks.Parallel.IsMutexSlow = lockFreeLoops || originalLockFreeLoops;
            uncapped = nextUncapped; physicsWorkers = nextWorkers; validate = nextValidate;
            UpdateRenderLimit();
            if (persist && mayPersist)
            {
                var settings = new JObject { ["renderUncapped"] = uncapped, ["disableVsync"] = disableVsync, ["terminalAccessCache"] = cacheEnabled,
                    ["actorBatching"] = actorBatching, ["lockFreeParallelLoops"] = lockFreeLoops,
                    ["entityBatching"] = entityBatching, ["entityBatchSize"] = EntityBatching.ChunkSize,
                    ["idleRotation"] = idleRotation, ["actorBatchSize"] = ActorBatching.ChunkSize, ["actorWorkers"] = ActorBatching.Workers,
                    ["texturePriority"] = texturePriority,
                    ["hierarchyUpdates"] = hierarchyUpdates,
                    ["lowLatencyGc"] = gcLatency.Enabled,
                    ["preferLargestL3"] = cacheCoreAffinity.Enabled, ["physicsWorkers"] = physicsWorkers };
                settings["mainThreadL3"]=cacheCoreAffinity.MainThreadOnly;
                settings["pinRenderWorkers"]=cacheCoreAffinity.PinRenderWorkers;
                Directory.CreateDirectory(Path.GetDirectoryName(path));
                File.WriteAllText(path + ".tmp", settings.ToString(Formatting.Indented));
                if (File.Exists(path)) File.Replace(path + ".tmp", path, path + ".bak"); else File.Move(path + ".tmp", path);
            }
            log("Performance tuning: " + Status().ToString(Formatting.None));
            return Status();
        }

        private static bool Boolean(JObject args, string name, bool fallback)
        {
            if (args[name] == null) return fallback;
            if (args[name].Type != JTokenType.Boolean) throw new ArgumentException(name + " must be boolean.");
            return (bool)args[name];
        }
        private static int Integer(JObject args, string name, int fallback, int min, int max)
        {
            if (args[name] == null) return fallback;
            if (args[name].Type != JTokenType.Integer || (long)args[name] < min || (long)args[name] > max)
                throw new ArgumentException(name + " must be an integer in " + min + ".." + max + ".");
            return (int)args[name];
        }

        public void UpdateRenderLimit()
        {
            gcLatency.Update(MySession.Static != null && MySandboxGame.IsGameReady && !MySandboxGame.IsPaused);
            var thread = MySandboxGame.Static?.GameRenderComponent?.RenderThread;
            if(!ReferenceEquals(thread,affinityRenderThread)) {
                if(affinityRenderThread!=null) affinityRenderThread.BeforeDraw-=cacheCoreAffinity.MarkRenderThread;
                affinityRenderThread=thread;
                if(thread!=null) thread.BeforeDraw+=cacheCoreAffinity.MarkRenderThread;
            }
            var waiter = thread == null ? null : RenderWaiter?.GetValue(thread) as WaitForTargetFrameRate;
            if (!ReferenceEquals(waiter, renderWaiter))
            {
                if (renderWaiter != null) Volatile.Write(ref renderWaiter.EnableMaxSpeed, originalRenderMaxSpeed);
                renderWaiter = waiter;
                if (waiter != null) originalRenderMaxSpeed = waiter.EnableMaxSpeed;
            }
            if (waiter != null) Volatile.Write(ref waiter.EnableMaxSpeed, uncapped || originalRenderMaxSpeed);
        }

        public JObject Status() => new JObject
        {
            ["renderUncapped"] = uncapped, ["renderLimitApplied"] = renderWaiter != null && renderWaiter.EnableMaxSpeed,
            ["disableVsync"] = disableVsync,
            ["actorBatching"] = actorBatching, ["lockFreeParallelLoops"] = lockFreeLoops,
            ["entityBatching"] = entityBatching, ["entityBatchSize"] = EntityBatching.ChunkSize,
            ["entityBatchesDispatched"] = EntityBatching.Dispatches, ["entityItemsProcessed"] = EntityBatching.ItemsProcessed,
            ["idleRotation"] = idleRotation, ["idleRotationsSkipped"] = IdleRotation.Skipped,
            ["texturePriority"] = texturePriority,
            ["hierarchyUpdates"] = hierarchyUpdates,
            ["lowLatencyGc"] = gcLatency.Enabled, ["lowLatencyGcApplied"] = gcLatency.Applied,
            ["lowLatencyGcReason"] = gcLatency.Reason, ["availablePhysicalBytes"] = gcLatency.AvailableBytes,
            ["actorBatchSize"] = ActorBatching.ChunkSize, ["actorWorkers"] = ActorBatching.Workers,
            ["preferLargestL3"] = cacheCoreAffinity.Enabled, ["processorAffinityHex"] = cacheCoreAffinity.CurrentMask,
            ["mainThreadL3"] = cacheCoreAffinity.MainThreadOnly,
            ["pinRenderWorkers"] = cacheCoreAffinity.PinRenderWorkers,["pinnedRenderThreads"] = cacheCoreAffinity.PinnedRenderThreads,["threadAffinityError"] = cacheCoreAffinity.ThreadError,
            ["selectedL3Bytes"] = cacheCoreAffinity.SelectedCacheBytes,
            ["actorBatchesDispatched"] = Interlocked.Read(ref ActorBatching.Dispatches), ["actorComponentsProcessed"] = Interlocked.Read(ref ActorBatching.ItemsProcessed),
            ["terminalAccessCache"] = cacheEnabled, ["validateCache"] = validate,
            ["physicsWorkersRequested"] = physicsWorkers, ["physicsWorkersApply"] = "next world load; 0 uses game default; native Havok may cap the request",
            ["havokMultithreading"] = MyFakes.ENABLE_HAVOK_MULTITHREADING,
            ["havokParallelScheduling"] = MyFakes.ENABLE_HAVOK_PARALLEL_SCHEDULING,
            ["cacheLookups"] = lookups, ["cacheHits"] = hits, ["cacheValidations"] = validations, ["cacheMismatches"] = mismatches,
            ["patchError"] = patchError
        };

        private static void PhysicsWorkersPostfix(ref int? __result)
        {
            if (current != null && current.physicsWorkers > 0) __result = current.physicsWorkers;
        }

        private void SetVsyncOverride(bool enabled)
        {
            if (presentMethod != null) harmony.Unpatch(presentMethod, HarmonyPatchType.Transpiler, PatchId);
            presentMethod = null; disableVsync = false;
            if (!enabled) return;
            var type = AccessTools.TypeByName("VRageRender.MyRender11");
            var method = type == null ? null : AccessTools.Method(type, "Present");
            if (method == null) throw new MissingMethodException("MyRender11.Present");
            harmony.Patch(method, transpiler: new HarmonyMethod(typeof(PerformanceTuning), nameof(PresentWithoutVsync)));
            presentMethod = method; disableVsync = true;
        }
        private void SetActorBatching(bool enabled)
        {
            if (actorUpdateMethod != null) harmony.Unpatch(actorUpdateMethod, HarmonyPatchType.Transpiler, PatchId);
            actorUpdateMethod = null; actorBatching = false;
            if (!enabled) return;
            var method = AccessTools.Method(typeof(VRage.Render.Scene.MyActorUpdater), "Update");
            harmony.Patch(method, transpiler: new HarmonyMethod(typeof(ActorBatching), nameof(ActorBatching.ReplaceLoop)));
            actorUpdateMethod = method; actorBatching = true;
        }
        private void SetEntityBatching(bool enabled)
        {
            if (entityUpdateMethod != null) harmony.Unpatch(entityUpdateMethod, HarmonyPatchType.Transpiler, PatchId);
            entityUpdateMethod = null; entityBatching = false;
            if (!enabled) return;
            var type = AccessTools.TypeByName("Sandbox.Game.Entities.MyParallelEntityUpdateOrchestrator");
            var method = type == null ? null : AccessTools.DeclaredMethod(type, "PerformParallelUpdate");
            if (method == null) throw new MissingMethodException("PerformParallelUpdate");
            harmony.Patch(method, transpiler: new HarmonyMethod(typeof(EntityBatching), nameof(EntityBatching.ReplaceLoop)));
            entityUpdateMethod = method; entityBatching = true;
        }
        private void SetIdleRotation(bool enabled)
        {
            if (rotationMethod != null) harmony.Unpatch(rotationMethod, HarmonyPatchType.Prefix, PatchId);
            rotationMethod = null; idleRotation = false;
            if (!enabled) return;
            IdleRotation.ValidateEngine();
            var method = AccessTools.Method(typeof(Sandbox.RenderDirect.ActorComponents.MyRotationAnimator), "OnUpdateBeforeDraw");
            harmony.Patch(method, prefix: new HarmonyMethod(typeof(IdleRotation), nameof(IdleRotation.Prefix)));
            rotationMethod = method; idleRotation = true;
        }
        private void SetTexturePriority(bool enabled)
        {
            if (texturePriorityMethod != null) harmony.Unpatch(texturePriorityMethod, HarmonyPatchType.Transpiler, PatchId);
            texturePriorityMethod = null; texturePriority = false;
            if (!enabled) return;
            var type = AccessTools.TypeByName("VRage.Render11.Resources.Textures.MyTextureStreamingManager+CompiledBatch");
            var method = type == null ? null : AccessTools.Method(type, "UpdateScenePriority");
            if (method == null) throw new MissingMethodException("CompiledBatch.UpdateScenePriority");
            harmony.Patch(method, transpiler: new HarmonyMethod(typeof(TexturePriorityUpdates), nameof(TexturePriorityUpdates.ReplaceMaximum)));
            texturePriorityMethod = method; texturePriority = true;
        }
        private void SetHierarchyUpdates(bool enabled)
        {
            if (hierarchyMethod != null) harmony.Unpatch(hierarchyMethod, HarmonyPatchType.Prefix, PatchId);
            hierarchyMethod = null; hierarchyUpdates = false;
            if (!enabled) return;
            HierarchyUpdates.ValidateEngine();
            var method = AccessTools.DeclaredMethod(typeof(VRage.Game.Components.MyPositionComponent), "UpdateChildren");
            harmony.Patch(method, prefix: new HarmonyMethod(typeof(HierarchyUpdates), nameof(HierarchyUpdates.Prefix)));
            hierarchyMethod = method; hierarchyUpdates = true;
        }
        private static IEnumerable<CodeInstruction> PresentWithoutVsync(IEnumerable<CodeInstruction> instructions)
        {
            var result = new List<CodeInstruction>(); int matches = 0;
            foreach (var instruction in instructions)
            {
                result.Add(instruction);
                bool setting = instruction.opcode == OpCodes.Ldfld && instruction.operand is FieldInfo field && field.Name == "VSync" && field.FieldType == typeof(int);
                bool dynamicSetting = instruction.opcode == OpCodes.Call && instruction.operand is MethodInfo method && method.Name == "GetDeviceVSyncMode" && method.ReturnType == typeof(int);
                if (!setting && !dynamicSetting) continue;
                // Change only DXGI's sync interval. Do not change swap-chain size, graphics quality or simulation cadence.
                result.Add(new CodeInstruction(OpCodes.Pop)); result.Add(new CodeInstruction(OpCodes.Ldc_I4_0)); matches++;
            }
            if (matches != 2) throw new InvalidOperationException("Unsupported Present method: expected two verified VSync interval sites.");
            return result;
        }

        private void SetCache(bool enabled)
        {
            foreach (var method in cacheMethods) harmony.Unpatch(method, HarmonyPatchType.All, PatchId);
            cacheMethods.Clear(); cacheEnabled = false;
            stamps = new ConditionalWeakTable<MyGridTerminalSystem, AccessStamp>();
            if (!enabled) return;
            try
            {
                PatchInvalidation(typeof(MyGridTerminalSystem), "Add", "Remove");
                PatchInvalidation(typeof(MyCubeGrid), "NotifyBlockOwnershipChange", "ChangeGridOwnership");
                PatchInvalidation(typeof(MyFactionCollection), "ApplyFactionStateChange", "AddPlayerToFactionInternal", "ClearPlayersFactionMapping",
                    "ChangeFactionRelation", "ChangeRelationWithPlayer", "ChangeReputationWithPlayer", "RemoveRelation", "DeleteFactionRelations", "DeletePlayerRelations");
                var walk = AccessTools.Method(typeof(MyGridTerminalSystem), "UpdateGridBlocksOwnership", new[] { typeof(long) });
                if (walk == null) throw new MissingMethodException("UpdateGridBlocksOwnership");
                harmony.Patch(walk, prefix: new HarmonyMethod(typeof(PerformanceTuning), nameof(AccessPrefix)), postfix: new HarmonyMethod(typeof(PerformanceTuning), nameof(AccessPostfix)));
                cacheMethods.Add(walk); cacheEnabled = true; patchError = null;
            }
            catch (Exception error)
            {
                foreach (var method in cacheMethods) harmony.Unpatch(method, HarmonyPatchType.All, PatchId);
                cacheMethods.Clear(); patchError = error.Message;
                throw new InvalidOperationException("Access cache remains disabled; engine patch verification failed.", error);
            }
        }

        private void PatchInvalidation(Type type, params string[] names)
        {
            foreach (string name in names)
            {
                var method = AccessTools.Method(type, name);
                if (method == null) throw new MissingMethodException(type.FullName, name);
                harmony.Patch(method, prefix: new HarmonyMethod(typeof(PerformanceTuning), nameof(Invalidate)));
                cacheMethods.Add(method);
            }
        }
        private static void Invalidate() { if (current != null) Interlocked.Increment(ref current.generation); }

        private sealed class AccessCall
        {
            public AccessStamp Stamp;
            public long Owner, Generation, Frame;
            public bool Admin, Factions;
        }
        private static bool AccessPrefix(MyGridTerminalSystem __instance, long ownerID, ref AccessCall __state)
        {
            var self = current; var session = MySession.Static;
            // The stock scripts run on the game thread. Do not interfere with another plugin's parallel runner.
            if (self == null || !self.cacheEnabled || session == null || Thread.CurrentThread.ManagedThreadId != self.gameThread) return true;
            self.lookups++;
            var stamp = self.stamps.GetValue(__instance, _ => new AccessStamp());
            bool admin = false;
            foreach (var block in __instance.Blocks) { admin = block.HasAdminUseTerminals(ownerID); break; }
            long gen = Interlocked.Read(ref self.generation), frame = session.GameplayFrameCounter;
            bool factions = MyFakes.SHOW_FACTIONS_GUI;
            if (stamp.Matches(ownerID, gen, frame, admin, factions))
            {
                if (self.validate)
                {
                    self.validations++;
                    foreach (var block in __instance.Blocks)
                    {
                        if (block.IsAccessibleForProgrammableBlock == block.HasPlayerAccessWithNobodyCheck(ownerID, true)) continue;
                        self.mismatches++; self.cacheEnabled = false;
                        self.log("Access cache disabled: validation mismatch on block " + block.EntityId);
                        return true;
                    }
                }
                self.hits++; return false;
            }
            __state = new AccessCall { Stamp = stamp, Owner = ownerID, Generation = gen, Frame = frame, Admin = admin, Factions = factions };
            return true;
        }
        private static void AccessPostfix(AccessCall __state)
        {
            if (__state != null) __state.Stamp.Record(__state.Owner, __state.Generation, __state.Frame, __state.Admin, __state.Factions);
        }
        public void Dispose()
        {
            if(affinityRenderThread!=null) affinityRenderThread.BeforeDraw-=cacheCoreAffinity.MarkRenderThread;
            if(ReferenceEquals(ActorBatching.Affinity,cacheCoreAffinity)) ActorBatching.Affinity=null;
            try { gcLatency.Dispose(); cacheCoreAffinity.Dispose(); }
            finally
            {
                ParallelTasks.Parallel.IsMutexSlow = originalLockFreeLoops;
                if (renderWaiter != null) Volatile.Write(ref renderWaiter.EnableMaxSpeed, originalRenderMaxSpeed);
                harmony.UnpatchAll(PatchId);
                if (ReferenceEquals(current, this)) current = null;
            }
        }
    }
}