[pve-devel] [PATCH v3 qemu-server 5/7] cpuconfig: add find_best_cpumodel
Alexandre Derumier
aderumier at odiso.com
Mon May 22 12:25:26 CEST 2023
return the more recent cpu model compatible with host cpu
Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
PVE/QemuServer/CPUConfig.pm | 46 +++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index 45bf26f..63e57d4 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -14,6 +14,7 @@ our @EXPORT_OK = qw(
print_cpu_device
get_cpu_options
get_host_cpu_flags
+find_best_cpumodel
);
# under certain race-conditions, this module might be loaded before pve-cluster
@@ -696,6 +697,51 @@ sub get_host_cpu_flags {
return $res;
}
+sub find_best_cpumodel {
+ my ($host_flags, $vendor) = @_;
+
+ my $res = {};
+
+ #flags in qemu model but not in cpuinfo
+ my $missing_flag = {
+ #qemu internal
+ 'svme-addr-chk' => 1,
+ #TCG exposes CPUID flags FSRM, FZRM, FSRS, FSRC.
+ #These do not correspond to any new instructions;
+ #they only direct software to use string operations even if the length is zero or short.
+ fzrm => 1,
+ fsrc => 1,
+ fsr => 1,
+ fsrs => 1,
+ #masked from cpuinfo
+ xfd => 1,
+ };
+
+ foreach my $cpu (keys %$qemu_cpu_models) {
+ next if $qemu_cpu_models->{$cpu}->{vendor} ne $vendor;
+ next if $qemu_cpu_models->{$cpu}->{model} == 0;
+ $res->{$cpu} = $qemu_cpu_models->{$cpu};
+ my $flags = $qemu_cpu_models->{$cpu}->{'flags'};
+ next if !$flags;
+ foreach my $flag (keys %$flags) {
+ next if $missing_flag->{$flag};
+ if (!$host_flags->{$flag}) {
+ #print"$cpu unsupported flag: $flag\n";
+ delete $res->{$cpu};
+ }
+ }
+ }
+
+ foreach my $name (sort {
+ $res->{$b}->{family} <=> $res->{$a}->{family} ||
+ $res->{$b}->{model} <=> $res->{$a}->{model} ||
+ $res->{$b}->{version} <=> $res->{$a}->{version}
+ } keys %$res) {
+ return $name;
+ }
+}
+
+
__PACKAGE__->register();
__PACKAGE__->init();
--
2.30.2
More information about the pve-devel
mailing list