]>
Commit | Line | Data |
---|---|---|
618234a5 LP |
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 | ||
11c3a366 TA |
21 | #include <errno.h> |
22 | #include <stddef.h> | |
23 | #include <syslog.h> | |
24 | ||
b5efdb8a | 25 | #include "alloc-util.h" |
6bedfcbb | 26 | #include "cpu-set-util.h" |
84ac7bea | 27 | #include "extract-word.h" |
11c3a366 TA |
28 | #include "log.h" |
29 | #include "macro.h" | |
93cc7779 | 30 | #include "parse-util.h" |
b11d6a7b | 31 | #include "string-util.h" |
618234a5 LP |
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; | |
a26662ce | 79 | unsigned cpu, cpu_lower, cpu_upper; |
618234a5 LP |
80 | int r; |
81 | ||
4fc66acb | 82 | r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES); |
a26662ce FB |
83 | if (r < 0) |
84 | return log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue); | |
618234a5 LP |
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 | ||
a26662ce FB |
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); | |
618234a5 LP |
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 | } | |
032cf8e4 YW |
115 | |
116 | int parse_cpu_set( | |
117 | const char *rvalue, | |
118 | cpu_set_t **cpu_set) { | |
119 | ||
120 | _cleanup_cpu_free_ cpu_set_t *c = NULL; | |
121 | unsigned ncpus = 0; | |
122 | ||
123 | assert(rvalue); | |
124 | ||
125 | for (;;) { | |
126 | _cleanup_free_ char *word = NULL; | |
127 | unsigned cpu, cpu_lower, cpu_upper; | |
128 | int r; | |
129 | ||
130 | r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES); | |
131 | if (r == -ENOMEM) | |
132 | return r; | |
133 | if (r <= 0) | |
134 | break; | |
135 | ||
136 | if (!c) { | |
137 | c = cpu_set_malloc(&ncpus); | |
138 | if (!c) | |
139 | return -ENOMEM; | |
140 | } | |
141 | ||
142 | r = parse_range(word, &cpu_lower, &cpu_upper); | |
143 | if (r < 0) | |
144 | return r; | |
145 | if (cpu_lower >= ncpus || cpu_upper >= ncpus) | |
146 | return -EINVAL; | |
147 | ||
148 | if (cpu_lower <= cpu_upper) | |
149 | for (cpu = cpu_lower; cpu <= cpu_upper; cpu++) | |
150 | CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c); | |
151 | } | |
152 | ||
153 | /* On success, sets *cpu_set and returns ncpus for the system. */ | |
154 | if (c) { | |
155 | *cpu_set = c; | |
156 | c = NULL; | |
157 | } | |
158 | ||
159 | return (int) ncpus; | |
160 | } |