Skip to content

vmware_guest: Add support to CPU Affinity #2339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions plugins/modules/vmware_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
cpu_reservation:
type: int
description: The amount of CPU resource that is guaranteed available to the virtual machine.
cpu_affinity_set:
type: list
description: A List of CPU cores that the VM should use.
version:
type: str
description:
Expand Down Expand Up @@ -1104,8 +1107,10 @@
import time
import string

HAS_PYVMOMI = False
try:
from pyVmomi import vim, vmodl
HAS_PYVMOMI = True
except ImportError:
pass

Expand Down Expand Up @@ -1273,6 +1278,7 @@ def configure_resource_alloc_info(self, vm_obj):
rai_change_detected = False
memory_allocation = vim.ResourceAllocationInfo()
cpu_allocation = vim.ResourceAllocationInfo()
cpu_affinity = vim.vm.AffinityInfo()

memory_shares_info = vim.SharesInfo()
cpu_shares_info = vim.SharesInfo()
Expand All @@ -1286,6 +1292,13 @@ def configure_resource_alloc_info(self, vm_obj):
memory_allocation.shares.level != vm_obj.config.memoryAllocation.shares.level:
rai_change_detected = True

cpu_affinity_set = self.params['hardware']['cpu_affinity_set']
if cpu_affinity_set is not None:
cpu_affinity.affinitySet = cpu_affinity_set
if vm_obj is None or \
cpu_affinity.affinitySet != self.get_vm_cpu_affinity_set(vm_obj):
rai_change_detected = True

cpu_shares_level = self.params['hardware']['cpu_shares_level']
if cpu_shares_level is not None:
cpu_shares_info.level = cpu_shares_level
Expand Down Expand Up @@ -1343,6 +1356,7 @@ def configure_resource_alloc_info(self, vm_obj):
if rai_change_detected:
self.configspec.memoryAllocation = memory_allocation
self.configspec.cpuAllocation = cpu_allocation
self.configspec.cpuAffinity = cpu_affinity
self.change_detected = True

def configure_cpu_and_memory(self, vm_obj, vm_creation=False):
Expand Down Expand Up @@ -1716,6 +1730,11 @@ def get_vm_nvdimm_ctl_device(self, vm=None):
def get_vm_nvdimm_devices(self, vm=None):
return self.get_device_by_type(vm=vm, type=vim.vm.device.VirtualNVDIMM)

def get_vm_cpu_affinity_set(self, vm_obj):
if vm_obj.config.__dict__.get('cpuAffinity') is None:
return []
return vm_obj.config.cpuAffinity.affinitySet

def configure_nvdimm(self, vm_obj):
"""
Manage virtual NVDIMM device to the virtual machine
Expand Down Expand Up @@ -3516,6 +3535,7 @@ def main():
nested_virt=dict(type='bool'),
num_cpu_cores_per_socket=dict(type='int'),
num_cpus=dict(type='int'),
cpu_affinity_set=dict(type='list', default=[], elements='int', aliases=['cpu_affinity']),
scsi=dict(type='str', choices=['buslogic', 'lsilogic', 'lsilogicsas', 'paravirtual']),
secure_boot=dict(type='bool'),
version=dict(type='str'),
Expand Down