Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracy #2127

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open

Tracy #2127

Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project>
<Import Project="RobustToolbox/Directory.Packages.props" />
<ItemGroup>
<PackageVersion Include="NUnit.ConsoleRunner" Version="3.15.0" />
<PackageVersion Include="Byond.TopicSender" Version="8.0.1" />
</ItemGroup>
<Import Project="RobustToolbox/Directory.Packages.props" />
<ItemGroup>
<PackageVersion Include="NUnit.ConsoleRunner" Version="3.15.0" />
<PackageVersion Include="Byond.TopicSender" Version="8.0.1" />
<PackageVersion Include="Tracy-CSharp" Version="0.11.1-hotfix" />
</ItemGroup>
</Project>
41 changes: 28 additions & 13 deletions OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ public void StartWorld() {
Initialized = true;
InitializedTick = _gameTiming.CurTick;

// Call global <init> with waitfor=FALSE
_objectTree.GlobalInitProc?.Spawn(WorldInstance, new());
using (Profiler.BeginZone("StartWorld", color:(uint)Color.OrangeRed.ToArgb())) {
// Call global <init> with waitfor=FALSE
_objectTree.GlobalInitProc?.Spawn(WorldInstance, new());

// Call New() on all /area and /turf that exist, each with waitfor=FALSE separately. If <global init> created any /area, call New a SECOND TIME
// new() up /objs and /mobs from compiled-in maps [order: (1,1) then (2,1) then (1,2) then (2,2)]
_dreamMapManager.InitializeAtoms(_compiledJson.Maps);
// Call New() on all /area and /turf that exist, each with waitfor=FALSE separately. If <global init> created any /area, call New a SECOND TIME
// new() up /objs and /mobs from compiled-in maps [order: (1,1) then (2,1) then (1,2) then (2,2)]
_dreamMapManager.InitializeAtoms(_compiledJson.Maps);

// Call world.New()
WorldInstance.SpawnProc("New");
// Call world.New()
WorldInstance.SpawnProc("New");
}
}

