[libvirt] [libvirt-test-api][PATCH 1/3] add new test case for getMemoryStats

Signed-off-by: Luyao Huang <lhuang@redhat.com> --- cases/test_connection.conf | 4 ++ repos/virconn/connection_getMemoryStats.py | 96 ++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 repos/virconn/connection_getMemoryStats.py diff --git a/cases/test_connection.conf b/cases/test_connection.conf index 3c08a95..336b1ad 100644 --- a/cases/test_connection.conf +++ b/cases/test_connection.conf @@ -73,3 +73,7 @@ virconn:connection_getCellsFreeMemory virconn:connection_getMemoryParameters conn qemu:///system + +virconn:connection_getMemoryStats + conn + qemu:///system diff --git a/repos/virconn/connection_getMemoryStats.py b/repos/virconn/connection_getMemoryStats.py new file mode 100644 index 0000000..fcc146b --- /dev/null +++ b/repos/virconn/connection_getMemoryStats.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python +import libvirt +from libvirt import libvirtError +from utils import utils + +required_params = () +optional_params = {'conn': ''} + +NODE_ONLINE = '/sys/devices/system/node/online' +MEMINFO = '/proc/meminfo' + +def getsysmem(a): + return open(a[0]).read().splitlines()[a[1]].split()[a[2]] + +def virtgetmem(a): + return a[0].getMemoryStats(a[1])[a[2]] + +def connection_getMemoryStats(params): + """ + test API for getMemoryStats in class virConnect + """ + logger = params['logger'] + fail=0 + + nodeset = utils.file_read(NODE_ONLINE) + logger.info("host exist node is %s" % nodeset) + + node_tuple = utils.param_to_tuple_nolength(nodeset) + if not node_tuple: + logger.info("error in function param_to_tuple_nolength") + return 1 + + try: + conn = libvirt.open(params['conn']) + + logger.info("get connection cells memory status") + for n in range(len(node_tuple)): + if not node_tuple[n]: + continue + + D = utils.get_standard_deviation(getsysmem, virtgetmem, \ + ['/sys/devices/system/node/node%d/meminfo' % n,1,3], [conn,n,'free']) + logger.info("Standard Deviation for free memory in node %d is %d" % (n, D)) + + + """ expectations 177 is a average collected in a x86_64 low load machine""" + if D > 177*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for node %d free memory" % (177*5, n)) + + a1 = ['/sys/devices/system/node/node%d/meminfo' % n, 0, 3] + a2 = [conn,n,'total'] + if long(getsysmem(a1)) != long(virtgetmem(a2)): + fail=1 + logger.info("FAIL: Total memory in node %d is not right" % n) + + + D = utils.get_standard_deviation(getsysmem, virtgetmem, \ + [MEMINFO, 3, 1], [conn, -1, 'buffers']) + logger.info("Standard Deviation for host buffers is %d" % D) + + """ expectations 30 is a average collected in a x86_64 low load machine""" + if D > 30*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host buffers" % 30*5) + + D = utils.get_standard_deviation(getsysmem, virtgetmem, \ + [MEMINFO,4,1], [conn,-1,'cached']) + logger.info("Standard Deviation for host cached is %d" % D) + + """ expectations 32 is a average collected in a x86_64 low load machine""" + if D > 32*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cached" % 32*5) + + D = utils.get_standard_deviation(getsysmem, virtgetmem, \ + [MEMINFO,1,1], [conn,-1,'free']) + logger.info("Standard Deviation for host free memory is %d" % D) + + """ expectations 177 is a average collected in a x86_64 low load machine""" + if D > 177*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host free memory" % 177*5) + + if long(getsysmem([MEMINFO,0,1])) != long(virtgetmem([conn,-1,'total'])): + fail=1 + logger.info("FAIL: Total memory for host is not right" % n) + + except libvirtError, e: + logger.error("API error message: %s" % e.message) + fail=1 + return fail -- 1.8.3.1

