naccyde / rpms / systemd

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