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
Open
cloudsealed
wants to merge
7
commits into
dotnet:master
Choose a base branch
from
cloudsealed:feature/issue-3041
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bab81cb
feat: Add RAM Frequency and Type to Environment Summary (#3041)
cloudsealed 207e88f
feat: Add SMBIOS GetSystemFirmwareTable P/Invoke for Windows memory d…
cloudsealed 05eaa81
refactor: Use CsWin32 generated PInvoke GetSystemFirmwareTable
cloudsealed 0b9b9a5
fix: Correct SMBIOS memory type enumeration mapping (DDR3=0x18, DDR4=…
cloudsealed cb20cab
fix: Pass 0 for FirmwareTableID in GetSystemFirmwareTable to parse fu…
cloudsealed ab023a6
fix: Update SMBIOS LPDDR, MRDIMM and LPDDR6 memory type mappings per …
cloudsealed f80c348
fix: Expand SMBIOS memory type mappings per DMTF DSP0134 3.10.0 spec
cloudsealed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| 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"); | ||
| } | ||
|
|
||
| return $"{Math.Round(totalGb, 2)} GB"; | ||
| return sb.ToString(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -56,12 +80,15 @@ public static class SystemMemory | |
| return null; | ||
| } | ||
|
|
||
| [SupportedOSPlatform("windows5.1.2600")] | ||
| [SupportedOSPlatform("windows6.0.6000")] | ||
| private static PhysicalMemoryInfo? GetWindowsMemory() | ||
| { | ||
| 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,144 @@ 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() | ||
|
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. |
||
| { | ||
| try | ||
| { | ||
| if (OsDetector.IsWindows7OrLater()) | ||
| { | ||
| return GetWindowsSmbiosMemoryDetails(); | ||
| } | ||
|
|
||
| // 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") | ||
| return (null, memType); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // Ignore fallback failures | ||
| } | ||
|
|
||
| return (null, null); | ||
| } | ||
|
|
||
| [SupportedOSPlatform("windows6.0.6000")] | ||
| private static unsafe (long? FrequencyMHz, string? MemoryType) GetWindowsSmbiosMemoryDetails() | ||
| { | ||
| try | ||
| { | ||
| const uint rsMb = 0x52534D42; // 'RSMB' | ||
| var provider = (FIRMWARE_TABLE_PROVIDER)rsMb; | ||
| uint bufferSize = PInvoke.GetSystemFirmwareTable(provider, 0, null, 0); | ||
| if (bufferSize == 0) | ||
| return (null, null); | ||
|
|
||
| byte[] buffer = new byte[bufferSize]; | ||
| fixed (byte* pBuffer = buffer) | ||
| { | ||
| if (PInvoke.GetSystemFirmwareTable(provider, 0, pBuffer, bufferSize) == 0) | ||
| return (null, null); | ||
| } | ||
|
|
||
| // SMBIOS table header: Data at offset 8 | ||
| int offset = 8; | ||
| long? maxSpeed = null; | ||
| string? detectedType = null; | ||
|
|
||
| while (offset + 4 <= buffer.Length) | ||
| { | ||
| byte type = buffer[offset]; | ||
| byte length = buffer[offset + 1]; | ||
|
|
||
| if (length < 4 || offset + length > buffer.Length) | ||
| break; | ||
|
|
||
| // Type 17: Memory Device | ||
| if (type == 17 && length >= 0x15) | ||
| { | ||
| // Memory Type at offset 0x12 | ||
| byte rawMemoryType = buffer[offset + 0x12]; | ||
| string? memTypeStr = MapSmbiosMemoryType(rawMemoryType); | ||
| if (memTypeStr != null && detectedType == null) | ||
| { | ||
| detectedType = memTypeStr; | ||
| } | ||
|
|
||
| // Speed in MHz at offset 0x15 (WORD) | ||
| if (length >= 0x17) | ||
| { | ||
| ushort speed = (ushort)(buffer[offset + 0x15] | (buffer[offset + 0x16] << 8)); | ||
| if (speed > 0 && speed < 0xFFFF) | ||
| { | ||
| if (maxSpeed == null || speed > maxSpeed.Value) | ||
| { | ||
| maxSpeed = speed; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Move past formatted area | ||
| offset += length; | ||
|
|
||
| // Skip unformatted string section (double null-terminated) | ||
| while (offset < buffer.Length - 1 && !(buffer[offset] == 0 && buffer[offset + 1] == 0)) | ||
| { | ||
| offset++; | ||
| } | ||
| offset += 2; | ||
| } | ||
|
|
||
| return (maxSpeed, detectedType); | ||
| } | ||
| catch | ||
| { | ||
| return (null, null); | ||
| } | ||
| } | ||
|
|
||
| private static string? MapSmbiosMemoryType(byte type) => type switch | ||
| { | ||
| 0x12 => "DDR", | ||
| 0x13 => "DDR2", | ||
| 0x18 => "DDR3", | ||
| 0x1A => "DDR4", | ||
| 0x1B => "LPDDR", | ||
| 0x1C => "LPDDR2", | ||
| 0x1D => "LPDDR3", | ||
| 0x1E => "LPDDR4", | ||
| 0x20 => "HBM", | ||
| 0x21 => "HBM2", | ||
| 0x22 => "DDR5", | ||
| 0x23 => "LPDDR5", | ||
| 0x24 => "HBM3", | ||
| 0x25 => "MRDIMM", | ||
| 0x26 => "LPDDR6", | ||
| _ => null | ||
| }; | ||
|
|
||
| private static PhysicalMemoryInfo? GetMacMemory() | ||
| { | ||
| long total = 0; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ PowerGetActiveScheme | |
| LocalFree | ||
| GlobalMemoryStatusEx | ||
| GetLogicalProcessorInformationEx | ||
| GetSystemFirmwareTable | ||
17 changes: 17 additions & 0 deletions
17
tests/BenchmarkDotNet.Tests/Environments/PhysicalMemoryInfoTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.