Guide Dynamically Assign Static HugePages on VM startup/shutdown

Status
Not open for further replies.

ech0

Administrator
Staff member
May 26, 2020
35
0
6
Introduction

NOTE: This guide assumes you have enabled Hugepages enabled for your OS. Every OS is different so look up how to do that for your Host OS and then come back here.

If you want to assign 8GiB ram permanently to your VM then reserve 8192 hugepages like so:
$ sudo nano /etc/sysctl.conf
Code:
...
# VFIO HugePages
vm.nr_hugepages = 4096
That will always reserve the memory even when the VM isn't running.

But you can dynamically reserve the memory before VM start and free it after.

I use a bash function like this to reserve my 8192 pages for 16Gbs of Memory for the VM. (I have 64 GB of RAM but I run 2 Gaming VMs at once for my GF and I).


This goes in your QEMU Hooks Dir

Bash:
HUGEPAGES=8192

function activate_hugepages {
  echo "Allocating hugepages..."
  echo $HUGEPAGES > /proc/sys/vm/nr_hugepages
  ALLOC_PAGES=`cat /proc/sys/vm/nr_hugepages`

  TRIES=0
  while (( $ALLOC_PAGES != $HUGEPAGES && $TRIES < 1000 ))
  do
    echo 1 > /proc/sys/vm/compact_memory
    echo $HUGEPAGES > /proc/sys/vm/nr_hugepages
    ALLOC_PAGES=`cat /proc/sys/vm/nr_hugepages`
    echo "Tried to allocate hugepages. Got pages $ALLOC_PAGES / $HUGEPAGES"
    let TRIES+=1
  done

  if [ "$ALLOC_PAGES" -ne "$HUGEPAGES" ]
  then
    echo "Not able to allocate hugepages"
    echo 0 > /proc/sys/vm/nr_hugepages
    exit 1
  fi
}
Then make sure you have HugePages enabled for your VM. VirtXML below:

XML:
<domain type="kvm">
  <name>win10</name>
  <uuid>2c9806d3-44a8-4239-9d08-4505b40c2440</uuid>
  <metadata>
    <libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
      <libosinfo:os id="http://microsoft.com/win/10"/>
    </libosinfo:libosinfo>
  </metadata>
  <memory unit="KiB">16482304</memory>
  <currentMemory unit="KiB">16482304</currentMemory>
  <memoryBacking>
    <hugepages/>
  </memoryBacking>
  <vcpu placement="static">6</vcpu>
  <iothreads>6</iothreads>
  <cputune>
  ...
 
Status
Not open for further replies.