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