@@ -21,33 +21,59 @@ module Fog
2121 module Proxmox
2222 # module Cpu mixins
2323 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 )
2741 end
2842
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
3150 end
3251
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 ] )
3554 end
3655
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
3963 end
4064
4165 def self . flatten ( cpu_h )
42- return { } unless cpu_h [ 'cpu_type' ]
66+ return '' unless cpu_h [ 'cpu_type' ]
4367
4468 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
5177 cpu_type
5278 end
5379 end
0 commit comments