diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 0137f099c0a97ea52be8cd91595cfa028477f6de..af533a39104c14e3383e6612f89bd3ea5b526fe2 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -2592,44 +2592,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 39a75a69f465a7f5c4d874ec631ff1fb3145bf74..3e3a73777d24d613f0960de745e12d588e334017 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 d11a5e6865653c4f24e84761f4eaa3974ea0cf8d..83a74622632e532235711ca60f9ed926c783d233 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -1882,6 +1882,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, @@ -1945,6 +1991,7 @@ virCgroupBackend virCgroupV1Backend = { .getCpuacctUsage = virCgroupV1GetCpuacctUsage, .getCpuacctPercpuUsage = virCgroupV1GetCpuacctPercpuUsage, + .getCpuacctStat = virCgroupV1GetCpuacctStat, };