ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
Brian Stinson 2593d8
From 46b4d26c54a773f7da350e89562039ccc5157a8f Mon Sep 17 00:00:00 2001
Brian Stinson 2593d8
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Brian Stinson 2593d8
Date: Sun, 19 May 2019 18:02:38 +0200
Brian Stinson 2593d8
Subject: [PATCH] shared/cpu-set-util: move the part to print cpu-set into a
Brian Stinson 2593d8
 separate function
Brian Stinson 2593d8
Brian Stinson 2593d8
Also avoid unnecessary asprintf() when we can write to the output area
Brian Stinson 2593d8
directly.
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit a832893f9c4f0a0329768e90f67e2fa24bb0008e)
Brian Stinson 2593d8
Brian Stinson 2593d8
Related: #1734787
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/basic/cpu-set-util.c     | 21 +++++++++++++++++++++
Brian Stinson 2593d8
 src/basic/cpu-set-util.h     |  1 +
Brian Stinson 2593d8
 src/core/dbus-execute.c      | 29 +++++------------------------
Brian Stinson 2593d8
 src/test/test-cpu-set-util.c | 29 +++++++++++++++++++++++++++++
Brian Stinson 2593d8
 4 files changed, 56 insertions(+), 24 deletions(-)
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 b1c927bcb8..8f24a2601a 100644
Brian Stinson 2593d8
--- a/src/basic/cpu-set-util.c
Brian Stinson 2593d8
+++ b/src/basic/cpu-set-util.c
Brian Stinson 2593d8
@@ -5,6 +5,7 @@
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 #include <errno.h>
Brian Stinson 2593d8
 #include <stddef.h>
Brian Stinson 2593d8
+#include <stdio.h>
Brian Stinson 2593d8
 #include <syslog.h>
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 #include "alloc-util.h"
Brian Stinson 2593d8
@@ -15,6 +16,26 @@
Brian Stinson 2593d8
 #include "parse-util.h"
Brian Stinson 2593d8
 #include "string-util.h"
Brian Stinson 2593d8
 
Brian Stinson 2593d8
+char* cpu_set_to_string(const cpu_set_t *set, size_t setsize) {
Brian Stinson 2593d8
+        _cleanup_free_ char *str = NULL;
Brian Stinson 2593d8
+        size_t allocated = 0, len = 0;
Brian Stinson 2593d8
+        int i, r;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        for (i = 0; (size_t) i < setsize * 8; i++) {
Brian Stinson 2593d8
+                if (!CPU_ISSET_S(i, setsize, set))
Brian Stinson 2593d8
+                        continue;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                if (!GREEDY_REALLOC(str, allocated, len + 1 + DECIMAL_STR_MAX(int)))
Brian Stinson 2593d8
+                        return NULL;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
Brian Stinson 2593d8
+                assert_se(r > 0);
Brian Stinson 2593d8
+                len += r;
Brian Stinson 2593d8
+        }
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        return TAKE_PTR(str) ?: strdup("");
Brian Stinson 2593d8
+}
Brian Stinson 2593d8
+
Brian Stinson 2593d8
 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
Brian Stinson 2593d8
         cpu_set_t *c;
Brian Stinson 2593d8
         unsigned n = 1024;
