From 97f3e1cef9707ac7b1e744aef51880d4538e286a Mon Sep 17 00:00:00 2001 Message-Id: <97f3e1cef9707ac7b1e744aef51880d4538e286a@dist-git> From: Pavel Hrdina Date: Mon, 1 Jul 2019 17:07:01 +0200 Subject: [PATCH] vircgroup: extract virCgroupV1GetCpuacctStat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Fabiano Fidêncio Reviewed-by: Ján Tomko Signed-off-by: Pavel Hrdina (cherry picked from commit e294615f9d849ee4923621db2224fce713018458) Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297 Signed-off-by: Pavel Hrdina Message-Id: <06921474355374c68d49725851f915e9bf44feeb.1561993100.git.phrdina@redhat.com> Reviewed-by: Ján Tomko --- src/util/vircgroup.c | 39 +----------------------------- src/util/vircgroupbackend.h | 6 +++++ src/util/vircgroupv1.c | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 4f791204d3..090fe140bb 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -2596,44 +2596,7 @@ int virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user, unsigned long long *sys) { - VIR_AUTOFREE(char *) str = NULL; - char *p; - static double scale = -1.0; - - if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT, - "cpuacct.stat", &str) < 0) - return -1; - - if (!(p = STRSKIP(str, "user ")) || - virStrToLong_ull(p, &p, 10, user) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Cannot parse user stat '%s'"), - p); - return -1; - } - if (!(p = STRSKIP(p, "\nsystem ")) || - virStrToLong_ull(p, NULL, 10, sys) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Cannot parse sys stat '%s'"), - p); - return -1; - } - /* times reported are in system ticks (generally 100 Hz), but that - * rate can theoretically vary between machines. Scale things - * into approximate nanoseconds. */ - if (scale < 0) { - long ticks_per_sec = sysconf(_SC_CLK_TCK); - if (ticks_per_sec == -1) { - virReportSystemError(errno, "%s", - _("Cannot determine system clock HZ")); - return -1; - } - scale = 1000000000.0 / ticks_per_sec; - } - *user *= scale; - *sys *= scale; - - return 0; + VIR_CGROUP_BACKEND_CALL(group, getCpuacctStat, -1, user, sys); } diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h index 39a75a69f4..3e3a73777d 100644 --- a/src/util/vircgroupbackend.h +++ b/src/util/vircgroupbackend.h @@ -311,6 +311,11 @@ typedef int (*virCgroupGetCpuacctPercpuUsageCB)(virCgroupPtr group, char **usage); +typedef int +(*virCgroupGetCpuacctStatCB)(virCgroupPtr group, + unsigned long long *user, + unsigned long long *sys); + struct _virCgroupBackend { virCgroupBackendType type; @@ -376,6 +381,7 @@ struct _virCgroupBackend { virCgroupGetCpuacctUsageCB getCpuacctUsage; virCgroupGetCpuacctPercpuUsageCB getCpuacctPercpuUsage; + virCgroupGetCpuacctStatCB getCpuacctStat; }; typedef struct _virCgroupBackend virCgroupBackend; typedef virCgroupBackend *virCgroupBackendPtr; diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c index 9fb0c5cb96..23a91adf60 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -1883,6 +1883,52 @@ virCgroupV1GetCpuacctPercpuUsage(virCgroupPtr group, } +static int +virCgroupV1GetCpuacctStat(virCgroupPtr group, + unsigned long long *user, + unsigned long long *sys) +{ + VIR_AUTOFREE(char *) str = NULL; + char *p; + static double scale = -1.0; + + if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT, + "cpuacct.stat", &str) < 0) + return -1; + + if (!(p = STRSKIP(str, "user ")) || + virStrToLong_ull(p, &p, 10, user) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Cannot parse user stat '%s'"), + p); + return -1; + } + if (!(p = STRSKIP(p, "\nsystem ")) || + virStrToLong_ull(p, NULL, 10, sys) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Cannot parse sys stat '%s'"), + p); + return -1; + } + /* times reported are in system ticks (generally 100 Hz), but that + * rate can theoretically vary between machines. Scale things + * into approximate nanoseconds. */ + if (scale < 0) { + long ticks_per_sec = sysconf(_SC_CLK_TCK); + if (ticks_per_sec == -1) { + virReportSystemError(errno, "%s", + _("Cannot determine system clock HZ")); + return -1; + } + scale = 1000000000.0 / ticks_per_sec; + } + *user *= scale; + *sys *= scale; + + return 0; +} + + virCgroupBackend virCgroupV1Backend = { .type = VIR_CGROUP_BACKEND_TYPE_V1, @@ -1946,6 +1992,7 @@ virCgroupBackend virCgroupV1Backend = { .getCpuacctUsage = virCgroupV1GetCpuacctUsage, .getCpuacctPercpuUsage = virCgroupV1GetCpuacctPercpuUsage, + .getCpuacctStat = virCgroupV1GetCpuacctStat, }; -- 2.22.0