[pve-devel] [PATCH 1/3] implement cgroups && cpu.shares

Alexandre Derumier aderumier at odiso.com
Thu May 28 12:28:28 CEST 2015


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer.pm | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 22ff875..21fa84c 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -67,6 +67,58 @@ PVE::JSONSchema::register_standard_option('pve-snapshot-name', {
 
 #no warnings 'redefine';
 
+sub cgroups_create {
+   my ($controller, $vmid) = @_;
+
+   my $pid = check_running($vmid, 1);
+   return if !$pid;
+
+   my $vm_path = cgroups_path($controller, $vmid);
+   return if !$vm_path;
+
+   dir_glob_foreach("/proc/$pid/task/", '(\d+)', sub {
+	my ($task) = @_;
+
+	PVE::ProcFSTools::write_proc_entry("$vm_path/tasks", $task);
+   });
+
+}
+
+sub cgroups_delete {
+   my ($controller, $vmid) = @_;
+
+   return if check_running($vmid, 1);
+
+   my $vm_path = cgroups_path($controller, $vmid);
+   return if !$vm_path;
+
+   rmdir $vm_path;
+}
+
+sub cgroups_write {
+   my ($controller, $vmid, $option, $value) = @_;
+
+   my $vm_path = cgroups_path($controller, $vmid);
+   return if !$vm_path;
+
+   PVE::ProcFSTools::write_proc_entry("$vm_path/$option", $value);
+
+}
+
+sub cgroups_path {
+   my ($controller, $vmid) = @_;
+
+   my $root_path = "/sys/fs/cgroup/";
+   my $controller_path = $root_path.$controller."/qemu";
+   my $vm_path = $controller_path."/".$vmid;
+
+   return undef if (! -d $root_path);
+   mkdir $controller_path if (! -d $controller_path);
+   mkdir $vm_path if (! -d $vm_path);
+
+   return $vm_path;
+}
+
 unless(defined(&_VZSYSCALLS_H_)) {
     eval 'sub _VZSYSCALLS_H_ () {1;}' unless defined(&_VZSYSCALLS_H_);
     require 'sys/syscall.ph';
@@ -4282,6 +4334,12 @@ sub vm_start {
 		    property => "guest-stats-polling-interval",
 		    value => 2) if (!defined($conf->{balloon}) || $conf->{balloon});
 
+        my $cpuunits = defined($conf->{cpuunits}) ?
+            $conf->{cpuunits} : $defaults->{cpuunits};
+	
+	cgroups_create("cpu", $vmid);
+	cgroups_write("cpu", $vmid, "cpu.shares", $cpuunits);
+	
     });
 }
 
@@ -4393,6 +4451,7 @@ sub vm_stop_cleanup {
     my ($storecfg, $vmid, $conf, $keepActive, $apply_pending_changes) = @_;
 
     eval {
+	cgroups_delete("cpu", $vmid);
 	fairsched_rmnod($vmid); # try to destroy group
 
 	if (!$keepActive) {
@@ -4549,6 +4608,7 @@ sub vm_destroy {
 	check_lock($conf) if !$skiplock;
 
 	if (!check_running($vmid)) {
+	    cgroups_delete("cpu", $vmid);
 	    fairsched_rmnod($vmid); # try to destroy group
 	    destroy_vm($storecfg, $vmid);
 	} else {
-- 
2.1.4




More information about the pve-devel mailing list