1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

Move cgroup objects into virLXCControllerPtr

Move the cgroup object into virLXCControllerPtr and rename
all the setup methods to include 'Cgroup' in their name
if appropriate

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2012-07-03 14:53:59 +01:00
parent deba8b0c4c
commit f547e8768d

View File

@ -130,6 +130,8 @@ struct _virLXCController {
int monitorServerWatch; int monitorServerWatch;
int monitorClientFd; int monitorClientFd;
int monitorClientWatch; int monitorClientWatch;
virCgroupPtr cgroup;
}; };
static void virLXCControllerFree(virLXCControllerPtr ctrl); static void virLXCControllerFree(virLXCControllerPtr ctrl);
@ -207,6 +209,8 @@ static void virLXCControllerStopInit(virLXCControllerPtr ctrl)
virLXCControllerCloseLoopDevices(ctrl, true); virLXCControllerCloseLoopDevices(ctrl, true);
virPidAbort(ctrl->initpid); virPidAbort(ctrl->initpid);
ctrl->initpid = 0; ctrl->initpid = 0;
virCgroupFree(&ctrl->cgroup);
} }
@ -487,7 +491,7 @@ cleanup:
} }
#if HAVE_NUMACTL #if HAVE_NUMACTL
static int lxcSetContainerNUMAPolicy(virDomainDefPtr def) static int virLXCControllerSetupNUMAPolicy(virLXCControllerPtr ctrl)
{ {
nodemask_t mask; nodemask_t mask;
int mode = -1; int mode = -1;
@ -497,7 +501,7 @@ static int lxcSetContainerNUMAPolicy(virDomainDefPtr def)
int maxnode = 0; int maxnode = 0;
bool warned = false; bool warned = false;
if (!def->numatune.memory.nodemask) if (!ctrl->def->numatune.memory.nodemask)
return 0; return 0;
VIR_DEBUG("Setting NUMA memory policy"); VIR_DEBUG("Setting NUMA memory policy");
@ -513,7 +517,7 @@ static int lxcSetContainerNUMAPolicy(virDomainDefPtr def)
/* Convert nodemask to NUMA bitmask. */ /* Convert nodemask to NUMA bitmask. */
nodemask_zero(&mask); nodemask_zero(&mask);
for (i = 0; i < VIR_DOMAIN_CPUMASK_LEN; i++) { for (i = 0; i < VIR_DOMAIN_CPUMASK_LEN; i++) {
if (def->numatune.memory.nodemask[i]) { if (ctrl->def->numatune.memory.nodemask[i]) {
if (i > NUMA_NUM_NODES) { if (i > NUMA_NUM_NODES) {
lxcError(VIR_ERR_CONFIG_UNSUPPORTED, lxcError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Host cannot support NUMA node %d"), i); _("Host cannot support NUMA node %d"), i);
@ -528,7 +532,7 @@ static int lxcSetContainerNUMAPolicy(virDomainDefPtr def)
} }
} }
mode = def->numatune.memory.mode; mode = ctrl->def->numatune.memory.mode;
if (mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) { if (mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {
numa_set_bind_policy(1); numa_set_bind_policy(1);
@ -567,9 +571,9 @@ cleanup:
return ret; return ret;
} }
#else #else
static int lxcSetContainerNUMAPolicy(virDomainDefPtr def) static int virLXCControllerSetupNUMAPolicy(virLXCControllerPtr ctrl)
{ {
if (def->numatune.memory.nodemask) { if (ctrl->def->numatune.memory.nodemask) {
lxcError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", lxcError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("NUMA policy is not available on this platform")); _("NUMA policy is not available on this platform"));
return -1; return -1;
@ -583,7 +587,7 @@ static int lxcSetContainerNUMAPolicy(virDomainDefPtr def)
/* /*
* To be run while still single threaded * To be run while still single threaded
*/ */
static int lxcSetContainerCpuAffinity(virDomainDefPtr def) static int virLXCControllerSetupCpuAffinity(virLXCControllerPtr ctrl)
{ {
int i, hostcpus, maxcpu = CPU_SETSIZE; int i, hostcpus, maxcpu = CPU_SETSIZE;
virNodeInfo nodeinfo; virNodeInfo nodeinfo;
@ -607,11 +611,11 @@ static int lxcSetContainerCpuAffinity(virDomainDefPtr def)
return -1; return -1;
} }
if (def->cpumask) { if (ctrl->def->cpumask) {
/* XXX why don't we keep 'cpumask' in the libvirt cpumap /* XXX why don't we keep 'cpumask' in the libvirt cpumap
* format to start with ?!?! */ * format to start with ?!?! */
for (i = 0 ; i < maxcpu && i < def->cpumasklen ; i++) for (i = 0 ; i < maxcpu && i < ctrl->def->cpumasklen ; i++)
if (def->cpumask[i]) if (ctrl->def->cpumask[i])
VIR_USE_CPU(cpumap, i); VIR_USE_CPU(cpumap, i);
} else { } else {
/* You may think this is redundant, but we can't assume libvirtd /* You may think this is redundant, but we can't assume libvirtd
@ -637,33 +641,33 @@ static int lxcSetContainerCpuAffinity(virDomainDefPtr def)
} }
static int lxcSetContainerCpuTune(virCgroupPtr cgroup, virDomainDefPtr def) static int virLXCControllerSetupCgroupsCpuTune(virLXCControllerPtr ctrl)
{ {
int ret = -1; int ret = -1;
if (def->cputune.shares != 0) { if (ctrl->def->cputune.shares != 0) {
int rc = virCgroupSetCpuShares(cgroup, def->cputune.shares); int rc = virCgroupSetCpuShares(ctrl->cgroup, ctrl->def->cputune.shares);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set io cpu shares for domain %s"), _("Unable to set io cpu shares for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
if (def->cputune.quota != 0) { if (ctrl->def->cputune.quota != 0) {
int rc = virCgroupSetCpuCfsQuota(cgroup, def->cputune.quota); int rc = virCgroupSetCpuCfsQuota(ctrl->cgroup, ctrl->def->cputune.quota);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set io cpu quota for domain %s"), _("Unable to set io cpu quota for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
if (def->cputune.period != 0) { if (ctrl->def->cputune.period != 0) {
int rc = virCgroupSetCpuCfsPeriod(cgroup, def->cputune.period); int rc = virCgroupSetCpuCfsPeriod(ctrl->cgroup, ctrl->def->cputune.period);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set io cpu period for domain %s"), _("Unable to set io cpu period for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
@ -673,16 +677,16 @@ cleanup:
} }
static int lxcSetContainerBlkioTune(virCgroupPtr cgroup, virDomainDefPtr def) static int virLXCControllerSetupCgroupsBlkioTune(virLXCControllerPtr ctrl)
{ {
int ret = -1; int ret = -1;
if (def->blkio.weight) { if (ctrl->def->blkio.weight) {
int rc = virCgroupSetBlkioWeight(cgroup, def->blkio.weight); int rc = virCgroupSetBlkioWeight(ctrl->cgroup, ctrl->def->blkio.weight);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set Blkio weight for domain %s"), _("Unable to set Blkio weight for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
@ -693,45 +697,45 @@ cleanup:
} }
static int lxcSetContainerMemTune(virCgroupPtr cgroup, virDomainDefPtr def) static int virLXCControllerSetupCgroupsMemTune(virLXCControllerPtr ctrl)
{ {
int ret = -1; int ret = -1;
int rc; int rc;
rc = virCgroupSetMemory(cgroup, def->mem.max_balloon); rc = virCgroupSetMemory(ctrl->cgroup, ctrl->def->mem.max_balloon);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set memory limit for domain %s"), _("Unable to set memory limit for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
if (def->mem.hard_limit) { if (ctrl->def->mem.hard_limit) {
rc = virCgroupSetMemoryHardLimit(cgroup, def->mem.hard_limit); rc = virCgroupSetMemoryHardLimit(ctrl->cgroup, ctrl->def->mem.hard_limit);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set memory hard limit for domain %s"), _("Unable to set memory hard limit for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
if (def->mem.soft_limit) { if (ctrl->def->mem.soft_limit) {
rc = virCgroupSetMemorySoftLimit(cgroup, def->mem.soft_limit); rc = virCgroupSetMemorySoftLimit(ctrl->cgroup, ctrl->def->mem.soft_limit);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set memory soft limit for domain %s"), _("Unable to set memory soft limit for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
if (def->mem.swap_hard_limit) { if (ctrl->def->mem.swap_hard_limit) {
rc = virCgroupSetMemSwapHardLimit(cgroup, def->mem.swap_hard_limit); rc = virCgroupSetMemSwapHardLimit(ctrl->cgroup, ctrl->def->mem.swap_hard_limit);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to set swap hard limit for domain %s"), _("Unable to set swap hard limit for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
@ -742,7 +746,7 @@ cleanup:
} }
static int lxcSetContainerDeviceACL(virCgroupPtr cgroup, virDomainDefPtr def) static int virLXCControllerSetupCgroupsDeviceACL(virLXCControllerPtr ctrl)
{ {
int ret = -1; int ret = -1;
int rc; int rc;
@ -757,17 +761,17 @@ static int lxcSetContainerDeviceACL(virCgroupPtr cgroup, virDomainDefPtr def)
{'c', LXC_DEV_MAJ_TTY, LXC_DEV_MIN_PTMX}, {'c', LXC_DEV_MAJ_TTY, LXC_DEV_MIN_PTMX},
{0, 0, 0}}; {0, 0, 0}};
rc = virCgroupDenyAllDevices(cgroup); rc = virCgroupDenyAllDevices(ctrl->cgroup);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to deny devices for domain %s"), _("Unable to deny devices for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
for (i = 0; devices[i].type != 0; i++) { for (i = 0; devices[i].type != 0; i++) {
const struct cgroup_device_policy *dev = &devices[i]; const struct cgroup_device_policy *dev = &devices[i];
rc = virCgroupAllowDevice(cgroup, rc = virCgroupAllowDevice(ctrl->cgroup,
dev->type, dev->type,
dev->major, dev->major,
dev->minor, dev->minor,
@ -775,34 +779,34 @@ static int lxcSetContainerDeviceACL(virCgroupPtr cgroup, virDomainDefPtr def)
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to allow device %c:%d:%d for domain %s"), _("Unable to allow device %c:%d:%d for domain %s"),
dev->type, dev->major, dev->minor, def->name); dev->type, dev->major, dev->minor, ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
for (i = 0 ; i < def->nfss ; i++) { for (i = 0 ; i < ctrl->def->nfss ; i++) {
if (def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK) if (ctrl->def->fss[i]->type != VIR_DOMAIN_FS_TYPE_BLOCK)
continue; continue;
rc = virCgroupAllowDevicePath(cgroup, rc = virCgroupAllowDevicePath(ctrl->cgroup,
def->fss[i]->src, ctrl->def->fss[i]->src,
def->fss[i]->readonly ? ctrl->def->fss[i]->readonly ?
VIR_CGROUP_DEVICE_READ : VIR_CGROUP_DEVICE_READ :
VIR_CGROUP_DEVICE_RW); VIR_CGROUP_DEVICE_RW);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to allow device %s for domain %s"), _("Unable to allow device %s for domain %s"),
def->fss[i]->src, def->name); ctrl->def->fss[i]->src, ctrl->def->name);
goto cleanup; goto cleanup;
} }
} }
rc = virCgroupAllowDeviceMajor(cgroup, 'c', LXC_DEV_MAJ_PTY, rc = virCgroupAllowDeviceMajor(ctrl->cgroup, 'c', LXC_DEV_MAJ_PTY,
VIR_CGROUP_DEVICE_RWM); VIR_CGROUP_DEVICE_RWM);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to allow PTY devices for domain %s"), _("Unable to allow PTY devices for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
@ -813,24 +817,23 @@ cleanup:
/** /**
* lxcSetContainerResources * virLXCControllerSetupResourceLimits
* @def: pointer to virtual machine structure * @ctrl: the controller state
* *
* Creates a cgroup for the container, moves the task inside, * Creates a cgroup for the container, moves the task inside,
* and sets resource limits * and sets resource limits
* *
* Returns 0 on success or -1 in case of error * Returns 0 on success or -1 in case of error
*/ */
static int lxcSetContainerResources(virDomainDefPtr def) static int virLXCControllerSetupResourceLimits(virLXCControllerPtr ctrl)
{ {
virCgroupPtr driver; virCgroupPtr driver;
virCgroupPtr cgroup;
int rc = -1; int rc = -1;
if (lxcSetContainerCpuAffinity(def) < 0) if (virLXCControllerSetupCpuAffinity(ctrl) < 0)
return -1; return -1;
if (lxcSetContainerNUMAPolicy(def) < 0) if (virLXCControllerSetupNUMAPolicy(ctrl) < 0)
return -1; return -1;
rc = virCgroupForDriver("lxc", &driver, 1, 0); rc = virCgroupForDriver("lxc", &driver, 1, 0);
@ -844,36 +847,35 @@ static int lxcSetContainerResources(virDomainDefPtr def)
return rc; return rc;
} }
rc = virCgroupForDomain(driver, def->name, &cgroup, 1); rc = virCgroupForDomain(driver, ctrl->def->name, &ctrl->cgroup, 1);
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to create cgroup for domain %s"), _("Unable to create cgroup for domain %s"),
def->name); ctrl->def->name);
goto cleanup; goto cleanup;
} }
if (lxcSetContainerCpuTune(cgroup, def) < 0) if (virLXCControllerSetupCgroupsCpuTune(ctrl) < 0)
goto cleanup; goto cleanup;
if (lxcSetContainerBlkioTune(cgroup, def) < 0) if (virLXCControllerSetupCgroupsBlkioTune(ctrl) < 0)
goto cleanup; goto cleanup;
if (lxcSetContainerMemTune(cgroup, def) < 0) if (virLXCControllerSetupCgroupsMemTune(ctrl) < 0)
goto cleanup; goto cleanup;
if (lxcSetContainerDeviceACL(cgroup, def) < 0) if (virLXCControllerSetupCgroupsDeviceACL(ctrl) < 0)
goto cleanup; goto cleanup;
rc = virCgroupAddTask(cgroup, getpid()); rc = virCgroupAddTask(ctrl->cgroup, getpid());
if (rc != 0) { if (rc != 0) {
virReportSystemError(-rc, virReportSystemError(-rc,
_("Unable to add task %d to cgroup for domain %s"), _("Unable to add task %d to cgroup for domain %s"),
getpid(), def->name); getpid(), ctrl->def->name);
} }
cleanup: cleanup:
virCgroupFree(&driver); virCgroupFree(&driver);
virCgroupFree(&cgroup);
return rc; return rc;
} }
@ -1718,7 +1720,7 @@ virLXCControllerRun(virLXCControllerPtr ctrl)
if (virLXCControllerSetupLoopDevices(ctrl) < 0) if (virLXCControllerSetupLoopDevices(ctrl) < 0)
goto cleanup; goto cleanup;
if (lxcSetContainerResources(ctrl->def) < 0) if (virLXCControllerSetupResourceLimits(ctrl) < 0)
goto cleanup; goto cleanup;
if (virLXCControllerSetupDevPTS(ctrl) < 0) if (virLXCControllerSetupDevPTS(ctrl) < 0)