Skip to content

Commit

Permalink
Make lscpu call language-invariant, fix #2577
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Aug 26, 2024
1 parent 5e9b35a commit a58872b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/BenchmarkDotNet/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal static class ProcessHelper
/// Run external process and return the console output.
/// In the case of any exception, null will be returned.
/// </summary>
internal static string? RunAndReadOutput(string fileName, string arguments = "", ILogger? logger = null)
internal static string? RunAndReadOutput(string fileName, string arguments = "", ILogger? logger = null,
Dictionary<string, string>? environmentVariables = null)
{
var processStartInfo = new ProcessStartInfo
{
Expand All @@ -24,6 +25,9 @@ internal static class ProcessHelper
RedirectStandardOutput = true,
RedirectStandardError = true
};
if (environmentVariables != null)
foreach (var variable in environmentVariables)
processStartInfo.Environment[variable.Key] = variable.Value;
using (var process = new Process { StartInfo = processStartInfo })
using (new ConsoleExitHandler(process, logger ?? NullLogger.Instance))
{
Expand Down
13 changes: 11 additions & 2 deletions src/BenchmarkDotNet/Portability/Cpu/Linux/LinuxCpuInfoDetector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BenchmarkDotNet.Helpers;
using System.Collections.Generic;
using BenchmarkDotNet.Helpers;

namespace BenchmarkDotNet.Portability.Cpu.Linux;

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

// lscpu output respects the system locale, so we should force language invariant environment for correct parsing
var languageInvariantEnvironment = new Dictionary<string, string>
{
["LC_ALL"] = "C",
["LANG"] = "C",
["LANGUAGE"] = "C"
};

string cpuInfo = ProcessHelper.RunAndReadOutput("cat", "/proc/cpuinfo") ?? "";
string lscpu = ProcessHelper.RunAndReadOutput("/bin/bash", "-c \"lscpu\"");
string lscpu = ProcessHelper.RunAndReadOutput("/bin/bash", "-c \"lscpu\"", environmentVariables: languageInvariantEnvironment);
return LinuxCpuInfoParser.Parse(cpuInfo, lscpu);
}
}

0 comments on commit a58872b

Please sign in to comment.