From 4a6633334c55629213a14476217516cd524320a2 Mon Sep 17 00:00:00 2001 From: amy Date: Sun, 8 Dec 2024 16:05:06 +0000 Subject: [PATCH] make it toggleable --- OpenDreamRuntime/DreamManager.cs | 1 + OpenDreamRuntime/DreamThread.cs | 36 +++++---- OpenDreamRuntime/EntryPoint.cs | 2 + OpenDreamRuntime/Profile.cs | 13 +++- OpenDreamShared/OpenDreamCVars.cs | 119 +++++++++++++++--------------- 5 files changed, 93 insertions(+), 78 deletions(-) diff --git a/OpenDreamRuntime/DreamManager.cs b/OpenDreamRuntime/DreamManager.cs index 42f15a4006..88f8dfb49a 100644 --- a/OpenDreamRuntime/DreamManager.cs +++ b/OpenDreamRuntime/DreamManager.cs @@ -80,6 +80,7 @@ public void StartWorld() { // It is now OK to call user code, like /New procs. Initialized = true; InitializedTick = _gameTiming.CurTick; + using (Profiler.BeginZone("StartWorld", color:(uint)Color.OrangeRed.ToArgb())) { // Call global with waitfor=FALSE _objectTree.GlobalInitProc?.Spawn(WorldInstance, new()); diff --git a/OpenDreamRuntime/DreamThread.cs b/OpenDreamRuntime/DreamThread.cs index a75e536b53..3bcda92ed9 100644 --- a/OpenDreamRuntime/DreamThread.cs +++ b/OpenDreamRuntime/DreamThread.cs @@ -138,8 +138,7 @@ public abstract class ProcState : IDisposable { private static int _idCounter = 0; public int Id { get; } = ++_idCounter; public abstract (string SourceFile, int Line) TracyLocationId { get; } - public ProfilerZone TracyZoneId { get; set; } - public bool TracyZoned { get; set; } + public ProfilerZone? TracyZoneId { get; set; } public DreamThread Thread { get; set; } [Access(typeof(ProcScheduler))] @@ -254,10 +253,9 @@ public DreamValue ReentrantResume(ProcState? untilState, out ProcStatus resultSt while (_current != null) { ProcStatus status; try { - if (!_current.TracyZoned && _current.Proc != null) { + if (_current.TracyZoneId is null && _current.Proc != null) { var location =_current.TracyLocationId; _current.TracyZoneId = Profiler.BeginZone((_current.Proc.OwningType.Path.Equals("/") ? "/proc/" : _current.Proc.OwningType.Path+"/") +_current.Proc.Name, filePath: location.SourceFile, lineNumber:(uint)location.Line); - _current.TracyZoned = true; } // _current.Resume may mutate our state!!! status = _current.Resume(); @@ -281,17 +279,17 @@ public DreamValue ReentrantResume(ProcState? untilState, out ProcStatus resultSt switch (status) { // The entire Thread is stopping case ProcStatus.Cancelled: - if (_current.TracyZoned) { - _current.TracyZoneId.Dispose(); - _current.TracyZoned = false; + if (_current.TracyZoneId is not null) { + _current.TracyZoneId.Value.Dispose(); + _current.TracyZoneId = null; } var current = _current; _current = null; foreach (var s in _stack) { - if (!s.TracyZoned) + if (s.TracyZoneId is null) continue; - s.TracyZoneId.Dispose(); - s.TracyZoned = false; + s.TracyZoneId.Value.Dispose(); + s.TracyZoneId = null; } _stack.Clear(); resultStatus = status; @@ -315,16 +313,16 @@ public DreamValue ReentrantResume(ProcState? untilState, out ProcStatus resultSt // The context is done executing for now case ProcStatus.Deferred: - if (_current.TracyZoned) { - _current.TracyZoneId.Dispose(); - _current.TracyZoned = false; + if (_current.TracyZoneId is not null) { + _current.TracyZoneId.Value.Dispose(); + _current.TracyZoneId = null; } foreach (var s in _stack) { - if (!s.TracyZoned) + if (s.TracyZoneId is null) continue; - s.TracyZoneId.Dispose(); - s.TracyZoned = false; + s.TracyZoneId.Value.Dispose(); + s.TracyZoneId = null; } // We return the current return value here even though it may not be the final result resultStatus = status; @@ -362,9 +360,9 @@ public void PushProcState(ProcState state) { } public void PopProcState(bool dispose = true) { - if (_current?.TracyZoned == true) { - _current.TracyZoneId.Dispose(); - _current.TracyZoned = false; + if (_current?.TracyZoneId is not null) { + _current.TracyZoneId.Value.Dispose(); + _current.TracyZoneId = null; } if (_current?.WaitFor == false) { diff --git a/OpenDreamRuntime/EntryPoint.cs b/OpenDreamRuntime/EntryPoint.cs index e797e5151d..6f9759a7ca 100644 --- a/OpenDreamRuntime/EntryPoint.cs +++ b/OpenDreamRuntime/EntryPoint.cs @@ -51,6 +51,8 @@ public override void Init() { _configManager.SetCVar(OpenDreamCVars.JsonPath, arg); break; } + if(_configManager.GetCVar(OpenDreamCVars.TracyEnable)) + Profiler.ActivateTracy(); _prototypeManager.LoadDirectory(new ResPath("/Resources/Prototypes")); diff --git a/OpenDreamRuntime/Profile.cs b/OpenDreamRuntime/Profile.cs index 062c844733..dab9c4c018 100644 --- a/OpenDreamRuntime/Profile.cs +++ b/OpenDreamRuntime/Profile.cs @@ -6,10 +6,16 @@ namespace OpenDreamRuntime; public static class Profiler { + //whether these procs are NOPs or not + private static bool _tracyActvated = false; // Plot names need to be cached for the lifetime of the program // seealso Tracy docs section 3.1 private static readonly Dictionary PlotNameCache = new(); + public static void ActivateTracy() { + _tracyActvated = true; + } + /// /// Begins a new and returns the handle to that zone. Time /// spent inside a zone is calculated by Tracy and shown in the profiler. A zone is @@ -33,7 +39,7 @@ public static class Profiler /// If this param is not explicitly assigned the value will provided by . /// /// - public static ProfilerZone BeginZone( + public static ProfilerZone? BeginZone( string? zoneName = null, bool active = true, uint color = 0, @@ -42,6 +48,9 @@ public static ProfilerZone BeginZone( [CallerFilePath] string? filePath = null, [CallerMemberName] string? memberName = null) { + if(!_tracyActvated) + return null; + using var filestr = GetCString(filePath, out var fileln); using var memberstr = GetCString(memberName, out var memberln); using var namestr = GetCString(zoneName, out var nameln); @@ -139,6 +148,8 @@ public static void AppInfo(string appInfo) /// public static void EmitFrameMark() { + if(!_tracyActvated) + return; TracyEmitFrameMark(null); } diff --git a/OpenDreamShared/OpenDreamCVars.cs b/OpenDreamShared/OpenDreamCVars.cs index cc87102218..f862be64e4 100644 --- a/OpenDreamShared/OpenDreamCVars.cs +++ b/OpenDreamShared/OpenDreamCVars.cs @@ -1,62 +1,65 @@ using System; using Robust.Shared.Configuration; -namespace OpenDreamShared { - [CVarDefs] - public abstract class OpenDreamCVars { - public static readonly CVarDef JsonPath = - CVarDef.Create("opendream.json_path", String.Empty, CVar.SERVERONLY); - - public static readonly CVarDef DownloadTimeout = - CVarDef.Create("opendream.download_timeout", 30, CVar.CLIENTONLY); - - public static readonly CVarDef AlwaysShowExceptions = - CVarDef.Create("opendream.always_show_exceptions", false, CVar.SERVERONLY); - - public static readonly CVarDef DebugAdapterLaunched = - CVarDef.Create("opendream.debug_adapter_launched", 0, CVar.SERVERONLY); - - public static readonly CVarDef SpoofIEUserAgent = - CVarDef.Create("opendream.spoof_ie_user_agent", true, CVar.CLIENTONLY); - - public static readonly CVarDef WorldParams = - CVarDef.Create("opendream.world_params", string.Empty, CVar.SERVERONLY); - - public static readonly CVarDef TopicPort = - CVarDef.Create("opendream.topic_port", 25567, CVar.SERVERONLY); - - /* - * INFOLINKS - */ - - /// - /// Link to Discord server to show in the launcher. - /// - public static readonly CVarDef InfoLinksDiscord = - CVarDef.Create("infolinks.discord", "", CVar.SERVER | CVar.REPLICATED); - - /// - /// Link to forum to show in the launcher. - /// - public static readonly CVarDef InfoLinksForum = - CVarDef.Create("infolinks.forum", "", CVar.SERVER | CVar.REPLICATED); - - /// - /// Link to GitHub page to show in the launcher. - /// - public static readonly CVarDef InfoLinksGithub = - CVarDef.Create("infolinks.github", "", CVar.SERVER | CVar.REPLICATED); - - /// - /// Link to website to show in the launcher. - /// - public static readonly CVarDef InfoLinksWebsite = - CVarDef.Create("infolinks.website", "", CVar.SERVER | CVar.REPLICATED); - - /// - /// Link to wiki to show in the launcher. - /// - public static readonly CVarDef InfoLinksWiki = - CVarDef.Create("infolinks.wiki", "", CVar.SERVER | CVar.REPLICATED); - } +namespace OpenDreamShared; +[CVarDefs] +public abstract class OpenDreamCVars { + public static readonly CVarDef JsonPath = + CVarDef.Create("opendream.json_path", String.Empty, CVar.SERVERONLY); + + public static readonly CVarDef DownloadTimeout = + CVarDef.Create("opendream.download_timeout", 30, CVar.CLIENTONLY); + + public static readonly CVarDef AlwaysShowExceptions = + CVarDef.Create("opendream.always_show_exceptions", false, CVar.SERVERONLY); + + public static readonly CVarDef DebugAdapterLaunched = + CVarDef.Create("opendream.debug_adapter_launched", 0, CVar.SERVERONLY); + + public static readonly CVarDef SpoofIEUserAgent = + CVarDef.Create("opendream.spoof_ie_user_agent", true, CVar.CLIENTONLY); + + public static readonly CVarDef WorldParams = + CVarDef.Create("opendream.world_params", string.Empty, CVar.SERVERONLY); + + public static readonly CVarDef TopicPort = + CVarDef.Create("opendream.topic_port", 25567, CVar.SERVERONLY); + + public static readonly CVarDef TracyEnable = + CVarDef.Create("opendream.enable_tracy", false, CVar.SERVERONLY); + + /* + * INFOLINKS + */ + + /// + /// Link to Discord server to show in the launcher. + /// + public static readonly CVarDef InfoLinksDiscord = + CVarDef.Create("infolinks.discord", "", CVar.SERVER | CVar.REPLICATED); + + /// + /// Link to forum to show in the launcher. + /// + public static readonly CVarDef InfoLinksForum = + CVarDef.Create("infolinks.forum", "", CVar.SERVER | CVar.REPLICATED); + + /// + /// Link to GitHub page to show in the launcher. + /// + public static readonly CVarDef InfoLinksGithub = + CVarDef.Create("infolinks.github", "", CVar.SERVER | CVar.REPLICATED); + + /// + /// Link to website to show in the launcher. + /// + public static readonly CVarDef InfoLinksWebsite = + CVarDef.Create("infolinks.website", "", CVar.SERVER | CVar.REPLICATED); + + /// + /// Link to wiki to show in the launcher. + /// + public static readonly CVarDef InfoLinksWiki = + CVarDef.Create("infolinks.wiki", "", CVar.SERVER | CVar.REPLICATED); + }