Skip to content

Commit 68599bd

Browse files
author
LoneWandererProductions
committed
improve logging
1 parent 71df9c5 commit 68599bd

9 files changed

Lines changed: 75 additions & 18 deletions

File tree

MemoryManager.Core/IMemoryAllocator.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* PROGRAMMER: Peter Geinitz (Wayfarer)
88
*/
99

10+
using System.Diagnostics;
11+
1012
namespace MemoryManager.Core
1113
{
1214
/// <summary>
@@ -52,5 +54,16 @@ public interface IMemoryAllocator
5254
/// <param name="handle">The handle.</param>
5355
/// <returns>An <see cref="AllocationEntry"/> representing the allocation entry.</returns>
5456
AllocationEntry GetEntry(MemoryHandle handle);
57+
58+
/// <summary>
59+
/// Generates a highly detailed string snapshot of the internal clockwork and moving parts.
60+
/// </summary>
61+
/// <returns>A string representing the internal state of the allocator.</returns>
62+
string DebugDump();
63+
64+
/// <summary>
65+
/// Default Interface Method: Automatically pushes the DebugDump straight to the system Trace.
66+
/// </summary>
67+
public void LogDump();
5568
}
5669
}

MemoryManager.Lanes/SlabLane.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,21 @@ public IEnumerable<MemoryHandle> GetHandles()
360360
}
361361
}
362362

363+
/// <summary>
364+
/// Ensures the entry capacity.
365+
/// </summary>
366+
/// <param name="requiredSlotIndex">Index of the required slot.</param>
363367
private void EnsureEntryCapacity(int requiredSlotIndex)
364368
{
365369
var oldSize = _entries!.Length;
366370
var newSize = MemoryLaneUtils.EnsureEntryCapacity(ref _entries, requiredSlotIndex);
367371
if (newSize > oldSize) OnAllocationExtension?.Invoke(nameof(SlabLane), oldSize, newSize);
368372
}
369373

374+
/// <summary>
375+
/// Grows the versions.
376+
/// </summary>
377+
/// <param name="minCapacity">The minimum capacity.</param>
370378
[MethodImpl(MethodImplOptions.NoInlining)]
371379
private unsafe void GrowVersions(int minCapacity)
372380
{

MemoryManager.Tests/ConcurrentMemoryArenaTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void Arena_ParallelAllocation_MaintainsThreadIsolation()
7878
arena.Free(handle);
7979
}
8080

81+
arena.DebugDump(); // Optional: Visualize the arena state after the test
8182
arena.Dispose();
8283
}
8384

@@ -147,6 +148,7 @@ public unsafe void Arena_CrossThreadFree_RecyclesViaRemoteQueue()
147148
threadA.Join();
148149
threadB.Join();
149150

151+
arena.DebugDump(); // Optional: Visualize the arena state after the test
150152
// Cleanup
151153
arena.Dispose();
152154
}
@@ -183,6 +185,8 @@ public void Arena_LargeAllocations_FallbackToGlobalSlowLane()
183185

184186
// Clean verification pass
185187
arena.Free(largeHandle);
188+
189+
arena.LogDump(); // Optional: Visualize the arena state after the test
186190
arena.Dispose();
187191
}
188192
}

MemoryManager.Tests/FastLaneTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void MoveFastToSlowMovesEntryAndReplacesStub()
7373
// 2. Move to SlowLane
7474
arena.MoveFastToSlow(fastHandle);
7575

76-
arena.DebugDump();
76+
arena.LogDump();
7777

7878
// 3. Assertions
7979
// The FastLane MUST still have the handle (it's a stub now!)

MemoryManager.Tests/MemoryArenaInlineTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void MemoryArenaJanitorAndCompactionStressTest()
198198
}
199199

200200
// 5. Check final health
201-
arena.DebugDump();
201+
arena.LogDump();
202202
Assert.AreEqual(0, arena.FastLane.EstimateFragmentation());
203203
}
204204

MemoryManager.Tests/MemoryArenaTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void AllocateWithinFastLaneThresholdAllocatesInFastLane()
4949

5050
var handle = arena.Allocate(size);
5151

52-
arena.DebugDump();
52+
arena.LogDump();
5353

5454
Assert.IsTrue(arena.Resolve(handle) != nint.Zero);
5555
//Assert.IsTrue(arena.FastLane.GetAllocationSize() < _config.FastLaneSize);

MemoryManager.Tests/SlowLaneTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void Setup()
7373
// 2. Move to FastLane (The method returns the NEW handle!)
7474
var fastHandle = arena.MoveSlowToFast(slowHandle);
7575

76-
arena.DebugDump();
76+
arena.LogDump();
7777

7878
// 3. Verify the old handle is dead
7979
Assert.IsFalse(arena.SlowLane.HasHandle(slowHandle), "SlowLane should no longer have the old handle.");