public void Shutdown() {
Expand All @@ -102,13 +104,26 @@ public void Shutdown() {
public void Update() {
if (!Initialized)
return;
using (Profiler.BeginZone("Tick", color:(uint)Color.OrangeRed.ToArgb()))
{
using (Profiler.BeginZone("DM Execution", color:(uint)Color.LightPink.ToArgb()))
_procScheduler.Process();

using (Profiler.BeginZone("Map Update", color:(uint)Color.LightPink.ToArgb())){
UpdateStat();
_dreamMapManager.UpdateTiles();
}

using (Profiler.BeginZone("Disk IO", color:(uint)Color.LightPink.ToArgb()))
Fixed Show fixed Hide fixed
DreamObjectSavefile.FlushAllUpdates();

WorldInstance.SetVariableValue("cpu", WorldInstance.GetVariable("tick_usage"));

using (Profiler.BeginZone("Deletion Queue", color:(uint)Color.LightPink.ToArgb()))
ProcessDelQueue();
}

_procScheduler.Process();
UpdateStat();
_dreamMapManager.UpdateTiles();
DreamObjectSavefile.FlushAllUpdates();
WorldInstance.SetVariableValue("cpu", WorldInstance.GetVariable("tick_usage"));
ProcessDelQueue();
Profiler.EmitFrameMark();
Fixed Show fixed Hide fixed
}

public void ProcessDelQueue() {
Expand Down
41 changes: 39 additions & 2 deletions OpenDreamRuntime/DreamThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public DMError(string message)
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 DreamThread Thread { get; set; }

[Access(typeof(ProcScheduler))]
Expand Down Expand Up @@ -210,7 +211,10 @@ public static DreamValue Run(DreamProc proc, DreamObject src, DreamObject? usr,
var context = new DreamThread(proc.ToString());

if (proc is NativeProc nativeProc) {
return nativeProc.Call(context, src, usr, new(arguments));
// ReSharper disable ExplicitCallerInfoArgument
using(Profiler.BeginZone(filePath:"Native Proc", lineNumber:0, memberName:nativeProc.Name))
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
return nativeProc.Call(context, src, usr, new(arguments));
// ReSharper restore ExplicitCallerInfoArgument
}

var state = proc.CreateState(context, src, usr, new DreamProcArguments(arguments));
Expand Down Expand Up @@ -252,6 +256,13 @@ public DreamValue ReentrantResume(ProcState? untilState, out ProcStatus resultSt
while (_current != null) {
ProcStatus status;
try {
if (Profiler.IsActivated() && _current.TracyZoneId is null && _current.Proc != null) { //IsActivated() call just for optimisation reasons
var location =_current.TracyLocationId;
var procpath = (_current.Proc.OwningType.Path.Equals("/") ? "/proc/" : _current.Proc.OwningType.Path+"/") +_current.Proc.Name;
// ReSharper disable ExplicitCallerInfoArgument
_current.TracyZoneId = Profiler.BeginZone(filePath: location.SourceFile, lineNumber: location.Line, memberName: procpath);
// ReSharper restore ExplicitCallerInfoArgument
}
// _current.Resume may mutate our state!!!
status = _current.Resume();
} catch (DMError dmError) {
Expand All @@ -274,8 +285,18 @@ public DreamValue ReentrantResume(ProcState? untilState, out ProcStatus resultSt
switch (status) {
// The entire Thread is stopping
case ProcStatus.Cancelled:
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.TracyZoneId is null)
continue;
s.TracyZoneId.Value.Dispose();
s.TracyZoneId = null;
}
_stack.Clear();
resultStatus = status;
return current.Result;
Expand All @@ -298,6 +319,17 @@ public DreamValue ReentrantResume(ProcState? untilState, out ProcStatus resultSt

// The context is done executing for now
case ProcStatus.Deferred:
if (_current.TracyZoneId is not null) {
_current.TracyZoneId.Value.Dispose();
_current.TracyZoneId = null;
}

foreach (var s in _stack) {
Fixed Show fixed Hide fixed
if (s.TracyZoneId is null)
continue;
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;
return _current.Result;
Expand Down Expand Up @@ -334,6 +366,11 @@ public void PushProcState(ProcState state) {
}

public void PopProcState(bool dispose = true) {
if (_current?.TracyZoneId is not null) {
_current.TracyZoneId.Value.Dispose();
_current.TracyZoneId = null;
}

if (_current?.WaitFor == false) {
_syncCount--;
}
Expand Down
3 changes: 3 additions & 0 deletions OpenDreamRuntime/DreamValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@

private object? _refValue;
private readonly float _floatValue;
//ReSharper disable once NotAccessedField.Local
Fixed Show fixed Hide fixed
private readonly ProfilerMemory? _tracyMemoryId; //only used for strings, since everything else is a value type or handled in DreamObject
Fixed Show fixed Hide fixed

public DreamValue(string value) {
DebugTools.Assert(value != null);
Type = DreamValueType.String;
_tracyMemoryId = Profiler.BeginMemoryZone((ulong) (1+value.Length*sizeof(char)), "string");
_refValue = value;
}

Expand Down
3 changes: 3 additions & 0 deletions OpenDreamRuntime/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public override void Init() {
break;
}

if(_configManager.GetCVar(OpenDreamCVars.TracyEnable))
Fixed Show fixed Hide fixed
Profiler.ActivateTracy();

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

_serverInfoManager.Initialize();
Expand Down
5 changes: 5 additions & 0 deletions OpenDreamRuntime/Objects/DreamObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace OpenDreamRuntime.Objects {
[Virtual]
public class DreamObject {
private readonly ProfilerMemory? _tracyMemoryId;
public DreamObjectDefinition ObjectDefinition;

[Access(typeof(DreamObject))]
Expand Down Expand Up @@ -87,6 +88,8 @@ public DreamObject(DreamObjectDefinition objectDefinition) {
if (this is not DreamObjectAtom && IsSubtypeOf(ObjectTree.Datum)) {
ObjectDefinition.DreamManager.Datums.AddLast(new WeakDreamRef(this));
}

_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamObject>() + ObjectDefinition.Variables.Count * Unsafe.SizeOf<DreamValue>() ), "/datum");
}

public virtual void Initialize(DreamProcArguments args) {
Expand All @@ -106,6 +109,7 @@ protected virtual void HandleDeletion(bool possiblyThreaded) {
Variables = null;

ObjectDefinition = null!;
_tracyMemoryId?.ReleaseMemory();
}

/// <summary>
Expand Down Expand Up @@ -145,6 +149,7 @@ public void Delete(bool possiblyThreaded = false) {
}

public bool IsSubtypeOf(TreeEntry ancestor) {
if(Deleted) return false;
return ObjectDefinition.IsSubtypeOf(ancestor);
}

Expand Down
74 changes: 38 additions & 36 deletions OpenDreamRuntime/Objects/DreamObjectTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,42 +139,44 @@ public IEnumerable<TreeEntry> GetAllDescendants(TreeEntry treeEntry) {
/// (by calling the result of <see cref="DreamObject.InitProc(DreamThread, DreamObject?, DreamProcArguments)"/> or <see cref="DreamObject.InitSpawn(DreamProcArguments)"/>)
/// </remarks>
public DreamObject CreateObject(TreeEntry type) {
if (type == List)
return CreateList();
if (type == Savefile)
return new DreamObjectSavefile(Savefile.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(DatabaseQuery))
return new DreamObjectDatabaseQuery(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Database))
return new DreamObjectDatabase(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Matrix))
return new DreamObjectMatrix(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Sound))
return new DreamObjectSound(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Regex))
return new DreamObjectRegex(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Image))
return new DreamObjectImage(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Icon))
return new DreamObjectIcon(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Filter))
return new DreamObjectFilter(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Mob))
return new DreamObjectMob(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Movable))
return new DreamObjectMovable(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Area))
return new DreamObjectArea(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Atom))
return new DreamObjectAtom(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Client))
throw new Exception("Cannot create objects of type /client");
if (type.ObjectDefinition.IsSubtypeOf(Turf))
throw new Exception("New turfs must be created by the map manager");
if (type.ObjectDefinition.IsSubtypeOf(Exception))
return new DreamObjectException(type.ObjectDefinition);

return new DreamObject(type.ObjectDefinition);
using(Profiler.BeginZone($"new {type}")){
if (type == List)
return CreateList();
if (type == Savefile)
return new DreamObjectSavefile(Savefile.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(DatabaseQuery))
return new DreamObjectDatabaseQuery(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Database))
return new DreamObjectDatabase(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Matrix))
return new DreamObjectMatrix(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Sound))
return new DreamObjectSound(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Regex))
return new DreamObjectRegex(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Image))
return new DreamObjectImage(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Icon))
return new DreamObjectIcon(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Filter))
return new DreamObjectFilter(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Mob))
return new DreamObjectMob(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Movable))
return new DreamObjectMovable(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Area))
return new DreamObjectArea(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Atom))
return new DreamObjectAtom(type.ObjectDefinition);
if (type.ObjectDefinition.IsSubtypeOf(Client))
throw new Exception("Cannot create objects of type /client");
if (type.ObjectDefinition.IsSubtypeOf(Turf))
throw new Exception("New turfs must be created by the map manager");
if (type.ObjectDefinition.IsSubtypeOf(Exception))
return new DreamObjectException(type.ObjectDefinition);

