basic: use comma as separator in cpuset cgroup cpu ranges v239-30
authorMichal Sekletár <msekleta@redhat.com>
Tue, 14 Apr 2020 14:16:45 +0000 (16:16 +0200)
committerThe Plumber <50238977+systemd-rhel-bot@users.noreply.github.com>
Wed, 15 Apr 2020 14:05:32 +0000 (16:05 +0200)
This is a workaround for
https://bugzilla.redhat.com/show_bug.cgi?id=1819152 and should be
reverted in RHEL-8.3.

RHEL-only

Related: #1824129

src/basic/cpu-set-util.c
src/basic/cpu-set-util.h
src/core/cgroup.c

index 36cb017ae7aba261cc2630596ad5975825bdc5ea..51752ad1a655b91d1019d94512f1c0b8e9e51fab 100644 (file)
@@ -86,6 +86,51 @@ char *cpu_set_to_range_string(const CPUSet *set) {
         return TAKE_PTR(str) ?: strdup("");
 }
 
+/* XXX(msekleta): this is the workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1819152, remove in 8.3 */
+char *cpu_set_to_range_string_kernel(const CPUSet *set) {
+        unsigned range_start = 0, range_end;
+        _cleanup_free_ char *str = NULL;
+        size_t allocated = 0, len = 0;
+        bool in_range = false;
+        int r;
+
+        for (unsigned i = 0; i < set->allocated * 8; i++)
+                if (CPU_ISSET_S(i, set->allocated, set->set)) {
+                        if (in_range)
+                                range_end++;
+                        else {
+                                range_start = range_end = i;
+                                in_range = true;
+                        }
+                } else if (in_range) {
+                        in_range = false;
+
+                        if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
+                                return NULL;
+
+                        if (range_end > range_start)
+                                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;
+                }
+
+        if (in_range) {
+                if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
+                        return NULL;
+
+                if (range_end > range_start)
+                        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);
+        }
+
+        return TAKE_PTR(str) ?: strdup("");
+}
+
+
 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
         size_t need;
 
index 295028cb5485dbd7267f7b49b996e32ab0561e1a..8519a9b6c8a0096ac33f1329d291f6781bf5409f 100644 (file)
@@ -27,6 +27,7 @@ int cpu_set_add_all(CPUSet *a, const CPUSet *b);
 
 char* cpu_set_to_string(const CPUSet *a);
 char *cpu_set_to_range_string(const CPUSet *a);
+char *cpu_set_to_range_string_kernel(const CPUSet *a);
 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus);
 
 int parse_cpu_set_full(
index 3f7665b755b4c48f9baf98896a6086cffb5618ef..9e4c3c7dac372469d2ea2d5748c523403ddf0bda 100644 (file)
@@ -557,7 +557,7 @@ static void cgroup_apply_unified_cpuset(Unit *u, CPUSet cpus, const char *name)
         _cleanup_free_ char *buf = NULL;
         int r;
 
-        buf = cpu_set_to_range_string(&cpus);
+        buf = cpu_set_to_range_string_kernel(&cpus);
         if (!buf)
             return;