54 lines
1.9 KiB
Bash
54 lines
1.9 KiB
Bash
#!/bin/bash
|
|
|
|
|
|
MEMORY=32768
|
|
SSDADDRESS=0000:04:00.0
|
|
|
|
## Calculate number of hugepages to allocate from memory (in MB)
|
|
HUGEPAGES="$(($MEMORY/$(($(grep Hugepagesize /proc/meminfo | awk '{print $2}')/1024))))"
|
|
## Node to allocate on (node1 for this VM)
|
|
NODESPATH=/sys/devices/system/node/node1
|
|
|
|
# BIND SSD
|
|
echo $SSDADDRESS > /sys/bus/pci/devices/$SSDADDRESS/driver/unbind
|
|
echo $SSDADDRESS > /sys/bus/pci/drivers/vfio-pci/bind
|
|
|
|
echo "Allocating hugepages..."
|
|
echo $HUGEPAGES > $NODESPATH/hugepages/hugepages-2048kB/nr_hugepages
|
|
ALLOC_PAGES=$(cat $NODESPATH/hugepages/hugepages-2048kB/nr_hugepages)
|
|
|
|
TRIES=0
|
|
while (( $ALLOC_PAGES != $HUGEPAGES && $TRIES < 10000 ))
|
|
do
|
|
echo 1 > /proc/sys/vm/compact_memory ## defrag ram
|
|
echo $HUGEPAGES > $NODESPATH/hugepages/hugepages-2048kB/nr_hugepages
|
|
ALLOC_PAGES=$(cat $NODESPATH/hugepages/hugepages-2048kB/nr_hugepages)
|
|
echo "Succesfully allocated $ALLOC_PAGES / $HUGEPAGES"
|
|
let TRIES+=1
|
|
done
|
|
|
|
if [ "$ALLOC_PAGES" -ne "$HUGEPAGES" ]
|
|
then
|
|
echo "Not able to allocate all hugepages. Reverting..."
|
|
echo 0 > $NODESPATH/hugepages/hugepages-2048kB/nr_hugepages
|
|
exit 1
|
|
fi
|
|
|
|
# libvirt likes to create a cpuset called "machine" and leave it behind after closing
|
|
# this confuses cset. remove it just in case
|
|
rmdir /sys/fs/cgroup/cpuset/machine
|
|
|
|
# reserve cpu 0,4 (core 0) for the host and give 1-3,5-7 (core 1-3) to the guest/vm
|
|
cset shield --kthread on --cpu 14-27,42-55
|
|
|
|
# cset reserves the cpusets for exclusive use but libvirt also wants to create
|
|
# new cpusets containing the same cpus. Remove the exclusive flag.
|
|
echo 0 > /sys/fs/cgroup/cpuset/system/cpuset.cpu_exclusive
|
|
echo 0 > /sys/fs/cgroup/cpuset/user/cpuset.cpu_exclusive
|
|
|
|
# The kernel's dirty page writeback mechanism uses kthread workers. They introduce
|
|
# massive arbitrary latencies when doing disk writes on the host and aren't
|
|
# migrated by cset. Restrict the workqueue to use only cpu 0.
|
|
|
|
echo 1 > /sys/devices/virtual/workqueue/cpumask
|