mirror of
https://github.com/systemd/systemd.git
synced 2024-12-23 21:35:11 +03:00
Merge pull request #15209 from anitazha/moar-cgroup-utils
cgroup-utils: convenience helpers
This commit is contained in:
commit
ce4c73eb5d
@ -37,6 +37,7 @@
|
||||
#include "strv.h"
|
||||
#include "unit-name.h"
|
||||
#include "user-util.h"
|
||||
#include "xattr-util.h"
|
||||
|
||||
static int cg_enumerate_items(const char *controller, const char *path, FILE **_f, const char *item) {
|
||||
_cleanup_free_ char *fs = NULL;
|
||||
@ -605,6 +606,24 @@ int cg_get_xattr(const char *controller, const char *path, const char *name, voi
|
||||
return (int) n;
|
||||
}
|
||||
|
||||
int cg_get_xattr_malloc(const char *controller, const char *path, const char *name, char **ret) {
|
||||
_cleanup_free_ char *fs = NULL;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(name);
|
||||
|
||||
r = cg_get_path(controller, path, NULL, &fs);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = getxattr_malloc(fs, name, ret, false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int cg_remove_xattr(const char *controller, const char *path, const char *name) {
|
||||
_cleanup_free_ char *fs = NULL;
|
||||
int r;
|
||||
@ -1639,6 +1658,32 @@ int cg_get_attribute(const char *controller, const char *path, const char *attri
|
||||
return read_one_line_file(p, ret);
|
||||
}
|
||||
|
||||
int cg_get_attribute_as_uint64(const char *controller, const char *path, const char *attribute, uint64_t *ret) {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
uint64_t v;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
|
||||
r = cg_get_attribute(controller, path, attribute, &value);
|
||||
if (r == -ENOENT)
|
||||
return -ENODATA;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (streq(value, "max")) {
|
||||
*ret = CGROUP_LIMIT_MAX;
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = safe_atou64(value, &v);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cg_get_keyed_attribute(
|
||||
const char *controller,
|
||||
const char *path,
|
||||
|
@ -184,10 +184,13 @@ int cg_set_attribute(const char *controller, const char *path, const char *attri
|
||||
int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret);
|
||||
int cg_get_keyed_attribute(const char *controller, const char *path, const char *attribute, char **keys, char **values);
|
||||
|
||||
int cg_get_attribute_as_uint64(const char *controller, const char *path, const char *attribute, uint64_t *ret);
|
||||
|
||||
int cg_set_access(const char *controller, const char *path, uid_t uid, gid_t gid);
|
||||
|
||||
int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags);
|
||||
int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size);
|
||||
int cg_get_xattr_malloc(const char *controller, const char *path, const char *name, char **ret);
|
||||
int cg_remove_xattr(const char *controller, const char *path, const char *name);
|
||||
|
||||
int cg_install_release_agent(const char *controller, const char *agent);
|
||||
|
@ -120,16 +120,9 @@ uint64_t system_tasks_max(void) {
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to determine cgroup root path, ignoring: %m");
|
||||
else {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
|
||||
r = cg_get_attribute("pids", root, "pids.max", &value);
|
||||
r = cg_get_attribute_as_uint64("pids", root, "pids.max", &b);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to read pids.max attribute of cgroup root, ignoring: %m");
|
||||
else if (!streq(value, "max")) {
|
||||
r = safe_atou64(value, &b);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to parse pids.max attribute of cgroup root, ignoring: %m");
|
||||
}
|
||||
}
|
||||
|
||||
return MIN3(TASKS_MAX,
|
||||
|
@ -215,31 +215,12 @@ void cgroup_context_done(CGroupContext *c) {
|
||||
}
|
||||
|
||||
static int unit_get_kernel_memory_limit(Unit *u, const char *file, uint64_t *ret) {
|
||||
_cleanup_free_ char *raw_kval = NULL;
|
||||
uint64_t kval;
|
||||
int r;
|
||||
|
||||
assert(u);
|
||||
|
||||
if (!u->cgroup_realized)
|
||||
return -EOWNERDEAD;
|
||||
|
||||
r = cg_get_attribute("memory", u->cgroup_path, file, &raw_kval);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (streq(raw_kval, "max")) {
|
||||
*ret = CGROUP_LIMIT_MAX;
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = safe_atou64(raw_kval, &kval);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = kval;
|
||||
|
||||
return 0;
|
||||
return cg_get_attribute_as_uint64("memory", u->cgroup_path, file, ret);
|
||||
}
|
||||
|
||||
static int unit_compare_memory_limit(Unit *u, const char *property_name, uint64_t *ret_unit_value, uint64_t *ret_kernel_value) {
|
||||
@ -3112,7 +3093,6 @@ int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
|
||||
}
|
||||
|
||||
int unit_get_memory_current(Unit *u, uint64_t *ret) {
|
||||
_cleanup_free_ char *v = NULL;
|
||||
int r;
|
||||
|
||||
assert(u);
|
||||
@ -3134,22 +3114,11 @@ int unit_get_memory_current(Unit *u, uint64_t *ret) {
|
||||
r = cg_all_unified();
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0)
|
||||
r = cg_get_attribute("memory", u->cgroup_path, "memory.current", &v);
|
||||
else
|
||||
r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v);
|
||||
if (r == -ENOENT)
|
||||
return -ENODATA;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return safe_atou64(v, ret);
|
||||
return cg_get_attribute_as_uint64("memory", u->cgroup_path, r > 0 ? "memory.current" : "memory.usage_in_bytes", ret);
|
||||
}
|
||||
|
||||
int unit_get_tasks_current(Unit *u, uint64_t *ret) {
|
||||
_cleanup_free_ char *v = NULL;
|
||||
int r;
|
||||
|
||||
assert(u);
|
||||
assert(ret);
|
||||
|
||||
@ -3166,17 +3135,10 @@ int unit_get_tasks_current(Unit *u, uint64_t *ret) {
|
||||
if ((u->cgroup_realized_mask & CGROUP_MASK_PIDS) == 0)
|
||||
return -ENODATA;
|
||||
|
||||
r = cg_get_attribute("pids", u->cgroup_path, "pids.current", &v);
|
||||
if (r == -ENOENT)
|
||||
return -ENODATA;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return safe_atou64(v, ret);
|
||||
return cg_get_attribute_as_uint64("pids", u->cgroup_path, "pids.current", ret);
|
||||
}
|
||||
|
||||
static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
|
||||
_cleanup_free_ char *v = NULL;
|
||||
uint64_t ns;
|
||||
int r;
|
||||
|
||||
@ -3212,17 +3174,8 @@ static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) {
|
||||
return r;
|
||||
|
||||
ns = us * NSEC_PER_USEC;
|
||||
} else {
|
||||
r = cg_get_attribute("cpuacct", u->cgroup_path, "cpuacct.usage", &v);
|
||||
if (r == -ENOENT)
|
||||
return -ENODATA;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = safe_atou64(v, &ns);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
} else
|
||||
return cg_get_attribute_as_uint64("cpuacct", u->cgroup_path, "cpuacct.usage", ret);
|
||||
|
||||
*ret = ns;
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user