mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-22 11:22:23 +00:00
vircgroup: extract virCgroupV1GetCpuacctStat
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
parent
8370466323
commit
e294615f9d
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user