Debian Codegraph @

src/shared/numa-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4
5 #include "alloc-util.h"
6 #include "cpu-set-util.h"
7 #include "dirent-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "log.h"
11 #include "numa-util.h"
12 #include "parse-util.h"
13 #include "stdio-util.h"
14 #include "string-table.h"
15 #include "string-util.h"
16
17 bool numa_policy_is_valid(const NUMAPolicy *policy) {
18 assert(policy);
19
20 if (!mpol_is_valid(numa_policy_get_type(policy)))
21 return false;
22
23 if (!policy->nodes.set &&
24 !IN_SET(numa_policy_get_type(policy), MPOL_DEFAULT, MPOL_LOCAL, MPOL_PREFERRED))
25 return false;
26
27 if (policy->nodes.set &&
28 numa_policy_get_type(policy) == MPOL_PREFERRED &&
29 cpu_set_count(&policy->nodes) != 1)
30 return false;
31
32 return true;
33 }
34
35 static int numa_policy_to_mempolicy(const NUMAPolicy *policy, unsigned long *ret_maxnode, unsigned long **ret_nodes) {
36 unsigned node, bits = 0, ulong_bits;
37 _cleanup_free_ unsigned long *out = NULL;
38
39 assert(policy);
40 assert(ret_maxnode);
41 assert(ret_nodes);
42
43 if (IN_SET(numa_policy_get_type(policy), MPOL_DEFAULT, MPOL_LOCAL) ||
44 (numa_policy_get_type(policy) == MPOL_PREFERRED && !policy->nodes.set)) {
45 *ret_nodes = NULL;
46 *ret_maxnode = 0;
47 return 0;
48 }
49
50 bits = policy->nodes.allocated * 8;
51 ulong_bits = sizeof(unsigned long) * 8;
52
53 out = new0(unsigned long, DIV_ROUND_UP(policy->nodes.allocated, sizeof(unsigned long)));
54 if (!out)
55 return -ENOMEM;
56
57 /* We don't make any assumptions about internal type libc is using to store NUMA node mask.
58 Hence we need to convert the node mask to the representation expected by set_mempolicy() */
59 for (node = 0; node < bits; node++)
60 if (CPU_ISSET_S(node, policy->nodes.allocated, policy->nodes.set))
61 out[node / ulong_bits] |= 1ul << (node % ulong_bits);
62
63 *ret_nodes = TAKE_PTR(out);
64 *ret_maxnode = bits + 1;
65 return 0;
66 }
67
68 int apply_numa_policy(const NUMAPolicy *policy) {
69 int r;
70 _cleanup_free_ unsigned long *nodes = NULL;
71 unsigned long maxnode;
72
73 assert(policy);
74
75 if (get_mempolicy(NULL, NULL, 0, NULL, 0) < 0 && errno == ENOSYS)
76 return -EOPNOTSUPP;
77
78 if (!numa_policy_is_valid(policy))
79 return -EINVAL;
80
81 r = numa_policy_to_mempolicy(policy, &maxnode, &nodes);
82 if (r < 0)
83 return r;
84
85 r = set_mempolicy(numa_policy_get_type(policy), nodes, maxnode);
86 if (r < 0)
87 return -errno;
88
89 return 0;
90 }
91
92 int numa_node_get_cpus(size_t node, CPUSet *ret) {
93 char p[STRLEN("/sys/devices/system/node/node//cpulist") + DECIMAL_STR_MAX(size_t) + 1];
94 _cleanup_free_ char *cpulist = NULL;
95 int r;
96
97 assert(ret);
98
99 xsprintf(p, "/sys/devices/system/node/node%zu/cpulist", node);
100
101 r = read_virtual_file(p, SIZE_MAX, &cpulist, /* ret_size= */ NULL);
102 if (r < 0)
103 return r;
104
105 return parse_cpu_set(cpulist, ret);
106 }
107
108 int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) {
109 _cleanup_(cpu_set_done) CPUSet s = {};
110 int r;
111
112 assert(policy);
113 assert(ret);
114
115 for (size_t i = 0; i < policy->nodes.allocated * 8; i++) {
116 if (!CPU_ISSET_S(i, policy->nodes.allocated, policy->nodes.set))
117 continue;
118
119 _cleanup_(cpu_set_done) CPUSet part = {};
120 r = numa_node_get_cpus(i, &part);
121 if (r < 0)
122 return r;
123
124 r = cpu_set_add_set(&s, &part);
125 if (r < 0)
126 return r;
127 }
128
129 *ret = TAKE_STRUCT(s);
130
131 return 0;
132 }
133
134 static int numa_max_node(void) {
135 _cleanup_closedir_ DIR *d = NULL;
136 int r, max_node = 0;
137
138 d = opendir("/sys/devices/system/node");
139 if (!d)
140 return -errno;
141
142 FOREACH_DIRENT(de, d, break) {
143 int node;
144 const char *n;
145
146 if (de->d_type != DT_DIR)
147 continue;
148
149 n = startswith(de->d_name, "node");
150 if (!n)
151 continue;
152
153 r = safe_atoi(n, &node);
154 if (r < 0)
155 continue;
156
157 if (node > max_node)
158 max_node = node;
159 }
160
161 return max_node;
162 }
163
164 int numa_get_node_from_cpu(unsigned cpu, unsigned *ret) {
165 _cleanup_closedir_ DIR *d = NULL;
166 int r;
167
168 assert(ret);
169
170 d = opendir("/sys/devices/system/node");
171 if (!d)
172 return -errno;
173
174 FOREACH_DIRENT(de, d, break) {
175 char p[STRLEN("/sys/devices/system/node/node/cpulist") + DECIMAL_STR_MAX(unsigned) + 1];
176 _cleanup_(cpu_set_done) CPUSet cpus = {};
177 _cleanup_free_ char *cpulist = NULL;
178 const char *n;
179 unsigned node;
180
181 if (de->d_type != DT_DIR)
182 continue;
183
184 n = startswith(de->d_name, "node");
185 if (!n)
186 continue;
187
188 r = safe_atou(n, &node);
189 if (r < 0) {
190 log_debug_errno(r, "Failed to parse node number %s to unsigned, ignoring: %m", n);
191 continue;
192 }
193
194 xsprintf(p, "/sys/devices/system/node/node%u/cpulist", node);
195
196 r = read_virtual_file(p, SIZE_MAX, &cpulist, /* ret_size= */ NULL);
197 if (r < 0) {
198 log_debug_errno(r, "Failed to read %s, ignoring: %m", p);
199 continue;
200 }
201
202 r = parse_cpu_set(cpulist, &cpus);
203 if (r < 0) {
204 log_debug_errno(r, "Failed to parse cpu set %s, ignoring: %m", cpulist);
205 continue;
206 }
207
208 if (CPU_ISSET_S(cpu, cpus.allocated, cpus.set)) {
209 *ret = node;
210 return 0;
211 }
212 }
213
214 /* CPU not found in any NUMA node, assume node 0 */
215 log_debug("CPU %u not found in any NUMA node, assuming node 0.", cpu);
216 *ret = 0;
217
218 return 0;
219 }
220
221 int numa_mask_add_all(CPUSet *mask) {
222 int m;
223
224 assert(mask);
225
226 m = numa_max_node();
227 if (m < 0) {
228 log_debug_errno(m, "Failed to determine maximum NUMA node index, assuming 1023: %m");
229 m = 1023; /* CONFIG_NODES_SHIFT is set to 10 on x86_64, i.e. 1024 NUMA nodes in total */
230 }
231
232 for (int i = 0; i <= m; i++) {
233 int r;
234
235 r = cpu_set_add(mask, i);
236 if (r < 0)
237 return r;
238 }
239
240 return 0;
241 }
242
243 static const char* const mpol_table[] = {
244 [MPOL_DEFAULT] = "default",
245 [MPOL_PREFERRED] = "preferred",
246 [MPOL_BIND] = "bind",
247 [MPOL_INTERLEAVE] = "interleave",
248 [MPOL_LOCAL] = "local",
249 };
250
251 DEFINE_STRING_TABLE_LOOKUP(mpol, int);