ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
b9a53a
From cabd9055d0d745f7de9625dec6c623d363dd3aa6 Mon Sep 17 00:00:00 2001
b9a53a
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
b9a53a
Date: Wed, 29 May 2019 10:17:43 +0200
b9a53a
Subject: [PATCH] shared/cpu-set-util: only force range printing one time
b9a53a
MIME-Version: 1.0
b9a53a
Content-Type: text/plain; charset=UTF-8
b9a53a
Content-Transfer-Encoding: 8bit
b9a53a
b9a53a
The idea is to have at least one range to make the new format clearly
b9a53a
distinguishable from the old. But it is enough to just do it once.
b9a53a
In particular, in case the affinity would be specified like 0, 2, 4, 6…,
b9a53a
this gives much shorter output.
b9a53a
b9a53a
(cherry picked from commit 1f57a176af5152d05719bf43740e87a47e37af50)
b9a53a
b9a53a
Related: #1734787
b9a53a
---
b9a53a
 src/basic/cpu-set-util.c     | 10 ++++++++--
b9a53a
 src/test/test-cpu-set-util.c |  7 ++++---
b9a53a
 2 files changed, 12 insertions(+), 5 deletions(-)
b9a53a
b9a53a
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
b9a53a
index bff39ec143..5024290557 100644
b9a53a
--- a/src/basic/cpu-set-util.c
b9a53a
+++ b/src/basic/cpu-set-util.c
b9a53a
@@ -58,7 +58,10 @@ char *cpu_set_to_range_string(const CPUSet *set) {
b9a53a
                         if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
b9a53a
                                 return NULL;
b9a53a
 
b9a53a
-                        r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
b9a53a
+                        if (range_end > range_start || len == 0)
b9a53a
+                                r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
b9a53a
+                        else
b9a53a
+                                r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
b9a53a
                         assert_se(r > 0);
b9a53a
                         len += r;
b9a53a
                 }
b9a53a
@@ -67,7 +70,10 @@ char *cpu_set_to_range_string(const CPUSet *set) {
b9a53a
                 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
b9a53a
                         return NULL;
b9a53a
 
b9a53a
-                r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
b9a53a
+                if (range_end > range_start || len == 0)
b9a53a
+                        r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
b9a53a
+                else
b9a53a
+                        r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
b9a53a
                 assert_se(r > 0);
b9a53a
         }
b9a53a
 
b9a53a
diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c
b9a53a
index 0d2741cd43..995b981d25 100644
b9a53a
--- a/src/test/test-cpu-set-util.c
b9a53a
+++ b/src/test/test-cpu-set-util.c
b9a53a
@@ -31,19 +31,20 @@ static void test_parse_cpu_set(void) {
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Simple range (from CPUAffinity example) */
b9a53a
-        assert_se(parse_cpu_set_full("1 2", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
b9a53a
+        assert_se(parse_cpu_set_full("1 2 4", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
b9a53a
         assert_se(c.set);
b9a53a
         assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
b9a53a
         assert_se(CPU_ISSET_S(1, c.allocated, c.set));
b9a53a
         assert_se(CPU_ISSET_S(2, c.allocated, c.set));
b9a53a
-        assert_se(CPU_COUNT_S(c.allocated, c.set) == 2);
b9a53a
+        assert_se(CPU_ISSET_S(4, c.allocated, c.set));
b9a53a
+        assert_se(CPU_COUNT_S(c.allocated, c.set) == 3);
b9a53a
 
b9a53a
         assert_se(str = cpu_set_to_string(&c);;
b9a53a
         log_info("cpu_set_to_string: %s", str);
b9a53a
         str = mfree(str);
b9a53a
         assert_se(str = cpu_set_to_range_string(&c);;
b9a53a
         log_info("cpu_set_to_range_string: %s", str);
b9a53a
-        assert_se(streq(str, "1-2"));
b9a53a
+        assert_se(streq(str, "1-2 4"));
b9a53a
         str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a