]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cpu-set-util.c
Merge pull request #653 from dvdhrm/bus-gold
[thirdparty/systemd.git] / src / basic / cpu-set-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010-2015 Lennart Poettering
5 Copyright 2015 Filipe Brandenburger
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <stddef.h>
23 #include <syslog.h>
24
25 #include "alloc-util.h"
26 #include "cpu-set-util.h"
27 #include "extract-word.h"
28 #include "log.h"
29 #include "macro.h"
30 #include "parse-util.h"
31 #include "string-util.h"
32
33 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
34 cpu_set_t *c;
35 unsigned n = 1024;
36
37 /* Allocates the cpuset in the right size */
38
39 for (;;) {
40 c = CPU_ALLOC(n);
41 if (!c)
42 return NULL;
43
44 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
45 CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
46
47 if (ncpus)
48 *ncpus = n;
49
50 return c;
51 }
52
53 CPU_FREE(c);
54
55 if (errno != EINVAL)
56 return NULL;
57
58 n *= 2;
59 }
60 }
61
62 int parse_cpu_set_and_warn(
63 const char *rvalue,
64 cpu_set_t **cpu_set,
65 const char *unit,
66 const char *filename,
67 unsigned line,
68 const char *lvalue) {
69
70 const char *whole_rvalue = rvalue;
71 _cleanup_cpu_free_ cpu_set_t *c = NULL;
72 unsigned ncpus = 0;
73
74 assert(lvalue);
75 assert(rvalue);
76
77 for (;;) {
78 _cleanup_free_ char *word = NULL;
79 unsigned cpu, cpu_lower, cpu_upper;
80 int r;
81
82 r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES);
83 if (r < 0)
84 return log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
85 if (r == 0)
86 break;
87
88 if (!c) {
89 c = cpu_set_malloc(&ncpus);
90 if (!c)
91 return log_oom();
92 }
93
94 r = parse_range(word, &cpu_lower, &cpu_upper);
95 if (r < 0)
96 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word);
97 if (cpu_lower >= ncpus || cpu_upper >= ncpus)
98 return log_syntax(unit, LOG_ERR, filename, line, EINVAL, "CPU out of range '%s' ncpus is %u", word, ncpus);
99
100 if (cpu_lower > cpu_upper)
101 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u", word, cpu_lower, cpu_upper);
102 else
103 for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
104 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
105 }
106
107 /* On success, sets *cpu_set and returns ncpus for the system. */
108 if (c) {
109 *cpu_set = c;
110 c = NULL;
111 }
112
113 return (int) ncpus;
114 }