aboutsummaryrefslogtreecommitdiff
path: root/plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs')
-rw-r--r--plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs53
1 files changed, 42 insertions, 11 deletions
diff --git a/plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs b/plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs
index 40f4c29..6f733ed 100644
--- a/plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs
+++ b/plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Extensions.VNCache
@@ -24,6 +24,7 @@
using System;
+using System.Linq;
using System.Buffers;
using System.Text.Json;
using System.Collections.Generic;
@@ -32,9 +33,10 @@ using System.Runtime.CompilerServices;
using VNLib.Plugins;
using VNLib.Utils;
using VNLib.Utils.Memory;
+using VNLib.Utils.Memory.Diagnostics;
+using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;
using VNLib.Plugins.Extensions.Loading;
-
/*
* How bucket local memory works:
*
@@ -53,6 +55,8 @@ namespace VNLib.Data.Caching.ObjectCache.Server
{
private readonly LinkedList<BucketLocalManager> _managers = new ();
private readonly bool _zeroAll;
+ private readonly bool _enableHeapTracking;
+ private readonly ILogProvider _statsLogger;
///<inheritdoc/>
public ICacheEntryMemoryManager CreateForBucket(uint bucketId)
@@ -60,6 +64,13 @@ namespace VNLib.Data.Caching.ObjectCache.Server
//Init a new heap for the individual bucket
IUnmangedHeap localHeap = MemoryUtil.InitializeNewHeapForProcess();
+ if (_enableHeapTracking)
+ {
+#pragma warning disable CA2000 // Dispose objects before losing scope
+ localHeap = new TrackedHeapWrapper(localHeap, true);
+#pragma warning restore CA2000 // Dispose objects before losing scope
+ }
+
BucketLocalManager manager = new (localHeap, bucketId, _zeroAll);
_managers.AddLast(manager);
@@ -74,11 +85,13 @@ namespace VNLib.Data.Caching.ObjectCache.Server
if (config != null)
{
//Try to get the zero all flag
- if (config.TryGetValue("zero_all", out JsonElement zeroEl))
- {
- _zeroAll = zeroEl.GetBoolean();
- }
+ _zeroAll = config.TryGetValue("zero_all", out JsonElement zeroEl) && zeroEl.GetBoolean();
+
+ //Get the heap tracking flag
+ _enableHeapTracking = config.TryGetValue("diag_mem", out JsonElement trackEl) && trackEl.GetBoolean();
}
+
+ _statsLogger = plugin.Log.CreateScope("Cache MemStats");
}
protected override void Free()
@@ -90,6 +103,24 @@ namespace VNLib.Data.Caching.ObjectCache.Server
}
}
+ public void LogHeapStats()
+ {
+ //If tracking is not enabled, the heap instances stored by the managers will not be tracked, and the cast in the code below will fail
+ if (!_enableHeapTracking)
+ {
+ return;
+ }
+
+ string[] statsPerHeap = _managers.Select(hm =>
+ {
+ HeapStatistics stats = (hm.Heap as TrackedHeapWrapper)!.GetCurrentStats();
+ return $"\tBucket {hm.BucketId}: Current {stats.AllocatedBytes / 1024}kB, Blocks {stats.AllocatedBlocks}, Max size {stats.MaxHeapSize / 1024}kB";
+
+ }).ToArray();
+
+ _statsLogger.Debug("Memory statistics for cache memory manager: {hm}\n{stats}", GetHashCode(), statsPerHeap);
+ }
+
/*
* Buckets are mutually exclusive, so we can use a single heap for each bucket
* to get a little more performance on memory operations
@@ -104,7 +135,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server
///<inheritdoc/>
public void FreeHandle(object handle)
{
- _ = handle ?? throw new ArgumentNullException(nameof(handle));
+ ArgumentNullException.ThrowIfNull(handle);
MemoryHandle<byte> _handle = Unsafe.As<object, MemoryHandle<byte>>(ref handle);
//Free the handle
@@ -114,7 +145,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server
///<inheritdoc/>
public uint GetHandleSize(object handle)
{
- _ = handle ?? throw new ArgumentNullException(nameof(handle));
+ ArgumentNullException.ThrowIfNull(handle);
MemoryHandle<byte> _handle = Unsafe.As<object, MemoryHandle<byte>>(ref handle);
return (uint)_handle.Length;
@@ -123,7 +154,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server
///<inheritdoc/>
public Span<byte> GetSpan(object handle, uint offset, uint length)
{
- _ = handle ?? throw new ArgumentNullException(nameof(handle));
+ ArgumentNullException.ThrowIfNull(handle);
MemoryHandle<byte> _handle = Unsafe.As<object, MemoryHandle<byte>>(ref handle);
return _handle.GetOffsetSpan(offset, checked((int)length));
@@ -132,7 +163,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server
///<inheritdoc/>
public MemoryHandle PinHandle(object handle, int offset)
{
- _ = handle ?? throw new ArgumentNullException(nameof(handle));
+ ArgumentNullException.ThrowIfNull(handle);
MemoryHandle<byte> _handle = Unsafe.As<object, MemoryHandle<byte>>(ref handle);
//Pin the handle
@@ -142,7 +173,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server
///<inheritdoc/>
public void ResizeHandle(object handle, uint newSize)
{
- _ = handle ?? throw new ArgumentNullException(nameof(handle));
+ ArgumentNullException.ThrowIfNull(handle);
MemoryHandle<byte> _handle = Unsafe.As<object, MemoryHandle<byte>>(ref handle);
//Resize the handle