Skip to content

Commit

Permalink
fuck it, tools build means we can trace all list ops right?
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle committed Dec 11, 2024
1 parent 7462ab9 commit 4986175
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
5 changes: 4 additions & 1 deletion OpenDreamRuntime/DreamValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ public static DreamValue False {
private object? _refValue;
private readonly float _floatValue;

#if TOOLS
//ReSharper disable once NotAccessedField.Local
private readonly ProfilerMemory? _tracyMemoryId; //only used for strings, since everything else is a value type or handled in DreamObject

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

Expand Down
4 changes: 0 additions & 4 deletions OpenDreamRuntime/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using OpenDreamRuntime.Objects.Types;
using OpenDreamRuntime.Procs.DebugAdapter;
using OpenDreamShared;
using Robust.Server.ServerStatus;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
Expand Down Expand Up @@ -52,9 +51,6 @@ public override void Init() {
break;
}

if(_configManager.GetCVar(OpenDreamCVars.TracyEnable))
Profiler.ActivateTracy();

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

_serverInfoManager.Initialize();
Expand Down
26 changes: 25 additions & 1 deletion OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public DreamList(DreamObjectDefinition listDef, List<DreamValue> values, Diction
_values = values;
_associativeValues = associativeValues;
#if TOOLS
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + _associativeValues?.Count * Unsafe.SizeOf<DreamValue>() ?? 0), "/list");
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + (_associativeValues?.Count ?? 0) * Unsafe.SizeOf<DreamValue>()), "/list");
#endif
}

Expand Down Expand Up @@ -130,6 +130,10 @@ public virtual void SetValue(DreamValue key, DreamValue value, bool allowGrowth
_associativeValues ??= new Dictionary<DreamValue, DreamValue>(1);
_associativeValues[key] = value;
}
#if TOOLS
_tracyMemoryId?.ReleaseMemory();
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + (_associativeValues?.Count ?? 0) * Unsafe.SizeOf<DreamValue>()), "/list");
#endif
}

public virtual void RemoveValue(DreamValue value) {
Expand All @@ -139,10 +143,18 @@ public virtual void RemoveValue(DreamValue value) {
_associativeValues?.Remove(value);
_values.RemoveAt(valueIndex);
}
#if TOOLS
_tracyMemoryId?.ReleaseMemory();
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + (_associativeValues?.Count ?? 0) * Unsafe.SizeOf<DreamValue>()), "/list");
#endif
}

public virtual void AddValue(DreamValue value) {
_values.Add(value);
#if TOOLS
_tracyMemoryId?.ReleaseMemory();
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + (_associativeValues?.Count ?? 0) * Unsafe.SizeOf<DreamValue>()), "/list");
#endif
}

//Does not include associations
Expand Down Expand Up @@ -179,10 +191,18 @@ public virtual void Cut(int start = 1, int end = 0) {

if (end > start)
_values.RemoveRange(start - 1, end - start);
#if TOOLS
_tracyMemoryId?.ReleaseMemory();
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + (_associativeValues?.Count ?? 0) * Unsafe.SizeOf<DreamValue>()), "/list");
#endif
}

public void Insert(int index, DreamValue value) {
_values.Insert(index - 1, value);
#if TOOLS
_tracyMemoryId?.ReleaseMemory();
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + (_associativeValues?.Count ?? 0) * Unsafe.SizeOf<DreamValue>()), "/list");
#endif
}

public void Swap(int index1, int index2) {
Expand All @@ -202,6 +222,10 @@ public void Resize(int size) {
} else {
Cut(size + 1);
}
#if TOOLS
_tracyMemoryId?.ReleaseMemory();
_tracyMemoryId = Profiler.BeginMemoryZone((ulong)(Unsafe.SizeOf<DreamList>() + _values.Count * Unsafe.SizeOf<DreamValue>() + (_associativeValues?.Count ?? 0) * Unsafe.SizeOf<DreamValue>()), "/list");
#endif
}

