Hi,
I'd like to ask about the best practice on KVM guest configuration to avoid host swap usage and have the best performance.
On virtualitzation systems I liked to remove the swap partition on my VMs and give enought resources to the VMs and Hosts, but this can be sometimes problematic, so I use this strategy just sometimes, with concrete VM that I don't want to page memory on slow disk (and I monitor them to be sure that the assigned memory is enought).
In some of my proxmox nodes, despite having enought memory, I see a high usage of swap (> 90%). For example there is one with 128GB and 48% of RAM usage which is using 90.38% of swap (7.23GB over 8GB available).
Using a command I found time ago in https://stackoverflow.com/questions/30481314/how-can-i-know-which-process-is-using-swap, I can see which are the VMs that use more swap, so the first thing I usually do to balance the swap usage on the hosts is to move some of the high swap demanding VMs to another node (I've seen also that reducing the swappiness on the node to 10 makes the live migration process faster in the case that the VM is using a lot of swap, because the host is more agressive recovering swap, but this is another point). The command I use is:
(echo "COMM PID SWAP"; for file in /proc/*/status ; do awk '/^Pid|VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | grep kB | grep -wv "0 kB" | sort -k 3 -n -r)
I have been searching information about the best practice on swap usage on the VM and one post, by Christopher Pereira on https://unix.stackexchange.com/questions/137509/allocating-swap-space-with-kvm, seems interesesting for me. It's a different aproach:
"You should also enable swap space on the VM, so it is aware that RAM is being swapped and avoid using it for disk cache and IO buffers.
If you only enable swap on the host, the VM will believe it has available real RAM and will use it, while it will be swapped on the host creating unnecessary bottlenecks and freezes. This is the biggest mistake you could do.
You should also use a different virtual drive as a swap disk, since you don't want to snapshot/backup it together with your other data drives.
Since swap space can be volatile, you could also use cache=unsafe or use zram/zswap for better performance.
Also remember that swapping is good, since it frees inactive RAM that can later be reused for disk cache and IO buffers."
What do you thing about this subject? Is Christopher Pereira right?
Regards,
Manuel Martínez
I'd like to ask about the best practice on KVM guest configuration to avoid host swap usage and have the best performance.
On virtualitzation systems I liked to remove the swap partition on my VMs and give enought resources to the VMs and Hosts, but this can be sometimes problematic, so I use this strategy just sometimes, with concrete VM that I don't want to page memory on slow disk (and I monitor them to be sure that the assigned memory is enought).
In some of my proxmox nodes, despite having enought memory, I see a high usage of swap (> 90%). For example there is one with 128GB and 48% of RAM usage which is using 90.38% of swap (7.23GB over 8GB available).
Using a command I found time ago in https://stackoverflow.com/questions/30481314/how-can-i-know-which-process-is-using-swap, I can see which are the VMs that use more swap, so the first thing I usually do to balance the swap usage on the hosts is to move some of the high swap demanding VMs to another node (I've seen also that reducing the swappiness on the node to 10 makes the live migration process faster in the case that the VM is using a lot of swap, because the host is more agressive recovering swap, but this is another point). The command I use is:
(echo "COMM PID SWAP"; for file in /proc/*/status ; do awk '/^Pid|VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | grep kB | grep -wv "0 kB" | sort -k 3 -n -r)
I have been searching information about the best practice on swap usage on the VM and one post, by Christopher Pereira on https://unix.stackexchange.com/questions/137509/allocating-swap-space-with-kvm, seems interesesting for me. It's a different aproach:
"You should also enable swap space on the VM, so it is aware that RAM is being swapped and avoid using it for disk cache and IO buffers.
If you only enable swap on the host, the VM will believe it has available real RAM and will use it, while it will be swapped on the host creating unnecessary bottlenecks and freezes. This is the biggest mistake you could do.
You should also use a different virtual drive as a swap disk, since you don't want to snapshot/backup it together with your other data drives.
Since swap space can be volatile, you could also use cache=unsafe or use zram/zswap for better performance.
Also remember that swapping is good, since it frees inactive RAM that can later be reused for disk cache and IO buffers."
What do you thing about this subject? Is Christopher Pereira right?
Regards,
Manuel Martínez
Last edited: