[lxc-devel] [lxcfs/master] cgroups: fixes and improvements
brauner on Github
lxc-bot at linuxcontainers.org
Tue Feb 25 12:34:32 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 365 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200225/e82fe725/attachment.bin>
-------------- next part --------------
From 0646f250d5dc7af1732b53beb9081c2d27cba662 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 25 Feb 2020 13:11:23 +0100
Subject: [PATCH 1/3] cgroups: move mntns fd into struct cgroup_ops
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
bindings.c | 11 ++++-------
cgroups/cgroup.c | 4 ++++
cgroups/cgroup.h | 6 ++++++
3 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/bindings.c b/bindings.c
index 4e2848c..390ae5d 100644
--- a/bindings.c
+++ b/bindings.c
@@ -414,8 +414,6 @@ static void lock_mutex(pthread_mutex_t *l)
struct cgroup_ops *cgroup_ops;
-static int cgroup_mount_ns_fd = -1;
-
static void unlock_mutex(pthread_mutex_t *l)
{
int ret;
@@ -701,7 +699,7 @@ static void print_subsystems(void)
{
int i = 0;
- fprintf(stderr, "mount namespace: %d\n", cgroup_mount_ns_fd);
+ fprintf(stderr, "mount namespace: %d\n", cgroup_ops->mntns_fd);
fprintf(stderr, "hierarchies:\n");
for (struct hierarchy **h = cgroup_ops->hierarchies; h && *h; h++, i++) {
__do_free char *controllers = lxc_string_join(",", (const char **)(*h)->controllers, false);
@@ -6056,8 +6054,8 @@ static bool cgfs_prepare_mounts(void)
return false;
}
- cgroup_mount_ns_fd = preserve_mnt_ns(getpid());
- if (cgroup_mount_ns_fd < 0) {
+ cgroup_ops->mntns_fd = preserve_mnt_ns(getpid());
+ if (cgroup_ops->mntns_fd < 0) {
lxcfs_error("Failed to preserve mount namespace: %s.\n", strerror(errno));
return false;
}
@@ -6146,8 +6144,7 @@ static void __attribute__((constructor)) lxcfs_init(void)
static void __attribute__((destructor)) lxcfs_exit(void)
{
- lxcfs_debug("%s\n", "Running destructor for liblxcfs.");
+ lxcfs_debug("%s\n", "Running destructor for liblxcfs");
free_cpuview();
- close_prot_errno_disarm(cgroup_mount_ns_fd);
cgroup_exit(cgroup_ops);
}
diff --git a/cgroups/cgroup.c b/cgroups/cgroup.c
index aebafbd..34e9a14 100644
--- a/cgroups/cgroup.c
+++ b/cgroups/cgroup.c
@@ -51,6 +51,10 @@ void cgroup_exit(struct cgroup_ops *ops)
free((*it)->monitor_full_path);
free(*it);
}
+
+ if (ops->mntns_fd >= 0)
+ close(ops->mntns_fd);
+
free(ops->hierarchies);
free(ops);
diff --git a/cgroups/cgroup.h b/cgroups/cgroup.h
index e955e75..d200000 100644
--- a/cgroups/cgroup.h
+++ b/cgroups/cgroup.h
@@ -74,6 +74,12 @@ struct hierarchy {
};
struct cgroup_ops {
+ /*
+ * File descriptor of the mount namespace the cgroup hierarchies are
+ * mounted in.
+ */
+ int mntns_fd;
+
/* string constant */
const char *driver;
From 6a9d2ce91bb44a43ca75ce08369bb453f3e09d47 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 25 Feb 2020 13:27:30 +0100
Subject: [PATCH 2/3] cgroups: remove ->nrtasks method
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
cgroups/cgfsng.c | 52 ------------------------------------------------
cgroups/cgroup.h | 1 -
2 files changed, 53 deletions(-)
diff --git a/cgroups/cgfsng.c b/cgroups/cgfsng.c
index 8d972b7..05982e4 100644
--- a/cgroups/cgfsng.c
+++ b/cgroups/cgfsng.c
@@ -501,57 +501,6 @@ static bool cgfsng_mount(struct cgroup_ops *ops, const char *root)
return retval;
}
-static int recursive_count_nrtasks(char *dirname)
-{
- __do_free char *path = NULL;
- __do_closedir DIR *dir = NULL;
- struct dirent *direntp;
- int count = 0, ret;
-
- dir = opendir(dirname);
- if (!dir)
- return 0;
-
- while ((direntp = readdir(dir))) {
- struct stat mystat;
-
- if (!strcmp(direntp->d_name, ".") ||
- !strcmp(direntp->d_name, ".."))
- continue;
-
- path = must_make_path(dirname, direntp->d_name, NULL);
-
- if (lstat(path, &mystat))
- continue;
-
- if (!S_ISDIR(mystat.st_mode))
- continue;
-
- count += recursive_count_nrtasks(path);
- }
-
- path = must_make_path(dirname, "cgroup.procs", NULL);
- ret = lxc_count_file_lines(path);
- if (ret != -1)
- count += ret;
-
- return count;
-}
-
-static int cgfsng_nrtasks(struct cgroup_ops *ops)
-{
- __do_free char *path = NULL;
-
- if (!ops)
- return ret_set_errno(-1, ENOENT);
-
- if (!ops->container_cgroup || !ops->hierarchies)
- return ret_set_errno(-1, EINVAL);
-
- path = must_make_path(ops->hierarchies[0]->container_full_path, NULL);
- return recursive_count_nrtasks(path);
-}
-
static int cgfsng_num_hierarchies(struct cgroup_ops *ops)
{
int i = 0;
@@ -1006,7 +955,6 @@ struct cgroup_ops *cgfsng_ops_init(void)
cgfsng_ops->driver = "cgfsng";
cgfsng_ops->version = "1.0.0";
cgfsng_ops->mount = cgfsng_mount;
- cgfsng_ops->nrtasks = cgfsng_nrtasks;
/* memory */
cgfsng_ops->get_memory_stats = cgfsng_get_memory_stats;
diff --git a/cgroups/cgroup.h b/cgroups/cgroup.h
index d200000..926130d 100644
--- a/cgroups/cgroup.h
+++ b/cgroups/cgroup.h
@@ -123,7 +123,6 @@ struct cgroup_ops {
int (*num_hierarchies)(struct cgroup_ops *ops);
bool (*get_hierarchies)(struct cgroup_ops *ops, int n, char ***out);
bool (*mount)(struct cgroup_ops *ops, const char *root);
- int (*nrtasks)(struct cgroup_ops *ops);
struct hierarchy *(*get_hierarchy)(struct cgroup_ops *ops,
const char *controller);
bool (*get)(struct cgroup_ops *ops, const char *controller,
From 0fd1b770c37f2e4ccb232029607a0eb57a60b721 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 25 Feb 2020 13:33:37 +0100
Subject: [PATCH 3/3] cgroups: remove unused variables
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
cgroups/cgfsng.c | 9 ++++-----
cgroups/cgroup.c | 15 +++------------
cgroups/cgroup.h | 8 +-------
3 files changed, 8 insertions(+), 24 deletions(-)
diff --git a/cgroups/cgfsng.c b/cgroups/cgfsng.c
index 05982e4..9708955 100644
--- a/cgroups/cgfsng.c
+++ b/cgroups/cgfsng.c
@@ -300,7 +300,7 @@ static char **cg_unified_get_controllers(const char *file)
}
static struct hierarchy *add_hierarchy(struct hierarchy ***h, char **clist, char *mountpoint,
- char *container_base_path, int type)
+ char *base_path, int type)
{
struct hierarchy *new;
int newentry;
@@ -308,7 +308,7 @@ static struct hierarchy *add_hierarchy(struct hierarchy ***h, char **clist, char
new = zalloc(sizeof(*new));
new->controllers = clist;
new->mountpoint = mountpoint;
- new->container_base_path = container_base_path;
+ new->base_path = base_path;
new->version = type;
newentry = append_null_to_list((void ***)h);
@@ -828,11 +828,10 @@ static int cg_hybrid_init(struct cgroup_ops *ops)
if (!mountpoint)
log_error_errno(goto next, EINVAL, "Failed parsing mountpoint from \"%s\"", line);
- if (type == CGROUP_SUPER_MAGIC) {
+ if (type == CGROUP_SUPER_MAGIC)
base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, controller_list[0], CGROUP_SUPER_MAGIC);
- } else {
+ else
base_cgroup = cg_hybrid_get_current_cgroup(basecginfo, NULL, CGROUP2_SUPER_MAGIC);
- }
if (!base_cgroup)
log_error_errno(goto next, EINVAL, "Failed to find current cgroup %s", mountpoint);
diff --git a/cgroups/cgroup.c b/cgroups/cgroup.c
index 34e9a14..9a5aa61 100644
--- a/cgroups/cgroup.c
+++ b/cgroups/cgroup.c
@@ -26,18 +26,11 @@ struct cgroup_ops *cgroup_init(void)
void cgroup_exit(struct cgroup_ops *ops)
{
- struct hierarchy **it;
-
if (!ops)
return;
- free(ops->container_cgroup);
- free(ops->monitor_cgroup);
-
- for (it = ops->hierarchies; it && *it; it++) {
- char **p;
-
- for (p = (*it)->controllers; p && *p; p++)
+ for (struct hierarchy **it = ops->hierarchies; it && *it; it++) {
+ for (char **p = (*it)->controllers; p && *p; p++)
free(*p);
free((*it)->controllers);
free((*it)->__controllers);
@@ -46,9 +39,7 @@ void cgroup_exit(struct cgroup_ops *ops)
close((*it)->fd);
free((*it)->mountpoint);
- free((*it)->container_base_path);
- free((*it)->container_full_path);
- free((*it)->monitor_full_path);
+ free((*it)->base_path);
free(*it);
}
diff --git a/cgroups/cgroup.h b/cgroups/cgroup.h
index 926130d..e84233b 100644
--- a/cgroups/cgroup.h
+++ b/cgroups/cgroup.h
@@ -63,9 +63,7 @@ struct hierarchy {
char **controllers;
char *__controllers;
char *mountpoint;
- char *container_base_path;
- char *container_full_path;
- char *monitor_full_path;
+ char *base_path;
int version;
/* cgroup2 only */
@@ -86,10 +84,6 @@ struct cgroup_ops {
/* string constant */
const char *version;
- /* What controllers is the container supposed to use. */
- char *container_cgroup;
- char *monitor_cgroup;
-
/* @hierarchies
* - A NULL-terminated array of struct hierarchy, one per legacy
* hierarchy. No duplicates. First sufficient, writeable mounted
More information about the lxc-devel
mailing list