return r;
}
+int cg_get_keyed_attribute_uint64(const char *path, const char *attribute, const char *key, uint64_t *ret) {
+ _cleanup_free_ char *val = NULL;
+ int r;
+
+ assert(key);
+ assert(ret);
+
+ r = cg_get_keyed_attribute(path, attribute, STRV_MAKE(key), &val);
+ if (r < 0)
+ return r;
+
+ r = safe_atou64(val, ret);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to parse value '%s' of key '%s' in cgroup attribute '%s': %m", val, key, attribute);
+
+ return 0;
+}
+
int cg_mask_to_string(CGroupMask mask, char **ret) {
_cleanup_free_ char *s = NULL;
bool space = false;
int cg_get_attribute_as_bool(const char *path, const char *attribute);
int cg_get_keyed_attribute(const char *path, const char *attribute, char * const *keys, char **values);
+int cg_get_keyed_attribute_uint64(const char *path, const char *attribute, const char *key, uint64_t *ret);
int cg_get_owner(const char *path, uid_t *ret_uid);
if (r < 0)
return r;
} else {
- _cleanup_free_ char *val = NULL;
uint64_t u;
- r = cg_get_keyed_attribute(g->path, "cpu.stat", STRV_MAKE("usage_usec"), &val);
+ r = cg_get_keyed_attribute_uint64(g->path, "cpu.stat", "usage_usec", &u);
if (IN_SET(r, -ENOENT, -ENXIO))
return 0;
if (r < 0)
return r;
- r = safe_atou64(val, &u);
- if (r < 0)
- return r;
-
new_usage = u * NSEC_PER_USEC;
}
}
int unit_check_oom(Unit *u) {
- _cleanup_free_ char *oom_kill = NULL;
bool increased;
- uint64_t c;
+ uint64_t c = 0;
int r;
CGroupRuntime *crt = unit_get_cgroup_runtime(u);
* back to reading oom_kill if we can't find the file or field. */
if (ctx->memory_oom_group) {
- r = cg_get_keyed_attribute(
+ r = cg_get_keyed_attribute_uint64(
crt->cgroup_path,
"memory.events.local",
- STRV_MAKE("oom_group_kill"),
- &oom_kill);
+ "oom_group_kill",
+ &c);
if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO))
return log_unit_debug_errno(u, r, "Failed to read oom_group_kill field of memory.events.local cgroup attribute, ignoring: %m");
}
- if (isempty(oom_kill)) {
- r = cg_get_keyed_attribute(
+ if (!ctx->memory_oom_group || r < 0) {
+ r = cg_get_keyed_attribute_uint64(
crt->cgroup_path,
"memory.events",
- STRV_MAKE("oom_kill"),
- &oom_kill);
+ "oom_kill",
+ &c);
if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO))
return log_unit_debug_errno(u, r, "Failed to read oom_kill field of memory.events cgroup attribute: %m");
}
- if (!oom_kill)
- c = 0;
- else {
- r = safe_atou64(oom_kill, &c);
- if (r < 0)
- return log_unit_debug_errno(u, r, "Failed to parse memory.events cgroup oom field: %m");
- }
-
increased = c > crt->oom_kill_last;
crt->oom_kill_last = c;
if (unit_has_host_root_cgroup(u))
return procfs_cpu_get_usage(ret);
- _cleanup_free_ char *val = NULL;
uint64_t us;
- r = cg_get_keyed_attribute(crt->cgroup_path, "cpu.stat", STRV_MAKE("usage_usec"), &val);
- if (r < 0)
- return r;
-
- r = safe_atou64(val, &us);
+ r = cg_get_keyed_attribute_uint64(crt->cgroup_path, "cpu.stat", "usage_usec", &us);
if (r < 0)
return r;
int oomd_cgroup_context_acquire(const char *path, OomdCGroupContext **ret) {
_cleanup_(oomd_cgroup_context_unrefp) OomdCGroupContext *ctx = NULL;
- _cleanup_free_ char *p = NULL, *val = NULL;
+ _cleanup_free_ char *p = NULL;
bool is_root;
int r;
else if (r < 0)
return log_debug_errno(r, "Error getting memory.swap.current from %s: %m", path);
- r = cg_get_keyed_attribute(path, "memory.stat", STRV_MAKE("pgscan"), &val);
+ r = cg_get_keyed_attribute_uint64(path, "memory.stat", "pgscan", &ctx->pgscan);
if (r < 0)
return log_debug_errno(r, "Error getting pgscan from memory.stat under %s: %m", path);
-
- r = safe_atou64(val, &ctx->pgscan);
- if (r < 0)
- return log_debug_errno(r, "Error converting pgscan value to uint64_t: %m");
}
*ret = TAKE_PTR(ctx);