CPU optimization between two VMs

Is the name correct? It has to be the name specified in the VMs XML <name></name> tag! Maybe do a reboot and try again!

This is wrong, the path is /etc/libvirt/hooks/qemu. The last part qemu being the name of the file.

1 Like

Also you need to make the script executable with
sudo chmod +x /etc/libvirt/hooks/qemu
and then restart at least libvirt with
sudo systemctl restart libvirtd.service

1 Like

Ok… Maybe that was the problem on the first place…
I had the scripts under the wrong folder all along…
There is a folder ‘/etc/libvirt/qemu/’, I created ‘hooks’ in it and put there my prepare, release, started scripts, and obviously they didn’t work…

Now I moved the qemu file you suggested under ‘/etc/libvirt/’ folder, made it executable (it should have been, as I edited my old one) and tried again, but still the same…

I will try a reboot a bit later and comeback.

edit: found this in my logs: libvirtd.service internal error: End of file from qemu monitor
Can it be related?

If I try to run in terminal:

sudo bash /etc/libvirt/hooks/qemu win10-games prepare

I am getting these errors:

  File "/usr/lib/python3.9/site-packages/vfio_isolate/cpuset.py", line 57, in get_cpus
    with open(self.__path("cpuset.cpus"), "r") as f:
FileNotFoundError: [Errno 2] No such file or directory: '/sys/fs/cgroup/cpuset/host.slice/cpuset.cpus'
.........
  File "/usr/lib/python3.9/site-packages/vfio_isolate/cpuset.py", line 43, in create
    os.mkdir(self.__path())
FileNotFoundError: [Errno 2] No such file or directory: '/sys/fs/cgroup/cpuset/host.slice'

Is there a timestamp when this error occurred?

I do not think you can use it that way. What is happening when you start the VM, how do you see that is not working?

When I start the stress, fps drops from >140 to <50 and it is almost unresponsive.
As soon as I stop the stress, everything is back to normal.

1 Like

This should not happen anymore. vfio-isolate shields the VM from the rest. Your script also looks right to me, I double checked. My guess is that the script is not being invoked correctly.

case "$2" in
"prepare")
	enable_isolation
        echo "prepare" >> /home/<yourusername>/qemu_hook.log
	;;
"started")
	;;
"release")
	disable_isolation
       echo "release" >> /home/<yourusername>/qemu_hook.log
	;;
esac

Can you maybe edit the script like the code I pasted above. I am not entirely sure if qemu can only invoke scripts or if it can also run shell commands in this hook. This should, hopefully, output a file called qemu_hook.log in your users home directory. This way you could at least see if the functions to enable and disable the isolation are even getting run!

Yes, it wrote to the log, in both “prepare” and “release” events. But still, it doesn’t work. As soon as I fire the stress, the VM becomes almost unusable.

edit: and yes, since qemu is a bash script, it should be run from the terminal, and isolate the terminal PID. But these errors should appear there.

Found it!

For some reason, which I cannot recall, I had below setting in my Kernel parameters:

systemd.unified_cgroup_hierarchy=1

In order for the isolation to work, it needs to be 0

Again thank you for your efforts, really appreciate it! :metal: :+1:

You’re welcome. Does your gaming VM behave correctly now or it still has framedrops?

Looks ok! Couple of games have framedrops, but they are badly designed/implemented.
FurMark for example, stays stable with stress running and the feeling remains excellent.

1 Like

Well no, but yes…
So, When I run the “stress”, I have zero framedrops…
However, when I have both VMs on, it does…
And the 2nd VM doesn’t have any PIN, script, etc, and the only passthrough devices are a webcam and a NVME disk…
Can this part cause the issue (I have the same on both VMs) ?

  <iothreads>2</iothreads>
  <cputune>
    <emulatorpin cpuset="3,15"/>
    <iothreadpin iothread="1" cpuset="3"/>
    <iothreadpin iothread="2" cpuset="15"/>
  </cputune>

@anon86748826 Is the above setting correct for my Gaming VM?
If yes, what should I use for my Coding VM? The same or different cores? Currently I have nothing in the section nor , but my Gaming VM suffers CPU performance… :pensive:

I would not use the same cores for the coding VM. Try not pinning anything in the coding VM and see if the framedrops in the gaming VM vanish. If this kind of isolation does not help I am a bit lost what to try next.

1 Like

I did remove all settings from Coding VM, but this didn’t help.
However while looking around, I found two possible solutions.

  1. I enabled Huge Pages. It was something I forgot to do, as I had issues before. I did the steps again and now looks ok. Didn’t see any obvious performance change, but didn’t get worse, so I kept it
  2. After reading at this post, I disabled SVM, and that made the trick. However I need to search a bit deeper, as this blocks the Hyper-V from running in the guest machine, something that helps few games (with certain anti-cheats) to run. Currently I only have one in that category, so no big deal, but in the future I may have to find another way

@lI_Simo_Hayha_Il Beware that the newest version of vfio-isolate, depending on your setup, might need changes to the start script! Depending on which cgroup version is used by your OS. You can read up on the changes on the developers Github page. The hook script has to look something like this for cgroups v2:

#!/bin/bash

if [ "$1" != "gaming" ]; then
	exit 0
fi

HCPUS=0-6,16-22

MCPUS=8-15,24-31

UNDOFILE=/var/run/libvirt/qemu/vfio-isolate-undo.bin

disable_isolation () {
	vfio-isolate \
		restore $UNDOFILE

	taskset -pc 0-31 2 # kthreadd reset
}

enable_isolation () {
	vfio-isolate \
		-u $UNDOFILE \
		drop-caches \
                cpuset-modify --cpus C$HCPUS /system.slice \
		cpuset-modify --cpus C$HCPUS /user.slice \
		compact-memory \
		irq-affinity mask C$MCPUS

	taskset -pc $HCPUS 2 # ktreadd only on host cores
}



case "$2" in
"prepare")
	enable_isolation
	;;
"started")
	;;
"release")
	disable_isolation
	;;
esac
2 Likes

Is C%HCPUS /user.slice \ a typo?
Should it be C$HCPUS ?

You are correct, I missed that.

1 Like

Any idea why both ways (individual scripts & vfio-isolate) stopped working recently?
I am not sure if it was since the last Kernel update or before that…