public virtual int GetLength() {
Expand Down
34 changes: 4 additions & 30 deletions OpenDreamRuntime/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,13 @@
namespace OpenDreamRuntime;

public static class Profiler{
//whether these procs are NOPs or not. Defaults to false. Use ActivateTracy() to set true
private static bool _tracyActivated;

//internal tracking for unique IDs for memory zones, because we can't use actual object pointers sadly as they are unstable outside of `unsafe`
private static UInt64 _memoryUID = 0;

Check notice

Code scanning / InspectCode

Replace built-in type reference with a CLR type name or a keyword Note

Built-in type reference is inconsistent with code style settings

Check warning

Code scanning / InspectCode

Inconsistent Naming Warning

Name '_memoryUID' does not match rule 'Static fields (private)'. Suggested name is '_memoryUid'.

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
// seealso Tracy docs section 3.1
private static readonly Dictionary<string, CString> PlotNameCache = new();

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

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsActivated() {
return _tracyActivated;
}

/// <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 Down Expand Up @@ -81,13 +68,14 @@ public static bool IsActivated() {

public static ProfilerMemory? BeginMemoryZone(ulong size, string? name)

Check warning

Code scanning / InspectCode

Return type of a function can be made non-nullable Warning

Return type of 'BeginMemoryZone' can be made non-nullable
{
if(!_tracyActivated)
return null;

#if !TOOLS
return null

Check failure on line 72 in OpenDreamRuntime/Profile.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 72 in OpenDreamRuntime/Profile.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 72 in OpenDreamRuntime/Profile.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

; expected

Check failure on line 72 in OpenDreamRuntime/Profile.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

; expected
#else
var namestr = name is null ? GetPlotCString("null") : GetPlotCString(name);
unsafe {
return new ProfilerMemory((void*)(Interlocked.Add(ref _memoryUID, size)-size), size, namestr);
}
#endif
}

/// <summary>
Expand All @@ -109,8 +97,6 @@ public static bool IsActivated() {
/// An <c>RRGGBB</c> color code that Tracy will use to color the plot in the profiler.
/// </param>
public static void PlotConfig(string name, PlotType type = PlotType.Number, bool step = false, bool fill = true, uint color = 0){
if(!_tracyActivated)
return;
var namestr = GetPlotCString(name);
TracyEmitPlotConfig(namestr, (int)type, step ? 1 : 0, fill ? 1 : 0, color);
}
Expand All @@ -119,8 +105,6 @@ public static void PlotConfig(string name, PlotType type = PlotType.Number, bool
/// Add a <see langword="double"/> value to a plot.
/// </summary>
public static void Plot(string name, double val){
if(!_tracyActivated)
return;
var namestr = GetPlotCString(name);
TracyEmitPlot(namestr, val);
}
Expand All @@ -129,8 +113,6 @@ public static void Plot(string name, double val){
/// Add a <see langword="float"/> value to a plot.
/// </summary>
public static void Plot(string name, int val){
if(!_tracyActivated)
return;
var namestr = GetPlotCString(name);
TracyEmitPlotInt(namestr, val);
}
Expand All @@ -139,8 +121,6 @@ public static void Plot(string name, int val){
/// Add a <see langword="float"/> value to a plot.
/// </summary>
public static void Plot(string name, float val){
if(!_tracyActivated)
return;
var namestr = GetPlotCString(name);
TracyEmitPlotFloat(namestr, val);
}
Expand All @@ -162,8 +142,6 @@ private static CString GetPlotCString(string name){
/// Viewable in the Info tab in the profiler.
/// </remarks>
public static void AppInfo(string appInfo){
if(!_tracyActivated)
return;
using var infostr = GetCString(appInfo, out var infoln);
TracyEmitMessageAppinfo(infostr, infoln);
}
Expand All @@ -175,8 +153,6 @@ public static void AppInfo(string appInfo){
/// Tracy Cpp API and docs refer to this as the <c>FrameMark</c> macro.
/// </remarks>
public static void EmitFrameMark(){
if(!_tracyActivated)
return;
TracyEmitFrameMark(null);
}

Expand All @@ -185,8 +161,6 @@ public static void EmitFrameMark(){
/// </summary>
/// <returns></returns>
public static bool IsConnected(){
if(!_tracyActivated)
return false;
return TracyConnected() != 0;
}

Expand Down
3 changes: 0 additions & 3 deletions OpenDreamShared/OpenDreamCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public abstract class OpenDreamCVars {
public static readonly CVarDef<ushort> TopicPort =
CVarDef.Create<ushort>("opendream.topic_port", 25567, CVar.SERVERONLY);

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

/*
* INFOLINKS
*/
Expand Down

0 comments on commit 4986175

Please sign in to comment.