From: Mike Yuan Date: Mon, 24 Mar 2025 15:47:09 +0000 (+0100) Subject: core/cgroup: fold unit_get_current_memory() into generic memory accounting helper X-Git-Tag: v258-rc1~906^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=702aa33935efb354c5b197b54dcdf583b507d1be;p=thirdparty%2Fsystemd.git core/cgroup: fold unit_get_current_memory() into generic memory accounting helper --- diff --git a/src/core/cgroup.c b/src/core/cgroup.c index afb53f26c32..fb913f7c90e 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -4505,7 +4505,7 @@ int unit_get_memory_available(Unit *u, uint64_t *ret) { if (!crt || !crt->cgroup_path) continue; - (void) unit_get_memory_current(u, ¤t); + (void) unit_get_memory_accounting(u, CGROUP_MEMORY_CURRENT, ¤t); /* in case of error, previous current propagates as lower bound */ if (unit_has_name(u, SPECIAL_ROOT_SLICE)) @@ -4523,38 +4523,10 @@ int unit_get_memory_available(Unit *u, uint64_t *ret) { return 0; } -int unit_get_memory_current(Unit *u, uint64_t *ret) { - int r; - - // FIXME: Merge this into unit_get_memory_accounting after support for cgroup v1 is dropped - - assert(u); - assert(ret); - - if (!UNIT_CGROUP_BOOL(u, memory_accounting)) - return -ENODATA; - - CGroupRuntime *crt = unit_get_cgroup_runtime(u); - if (!crt || !crt->cgroup_path) - return -ENODATA; - - /* The root cgroup doesn't expose this information, let's get it from /proc instead */ - if (unit_has_host_root_cgroup(u)) - return procfs_memory_get_used(ret); - - if ((crt->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0) - return -ENODATA; - - r = cg_all_unified(); - if (r < 0) - return r; - - return cg_get_attribute_as_uint64("memory", crt->cgroup_path, r > 0 ? "memory.current" : "memory.usage_in_bytes", ret); -} - int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uint64_t *ret) { static const char* const attributes_table[_CGROUP_MEMORY_ACCOUNTING_METRIC_MAX] = { + [CGROUP_MEMORY_CURRENT] = "memory.current", [CGROUP_MEMORY_PEAK] = "memory.peak", [CGROUP_MEMORY_SWAP_CURRENT] = "memory.swap.current", [CGROUP_MEMORY_SWAP_PEAK] = "memory.swap.peak", @@ -4573,8 +4545,13 @@ int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uin return -ENODATA; /* The root cgroup doesn't expose this information. */ - if (unit_has_host_root_cgroup(u)) + if (unit_has_host_root_cgroup(u)) { + /* System-wide memory usage can be acquired from /proc/ */ + if (metric == CGROUP_MEMORY_CURRENT) + return procfs_memory_get_used(ret); + return -ENODATA; + } CGroupRuntime *crt = unit_get_cgroup_runtime(u); if (!crt) @@ -4586,12 +4563,6 @@ int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uin if (!FLAGS_SET(crt->cgroup_realized_mask, CGROUP_MASK_MEMORY)) return -ENODATA; - r = cg_all_unified(); - if (r < 0) - return r; - if (r == 0) - return -ENODATA; - r = cg_get_attribute_as_uint64("memory", crt->cgroup_path, attributes_table[metric], &bytes); if (r < 0 && r != -ENODATA) return r; @@ -5686,6 +5657,7 @@ static const char* const cgroup_io_accounting_metric_table[_CGROUP_IO_ACCOUNTING DEFINE_STRING_TABLE_LOOKUP(cgroup_io_accounting_metric, CGroupIOAccountingMetric); static const char* const cgroup_memory_accounting_metric_table[_CGROUP_MEMORY_ACCOUNTING_METRIC_MAX] = { + [CGROUP_MEMORY_CURRENT] = "MemoryCurrent", [CGROUP_MEMORY_PEAK] = "MemoryPeak", [CGROUP_MEMORY_SWAP_CURRENT] = "MemorySwapCurrent", [CGROUP_MEMORY_SWAP_PEAK] = "MemorySwapPeak", diff --git a/src/core/cgroup.h b/src/core/cgroup.h index b25cdad7d86..7b3fbc3b150 100644 --- a/src/core/cgroup.h +++ b/src/core/cgroup.h @@ -280,6 +280,7 @@ typedef enum CGroupMemoryAccountingMetric { _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST = CGROUP_MEMORY_SWAP_PEAK, /* These attributes are transient, so no need for caching. */ + CGROUP_MEMORY_CURRENT, CGROUP_MEMORY_SWAP_CURRENT, CGROUP_MEMORY_ZSWAP_CURRENT, @@ -478,7 +479,6 @@ int unit_watch_all_pids(Unit *u); int unit_synthesize_cgroup_empty_event(Unit *u); int unit_get_memory_available(Unit *u, uint64_t *ret); -int unit_get_memory_current(Unit *u, uint64_t *ret); int unit_get_memory_accounting(Unit *u, CGroupMemoryAccountingMetric metric, uint64_t *ret); int unit_get_tasks_current(Unit *u, uint64_t *ret); int unit_get_cpu_usage(Unit *u, nsec_t *ret); diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index 68cfed9444f..e9a2a4a4c09 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -1052,29 +1052,6 @@ static int property_get_slice( return sd_bus_message_append(reply, "s", unit_slice_name(u)); } -static int property_get_current_memory( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - uint64_t sz = UINT64_MAX; - Unit *u = ASSERT_PTR(userdata); - int r; - - assert(bus); - assert(reply); - - r = unit_get_memory_current(u, &sz); - if (r < 0 && r != -ENODATA) - log_unit_warning_errno(u, r, "Failed to get current memory usage from cgroup: %m"); - - return sd_bus_message_append(reply, "t", sz); -} - static int property_get_available_memory( sd_bus *bus, const char *path, @@ -1652,7 +1629,7 @@ const sd_bus_vtable bus_unit_cgroup_vtable[] = { SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0), SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0), SD_BUS_PROPERTY("ControlGroupId", "t", property_get_cgroup_id, 0, 0), - SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0), + SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_memory_accounting, 0, 0), SD_BUS_PROPERTY("MemoryPeak", "t", property_get_memory_accounting, 0, 0), SD_BUS_PROPERTY("MemorySwapCurrent", "t", property_get_memory_accounting, 0, 0), SD_BUS_PROPERTY("MemorySwapPeak", "t", property_get_memory_accounting, 0, 0),