Merge pull request #30188 from YHNdnzj/memory-accounting-followup

core: a few fixes and improvements for memory accounting
This commit is contained in:
Yu Watanabe
2023-11-25 12:52:48 +09:00
committed by GitHub
3 changed files with 21 additions and 13 deletions

View File

@@ -4052,6 +4052,7 @@ int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uin
};
uint64_t bytes;
bool updated = false;
int r;
assert(u);
@@ -4062,7 +4063,8 @@ int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uin
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))
@@ -4078,19 +4080,22 @@ int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uin
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;

View File

@@ -1075,7 +1075,7 @@ static int property_get_current_memory(
r = unit_get_memory_current(u, &sz);
if (r < 0 && r != -ENODATA)
log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m");
log_unit_warning_errno(u, r, "Failed to get current memory usage from cgroup: %m");
return sd_bus_message_append(reply, "t", sz);
}

View File

@@ -158,11 +158,14 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b
(STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
(STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
(STR_IN_SET(name, "MemoryCurrent", "MemoryAvailable", "TasksCurrent") && u == UINT64_MAX) ||
(startswith(name, "Memory") && ENDSWITH_SET(name, "Current", "Peak") && u == CGROUP_LIMIT_MAX) ||
(startswith(name, "IO") && ENDSWITH_SET(name, "Bytes", "Operations") && u == UINT64_MAX) ||
(endswith(name, "NSec") && u == UINT64_MAX))
bus_print_property_value(name, expected_value, flags, "[not set]");
else if ((STR_IN_SET(name, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryZSwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
else if ((ENDSWITH_SET(name, "MemoryLow", "MemoryMin", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryZSwapMax", "MemoryLimit") &&
u == CGROUP_LIMIT_MAX) ||
(STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == UINT64_MAX) ||
(startswith(name, "Limit") && u == UINT64_MAX) ||
(startswith(name, "DefaultLimit") && u == UINT64_MAX))