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