From: Zbigniew Jędrzejewski-Szmek Date: Wed, 29 May 2019 08:17:43 +0000 (+0200) Subject: shared/cpu-set-util: only force range printing one time X-Git-Tag: v243-rc1~340^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1f57a176af5152d05719bf43740e87a47e37af50;p=thirdparty%2Fsystemd.git shared/cpu-set-util: only force range printing one time The idea is to have at least one range to make the new format clearly distinguishable from the old. But it is enough to just do it once. In particular, in case the affinity would be specified like 0, 2, 4, 6…, this gives much shorter output. --- diff --git a/src/shared/cpu-set-util.c b/src/shared/cpu-set-util.c index 13a43dc6cca..7c8f4d42d27 100644 --- a/src/shared/cpu-set-util.c +++ b/src/shared/cpu-set-util.c @@ -55,7 +55,10 @@ char *cpu_set_to_range_string(const CPUSet *set) { if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned))) return NULL; - r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end); + if (range_end > range_start || len == 0) + r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end); + else + r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start); assert_se(r > 0); len += r; } @@ -64,7 +67,10 @@ char *cpu_set_to_range_string(const CPUSet *set) { if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int))) return NULL; - r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end); + if (range_end > range_start || len == 0) + r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end); + else + r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start); assert_se(r > 0); } diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c index d66d7d304d2..cb970a61c15 100644 --- a/src/test/test-cpu-set-util.c +++ b/src/test/test-cpu-set-util.c @@ -29,19 +29,20 @@ static void test_parse_cpu_set(void) { cpu_set_reset(&c); /* Simple range (from CPUAffinity example) */ - assert_se(parse_cpu_set_full("1 2", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); + assert_se(parse_cpu_set_full("1 2 4", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0); assert_se(c.set); assert_se(c.allocated >= sizeof(__cpu_mask) / 8); assert_se(CPU_ISSET_S(1, c.allocated, c.set)); assert_se(CPU_ISSET_S(2, c.allocated, c.set)); - assert_se(CPU_COUNT_S(c.allocated, c.set) == 2); + assert_se(CPU_ISSET_S(4, c.allocated, c.set)); + assert_se(CPU_COUNT_S(c.allocated, c.set) == 3); assert_se(str = cpu_set_to_string(&c)); log_info("cpu_set_to_string: %s", str); str = mfree(str); assert_se(str = cpu_set_to_range_string(&c)); log_info("cpu_set_to_range_string: %s", str); - assert_se(streq(str, "1-2")); + assert_se(streq(str, "1-2 4")); str = mfree(str); cpu_set_reset(&c);