Repository navigation
feat: Add RAM Frequency and Type to Environment Summary (#3041) #3281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
bab81cb
207e88f
05eaa81
0b9b9a5
cb20cab
ab023a6
f80c348
79ded60
1eb5367
e3e7451
b49d075
891ef4f
20a7f2f
d1a11d7
bb66b8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
@@ -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"); | ||
|
|
||
| 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"); | ||
|
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"); | ||
|
timcassell marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return $"{Math.Round(totalGb, 2)} GB"; | ||
| return sb.ToString(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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() | ||
|
timcassell marked this conversation as resolved.
Outdated
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this should be 2 separate methods.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please address
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You did not address. |
||
| { | ||
| 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") | ||
|
timcassell marked this conversation as resolved.
Outdated
|
||
| return (null, memType); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Ignore fallback failures | ||
| } | ||
|
|
||
| return (null, null); | ||
| } | ||
|
|
||
| private static PhysicalMemoryInfo? GetMacMemory() | ||
| { | ||
| long total = 0; | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.