Dedicated cpu with CPU affinity

meocon12

Renowned Member
Aug 7, 2014
23
0
66
Hello,
I want to ask about CPU affinity.
I have a server with hyperthreading enabled.
CPU has 14 cores and 28 threads:
CPU(s): 0-13,28-41
As I understand it 0-13 are physical cores.
28-41 are hyper-threaded cores
Do I understand that correctly?
When I create a virtual machine with 4core and 4Vcpu. I set CPU affinity to: 0-3, 28-31.
so my virtual machine is stuck on 4 physical cores and the first 4 hyper-threaded cores.
Will my virtual machine then be able to use up to 4 physical cores?
Like I set 4 cores with hyperthreading turned off.
Can anyone with experience tell me if I understand this correctly?
I want a virtual machine that can use up to 4 physical cores without being shared with other virtual machines.
Thank!
 
As I understand it 0-13 are physical cores.
28-41 are hyper-threaded cores
Do I understand that correctly?
The CPU numbering can wary from system to system. You can check which core each CPU is assigned to with the following command:
Code:
cat /proc/cpuinfo | grep "process\|core id"

When I create a virtual machine with 4core and 4Vcpu. I set CPU affinity to: 0-3, 28-31.
so my virtual machine is stuck on 4 physical cores and the first 4 hyper-threaded cores.
Will my virtual machine then be able to use up to 4 physical cores?
If CPUs 0-3 and 28-31 are assigned to physical cores 0-3 then yes.

I want a virtual machine that can use up to 4 physical cores without being shared with other virtual machines.
Keep in mind that setting the CPU affinity on one VM will not prevent other VMs or the host from still using these CPUs.
 
Last edited:
  • Like
Reactions: meocon12
The CPU numbering can wary from system to system. You can check which core each CPU is assigned to with the following command:
Code:
cat /proc/cpuinfo | grep "process\|core id"


If CPUs 0-3 and 28-31 are assigned to physical cores 0-3 then yes.


Keep in mind that setting the CPU affinity on one VM will not prevent other VMs or the host from still using these CPUs.
Hello,
Thank you for responding.
"Keep in mind that setting the CPU affinity on one VM will not prevent other VMs or the host from still using these CPUs.
"
Is there any way to do this?
I discovered virtual machines are always placed on the first cores.
So I set CPU affinity for the last cores, like mine are: 10-13, 38-41 and set cpu units to a high value.
Is my idea a good idea?
 
The CPUs that should be available to the host can be configured with a hook script that isolates the CPUs when the VM starts and makes all CPUs available to the host again when the VM stops:

Bash:
vmid="$1"
phase="$2"

case "$phase" in
    post-start)
        allowedCpus=0-9,28-37
        systemctl set-property --runtime init.scope AllowedCPUs=$allowedCpus
        systemctl set-property --runtime system.slice AllowedCPUs=$allowedCpus
        systemctl set-property --runtime user.slice AllowedCPUs=$allowedCpus
        ;;
    post-stop)
        allowedCpus=0-13,28-41
        systemctl set-property --runtime init.scope AllowedCPUs=$allowedCpus
        systemctl set-property --runtime system.slice AllowedCPUs=$allowedCpus
        systemctl set-property --runtime user.slice AllowedCPUs=$allowedCpus
        ;;
esac

The hook script can be configured with qm set <vmid> --hookscript local:snippets/hook.sh
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!