def check_vcpu_affinity(): """ check vcpu affinity defined by vcpu cpuset or cputune cpuset """ affinity = vcpu_cpuset if not cputune_cpuset else cputune_cpuset affinity = {vcpu: affinity} virsh.vcpuinfo(vm_name, debug=True) host_cpu_count = cpu.total_cpus_count() vmxml_live = vm_xml.VMXML.new_from_dumpxml(vm_name) logging.debug(vmxml_live) # if vcpu >= maxvcpu, the cputune should not exist in xml if int(vcpu) >= int(maxvcpu): try: if hasattr(vmxml_live, 'cputune'): test.fail("cputune tag is set when vcpu >= maxvcpu") except xcepts.LibvirtXMLError: pass elif "config" in vcpupin_option: vcpu_affinity = utils_hotplug.affinity_from_vcpupin( vm, vcpu, vcpupin_option) affinity = libvirt.cpus_string_to_affinity_list( str(affinity[vcpu]), host_cpu_count) logging.debug("vcpu_affinity {}".format(vcpu_affinity)) logging.debug("affinity {}".format(affinity)) if vcpu_affinity[int(vcpu)] != affinity: test.fail("vcpu affinity check fail") # check the expected vcpu affinity with the one got from running vm elif not utils_hotplug.check_affinity(vm, affinity): test.fail("vcpu affinity check fail")
def affinity_from_vcpupin(vm): """ Returns dict of vcpu's affinity from virsh vcpupin output :param vm: VM object :return: dict of affinity of VM """ vcpupin_output = {} vcpupin_affinity = {} host_cpu_count = utils.total_cpus_count() result = virsh.vcpupin(vm.name) for vcpu in results_stdout_52lts(result).strip().split('\n')[2:]: vcpupin_output[int(vcpu.split(":")[0])] = vcpu.split(":")[1] for vcpu in vcpupin_output: vcpupin_affinity[vcpu] = libvirt.cpus_string_to_affinity_list( vcpupin_output[vcpu], host_cpu_count) return vcpupin_affinity
def affinity_from_vcpupin(vm): """ Returns dict of vcpu's affinity from virsh vcpupin output :param vm: VM object :return: dict of affinity of VM """ vcpupin_output = {} vcpupin_affinity = {} host_cpu_count = utils.total_cpus_count() result = virsh.vcpupin(vm.name) for vcpu in results_stdout_52lts(result).strip().split('\n')[2:]: vcpupin_output[int(vcpu.split(":")[0])] = vcpu.split(":")[1] for vcpu in vcpupin_output: vcpupin_affinity[vcpu] = libvirt.cpus_string_to_affinity_list( vcpupin_output[vcpu], host_cpu_count) return vcpupin_affinity
def affinity_from_vcpupin(vm, vcpu=None, options=None): """ Returns dict of vcpu's affinity from virsh vcpupin output :param vm: VM object :param vcpu: virtual cpu to qeury :param options: --live, --current or --config :return: dict of affinity of VM """ vcpupin_output = {} vcpupin_affinity = {} host_cpu_count = utils.total_cpus_count() result = virsh.vcpupin(vm.name, vcpu=vcpu, options=options, debug=True) for vcpu in results_stdout_52lts(result).strip().split('\n')[2:]: # On newer version of libvirt, there is no ':' in # vcpupin output anymore vcpupin_output[int(vcpu.split()[0].rstrip(':'))] = vcpu.split()[1] for vcpu in vcpupin_output: vcpupin_affinity[vcpu] = libvirt.cpus_string_to_affinity_list( vcpupin_output[vcpu], host_cpu_count) return vcpupin_affinity
def affinity_from_vcpupin(vm, vcpu=None, options=None): """ Returns dict of vcpu's affinity from virsh vcpupin output :param vm: VM object :param vcpu: virtual cpu to qeury :param options: --live, --current or --config :return: dict of affinity of VM """ vcpupin_output = {} vcpupin_affinity = {} host_cpu_count = utils.total_cpus_count() result = virsh.vcpupin(vm.name, vcpu=vcpu, options=options, debug=True) for vcpu in results_stdout_52lts(result).strip().split('\n')[2:]: # On newer version of libvirt, there is no ':' in # vcpupin output anymore vcpupin_output[int(vcpu.split()[0].rstrip(':'))] = vcpu.split()[1] for vcpu in vcpupin_output: vcpupin_affinity[vcpu] = libvirt.cpus_string_to_affinity_list( vcpupin_output[vcpu], host_cpu_count) return vcpupin_affinity
def affinity_from_xml(vm): """ Returns dict of the vcpu's affinity from guest xml :param vm: VM object :return: dict of affinity of VM """ host_cpu_count = utils.total_cpus_count() xml_affinity_list = [] xml_affinity = {} try: vmxml = libvirt_xml.VMXML.new_from_dumpxml(vm.name) xml_affinity_list = vmxml['cputune'].vcpupins except LibvirtXMLNotFoundError: logging.debug("No <cputune> element find in domain xml") return xml_affinity # Store xml_affinity_list to a dict for vcpu in xml_affinity_list: xml_affinity[vcpu['vcpu']] = libvirt.cpus_string_to_affinity_list(vcpu['cpuset'], host_cpu_count) return xml_affinity
def affinity_from_xml(vm): """ Returns dict of the vcpu's affinity from guest xml :param vm: VM object :return: dict of affinity of VM """ host_cpu_count = utils.total_cpus_count() xml_affinity_list = [] xml_affinity = {} try: vmxml = libvirt_xml.VMXML.new_from_dumpxml(vm.name) xml_affinity_list = vmxml['cputune'].vcpupins except LibvirtXMLNotFoundError: logging.debug("No <cputune> element find in domain xml") return xml_affinity # Store xml_affinity_list to a dict for vcpu in xml_affinity_list: xml_affinity[vcpu['vcpu']] = libvirt.cpus_string_to_affinity_list( vcpu['cpuset'], host_cpu_count) return xml_affinity
def check_affinity(vm, expect_vcpupin): """ Check the affinity of vcpus in various libvirt API output :param vm: VM object :param expect_vcpupin: Expected affinity details :return: True if affinity matches from different virsh API outputs, False if not """ host_cpu_count = utils.total_cpus_count() affinity_xml = affinity_from_xml(vm) affinity_vcpupin = affinity_from_vcpupin(vm) affinity_vcpuinfo = affinity_from_vcpuinfo(vm) result = True for vcpu in list(expect_vcpupin.keys()): expect_affinity = libvirt.cpus_string_to_affinity_list( str(expect_vcpupin[vcpu]), host_cpu_count) # Check for vcpuinfo affinity if affinity_vcpuinfo[int(vcpu)] != expect_affinity: logging.error("CPU affinity in virsh vcpuinfo output" " is unexpected") result = False # Check for vcpupin affinity if affinity_vcpupin[int(vcpu)] != expect_affinity: logging.error("Virsh vcpupin output is unexpected") result = False # Check for affinity in Domain xml if affinity_xml: if affinity_xml[vcpu] != expect_affinity: logging.error("Affinity in domain XML is unexpected") result = False if result: logging.debug("Vcpupin info check pass") return result
def check_affinity(vm, expect_vcpupin): """ Check the affinity of vcpus in various libvirt API output :param vm: VM object :param expect_vcpupin: Expected affinity details :return: True if affinity matches from different virsh API outputs, False if not """ host_cpu_count = utils.total_cpus_count() affinity_xml = affinity_from_xml(vm) affinity_vcpupin = affinity_from_vcpupin(vm) affinity_vcpuinfo = affinity_from_vcpuinfo(vm) result = True for vcpu in list(expect_vcpupin.keys()): expect_affinity = libvirt.cpus_string_to_affinity_list( str(expect_vcpupin[vcpu]), host_cpu_count) # Check for vcpuinfo affinity if affinity_vcpuinfo[int(vcpu)] != expect_affinity: logging.error("CPU affinity in virsh vcpuinfo output" " is unexpected") result = False # Check for vcpupin affinity if affinity_vcpupin[int(vcpu)] != expect_affinity: logging.error("Virsh vcpupin output is unexpected") result = False # Check for affinity in Domain xml if affinity_xml: if affinity_xml[vcpu] != expect_affinity: logging.error("Affinity in domain XML is unexpected") result = False if result: logging.debug("Vcpupin info check pass") return result
# output, and vcpupin info(cputune element) in domain xml result = virsh.vcpupin(vm.name, ignore_status=True, debug=True) libvirt.check_exit_status(result) vcpupin_output = result.stdout.strip().splitlines()[2:] if expect_vcpupin: host_cpu_count = utils.count_cpus() xml_affinity_list = [] xml_affinity = {} try: xml_affinity_list = vmxml['cputune'].vcpupins except LibvirtXMLNotFoundError: logging.debug("No <cputune> element find in domain xml") # Store xml_affinity_list to a dict for vcpu in xml_affinity_list: xml_affinity[vcpu['vcpu']] = "".join( libvirt.cpus_string_to_affinity_list(vcpu['cpuset'], host_cpu_count)) # Check for vcpu in expect_vcpupin.keys(): expect_affinity = "".join( libvirt.cpus_string_to_affinity_list(expect_vcpupin[vcpu], host_cpu_count)) logging.debug("Expect affinity of vcpu %s is: %s", vcpu, expect_affinity) # Vcpuinfo logging.debug("Virsh vcpuinfo shows affinity of vcpu %s is: %s", vcpu, vcpuinfo_affinity[int(vcpu)]) if vcpuinfo_affinity[int(vcpu)] != expect_affinity: raise error.TestFail("CPU affinity in virsh vcpuinfo output" " is unexpected") # Vcpupin vcpupin_affinity = vcpupin_output[int(vcpu)].split(":")[1].strip()
# output, and vcpupin info(cputune element) in domain xml result = virsh.vcpupin(vm.name, ignore_status=True, debug=True) libvirt.check_exit_status(result) vcpupin_output = result.stdout.strip().splitlines()[2:] if expect_vcpupin: host_cpu_count = utils.count_cpus() xml_affinity_list = [] xml_affinity = {} try: xml_affinity_list = vmxml['cputune'].vcpupins except LibvirtXMLNotFoundError: logging.debug("No <cputune> element find in domain xml") # Store xml_affinity_list to a dict for vcpu in xml_affinity_list: xml_affinity[vcpu['vcpu']] = "".join( libvirt.cpus_string_to_affinity_list(vcpu['cpuset'], host_cpu_count)) # Check for vcpu in expect_vcpupin.keys(): if int(vcpu) not in range(len(vcpuinfo_affinity)): logging.error('Expect vcpu %s not exist', vcpu) continue expect_affinity = "".join(libvirt.cpus_string_to_affinity_list( expect_vcpupin[vcpu], host_cpu_count)) logging.debug("Expect affinity of vcpu %s is: %s", vcpu, expect_affinity) # Vcpuinfo logging.debug("Virsh vcpuinfo shows affinity of vcpu %s is: %s", vcpu, vcpuinfo_affinity[int(vcpu)]) if vcpuinfo_affinity[int(vcpu)] != expect_affinity: raise error.TestFail("CPU affinity in virsh vcpuinfo output"