Signed-off-by: Luyao Huang <lhuang@redhat.com> --- utils/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/utils.py b/utils/utils.py index 954b2bf..a6e6965 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -906,9 +906,10 @@ def get_standard_deviation(cb1, cb2, opaque1, opaque2, number = 1000): """ D = 0 for i in range(number): - a = cb1(opaque1) + a1 = cb1(opaque1) b = cb2(opaque2) - D += (int(a) - int(b))**2 + a2 = cb1(opaque1) + D += ((int(a1) + int(a2))/2 - int(b))**2 return math.sqrt(D/number) def param_to_tuple_nolength(paramlist): -- 1.8.3.1

On 05/18/2015 09:28 AM, Luyao Huang wrote:
Signed-off-by: Luyao Huang <lhuang@redhat.com> --- utils/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/utils/utils.py b/utils/utils.py index 954b2bf..a6e6965 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -906,9 +906,10 @@ def get_standard_deviation(cb1, cb2, opaque1, opaque2, number = 1000): """ D = 0 for i in range(number): - a = cb1(opaque1) + a1 = cb1(opaque1) b = cb2(opaque2) - D += (int(a) - int(b))**2 + a2 = cb1(opaque1) + D += ((int(a1) + int(a2))/2 - int(b))**2 return math.sqrt(D/number)
ACK, this will spend a double time, whatever, we need a more accurate return.
def param_to_tuple_nolength(paramlist):

Signed-off-by: Luyao Huang <lhuang@redhat.com> --- cases/linux_domain.conf | 6 +++ repos/domain/cpu_status.py | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 repos/domain/cpu_status.py diff --git a/cases/linux_domain.conf b/cases/linux_domain.conf index 0a7d134..9f64226 100644 --- a/cases/linux_domain.conf +++ b/cases/linux_domain.conf @@ -269,6 +269,12 @@ virconn:connection_security_model guestname $defaultname +domain:virDomain_getCPUStats + guestname + $defaultname + conn + qemu:///system + domain:destroy guestname $defaultname diff --git a/repos/domain/cpu_status.py b/repos/domain/cpu_status.py new file mode 100644 index 0000000..6e511c0 --- /dev/null +++ b/repos/domain/cpu_status.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +import libvirt +from libvirt import libvirtError +from utils import utils + +required_params = ('guestname',) +optional_params = {'conn': 'qemu:///system'} + +ONLINE_CPU = '/sys/devices/system/cpu/online' +CGROUP_PERCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage_percpu' +CGROUP_PERVCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/vcpu%d/cpuacct.usage_percpu' +CGROUP_USAGE = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage' +CGROUP_STAT = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.stat' + +def getcputime(a): + return open(a[0]).read().split()[a[1]] + +def virtgetcputime(a): + return a[0].getCPUStats(0)[a[1]][a[2]] + +def getvcputime(a): + ret = 0 + for i in range(int(a[0])): + ret += int(open(CGROUP_PERVCPU % (a[1], i)).read().split()[a[2]]) + + return ret + +def virtgettotalcputime(a): + return a[0].getCPUStats(1)[0][a[1]] + +def virtgettotalcputime2(a): + return a[0].getCPUStats(1)[0][a[1]]/10000000 + +def cpu_status(params): + """ + test API for getCPUStats in class virDomain + """ + logger = params['logger'] + fail=0 + + cpu = utils.file_read(ONLINE_CPU) + logger.info("host online cpulist is %s" % cpu) + + cpu_tuple = utils.param_to_tuple_nolength(cpu) + if not cpu_tuple: + logger.info("error in function param_to_tuple_nolength") + return 1 + + try: + conn = libvirt.open(params['conn']) + + logger.info("get connection to libvirtd") + guest = params['guestname'] + vm = conn.lookupByName(guest) + vcpus = vm.info()[3] + for n in range(len(cpu_tuple)): + if not cpu_tuple[n]: + continue + + D = utils.get_standard_deviation(getcputime, virtgetcputime, \ + [CGROUP_PERCPU % guest, n], [vm,n,'cpu_time']) + logger.info("Standard Deviation for host cpu %d cputime is %d" % (n, D)) + + """ expectations 403423 is a average collected in a x86_64 low load machine""" + if D > 403423*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu %d" % (403423*5, n)) + + D = utils.get_standard_deviation(getvcputime, virtgetcputime, \ + [vcpus, guest, n], [vm,n,'vcpu_time']) + logger.info("Standard Deviation for host cpu %d vcputime is %d" % (n, D)) + + """ expectations 4034 is a average collected in a x86_64 low load machine""" + if D > 4034*5*vcpus: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (4034*5*vcpus, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime, \ + [CGROUP_USAGE % guest, 0], [vm,'cpu_time']) + logger.info("Standard Deviation for host cpu total cputime is %d" % D) + + """ expectations 313451 is a average collected in a x86_64 low load machine""" + if D > 313451*5*len(cpu_tuple): + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (313451*5*len(cpu_tuple), n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 3], [vm,'system_time']) + logger.info("Standard Deviation for host cpu total system time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host system cpu time %d" % (10*5, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 1], [vm,'user_time']) + logger.info("Standard Deviation for host cpu total user time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host user cpu time %d" % (10*5, n)) + + except libvirtError, e: + logger.error("API error message: %s" % e.message) + fail=1 + return fail -- 1.8.3.1

