[pve-devel] [PATCH 1/2] add hugepages option

Alexandre Derumier aderumier at odiso.com
Mon May 9 05:02:59 CEST 2016


vm configuration
----------------
hugepages: 1

host configuration
------------------
hugepages need to be allocated at boot

for 4GB of 2M hugepages

/etc/default/grub
-----------------
GRUB_CMDLINE_LINUX_DEFAULT="quiet hugepagesz=2M hugepages=2048"

/etc/fstab
----------
hugetlbfs  /dev/hugepages  hugetlbfs       pagesize=2048k        0 0

for 4GB for 1GB hugepages

/etc/default/grub
-----------------
GRUB_CMDLINE_LINUX_DEFAULT="quiet default_hugepagesz=1G hugepagesz=1G hugepages=4"

/etc/fstab
----------
hugetlbfs  /dev/hugepages  hugetlbfs       pagesize=1GB        0 0

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

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 89873e9..08ad61f 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -319,6 +319,12 @@ EODESC
 	description => "Enable/disable NUMA.",
 	default => 0,
     },
+    hugepages => {
+	optional => 1,
+	type => 'boolean',
+	description => "Enable/disable hugepages memory.",
+	default => 0,
+    },
     vcpus => {
 	optional => 1,
 	type => 'integer',
@@ -1390,6 +1396,17 @@ sub machine_type_is_q35 {
     return $conf->{machine} && ($conf->{machine} =~ m/q35/) ? 1 : 0;
 }
 
+sub print_mem_object {
+    my ($conf, $id, $size) = @_;
+
+    if ($conf->{hugepages}) {
+	return "memory-backend-file,id=$id,size=${size}M,mem-path=/dev/hugepages,share=on,prealloc=yes";
+    } else {
+	return "memory-backend-ram,id=$id,size=${size}M";
+    }
+
+}
+
 sub print_tabletdevice_full {
     my ($conf) = @_;
 
@@ -3140,6 +3157,8 @@ sub config_to_command {
 	push @$cmd, '-m', $static_memory;
     }
 
+    die "numa need to be enabled to use hugepages" if $conf->{hugepages} && !$conf->{numa};
+
     if ($conf->{numa}) {
 
 	my $numa_totalmemory = undef;
@@ -3151,7 +3170,8 @@ sub config_to_command {
 	    die "missing numa node$i memory value\n" if !$numa->{memory};
 	    my $numa_memory = $numa->{memory};
 	    $numa_totalmemory += $numa_memory;
-	    my $numa_object = "memory-backend-ram,id=ram-node$i,size=${numa_memory}M";
+
+	    my $mem_object = print_mem_object($conf, "ram-node$i", $numa_memory);
 
 	    # cpus
 	    my $cpulists = $numa->{cpus};
@@ -3179,10 +3199,10 @@ sub config_to_command {
 		# policy
 		my $policy = $numa->{policy};
 		die "you need to define a policy for hostnode $hostnodes\n" if !$policy;
-		$numa_object .= ",host-nodes=$hostnodes,policy=$policy";
+		$mem_object .= ",host-nodes=$hostnodes,policy=$policy";
 	    }
 
-	    push @$cmd, '-object', $numa_object;
+	    push @$cmd, '-object', $mem_object;
 	    push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=ram-node$i";
 	}
 
@@ -3192,7 +3212,7 @@ sub config_to_command {
 	#if no custom tology, we split memory and cores across numa nodes
 	if(!$numa_totalmemory) {
 
-	    my $numa_memory = ($static_memory / $sockets) . "M";
+	    my $numa_memory = ($static_memory / $sockets);
 
 	    for (my $i = 0; $i < $sockets; $i++)  {
 
@@ -3201,7 +3221,9 @@ sub config_to_command {
 		my $cpus = $cpustart;
 		$cpus .= "-$cpuend" if $cpuend;
 
-		push @$cmd, '-object', "memory-backend-ram,size=$numa_memory,id=ram-node$i";
+		my $mem_object = print_mem_object($conf, "ram-node$i", $numa_memory);
+
+		push @$cmd, '-object', $mem_object;
 		push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=ram-node$i";
 	    }
 	}
@@ -3210,7 +3232,9 @@ sub config_to_command {
     if ($hotplug_features->{memory}) {
 	foreach_dimm($conf, $vmid, $memory, $sockets, sub {
 	    my ($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory) = @_;
-	    push @$cmd, "-object" , "memory-backend-ram,id=mem-$name,size=${dimm_size}M";
+	    my $mem_object = print_mem_object($conf, "mem-$name", $dimm_size);
+
+	    push @$cmd, "-object" , $mem_object;
 	    push @$cmd, "-device", "pc-dimm,id=$name,memdev=mem-$name,node=$numanode";
 
 	    #if dimm_memory is not aligned to dimm map
@@ -3862,7 +3886,13 @@ sub qemu_memory_hotplug {
 
 		return if $current_size <= $conf->{memory};
 
-		eval { vm_mon_cmd($vmid, "object-add", 'qom-type' => "memory-backend-ram", id => "mem-$name", props => { size => int($dimm_size*1024*1024) } ) };
+		if ($conf->{hugepages}) {
+		    eval { vm_mon_cmd($vmid, "object-add", 'qom-type' => "memory-backend-file", id => "mem-$name", props => { 
+					     size => int($dimm_size*1024*1024), 'mem-path' => '/dev/hugepages', share => JSON::true, prealloc => JSON::true } ) };
+		} else {
+		    eval { vm_mon_cmd($vmid, "object-add", 'qom-type' => "memory-backend-ram", id => "mem-$name", props => { size => int($dimm_size*1024*1024) } ) };
+		}
+
 		if (my $err = $@) {
 		    eval { qemu_objectdel($vmid, "mem-$name"); };
 		    die $err;
-- 
2.1.4




More information about the pve-devel mailing list