cgroup: de-duplicate uni_get_memory_* code

This commit is contained in:
Florian Schmaus
2023-11-11 12:14:07 +01:00
parent aac3384e56
commit 991506ae5d
+35 -79
View File
@@ -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);
}
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(mem_attribute);
assert(last);
if (!UNIT_CGROUP_BOOL(u, memory_accounting))
return -ENODATA;
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 = *last;
return 0;
}
if (r < 0)
return r;
*last = bytes;
if (ret)
*ret = bytes;
return 0;
}
int unit_get_memory_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_peak_raw(u, &bytes);
if (r == -ENODATA && u->memory_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_peak_last;
return 0;
}
if (r < 0)
return r;
u->memory_peak_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);
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) {