};
uint64_t bytes;
+ bool updated = false;
int r;
assert(u);
return -ENODATA;
if (!u->cgroup_path)
- return -ENODATA;
+ /* If the cgroup is already gone, we try to find the last cached value. */
+ goto cache;
/* The root cgroup doesn't expose this information. */
if (unit_has_host_root_cgroup(u))
return -ENODATA;
r = cg_get_attribute_as_uint64("memory", u->cgroup_path, attributes_table[metric], &bytes);
- if (r < 0 && (r != -ENODATA || metric > _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST))
+ if (r < 0 && r != -ENODATA)
return r;
+ updated = r >= 0;
- if (metric <= _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST) {
- uint64_t *last = &u->memory_accounting_last[metric];
+cache:
+ if (metric > _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST)
+ return -ENODATA;
- if (r >= 0)
- *last = bytes;
- else if (*last != UINT64_MAX)
- bytes = *last;
- else
- return r;
- }
+ uint64_t *last = &u->memory_accounting_last[metric];
+
+ if (updated)
+ *last = bytes;
+ else if (*last != UINT64_MAX)
+ bytes = *last;
+ else
+ return -ENODATA;
if (ret)
*ret = bytes;