2008-10-03 17:58:02 +00:00
|
|
|
/*
|
|
|
|
* cgroup.c: Tools for managing cgroups
|
|
|
|
*
|
2010-06-23 16:00:55 +00:00
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
2008-10-03 17:58:02 +00:00
|
|
|
* Copyright IBM Corp. 2008
|
|
|
|
*
|
|
|
|
* See COPYING.LIB for the License of this software
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Dan Smith <danms@us.ibm.com>
|
|
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2010-04-23 09:34:17 +00:00
|
|
|
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
|
2010-03-09 18:22:22 +00:00
|
|
|
# include <mntent.h>
|
2009-10-07 10:18:31 +00:00
|
|
|
#endif
|
2008-10-03 17:58:02 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <libgen.h>
|
2010-06-23 16:00:55 +00:00
|
|
|
#include <dirent.h>
|
2008-10-03 17:58:02 +00:00
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "cgroup.h"
|
2008-11-06 16:36:07 +00:00
|
|
|
#include "logging.h"
|
2008-10-03 17:58:02 +00:00
|
|
|
|
|
|
|
#define CGROUP_MAX_VAL 512
|
|
|
|
|
2009-07-09 13:09:38 +00:00
|
|
|
VIR_ENUM_IMPL(virCgroupController, VIR_CGROUP_CONTROLLER_LAST,
|
2009-09-21 14:31:22 +00:00
|
|
|
"cpu", "cpuacct", "cpuset", "memory", "devices",
|
|
|
|
"freezer");
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
struct virCgroupController {
|
|
|
|
int type;
|
|
|
|
char *mountPoint;
|
|
|
|
char *placement;
|
|
|
|
};
|
2009-07-09 13:09:38 +00:00
|
|
|
|
|
|
|
struct virCgroup {
|
|
|
|
char *path;
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
struct virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST];
|
2008-10-03 17:58:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virCgroupFree:
|
|
|
|
*
|
|
|
|
* @group: The group structure to free
|
|
|
|
*/
|
|
|
|
void virCgroupFree(virCgroupPtr *group)
|
|
|
|
{
|
2009-07-09 13:10:41 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (*group == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
|
|
|
VIR_FREE((*group)->controllers[i].mountPoint);
|
|
|
|
VIR_FREE((*group)->controllers[i].placement);
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
VIR_FREE((*group)->path);
|
|
|
|
VIR_FREE(*group);
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2010-04-23 09:34:17 +00:00
|
|
|
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
|
2009-07-09 13:10:41 +00:00
|
|
|
/*
|
|
|
|
* Process /proc/mounts figuring out what controllers are
|
|
|
|
* mounted and where
|
|
|
|
*/
|
|
|
|
static int virCgroupDetectMounts(virCgroupPtr group)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
2009-07-09 13:10:41 +00:00
|
|
|
int i;
|
2009-03-16 10:41:37 +00:00
|
|
|
FILE *mounts = NULL;
|
2008-10-03 17:58:02 +00:00
|
|
|
struct mntent entry;
|
|
|
|
char buf[CGROUP_MAX_VAL];
|
|
|
|
|
|
|
|
mounts = fopen("/proc/mounts", "r");
|
|
|
|
if (mounts == NULL) {
|
2010-05-20 06:14:50 +00:00
|
|
|
VIR_ERROR0(_("Unable to open /proc/mounts"));
|
2009-07-09 13:10:41 +00:00
|
|
|
return -ENOENT;
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
|
2009-07-09 13:10:41 +00:00
|
|
|
if (STRNEQ(entry.mnt_type, "cgroup"))
|
|
|
|
continue;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
|
|
|
const char *typestr = virCgroupControllerTypeToString(i);
|
|
|
|
int typelen = strlen(typestr);
|
|
|
|
char *tmp = entry.mnt_opts;
|
|
|
|
while (tmp) {
|
|
|
|
char *next = strchr(tmp, ',');
|
|
|
|
int len;
|
|
|
|
if (next) {
|
|
|
|
len = next-tmp;
|
|
|
|
next++;
|
|
|
|
} else {
|
|
|
|
len = strlen(tmp);
|
|
|
|
}
|
|
|
|
if (typelen == len && STREQLEN(typestr, tmp, len) &&
|
|
|
|
!(group->controllers[i].mountPoint = strdup(entry.mnt_dir)))
|
|
|
|
goto no_memory;
|
|
|
|
tmp = next;
|
|
|
|
}
|
|
|
|
}
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(mounts);
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
return 0;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
no_memory:
|
|
|
|
if (mounts)
|
|
|
|
fclose(mounts);
|
|
|
|
return -ENOMEM;
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process /proc/self/cgroup figuring out what cgroup
|
|
|
|
* sub-path the current process is assigned to. ie not
|
|
|
|
* neccessarily in the root
|
2008-10-03 17:58:02 +00:00
|
|
|
*/
|
2009-07-09 13:10:41 +00:00
|
|
|
static int virCgroupDetectPlacement(virCgroupPtr group)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-07-09 13:10:41 +00:00
|
|
|
FILE *mapping = NULL;
|
|
|
|
char line[1024];
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
mapping = fopen("/proc/self/cgroup", "r");
|
|
|
|
if (mapping == NULL) {
|
2010-05-20 06:14:50 +00:00
|
|
|
VIR_ERROR0(_("Unable to open /proc/self/cgroup"));
|
2009-07-09 13:10:41 +00:00
|
|
|
return -ENOENT;
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
while (fgets(line, sizeof(line), mapping) != NULL) {
|
|
|
|
char *controllers = strchr(line, ':');
|
|
|
|
char *path = controllers ? strchr(controllers+1, ':') : NULL;
|
|
|
|
char *nl = path ? strchr(path, '\n') : NULL;
|
|
|
|
|
|
|
|
if (!controllers || !path)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (nl)
|
|
|
|
*nl = '\0';
|
|
|
|
|
|
|
|
*path = '\0';
|
|
|
|
controllers++;
|
|
|
|
path++;
|
|
|
|
|
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
|
|
|
const char *typestr = virCgroupControllerTypeToString(i);
|
|
|
|
int typelen = strlen(typestr);
|
|
|
|
char *tmp = controllers;
|
|
|
|
while (tmp) {
|
|
|
|
char *next = strchr(tmp, ',');
|
|
|
|
int len;
|
|
|
|
if (next) {
|
|
|
|
len = next-tmp;
|
|
|
|
next++;
|
|
|
|
} else {
|
|
|
|
len = strlen(tmp);
|
|
|
|
}
|
|
|
|
if (typelen == len && STREQLEN(typestr, tmp, len) &&
|
|
|
|
!(group->controllers[i].placement = strdup(STREQ(path, "/") ? "" : path)))
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
tmp = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(mapping);
|
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
return 0;
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
no_memory:
|
2010-02-04 10:22:20 +00:00
|
|
|
fclose(mapping);
|
2009-07-09 13:10:41 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
static int virCgroupDetect(virCgroupPtr group)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
2009-07-09 13:10:41 +00:00
|
|
|
int any = 0;
|
|
|
|
int rc;
|
|
|
|
int i;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupDetectMounts(group);
|
|
|
|
if (rc < 0) {
|
2010-05-20 06:15:46 +00:00
|
|
|
VIR_ERROR(_("Failed to detect mounts for %s"), group->path);
|
2009-07-09 13:10:41 +00:00
|
|
|
return rc;
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
/* Check that at least 1 controller is available */
|
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
|
|
|
if (group->controllers[i].mountPoint != NULL)
|
|
|
|
any = 1;
|
|
|
|
}
|
|
|
|
if (!any)
|
|
|
|
return -ENXIO;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupDetectPlacement(group);
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
if (rc == 0) {
|
|
|
|
/* Check that for every mounted controller, we found our placement */
|
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
|
|
|
if (!group->controllers[i].mountPoint)
|
|
|
|
continue;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
if (!group->controllers[i].placement) {
|
2010-05-20 06:15:46 +00:00
|
|
|
VIR_ERROR(_("Could not find placement for controller %s at %s"),
|
2009-07-09 13:10:41 +00:00
|
|
|
virCgroupControllerTypeToString(i),
|
|
|
|
group->controllers[i].placement);
|
|
|
|
rc = -ENOENT;
|
|
|
|
break;
|
|
|
|
}
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_DEBUG("Detected mount/mapping %i:%s at %s in %s", i,
|
|
|
|
virCgroupControllerTypeToString(i),
|
|
|
|
group->controllers[i].mountPoint,
|
|
|
|
group->controllers[i].placement);
|
|
|
|
}
|
|
|
|
} else {
|
2010-05-20 06:15:46 +00:00
|
|
|
VIR_ERROR(_("Failed to detect mapping for %s"), group->path);
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2009-10-07 10:18:31 +00:00
|
|
|
#endif
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
static int virCgroupPathOfController(virCgroupPtr group,
|
|
|
|
int controller,
|
|
|
|
const char *key,
|
|
|
|
char **path)
|
|
|
|
{
|
|
|
|
if (group->controllers[controller].mountPoint == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (group->controllers[controller].placement == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (virAsprintf(path, "%s%s%s/%s",
|
|
|
|
group->controllers[controller].mountPoint,
|
|
|
|
group->controllers[controller].placement,
|
|
|
|
STREQ(group->path, "/") ? "" : group->path,
|
|
|
|
key ? key : "") == -1)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
static int virCgroupSetValueStr(virCgroupPtr group,
|
2009-07-09 13:10:41 +00:00
|
|
|
int controller,
|
2008-10-03 17:58:02 +00:00
|
|
|
const char *key,
|
|
|
|
const char *value)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
char *keypath = NULL;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupPathOfController(group, controller, key, &keypath);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
|
2010-06-23 15:17:15 +00:00
|
|
|
VIR_DEBUG("Set value '%s' to '%s'", keypath, value);
|
2009-07-09 13:10:06 +00:00
|
|
|
rc = virFileWriteStr(keypath, value);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
DEBUG("Failed to write value '%s': %m", value);
|
|
|
|
rc = -errno;
|
2009-07-09 13:10:06 +00:00
|
|
|
} else {
|
|
|
|
rc = 0;
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(keypath);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virCgroupGetValueStr(virCgroupPtr group,
|
2009-07-09 13:10:41 +00:00
|
|
|
int controller,
|
2008-10-03 17:58:02 +00:00
|
|
|
const char *key,
|
|
|
|
char **value)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *keypath = NULL;
|
|
|
|
|
2009-07-09 13:10:06 +00:00
|
|
|
*value = NULL;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupPathOfController(group, controller, key, &keypath);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
DEBUG("No path of %s, %s", group->path, key);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:06 +00:00
|
|
|
VIR_DEBUG("Get value %s", keypath);
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:06 +00:00
|
|
|
rc = virFileReadAll(keypath, 1024, value);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
DEBUG("Failed to read %s: %m\n", keypath);
|
|
|
|
rc = -errno;
|
2009-07-09 13:10:06 +00:00
|
|
|
} else {
|
2009-10-19 12:29:42 +00:00
|
|
|
/* Terminated with '\n' has sometimes harmful effects to the caller */
|
|
|
|
char *p = strchr(*value, '\n');
|
|
|
|
if (p) *p = '\0';
|
|
|
|
|
2009-07-09 13:10:06 +00:00
|
|
|
rc = 0;
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(keypath);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:06 +00:00
|
|
|
static int virCgroupSetValueU64(virCgroupPtr group,
|
2009-07-09 13:10:41 +00:00
|
|
|
int controller,
|
2009-07-09 13:10:06 +00:00
|
|
|
const char *key,
|
2009-10-07 10:18:31 +00:00
|
|
|
unsigned long long int value)
|
2009-07-09 13:10:06 +00:00
|
|
|
{
|
|
|
|
char *strval = NULL;
|
|
|
|
int rc;
|
|
|
|
|
2009-10-07 10:18:31 +00:00
|
|
|
if (virAsprintf(&strval, "%llu", value) == -1)
|
2009-07-09 13:10:06 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupSetValueStr(group, controller, key, strval);
|
2009-07-09 13:10:06 +00:00
|
|
|
|
|
|
|
VIR_FREE(strval);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-08 16:28:48 +00:00
|
|
|
#if 0
|
|
|
|
/* This is included for completeness, but not yet used */
|
|
|
|
|
|
|
|
static int virCgroupSetValueI64(virCgroupPtr group,
|
2009-07-09 13:10:41 +00:00
|
|
|
int controller,
|
2008-10-03 17:58:02 +00:00
|
|
|
const char *key,
|
2009-10-07 10:18:31 +00:00
|
|
|
long long int value)
|
2008-10-08 16:28:48 +00:00
|
|
|
{
|
|
|
|
char *strval = NULL;
|
|
|
|
int rc;
|
|
|
|
|
2009-10-07 10:18:31 +00:00
|
|
|
if (virAsprintf(&strval, "%lld", value) == -1)
|
2008-10-08 16:28:48 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupSetValueStr(group, controller, key, strval);
|
2008-10-08 16:28:48 +00:00
|
|
|
|
|
|
|
VIR_FREE(strval);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virCgroupGetValueI64(virCgroupPtr group,
|
2009-07-09 13:10:41 +00:00
|
|
|
int controller,
|
2008-10-08 16:28:48 +00:00
|
|
|
const char *key,
|
2009-10-07 10:18:31 +00:00
|
|
|
long long int *value)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
char *strval = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupGetValueStr(group, controller, key, &strval);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc != 0)
|
|
|
|
goto out;
|
|
|
|
|
2010-03-30 14:20:41 +00:00
|
|
|
if (virStrToLong_ll(strval, NULL, 10, value) < 0)
|
2008-10-03 17:58:02 +00:00
|
|
|
rc = -EINVAL;
|
|
|
|
out:
|
|
|
|
VIR_FREE(strval);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2008-10-08 16:28:48 +00:00
|
|
|
#endif
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2008-10-08 16:28:48 +00:00
|
|
|
static int virCgroupGetValueU64(virCgroupPtr group,
|
2009-07-09 13:10:41 +00:00
|
|
|
int controller,
|
2008-10-03 17:58:02 +00:00
|
|
|
const char *key,
|
2009-10-07 10:18:31 +00:00
|
|
|
unsigned long long int *value)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
char *strval = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupGetValueStr(group, controller, key, &strval);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc != 0)
|
|
|
|
goto out;
|
|
|
|
|
2009-10-07 10:18:31 +00:00
|
|
|
if (virStrToLong_ull(strval, NULL, 10, value) < 0)
|
2008-10-03 17:58:02 +00:00
|
|
|
rc = -EINVAL;
|
|
|
|
out:
|
|
|
|
VIR_FREE(strval);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-23 09:34:17 +00:00
|
|
|
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
|
2009-07-09 13:10:41 +00:00
|
|
|
static int virCgroupCpuSetInherit(virCgroupPtr parent, virCgroupPtr group)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rc = 0;
|
|
|
|
const char *inherit_values[] = {
|
|
|
|
"cpuset.cpus",
|
|
|
|
"cpuset.mems",
|
|
|
|
};
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_DEBUG("Setting up inheritance %s -> %s", parent->path, group->path);
|
|
|
|
for (i = 0; i < ARRAY_CARDINALITY(inherit_values) ; i++) {
|
|
|
|
char *value;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupGetValueStr(parent,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPUSET,
|
|
|
|
inherit_values[i],
|
|
|
|
&value);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc != 0) {
|
2010-05-20 06:15:46 +00:00
|
|
|
VIR_ERROR(_("Failed to get %s %d"), inherit_values[i], rc);
|
2009-07-09 13:10:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Inherit %s = %s", inherit_values[i], value);
|
|
|
|
|
|
|
|
rc = virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPUSET,
|
|
|
|
inherit_values[i],
|
|
|
|
value);
|
2010-02-04 10:14:03 +00:00
|
|
|
VIR_FREE(value);
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
if (rc != 0) {
|
2010-05-20 06:15:46 +00:00
|
|
|
VIR_ERROR(_("Failed to set %s %d"), inherit_values[i], rc);
|
2008-10-03 17:58:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
cgroup: Enable memory.use_hierarchy of cgroup for domain
Through conversation with Kumar L Srikanth-B22348, I found
that the function of getting memory usage (e.g., virsh dominfo)
doesn't work for lxc with ns subsystem of cgroup enabled.
This is because of features of ns and memory subsystems.
Ns creates child cgroup on every process fork and as a result
processes in a container are not assigned in a cgroup for
domain (e.g., libvirt/lxc/test1/). For example, libvirt_lxc
and init (or somewhat specified in XML) are assigned into
libvirt/lxc/test1/8839/ and libvirt/lxc/test1/8839/8849/,
respectively. On the other hand, memory subsystem accounts
memory usage within a group of processes by default, i.e.,
it does not take any child (and descendant) groups into
account. With the two features, virsh dominfo which just
checks memory usage of a cgroup for domain always returns
zero because the cgroup has no process.
Setting memory.use_hierarchy of a group allows to account
(and limit) memory usage of every descendant groups of the group.
By setting it of a cgroup for domain, we can get proper memory
usage of lxc with ns subsystem enabled. (To be exact, the
setting is required only when memory and ns subsystems are
enabled at the same time, e.g., mount -t cgroup none /cgroup.)
2010-06-23 16:00:56 +00:00
|
|
|
static int virCgroupSetMemoryUseHierarchy(virCgroupPtr group)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
unsigned long long value;
|
|
|
|
const char *filename = "memory.use_hierarchy";
|
|
|
|
|
|
|
|
rc = virCgroupGetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
filename, &value);
|
|
|
|
if (rc != 0) {
|
|
|
|
VIR_ERROR(_("Failed to read %s/%s (%d)"), group->path, filename, rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setting twice causes error, so if already enabled, skip setting */
|
|
|
|
if (value == 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
VIR_DEBUG("Setting up %s/%s", group->path, filename);
|
|
|
|
rc = virCgroupSetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
filename, 1);
|
|
|
|
|
|
|
|
if (rc != 0) {
|
|
|
|
VIR_ERROR(_("Failed to set %s/%s (%d)"), group->path, filename, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virCgroupMakeGroup(virCgroupPtr parent, virCgroupPtr group,
|
|
|
|
int create, bool memory_hierarchy)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rc = 0;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_DEBUG("Make group %s", group->path);
|
2009-07-09 13:09:38 +00:00
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
2008-10-03 17:58:02 +00:00
|
|
|
char *path = NULL;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
/* Skip over controllers that aren't mounted */
|
|
|
|
if (!group->controllers[i].mountPoint)
|
2008-10-03 17:58:02 +00:00
|
|
|
continue;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupPathOfController(group, i, "", &path);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_DEBUG("Make controller %s", path);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (access(path, F_OK) != 0) {
|
2009-11-12 14:11:43 +00:00
|
|
|
if (!create ||
|
|
|
|
mkdir(path, 0755) < 0) {
|
2008-10-03 17:58:02 +00:00
|
|
|
rc = -errno;
|
|
|
|
VIR_FREE(path);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-09 13:10:41 +00:00
|
|
|
if (group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint != NULL &&
|
|
|
|
(i == VIR_CGROUP_CONTROLLER_CPUSET ||
|
|
|
|
STREQ(group->controllers[i].mountPoint, group->controllers[VIR_CGROUP_CONTROLLER_CPUSET].mountPoint))) {
|
|
|
|
rc = virCgroupCpuSetInherit(parent, group);
|
2010-05-03 19:04:51 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
VIR_FREE(path);
|
2009-07-09 13:10:41 +00:00
|
|
|
break;
|
2010-05-03 19:04:51 +00:00
|
|
|
}
|
2009-07-09 13:10:41 +00:00
|
|
|
}
|
cgroup: Enable memory.use_hierarchy of cgroup for domain
Through conversation with Kumar L Srikanth-B22348, I found
that the function of getting memory usage (e.g., virsh dominfo)
doesn't work for lxc with ns subsystem of cgroup enabled.
This is because of features of ns and memory subsystems.
Ns creates child cgroup on every process fork and as a result
processes in a container are not assigned in a cgroup for
domain (e.g., libvirt/lxc/test1/). For example, libvirt_lxc
and init (or somewhat specified in XML) are assigned into
libvirt/lxc/test1/8839/ and libvirt/lxc/test1/8839/8849/,
respectively. On the other hand, memory subsystem accounts
memory usage within a group of processes by default, i.e.,
it does not take any child (and descendant) groups into
account. With the two features, virsh dominfo which just
checks memory usage of a cgroup for domain always returns
zero because the cgroup has no process.
Setting memory.use_hierarchy of a group allows to account
(and limit) memory usage of every descendant groups of the group.
By setting it of a cgroup for domain, we can get proper memory
usage of lxc with ns subsystem enabled. (To be exact, the
setting is required only when memory and ns subsystems are
enabled at the same time, e.g., mount -t cgroup none /cgroup.)
2010-06-23 16:00:56 +00:00
|
|
|
/*
|
|
|
|
* Note that virCgroupSetMemoryUseHierarchy should always be
|
|
|
|
* called prior to creating subcgroups and attaching tasks.
|
|
|
|
*/
|
|
|
|
if (memory_hierarchy &&
|
|
|
|
group->controllers[VIR_CGROUP_CONTROLLER_MEMORY].mountPoint != NULL &&
|
|
|
|
(i == VIR_CGROUP_CONTROLLER_MEMORY ||
|
|
|
|
STREQ(group->controllers[i].mountPoint, group->controllers[VIR_CGROUP_CONTROLLER_MEMORY].mountPoint))) {
|
|
|
|
rc = virCgroupSetMemoryUseHierarchy(group);
|
|
|
|
if (rc != 0) {
|
|
|
|
VIR_FREE(path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
|
|
|
|
static int virCgroupNew(const char *path,
|
|
|
|
virCgroupPtr *group)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
char *typpath = NULL;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_DEBUG("New group %s", path);
|
|
|
|
*group = NULL;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
if (VIR_ALLOC((*group)) != 0) {
|
2008-10-03 17:58:02 +00:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
if (!((*group)->path = strdup(path))) {
|
2008-10-03 17:58:02 +00:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupDetect(*group);
|
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
err:
|
2009-07-09 13:10:41 +00:00
|
|
|
virCgroupFree(group);
|
|
|
|
*group = NULL;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
|
|
|
VIR_FREE(typpath);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
static int virCgroupAppRoot(int privileged,
|
2010-03-04 11:01:52 +00:00
|
|
|
virCgroupPtr *group,
|
|
|
|
int create)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
2009-07-10 10:40:04 +00:00
|
|
|
virCgroupPtr rootgrp = NULL;
|
|
|
|
int rc;
|
2009-07-09 13:10:41 +00:00
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
rc = virCgroupNew("/", &rootgrp);
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
if (privileged) {
|
|
|
|
rc = virCgroupNew("/libvirt", group);
|
|
|
|
} else {
|
|
|
|
char *rootname;
|
|
|
|
char *username;
|
2010-02-04 22:41:52 +00:00
|
|
|
username = virGetUserName(getuid());
|
2009-07-10 10:40:04 +00:00
|
|
|
if (!username) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
rc = virAsprintf(&rootname, "/libvirt-%s", username);
|
|
|
|
VIR_FREE(username);
|
|
|
|
if (rc < 0) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-07-09 13:10:41 +00:00
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
rc = virCgroupNew(rootname, group);
|
|
|
|
VIR_FREE(rootname);
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
if (rc != 0)
|
2009-07-10 10:40:04 +00:00
|
|
|
goto cleanup;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
cgroup: Enable memory.use_hierarchy of cgroup for domain
Through conversation with Kumar L Srikanth-B22348, I found
that the function of getting memory usage (e.g., virsh dominfo)
doesn't work for lxc with ns subsystem of cgroup enabled.
This is because of features of ns and memory subsystems.
Ns creates child cgroup on every process fork and as a result
processes in a container are not assigned in a cgroup for
domain (e.g., libvirt/lxc/test1/). For example, libvirt_lxc
and init (or somewhat specified in XML) are assigned into
libvirt/lxc/test1/8839/ and libvirt/lxc/test1/8839/8849/,
respectively. On the other hand, memory subsystem accounts
memory usage within a group of processes by default, i.e.,
it does not take any child (and descendant) groups into
account. With the two features, virsh dominfo which just
checks memory usage of a cgroup for domain always returns
zero because the cgroup has no process.
Setting memory.use_hierarchy of a group allows to account
(and limit) memory usage of every descendant groups of the group.
By setting it of a cgroup for domain, we can get proper memory
usage of lxc with ns subsystem enabled. (To be exact, the
setting is required only when memory and ns subsystems are
enabled at the same time, e.g., mount -t cgroup none /cgroup.)
2010-06-23 16:00:56 +00:00
|
|
|
rc = virCgroupMakeGroup(rootgrp, *group, create, false);
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
cleanup:
|
|
|
|
virCgroupFree(&rootgrp);
|
2008-10-03 17:58:02 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2009-10-07 10:18:31 +00:00
|
|
|
#endif
|
2009-07-10 10:40:04 +00:00
|
|
|
|
2010-06-23 16:00:55 +00:00
|
|
|
static int virCgroupRemoveRecursively(char *grppath)
|
|
|
|
{
|
|
|
|
DIR *grpdir;
|
|
|
|
struct dirent *ent;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
grpdir = opendir(grppath);
|
|
|
|
if (grpdir == NULL) {
|
2010-06-26 17:21:28 +00:00
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
2010-06-23 16:00:55 +00:00
|
|
|
VIR_ERROR(_("Unable to open %s (%d)"), grppath, errno);
|
|
|
|
rc = -errno;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ent = readdir(grpdir);
|
|
|
|
if (ent == NULL) {
|
|
|
|
if ((rc = -errno))
|
|
|
|
VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ent->d_name[0] == '.') continue;
|
|
|
|
if (ent->d_type != DT_DIR) continue;
|
|
|
|
|
|
|
|
if (virAsprintf(&path, "%s/%s", grppath, ent->d_name) == -1) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc = virCgroupRemoveRecursively(path);
|
|
|
|
VIR_FREE(path);
|
|
|
|
if (rc != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
closedir(grpdir);
|
|
|
|
|
|
|
|
DEBUG("Removing cgroup %s", grppath);
|
|
|
|
if (rmdir(grppath) != 0 && errno != ENOENT) {
|
|
|
|
rc = -errno;
|
|
|
|
VIR_ERROR(_("Unable to remove %s (%d)"), grppath, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
/**
|
|
|
|
* virCgroupRemove:
|
|
|
|
*
|
|
|
|
* @group: The group to be removed
|
|
|
|
*
|
2010-06-23 16:00:55 +00:00
|
|
|
* It first removes all child groups recursively
|
|
|
|
* in depth first order and then removes @group
|
|
|
|
* because the presence of the child groups
|
|
|
|
* prevents removing @group.
|
|
|
|
*
|
2008-10-03 17:58:02 +00:00
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
|
|
|
int virCgroupRemove(virCgroupPtr group)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int i;
|
|
|
|
char *grppath = NULL;
|
|
|
|
|
2009-07-09 13:09:38 +00:00
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
2009-07-09 13:10:41 +00:00
|
|
|
/* Skip over controllers not mounted */
|
|
|
|
if (!group->controllers[i].mountPoint)
|
2008-10-03 17:58:02 +00:00
|
|
|
continue;
|
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
if (virCgroupPathOfController(group,
|
|
|
|
i,
|
|
|
|
NULL,
|
|
|
|
&grppath) != 0)
|
|
|
|
continue;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2010-06-23 16:00:55 +00:00
|
|
|
DEBUG("Removing cgroup %s and all child cgroups", grppath);
|
|
|
|
rc = virCgroupRemoveRecursively(grppath);
|
2008-10-03 17:58:02 +00:00
|
|
|
VIR_FREE(grppath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virCgroupAddTask:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to add a task to
|
|
|
|
* @pid: The pid of the task to add
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
|
|
|
int virCgroupAddTask(virCgroupPtr group, pid_t pid)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int i;
|
|
|
|
|
2009-07-09 13:09:38 +00:00
|
|
|
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
|
2009-07-09 13:10:41 +00:00
|
|
|
/* Skip over controllers not mounted */
|
|
|
|
if (!group->controllers[i].mountPoint)
|
|
|
|
continue;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-09 13:10:41 +00:00
|
|
|
rc = virCgroupSetValueU64(group, i, "tasks", (unsigned long long)pid);
|
2008-10-03 17:58:02 +00:00
|
|
|
if (rc != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
/**
|
2009-07-10 10:40:04 +00:00
|
|
|
* virCgroupForDriver:
|
2008-10-03 17:58:02 +00:00
|
|
|
*
|
2009-07-10 10:40:04 +00:00
|
|
|
* @name: name of this driver (e.g., xen, qemu, lxc)
|
2008-10-03 17:58:02 +00:00
|
|
|
* @group: Pointer to returned virCgroupPtr
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
2010-04-23 09:34:17 +00:00
|
|
|
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
|
2009-07-10 10:40:04 +00:00
|
|
|
int virCgroupForDriver(const char *name,
|
|
|
|
virCgroupPtr *group,
|
|
|
|
int privileged,
|
|
|
|
int create)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2009-07-10 10:40:04 +00:00
|
|
|
char *path = NULL;
|
2009-07-09 13:10:41 +00:00
|
|
|
virCgroupPtr rootgrp = NULL;
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2010-03-04 11:01:52 +00:00
|
|
|
rc = virCgroupAppRoot(privileged, &rootgrp, create);
|
2009-07-09 13:10:41 +00:00
|
|
|
if (rc != 0)
|
2008-10-03 17:58:02 +00:00
|
|
|
goto out;
|
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
if (virAsprintf(&path, "%s/%s", rootgrp->path, name) < 0) {
|
|
|
|
rc = -ENOMEM;
|
2009-07-09 13:10:41 +00:00
|
|
|
goto out;
|
2009-07-10 10:40:04 +00:00
|
|
|
}
|
2009-07-09 13:10:41 +00:00
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
rc = virCgroupNew(path, group);
|
|
|
|
VIR_FREE(path);
|
|
|
|
|
2009-11-12 14:11:43 +00:00
|
|
|
if (rc == 0) {
|
cgroup: Enable memory.use_hierarchy of cgroup for domain
Through conversation with Kumar L Srikanth-B22348, I found
that the function of getting memory usage (e.g., virsh dominfo)
doesn't work for lxc with ns subsystem of cgroup enabled.
This is because of features of ns and memory subsystems.
Ns creates child cgroup on every process fork and as a result
processes in a container are not assigned in a cgroup for
domain (e.g., libvirt/lxc/test1/). For example, libvirt_lxc
and init (or somewhat specified in XML) are assigned into
libvirt/lxc/test1/8839/ and libvirt/lxc/test1/8839/8849/,
respectively. On the other hand, memory subsystem accounts
memory usage within a group of processes by default, i.e.,
it does not take any child (and descendant) groups into
account. With the two features, virsh dominfo which just
checks memory usage of a cgroup for domain always returns
zero because the cgroup has no process.
Setting memory.use_hierarchy of a group allows to account
(and limit) memory usage of every descendant groups of the group.
By setting it of a cgroup for domain, we can get proper memory
usage of lxc with ns subsystem enabled. (To be exact, the
setting is required only when memory and ns subsystems are
enabled at the same time, e.g., mount -t cgroup none /cgroup.)
2010-06-23 16:00:56 +00:00
|
|
|
rc = virCgroupMakeGroup(rootgrp, *group, create, false);
|
2009-07-10 10:40:04 +00:00
|
|
|
if (rc != 0)
|
|
|
|
virCgroupFree(group);
|
|
|
|
}
|
2009-07-09 13:10:41 +00:00
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
out:
|
2009-07-09 13:10:41 +00:00
|
|
|
virCgroupFree(&rootgrp);
|
2008-10-03 17:58:02 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2009-10-07 10:18:31 +00:00
|
|
|
#else
|
|
|
|
int virCgroupForDriver(const char *name ATTRIBUTE_UNUSED,
|
|
|
|
virCgroupPtr *group ATTRIBUTE_UNUSED,
|
|
|
|
int privileged ATTRIBUTE_UNUSED,
|
|
|
|
int create ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
/* Claim no support */
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
#endif
|
2008-10-03 17:58:02 +00:00
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virCgroupForDomain:
|
|
|
|
*
|
|
|
|
* @driver: group for driver owning the domain
|
|
|
|
* @name: name of the domain
|
|
|
|
* @group: Pointer to returned virCgroupPtr
|
|
|
|
*
|
|
|
|
* Returns 0 on success
|
|
|
|
*/
|
2010-04-23 09:34:17 +00:00
|
|
|
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
|
2009-07-10 10:40:04 +00:00
|
|
|
int virCgroupForDomain(virCgroupPtr driver,
|
|
|
|
const char *name,
|
|
|
|
virCgroupPtr *group,
|
|
|
|
int create)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *path;
|
|
|
|
|
2010-03-22 15:42:14 +00:00
|
|
|
if (driver == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-07-10 10:40:04 +00:00
|
|
|
if (virAsprintf(&path, "%s/%s", driver->path, name) < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
rc = virCgroupNew(path, group);
|
|
|
|
VIR_FREE(path);
|
|
|
|
|
2009-11-12 14:11:43 +00:00
|
|
|
if (rc == 0) {
|
cgroup: Enable memory.use_hierarchy of cgroup for domain
Through conversation with Kumar L Srikanth-B22348, I found
that the function of getting memory usage (e.g., virsh dominfo)
doesn't work for lxc with ns subsystem of cgroup enabled.
This is because of features of ns and memory subsystems.
Ns creates child cgroup on every process fork and as a result
processes in a container are not assigned in a cgroup for
domain (e.g., libvirt/lxc/test1/). For example, libvirt_lxc
and init (or somewhat specified in XML) are assigned into
libvirt/lxc/test1/8839/ and libvirt/lxc/test1/8839/8849/,
respectively. On the other hand, memory subsystem accounts
memory usage within a group of processes by default, i.e.,
it does not take any child (and descendant) groups into
account. With the two features, virsh dominfo which just
checks memory usage of a cgroup for domain always returns
zero because the cgroup has no process.
Setting memory.use_hierarchy of a group allows to account
(and limit) memory usage of every descendant groups of the group.
By setting it of a cgroup for domain, we can get proper memory
usage of lxc with ns subsystem enabled. (To be exact, the
setting is required only when memory and ns subsystems are
enabled at the same time, e.g., mount -t cgroup none /cgroup.)
2010-06-23 16:00:56 +00:00
|
|
|
/*
|
|
|
|
* Create a cgroup with memory.use_hierarchy enabled to
|
|
|
|
* surely account memory usage of lxc with ns subsystem
|
|
|
|
* enabled. (To be exact, memory and ns subsystems are
|
|
|
|
* enabled at the same time.)
|
|
|
|
*
|
|
|
|
* The reason why doing it here, not a upper group, say
|
|
|
|
* a group for driver, is to avoid overhead to track
|
|
|
|
* cumulative usage that we don't need.
|
|
|
|
*/
|
|
|
|
rc = virCgroupMakeGroup(driver, *group, create, true);
|
2009-07-10 10:40:04 +00:00
|
|
|
if (rc != 0)
|
|
|
|
virCgroupFree(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2009-10-07 10:18:31 +00:00
|
|
|
#else
|
|
|
|
int virCgroupForDomain(virCgroupPtr driver ATTRIBUTE_UNUSED,
|
|
|
|
const char *name ATTRIBUTE_UNUSED,
|
|
|
|
virCgroupPtr *group ATTRIBUTE_UNUSED,
|
|
|
|
int create ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-10 10:40:04 +00:00
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
/**
|
|
|
|
* virCgroupSetMemory:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to change memory for
|
|
|
|
* @kb: The memory amount in kilobytes
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
|
|
|
int virCgroupSetMemory(virCgroupPtr group, unsigned long kb)
|
|
|
|
{
|
|
|
|
return virCgroupSetValueU64(group,
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
2008-10-03 17:58:02 +00:00
|
|
|
"memory.limit_in_bytes",
|
|
|
|
kb << 10);
|
|
|
|
}
|
|
|
|
|
2009-10-07 13:26:23 +00:00
|
|
|
/**
|
|
|
|
* virCgroupGetMemoryUsage:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to change memory for
|
|
|
|
* @kb: Pointer to returned used memory in kilobytes
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
|
|
|
int virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb)
|
|
|
|
{
|
2009-10-08 14:26:42 +00:00
|
|
|
long long unsigned int usage_in_bytes;
|
2009-10-07 13:26:23 +00:00
|
|
|
int ret;
|
|
|
|
ret = virCgroupGetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.usage_in_bytes", &usage_in_bytes);
|
|
|
|
if (ret == 0)
|
|
|
|
*kb = (unsigned long) usage_in_bytes >> 10;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-03 17:58:02 +00:00
|
|
|
/**
|
|
|
|
* virCgroupDenyAllDevices:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to deny devices for
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
|
|
|
int virCgroupDenyAllDevices(virCgroupPtr group)
|
|
|
|
{
|
|
|
|
return virCgroupSetValueStr(group,
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_CGROUP_CONTROLLER_DEVICES,
|
|
|
|
"devices.deny",
|
|
|
|
"a");
|
2008-10-03 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virCgroupAllowDevice:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to allow a device for
|
|
|
|
* @type: The device type (i.e., 'c' or 'b')
|
|
|
|
* @major: The major number of the device
|
|
|
|
* @minor: The minor number of the device
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
2009-07-09 13:11:49 +00:00
|
|
|
int virCgroupAllowDevice(virCgroupPtr group, char type, int major, int minor)
|
2008-10-03 17:58:02 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *devstr = NULL;
|
|
|
|
|
2008-12-23 13:03:29 +00:00
|
|
|
if (virAsprintf(&devstr, "%c %i:%i rwm", type, major, minor) == -1) {
|
2008-10-03 17:58:02 +00:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = virCgroupSetValueStr(group,
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_CGROUP_CONTROLLER_DEVICES,
|
2008-10-03 17:58:02 +00:00
|
|
|
"devices.allow",
|
|
|
|
devstr);
|
|
|
|
out:
|
|
|
|
VIR_FREE(devstr);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2008-10-08 16:28:48 +00:00
|
|
|
|
2008-10-21 16:46:47 +00:00
|
|
|
/**
|
|
|
|
* virCgroupAllowDeviceMajor:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to allow an entire device major type for
|
|
|
|
* @type: The device type (i.e., 'c' or 'b')
|
|
|
|
* @major: The major number of the device type
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
2009-07-09 13:11:49 +00:00
|
|
|
int virCgroupAllowDeviceMajor(virCgroupPtr group, char type, int major)
|
2008-10-21 16:46:47 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *devstr = NULL;
|
|
|
|
|
2008-12-23 13:03:29 +00:00
|
|
|
if (virAsprintf(&devstr, "%c %i:* rwm", type, major) == -1) {
|
2008-10-21 16:46:47 +00:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = virCgroupSetValueStr(group,
|
2009-07-09 13:10:41 +00:00
|
|
|
VIR_CGROUP_CONTROLLER_DEVICES,
|
2008-10-21 16:46:47 +00:00
|
|
|
"devices.allow",
|
|
|
|
devstr);
|
|
|
|
out:
|
|
|
|
VIR_FREE(devstr);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-07-09 13:11:49 +00:00
|
|
|
/**
|
|
|
|
* virCgroupAllowDevicePath:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to allow the device for
|
|
|
|
* @path: the device to allow
|
|
|
|
*
|
|
|
|
* Queries the type of device and its major/minor number, and
|
|
|
|
* adds that to the cgroup ACL
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
2009-10-07 10:18:31 +00:00
|
|
|
#if defined(major) && defined(minor)
|
2009-07-09 13:11:49 +00:00
|
|
|
int virCgroupAllowDevicePath(virCgroupPtr group, const char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(path, &sb) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return virCgroupAllowDevice(group,
|
|
|
|
S_ISCHR(sb.st_mode) ? 'c' : 'b',
|
|
|
|
major(sb.st_rdev),
|
|
|
|
minor(sb.st_rdev));
|
|
|
|
}
|
2009-10-07 10:18:31 +00:00
|
|
|
#else
|
|
|
|
int virCgroupAllowDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED,
|
|
|
|
const char *path ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-09 13:11:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virCgroupDenyDevice:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to deny a device for
|
|
|
|
* @type: The device type (i.e., 'c' or 'b')
|
|
|
|
* @major: The major number of the device
|
|
|
|
* @minor: The minor number of the device
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
|
|
|
int virCgroupDenyDevice(virCgroupPtr group, char type, int major, int minor)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *devstr = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&devstr, "%c %i:%i rwm", type, major, minor) == -1) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_DEVICES,
|
|
|
|
"devices.deny",
|
|
|
|
devstr);
|
|
|
|
out:
|
|
|
|
VIR_FREE(devstr);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virCgroupDenyDeviceMajor:
|
|
|
|
*
|
|
|
|
* @group: The cgroup to deny an entire device major type for
|
|
|
|
* @type: The device type (i.e., 'c' or 'b')
|
|
|
|
* @major: The major number of the device type
|
|
|
|
*
|
|
|
|
* Returns: 0 on success
|
|
|
|
*/
|
|
|
|
int virCgroupDenyDeviceMajor(virCgroupPtr group, char type, int major)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *devstr = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&devstr, "%c %i:* rwm", type, major) == -1) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_DEVICES,
|
|
|
|
"devices.deny",
|
|
|
|
devstr);
|
|
|
|
out:
|
|
|
|
VIR_FREE(devstr);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:18:31 +00:00
|
|
|
#if defined(major) && defined(minor)
|
2009-07-09 13:11:49 +00:00
|
|
|
int virCgroupDenyDevicePath(virCgroupPtr group, const char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(path, &sb) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return virCgroupDenyDevice(group,
|
|
|
|
S_ISCHR(sb.st_mode) ? 'c' : 'b',
|
|
|
|
major(sb.st_rdev),
|
|
|
|
minor(sb.st_rdev));
|
|
|
|
}
|
2009-10-07 10:18:31 +00:00
|
|
|
#else
|
|
|
|
int virCgroupDenyDevicePath(virCgroupPtr group ATTRIBUTE_UNUSED,
|
|
|
|
const char *path ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-09 13:11:49 +00:00
|
|
|
|
2009-07-09 13:11:21 +00:00
|
|
|
int virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares)
|
2008-10-08 16:28:48 +00:00
|
|
|
{
|
2009-07-09 13:10:41 +00:00
|
|
|
return virCgroupSetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
2009-10-07 10:18:31 +00:00
|
|
|
"cpu.shares", shares);
|
2008-10-08 16:28:48 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 13:11:21 +00:00
|
|
|
int virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares)
|
2008-10-08 16:28:48 +00:00
|
|
|
{
|
2009-07-09 13:10:41 +00:00
|
|
|
return virCgroupGetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
2009-10-07 10:18:31 +00:00
|
|
|
"cpu.shares", shares);
|
2008-10-08 16:28:48 +00:00
|
|
|
}
|
2009-03-06 14:44:04 +00:00
|
|
|
|
|
|
|
int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
|
|
|
|
{
|
2009-07-09 13:10:41 +00:00
|
|
|
return virCgroupGetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPUACCT,
|
2009-10-07 10:18:31 +00:00
|
|
|
"cpuacct.usage", usage);
|
2009-03-06 14:44:04 +00:00
|
|
|
}
|
2009-09-21 14:31:22 +00:00
|
|
|
|
|
|
|
int virCgroupSetFreezerState(virCgroupPtr group, const char *state)
|
|
|
|
{
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"freezer.state", state);
|
|
|
|
}
|
|
|
|
|
|
|
|
int virCgroupGetFreezerState(virCgroupPtr group, char **state)
|
|
|
|
{
|
2009-10-19 12:29:42 +00:00
|
|
|
return virCgroupGetValueStr(group,
|
2009-09-21 14:31:22 +00:00
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"freezer.state", state);
|
|
|
|
}
|