提交 d79fe8b5 编写于 作者: P Peter Krempa

cgroup: Move [qemu|lxc]GetCpuBWStatus to vicgroup.c and refactor it

The function existed in two identical instances in lxc and qemu. Move it
to vircgroup.c and simplify it. Refactor the callers too.
上级 4baa8d76
...@@ -1217,6 +1217,7 @@ virCgroupSetMemory; ...@@ -1217,6 +1217,7 @@ virCgroupSetMemory;
virCgroupSetMemoryHardLimit; virCgroupSetMemoryHardLimit;
virCgroupSetMemorySoftLimit; virCgroupSetMemorySoftLimit;
virCgroupSetMemSwapHardLimit; virCgroupSetMemSwapHardLimit;
virCgroupSupportsCpuBW;
# util/virclosecallbacks.h # util/virclosecallbacks.h
......
...@@ -1574,45 +1574,10 @@ static char *lxcConnectGetHostname(virConnectPtr conn) ...@@ -1574,45 +1574,10 @@ static char *lxcConnectGetHostname(virConnectPtr conn)
} }
/*
* check whether the host supports CFS bandwidth
*
* Return 1 when CFS bandwidth is supported, 0 when CFS bandwidth is not
* supported, -1 on error.
*/
static int lxcGetCpuBWStatus(virCgroupPtr cgroup)
{
char *cfs_period_path = NULL;
int ret = -1;
if (!cgroup)
return 0;
if (virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_CPU,
"cpu.cfs_period_us", &cfs_period_path) < 0) {
VIR_INFO("cannot get the path of cgroup CPU controller");
ret = 0;
goto cleanup;
}
if (access(cfs_period_path, F_OK) < 0) {
ret = 0;
} else {
ret = 1;
}
cleanup:
VIR_FREE(cfs_period_path);
return ret;
}
static char *lxcDomainGetSchedulerType(virDomainPtr dom, static char *lxcDomainGetSchedulerType(virDomainPtr dom,
int *nparams) int *nparams)
{ {
char *ret = NULL; char *ret = NULL;
int rc;
virDomainObjPtr vm; virDomainObjPtr vm;
virLXCDomainObjPrivatePtr priv; virLXCDomainObjPrivatePtr priv;
...@@ -1639,13 +1604,10 @@ static char *lxcDomainGetSchedulerType(virDomainPtr dom, ...@@ -1639,13 +1604,10 @@ static char *lxcDomainGetSchedulerType(virDomainPtr dom,
} }
if (nparams) { if (nparams) {
rc = lxcGetCpuBWStatus(priv->cgroup); if (virCgroupSupportsCpuBW(priv->cgroup))
if (rc < 0)
goto cleanup;
else if (rc == 0)
*nparams = 1;
else
*nparams = 3; *nparams = 3;
else
*nparams = 1;
} }
ignore_value(VIR_STRDUP(ret, "posix")); ignore_value(VIR_STRDUP(ret, "posix"));
...@@ -1872,12 +1834,8 @@ lxcDomainGetSchedulerParametersFlags(virDomainPtr dom, ...@@ -1872,12 +1834,8 @@ lxcDomainGetSchedulerParametersFlags(virDomainPtr dom,
if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0) if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
if (*nparams > 1) { if (*nparams > 1)
rc = lxcGetCpuBWStatus(priv->cgroup); cpu_bw_status = virCgroupSupportsCpuBW(priv->cgroup);
if (rc < 0)
goto cleanup;
cpu_bw_status = !!rc;
}
if (!(caps = virLXCDriverGetCapabilities(driver, false))) if (!(caps = virLXCDriverGetCapabilities(driver, false)))
goto cleanup; goto cleanup;
......
...@@ -7325,44 +7325,10 @@ cleanup: ...@@ -7325,44 +7325,10 @@ cleanup:
} }
/*
* check whether the host supports CFS bandwidth
*
* Return 1 when CFS bandwidth is supported, 0 when CFS bandwidth is not
* supported, -1 on error.
*/
static int qemuGetCpuBWStatus(virCgroupPtr cgroup)
{
char *cfs_period_path = NULL;
int ret = -1;
if (!cgroup)
return 0;
if (virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_CPU,
"cpu.cfs_period_us", &cfs_period_path) < 0) {
VIR_INFO("cannot get the path of cgroup CPU controller");
ret = 0;
goto cleanup;
}
if (access(cfs_period_path, F_OK) < 0) {
ret = 0;
} else {
ret = 1;
}
cleanup:
VIR_FREE(cfs_period_path);
return ret;
}
static char *qemuDomainGetSchedulerType(virDomainPtr dom, static char *qemuDomainGetSchedulerType(virDomainPtr dom,
int *nparams) int *nparams)
{ {
char *ret = NULL; char *ret = NULL;
int rc;
virDomainObjPtr vm = NULL; virDomainObjPtr vm = NULL;
qemuDomainObjPrivatePtr priv; qemuDomainObjPrivatePtr priv;
...@@ -7389,13 +7355,10 @@ static char *qemuDomainGetSchedulerType(virDomainPtr dom, ...@@ -7389,13 +7355,10 @@ static char *qemuDomainGetSchedulerType(virDomainPtr dom,
} }
if (nparams) { if (nparams) {
rc = qemuGetCpuBWStatus(priv->cgroup); if (virCgroupSupportsCpuBW(priv->cgroup))
if (rc < 0)
goto cleanup;
else if (rc == 0)
*nparams = 1;
else
*nparams = 5; *nparams = 5;
else
*nparams = 1;
} }
ignore_value(VIR_STRDUP(ret, "posix")); ignore_value(VIR_STRDUP(ret, "posix"));
...@@ -8728,12 +8691,8 @@ qemuDomainGetSchedulerParametersFlags(virDomainPtr dom, ...@@ -8728,12 +8691,8 @@ qemuDomainGetSchedulerParametersFlags(virDomainPtr dom,
if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0) if (virDomainGetSchedulerParametersFlagsEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
if (*nparams > 1) { if (*nparams > 1)
rc = qemuGetCpuBWStatus(priv->cgroup); cpu_bw_status = virCgroupSupportsCpuBW(priv->cgroup);
if (rc < 0)
goto cleanup;
cpu_bw_status = !!rc;
}
if (!(caps = virQEMUDriverGetCapabilities(driver, false))) if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
goto cleanup; goto cleanup;
......
...@@ -3061,6 +3061,36 @@ cleanup: ...@@ -3061,6 +3061,36 @@ cleanup:
} }
/**
* virCgroupSupportsCpuBW():
* Check whether the host supports CFS bandwidth.
*
* Return true when CFS bandwidth is supported,
* false when CFS bandwidth is not supported.
*/
bool
virCgroupSupportsCpuBW(virCgroupPtr cgroup)
{
char *path = NULL;
int ret = false;
if (!cgroup)
return false;
if (virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_CPU,
"cpu.cfs_period_us", &path) < 0) {
virResetLastError();
goto cleanup;
}
ret = virFileExists(path);
cleanup:
VIR_FREE(path);
return ret;
}
#else /* !VIR_CGROUP_SUPPORTED */ #else /* !VIR_CGROUP_SUPPORTED */
bool bool
...@@ -3646,4 +3676,12 @@ virCgroupIsolateMount(virCgroupPtr group ATTRIBUTE_UNUSED, ...@@ -3646,4 +3676,12 @@ virCgroupIsolateMount(virCgroupPtr group ATTRIBUTE_UNUSED,
return -1; return -1;
} }
bool
virCgroupSupportsCpuBW(virCgroupPtr cgroup ATTRIBUTE_UNUSED)
{
VIR_DEBUG("Control groups not supported on this platform");
return false;
}
#endif /* !VIR_CGROUP_SUPPORTED */ #endif /* !VIR_CGROUP_SUPPORTED */
...@@ -207,4 +207,6 @@ int virCgroupIsolateMount(virCgroupPtr group, ...@@ -207,4 +207,6 @@ int virCgroupIsolateMount(virCgroupPtr group,
const char *oldroot, const char *oldroot,
const char *mountopts); const char *mountopts);
bool virCgroupSupportsCpuBW(virCgroupPtr cgroup);
#endif /* __VIR_CGROUP_H__ */ #endif /* __VIR_CGROUP_H__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册