[pve-devel] [PATCH container] start: use a random cpuset at startup

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Nov 23 12:48:55 CET 2016


we'll leave the rebalancing to pvestatd's next iteration
---
 src/PVE/LXC.pm | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 3a284f7..ab16109 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -18,6 +18,7 @@ use PVE::Storage;
 use PVE::SafeSyslog;
 use PVE::INotify;
 use PVE::Tools qw($IPV6RE $IPV4RE dir_glob_foreach lock_file lock_file_full O_PATH);
+use PVE::CpuSet;
 use PVE::Network;
 use PVE::AccessControl;
 use PVE::ProcFSTools;
@@ -436,15 +437,29 @@ sub update_lxc_config {
 	$raw .= "lxc.network.mtu = $d->{mtu}\n" if defined($d->{mtu});
     }
 
+    my $had_cpuset = 0;
     if (my $lxcconf = $conf->{lxc}) {
 	foreach my $entry (@$lxcconf) {
 	    my ($k, $v) = @$entry;
 	    $netcount++ if $k eq 'lxc.network.type';
+	    $had_cpuset = 1 if $k eq 'lxc.cgroup.cpuset.cpus';
 	    $raw .= "$k = $v\n";
 	}
     }
 
     $raw .= "lxc.network.type = empty\n" if !$netcount;
+
+    my $cores = $conf->{cores};
+    if (!$had_cpuset && $cores) {
+	my $cpuset = PVE::CpuSet->new_from_cgroup('lxc', 'effective_cpus');
+	my @members = $cpuset->members();
+	while (scalar(@members) > $cores) {
+	    my $randidx = int(rand(scalar(@members)));
+	    $cpuset->delete($members[$randidx]);
+	    splice(@members, $randidx, 1); # keep track of the changes
+	}
+	$raw .= "lxc.cgroup.cpuset.cpus = ".$cpuset->short_string()."\n";
+    }
     
     File::Path::mkpath("$dir/rootfs");
 
-- 
2.1.4





More information about the pve-devel mailing list