From e0e2112f618450e8836836e71fe9b1b422a6d63e Mon Sep 17 00:00:00 2001 From: Chris Down Date: Wed, 5 Feb 2020 12:12:52 +0000 Subject: [PATCH] cgroup: systemctl: Don't display NULL if protection was set to max Inside format_bytes, we return NULL if the value is UINT64_MAX. This makes some kind of sense where this has some other semantic meaning than being a value, but in this case the value is both a.) not the default (so we definitely want to display it), and b.) means "infinity" (or "max" in cgroup terminology). This patch adds a small wrapper around format_bytes that can be used for these cases, to avoid the following situation: [root@tangsanjiao ~]# cat /sys/fs/cgroup/workload.slice/memory.low max [root@tangsanjiao ~]# systemctl show workload.slice -p MemoryLow MemoryLow=infinity [root@tangsanjiao ~]# systemctl status workload.slice | grep low: Memory: 14.9G (low: (null)) After the patch: [root@tangsanjiao ~]# systemctl status workload.slice | grep low: Memory: 15.1G (low: infinity) --- src/basic/format-util.h | 10 +++++++++- src/systemctl/systemctl.c | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/basic/format-util.h b/src/basic/format-util.h index 29fbc947b3..c47fa76ea8 100644 --- a/src/basic/format-util.h +++ b/src/basic/format-util.h @@ -5,6 +5,7 @@ #include #include +#include "cgroup-util.h" #include "macro.h" assert_cc(sizeof(pid_t) == sizeof(int32_t)); @@ -71,8 +72,15 @@ typedef enum { FORMAT_BYTES_TRAILING_B = 1 << 2, } FormatBytesFlag; -#define FORMAT_BYTES_MAX 8 +#define FORMAT_BYTES_MAX 16 char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag); static inline char *format_bytes(char *buf, size_t l, uint64_t t) { return format_bytes_full(buf, l, t, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_BELOW_POINT | FORMAT_BYTES_TRAILING_B); } +static inline char *format_bytes_cgroup_protection(char *buf, size_t l, uint64_t t) { + if (t == CGROUP_LIMIT_MAX) { + (void) snprintf(buf, l, "%s", "infinity"); + return buf; + } + return format_bytes(buf, l, t); +} diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 5547058296..37febfa104 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -4478,11 +4478,11 @@ static void print_status_info( printf(" ("); if (i->memory_min > 0) { - printf("%smin: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_min)); + printf("%smin: %s", prefix, format_bytes_cgroup_protection(buf, sizeof(buf), i->memory_min)); prefix = " "; } if (i->memory_low > 0) { - printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low)); + printf("%slow: %s", prefix, format_bytes_cgroup_protection(buf, sizeof(buf), i->memory_low)); prefix = " "; } if (i->memory_high != CGROUP_LIMIT_MAX) { -- 2.25.1