From 016a96a80cce025a86c6cf26707738f6a2eb2658 Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 29 Feb 2024 21:22:38 -0500 Subject: feat: add future support for memory diagnostics, and some docs --- .../src/BucketLocalManagerFactory.cs | 52 +++++++++++++++++----- plugins/ObjectCacheServer/src/Cache/CacheStore.cs | 11 ++--- 2 files changed, 45 insertions(+), 18 deletions(-) (limited to 'plugins') diff --git a/plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs b/plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs index 40f4c29..e7fa3e1 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; @@ -34,6 +35,8 @@ using VNLib.Utils; using VNLib.Utils.Memory; using VNLib.Utils.Extensions; using VNLib.Plugins.Extensions.Loading; +using VNLib.Utils.Memory.Diagnostics; +using VNLib.Utils.Logging; /* * How bucket local memory works: @@ -53,6 +56,8 @@ namespace VNLib.Data.Caching.ObjectCache.Server { private readonly LinkedList _managers = new (); private readonly bool _zeroAll; + private readonly bool _enableHeapTracking; + private readonly ILogProvider _statsLogger; /// public ICacheEntryMemoryManager CreateForBucket(uint bucketId) @@ -60,6 +65,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 +86,31 @@ 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"); + } + + 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("Priting memory statistics for cache memory manager: {hm}\n{stats}", GetHashCode(), statsPerHeap); } protected override void Free() @@ -104,7 +136,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server /// public void FreeHandle(object handle) { - _ = handle ?? throw new ArgumentNullException(nameof(handle)); + ArgumentNullException.ThrowIfNull(handle); MemoryHandle _handle = Unsafe.As>(ref handle); //Free the handle @@ -114,7 +146,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server /// public uint GetHandleSize(object handle) { - _ = handle ?? throw new ArgumentNullException(nameof(handle)); + ArgumentNullException.ThrowIfNull(handle); MemoryHandle _handle = Unsafe.As>(ref handle); return (uint)_handle.Length; @@ -123,7 +155,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server /// public Span GetSpan(object handle, uint offset, uint length) { - _ = handle ?? throw new ArgumentNullException(nameof(handle)); + ArgumentNullException.ThrowIfNull(handle); MemoryHandle _handle = Unsafe.As>(ref handle); return _handle.GetOffsetSpan(offset, checked((int)length)); @@ -132,7 +164,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server /// public MemoryHandle PinHandle(object handle, int offset) { - _ = handle ?? throw new ArgumentNullException(nameof(handle)); + ArgumentNullException.ThrowIfNull(handle); MemoryHandle _handle = Unsafe.As>(ref handle); //Pin the handle @@ -142,7 +174,7 @@ namespace VNLib.Data.Caching.ObjectCache.Server /// public void ResizeHandle(object handle, uint newSize) { - _ = handle ?? throw new ArgumentNullException(nameof(handle)); + ArgumentNullException.ThrowIfNull(handle); MemoryHandle _handle = Unsafe.As>(ref handle); //Resize the handle diff --git a/plugins/ObjectCacheServer/src/Cache/CacheStore.cs b/plugins/ObjectCacheServer/src/Cache/CacheStore.cs index 75abe37..86df849 100644 --- a/plugins/ObjectCacheServer/src/Cache/CacheStore.cs +++ b/plugins/ObjectCacheServer/src/Cache/CacheStore.cs @@ -39,19 +39,14 @@ namespace VNLib.Data.Caching.ObjectCache.Server.Cache */ [ConfigurationName("cache")] - internal sealed class CacheStore : ICacheStore, IDisposable + internal sealed class CacheStore(PluginBase plugin, IConfigScope config) : ICacheStore, IDisposable { + /// /// Gets the underlying cache listener /// - public BlobCacheListener Listener { get; } - + public BlobCacheListener Listener { get; } = InitializeCache((ObjectCacheServerEntry)plugin, config); - public CacheStore(PluginBase plugin, IConfigScope config) - { - //Init cache - Listener = InitializeCache((ObjectCacheServerEntry)plugin, config); - } /// ValueTask ICacheStore.AddOrUpdateBlobAsync(string objectId, string? alternateId, ObjectDataGet bodyData, T state, CancellationToken token) -- cgit