From ad009380e1cbee8cf6e965f1a9b5671bd74ac146 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 9 Dec 2023 20:10:31 +0800 Subject: [PATCH 1/2] core/cgroup: cache the last memory usage values before destroying cgroup Currently, memory accounting values are only cached if it was queued at least once before destroying cgroup. Let's always cache it like what we already do for CPU usage. Preparation for later changes. --- src/core/cgroup.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 61ac4df1a6..5e66ef76b5 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -3131,7 +3131,11 @@ void unit_prune_cgroup(Unit *u) { if (!u->cgroup_path) return; - (void) unit_get_cpu_usage(u, NULL); /* Cache the last CPU usage value before we destroy the cgroup */ + /* Cache the last CPU and memory usage values before we destroy the cgroup */ + (void) unit_get_cpu_usage(u, /* ret = */ NULL); + + for (CGroupMemoryAccountingMetric metric = 0; metric <= _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST; metric++) + (void) unit_get_memory_accounting(u, metric, /* ret = */ NULL); #if BPF_FRAMEWORK (void) lsm_bpf_cleanup(u); /* Remove cgroup from the global LSM BPF map */ From 2747cd6d17039b6c384faacd3563716a14063509 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 9 Dec 2023 20:19:57 +0800 Subject: [PATCH 2/2] systemctl-show: always show memory peak if available --- src/systemctl/systemctl-show.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index 963ba69ab6..c74bcf2642 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -703,17 +703,19 @@ static void print_status_info( if (i->n_fd_store > 0 || i->fd_store_max > 0) printf(" FD Store: %u%s (limit: %u)%s\n", i->n_fd_store, ansi_grey(), i->fd_store_max, ansi_normal()); + bool show_memory_peak = i->memory_peak != CGROUP_LIMIT_MAX, + show_memory_swap_peak = !IN_SET(i->memory_swap_peak, 0, CGROUP_LIMIT_MAX); + if (i->memory_current != UINT64_MAX) { printf(" Memory: %s", FORMAT_BYTES(i->memory_current)); /* Only show current swap if it ever was non-zero or is currently non-zero. In both cases memory_swap_peak will be non-zero (and not CGROUP_LIMIT_MAX). Only show the available memory if it was artificially limited. */ - bool show_memory_swap = !IN_SET(i->memory_swap_peak, 0, CGROUP_LIMIT_MAX), - show_memory_zswap_current = !IN_SET(i->memory_zswap_current, 0, CGROUP_LIMIT_MAX), + bool show_memory_zswap_current = !IN_SET(i->memory_zswap_current, 0, CGROUP_LIMIT_MAX), show_memory_available = i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX; - if (i->memory_peak != CGROUP_LIMIT_MAX || - show_memory_swap || + if (show_memory_peak || + show_memory_swap_peak || show_memory_zswap_current || show_memory_available || i->memory_min > 0 || @@ -779,11 +781,11 @@ static void print_status_info( printf("%savailable: %s", prefix, FORMAT_BYTES(i->memory_available)); prefix = " "; } - if (i->memory_peak != CGROUP_LIMIT_MAX) { + if (show_memory_peak) { printf("%speak: %s", prefix, FORMAT_BYTES(i->memory_peak)); prefix = " "; } - if (show_memory_swap) { + if (show_memory_swap_peak) { printf("%sswap: %s swap peak: %s", prefix, FORMAT_BYTES(i->memory_swap_current), FORMAT_BYTES(i->memory_swap_peak)); prefix = " "; @@ -795,6 +797,14 @@ static void print_status_info( printf(")"); } printf("\n"); + + } else if (show_memory_peak) { + printf(" Mem peak: %s", FORMAT_BYTES(i->memory_peak)); + + if (show_memory_swap_peak) + printf(" (swap: %s)", FORMAT_BYTES(i->memory_swap_peak)); + + putchar('\n'); } if (i->cpu_usage_nsec != UINT64_MAX)