From 7f39e2e5e56ff1c415eac33489801e173349c420 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Fri, 6 Mar 2009 14:44:04 +0000 Subject: [PATCH] * src/cgroup.c src/cgroup.h src/lxc_driver.c: allows to get CPU usage of a lxc using cpuacct subsystem of cgroups, patch by Ryota Ozaki Daniel --- ChangeLog | 6 ++++++ src/cgroup.c | 6 ++++++ src/cgroup.h | 2 ++ src/lxc_driver.c | 16 ++++++++++++++-- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index f06074e325..94f1cad022 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Mar 6 15:42:46 CET 2009 Daniel Veillard + + * src/cgroup.c src/cgroup.h src/lxc_driver.c: allows to get + CPU usage of a lxc using cpuacct subsystem of cgroups, patch + by Ryota Ozaki + Wed Mar 4 14:11:15 CET 2009 Daniel Veillard * NEWS configure.in libvirt.spec.in doc/* include/libvirt/libvirt.h: diff --git a/src/cgroup.c b/src/cgroup.c index 8bd6e87309..5af44bd93e 100644 --- a/src/cgroup.c +++ b/src/cgroup.c @@ -38,6 +38,7 @@ struct virCgroup { const char *supported_controllers[] = { "memory", "devices", + "cpuacct", NULL }; @@ -797,3 +798,8 @@ int virCgroupGetCpuShares(virCgroupPtr group, unsigned long *shares) { return virCgroupGetValueU64(group, "cpu.shares", (uint64_t *)shares); } + +int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage) +{ + return virCgroupGetValueU64(group, "cpuacct.usage", (uint64_t *)usage); +} diff --git a/src/cgroup.h b/src/cgroup.h index db68bace76..11c44f98e8 100644 --- a/src/cgroup.h +++ b/src/cgroup.h @@ -42,6 +42,8 @@ int virCgroupAllowDeviceMajor(virCgroupPtr group, int virCgroupSetCpuShares(virCgroupPtr group, unsigned long shares); int virCgroupGetCpuShares(virCgroupPtr group, unsigned long *shares); +int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage); + int virCgroupRemove(virCgroupPtr group); void virCgroupFree(virCgroupPtr *group); diff --git a/src/lxc_driver.c b/src/lxc_driver.c index dff05b67a1..c08ca9cdef 100644 --- a/src/lxc_driver.c +++ b/src/lxc_driver.c @@ -362,6 +362,7 @@ static int lxcDomainGetInfo(virDomainPtr dom, { lxc_driver_t *driver = dom->conn->privateData; virDomainObjPtr vm; + virCgroupPtr cgroup = NULL; int ret = -1; lxcDriverLock(driver); @@ -376,10 +377,19 @@ static int lxcDomainGetInfo(virDomainPtr dom, info->state = vm->state; - if (!virDomainIsActive(vm)) { + if (!virDomainIsActive(vm) || virCgroupHaveSupport() != 0) { info->cpuTime = 0; } else { - info->cpuTime = 0; + if (virCgroupForDomain(vm->def, "lxc", &cgroup) != 0) { + lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR, + _("Unable to get cgroup for %s\n"), vm->def->name); + goto cleanup; + } + + if (virCgroupGetCpuacctUsage(cgroup, &(info->cpuTime)) < 0) { + lxcError(dom->conn, dom, VIR_ERR_OPERATION_FAILED, ("cannot read cputime for domain")); + goto cleanup; + } } info->maxMem = vm->def->maxmem; @@ -388,6 +398,8 @@ static int lxcDomainGetInfo(virDomainPtr dom, ret = 0; cleanup: + if (cgroup) + virCgroupFree(&cgroup); if (vm) virDomainObjUnlock(vm); return ret; -- GitLab