MemoryManager/ConcurrentMemoryArena.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using MemoryManager.Lanes;
1212
using System;
1313
using System.Collections.Concurrent;
14+
using System.Diagnostics;
1415
using System.Threading;
1516

1617
namespace MemoryManager
@@ -260,6 +261,23 @@ private void DrainRemoteFreesForCurrentThread()
260261
}
261262
}
262263

264+
/// <inheritdoc />
265+
public string DebugDump()
266+
{
267+
var localLane = _threadLocalFastLane.Value;
268+
string laneStatus = localLane != null ? localLane.DebugDump() : "Uninitialized Local Track";
269+
270+
return $"=== CONCURRENT ARENA DIAGNOSTICS ===\n" +
271+
$"Calling Thread ID: {System.Threading.Thread.CurrentThread.ManagedThreadId}\n" +
272+
$"Remote Free Backlog: {_remoteFreeQueue.Count} Handles Pending\n" +
273+
$"[HOT TIER] {localLane?.GetType().Name ?? "None"} -> {laneStatus}\n" +
274+
$"[COLD TIER] Global SlowLane -> {_globalSlowLane.DebugDump()}\n" +
275+
$"====================================";
276+
}
277+
278+
/// <inheritdoc />
279+
public void LogDump() => Trace.WriteLine(DebugDump());
280+
263281
/// <summary>
264282
/// Flushes memory structures cleanly across all thread execution tracks.
265283
/// </summary>

MemoryManager/MemoryArena.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,29 +358,43 @@ public void CompactAll()
358358
}
359359
}
360360

361+
/// <inheritdoc />
361362
/// <summary>
362363
/// Debugdump.
363364
/// </summary>
364-
public void DebugDump()
365+
public string DebugDump()
365366
{
366367
lock (_lock)
367368
{
368-
Trace.WriteLine("===== MemoryArena Dump =====");
369-
Trace.WriteLine($"Fast Lane Usage: {FastLane.UsagePercentage():P2}, Free: {FastLane.FreeSpace()} bytes, Entries: {FastLane.EntryCount}, Stubs: {FastLane.StubCount()}");
370-
Trace.WriteLine($"Estimated Fragmentation: {FastLane.EstimateFragmentation()}%");
371-
Trace.WriteLine(FastLane.DebugDump());
372-
Trace.WriteLine(FastLane.DebugVisualMap());
373-
Trace.WriteLine(FastLane.DebugRedirections());
374-
375-
Trace.WriteLine($"Slow Lane Usage: {SlowLane.UsagePercentage():P2}, Free: {SlowLane.FreeSpace()} bytes, Entries: {SlowLane.EntryCount}, Stubs: {SlowLane.StubCount()}");
376-
Trace.WriteLine($"Estimated Fragmentation: {SlowLane.EstimateFragmentation()}%");
377-
Trace.WriteLine(SlowLane.DebugDump());
378-
Trace.WriteLine(SlowLane.DebugVisualMap());
379-
Trace.WriteLine(SlowLane.DebugRedirections());
380-
Trace.WriteLine("============================");
369+
var sb = new System.Text.StringBuilder();
370+
371+
sb.AppendLine("===== MemoryArena Dump =====");
372+
373+
// Hot Path Telemetry
374+
sb.AppendLine($"Fast Lane Usage: {FastLane.UsagePercentage():P2}, Free: {FastLane.FreeSpace()} bytes, Entries: {FastLane.EntryCount}, Stubs: {FastLane.StubCount()}");
375+
sb.AppendLine($"Estimated Fragmentation: {FastLane.EstimateFragmentation()}%");
376+
sb.AppendLine(FastLane.DebugDump());
377+
sb.AppendLine(FastLane.DebugVisualMap());
378+
sb.AppendLine(FastLane.DebugRedirections());
379+
380+
sb.AppendLine(); // Clean spacing break
381+
382+
// Cold Path Telemetry
383+
sb.AppendLine($"Slow Lane Usage: {SlowLane.UsagePercentage():P2}, Free: {SlowLane.FreeSpace()} bytes, Entries: {SlowLane.EntryCount}, Stubs: {SlowLane.StubCount()}");
384+
sb.AppendLine($"Estimated Fragmentation: {SlowLane.EstimateFragmentation()}%");
385+
sb.AppendLine(SlowLane.DebugDump());
386+
sb.AppendLine(SlowLane.DebugVisualMap());
387+
sb.AppendLine(SlowLane.DebugRedirections());
388+
389+
sb.AppendLine("============================");
390+
391+
return sb.ToString();
381392
}
382393
}
383394

395+
/// <inheritdoc />
396+
public void LogDump() => Trace.WriteLine(DebugDump());
397+
384398
// --- UNLOCKED INTERNAL PATHS ---
385399

386400
/// <summary>

0 commit comments

Comments
 (0)