Skip to content

Commit

Permalink
make it toggleable
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle committed Dec 8, 2024
1 parent 27d77f7 commit 4a66333
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 78 deletions.
1 change: 1 addition & 0 deletions OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> with waitfor=FALSE
_objectTree.GlobalInitProc?.Spawn(WorldInstance, new());
Expand Down
36 changes: 17 additions & 19 deletions OpenDreamRuntime/DreamThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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;
}

Check warning

Code scanning / InspectCode

Explicit argument passed to parameter with caller info attribute Warning

Explicit argument passed to parameter with caller info attribute

Check warning

Code scanning / InspectCode

Explicit argument passed to parameter with caller info attribute Warning

Explicit argument passed to parameter with caller info attribute
// _current.Resume may mutate our state!!!
status = _current.Resume();
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions OpenDreamRuntime/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public override void Init() {
_configManager.SetCVar(OpenDreamCVars.JsonPath, arg);
break;
}
if(_configManager.GetCVar(OpenDreamCVars.TracyEnable))

Check warning

Code scanning / InspectCode

Incorrect blank lines: Blank lines are missing elsewhere Warning

Blank lines are missing, expected minimum 1 instead of 0
Profiler.ActivateTracy();

_prototypeManager.LoadDirectory(new ResPath("/Resources/Prototypes"));

Expand Down
13 changes: 12 additions & 1 deletion OpenDreamRuntime/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ namespace OpenDreamRuntime;

public static class Profiler
{
//whether these procs are NOPs or not
private static bool _tracyActvated = false;

Check warning

Code scanning / InspectCode

Redundant member initializer Warning

Initializing field by default value is redundant
// Plot names need to be cached for the lifetime of the program

Check warning

Code scanning / InspectCode

Incorrect blank lines: Blank lines are missing elsewhere Warning

Blank lines are missing, expected minimum 1 instead of 0
// seealso Tracy docs section 3.1
private static readonly Dictionary<string, CString> PlotNameCache = new();

public static void ActivateTracy() {
_tracyActvated = true;
}

/// <summary>
/// Begins a new <see cref="ProfilerZone"/> and returns the handle to that zone. Time
/// spent inside a zone is calculated by Tracy and shown in the profiler. A zone is
Expand All @@ -33,7 +39,7 @@ public static class Profiler
/// If this param is not explicitly assigned the value will provided by <see cref="CallerMemberNameAttribute"/>.
/// </param>
/// <returns></returns>

Check failure

Code scanning / InspectCode

Parameter type with caller info must be implicitly convertible to 'int' Error

Parameter type with caller info must be implicitly convertible to 'int'
public static ProfilerZone BeginZone(
public static ProfilerZone? BeginZone(
string? zoneName = null,
bool active = true,
uint color = 0,
Expand All @@ -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);
Expand Down Expand Up @@ -139,6 +148,8 @@ public static void AppInfo(string appInfo)
/// </remarks>
public static void EmitFrameMark()
{
if(!_tracyActvated)
return;
TracyEmitFrameMark(null);
}

Expand Down
119 changes: 61 additions & 58 deletions OpenDreamShared/OpenDreamCVars.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,65 @@
using System;
using Robust.Shared.Configuration;

namespace OpenDreamShared {
[CVarDefs]
public abstract class OpenDreamCVars {
public static readonly CVarDef<string> JsonPath =
CVarDef.Create("opendream.json_path", String.Empty, CVar.SERVERONLY);

public static readonly CVarDef<int> DownloadTimeout =
CVarDef.Create("opendream.download_timeout", 30, CVar.CLIENTONLY);

public static readonly CVarDef<bool> AlwaysShowExceptions =
CVarDef.Create("opendream.always_show_exceptions", false, CVar.SERVERONLY);

public static readonly CVarDef<int> DebugAdapterLaunched =
CVarDef.Create("opendream.debug_adapter_launched", 0, CVar.SERVERONLY);

public static readonly CVarDef<bool> SpoofIEUserAgent =
CVarDef.Create("opendream.spoof_ie_user_agent", true, CVar.CLIENTONLY);

public static readonly CVarDef<string> WorldParams =
CVarDef.Create("opendream.world_params", string.Empty, CVar.SERVERONLY);

public static readonly CVarDef<ushort> TopicPort =
CVarDef.Create<ushort>("opendream.topic_port", 25567, CVar.SERVERONLY);

/*
* INFOLINKS
*/

/// <summary>
/// Link to Discord server to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksDiscord =
CVarDef.Create("infolinks.discord", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to forum to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksForum =
CVarDef.Create("infolinks.forum", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to GitHub page to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksGithub =
CVarDef.Create("infolinks.github", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to website to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksWebsite =
CVarDef.Create("infolinks.website", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to wiki to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksWiki =
CVarDef.Create("infolinks.wiki", "", CVar.SERVER | CVar.REPLICATED);
}
namespace OpenDreamShared;
[CVarDefs]

Check warning

Code scanning / InspectCode

Incorrect blank lines: Blank lines are missing elsewhere Warning

Blank lines are missing, expected minimum 1 instead of 0
public abstract class OpenDreamCVars {
public static readonly CVarDef<string> JsonPath =
CVarDef.Create("opendream.json_path", String.Empty, CVar.SERVERONLY);

Check notice

Code scanning / InspectCode

Replace built-in type reference with a CLR type name or a keyword in static member access expressions Note

Built-in type reference is inconsistent with code style settings

public static readonly CVarDef<int> DownloadTimeout =
CVarDef.Create("opendream.download_timeout", 30, CVar.CLIENTONLY);

public static readonly CVarDef<bool> AlwaysShowExceptions =
CVarDef.Create("opendream.always_show_exceptions", false, CVar.SERVERONLY);

public static readonly CVarDef<int> DebugAdapterLaunched =
CVarDef.Create("opendream.debug_adapter_launched", 0, CVar.SERVERONLY);

public static readonly CVarDef<bool> SpoofIEUserAgent =

Check warning

Code scanning / InspectCode

Inconsistent Naming Warning

Name 'SpoofIEUserAgent' does not match rule 'Static readonly fields (not private)'. Suggested name is 'SpoofIeUserAgent'.
CVarDef.Create("opendream.spoof_ie_user_agent", true, CVar.CLIENTONLY);

public static readonly CVarDef<string> WorldParams =
CVarDef.Create("opendream.world_params", string.Empty, CVar.SERVERONLY);

public static readonly CVarDef<ushort> TopicPort =
CVarDef.Create<ushort>("opendream.topic_port", 25567, CVar.SERVERONLY);

public static readonly CVarDef<bool> TracyEnable =
CVarDef.Create<bool>("opendream.enable_tracy", false, CVar.SERVERONLY);

Check warning

Code scanning / InspectCode

Redundant type arguments of method Warning

Type argument specification is redundant

/*
* INFOLINKS
*/

/// <summary>
/// Link to Discord server to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksDiscord =
CVarDef.Create("infolinks.discord", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to forum to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksForum =
CVarDef.Create("infolinks.forum", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to GitHub page to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksGithub =
CVarDef.Create("infolinks.github", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to website to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksWebsite =
CVarDef.Create("infolinks.website", "", CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Link to wiki to show in the launcher.
/// </summary>
public static readonly CVarDef<string> InfoLinksWiki =
CVarDef.Create("infolinks.wiki", "", CVar.SERVER | CVar.REPLICATED);

}

0 comments on commit 4a66333

Please sign in to comment.