ACK and pushed Don't forget to remove trailing whitespace next time. Thanks Hongming On 05/18/2015 09:28 AM, Luyao Huang wrote:
Signed-off-by: Luyao Huang <lhuang@redhat.com> --- cases/linux_domain.conf | 6 +++ repos/domain/cpu_status.py | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 repos/domain/cpu_status.py
diff --git a/cases/linux_domain.conf b/cases/linux_domain.conf index 0a7d134..9f64226 100644 --- a/cases/linux_domain.conf +++ b/cases/linux_domain.conf @@ -269,6 +269,12 @@ virconn:connection_security_model guestname $defaultname
+domain:virDomain_getCPUStats + guestname + $defaultname + conn + qemu:///system + domain:destroy guestname $defaultname diff --git a/repos/domain/cpu_status.py b/repos/domain/cpu_status.py new file mode 100644 index 0000000..6e511c0 --- /dev/null +++ b/repos/domain/cpu_status.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +import libvirt +from libvirt import libvirtError +from utils import utils + +required_params = ('guestname',) +optional_params = {'conn': 'qemu:///system'} + +ONLINE_CPU = '/sys/devices/system/cpu/online' +CGROUP_PERCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage_percpu' +CGROUP_PERVCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/vcpu%d/cpuacct.usage_percpu' +CGROUP_USAGE = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage' +CGROUP_STAT = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.stat' + +def getcputime(a): + return open(a[0]).read().split()[a[1]] + +def virtgetcputime(a): + return a[0].getCPUStats(0)[a[1]][a[2]] + +def getvcputime(a): + ret = 0 + for i in range(int(a[0])): + ret += int(open(CGROUP_PERVCPU % (a[1], i)).read().split()[a[2]]) + + return ret + +def virtgettotalcputime(a): + return a[0].getCPUStats(1)[0][a[1]] + +def virtgettotalcputime2(a): + return a[0].getCPUStats(1)[0][a[1]]/10000000 + +def cpu_status(params): + """ + test API for getCPUStats in class virDomain + """ + logger = params['logger'] + fail=0 + + cpu = utils.file_read(ONLINE_CPU) + logger.info("host online cpulist is %s" % cpu) + + cpu_tuple = utils.param_to_tuple_nolength(cpu) + if not cpu_tuple: + logger.info("error in function param_to_tuple_nolength") + return 1 + + try: + conn = libvirt.open(params['conn']) + + logger.info("get connection to libvirtd") + guest = params['guestname'] + vm = conn.lookupByName(guest) + vcpus = vm.info()[3] + for n in range(len(cpu_tuple)): + if not cpu_tuple[n]: + continue + + D = utils.get_standard_deviation(getcputime, virtgetcputime, \ + [CGROUP_PERCPU % guest, n], [vm,n,'cpu_time']) + logger.info("Standard Deviation for host cpu %d cputime is %d" % (n, D)) + + """ expectations 403423 is a average collected in a x86_64 low load machine""" + if D > 403423*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu %d" % (403423*5, n)) + + D = utils.get_standard_deviation(getvcputime, virtgetcputime, \ + [vcpus, guest, n], [vm,n,'vcpu_time']) + logger.info("Standard Deviation for host cpu %d vcputime is %d" % (n, D)) + + """ expectations 4034 is a average collected in a x86_64 low load machine""" + if D > 4034*5*vcpus: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (4034*5*vcpus, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime, \ + [CGROUP_USAGE % guest, 0], [vm,'cpu_time']) + logger.info("Standard Deviation for host cpu total cputime is %d" % D) + + """ expectations 313451 is a average collected in a x86_64 low load machine""" + if D > 313451*5*len(cpu_tuple): + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (313451*5*len(cpu_tuple), n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 3], [vm,'system_time']) + logger.info("Standard Deviation for host cpu total system time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host system cpu time %d" % (10*5, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 1], [vm,'user_time']) + logger.info("Standard Deviation for host cpu total user time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host user cpu time %d" % (10*5, n)) + + except libvirtError, e: + logger.error("API error message: %s" % e.message) + fail=1 + return fail

