[pve-devel] [PATCH] RFC : hugepages lock allocation

Alexandre Derumier aderumier at odiso.com
Tue May 24 05:42:12 CEST 2016


This is a try to implementation hugepages lock on allocation.

Hupagepages need to be created, then qemu allocated them on start or hotplug

I'm not sure my code is super clean, please comment

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer.pm        | 23 +++++++++++++++--------
 PVE/QemuServer/Memory.pm | 23 ++++++++++++++++++++---
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 3b35d87..407aca3 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4344,12 +4344,6 @@ sub vm_start {
 	  }
         }
 
-        if ($conf->{hugepages}) {
-	    my $hugepages_topology = PVE::QemuServer::Memory::hugepages_topology($conf);
-	    PVE::QemuServer::Memory::hugepages_mount();
-	    PVE::QemuServer::Memory::hugepages_allocate($hugepages_topology);
-	}
-	
 	PVE::Storage::activate_volumes($storecfg, $vollist);
 
 	if (!check_running($vmid, 1) && -d "/sys/fs/cgroup/systemd/qemu.slice/$vmid.scope") {
@@ -4358,8 +4352,21 @@ sub vm_start {
 	    eval  { run_command($cmd); };
 	}
 
-	eval  { run_command($cmd, timeout => $statefile ? undef : 30,
-		    umask => 0077); };
+        if ($conf->{hugepages}) {
+	    my $code = sub {
+		my $hugepages_topology = PVE::QemuServer::Memory::hugepages_topology($conf);
+		PVE::QemuServer::Memory::hugepages_mount();
+		PVE::QemuServer::Memory::hugepages_allocate($hugepages_topology);
+
+		run_command($cmd, timeout => $statefile ? undef : 30,
+		    umask => 0077);
+	    };
+	    eval { PVE::QemuServer::Memory::hugepages_update_locked($code) };
+
+	} else {
+	    eval  { run_command($cmd, timeout => $statefile ? undef : 30,
+	 		umask => 0077); };
+	}
 
 	if (my $err = $@) {
 	    # deactivate volumes if start fails
diff --git a/PVE/QemuServer/Memory.pm b/PVE/QemuServer/Memory.pm
index 7b76bbf..dd269f9 100644
--- a/PVE/QemuServer/Memory.pm
+++ b/PVE/QemuServer/Memory.pm
@@ -82,10 +82,15 @@ sub qemu_memory_hotplug {
 		    my $hugepages_size = hugepages_size($conf, $dimm_size);
 		    my $path = hugepages_mount_path($hugepages_size);
 		    my $hugepages_topology->{$hugepages_size}->{$numanode} = hugepages_nr($dimm_size, $hugepages_size);
-		    hugepages_allocate($hugepages_topology);
 
-		    eval { PVE::QemuServer::vm_mon_cmd($vmid, "object-add", 'qom-type' => "memory-backend-file", id => "mem-$name", props => { 
-					     size => int($dimm_size*1024*1024), 'mem-path' => $path, share => JSON::true, prealloc => JSON::true } ) };
+		    my $code = {
+			hugepages_allocate($hugepages_topology);
+
+			PVE::QemuServer::vm_mon_cmd($vmid, "object-add", 'qom-type' => "memory-backend-file", id => "mem-$name", props => { 
+					     size => int($dimm_size*1024*1024), 'mem-path' => $path, share => JSON::true, prealloc => JSON::true } );
+		    };
+		    eval { hugepages_update_locked($code) };
+
 		} else {
 		    eval { PVE::QemuServer::vm_mon_cmd($vmid, "object-add", 'qom-type' => "memory-backend-ram", id => "mem-$name", props => { size => int($dimm_size*1024*1024) } ) };
 		}
@@ -484,5 +489,17 @@ sub hugepages_reset {
     }
 }
 
+sub hugepages_update_locked {
+    my ($code, @param) = @_;
+
+    my $timeout = 60; #could be long if a lot of hugepages need to be alocated
+
+    my $lock_filename = "/var/lock/hugepages.lck";
+
+    my $res = lock_file($lock_filename, $timeout, $code, @param);
+    die $@ if $@;
+
+    return $res;
+}
 1;
 
-- 
2.1.4




More information about the pve-devel mailing list