Skip to content

Commit

Permalink
memory tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle committed Dec 9, 2024
1 parent 696e6a0 commit 4e62bae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 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 ProfilerMemory? _tracyMemoryId;

Check warning

Code scanning / InspectCode

Non-accessed field: Private accessibility Warning

Field '_tracyMemoryId' is assigned but its value is never used
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>() ), "object");
}

public virtual void Initialize(DreamProcArguments args) {
Expand Down Expand Up @@ -145,6 +148,7 @@ public void Delete(bool possiblyThreaded = false) {
}

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

Expand Down
29 changes: 29 additions & 0 deletions OpenDreamRuntime/Profile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Check warning

Code scanning / InspectCode

Redundant using directive Warning

Using directive is not required by the code and can be safely removed
using bottlenoselabs.C2CS.Runtime;
using static Tracy.PInvoke;

Expand All @@ -8,6 +9,7 @@ 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;
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
Expand Down Expand Up @@ -72,6 +74,17 @@ public static bool IsActivated() {
return new ProfilerZone(context);
}

public static ProfilerMemory? BeginMemoryZone(ulong size, string? name)
{
if(!_tracyActivated)
return null;

var namestr = name is null ? GetPlotCString("null") : GetPlotCString(name);
unsafe {
return new ProfilerMemory((void*)_memoryUID++, size, namestr);
}
}

/// <summary>
/// Configure how Tracy will display plotted values.
/// </summary>
Expand Down Expand Up @@ -234,3 +247,19 @@ public void Dispose(){
TracyEmitZoneEnd(Context);
}
}

public sealed unsafe class ProfilerMemory {

private readonly void* _ptr;

Check warning

Code scanning / InspectCode

Incorrect blank lines: Incorrect number of blank lines near braces Warning

Incorrect number of blank lines near braces, expected maximum 0 instead of 1
private CString _name;

Check notice

Code scanning / InspectCode

Field can be made readonly: Private accessibility Note

Field can be made readonly

internal ProfilerMemory(void* pointer, ulong size, CString name){
_ptr = pointer;
_name = name;
TracyEmitMemoryAllocNamed(_ptr, size, 0, _name);
}

~ProfilerMemory(){
TracyEmitMemoryFreeNamed(_ptr, 0, _name);
}
}
3 changes: 3 additions & 0 deletions OpenDreamRuntime/Resources/DreamResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ public byte[]? ResourceData {

private readonly string? _filePath;
private byte[]? _resourceData;
private ProfilerMemory? _tracyMemoryId;

Check warning

Code scanning / InspectCode

Non-accessed field: Private accessibility Warning

Field '_tracyMemoryId' is assigned but its value is never used

public DreamResource(int id, string? filePath, string? resourcePath) {
Id = id;
ResourcePath = resourcePath;
_filePath = filePath;
_tracyMemoryId = Profiler.BeginMemoryZone(ResourceData is null? 0 : (ulong)ResourceData.Length, "resource");
}

public DreamResource(int id, byte[] data) {
Id = id;
_resourceData = data;
_tracyMemoryId = Profiler.BeginMemoryZone(ResourceData is null? 0 : (ulong)ResourceData.Length, "resource");
}

public virtual string? ReadAsString() {
Expand Down

0 comments on commit 4e62bae

Please sign in to comment.