Merge alloc_hugepages, and unalloc_hugepages into one script, add checks and logging

This commit is contained in:
Shoelaceman 2020-02-12 16:52:32 -06:00
parent 5a33a6d62d
commit 3a0a6e772c
3 changed files with 67 additions and 72 deletions

View File

@ -1,39 +0,0 @@
#! /bin/bash
#
# Author:
#
# This hook automatically (un-)allocates static HugePages when starting/stopping a VM.
# This file depends on the Passthrough POST hook helper script found in this repo.
# Place this script in BOTH these directories (or symlink it):
# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/prepare/begin/
# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/release/end/
# $SYSCONFDIR usually is /etc/libvirt.
# Set the files as executable through `chmod +x`.
#
# Also make sure to configure your VM's XML file and /etc/fstab or this script won't work.
# Get size of VM-Memory and HugePages
XML_PATH=/etc/libvirt/qemu/$1.xml
MEM_HOST=$(grep 'MemAvailable' /proc/meminfo | grep -ohE '[[:digit:]]+') # Available host memory
MEM_GUEST=$(grep '<memory unit' $XML_PATH | grep -ohE '[[:digit:]]+') # VM memory to be allocated
HPG_SIZE=$(grep '<page size' $XML_PATH | grep -ohE '[[:digit:]]+') # HugePage size
function allocPages {
# Define path and current amount of HugePages
HPG_PATH=/sys/devices/system/node/node0/hugepages/hugepages-"$HPG_SIZE"kB/nr_hugepages
HPG_CURRENT=$(cat $HPG_PATH)
# Allocate HugePages
((HPG_NEW = HPG_CURRENT + MEM_GUEST / HPG_SIZE ))
echo $HPG_NEW > $HPG_PATH
}
function prepMemory {
# Prepare memory for allocation
echo 1 > /proc/sys/vm/compact_memory
}
# If VM fits into memory, then allocate HugePages
if (($MEM_GUEST < $MEM_HOST)); then
allocPages
fi

View File

@ -0,0 +1,67 @@
#! /bin/bash
#
# Author: Stefsinn (https://github.com/Stefsinn)
#
# This hook automatically un-allocates static HugePages when stopping a VM.
# This file depends on the PassthroughPOST hook helper script found here:
# https://github.com/PassthroughPOST/VFIO-Tools/tree/master/libvirt_hooks
# Place this script in BOTH these directories (or symlink it):
# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/prepare/begin/
# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/release/end/
# $SYSCONFDIR usually is /etc/libvirt.
# Get inputs from libvirt
GUEST_NAME="$1"
GUEST_ACTION="$2/$3"
# Get path to guest XML
XML_PATH="/etc/libvirt/qemu/$GUEST_NAME.xml"
# Get guest HugePage size
HPG_SIZE=$(grep '<page size' "$XML_PATH" | grep -ohE '[[:digit:]]+')
# Set path to HugePages
HPG_PATH="/sys/devices/system/node/node0/hugepages"
# Get current number of HugePages
HPG_CURRENT=$(cat "${HPG_PATH}/hugepages-${HPG_SIZE}kB/nr_hugepages")
# Get amount of memory used by the guest
GUEST_MEM=$(grep '<memory unit' "$XML_PATH" | grep -ohE '[[:digit:]]+')
# We define functions here named for each step libvirt calls the hook against
# respectively. These will be ran after checks pass at the end of the script.
function prepare/begin {
# Allocate HugePages
((HPG_NEW = HPG_CURRENT + GUEST_MEM / HPG_SIZE ))
echo "$HPG_NEW" > "$HPG_PATH"
kmessageNotify "Allocating ${GUEST_MEM}kB of HugePages for VM ${GUEST_NAME}"
}
function release/end {
# Unallocate HugePages
((HPG_NEW = HPG_CURRENT - GUEST_MEM / HPG_SIZE ))
echo "$HPG_NEW" > "$HPG_PATH"
kmessageNotify "Releasing ${GUEST_MEM}kB of HugePages for VM ${GUEST_NAME}"
}
function kmessageNotify {
MESSAGE="$1"
while read -r line; do
echo "libvirt_qemu hugepages: ${line}" > /dev/kmsg 2>&1
done < <(echo "${MESSAGE}")
}
# Do some checks before continuing
if [[ $HPG_SIZE -eq 0 ]]; then
# Break if HugePage size is 0
echo "ERROR: HugePage size cannot be 0." >&2
exit 1
elif [[ -z $GUEST_MEM ]]; then
echo "ERROR: Can't determine guest's memory allocation" >&2
exit 1
elif [[ ! -d "$HPG_PATH" ]]; then
# Break if HugePages path doesn't exist
echo "ERROR: ${HPG_PATH} does not exist. (HugePages disabled in kernel?)" >&2
exit 1
elif [[ -z $HPG_SIZE ]]; then
# This exits silently if HugePages appear disabled for a guest
exit 0
fi
# All checks passed, continue
${GUEST_ACTION}

View File

@ -1,33 +0,0 @@
#! /bin/bash
#
# Author: Stefsinn (https://github.com/Stefsinn)
#
# This hook automatically un-allocates static HugePages when stopping a VM.
# This file depends on the PassthroughPOST hook helper script found here:
# https://github.com/PassthroughPOST/VFIO-Tools/tree/master/libvirt_hooks
# Place this script in BOTH these directories (or symlink it):
# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/prepare/begin/
# $SYSCONFDIR/libvirt/hooks/qemu.d/your_vm/release/end/
# $SYSCONFDIR usually is /etc/libvirt.
# Get path to guest XML
XML_PATH="/etc/libvirt/qemu/${1}.xml"
# Get guest HugePage size
HPG_SIZE=$(grep '<page size' "$XML_PATH" | grep -ohE '[[:digit:]]+')
# Set path to HugePages
HPG_PATH="/sys/devices/system/node/node0/hugepages"
function unallocPages {
# VM memory to be allocated
MEM_GUEST=$(grep '<memory unit' "$XML_PATH" | grep -ohE '[[:digit:]]+')
# Current number of HugePages
HPG_CURRENT=$(cat "${HPG_PATH}/hugepages-${HPG_SIZE}kB/nr_hugepages")
# Unallocate HugePages
((HPG_NEW = HPG_CURRENT - MEM_GUEST / HPG_SIZE ))
echo "$HPG_NEW" > "$HPG_PATH"
}
# Call function to unallocate HugePages if HugePage count is greater than 0
if [[ HPG_SIZE -gt 0 ]]; then
unallocPages
fi