]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgroup: systemctl: Don't display NULL if protection was set to max
authorChris Down <chris@chrisdown.name>
Wed, 5 Feb 2020 12:12:52 +0000 (12:12 +0000)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 5 Feb 2020 17:32:33 +0000 (18:32 +0100)
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
src/systemctl/systemctl.c

index 29fbc947b3b67223306198b4c51966d4d8f39743..c47fa76ea8ff2e709d1b8d92b92fe0aed0609cf8 100644 (file)
@@ -5,6 +5,7 @@
 #include <net/if.h>
 #include <stdbool.h>
 
+#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);
+}
index 5547058296ac091be043231374be5d5259cfc5fc..37febfa104e11fe365ffc040603f842be5955b0d 100644 (file)
@@ -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) {