daandemeyer / rpms / systemd

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