Skip to content

Commit

Permalink
RavenDB-23705 - don't count the thread stats when we don't save the s…
Browse files Browse the repository at this point in the history
…ource
  • Loading branch information
grisha-kotler committed Feb 11, 2025
1 parent 0befa57 commit 22add1d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
31 changes: 24 additions & 7 deletions src/Sparrow/Utils/NativeMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,30 @@ public static void Free(byte* ptr, long size, ThreadStats stats)

public static void Free(byte* ptr, long size)
{
Free(ptr, size, ThreadAllocations.Value);
Debug.Assert(ptr != null);
Interlocked.Add(ref _totalAllocatedMemory, -size);
Marshal.FreeHGlobal((IntPtr)ptr);
}

public static byte* AllocateMemory(long size)
{
ThreadStats _;
return AllocateMemory(size, out _);
// Allocating when there isn't enough commit charge available is dangerous, on Linux, the OOM
// will try to kill us. On Windows, we might get into memory allocation failures that are not
// fun, so let's try to avoid it explicitly.
// This is not expected to be called frequently, since we are caching the memory used here

LowMemoryNotification.AssertNotAboutToRunOutOfMemory();

try
{
var ptr = (byte*)Marshal.AllocHGlobal((IntPtr)size).ToPointer();
Interlocked.Add(ref _totalAllocatedMemory, size);
return ptr;
}
catch (OutOfMemoryException e)
{
return ThrowFailedToAllocate(size, ThreadAllocations.Value, e);
}
}

public static byte* AllocateMemory(long size, out ThreadStats thread)
Expand Down Expand Up @@ -170,25 +187,25 @@ public static void DecrementLuceneManagedAllocations(long size)
public static byte* AllocateMemoryForLuceneTermCache(long size)
{
Interlocked.Add(ref _totalLuceneUnmanagedAllocationsForTermCache, size);
return AllocateMemory(size, out _);
return AllocateMemory(size);
}

public static byte* AllocateMemoryForLuceneSorting(long size)
{
Interlocked.Add(ref _totalLuceneUnmanagedAllocationsForSorting, size);
return AllocateMemory(size, out _);
return AllocateMemory(size);
}

public static void FreeMemoryByLuceneTermCache(byte* ptr, long size)
{
Interlocked.Add(ref _totalLuceneUnmanagedAllocationsForTermCache, -size);
Free(ptr, size, ThreadAllocations.Value);
Free(ptr, size);
}

public static void FreeMemoryByLuceneSorting(byte* ptr, long size)
{
Interlocked.Add(ref _totalLuceneUnmanagedAllocationsForSorting, -size);
Free(ptr, size, ThreadAllocations.Value);
Free(ptr, size);
}

private static byte* ThrowFailedToAllocate(long size, ThreadStats thread, OutOfMemoryException e)
Expand Down
22 changes: 10 additions & 12 deletions src/Voron/StorageEnvironmentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,8 @@ public class PureMemoryStorageEnvironmentOptions : StorageEnvironmentOptions
private readonly Dictionary<string, IJournalWriter> _logs =
new Dictionary<string, IJournalWriter>(StringComparer.OrdinalIgnoreCase);

private readonly Dictionary<string, IntPtr> _headers =
new Dictionary<string, IntPtr>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, (IntPtr Pointer, NativeMemory.ThreadStats Stats)> _headers =
new Dictionary<string, (IntPtr, NativeMemory.ThreadStats stats)>(StringComparer.OrdinalIgnoreCase);
private readonly int _instanceId;


Expand Down Expand Up @@ -1010,7 +1010,7 @@ public override int GetNumberOfJournalsForReuse()
return 0;
}

protected override void Disposing()
protected override unsafe void Disposing()
{
if (Disposed)
return;
Expand All @@ -1024,7 +1024,7 @@ protected override void Disposing()

foreach (var headerSpace in _headers)
{
Marshal.FreeHGlobal(headerSpace.Value);
NativeMemory.Free((byte*)headerSpace.Value.Pointer, sizeof(FileHeader), headerSpace.Value.Stats);
}

_headers.Clear();
Expand All @@ -1051,12 +1051,11 @@ public override unsafe bool ReadHeader(string filename, FileHeader* header)
{
if (Disposed)
throw new ObjectDisposedException("PureMemoryStorageEnvironmentOptions");
IntPtr ptr;
if (_headers.TryGetValue(filename, out ptr) == false)
if (_headers.TryGetValue(filename, out var tuple) == false)
{
return false;
}
*header = *((FileHeader*)ptr);
*header = *((FileHeader*)tuple.Pointer);

return header->Hash == HeaderAccessor.CalculateFileHeaderHash(header);
}
Expand All @@ -1066,13 +1065,12 @@ public override unsafe void WriteHeader(string filename, FileHeader* header)
if (Disposed)
throw new ObjectDisposedException("PureMemoryStorageEnvironmentOptions");

IntPtr ptr;
if (_headers.TryGetValue(filename, out ptr) == false)
if (_headers.TryGetValue(filename, out var tuple) == false)
{
ptr = (IntPtr)NativeMemory.AllocateMemory(sizeof(FileHeader));
_headers[filename] = ptr;
var ptr = (IntPtr)NativeMemory.AllocateMemory(sizeof(FileHeader), out NativeMemory.ThreadStats stats);
_headers[filename] = tuple = (ptr, stats);
}
Memory.Copy((byte*)ptr, (byte*)header, sizeof(FileHeader));
Memory.Copy((byte*)tuple.Pointer, (byte*)header, sizeof(FileHeader));
}

private AbstractPager GetTempMemoryMapPager(PureMemoryStorageEnvironmentOptions options, VoronPathSetting path, long? intialSize, Win32NativeFileAttributes win32NativeFileAttributes)
Expand Down

0 comments on commit 22add1d

Please sign in to comment.