def vcpupin_live(params): """pin domain vcpu to host cpu with live flag """ global logger logger = params['logger'] params.pop('logger') guestname = params['guestname'] vcpu = int(params['vcpu']) cpulist = params['cpulist'] logger.info("the name of virtual machine is %s" % guestname) logger.info("the given vcpu is %s" % vcpu) logger.info("the given cpulist is %s" % cpulist) global maxcpu maxcpu = utils.get_host_cpus() logger.info("%s physical cpu on host" % maxcpu) conn = sharedmod.libvirtobj['conn'] try: domobj = conn.lookupByName(guestname) cpumap = utils.param_to_tuple(cpulist, maxcpu) if not cpumap: logger.error("cpulist: Invalid format") return 1 logger.debug("cpumap for vcpu pin is:") logger.debug(cpumap) logger.info("pin domain vcpu %s to host cpu %s with flag: %s" % (vcpu, cpulist, libvirt.VIR_DOMAIN_AFFECT_LIVE)) domobj.pinVcpuFlags(vcpu, cpumap, libvirt.VIR_DOMAIN_AFFECT_LIVE) logger.info("check vcpus info") ret = domobj.vcpus() logger.debug("vcpus info is:") logger.debug(ret) if ret[1][vcpu] == cpumap: logger.info("vcpus info is expected") else: logger.error("vcpus info is not expected") return 1 except libvirtError as e: logger.error("libvirt call failed: " + str(e)) return 1 logger.info("check vcpu pin status on host") ret = vcpupin_check(guestname, vcpu, cpulist) if ret: logger.error("domain vcpu pin failed") return 1 else: logger.info("domain vcpu pin succeed") return 0
def vcpupin_config(params): """pin domain vcpu to host cpu with config flag """ global logger logger = params['logger'] params.pop('logger') guestname = params['guestname'] vcpu = int(params['vcpu']) cpulist = params['cpulist'] logger.info("the name of virtual machine is %s" % guestname) logger.info("the given vcpu is %s" % vcpu) logger.info("the given cpulist is %s" % cpulist) global maxcpu maxcpu = utils.get_host_cpus() logger.info("%s physical cpu on host" % maxcpu) conn = sharedmod.libvirtobj['conn'] try: domobj = conn.lookupByName(guestname) cpumap = utils.param_to_tuple(cpulist, maxcpu) if not cpumap: logger.error("cpulist: Invalid format") return 1 logger.debug("cpumap for vcpu pin is:") logger.debug(cpumap) logger.info("pin domain vcpu %s to host cpulist %s with flag: %s" % (vcpu, cpulist, libvirt.VIR_DOMAIN_AFFECT_CONFIG)) domobj.pinVcpuFlags(vcpu, cpumap, libvirt.VIR_DOMAIN_AFFECT_CONFIG) logger.info("check vcpu pin info") ret = domobj.vcpuPinInfo(libvirt.VIR_DOMAIN_AFFECT_CONFIG) logger.debug("vcpu pin info is:") logger.debug(ret) if ret[vcpu] == cpumap: logger.info("vcpu pin info is expected") else: logger.error("vcpu pin info is not expected") return 1 except libvirtError as e: logger.error("libvirt call failed: " + str(e)) return 1 logger.info("check domain vcpupin configuration in xml") ret = vcpupin_check(domobj, vcpu, cpumap) if ret: logger.error("domain vcpu pin check failed") return 1 else: logger.info("domain vcpu pin check succeed") return 0
def pinemulator(params): """Dynamically change the real CPUs which can be allocated to the emulator process of a domain. This function requires privileged access to the hypervisor. """ global logger logger = params['logger'] guestname = params['guestname'] cpulist = params['cpulist'] logger.info("the name of virtual machine is %s" % guestname) logger.info("the given cpulist is %s" % cpulist) maxcpu = utils.get_host_cpus() logger.info("%s physical cpu on host" % maxcpu) cpumap = utils.param_to_tuple(cpulist, maxcpu) if not cpumap: logger.error("cpulist: Invalid format") return 1 conn = sharedmod.libvirtobj['conn'] try: domobj = conn.lookupByName(guestname) pininfo_original = str(domobj.emulatorPinInfo()) logger.info("the original emulator pin of the domain is: %s" % pininfo_original) logger.info("pin domain emulator to host cpu %s" % cpulist) domobj.pinEmulator(cpumap) pininfo_after = str(domobj.emulatorPinInfo()) logger.info("the revised emulator pin of the domain is: %s" % pininfo_after) ret = check_pinemulator(guestname, maxcpu, pininfo_after) return ret except libvirtError as e: logger.error("libvirt call failed: " + str(e)) return 1
def cpu_affinity(params): """set vcpu of virtual machine to value of parameter vcpu call libvirt API function to set cpu affinity check the result after cpupin """ global logger logger = params['logger'] params.pop('logger') domain_name = params['guestname'] vcpu = params['vcpu'] logger.info("the name of virtual machine is %s" % domain_name) logger.info("the vcpu given is %s" % vcpu) conn = sharedmod.libvirtobj['conn'] uri = conn.getURI() hypervisor = uri.split(':')[0] # Get cpu affinity guest_names = [] ids = conn.listDomainsID() for id in ids: obj = conn.lookupByID(id) guest_names.append(obj.name()) if domain_name not in guest_names: logger.error("guest %s doesn't exist or not be running." % domain_name) return 1 domobj = conn.lookupByName(domain_name) vcpunum = utils.get_num_vcpus(domain_name) logger.info("the current vcpu number of guest %s is %s" % \ (domain_name, vcpunum)) if vcpunum != vcpu: logger.info("set the vcpu of the guest to %s" % vcpu) ret = set_vcpus(domobj, domain_name, vcpu) if ret != 0: return 1 vcpunum_after_set = utils.get_num_vcpus(domain_name) logger.info("after setting, the current vcpu number the guest is %s" % \ vcpunum_after_set) vcpu_list = range(int(vcpunum_after_set)) physical_cpu_num = utils.get_host_cpus() logger.info("in the host, we have %s physical cpu" % physical_cpu_num) cpu_affinity = () for i in range(physical_cpu_num): cpu_affinity = cpu_affinity + (False,) retflag = 0 for i in range(physical_cpu_num): cpu_affinity_test = () for affinity_num in range(len(cpu_affinity)): if affinity_num == i: cpu_affinity_test = cpu_affinity_test + (True,) else: cpu_affinity_test = cpu_affinity_test + \ (cpu_affinity[affinity_num],) logger.debug("the data for testing is") logger.debug(cpu_affinity_test) for vcpu_pinned in vcpu_list: try: logger.info("Now, we pin vcpu %s to physical vcpu %s" % (vcpu_pinned, i)) shell_cmd = "virsh vcpuinfo %s" % domain_name text = commands.getstatusoutput(shell_cmd)[1] logger.debug("before pinning, the vcpu status is %s" % text) domobj.pinVcpu(vcpu_pinned, cpu_affinity_test) except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) logger.error("fail to vcpupin domain") return 1 ret = vcpu_affinity_check(domain_name, vcpu_pinned, i, hypervisor) retflag = retflag + ret if ret: logger.error("vcpu affinity checking failed.") else: logger.info("vcpu affinity checking successed.")
def cpu_affinity(params): """set vcpu of virtual machine to value of parameter vcpu call libvirt API function to set cpu affinity check the result after cpupin """ global logger logger = params['logger'] params.pop('logger') domain_name = params['guestname'] vcpu = params['vcpu'] logger.info("the name of virtual machine is %s" % domain_name) logger.info("the vcpu given is %s" % vcpu) conn = sharedmod.libvirtobj['conn'] uri = conn.getURI() hypervisor = uri.split(':')[0] # Get cpu affinity guest_names = [] ids = conn.listDomainsID() for id in ids: obj = conn.lookupByID(id) guest_names.append(obj.name()) if domain_name not in guest_names: logger.error("guest %s doesn't exist or not be running." % domain_name) return 1 domobj = conn.lookupByName(domain_name) vcpunum = utils.get_num_vcpus(domain_name) logger.info("the current vcpu number of guest %s is %s" % \ (domain_name, vcpunum)) if vcpunum != vcpu: logger.info("set the vcpu of the guest to %s" % vcpu) ret = set_vcpus(domobj, domain_name, vcpu) if ret != 0: return 1 vcpunum_after_set = utils.get_num_vcpus(domain_name) logger.info("after setting, the current vcpu number the guest is %s" % \ vcpunum_after_set) vcpu_list = range(int(vcpunum_after_set)) physical_cpu_num = utils.get_host_cpus() logger.info("in the host, we have %s physical cpu" % physical_cpu_num) cpu_affinity = () for i in range(physical_cpu_num): cpu_affinity = cpu_affinity + (False, ) retflag = 0 for i in range(physical_cpu_num): cpu_affinity_test = () for affinity_num in range(len(cpu_affinity)): if affinity_num == i: cpu_affinity_test = cpu_affinity_test + (True, ) else: cpu_affinity_test = cpu_affinity_test + \ (cpu_affinity[affinity_num],) logger.debug("the data for testing is") logger.debug(cpu_affinity_test) for vcpu_pinned in vcpu_list: try: logger.info("Now, we pin vcpu %s to physical vcpu %s" % (vcpu_pinned, i)) shell_cmd = "virsh vcpuinfo %s" % domain_name text = commands.getstatusoutput(shell_cmd)[1] logger.debug("before pinning, the vcpu status is %s" % text) domobj.pinVcpu(vcpu_pinned, cpu_affinity_test) except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) logger.error("fail to vcpupin domain") return 1 ret = vcpu_affinity_check(domain_name, vcpu_pinned, i, hypervisor) retflag = retflag + ret if ret: logger.error("vcpu affinity checking failed.") else: logger.info("vcpu affinity checking successed.")
def get_cpus(logger): """get nodeinfo cpus """ output = utils.get_host_cpus() logger.info("cpus is %s" % output) return output
def pin_iothread(params): """ test API for pinIOThread in class virDomain """ global logger logger = params["logger"] fail = 0 try: conn = libvirt.open(params["conn"]) logger.info("get connection to libvirtd") guest = params["guestname"] vm = conn.lookupByName(guest) hostcpu = utils.get_host_cpus() tu_cpu = () logger.info("test guest name: %s" % guest) for i in range(hostcpu): if i % 2 == 0: tu_cpu += (1,) else: tu_cpu += (0,) """ test effect a running guest""" if vm.isActive() == 1: logger.info("guest is running test with running guest") if not find_iothreadid_fromxml(vm, 1, 1): logger.info("add iothread %d to running guest" % 1) vm.addIOThread(i, libvirt.VIR_DOMAIN_AFFECT_LIVE) vm.pinIOThread(1, tu_cpu, libvirt.VIR_DOMAIN_AFFECT_LIVE) cpuset = find_iothreadpin_fromxml(vm, 1, 1) if cpuset: if not check_iothreadpin(vm, 1, cpuset): fail = 1 tmp_cpuset = utils.param_to_tuple(cpuset, hostcpu) if not tmp_cpuset: fail = 1 elif tmp_cpuset != tu_cpu: logger.info("FAIL: the cpuset in xml is not equal the cpuset we set") fail = 1 else: logger.info("FAIL: cannot find iothreadpin in XML") fail = 1 """ test effect guest config""" logger.info("test with guest inactive XML") if not find_iothreadid_fromxml(vm, 0, 1): logger.info("add iothread 1 to guest config") vm.addIOThread(1, libvirt.VIR_DOMAIN_AFFECT_CONFIG) vm.pinIOThread(1, tu_cpu, libvirt.VIR_DOMAIN_AFFECT_LIVE) cpuset = find_iothreadpin_fromxml(vm, 1, 1) if cpuset: tmp_cpuset = utils.param_to_tuple(cpuset, hostcpu) if not tmp_cpuset: fail = 1 elif tmp_cpuset != tu_cpu: logger.info("FAIL: the cpuset in xml is not equal the cpuset we set") fail = 1 else: logger.info("FAIL: cannot find iothreadpin in XML") fail = 1 except libvirtError, e: logger.error("API error message: %s" % e.message) fail = 1
def pin_iothread(params): """ test API for pinIOThread in class virDomain """ global logger logger = params['logger'] fail = 0 try: conn = libvirt.open(params['conn']) logger.info("get connection to libvirtd") guest = params['guestname'] vm = conn.lookupByName(guest) hostcpu = utils.get_host_cpus() tu_cpu = () logger.info("test guest name: %s" % guest) for i in range(hostcpu): if i % 2 == 0: tu_cpu += (1, ) else: tu_cpu += (0, ) """ test effect a running guest""" if vm.isActive() == 1: logger.info("guest is running test with running guest") if not find_iothreadid_fromxml(vm, 1, 1): logger.info("add iothread %d to running guest" % 1) vm.addIOThread(i, libvirt.VIR_DOMAIN_AFFECT_LIVE) vm.pinIOThread(1, tu_cpu, libvirt.VIR_DOMAIN_AFFECT_LIVE) cpuset = find_iothreadpin_fromxml(vm, 1, 1) if cpuset: if not check_iothreadpin(vm, 1, cpuset): fail = 1 tmp_cpuset = utils.param_to_tuple(cpuset, hostcpu) if not tmp_cpuset: fail = 1 elif tmp_cpuset != tu_cpu: logger.info( "FAIL: the cpuset in xml is not equal the cpuset we set" ) fail = 1 else: logger.info("FAIL: cannot find iothreadpin in XML") fail = 1 """ test effect guest config""" logger.info("test with guest inactive XML") if not find_iothreadid_fromxml(vm, 0, 1): logger.info("add iothread 1 to guest config") vm.addIOThread(1, libvirt.VIR_DOMAIN_AFFECT_CONFIG) vm.pinIOThread(1, tu_cpu, libvirt.VIR_DOMAIN_AFFECT_LIVE) cpuset = find_iothreadpin_fromxml(vm, 1, 1) if cpuset: tmp_cpuset = utils.param_to_tuple(cpuset, hostcpu) if not tmp_cpuset: fail = 1 elif tmp_cpuset != tu_cpu: logger.info( "FAIL: the cpuset in xml is not equal the cpuset we set") fail = 1 else: logger.info("FAIL: cannot find iothreadpin in XML") fail = 1 except libvirtError, e: logger.error("API error message: %s" % e.message) fail = 1