#!/bin/bash
# requires `cset`
# available on Fedora through my COPR:
# - https://copr.fedorainfracloud.org/coprs/jlay/cpuset/
# review notes regarding XML entries
# this script:
# - isolates host tasks from the VM CPUs
# - applies some generic tweaks
# - resets host isolation on VM shutdown
# Set the range of cores assigned to the windows VM
# on an i9-7920x this is the first 8 cores and the corresponding hyperthreads
VM_CORES='0-7,12-19'
# based on the assignments of pinned CPUs in libvirt XML, eg:
#
#
# [...]
#
#
#
# `lscpu -e` and `virsh capabilities` are helpful for determining valid pinning settings
# on example system (i9 7920x), vCPUs 0-15 are pinned (alternating between) cores 0-7 and hyperthreads 12-19
# reduce kernel jitter
sudo sysctl vm.stat_interval=120
sudo sysctl kernel.watchdog=0
# isolate VM CPUs from host tasks
# VM must have the partition cset uses w/ userset below defined in libvirt XML, eg:
#
# ... reference line
#
# /windows10
#
# ... reference line
# first, attempt to reset shielding. we want to recreate conflicting names with set params
sudo cset shield --reset --sysset=host.slice --userset=windows10.slice
sudo cset set -d windows10.slice
# shield cores
sudo cset shield -c $VM_CORES --sysset=host.slice --userset=windows10.slice
# start the VM
sudo virsh start windows10
# set higher priority
QEMU_PID=$(sudo ps fauxww | awk '$0 ~ /qemu.*windows1[0]/ {print $2}')
sudo renice -20 -p ${QEMU_PID}
sudo chrt -f -p 99 ${QEMU_PID}
echo "Waiting for windows10 VM to stop before resetting cpusets"
while true; do
# chill a bit
sleep 10
# get vm state, check if it's off
VM_STATE=$(sudo virsh dominfo windows10 | awk '$1 ~ /State/ {print $NF}')
if [ "$VM_STATE" == 'off' ]; then
echo -e "windows10 VM shut down, setting cpusets back to normal\n"
sudo cset shield --reset --sysset=host.slice --userset=windows10.slice
exit 0
fi
done