Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(rebase) Add support for /sys/devices/system/node and subfiles #632

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ such as:
/proc/uptime
/proc/slabinfo
/sys/devices/system/cpu/online
/sys/devices/system/node
/sys/devices/system/node/online
/sys/devices/system/node/has_cpu
/sys/devices/system/node/has_memory
/sys/devices/system/node/has_normal_memory
```

are container aware such that the values displayed (e.g. in `/proc/uptime`)
Expand Down
16 changes: 16 additions & 0 deletions src/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ enum lxcfs_virt_t {

LXC_TYPE_SYS_DEVICES_SYSTEM_CPU_ONLINE,
#define LXC_TYPE_SYS_DEVICES_SYSTEM_CPU_ONLINE_PATH "/sys/devices/system/cpu/online"

LXC_TYPE_SYS_DEVICES_SYSTEM_NODE,
LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_SUBDIR,
LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_SUBFILE,

LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_ONLINE,
#define LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_ONLINE_PATH "/sys/devices/system/node/online"
LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_HAS_CPU,
#define LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_HAS_CPU_PATH "/sys/devices/system/node/has_cpu"
LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_HAS_MEMORY,
#define LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_HAS_MEMORY_PATH "/sys/devices/system/node/has_memory"
LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_HAS_NORMAL_MEMORY,
#define LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_HAS_NORMAL_MEMORY_PATH "/sys/devices/system/node/has_normal_memory"

LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_NODEX_CPULIST,
LXC_TYPE_SYS_DEVICES_SYSTEM_NODE_NODEX_CPUMAP,
LXC_TYPE_MAX,
};

Expand Down
70 changes: 70 additions & 0 deletions src/cgroups/cgfsng.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,22 @@ static char *readat_cpuset(int cgroup_fd)
return NULL;
}

static char *readat_cpuset_mems(int cgroup_fd)
{
__do_free char *val = NULL;

val = readat_file(cgroup_fd, "cpuset.mems");
if (val && strcmp(val, "") != 0)
return move_ptr(val);

free_disarm(val);
val = readat_file(cgroup_fd, "cpuset.mems.effective");
if (val && strcmp(val, "") != 0)
return move_ptr(val);

return NULL;
}

static int cgfsng_get_cpuset_cpus(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
Expand Down Expand Up @@ -736,6 +752,59 @@ static int cgfsng_get_cpuset_cpus(struct cgroup_ops *ops, const char *cgroup,
return -1;
}

static int cgfsng_get_cpuset_mems(struct cgroup_ops *ops, const char *cgroup,
char **value)
{
__do_close int cgroup_fd = -EBADF;
__do_free char *path = NULL;
char *v;
struct hierarchy *h;
int ret;

h = ops->get_hierarchy(ops, "cpuset");
if (!h)
return -1;

if (!is_unified_hierarchy(h))
ret = CGROUP_SUPER_MAGIC;
else
ret = CGROUP2_SUPER_MAGIC;

*value = NULL;
path = must_make_path_relative(cgroup, NULL);
cgroup_fd = openat_safe(h->fd, path);
if (cgroup_fd < 0)
return -1;

v = readat_cpuset_mems(cgroup_fd);
if (v) {
*value = v;
return ret;
}

/*
* cpuset.cpus and cpuset.cpus.effective are empty so we need to look
* the nearest ancestor with a non-empty cpuset.cpus{.effective} file.
*/
for (;;) {
int fd;

fd = openat_safe(cgroup_fd, "../");
if (fd < 0 || !is_cgroup_fd(fd))
return -1;

close_prot_errno_replace(cgroup_fd, fd);

v = readat_cpuset_mems(fd);
if (v) {
*value = v;
return ret;
}
}

return -1;
}

static int cgfsng_get_io(struct cgroup_ops *ops, const char *cgroup,
const char *file, char **value)
{
Expand Down Expand Up @@ -1023,6 +1092,7 @@ struct cgroup_ops *cgfsng_ops_init(void)
/* cpuset */
cgfsng_ops->get_cpuset_cpus = cgfsng_get_cpuset_cpus;
cgfsng_ops->can_use_cpuview = cgfsng_can_use_cpuview;
cgfsng_ops->get_cpuset_mems = cgfsng_get_cpuset_mems;

/* blkio */
cgfsng_ops->get_io_service_bytes = cgfsng_get_io_service_bytes;
Expand Down
16 changes: 16 additions & 0 deletions src/cgroups/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,19 @@ char *get_cpuset(const char *cg)

return value;
}

/*
* Read the cpuset.mems for cg
* Return the answer in a newly allocated string which must be freed
*/
char *get_cpuset_mems(const char *cg)
{
char *value = NULL;
int ret;

ret = cgroup_ops->get_cpuset_mems(cgroup_ops, cg, &value);
if (ret < 0)
return NULL;

return value;
}
3 changes: 3 additions & 0 deletions src/cgroups/cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ struct cgroup_ops {
int (*get_cpuset_cpus)(struct cgroup_ops *ops, const char *cgroup,
char **value);
bool (*can_use_cpuview)(struct cgroup_ops *ops);
int (*get_cpuset_mems)(struct cgroup_ops *ops, const char *cgroup,
char **value);

/* io */
int (*get_io_service_bytes)(struct cgroup_ops *ops, const char *cgroup,
Expand Down Expand Up @@ -211,5 +213,6 @@ static inline int get_cgroup_fd(const char *controller)
extern char *get_pid_cgroup(pid_t pid, const char *contrl);

extern char *get_cpuset(const char *cg);
extern char *get_cpuset_mems(const char *cg);

#endif
Loading
Loading