ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
b9a53a
From 93777a6dd8c12d5cba094694bf7ed6e8c06c2d6d Mon Sep 17 00:00:00 2001
b9a53a
From: Michal Sekletar <msekleta@redhat.com>
b9a53a
Date: Thu, 23 May 2019 14:27:18 +0200
b9a53a
Subject: [PATCH] shared/cpu-set-util: introduce cpu_set_to_range()
b9a53a
b9a53a
(cherry picked from commit 71b28519b55b496237146f9bcb5a627455f15f7e)
b9a53a
b9a53a
Related: #1734787
b9a53a
---
b9a53a
 src/basic/cpu-set-util.c     | 37 ++++++++++++++++++++++++++
b9a53a
 src/basic/cpu-set-util.h     |  2 ++
b9a53a
 src/test/test-cpu-set-util.c | 50 ++++++++++++++++++++++++++++++++++++
b9a53a
 3 files changed, 89 insertions(+)
b9a53a
b9a53a
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
b9a53a
index 74e35e57dd..bff39ec143 100644
b9a53a
--- a/src/basic/cpu-set-util.c
b9a53a
+++ b/src/basic/cpu-set-util.c
b9a53a
@@ -37,6 +37,43 @@ char* cpu_set_to_string(const CPUSet *a) {
b9a53a
         return TAKE_PTR(str) ?: strdup("");
b9a53a
 }
b9a53a
 
b9a53a
+char *cpu_set_to_range_string(const CPUSet *set) {
b9a53a
+        unsigned range_start = 0, range_end;
b9a53a
+        _cleanup_free_ char *str = NULL;
b9a53a
+        size_t allocated = 0, len = 0;
b9a53a
+        bool in_range = false;
b9a53a
+        int r;
b9a53a
+
b9a53a
+        for (unsigned i = 0; i < set->allocated * 8; i++)
b9a53a
+                if (CPU_ISSET_S(i, set->allocated, set->set)) {
b9a53a
+                        if (in_range)
b9a53a
+                                range_end++;
b9a53a
+                        else {
b9a53a
+                                range_start = range_end = i;
b9a53a
+                                in_range = true;
b9a53a
+                        }
b9a53a
+                } else if (in_range) {
b9a53a
+                        in_range = false;
b9a53a
+
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
+                        assert_se(r > 0);
b9a53a
+                        len += r;
b9a53a
+                }
b9a53a
+
b9a53a
+        if (in_range) {
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
+                assert_se(r > 0);
b9a53a
+        }
b9a53a
+
b9a53a
+        return TAKE_PTR(str) ?: strdup("");
b9a53a
+}
b9a53a
+
b9a53a
 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
b9a53a
         size_t need;
b9a53a
 
b9a53a
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
b9a53a
index 415c6ca295..ec640b2ec9 100644
b9a53a
--- a/src/basic/cpu-set-util.h
b9a53a
+++ b/src/basic/cpu-set-util.h
b9a53a
@@ -25,7 +25,9 @@ static inline void cpu_set_reset(CPUSet *a) {
b9a53a
 int cpu_set_add_all(CPUSet *a, const CPUSet *b);
b9a53a
 
b9a53a
 char* cpu_set_to_string(const CPUSet *a);
b9a53a
+char *cpu_set_to_range_string(const CPUSet *a);
b9a53a
 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus);
b9a53a
+
b9a53a
 int parse_cpu_set_full(
b9a53a
                 const char *rvalue,
b9a53a
                 CPUSet *cpu_set,
b9a53a
diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c
b9a53a
index cae51ad7d9..0d2741cd43 100644
b9a53a
--- a/src/test/test-cpu-set-util.c
b9a53a
+++ b/src/test/test-cpu-set-util.c
b9a53a
@@ -4,6 +4,7 @@
b9a53a
 
b9a53a
 #include "alloc-util.h"
b9a53a
 #include "cpu-set-util.h"
b9a53a
+#include "string-util.h"
b9a53a
 #include "macro.h"
b9a53a
 
b9a53a
 static void test_parse_cpu_set(void) {
b9a53a
@@ -13,6 +14,22 @@ static void test_parse_cpu_set(void) {
b9a53a
 
b9a53a
         log_info("/* %s */", __func__);
b9a53a
 
b9a53a
+        /* Single value */
b9a53a
+        assert_se(parse_cpu_set_full("0", &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(0, c.allocated, c.set));
b9a53a
+        assert_se(CPU_COUNT_S(c.allocated, c.set) == 1);
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, "0-0"));
b9a53a
+        str = mfree(str);
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(c.set);
b9a53a
@@ -24,6 +41,10 @@ static void test_parse_cpu_set(void) {
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
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* A more interesting range */
b9a53a
@@ -34,9 +55,14 @@ static void test_parse_cpu_set(void) {
b9a53a
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
b9a53a
         for (cpu = 8; cpu < 12; cpu++)
b9a53a
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
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, "0-3 8-11"));
b9a53a
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Quoted strings */
b9a53a
@@ -48,6 +74,10 @@ static void test_parse_cpu_set(void) {
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, "8-11"));
b9a53a
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Use commas as separators */
b9a53a
@@ -72,6 +102,10 @@ static void test_parse_cpu_set(void) {
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, "0-7"));
b9a53a
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Ranges */
b9a53a
@@ -98,6 +132,10 @@ static void test_parse_cpu_set(void) {
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, "0-3 8-11"));
b9a53a
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Negative range (returns empty cpu_set) */
b9a53a
@@ -115,6 +153,10 @@ static void test_parse_cpu_set(void) {
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, "0-11"));
b9a53a
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Mix ranges and individual CPUs */
b9a53a
@@ -128,6 +170,10 @@ static void test_parse_cpu_set(void) {
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, "0-1 4-11"));
b9a53a
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Garbage */
b9a53a
@@ -156,6 +202,10 @@ static void test_parse_cpu_set(void) {
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, "8000-8191"));
b9a53a
+        str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 }
b9a53a