Skip to content

Commit

Permalink
Support WSL detection in RuntimeInformation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Oct 3, 2023
1 parent 5838745 commit 6e3a159
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/BenchmarkDotNet/Portability/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,30 @@ public static string GetOsVersion()
return null;
try
{
return LinuxOsReleaseHelper.GetNameByOsRelease(File.ReadAllLines("/etc/os-release"));
string version = LinuxOsReleaseHelper.GetNameByOsRelease(File.ReadAllLines("/etc/os-release"));
bool wsl = IsUnderWsl();
return wsl ? version + " WSL" : version;
}
catch (Exception)
{
return null;
}
}

private static bool IsUnderWsl()
{
if (!IsLinux())
return false;
try
{
return File.Exists("/proc/sys/fs/binfmt_misc/WSLInterop"); // https://superuser.com/a/1749811
}
catch (Exception)
{
return false;
}
}

// TODO: Introduce a common util API for registry calls, use it also in BenchmarkDotNet.Toolchains.CsProj.GetCurrentVersionBasedOnWindowsRegistry
/// <summary>
/// On Windows, this method returns UBR (Update Build Revision) based on Registry.
Expand Down

0 comments on commit 6e3a159

Please sign in to comment.