daandemeyer / rpms / systemd

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