-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added Memory Information - Free and Total memory for different platforms #847
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
ChiragRupani
wants to merge
8
commits into
dotnet:master
Choose a base branch
from
ChiragRupani:feature/memoryinfo
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
8 commits
Select commit
Hold shift + click to select a range
7d7dd02
Added Memory Information - Free and Total memory for different platforms
ChiragRupani 0d41146
Added Memory Information - Free and Total memory for different platforms
ChiragRupani fe18d29
Added common SysctlInfoProvider for CPU and Memory Info on MacOSX
ChiragRupani a2c0b08
Removed Management object search in favour of wmic
ChiragRupani 42dd62f
Included Memory Info in Exporter Approval tests
ChiragRupani 16573bd
Fixed memory info for Mac OS
ChiragRupani 8312bb7
Improved Memory Info shown for Linux and MacOS
ChiragRupani 178e681
Changed text Free to Available
ChiragRupani 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
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
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,14 @@ | ||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| public class MemoryInfo | ||
| { | ||
| public MemoryInfo(long totalMemory, long freePhysicalMemory) | ||
| { | ||
| TotalMemory = totalMemory; | ||
| FreePhysicalMemory = freePhysicalMemory; | ||
| } | ||
|
|
||
| public long TotalMemory { get; set; } | ||
| public long FreePhysicalMemory { get; set; } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/BenchmarkDotNet/Portability/Memory/MemoryInfoFormatter.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,19 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| public class MemoryInfoFormatter | ||
| { | ||
| public static string Format(MemoryInfo memoryInfo) | ||
| { | ||
| string totalMemory = $"Total Memory={Math.Round((memoryInfo.TotalMemory) / (1024.0 * 1024.0), 2)} GB"; | ||
| string freeMemory = $"Available Memory={Math.Round(memoryInfo.FreePhysicalMemory / (1024.0 * 1024.0), 2)} GB"; | ||
|
|
||
| return $"{totalMemory}, {freeMemory}"; | ||
| } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/BenchmarkDotNet/Portability/Memory/ProcMemoryInfoKeyNames.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,10 @@ | ||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| internal static class ProcMemoryInfoKeyNames | ||
| { | ||
| internal const string MemTotal = "MemTotal"; | ||
| internal const string MemAvailable = "MemAvailable"; | ||
| internal const string MemFree = "MemFree"; | ||
| internal const string Cached = "Cached"; | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
src/BenchmarkDotNet/Portability/Memory/ProcMemoryInfoParser.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,55 @@ | ||
| using BenchmarkDotNet.Helpers; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| internal static class ProcMemoryInfoParser | ||
| { | ||
| [CanBeNull] | ||
| internal static MemoryInfo ParseOutput([CanBeNull] string content) | ||
| { | ||
| var memoryInfoContent = SectionsHelper.ParseSections(content, ':'); | ||
| long TotalMemory = 0L; | ||
| long FreePhysicalMemory = 0L; | ||
| bool isValid = memoryInfoContent.Count > 0; | ||
|
|
||
| foreach (var memory in memoryInfoContent) | ||
| { | ||
| if (memory.TryGetValue(ProcMemoryInfoKeyNames.MemTotal, out string totalMemoryValue) && | ||
| long.TryParse(totalMemoryValue.Replace("kB", string.Empty), out long totalMemory)) | ||
| { | ||
| TotalMemory += totalMemory; | ||
| } | ||
| else | ||
| { | ||
| isValid = false; | ||
| } | ||
|
|
||
| if (memory.TryGetValue(ProcMemoryInfoKeyNames.MemAvailable, out string availablePhysicalMemoryValue) && | ||
| long.TryParse(availablePhysicalMemoryValue.Replace("kB", string.Empty), out long availablePhysicalMemory)) | ||
| { | ||
| FreePhysicalMemory += availablePhysicalMemory; | ||
| } | ||
| else | ||
| { | ||
| // Fallback for kernels < 3.14 where proc/meminfo does not provide "MemAvailable" | ||
| // calculated as Free + cached | ||
| if (memory.TryGetValue(ProcMemoryInfoKeyNames.MemFree, out string freePhysicalMemoryValue) && | ||
| long.TryParse(freePhysicalMemoryValue.Replace("kB", string.Empty), out long freePhysicalMemory) && | ||
| memory.TryGetValue(ProcMemoryInfoKeyNames.Cached, out string cachedMemoryValue) && | ||
| long.TryParse(cachedMemoryValue.Replace("kB", string.Empty), out long cachedMemory)) | ||
| { | ||
| FreePhysicalMemory += (freePhysicalMemory + cachedMemory); | ||
| } | ||
| else | ||
| { | ||
| isValid = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var memoryInfo = isValid ? new MemoryInfo(TotalMemory, FreePhysicalMemory) : null; | ||
| return memoryInfo; | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/BenchmarkDotNet/Portability/Memory/ProcMemoryInfoProvider.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,28 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using BenchmarkDotNet.Helpers; | ||
| using BenchmarkDotNet.Horology; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| /// <summary> | ||
| /// Memory information from output of the `cat /proc/meminfo` command. | ||
| /// Linux only. | ||
| /// </summary> | ||
| internal static class ProcMemoryInfoProvider | ||
| { | ||
| internal static readonly Lazy<MemoryInfo> ProcMemoryInfo = new Lazy<MemoryInfo>(Load); | ||
|
|
||
| [CanBeNull] | ||
| private static MemoryInfo Load() | ||
| { | ||
| if (RuntimeInformation.IsLinux()) | ||
| { | ||
| string content = ProcessHelper.RunAndReadOutput("cat", "/proc/meminfo"); | ||
| return ProcMemoryInfoParser.ParseOutput(content); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
src/BenchmarkDotNet/Portability/Memory/SysctlMemoryInfoParser.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,50 @@ | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using BenchmarkDotNet.Helpers; | ||
| using JetBrains.Annotations; | ||
| #if !NETCOREAPP2_1 | ||
| using BenchmarkDotNet.Extensions; | ||
| #endif | ||
|
|
||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| internal static class SysctlMemoryInfoParser | ||
| { | ||
| [CanBeNull] | ||
| [SuppressMessage("ReSharper", "StringLiteralTypo")] | ||
| internal static MemoryInfo ParseOutput([CanBeNull] string systlContent, [CanBeNull] string vmStatContent) | ||
| { | ||
| var sysctl = SectionsHelper.ParseSection(systlContent, ':'); | ||
| var vmstat = SectionsHelper.ParseSection(vmStatContent, ':'); | ||
| long? TotalMemory = null; | ||
| long? FreePhysicalMemory = null; | ||
|
|
||
| if (sysctl.TryGetValue("hw.memsize", out string totalMemoryValue) && long.TryParse(totalMemoryValue, out long totalMemory)) | ||
| { | ||
| // hw.memsize returns in bytes | ||
| TotalMemory = (long)(totalMemory/1024.0); | ||
| } | ||
|
|
||
| // Available Memory = Free + Inactive | ||
| if (vmstat.TryGetValue("Pages free", out string freeMemoryValue) && long.TryParse(freeMemoryValue.Replace(".",string.Empty), out long freeMemory)) | ||
| { | ||
| // Pages are in 4K units | ||
| FreePhysicalMemory = freeMemory * 4; | ||
| } | ||
| if (vmstat.TryGetValue("Pages inactive", out string inactiveMemoryValue) && long.TryParse(inactiveMemoryValue.Replace(".", string.Empty), out long inactiveMemory)) | ||
| { | ||
| // Pages are in 4K units | ||
| FreePhysicalMemory += inactiveMemory * 4; | ||
| } | ||
|
|
||
| if (TotalMemory.HasValue && FreePhysicalMemory.HasValue) | ||
| { | ||
| return new MemoryInfo(TotalMemory.Value, FreePhysicalMemory.Value); | ||
| } | ||
| else | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/BenchmarkDotNet/Portability/Memory/SysctlMemoryInfoProvider.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,29 @@ | ||
| using System; | ||
| using BenchmarkDotNet.Helpers; | ||
| using BenchmarkDotNet.Portability.Cpu; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| /// <summary> | ||
| /// Memory information from output of the `sysctl -a` command. | ||
| /// It is cached by SysctlInfoProvider for reuse in memory and CPU | ||
| /// MacOSX only. | ||
| /// </summary> | ||
| internal static class SysctlMemoryInfoProvider | ||
| { | ||
| internal static readonly Lazy<MemoryInfo> SysctlMemoryInfo = new Lazy<MemoryInfo>(Load); | ||
|
|
||
| [CanBeNull] | ||
| private static MemoryInfo Load() | ||
| { | ||
| if (RuntimeInformation.IsMacOSX()) | ||
| { | ||
| string sysctlContent = SysctlInfoProvider.SysctlInfo.Value; | ||
| string vmsStatContent = ProcessHelper.RunAndReadOutput("vm_stat"); | ||
| return SysctlMemoryInfoParser.ParseOutput(sysctlContent, vmsStatContent); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/BenchmarkDotNet/Portability/Memory/WmicMemoryInfoKeyNames.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,8 @@ | ||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| internal static class WmicMemoryInfoKeyNames | ||
| { | ||
| internal const string FreePhysicalMemory = "FreePhysicalMemory"; | ||
| internal const string TotalVisibleMemorySize = "TotalVisibleMemorySize"; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/BenchmarkDotNet/Portability/Memory/WmicMemoryInfoParser.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,43 @@ | ||
| using BenchmarkDotNet.Helpers; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| internal static class WmicMemoryInfoParser | ||
| { | ||
| [CanBeNull] | ||
| internal static MemoryInfo ParseOutput([CanBeNull] string content) | ||
| { | ||
| var memoryInfoContent = SectionsHelper.ParseSections(content, '='); | ||
| long TotalMemory = 0L; | ||
| long FreePhysicalMemory = 0L; | ||
| bool isValid = memoryInfoContent.Count > 0; | ||
|
|
||
| foreach (var memory in memoryInfoContent) | ||
| { | ||
| if (memory.TryGetValue(WmicMemoryInfoKeyNames.TotalVisibleMemorySize, out string totalMemoryValue) && | ||
| long.TryParse(totalMemoryValue, out long totalMemory)) | ||
| { | ||
| TotalMemory += totalMemory; | ||
| } | ||
| else | ||
| { | ||
| isValid = false; | ||
| } | ||
|
|
||
| if (memory.TryGetValue(WmicMemoryInfoKeyNames.FreePhysicalMemory, out string freePhysicalMemoryValue) && | ||
| long.TryParse(freePhysicalMemoryValue, out long freePhysicalMemory)) | ||
| { | ||
| FreePhysicalMemory += freePhysicalMemory; | ||
| } | ||
| else | ||
| { | ||
| isValid = false; | ||
| } | ||
| } | ||
|
|
||
| var memoryInfo = isValid ? new MemoryInfo(TotalMemory, FreePhysicalMemory) : null; | ||
| return memoryInfo; | ||
| } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/BenchmarkDotNet/Portability/Memory/wmicMemoryInfoProvider.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,30 @@ | ||
| using BenchmarkDotNet.Helpers; | ||
| using JetBrains.Annotations; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace BenchmarkDotNet.Portability.Memory | ||
| { | ||
| /// <summary> | ||
| /// Memory information from output of the `wmic OS get TotalVisibleMemorySize, FreePhysicalMemory /Format:List` command. | ||
| /// Windows only. | ||
| /// </summary> | ||
| internal static class WmicMemoryInfoProvider | ||
| { | ||
| internal static readonly Lazy<MemoryInfo> WmicMemoryInfo = new Lazy<MemoryInfo>(Load); | ||
|
|
||
| [CanBeNull] | ||
| private static MemoryInfo Load() | ||
| { | ||
| if (RuntimeInformation.IsWindows()) | ||
| { | ||
| string content = ProcessHelper.RunAndReadOutput("wmic", "OS get TotalVisibleMemorySize, FreePhysicalMemory /Format:List"); | ||
| return WmicMemoryInfoParser.ParseOutput(content); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.