Skip to content

Commit a58872b

Browse files
Make lscpu call language-invariant, fix #2577
1 parent 5e9b35a commit a58872b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/BenchmarkDotNet/Helpers/ProcessHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ internal static class ProcessHelper
1212
/// Run external process and return the console output.
1313
/// In the case of any exception, null will be returned.
1414
/// </summary>
15-
internal static string? RunAndReadOutput(string fileName, string arguments = "", ILogger? logger = null)
15+
internal static string? RunAndReadOutput(string fileName, string arguments = "", ILogger? logger = null,
16+
Dictionary<string, string>? environmentVariables = null)
1617
{
1718
var processStartInfo = new ProcessStartInfo
1819
{
@@ -24,6 +25,9 @@ internal static class ProcessHelper
2425
RedirectStandardOutput = true,
2526
RedirectStandardError = true
2627
};
28+
if (environmentVariables != null)
29+
foreach (var variable in environmentVariables)
30+
processStartInfo.Environment[variable.Key] = variable.Value;
2731
using (var process = new Process { StartInfo = processStartInfo })
2832
using (new ConsoleExitHandler(process, logger ?? NullLogger.Instance))
2933
{

src/BenchmarkDotNet/Portability/Cpu/Linux/LinuxCpuInfoDetector.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using BenchmarkDotNet.Helpers;
1+
using System.Collections.Generic;
2+
using BenchmarkDotNet.Helpers;
23

34
namespace BenchmarkDotNet.Portability.Cpu.Linux;
45

@@ -14,8 +15,16 @@ internal class LinuxCpuInfoDetector : ICpuInfoDetector
1415
{
1516
if (!IsApplicable()) return null;
1617

18+
// lscpu output respects the system locale, so we should force language invariant environment for correct parsing
19+
var languageInvariantEnvironment = new Dictionary<string, string>
20+
{
21+
["LC_ALL"] = "C",
22+
["LANG"] = "C",
23+
["LANGUAGE"] = "C"
24+
};
25+
1726
string cpuInfo = ProcessHelper.RunAndReadOutput("cat", "/proc/cpuinfo") ?? "";
18-
string lscpu = ProcessHelper.RunAndReadOutput("/bin/bash", "-c \"lscpu\"");
27+
string lscpu = ProcessHelper.RunAndReadOutput("/bin/bash", "-c \"lscpu\"", environmentVariables: languageInvariantEnvironment);
1928
return LinuxCpuInfoParser.Parse(cpuInfo, lscpu);
2029
}
2130
}

0 commit comments

Comments
 (0)