From: Chris Down Date: Wed, 5 Feb 2020 12:12:52 +0000 (+0000) Subject: cgroup: systemctl: Don't display NULL if protection was set to max X-Git-Tag: v245-rc2~89 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0e2112f618450e8836836e71fe9b1b422a6d63e;p=thirdparty%2Fsystemd.git 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) --- diff --git a/src/basic/format-util.h b/src/basic/format-util.h index 29fbc947b3b..c47fa76ea8f 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 5547058296a..37febfa104e 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) {