Brian Stinson 2593d8
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
Brian Stinson 2593d8
index 88470fe15a..3c546beb55 100644
Brian Stinson 2593d8
--- a/src/basic/cpu-set-util.h
Brian Stinson 2593d8
+++ b/src/basic/cpu-set-util.h
Brian Stinson 2593d8
@@ -26,6 +26,7 @@ static inline cpu_set_t* cpu_set_mfree(cpu_set_t *p) {
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 cpu_set_t* cpu_set_malloc(unsigned *ncpus);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
+char* cpu_set_to_string(const cpu_set_t *set, size_t setsize);
Brian Stinson 2593d8
 int parse_cpu_set_internal(const char *rvalue, cpu_set_t **cpu_set, bool warn, const char *unit, const char *filename, unsigned line, const char *lvalue);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 static inline int parse_cpu_set_and_warn(const char *rvalue, cpu_set_t **cpu_set, const char *unit, const char *filename, unsigned line, const char *lvalue) {
Brian Stinson 2593d8
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
Brian Stinson 2593d8
index 5379545d57..d9f4445745 100644
Brian Stinson 2593d8
--- a/src/core/dbus-execute.c
Brian Stinson 2593d8
+++ b/src/core/dbus-execute.c
Brian Stinson 2593d8
@@ -1565,32 +1565,13 @@ int bus_exec_context_set_transient_property(
Brian Stinson 2593d8
                                 unit_write_settingf(u, flags, name, "%s=", name);
Brian Stinson 2593d8
                         } else {
Brian Stinson 2593d8
                                 _cleanup_free_ char *str = NULL;
Brian Stinson 2593d8
-                                size_t allocated = 0, len = 0, i, ncpus;
Brian Stinson 2593d8
+                                size_t ncpus;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-                                ncpus = CPU_SIZE_TO_NUM(n);
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                                for (i = 0; i < ncpus; i++) {
Brian Stinson 2593d8
-                                        _cleanup_free_ char *p = NULL;
Brian Stinson 2593d8
-                                        size_t add;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                                        if (!CPU_ISSET_S(i, n, (cpu_set_t*) a))
Brian Stinson 2593d8
-                                                continue;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                                        r = asprintf(&p, "%zu", i);
Brian Stinson 2593d8
-                                        if (r < 0)
Brian Stinson 2593d8
-                                                return -ENOMEM;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                                        add = strlen(p);
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                                        if (!GREEDY_REALLOC(str, allocated, len + add + 2))
Brian Stinson 2593d8
-                                                return -ENOMEM;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                                        strcpy(mempcpy(str + len, p, add), " ");
Brian Stinson 2593d8
-                                        len += add + 1;
Brian Stinson 2593d8
-                                }
Brian Stinson 2593d8
+                                str = cpu_set_to_string(a, n);
Brian Stinson 2593d8
+                                if (!str)
Brian Stinson 2593d8
+                                        return -ENOMEM;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-                                if (len != 0)
Brian Stinson 2593d8
-                                        str[len - 1] = '\0';
Brian Stinson 2593d8
+                                ncpus = CPU_SIZE_TO_NUM(n);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
                                 if (!c->cpuset || c->cpuset_ncpus < ncpus) {
Brian Stinson 2593d8
                                         cpu_set_t *cpuset;
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 c9272459b4..ff5edb2a69 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
@@ -6,6 +6,7 @@
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
         cpu_set_t *c = NULL;
Brian Stinson 2593d8
+        _cleanup_free_ char *str = NULL;
Brian Stinson 2593d8
         int ncpus;
Brian Stinson 2593d8
         int cpu;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
@@ -15,6 +16,10 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
         assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
         assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* A more interesting range */
Brian Stinson 2593d8
@@ -25,6 +30,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
         for (cpu = 8; cpu < 12; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Quoted strings */
Brian Stinson 2593d8
@@ -33,6 +41,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
Brian Stinson 2593d8
         for (cpu = 8; cpu < 12; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Use commas as separators */
Brian Stinson 2593d8
@@ -43,6 +54,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
         for (cpu = 8; cpu < 12; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Commas with spaces (and trailing comma, space) */
Brian Stinson 2593d8
@@ -51,6 +65,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
Brian Stinson 2593d8
         for (cpu = 0; cpu < 8; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Ranges */
Brian Stinson 2593d8
@@ -61,6 +78,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
         for (cpu = 8; cpu < 12; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Ranges with trailing comma, space */
Brian Stinson 2593d8
@@ -71,6 +91,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
         for (cpu = 8; cpu < 12; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Negative range (returns empty cpu_set) */
Brian Stinson 2593d8
@@ -85,6 +108,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
         assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 12);
Brian Stinson 2593d8
         for (cpu = 0; cpu < 12; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Mix ranges and individual CPUs */
Brian Stinson 2593d8
@@ -95,6 +121,9 @@ static void test_parse_cpu_set(void) {
Brian Stinson 2593d8
         assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
         for (cpu = 4; cpu < 12; cpu++)
Brian Stinson 2593d8
                 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
Brian Stinson 2593d8
+        assert_se(str = cpu_set_to_string(c, CPU_ALLOC_SIZE(ncpus)));
Brian Stinson 2593d8
+        log_info("cpu_set_to_string: %s", str);
Brian Stinson 2593d8
+        str = mfree(str);
Brian Stinson 2593d8
         c = cpu_set_mfree(c);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         /* Garbage */