1
1
using System ;
2
2
using System . Linq ;
3
+ using System . Text ;
3
4
using BenchmarkDotNet . Helpers ;
4
- using JetBrains . Annotations ;
5
5
using Perfolizer . Horology ;
6
6
7
7
namespace BenchmarkDotNet . Portability . Cpu
@@ -12,46 +12,64 @@ namespace BenchmarkDotNet.Portability.Cpu
12
12
/// </summary>
13
13
internal static class ProcCpuInfoProvider
14
14
{
15
- internal static readonly Lazy < CpuInfo > ProcCpuInfo = new Lazy < CpuInfo > ( Load ) ;
15
+ internal static readonly Lazy < CpuInfo > ProcCpuInfo = new ( Load ) ;
16
16
17
17
private static CpuInfo ? Load ( )
18
18
{
19
19
if ( RuntimeInformation . IsLinux ( ) )
20
20
{
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 ;
24
24
return ProcCpuInfoParser . ParseOutput ( content ) ;
25
25
}
26
26
return null ;
27
27
}
28
28
29
- private static string GetCpuSpeed ( )
29
+ private static string ? GetCpuSpeed ( )
30
30
{
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 ( ) ;
35
37
36
- return ParseCpuFrequencies ( output ) ;
38
+ return ParseCpuFrequencies ( output ) ;
39
+ }
40
+ catch ( Exception )
41
+ {
42
+ return null ;
43
+ }
37
44
}
38
45
39
- private static string ParseCpuFrequencies ( string [ ] input )
46
+ private static string ? ParseCpuFrequencies ( string [ ] ? input )
40
47
{
41
48
// Example of output we trying to parse:
42
49
//
43
50
// CPU MHz: 949.154
44
51
// CPU max MHz: 3200,0000
45
52
// 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 )
49
55
return null ;
50
56
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
+ }
53
71
54
- return $ " \n { ProcCpuInfoKeyNames . MinFrequency } \t : { minFrequency . ToMHz ( ) } \n { ProcCpuInfoKeyNames . MaxFrequency } \t : { maxFrequency . ToMHz ( ) } \n " ;
72
+ return output . ToString ( ) ;
55
73
}
56
74
}
57
75
}
0 commit comments