--- cases/sched_params.conf | 120 +++++++++++++++++++++++++++++++ repos/domain/sched_params.py | 136 ++++++++++++++++------------------- repos/domain/sched_params_flag.py | 148 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 330 insertions(+), 74 deletions(-) create mode 100755 cases/sched_params.conf create mode 100644 repos/domain/sched_params_flag.py diff --git a/cases/sched_params.conf b/cases/sched_params.conf new file mode 100755 index 0000000..6f7acce --- /dev/null +++ b/cases/sched_params.conf @@ -0,0 +1,120 @@ +domain:install_linux_cdrom + guestname + $defaultname + guestos + $defaultos + guestarch + $defaultarch + vcpu + $defaultvcpu + memory + $defaultmem + hddriver + $defaulthd + nicdriver + $defaultnic + imageformat + qcow2 + +domain:sched_params + guestname + $defaultname + vcpuquota + 1000 + vcpuperiod + 200000 + emulatorperiod + 300000 + emulatorquota + 4000 + cpushares + 5000 + +domain:sched_params_flag + guestname + $defaultname + vcpuquota + 1001 + vcpuperiod + 200001 + emulatorperiod + 300001 + emulatorquota + 4001 + cpushares + 5001 + flag + current + +domain:sched_params_flag + guestname + $defaultname + vcpuquota + 1002 + vcpuperiod + 200002 + emulatorperiod + 300002 + emulatorquota + 4002 + cpushares + 5002 + flag + live + +domain:sched_params_flag + guestname + $defaultname + vcpuquota + 1003 + vcpuperiod + 200003 + emulatorperiod + 300003 + emulatorquota + 4003 + cpushares + 5003 + flag + config + +domain:destroy + guestname + $defaultname + +domain:sched_params_flag + guestname + $defaultname + vcpuquota + 1004 + vcpuperiod + 200004 + emulatorperiod + 300004 + emulatorquota + 4004 + cpushares + 5004 + flag + current + +domain:sched_params_flag + guestname + $defaultname + vcpuquota + 1005 + vcpuperiod + 200005 + emulatorperiod + 300005 + emulatorquota + 4005 + cpushares + 5005 + flag + config + +domain:undefine + guestname + $defaultname + diff --git a/repos/domain/sched_params.py b/repos/domain/sched_params.py index 786e357..1be5986 100644 --- a/repos/domain/sched_params.py +++ b/repos/domain/sched_params.py @@ -1,95 +1,83 @@ #!/usr/bin/evn python -# To test domain scheduler parameters - -import os -import sys -import time -import commands +# Set and show scheduler parameters import libvirt +from libvirt import libvirtError from src import sharedmod from utils import utils -required_params = ('guestname', 'capshares',) +import os + +required_params = ('guestname', 'vcpuquota', 'vcpuperiod', 'emulatorperiod', \ + 'emulatorquota', 'cpushares',) optional_params = {} -def check_guest_status(domobj): - """Check guest current status""" - state = domobj.info()[0] - if state == libvirt.VIR_DOMAIN_SHUTOFF or state == libvirt.VIR_DOMAIN_SHUTDOWN: - domobj.create() - time.sleep(30) - # Add check function - return True +def check_sched_params(guestname, sched_params_after): + """Check scheduler parameters validity after setting + """ + + if os.path.exists("/cgroup"): + """ Add the judgment method, since the cgroup path is different on + rhel6 and rhel7. + if the folder cgroup is existed, it means the host os is rhel6, + if not existed, it means the the host of is rhel7 + """ + + cgroup_path = "cat /cgroup/cpu/libvirt/qemu/%s/" % guestname else: - return True - -def check_sched_params(*args): - """Check scheduler parameters validity after setting""" - hypervisor, dicts, guestname, domobj = args - sched_dict = {} - if hypervisor == "xen": - sched_dict = eval(commands.getoutput('xm sched-credit -d %s' - % guestname)) - if sched_dict['weight'] == dicts['weight'] and \ - sched_dict['cap'] == dicts['cap']: - return 0 - else: + cgroup_path = "cat /sys/fs/cgroup/cpu\,cpuacct/machine.slice/" \ + "machine-qemu\\\\x2d%s.scope/" % guestname + + sched_dicts = {'vcpu_quota': 'vcpu0/cpu.cfs_quota_us', \ + 'vcpu_period': 'vcpu0/cpu.cfs_period_us', \ + 'emulator_period': 'emulator/cpu.cfs_period_us', \ + 'emulator_quota': 'emulator/cpu.cfs_quota_us', \ + 'cpu_shares': 'cpu.shares'} + + for sched_key in sched_dicts: + cmd = cgroup_path + sched_dicts[sched_key] + status, cmd_value = utils.exec_cmd(cmd, shell=True) + if status: + logger.error("failed to get ***%s*** value" % sched_key) return 1 - if hypervisor == "kvm": - sched_dict = domobj.schedulerParameters() - if sched_dict['cpu_shares'] == dicts['cpu_shares']: - return 0 - else: + sched_dicts[sched_key] = int(cmd_value[0]) + if sched_dicts[sched_key] != sched_params_after[sched_key]: + logger.error("set scheduler parameters failed") return 1 + logger.info("set scheduler parameters success") + return 0 + def sched_params(params): - """Setting scheduler parameters, argument params is a - dictionary data type.which includes 'weight' and 'cap' - keys, by assigning different value to 'weight' and 'cap' - to verify validity of the result + """ Change and get the scheduler parameters """ - hypervisor = utils.get_hypervisor() + global logger logger = params['logger'] guestname = params['guestname'] - conn = sharedmod.libvirtobj['conn'] - - domobj = conn.lookupByName(guestname) - - if check_guest_status(domobj): - sched_params = domobj.schedulerParameters() - logger.info("original scheduler parameters: %s\n" % sched_params) - - if 'xen' in hypervisor: - str_weight = params['weight'] - str_cap = params['cap'] - for wgt in eval(str_weight): - for cap in eval(str_cap): - dicts = {'weight': wgt, 'cap': cap} - logger.info("setting scheduler parameters: %s" % dicts) - domobj.setSchedulerParameters(dicts) - sched_params = domobj.schedulerParameters() - logger.info("current scheduler parameters: %s\n" % sched_params) - - retval = check_sched_params(hypervisor, dicts, - guestname, domobj) - if retval != 0: - return 1 - elif 'kvm' in hypervisor: - cpu_shares = int(params['cpushares']) - dicts = {'cpu_shares': cpu_shares} + dicts = {'vcpu_quota': int(params['vcpuquota']), \ + 'vcpu_period': int(params['vcpuperiod']), \ + 'emulator_period': int(params['emulatorperiod']), \ + 'emulator_quota': int(params['emulatorquota']), \ + 'cpu_shares': int(params['cpushares'])} + + try: + conn = sharedmod.libvirtobj['conn'] + domobj = conn.lookupByName(guestname) + + sched_type = str(domobj.schedulerType()[0]) + logger.info("the scheduler type is: %s" % sched_type) + sched_params_original = domobj.schedulerParameters() + logger.info("original scheduler parameters: %s" % sched_params_original) logger.info("setting scheduler parameters: %s" % dicts) domobj.setSchedulerParameters(dicts) - sched_params = domobj.schedulerParameters() - logger.info("current scheduler parameters: %s\n" % sched_params) - retval = check_sched_params(hypervisor, dicts, - guestname, domobj) - if retval != 0: - return 1 - else: - logger.error("unsupported hypervisor type: %s" % hypervisor) - return 1 + sched_params_after = domobj.schedulerParameters() + logger.info("current scheduler parameters: %s" % sched_params_after) - return 0 + ret = check_sched_params(guestname, sched_params_after) + + return ret + except libvirtError, e: + logger.error("libvirt call failed: " + str(e)) + return 1 diff --git a/repos/domain/sched_params_flag.py b/repos/domain/sched_params_flag.py new file mode 100644 index 0000000..19bcb13 --- /dev/null +++ b/repos/domain/sched_params_flag.py @@ -0,0 +1,148 @@ +#!/usr/bin/evn python +# Set and show scheduler parameters with flag, such as "--current", "--live" +# and "--config" + +import libvirt +from libvirt import libvirtError + +from src import sharedmod +from utils import utils +from xml.dom import minidom +import os + +required_params = ('guestname', 'vcpuquota', 'vcpuperiod', 'emulatorperiod', \ + 'emulatorquota', 'cpushares', 'flag',) +optional_params = {} + +def check_sched_params_flag(guestname, domobj, sched_params_after, domstate, \ + flags_value): + """Check scheduler parameters validity after setting + """ + + if (domstate == 1) and ((flags_value == 0) or (flags_value == 1)): + """While the domain is running and the flag is "--live" or "--current", + the value can be checked with the cgroup value + As for the other condition, the value can be checked with the domain + config xml + """ + + if os.path.exists("/cgroup"): + """ Add the judgment method, since the cgroup path is different on + rhel6 and rhel7. + if the folder cgroup is existed, it means the host os is rhel6, + if not existed, it means the the host of is rhel7 + """ + + cgroup_path = "cat /cgroup/cpu/libvirt/qemu/%s/" % guestname + else: + cgroup_path = "cat /sys/fs/cgroup/cpu\,cpuacct/machine.slice/" \ + "machine-qemu\\\\x2d%s.scope/" % guestname + + sched_dicts = {'vcpu_quota': 'vcpu0/cpu.cfs_quota_us', \ + 'vcpu_period': 'vcpu0/cpu.cfs_period_us', \ + 'emulator_period': 'emulator/cpu.cfs_period_us', \ + 'emulator_quota': 'emulator/cpu.cfs_quota_us', \ + 'cpu_shares': 'cpu.shares'} + + for sched_key in sched_dicts: + cmd = cgroup_path + sched_dicts[sched_key] + status, cmd_value = utils.exec_cmd(cmd, shell=True) + if status: + logger.error("failed to get ***%s*** value" % sched_key) + return 1 + sched_dicts[sched_key] = int(cmd_value[0]) + if sched_dicts[sched_key] != sched_params_after[sched_key]: + logger.error("set scheduler parameters failed") + return 1 + + else: + guestxml = domobj.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE) + logger.debug("domain %s config xml :\n%s" % (domobj.name(), guestxml)) + + xmlrootnode = minidom.parseString(guestxml) + + sched_dicts = {'vcpu_quota': 'quota', 'vcpu_period': 'period', \ + 'emulator_period': 'emulator_period', \ + 'emulator_quota': 'emulator_quota', \ + 'cpu_shares': 'shares'} + + for sched_key in sched_dicts: + node = xmlrootnode.getElementsByTagName(sched_dicts[sched_key])[0] + sched_dicts[sched_key] = int(node.childNodes[0].data) + if sched_dicts[sched_key] != sched_params_after[sched_key]: + logger.error("set scheduler parameters failed") + return 1 + + logger.info("set scheduler parameters success") + return 0 + +def sched_params_flag(params): + """ Change and get the scheduler parameters + """ + + global logger + logger = params['logger'] + guestname = params['guestname'] + dicts = {'vcpu_quota': int(params['vcpuquota']), \ + 'vcpu_period': int(params['vcpuperiod']), \ + 'emulator_period': int(params['emulatorperiod']), \ + 'emulator_quota': int(params['emulatorquota']), \ + 'cpu_shares': int(params['cpushares'])} + flags = params['flag'] + + try: + conn = sharedmod.libvirtobj['conn'] + domobj = conn.lookupByName(guestname) + + domstate = domobj.state(0)[0] + """following is the domain state: + '1' is running status + '5' is shutoff status + please see the following reference link: + http://libvirt.org/html/libvirt-libvirt.html#virDomainState + """ + if domstate == 1: + logger.info("the state of virtual machine is ***running***") + elif domstate == 5: + logger.info("the state of virtual machine is ***shutoff***") + else: + logger.error("the state of virtual machine is not running or " \ + "shutoff now, it is out of the check range of this " \ + "script. Please check the domain status.") + return 1 + + """virDomainModificationImpact + VIR_DOMAIN_AFFECT_CURRENT = 0 + VIR_DOMAIN_AFFECT_LIVE = 1 + VIR_DOMAIN_AFFECT_CONFIG = 2 + """ + + if flags == "current": + flags_value = libvirt.VIR_DOMAIN_AFFECT_CURRENT + elif flags == "live": + flags_value = libvirt.VIR_DOMAIN_AFFECT_LIVE + elif flags == "config": + flags_value = libvirt.VIR_DOMAIN_AFFECT_CONFIG + else: + logger.error("the value of flags is not correct, please check " \ + "the conf file") + return 1 + + sched_type = str(domobj.schedulerType()[0]) + logger.info("the scheduler type is: %s" % sched_type) + sched_params_original = domobj.schedulerParametersFlags(flags_value) + logger.info("original scheduler parameters with flag ***%s***: %s" % \ + (flags, sched_params_original)) + logger.info("setting scheduler parameters: %s" % dicts) + domobj.setSchedulerParametersFlags(dicts, flags_value) + sched_params_after = domobj.schedulerParametersFlags(flags_value) + logger.info("current scheduler parameters with flag ***%s***: %s" % \ + (flags, sched_params_after)) + + ret = check_sched_params_flag(guestname, domobj, sched_params_after, \ + domstate, flags_value) + + return ret + except libvirtError, e: + logger.error("libvirt call failed: " + str(e)) + return 1 -- 1.8.3.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list