[pve-devel] [PATCH v2 qemu-server 1/4] fix #3593: add affinity to qemu

Daniel Bowder daniel at bowdernet.com
Fri Jul 1 02:09:45 CEST 2022


Reuse the PVE::CpuSet to validate cpuset formatting.
Add new qemu property called 'affinity' to store the cpuset.
Push taskset command in front of kvm if 'affinity' is set.

Signed-off-by: Daniel Bowder <daniel at bowdernet.com>
---
 PVE/QemuServer.pm | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 7d9cf22..94b44ae 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -28,6 +28,7 @@ use UUID;
 
 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file);
 use PVE::CGroup;
+use PVE::CpuSet;
 use PVE::DataCenterConfig;
 use PVE::Exception qw(raise raise_param_exc);
 use PVE::Format qw(render_duration render_bytes);
@@ -713,6 +714,11 @@ EODESCR
 	description => "Some (read-only) meta-information about this guest.",
 	optional => 1,
     },
+    affinity => {
+	type => 'string', format => 'pve-cpuset',
+	description => "List of host cores used to execute guest processes.",
+	optional => 1,
+    },
 };
 
 my $cicustom_fmt = {
@@ -1032,6 +1038,20 @@ foreach my $key (keys %$confdesc_cloudinit) {
     $confdesc->{$key} = $confdesc_cloudinit->{$key};
 }
 
+PVE::JSONSchema::register_format('pve-cpuset', \&pve_verify_cpuset);
+sub pve_verify_cpuset {
+    my ($set_text, $noerr) = @_;
+
+    my ($count, $members) = eval { PVE::CpuSet::parse_cpuset($set_text) };
+
+    if ($@) {
+	return if $noerr;
+	die "unable to parse cpuset option\n";
+    }
+
+    return PVE::CpuSet->new($members)->short_string();
+}
+
 PVE::JSONSchema::register_format('pve-volume-id-or-qm-path', \&verify_volume_id_or_qm_path);
 sub verify_volume_id_or_qm_path {
     my ($volid, $noerr) = @_;
@@ -3535,6 +3555,13 @@ sub config_to_command {
     my $use_old_bios_files = undef;
     ($use_old_bios_files, $machine_type) = qemu_use_old_bios_files($machine_type);
 
+    if ($conf->{affinity}) {
+	push @$cmd, "taskset";
+	push @$cmd, "--cpu-list";
+	push @$cmd, "--all-tasks";
+	push @$cmd, $conf->{affinity};
+    }
+
     push @$cmd, $kvm_binary;
 
     push @$cmd, '-id', $vmid;
-- 
2.36.1




More information about the pve-devel mailing list