Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
70 changes: 65 additions & 5 deletions src/BenchmarkDotNet/Environments/PhysicalMemoryInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Extensions;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Text.RegularExpressions;
using Windows.Win32;
using Windows.Win32.System.SystemInformation;
Expand All @@ -12,24 +16,44 @@ public class PhysicalMemoryInfo
{
public long TotalPhysicalBytes { get; }
public long? AvailablePhysicalBytes { get; }
public long? FrequencyMHz { get; }
public string? MemoryType { get; }

public PhysicalMemoryInfo(long totalPhysicalBytes, long? availablePhysicalBytes = null)
public PhysicalMemoryInfo(long totalPhysicalBytes, long? availablePhysicalBytes = null, long? frequencyMHz = null, string? memoryType = null)
{
TotalPhysicalBytes = totalPhysicalBytes;
AvailablePhysicalBytes = availablePhysicalBytes;
FrequencyMHz = frequencyMHz;
MemoryType = memoryType;
}

public string ToFormattedString()
{
double totalGb = TotalPhysicalBytes / (1024.0 * 1024.0 * 1024.0);
var sb = new StringBuilder();
sb.Append($"{Math.Round(totalGb, 2)} GB");
Comment thread
timcassell marked this conversation as resolved.
Outdated

if (MemoryType.IsNotBlank() || FrequencyMHz.HasValue)
{
sb.Append(" (");
if (MemoryType.IsNotBlank())
sb.Append(MemoryType);
if (FrequencyMHz.HasValue)
{
if (MemoryType.IsNotBlank())
sb.Append('-');
sb.Append($"{FrequencyMHz}MHz");
Comment thread
timcassell marked this conversation as resolved.
Outdated
}
sb.Append(')');
}

if (AvailablePhysicalBytes.HasValue)
{
double availableGb = AvailablePhysicalBytes.Value / (1024.0 * 1024.0 * 1024.0);
return $"{Math.Round(totalGb, 2)} GB Total, {Math.Round(availableGb, 2)} GB Available";
sb.Append($", {Math.Round(availableGb, 2)} GB Available");
Comment thread
timcassell marked this conversation as resolved.
}

return $"{Math.Round(totalGb, 2)} GB";
return sb.ToString();
}
}

Expand Down Expand Up @@ -61,7 +85,10 @@ public static class SystemMemory
{
var memStatus = new MEMORYSTATUSEX { dwLength = (uint)Marshal.SizeOf<MEMORYSTATUSEX>() };
if (PInvoke.GlobalMemoryStatusEx(ref memStatus))
return new PhysicalMemoryInfo((long)memStatus.ullTotalPhys, (long)memStatus.ullAvailPhys);
{
var (freq, type) = GetLinuxOrWindowsMemoryDetails();
return new PhysicalMemoryInfo((long)memStatus.ullTotalPhys, (long)memStatus.ullAvailPhys, freq, type);
}

return null;
}
Expand Down Expand Up @@ -91,11 +118,44 @@ public static class SystemMemory
}

if (total > 0)
return new PhysicalMemoryInfo(total, available);
{
var (freq, type) = GetLinuxOrWindowsMemoryDetails();
return new PhysicalMemoryInfo(total, available, freq, type);
}
}
return null;
}

private static (long? FrequencyMHz, string? MemoryType) GetLinuxOrWindowsMemoryDetails()
Comment thread
timcassell marked this conversation as resolved.
Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this should be 2 separate methods.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did not address. GetLinuxOrWindowsMemoryDetails should be split to GetLinuxMemoryDetails and GetWindowsMemoryDetails.

{
try
{
// Try reading /sys/devices/system/edac/mc/ on Linux
if (Directory.Exists("/sys/devices/system/edac/mc"))
{
foreach (var mcDir in Directory.GetDirectories("/sys/devices/system/edac/mc", "mc*"))
{
foreach (var dimmDir in Directory.GetDirectories(mcDir, "dimm*"))
{
string typePath = Path.Combine(dimmDir, "dimm_mem_type");
if (File.Exists(typePath))
{
string memType = File.ReadAllText(typePath).Trim();
if (!string.IsNullOrEmpty(memType) && memType != "Unspecified")
Comment thread
timcassell marked this conversation as resolved.
Outdated
return (null, memType);
}
}
}
}
}
catch
{
// Ignore fallback failures
}

return (null, null);
}

private static PhysicalMemoryInfo? GetMacMemory()
{
long total = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using BenchmarkDotNet.Environments;
using Xunit;

namespace BenchmarkDotNet.Tests.Environments
{
public class PhysicalMemoryInfoTests
{
[Fact]
public void PhysicalMemoryInfo_ParsesAndConstructsCorrectly()
{
var memInfo = new PhysicalMemoryInfo(16000000000, null, 3200, "DDR4");
Assert.Equal(16000000000L, memInfo.TotalPhysicalBytes);
Assert.Equal(3200L, memInfo.FrequencyMHz);
Assert.Equal("DDR4", memInfo.MemoryType);
}
}
}