From: Florian Schmaus Date: Thu, 9 Nov 2023 08:49:29 +0000 (+0100) Subject: cgroup: de-duplicate uni_get_memory_* code X-Git-Tag: v255-rc2~42^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=991506ae5dd691b8ecf214dc31e57e02bf8c8a4c;p=thirdparty%2Fsystemd.git cgroup: de-duplicate uni_get_memory_* code --- diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 43e38de78de..c81cc16202e 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -4040,10 +4040,11 @@ int unit_get_memory_current(Unit *u, uint64_t *ret) { return cg_get_attribute_as_uint64("memory", u->cgroup_path, r > 0 ? "memory.current" : "memory.usage_in_bytes", ret); } -static int unit_get_memory_peak_raw(Unit *u, uint64_t *ret) { +static int unit_get_memory_attr_raw(Unit *u, const char* mem_attribute, uint64_t *ret) { int r; assert(u); + assert(mem_attribute); assert(ret); if (!u->cgroup_path) @@ -4062,90 +4063,45 @@ static int unit_get_memory_peak_raw(Unit *u, uint64_t *ret) { if (!r) return -ENODATA; - return cg_get_attribute_as_uint64("memory", u->cgroup_path, "memory.peak", ret); + return cg_get_attribute_as_uint64("memory", u->cgroup_path, mem_attribute, ret); } -int unit_get_memory_peak(Unit *u, uint64_t *ret) { +static int unit_get_memory_attr_cached(Unit *u, const char* mem_attribute, uint64_t* last, uint64_t *ret) { uint64_t bytes; int r; assert(u); - assert(ret); + assert(mem_attribute); + assert(last); if (!UNIT_CGROUP_BOOL(u, memory_accounting)) return -ENODATA; - r = unit_get_memory_peak_raw(u, &bytes); - if (r == -ENODATA && u->memory_peak_last != UINT64_MAX) { + r = unit_get_memory_attr_raw(u, mem_attribute, &bytes); + if (r == -ENODATA && *last != UINT64_MAX) { /* If we can't get the memory peak anymore (because the cgroup was already removed, for example), * use our cached value. */ if (ret) - *ret = u->memory_peak_last; + *ret = *last; return 0; } if (r < 0) return r; - u->memory_peak_last = bytes; + *last = bytes; if (ret) *ret = bytes; return 0; } -static int unit_get_memory_swap_peak_raw(Unit *u, uint64_t *ret) { - int r; - - assert(u); - assert(ret); - - if (!u->cgroup_path) - return -ENODATA; - - /* The root cgroup doesn't expose this information. */ - if (unit_has_host_root_cgroup(u)) - return -ENODATA; - - if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0) - return -ENODATA; - - r = cg_all_unified(); - if (r < 0) - return r; - if (!r) - return -ENODATA; - - return cg_get_attribute_as_uint64("memory", u->cgroup_path, "memory.swap.peak", ret); +int unit_get_memory_peak(Unit *u, uint64_t *ret) { + return unit_get_memory_attr_cached(u, "memory.peak", &u->memory_peak_last, ret); } int unit_get_memory_swap_peak(Unit *u, uint64_t *ret) { - uint64_t bytes; - int r; - - assert(u); - assert(ret); - - if (!UNIT_CGROUP_BOOL(u, memory_accounting)) - return -ENODATA; - - r = unit_get_memory_swap_peak_raw(u, &bytes); - if (r == -ENODATA && u->memory_swap_peak_last != UINT64_MAX) { - /* If we can't get the memory peak anymore (because the cgroup was already removed, for example), - * use our cached value. */ - - if (ret) - *ret = u->memory_swap_peak_last; - return 0; - } - if (r < 0) - return r; - - u->memory_swap_peak_last = bytes; - if (ret) - *ret = bytes; - - return 0; + return unit_get_memory_attr_cached(u, "memory.swap.peak", &u->memory_swap_peak_last, ret); } int unit_get_tasks_current(Unit *u, uint64_t *ret) {