To limit the amount of CPU resources a virtual machine (VM) can use, you can configure virtual CPU (vCPU) capping. vCPU capping can improve the overall performance by preventing excessive use of host’s CPU resources by a single VM and by making it easier for the hypervisor to manage CPU scheduling.
Procedure
-
View the current vCPU scheduling configuration on the host.
# virsh schedinfo <vm_name>
Scheduler : posix
cpu_shares : 0
vcpu_period : 0
vcpu_quota : 0
emulator_period: 0
emulator_quota : 0
global_period : 0
global_quota : 0
iothread_period: 0
iothread_quota : 0
-
To configure an absolute vCPU cap for a VM, set the
vcpu_period and vcpu_quota parameters. Both parameters use a numerical value that represents a time duration in microseconds.
-
Set the
vcpu_period parameter by using the virsh schedinfo command. For example:
# virsh schedinfo <vm_name> --set vcpu_period=100000
In this example, the vcpu_period is set to 100,000 microseconds, which means the scheduler enforces vCPU capping during this time interval.
You can also use the --live --config options to configure a running VM without restarting it.
-
Set the
vcpu_quota parameter by using the virsh schedinfo command. For example:
# virsh schedinfo <vm_name> --set vcpu_quota=50000
In this example, the vcpu_quota is set to 50,000 microseconds, which specifies the maximum amount of CPU time that the VM can use during the vcpu_period time interval. In this case, vcpu_quota is set as the half of vcpu_period, so the VM can use up to 50% of the CPU time during that interval.
You can also use the --live --config options to configure a running VM without restarting it.
Results
- Check that the vCPU scheduling parameters have the correct values.
# virsh schedinfo <vm_name>
Scheduler : posix
cpu_shares : 2048
vcpu_period : 100000
vcpu_quota : 50000
...