return new DreamObject(type.ObjectDefinition);
}
}

public T CreateObject<T>(TreeEntry type) where T : DreamObject {
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/OpenDreamRuntime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Byond.TopicSender" />
<PackageReference Include="Tracy-CSharp" />
</ItemGroup>

<Import Project="..\RobustToolbox\MSBuild\Robust.Properties.targets" />
Expand Down
2 changes: 2 additions & 0 deletions OpenDreamRuntime/Procs/AsyncNativeProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public sealed class State : ProcState {
private AsyncNativeProc? _proc;
public override DreamProc? Proc => _proc;

public override (string SourceFile, int Line) TracyLocationId => ("Async Native Proc", 0);

private Func<State, Task<DreamValue>> _taskFunc;
private Task? _task;

Expand Down
12 changes: 11 additions & 1 deletion OpenDreamRuntime/Procs/DMProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public DMProc(int id, TreeEntry owningType, ProcDefinitionJson json, string? nam
}

public (string Source, int Line) GetSourceAtOffset(int offset) {
if(SourceInfo.Count == 0)
return ("<No Source Attached>",0);
SourceInfoJson current = SourceInfo[0];
string source = ObjectTree.Strings[current.File!.Value];

Expand Down Expand Up @@ -151,6 +153,8 @@ public sealed class NullProcState : ProcState {

public override DreamProc? Proc => _proc;

public override (string SourceFile, int Line) TracyLocationId => ("<NO-OP>",0);

private DreamProc? _proc;

public override ProcStatus Resume() {
Expand Down Expand Up @@ -354,6 +358,9 @@ public sealed class DMProcState : ProcState {
private DMProc _proc;
public override DMProc Proc => _proc;

public override (string SourceFile, int Line) TracyLocationId => _proc.GetSourceAtOffset(_pc+1);


/// Static initializer for maintainer friendly OpcodeHandlers to performance friendly _opcodeHandlers
static unsafe DMProcState() {
int maxOpcode = (int)_opcodeHandlers.Keys.Max();
Expand Down Expand Up @@ -481,7 +488,10 @@ public void SetReturn(DreamValue value) {
public ProcStatus Call(DreamProc proc, DreamObject? src, DreamProcArguments arguments) {
if (proc is NativeProc p) {
// Skip a whole song and dance.
Push(p.Call(Thread, src, Usr, arguments));
// ReSharper disable ExplicitCallerInfoArgument
using(Profiler.BeginZone(filePath:"Native Proc", lineNumber:0, memberName:p.Name))
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Push(p.Call(Thread, src, Usr, arguments));
// ReSharper restore ExplicitCallerInfoArgument
return ProcStatus.Continue;
}

Expand Down
4 changes: 3 additions & 1 deletion OpenDreamRuntime/Procs/InitDreamObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class InitDreamObjectState : ProcState {
private readonly DreamObjectTree _objectTree;

private enum Stage {
// Need to call the object's (init) proc
// Need to call the object's (init) proc
Init,

// Need to call IDreamMetaObject.OnObjectCreated & New
Expand Down Expand Up @@ -42,6 +42,8 @@ public void Initialize(DreamThread thread, DreamObject dreamObject, DreamObject?

public override DreamProc? Proc => null;

public override (string SourceFile, int Line) TracyLocationId => ($"new {_dreamObject.ObjectDefinition.Type}",0);

public override void AppendStackFrame(StringBuilder builder) {
builder.AppendLine($"new {_dreamObject.ObjectDefinition.Type}");
}
Expand Down
Loading
Loading