On 06/02/2015 07:57 AM, hongming wrote:
ACK and pushed Don't forget to remove trailing whitespace next time. Thanks
Hongming On 05/18/2015 09:28 AM, Luyao Huang wrote:
Signed-off-by: Luyao Huang <lhuang@redhat.com> --- cases/linux_domain.conf | 6 +++ repos/domain/cpu_status.py | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 repos/domain/cpu_status.py
diff --git a/cases/linux_domain.conf b/cases/linux_domain.conf index 0a7d134..9f64226 100644 --- a/cases/linux_domain.conf +++ b/cases/linux_domain.conf @@ -269,6 +269,12 @@ virconn:connection_security_model guestname $defaultname +domain:virDomain_getCPUStats
The case name is wrong , I have fixed it directly commit 5c433b7c934cd8ac3ad783939c5906b7cbc129a8 Author: Hongming Zhang <honzhang@redhat.com> Date: Mon Aug 24 11:25:04 2015 +0800 Fix a wrong case name in linux_domain.conf Modify the virDomain_getCPUStats as cpu_status modified: cases/linux_domain.conf
+ guestname + $defaultname + conn + qemu:///system + domain:destroy guestname $defaultname diff --git a/repos/domain/cpu_status.py b/repos/domain/cpu_status.py new file mode 100644 index 0000000..6e511c0 --- /dev/null +++ b/repos/domain/cpu_status.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +import libvirt +from libvirt import libvirtError +from utils import utils + +required_params = ('guestname',) +optional_params = {'conn': 'qemu:///system'} + +ONLINE_CPU = '/sys/devices/system/cpu/online' +CGROUP_PERCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage_percpu' +CGROUP_PERVCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/vcpu%d/cpuacct.usage_percpu' +CGROUP_USAGE = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage' +CGROUP_STAT = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.stat' + +def getcputime(a): + return open(a[0]).read().split()[a[1]] + +def virtgetcputime(a): + return a[0].getCPUStats(0)[a[1]][a[2]] + +def getvcputime(a): + ret = 0 + for i in range(int(a[0])): + ret += int(open(CGROUP_PERVCPU % (a[1], i)).read().split()[a[2]]) + + return ret + +def virtgettotalcputime(a): + return a[0].getCPUStats(1)[0][a[1]] + +def virtgettotalcputime2(a): + return a[0].getCPUStats(1)[0][a[1]]/10000000 + +def cpu_status(params): + """ + test API for getCPUStats in class virDomain + """ + logger = params['logger'] + fail=0 + + cpu = utils.file_read(ONLINE_CPU) + logger.info("host online cpulist is %s" % cpu) + + cpu_tuple = utils.param_to_tuple_nolength(cpu) + if not cpu_tuple: + logger.info("error in function param_to_tuple_nolength") + return 1 + + try: + conn = libvirt.open(params['conn']) + + logger.info("get connection to libvirtd") + guest = params['guestname'] + vm = conn.lookupByName(guest) + vcpus = vm.info()[3] + for n in range(len(cpu_tuple)): + if not cpu_tuple[n]: + continue + + D = utils.get_standard_deviation(getcputime, virtgetcputime, \ + [CGROUP_PERCPU % guest, n], [vm,n,'cpu_time']) + logger.info("Standard Deviation for host cpu %d cputime is %d" % (n, D)) + + """ expectations 403423 is a average collected in a x86_64 low load machine""" + if D > 403423*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu %d" % (403423*5, n)) + + D = utils.get_standard_deviation(getvcputime, virtgetcputime, \ + [vcpus, guest, n], [vm,n,'vcpu_time']) + logger.info("Standard Deviation for host cpu %d vcputime is %d" % (n, D)) + + """ expectations 4034 is a average collected in a x86_64 low load machine""" + if D > 4034*5*vcpus: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (4034*5*vcpus, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime, \ + [CGROUP_USAGE % guest, 0], [vm,'cpu_time']) + logger.info("Standard Deviation for host cpu total cputime is %d" % D) + + """ expectations 313451 is a average collected in a x86_64 low load machine""" + if D > 313451*5*len(cpu_tuple): + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (313451*5*len(cpu_tuple), n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 3], [vm,'system_time']) + logger.info("Standard Deviation for host cpu total system time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host system cpu time %d" % (10*5, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 1], [vm,'user_time']) + logger.info("Standard Deviation for host cpu total user time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host user cpu time %d" % (10*5, n)) + + except libvirtError, e: + logger.error("API error message: %s" % e.message) + fail=1 + return fail
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

