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