Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/BenchmarkDotNet/Environments/HostEnvironmentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Portability.Cpu;
using BenchmarkDotNet.Portability.Memory;
using BenchmarkDotNet.Properties;
using BenchmarkDotNet.Toolchains.DotNetCli;
using JetBrains.Annotations;
Expand Down Expand Up @@ -41,6 +42,11 @@ public class HostEnvironmentInfo : BenchmarkEnvironmentInfo
/// </summary>
public Lazy<CpuInfo> CpuInfo { get; protected set; }

// <summary>
/// Provides total and free memory, Could be expensive
/// </summary>
public Lazy<MemoryInfo> MemoryInfo { get; protected set; }

public string JitModules { get; protected set; }

/// <summary>
Expand Down Expand Up @@ -78,6 +84,7 @@ protected HostEnvironmentInfo()
BenchmarkDotNetVersion = GetBenchmarkDotNetVersion();
OsVersion = new Lazy<string>(RuntimeInformation.GetOsVersion);
CpuInfo = new Lazy<CpuInfo>(RuntimeInformation.GetCpuInfo);
MemoryInfo = new Lazy<MemoryInfo>(RuntimeInformation.GetMemoryInfo);
ChronometerFrequency = Chronometer.Frequency;
HardwareTimerKind = Chronometer.HardwareTimerKind;
JitModules = RuntimeInformation.GetJitModulesInfo();
Expand All @@ -98,9 +105,13 @@ public override IEnumerable<string> ToFormattedString()
yield return $"{BenchmarkDotNetCaption}=v{BenchmarkDotNetVersion}, OS={OsVersion.Value}, VM={vmName}";

yield return CpuInfoFormatter.Format(CpuInfo.Value);

if (HardwareTimerKind != HardwareTimerKind.Unknown)
yield return $"Frequency={ChronometerFrequency}, Resolution={ChronometerResolution}, Timer={HardwareTimerKind.ToString().ToUpper()}";

if (MemoryInfo.Value != null)
yield return MemoryInfoFormatter.Format(MemoryInfo.Value);

if (RuntimeInformation.IsNetCore && IsDotNetCliInstalled())
yield return $".NET Core SDK={DotNetSdkVersion.Value}";
}
Expand Down
5 changes: 3 additions & 2 deletions src/BenchmarkDotNet/Portability/Cpu/SysctlCpuInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace BenchmarkDotNet.Portability.Cpu
{
/// <summary>
/// CPU information from output of the `sysctl -a` command.
/// CPU 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 SysctlCpuInfoProvider
Expand All @@ -17,7 +18,7 @@ private static CpuInfo Load()
{
if (RuntimeInformation.IsMacOSX())
{
string content = ProcessHelper.RunAndReadOutput("sysctl", "-a");
string content = SysctlInfoProvider.SysctlInfo.Value;
return SysctlCpuInfoParser.ParseOutput(content);
}
return null;
Expand Down
14 changes: 14 additions & 0 deletions src/BenchmarkDotNet/Portability/Memory/MemoryInfo.cs
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 src/BenchmarkDotNet/Portability/Memory/MemoryInfoFormatter.cs
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 src/BenchmarkDotNet/Portability/Memory/ProcMemoryInfoKeyNames.cs
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 src/BenchmarkDotNet/Portability/Memory/ProcMemoryInfoParser.cs
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 src/BenchmarkDotNet/Portability/Memory/ProcMemoryInfoProvider.cs
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 src/BenchmarkDotNet/Portability/Memory/SysctlMemoryInfoParser.cs
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 src/BenchmarkDotNet/Portability/Memory/SysctlMemoryInfoProvider.cs
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;
}
}
}
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 src/BenchmarkDotNet/Portability/Memory/WmicMemoryInfoParser.cs
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 src/BenchmarkDotNet/Portability/Memory/wmicMemoryInfoProvider.cs
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");

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

return WmicMemoryInfoParser.ParseOutput(content);
}
return null;
}
}
}
Loading