]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/numa-util.c
Define FOREACH_DIRENT through FOREACH_DIRENT_ALL
[thirdparty/systemd.git] / src / shared / numa-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
1808f768
MS
2
3#include <errno.h>
4#include <sched.h>
5
6#include "alloc-util.h"
7#include "cpu-set-util.h"
332d387f
MS
8#include "dirent-util.h"
9#include "fd-util.h"
1808f768
MS
10#include "fileio.h"
11#include "macro.h"
12#include "missing_syscall.h"
13#include "numa-util.h"
14#include "stdio-util.h"
15#include "string-table.h"
16
17bool 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_COUNT_S(policy->nodes.allocated, policy->nodes.set) != 1)
30 return false;
31
32 return true;
33}
34
35static 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
68int 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, 0, 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
92int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) {
93 int r;
94 size_t i;
95 _cleanup_(cpu_set_reset) CPUSet s = {};
96
97 assert(policy);
98 assert(ret);
99
100 for (i = 0; i < policy->nodes.allocated * 8; i++) {
101 _cleanup_free_ char *l = NULL;
102 char p[STRLEN("/sys/devices/system/node/node//cpulist") + DECIMAL_STR_MAX(size_t) + 1];
103 _cleanup_(cpu_set_reset) CPUSet part = {};
104
105 if (!CPU_ISSET_S(i, policy->nodes.allocated, policy->nodes.set))
106 continue;
107
108 xsprintf(p, "/sys/devices/system/node/node%zu/cpulist", i);
109
110 r = read_one_line_file(p, &l);
111 if (r < 0)
112 return r;
113
114 r = parse_cpu_set(l, &part);
115 if (r < 0)
116 return r;
117
118 r = cpu_set_add_all(&s, &part);
119 if (r < 0)
120 return r;
121 }
122
123 *ret = s;
124 s = (CPUSet) {};
125
126 return 0;
127}
128
332d387f
MS
129static int numa_max_node(void) {
130 _cleanup_closedir_ DIR *d = NULL;
332d387f
MS
131 int r, max_node = 0;
132
133 d = opendir("/sys/devices/system/node");
134 if (!d)
135 return -errno;
136
137 FOREACH_DIRENT(de, d, break) {
138 int node;
139 const char *n;
140
332d387f
MS
141 if (de->d_type != DT_DIR)
142 continue;
143
144 n = startswith(de->d_name, "node");
145 if (!n)
146 continue;
147
148 r = safe_atoi(n, &node);
149 if (r < 0)
150 continue;
151
152 if (node > max_node)
153 max_node = node;
154 }
155
156 return max_node;
157}
158
159int numa_mask_add_all(CPUSet *mask) {
160 int m;
161
162 assert(mask);
163
164 m = numa_max_node();
165 if (m < 0) {
166 log_debug_errno(m, "Failed to determine maximum NUMA node index, assuming 1023: %m");
167 m = 1023; /* CONFIG_NODES_SHIFT is set to 10 on x86_64, i.e. 1024 NUMA nodes in total */
168 }
169
170 for (int i = 0; i <= m; i++) {
171 int r;
172
173 r = cpu_set_add(mask, i);
174 if (r < 0)
175 return r;
176 }
177
178 return 0;
179}
180
1808f768
MS
181static const char* const mpol_table[] = {
182 [MPOL_DEFAULT] = "default",
183 [MPOL_PREFERRED] = "preferred",
184 [MPOL_BIND] = "bind",
185 [MPOL_INTERLEAVE] = "interleave",
186 [MPOL_LOCAL] = "local",
187};
188
189DEFINE_STRING_TABLE_LOOKUP(mpol, int);