Skip to content

Commit 6e3a159

Browse files
committedOct 3, 2023
Support WSL detection in RuntimeInformation
1 parent 5838745 commit 6e3a159

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed
 

‎src/BenchmarkDotNet/Portability/RuntimeInformation.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,30 @@ public static string GetOsVersion()
185185
return null;
186186
try
187187
{
188-
return LinuxOsReleaseHelper.GetNameByOsRelease(File.ReadAllLines("/etc/os-release"));
188+
string version = LinuxOsReleaseHelper.GetNameByOsRelease(File.ReadAllLines("/etc/os-release"));
189+
bool wsl = IsUnderWsl();
190+
return wsl ? version + " WSL" : version;
189191
}
190192
catch (Exception)
191193
{
192194
return null;
193195
}
194196
}
195197

198+
private static bool IsUnderWsl()
199+
{
200+
if (!IsLinux())
201+
return false;
202+
try
203+
{
204+
return File.Exists("/proc/sys/fs/binfmt_misc/WSLInterop"); // https://superuser.com/a/1749811
205+
}
206+
catch (Exception)
207+
{
208+
return false;
209+
}
210+
}
211+
196212
// TODO: Introduce a common util API for registry calls, use it also in BenchmarkDotNet.Toolchains.CsProj.GetCurrentVersionBasedOnWindowsRegistry
197213
/// <summary>
198214
/// On Windows, this method returns UBR (Update Build Revision) based on Registry.

0 commit comments

Comments
 (0)