Skip to content

Commit a6edfe6

Browse files
Fix CPU frequency detection on Linux
1 parent 0978d02 commit a6edfe6

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Linq;
3+
using System.Text;
34
using BenchmarkDotNet.Helpers;
4-
using JetBrains.Annotations;
55
using Perfolizer.Horology;
66

77
namespace BenchmarkDotNet.Portability.Cpu
@@ -12,46 +12,64 @@ namespace BenchmarkDotNet.Portability.Cpu
1212
/// </summary>
1313
internal static class ProcCpuInfoProvider
1414
{
15-
internal static readonly Lazy<CpuInfo> ProcCpuInfo = new Lazy<CpuInfo>(Load);
15+
internal static readonly Lazy<CpuInfo> ProcCpuInfo = new (Load);
1616

1717
private static CpuInfo? Load()
1818
{
1919
if (RuntimeInformation.IsLinux())
2020
{
21-
string content = ProcessHelper.RunAndReadOutput("cat", "/proc/cpuinfo");
22-
string output = GetCpuSpeed();
23-
content = content + output;
21+
string content = ProcessHelper.RunAndReadOutput("cat", "/proc/cpuinfo") ?? "";
22+
string output = GetCpuSpeed() ?? "";
23+
content += output;
2424
return ProcCpuInfoParser.ParseOutput(content);
2525
}
2626
return null;
2727
}
2828

29-
private static string GetCpuSpeed()
29+
private static string? GetCpuSpeed()
3030
{
31-
var output = ProcessHelper.RunAndReadOutput("/bin/bash", "-c \"lscpu | grep MHz\"")?
32-
.Split('\n')
33-
.SelectMany(x => x.Split(':'))
34-
.ToArray();
31+
try
32+
{
33+
string[]? output = ProcessHelper.RunAndReadOutput("/bin/bash", "-c \"lscpu | grep MHz\"")?
34+
.Split('\n')
35+
.SelectMany(x => x.Split(':'))
36+
.ToArray();
3537

36-
return ParseCpuFrequencies(output);
38+
return ParseCpuFrequencies(output);
39+
}
40+
catch (Exception)
41+
{
42+
return null;
43+
}
3744
}
3845

39-
private static string ParseCpuFrequencies(string[] input)
46+
private static string? ParseCpuFrequencies(string[]? input)
4047
{
4148
// Example of output we trying to parse:
4249
//
4350
// CPU MHz: 949.154
4451
// CPU max MHz: 3200,0000
4552
// CPU min MHz: 800,0000
46-
//
47-
// And we don't need "CPU MHz" line
48-
if (input == null || input.Length < 6)
53+
54+
if (input == null)
4955
return null;
5056

51-
Frequency.TryParseMHz(input[3].Trim().Replace(',', '.'), out var minFrequency);
52-
Frequency.TryParseMHz(input[5].Trim().Replace(',', '.'), out var maxFrequency);
57+
var output = new StringBuilder();
58+
for (int i = 0; i + 1 < input.Length; i += 2)
59+
{
60+
string name = input[i].Trim();
61+
string value = input[i + 1].Trim();
62+
63+
if (name.EqualsWithIgnoreCase("CPU min MHz"))
64+
if (Frequency.TryParseMHz(value.Replace(',', '.'), out var minFrequency))
65+
output.Append($"\n{ProcCpuInfoKeyNames.MinFrequency}\t:{minFrequency.ToMHz()}");
66+
67+
if (name.EqualsWithIgnoreCase("CPU max MHz"))
68+
if (Frequency.TryParseMHz(value.Replace(',', '.'), out var maxFrequency))
69+
output.Append($"\n{ProcCpuInfoKeyNames.MaxFrequency}\t:{maxFrequency.ToMHz()}");
70+
}
5371

54-
return $"\n{ProcCpuInfoKeyNames.MinFrequency}\t:{minFrequency.ToMHz()}\n{ProcCpuInfoKeyNames.MaxFrequency}\t:{maxFrequency.ToMHz()}\n";
72+
return output.ToString();
5573
}
5674
}
5775
}

0 commit comments

Comments
 (0)