@@ -21,33 +21,59 @@ module Fog
21
21
module Proxmox
22
22
# module Cpu mixins
23
23
module CpuHelper
24
- CPU_REGEXP = /(\b cputype=)?([\w -]+)[,]?(\b flags=)?(\+ [\w -]+)?[;]?(\+ [\w -]+)?/
25
- def self . extract ( cpu , i )
26
- cpu ? CPU_REGEXP . match ( cpu . to_s ) [ i ] : ''
24
+ CPU_REGEXP = /(\b cputype=)?(?<cputype>[\w -]+)[,]?(\b flags=)?(?<flags>[[\+ \- ][\w -]+[;]?]*)/
25
+ FLAGS = { spectre : 'spec-ctrl' , pcid : 'pcid' , ssbd : 'ssbd' , ibpb : 'ibpb' , virt_ssbd : 'virt-ssbd' , amd_ssbd : 'amd-ssbd' , amd_no_ssb : 'amd-no-ssb' , md_clear : 'md-clear' , pdpe1gb : 'pdpe1gb' , hv_tlbflush : 'hv-tlbflush' , aes : 'aes' , hv_evmcs : 'hv-evmcs' }
26
+ def self . flags
27
+ FLAGS
28
+ end
29
+
30
+ def self . extract ( cpu , name )
31
+ captures_h = cpu ? CPU_REGEXP . match ( cpu . to_s ) : { cputype : '' , flags : '' }
32
+ captures_h [ name ]
33
+ end
34
+
35
+ def self . extract_cputype ( cpu )
36
+ extract ( cpu , :cputype )
37
+ end
38
+
39
+ def self . extract_flags ( cpu )
40
+ extract ( cpu , :flags )
27
41
end
28
42
29
- def self . extract_type ( cpu )
30
- extract ( cpu , 2 )
43
+ def self . flag_value ( cpu , flag_key )
44
+ flag_value = '0'
45
+ raw_values = extract_flags ( cpu ) . split ( ';' ) . select { |flag | [ '+' + flag_key , '-' + flag_key ] . include? ( flag ) }
46
+ unless raw_values . empty?
47
+ flag_value = raw_values [ 0 ] . start_with? ( '+' ) ? '+1' : raw_values [ 0 ] . start_with? ( '-' ) ? '-1' : '0'
48
+ end
49
+ flag_value
31
50
end
32
51
33
- def self . has_pcid? ( cpu )
34
- extract ( cpu , 5 ) == '+pcid'
52
+ def self . hash_has_no_default_flag? ( cpu_h , flag_name )
53
+ cpu_h . key? ( flag_name ) && [ '-1' , '+1' ] . include? ( cpu_h [ flag_name ] )
35
54
end
36
55
37
- def self . has_spectre? ( cpu )
38
- extract ( cpu , 4 ) == '+spec-ctrl'
56
+ def self . hash_flag ( cpu_h , flag_name )
57
+ flag = ''
58
+ if cpu_h . key? ( flag_name )
59
+ flag = '+' if cpu_h [ flag_name ] == '+1'
60
+ flag = '-' if cpu_h [ flag_name ] == '-1'
61
+ end
62
+ flag
39
63
end
40
64
41
65
def self . flatten ( cpu_h )
42
- return { } unless cpu_h [ 'cpu_type' ]
66
+ return '' unless cpu_h [ 'cpu_type' ]
43
67
44
68
cpu_type = "cputype=#{ cpu_h [ 'cpu_type' ] } "
45
- spectre = cpu_h [ 'spectre' ] . to_i == 1
46
- pcid = cpu_h [ 'pcid' ] . to_i == 1
47
- cpu_type += ',flags=' if spectre || pcid
48
- cpu_type += '+spec-ctrl' if spectre
49
- cpu_type += ';' if spectre && pcid
50
- cpu_type += '+pcid' if pcid
69
+ num_flags = 0
70
+ FLAGS . each_key { |flag_key | num_flags += 1 if hash_has_no_default_flag? ( cpu_h , flag_key . to_s ) }
71
+ cpu_type += ',flags=' if num_flags > 0
72
+ flags_with_no_default_value = FLAGS . select { |flag_key , _flag_value | hash_has_no_default_flag? ( cpu_h , flag_key . to_s ) }
73
+ flags_with_no_default_value . each_with_index do |( flag_key , flag_value ) , index |
74
+ cpu_type += hash_flag ( cpu_h , flag_key . to_s ) + flag_value if hash_has_no_default_flag? ( cpu_h , flag_key . to_s )
75
+ cpu_type += ';' if num_flags > index + 1
76
+ end
51
77
cpu_type
52
78
end
53
79
end
0 commit comments