From e5bb0ee302e789cb96e7ecfe839cbbcc8e3fd5d7 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 10 Mar 2024 16:46:50 -0400 Subject: Squashed commit of the following: commit 2f7565976472f0f056db60520bf253a776112c10 Merge: 323ff67 6b87785 Author: vnugent Date: Sun Mar 10 16:45:23 2024 -0400 merge master commit 323ff67badfc46ad638d75f059d60d9425ccb2fa Author: vnugent Date: Sun Mar 10 15:50:07 2024 -0400 ci(server): Conainerize and add vncache server packages commit 5d4192880654fd6e00e587814169415b42621327 Author: vnugent Date: Sat Mar 9 19:13:21 2024 -0500 chore: #2 Minor fixes and polish before release commit a4b3504bb891829074d1efde0433eae010862181 Author: vnugent Date: Sat Mar 9 16:30:44 2024 -0500 package updates commit 4d8cfc10382105b0acbd94df93ad3d05ff91db54 Author: vnugent Date: Wed Mar 6 21:30:58 2024 -0500 refactor: #2 Centralize server state, default discovery endpoints & more commit 016a96a80cce025a86c6cf26707738f6a2eb2658 Author: vnugent Date: Thu Feb 29 21:22:38 2024 -0500 feat: add future support for memory diagnostics, and some docs commit 456ead9bc8b0f61357bae93152ad0403c4940101 Author: vnugent Date: Tue Feb 13 14:46:35 2024 -0500 fix: #1 shared cluster index on linux & latested core updates commit a481d63f964a5d5204cac2e95141f37f9a28d573 Author: vnugent Date: Tue Jan 23 15:43:50 2024 -0500 cache extension api tweaks --- .../src/BucketLocalManagerFactory.cs | 53 +++++++++++++++++----- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'plugins/ObjectCacheServer/src/BucketLocalManagerFactory.cs') 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 _managers = new (); private readonly bool _zeroAll; + private readonly bool _enableHeapTracking; + private readonly ILogProvider _statsLogger; /// 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 /// 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 +145,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 +154,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 +163,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 +173,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 -- cgit