Brian Stinson 2593d8
From 5e6b616ed2708391752ba8c45f183ceb38573d7d 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: Tue, 28 May 2019 21:38:41 +0200
Brian Stinson 2593d8
Subject: [PATCH] test-execute: use CPUSet too
Brian Stinson 2593d8
Brian Stinson 2593d8
cpu_set_malloc() was the last user. It doesn't seem useful to keep
Brian Stinson 2593d8
it just to save the allocation of a few hundred bytes in a test, so
Brian Stinson 2593d8
it is dropped and a fixed maximum is allocated (1024 bytes).
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit 167a776dbe9d033523bd6881e5a695f2155dc321)
Brian Stinson 2593d8
Brian Stinson 2593d8
Related: #1734787
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/basic/cpu-set-util.c | 31 +------------------------------
Brian Stinson 2593d8
 src/basic/cpu-set-util.h |  3 +--
Brian Stinson 2593d8
 src/test/test-execute.c  | 13 ++++++-------
Brian Stinson 2593d8
 3 files changed, 8 insertions(+), 39 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 1803539ac6..c297eab032 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,36 +37,7 @@ 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
-cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
Brian Stinson 2593d8
-        cpu_set_t *c;
Brian Stinson 2593d8
-        unsigned n = 1024;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-        /* Allocates the cpuset in the right size */
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-        for (;;) {
Brian Stinson 2593d8
-                c = CPU_ALLOC(n);
Brian Stinson 2593d8
-                if (!c)
Brian Stinson 2593d8
-                        return NULL;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
Brian Stinson 2593d8
-                        CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                        if (ncpus)
Brian Stinson 2593d8
-                                *ncpus = n;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                        return c;
Brian Stinson 2593d8
-                }
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                CPU_FREE(c);
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                if (errno != EINVAL)
Brian Stinson 2593d8
-                        return NULL;
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-                n *= 2;
Brian Stinson 2593d8
-        }
Brian Stinson 2593d8
-}
Brian Stinson 2593d8
-
Brian Stinson 2593d8
-static int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
Brian Stinson 2593d8
+int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
Brian Stinson 2593d8
         size_t need;
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         assert(cpu_set);
Brian Stinson 2593d8
diff --git a/src/basic/cpu-set-util.h b/src/basic/cpu-set-util.h
Brian Stinson 2593d8
index 9b026aca09..b54e737110 100644
Brian Stinson 2593d8
--- a/src/basic/cpu-set-util.h
Brian Stinson 2593d8
+++ b/src/basic/cpu-set-util.h
Brian Stinson 2593d8
@@ -12,8 +12,6 @@
Brian Stinson 2593d8
 DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE);
Brian Stinson 2593d8
 #define _cleanup_cpu_free_ _cleanup_(CPU_FREEp)
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-cpu_set_t* cpu_set_malloc(unsigned *ncpus);
Brian Stinson 2593d8
-
Brian Stinson 2593d8
 /* This wraps the libc interface with a variable to keep the allocated size. */
Brian Stinson 2593d8
 typedef struct CPUSet {
Brian Stinson 2593d8
         cpu_set_t *set;
Brian Stinson 2593d8
@@ -30,6 +28,7 @@ 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
+int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus);
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-execute.c b/src/test/test-execute.c
Brian Stinson 2593d8
index fa8efdddd2..6c22995b1e 100644
Brian Stinson 2593d8
--- a/src/test/test-execute.c
Brian Stinson 2593d8
+++ b/src/test/test-execute.c
Brian Stinson 2593d8
@@ -144,13 +144,12 @@ static void test_exec_bindpaths(Manager *m) {
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 static void test_exec_cpuaffinity(Manager *m) {
Brian Stinson 2593d8
-        _cleanup_cpu_free_ cpu_set_t *c = NULL;
Brian Stinson 2593d8
-        unsigned n;
Brian Stinson 2593d8
+        _cleanup_(cpu_set_reset) CPUSet c = {};
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-        assert_se(c = cpu_set_malloc(&n);;
Brian Stinson 2593d8
-        assert_se(sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0);
Brian Stinson 2593d8
+        assert_se(cpu_set_realloc(&c, 8192) >= 0); /* just allocate the maximum possible size */
Brian Stinson 2593d8
+        assert_se(sched_getaffinity(0, c.allocated, c.set) >= 0);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-        if (CPU_ISSET_S(0, CPU_ALLOC_SIZE(n), c) == 0) {
Brian Stinson 2593d8
+        if (!CPU_ISSET_S(0, c.allocated, c.set)) {
Brian Stinson 2593d8
                 log_notice("Cannot use CPU 0, skipping %s", __func__);
Brian Stinson 2593d8
                 return;
Brian Stinson 2593d8
         }
Brian Stinson 2593d8
@@ -158,8 +157,8 @@ static void test_exec_cpuaffinity(Manager *m) {
Brian Stinson 2593d8
         test(m, "exec-cpuaffinity1.service", 0, CLD_EXITED);
Brian Stinson 2593d8
         test(m, "exec-cpuaffinity2.service", 0, CLD_EXITED);
Brian Stinson 2593d8
 
Brian Stinson 2593d8
-        if (CPU_ISSET_S(1, CPU_ALLOC_SIZE(n), c) == 0 ||
Brian Stinson 2593d8
-            CPU_ISSET_S(2, CPU_ALLOC_SIZE(n), c) == 0) {
Brian Stinson 2593d8
+        if (!CPU_ISSET_S(1, c.allocated, c.set) ||
Brian Stinson 2593d8
+            !CPU_ISSET_S(2, c.allocated, c.set)) {
Brian Stinson 2593d8
                 log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__);
Brian Stinson 2593d8
                 return;
Brian Stinson 2593d8
         }