Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 9 additions & 4 deletions src/BenchmarkDotNet/Environments/Runtimes/CoreRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,16 @@ internal static bool TryGetVersion(out Version? version)
return true;
}

var systemPrivateCoreLib = FileVersionInfo.GetVersionInfo(typeof(object).Assembly.Location);
// systemPrivateCoreLib.Product*Part properties return 0 so we have to implement some ugly parsing...
if (TryGetVersionFromProductInfo(systemPrivateCoreLib.ProductVersion, systemPrivateCoreLib.ProductName, out version))
string coreclrLocation = typeof(object).Assembly.Location;
// Single-file publish has empty assembly location.
if (!string.IsNullOrEmpty(coreclrLocation))
{
return true;
var systemPrivateCoreLib = FileVersionInfo.GetVersionInfo(coreclrLocation);
// systemPrivateCoreLib.Product*Part properties return 0 so we have to implement some ugly parsing...
if (TryGetVersionFromProductInfo(systemPrivateCoreLib.ProductVersion, systemPrivateCoreLib.ProductName, out version))
{
return true;
}
}

// it's OK to use this method only after checking the previous ones
Expand Down
48 changes: 26 additions & 22 deletions src/BenchmarkDotNet/Portability/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Detectors.Cpu;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Helpers;
using JetBrains.Annotations;
using Microsoft.Win32;
using Perfolizer.Helpers;
using static System.Runtime.InteropServices.RuntimeInformation;
using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;

namespace BenchmarkDotNet.Portability
{
Expand Down Expand Up @@ -50,24 +44,15 @@ internal static class RuntimeInformation

public static readonly bool IsNetNative = FrameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase);

public static readonly bool IsNetCore =
((Environment.Version.Major >= 5) || FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
&& !string.IsNullOrEmpty(typeof(object).Assembly.Location);

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatformGuard("browser")]
public static readonly bool IsWasm = OperatingSystem.IsBrowser();
#else
public static readonly bool IsWasm = IsOSPlatform(OSPlatform.Create("BROWSER"));
#endif

public static readonly bool IsNativeAOT =
Environment.Version.Major >= 5
&& string.IsNullOrEmpty(typeof(object).Assembly.Location) // it's merged to a single .exe and .Location returns null
&& !IsWasm; // Wasm also returns "" for assembly locations

#if NETSTANDARD2_0
public static readonly bool IsAot = IsAotMethod();
public static readonly bool IsAot = IsAotMethod() || IsNetNative;

private static bool IsAotMethod()
{
Expand All @@ -88,6 +73,16 @@ private static bool IsAotMethod()
public static readonly bool IsAot = !System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeCompiled;
#endif

public static bool IsNetCore
=> ((Environment.Version.Major >= 5) || FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
&& !IsAot;

public static bool IsNativeAOT
=> Environment.Version.Major >= 5
&& IsAot
&& !IsWasm && !IsMono; // Wasm and MonoAOTLLVM are also AOT


public static readonly bool IsTieredJitEnabled =
IsNetCore
&& (Environment.Version.Major < 3
Expand Down Expand Up @@ -173,10 +168,21 @@ private static string GetNetCoreVersion()
}
else
{
var coreclrAssemblyInfo = FileVersionInfo.GetVersionInfo(typeof(object).GetTypeInfo().Assembly.Location);
var corefxAssemblyInfo = FileVersionInfo.GetVersionInfo(typeof(Regex).GetTypeInfo().Assembly.Location);
string coreclrLocation = typeof(object).GetTypeInfo().Assembly.Location;
string corefxLocation = typeof(Regex).GetTypeInfo().Assembly.Location;

if (CoreRuntime.TryGetVersion(out var version) && version >= new Version(5, 0))
// Handle cases where assembly location is empty (e.g. single-file publish, AOT, some test runners)
if (string.IsNullOrEmpty(coreclrLocation) || string.IsNullOrEmpty(corefxLocation))
{
return CoreRuntime.TryGetVersion(out var ver) && ver.Major >= 5
? $".NET {ver} (assembly location unavailable)"
: $".NET Core {ver?.ToString() ?? Unknown} (assembly location unavailable)";
}

var coreclrAssemblyInfo = FileVersionInfo.GetVersionInfo(coreclrLocation);
var corefxAssemblyInfo = FileVersionInfo.GetVersionInfo(corefxLocation);

if (CoreRuntime.TryGetVersion(out var version) && version.Major >= 5)
{
// after the merge of dotnet/corefx and dotnet/coreclr into dotnet/runtime the version should always be the same
Debug.Assert(coreclrAssemblyInfo.FileVersion == corefxAssemblyInfo.FileVersion);
Expand All @@ -185,9 +191,7 @@ private static string GetNetCoreVersion()
}
else
{
string runtimeVersion = version != default ? version.ToString() : Unknown;

return $".NET Core {runtimeVersion} (CoreCLR {coreclrAssemblyInfo.FileVersion}, CoreFX {corefxAssemblyInfo.FileVersion})";
return $".NET Core {version?.ToString() ?? Unknown} (CoreCLR {coreclrAssemblyInfo.FileVersion}, CoreFX {corefxAssemblyInfo.FileVersion})";
}
}
}
Expand Down