From f44b3035d4a698aa0ce08a552199b54d43de3d85 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 28 May 2019 21:28:31 +0200 Subject: [PATCH] Move cpus_in_affinity_mask() to cpu-set-util.[ch] It just seems to fit better there and it's always better to have things in shared/ rather than basic/. --- src/basic/process-util.c | 39 ++------------------------------------- src/basic/process-util.h | 2 -- src/shared/condition.c | 1 + src/shared/cpu-set-util.c | 34 ++++++++++++++++++++++++++++++++++ src/shared/cpu-set-util.h | 2 ++ src/test/test-condition.c | 1 + 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 3dc3534e1ab..85ea3cf1d11 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -1534,45 +1533,11 @@ int set_oom_score_adjust(int value) { WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER); } -int cpus_in_affinity_mask(void) { - size_t n = 16; - int r; - - for (;;) { - cpu_set_t *c; - - c = CPU_ALLOC(n); - if (!c) - return -ENOMEM; - - if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) { - int k; - - k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c); - CPU_FREE(c); - - if (k <= 0) - return -EINVAL; - - return k; - } - - r = -errno; - CPU_FREE(c); - - if (r != -EINVAL) - return r; - if (n > SIZE_MAX/2) - return -ENOMEM; - n *= 2; - } -} - static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime", [IOPRIO_CLASS_BE] = "best-effort", - [IOPRIO_CLASS_IDLE] = "idle" + [IOPRIO_CLASS_IDLE] = "idle", }; DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, IOPRIO_N_CLASSES); @@ -1593,7 +1558,7 @@ static const char* const sched_policy_table[] = { [SCHED_BATCH] = "batch", [SCHED_IDLE] = "idle", [SCHED_FIFO] = "fifo", - [SCHED_RR] = "rr" + [SCHED_RR] = "rr", }; DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX); diff --git a/src/basic/process-util.h b/src/basic/process-util.h index 7e1d65a7bf1..1571f1a6529 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -194,5 +194,3 @@ assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX) (pid) = 0; \ _pid_; \ }) - -int cpus_in_affinity_mask(void); diff --git a/src/shared/condition.c b/src/shared/condition.c index d1ac9de7565..2d521bc8c69 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -21,6 +21,7 @@ #include "cap-list.h" #include "cgroup-util.h" #include "condition.h" +#include "cpu-set-util.h" #include "efivars.h" #include "env-file.h" #include "extract-word.h" diff --git a/src/shared/cpu-set-util.c b/src/shared/cpu-set-util.c index b8d13a2ba62..b0c7acd7893 100644 --- a/src/shared/cpu-set-util.c +++ b/src/shared/cpu-set-util.c @@ -201,3 +201,37 @@ int parse_cpu_set_extend( return cpu_set_add_all(old, &cpuset); } + +int cpus_in_affinity_mask(void) { + size_t n = 16; + int r; + + for (;;) { + cpu_set_t *c; + + c = CPU_ALLOC(n); + if (!c) + return -ENOMEM; + + if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) { + int k; + + k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c); + CPU_FREE(c); + + if (k <= 0) + return -EINVAL; + + return k; + } + + r = -errno; + CPU_FREE(c); + + if (r != -EINVAL) + return r; + if (n > SIZE_MAX/2) + return -ENOMEM; + n *= 2; + } +} diff --git a/src/shared/cpu-set-util.h b/src/shared/cpu-set-util.h index 4c1c81fc593..602c983ae30 100644 --- a/src/shared/cpu-set-util.h +++ b/src/shared/cpu-set-util.h @@ -45,3 +45,5 @@ int parse_cpu_set_extend( static inline int parse_cpu_set(const char *rvalue, CPUSet *cpu_set){ return parse_cpu_set_full(rvalue, cpu_set, false, NULL, NULL, 0, NULL); } + +int cpus_in_affinity_mask(void); diff --git a/src/test/test-condition.c b/src/test/test-condition.c index 2a5e3aba807..4bbca2074f5 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -13,6 +13,7 @@ #include "audit-util.h" #include "cgroup-util.h" #include "condition.h" +#include "cpu-set-util.h" #include "efivars.h" #include "hostname-util.h" #include "id128-util.h" -- 2.39.2