On 08/24/2015 11:41 AM, hongming wrote:
On 06/02/2015 07:57 AM, hongming wrote:
ACK and pushed Don't forget to remove trailing whitespace next time. Thanks
Hongming On 05/18/2015 09:28 AM, Luyao Huang wrote:
Signed-off-by: Luyao Huang <lhuang@redhat.com> --- cases/linux_domain.conf | 6 +++ repos/domain/cpu_status.py | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 repos/domain/cpu_status.py
diff --git a/cases/linux_domain.conf b/cases/linux_domain.conf index 0a7d134..9f64226 100644 --- a/cases/linux_domain.conf +++ b/cases/linux_domain.conf @@ -269,6 +269,12 @@ virconn:connection_security_model guestname $defaultname +domain:virDomain_getCPUStats
The case name is wrong , I have fixed it directly
commit 5c433b7c934cd8ac3ad783939c5906b7cbc129a8 Author: Hongming Zhang <honzhang@redhat.com> Date: Mon Aug 24 11:25:04 2015 +0800
Fix a wrong case name in linux_domain.conf
Modify the virDomain_getCPUStats as cpu_status modified: cases/linux_domain.conf
Thanks, i must missed this place during change the name of this api. Luyao
+ guestname + $defaultname + conn + qemu:///system + domain:destroy guestname $defaultname diff --git a/repos/domain/cpu_status.py b/repos/domain/cpu_status.py new file mode 100644 index 0000000..6e511c0 --- /dev/null +++ b/repos/domain/cpu_status.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +import libvirt +from libvirt import libvirtError +from utils import utils + +required_params = ('guestname',) +optional_params = {'conn': 'qemu:///system'} + +ONLINE_CPU = '/sys/devices/system/cpu/online' +CGROUP_PERCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage_percpu' +CGROUP_PERVCPU = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/vcpu%d/cpuacct.usage_percpu' +CGROUP_USAGE = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.usage' +CGROUP_STAT = '/sys/fs/cgroup/cpuacct/machine.slice/machine-qemu\\x2d%s.scope/cpuacct.stat' + +def getcputime(a): + return open(a[0]).read().split()[a[1]] + +def virtgetcputime(a): + return a[0].getCPUStats(0)[a[1]][a[2]] + +def getvcputime(a): + ret = 0 + for i in range(int(a[0])): + ret += int(open(CGROUP_PERVCPU % (a[1], i)).read().split()[a[2]]) + + return ret + +def virtgettotalcputime(a): + return a[0].getCPUStats(1)[0][a[1]] + +def virtgettotalcputime2(a): + return a[0].getCPUStats(1)[0][a[1]]/10000000 + +def cpu_status(params): + """ + test API for getCPUStats in class virDomain + """ + logger = params['logger'] + fail=0 + + cpu = utils.file_read(ONLINE_CPU) + logger.info("host online cpulist is %s" % cpu) + + cpu_tuple = utils.param_to_tuple_nolength(cpu) + if not cpu_tuple: + logger.info("error in function param_to_tuple_nolength") + return 1 + + try: + conn = libvirt.open(params['conn']) + + logger.info("get connection to libvirtd") + guest = params['guestname'] + vm = conn.lookupByName(guest) + vcpus = vm.info()[3] + for n in range(len(cpu_tuple)): + if not cpu_tuple[n]: + continue + + D = utils.get_standard_deviation(getcputime, virtgetcputime, \ + [CGROUP_PERCPU % guest, n], [vm,n,'cpu_time']) + logger.info("Standard Deviation for host cpu %d cputime is %d" % (n, D)) + + """ expectations 403423 is a average collected in a x86_64 low load machine""" + if D > 403423*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu %d" % (403423*5, n)) + + D = utils.get_standard_deviation(getvcputime, virtgetcputime, \ + [vcpus, guest, n], [vm,n,'vcpu_time']) + logger.info("Standard Deviation for host cpu %d vcputime is %d" % (n, D)) + + """ expectations 4034 is a average collected in a x86_64 low load machine""" + if D > 4034*5*vcpus: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (4034*5*vcpus, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime, \ + [CGROUP_USAGE % guest, 0], [vm,'cpu_time']) + logger.info("Standard Deviation for host cpu total cputime is %d" % D) + + """ expectations 313451 is a average collected in a x86_64 low load machine""" + if D > 313451*5*len(cpu_tuple): + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host cpu time %d" % (313451*5*len(cpu_tuple), n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 3], [vm,'system_time']) + logger.info("Standard Deviation for host cpu total system time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host system cpu time %d" % (10*5, n)) + + D = utils.get_standard_deviation(getcputime, virtgettotalcputime2, \ + [CGROUP_STAT % guest, 1], [vm,'user_time']) + logger.info("Standard Deviation for host cpu total user time is %d" % D) + + """ expectations 10 is a average collected in a x86_64 low load machine""" + if D > 10*5: + fail=1 + logger.info("FAIL: Standard Deviation is too big \ + (biger than %d) for host user cpu time %d" % (10*5, n)) + + except libvirtError, e: + logger.error("API error message: %s" % e.message) + fail=1 + return fail
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (4)
-
hongming
-
jiahu
-
lhuang
-
Luyao Huang