Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/BenchmarkDotNet/Environments/PhysicalMemoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,23 +361,10 @@ private static bool IsInstalledMemoryDevice(ReadOnlySpan<byte> device)
if (string.IsNullOrEmpty(sysctlOutput) || !long.TryParse(sysctlOutput!.Trim(), out total) || total == 0)
return null;

// 2. Get Free Memory using vm_stat
// 2. Get Available Memory using vm_stat
string? vmStatOutput = ProcessHelper.RunAndReadOutput("vm_stat");
if (!string.IsNullOrEmpty(vmStatOutput))
{
long pageSize = 4096;
var pageSizeMatch = Regex.Match(vmStatOutput, @"page size of (\d+) bytes");
if (pageSizeMatch.Success && long.TryParse(pageSizeMatch.Groups[1].Value, out long parsedPageSize))
{
pageSize = parsedPageSize;
}

var match = Regex.Match(vmStatOutput, @"Pages free:\s+(\d+)");
if (match.Success && long.TryParse(match.Groups[1].Value, out long pagesFree))
{
available = pagesFree * pageSize;
}
}
available = ParseMacAvailableMemory(vmStatOutput!);

var (speed, type) = GetMacMemoryDetails();
return new PhysicalMemoryInfo(total, available, speed, type);
Expand Down Expand Up @@ -453,5 +440,30 @@ void ReadMemoryDevice(JsonElement device)
}
}
}

/// <summary>
/// Available memory is free + speculative + inactive pages, the pages macOS can reuse for new allocations.
/// vm_stat reports speculative pages separately from "Pages free". Only "Pages free" is required.
/// </summary>
internal static long? ParseMacAvailableMemory(string output)
{
long pageSize = 4096;
var pageSizeMatch = Regex.Match(output, @"page size of (\d+) bytes");
if (pageSizeMatch.Success && long.TryParse(pageSizeMatch.Groups[1].Value, out long parsedPageSize))
pageSize = parsedPageSize;

if (TryGetPages("free") is not long availablePages)
return null;

availablePages += TryGetPages("speculative") ?? 0;
availablePages += TryGetPages("inactive") ?? 0;
return availablePages * pageSize;

long? TryGetPages(string pageName)
{
var match = Regex.Match(output, $@"Pages {pageName}:\s+(\d+)");
return match.Success && long.TryParse(match.Groups[1].Value, out long pages) ? pages : null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,71 @@ private static byte[] WithStrings(byte[] formatted, string[] strings)
bytes.Add(0);
return bytes.ToArray();
}

[Fact]
public void MacAvailableMemoryIncludesReusablePages()
{
const string vmStat = """
Mach Virtual Memory Statistics: (page size of 16384 bytes)
Pages free: 100.
Pages active: 2000.
Pages inactive: 300.
Pages speculative: 50.
Pages throttled: 0.
Pages wired down: 500.
""";

Assert.Equal(7_372_800, SystemMemory.ParseMacAvailableMemory(vmStat));
}

[Fact]
public void MacAvailableMemoryReturnsZeroWhenAllAvailablePagesAreZero()
{
const string vmStat = """
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 0.
Pages inactive: 0.
Pages speculative: 0.
""";

Assert.Equal(0, SystemMemory.ParseMacAvailableMemory(vmStat));
}

[Fact]
public void MacAvailableMemoryReturnsNullWhenFreePagesAreMissing()
{
const string vmStat = """
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages inactive: 300.
Pages speculative: 50.
""";

Assert.Null(SystemMemory.ParseMacAvailableMemory(vmStat));
}

[Fact]
public void MacAvailableMemoryTreatsMissingReusablePagesAsZero()
{
const string vmStat = """
Mach Virtual Memory Statistics: (page size of 16384 bytes)
Pages free: 100.
Pages inactive: 300.
""";

Assert.Equal(400 * 16384, SystemMemory.ParseMacAvailableMemory(vmStat));
}

[Fact]
public void MacAvailableMemoryDefaultsTo4KPagesWhenPageSizeIsMissing()
{
const string vmStat = """
Mach Virtual Memory Statistics:
Pages free: 100.
Pages inactive: 300.
Pages speculative: 50.
""";

Assert.Equal(450 * 4096, SystemMemory.ParseMacAvailableMemory(vmStat));
}
}
}
Loading