Brian Stinson 2593d8
From 42032749e61076b3d9e5004432073c2a5ea737ce 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:28:31 +0200
Brian Stinson 2593d8
Subject: [PATCH] Move cpus_in_affinity_mask() to cpu-set-util.[ch]
Brian Stinson 2593d8
Brian Stinson 2593d8
It just seems to fit better there and it's always better to have things
Brian Stinson 2593d8
in shared/ rather than basic/.
Brian Stinson 2593d8
Brian Stinson 2593d8
(cherry picked from commit f44b3035d4a698aa0ce08a552199b54d43de3d85)
Brian Stinson 2593d8
Brian Stinson 2593d8
Related: #1734787
Brian Stinson 2593d8
---
Brian Stinson 2593d8
 src/basic/cpu-set-util.c  | 34 ++++++++++++++++++++++++++++++++++
Brian Stinson 2593d8
 src/basic/cpu-set-util.h  |  2 ++
Brian Stinson 2593d8
 src/basic/process-util.c  |  5 ++---
Brian Stinson 2593d8
 src/shared/condition.c    |  1 +
Brian Stinson 2593d8
 src/test/test-condition.c |  1 +
Brian Stinson 2593d8
 5 files changed, 40 insertions(+), 3 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 fe440f6381..1803539ac6 100644
Brian Stinson 2593d8
--- a/src/basic/cpu-set-util.c
Brian Stinson 2593d8
+++ b/src/basic/cpu-set-util.c
Brian Stinson 2593d8
@@ -204,3 +204,37 @@ int parse_cpu_set_extend(
Brian Stinson 2593d8
 
Brian Stinson 2593d8
         return cpu_set_add_all(old, &cpuset);
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+int cpus_in_affinity_mask(void) {
Brian Stinson 2593d8
+        size_t n = 16;
Brian Stinson 2593d8
+        int r;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+        for (;;) {
Brian Stinson 2593d8
+                cpu_set_t *c;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                c = CPU_ALLOC(n);
Brian Stinson 2593d8
+                if (!c)
Brian Stinson 2593d8
+                        return -ENOMEM;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
Brian Stinson 2593d8
+                        int k;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                        k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c);
Brian Stinson 2593d8
+                        CPU_FREE(c);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                        if (k <= 0)
Brian Stinson 2593d8
+                                return -EINVAL;
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                        return k;
Brian Stinson 2593d8
+                }
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                r = -errno;
Brian Stinson 2593d8
+                CPU_FREE(c);
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+                if (r != -EINVAL)
Brian Stinson 2593d8
+                        return r;
Brian Stinson 2593d8
+                if (n > SIZE_MAX/2)
Brian Stinson 2593d8
+                        return -ENOMEM;
Brian Stinson 2593d8
+                n *= 2;
Brian Stinson 2593d8
+        }
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 eb31b362fe..9b026aca09 100644
Brian Stinson 2593d8
--- a/src/basic/cpu-set-util.h
Brian Stinson 2593d8
+++ b/src/basic/cpu-set-util.h
Brian Stinson 2593d8
@@ -49,3 +49,5 @@ int parse_cpu_set_extend(
Brian Stinson 2593d8
 static inline int parse_cpu_set(const char *rvalue, CPUSet *cpu_set){
Brian Stinson 2593d8
         return parse_cpu_set_full(rvalue, cpu_set, false, NULL, NULL, 0, NULL);
Brian Stinson 2593d8
 }
Brian Stinson 2593d8
+
Brian Stinson 2593d8
+int cpus_in_affinity_mask(void);
Brian Stinson 2593d8
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
Brian Stinson 2593d8
index 6dbeee9dda..0a4a747ba4 100644
Brian Stinson 2593d8
--- a/src/basic/process-util.c
Brian Stinson 2593d8
+++ b/src/basic/process-util.c
Brian Stinson 2593d8
@@ -4,7 +4,6 @@
Brian Stinson 2593d8
 #include <errno.h>
Brian Stinson 2593d8
 #include <limits.h>
Brian Stinson 2593d8
 #include <linux/oom.h>
Brian Stinson 2593d8
-#include <sched.h>
Brian Stinson 2593d8
 #include <signal.h>
Brian Stinson 2593d8
 #include <stdbool.h>
Brian Stinson 2593d8
 #include <stdio.h>
Brian Stinson 2593d8
@@ -1474,7 +1473,7 @@ static const char *const ioprio_class_table[] = {
Brian Stinson 2593d8
         [IOPRIO_CLASS_NONE] = "none",
Brian Stinson 2593d8
         [IOPRIO_CLASS_RT] = "realtime",
Brian Stinson 2593d8
         [IOPRIO_CLASS_BE] = "best-effort",
Brian Stinson 2593d8
-        [IOPRIO_CLASS_IDLE] = "idle"
Brian Stinson 2593d8
+        [IOPRIO_CLASS_IDLE] = "idle",
Brian Stinson 2593d8
 };
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, IOPRIO_N_CLASSES);
Brian Stinson 2593d8
@@ -1495,7 +1494,7 @@ static const char* const sched_policy_table[] = {
Brian Stinson 2593d8
         [SCHED_BATCH] = "batch",
Brian Stinson 2593d8
         [SCHED_IDLE] = "idle",
Brian Stinson 2593d8
         [SCHED_FIFO] = "fifo",
Brian Stinson 2593d8
-        [SCHED_RR] = "rr"
Brian Stinson 2593d8
+        [SCHED_RR] = "rr",
Brian Stinson 2593d8
 };
Brian Stinson 2593d8
 
Brian Stinson 2593d8
 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);
Brian Stinson 2593d8
diff --git a/src/shared/condition.c b/src/shared/condition.c
Brian Stinson 2593d8
index 2969a89b4e..b829f0528c 100644
Brian Stinson 2593d8
--- a/src/shared/condition.c
Brian Stinson 2593d8
+++ b/src/shared/condition.c
Brian Stinson 2593d8
@@ -21,6 +21,7 @@
Brian Stinson 2593d8
 #include "cap-list.h"
Brian Stinson 2593d8
 #include "cgroup-util.h"
Brian Stinson 2593d8
 #include "condition.h"
Brian Stinson 2593d8
+#include "cpu-set-util.h"
Brian Stinson 2593d8
 #include "efivars.h"
Brian Stinson 2593d8
 #include "extract-word.h"
Brian Stinson 2593d8
 #include "fd-util.h"
Brian Stinson 2593d8
diff --git a/src/test/test-condition.c b/src/test/test-condition.c
Brian Stinson 2593d8
index 7ce6ee80ea..24395dafc6 100644
Brian Stinson 2593d8
--- a/src/test/test-condition.c
Brian Stinson 2593d8
+++ b/src/test/test-condition.c
Brian Stinson 2593d8
@@ -13,6 +13,7 @@
Brian Stinson 2593d8
 #include "audit-util.h"
Brian Stinson 2593d8
 #include "cgroup-util.h"
Brian Stinson 2593d8
 #include "condition.h"
Brian Stinson 2593d8
+#include "cpu-set-util.h"
Brian Stinson 2593d8
 #include "efivars.h"
Brian Stinson 2593d8
 #include "hostname-util.h"
Brian Stinson 2593d8
 #include "id128-util.h"