]>
Commit | Line | Data |
---|---|---|
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 | ||
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_COUNT_S(policy->nodes.allocated, policy->nodes.set) != 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, 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 | ||
92 | int 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 |
129 | static int numa_max_node(void) { |
130 | _cleanup_closedir_ DIR *d = NULL; | |
131 | struct dirent *de; | |
132 | int r, max_node = 0; | |
133 | ||
134 | d = opendir("/sys/devices/system/node"); | |
135 | if (!d) | |
136 | return -errno; | |
137 | ||
138 | FOREACH_DIRENT(de, d, break) { | |
139 | int node; | |
140 | const char *n; | |
141 | ||
142 | (void) dirent_ensure_type(d, de); | |
143 | ||
144 | if (de->d_type != DT_DIR) | |
145 | continue; | |
146 | ||
147 | n = startswith(de->d_name, "node"); | |
148 | if (!n) | |
149 | continue; | |
150 | ||
151 | r = safe_atoi(n, &node); | |
152 | if (r < 0) | |
153 | continue; | |
154 | ||
155 | if (node > max_node) | |
156 | max_node = node; | |
157 | } | |
158 | ||
159 | return max_node; | |
160 | } | |
161 | ||
162 | int numa_mask_add_all(CPUSet *mask) { | |
163 | int m; | |
164 | ||
165 | assert(mask); | |
166 | ||
167 | m = numa_max_node(); | |
168 | if (m < 0) { | |
169 | log_debug_errno(m, "Failed to determine maximum NUMA node index, assuming 1023: %m"); | |
170 | m = 1023; /* CONFIG_NODES_SHIFT is set to 10 on x86_64, i.e. 1024 NUMA nodes in total */ | |
171 | } | |
172 | ||
173 | for (int i = 0; i <= m; i++) { | |
174 | int r; | |
175 | ||
176 | r = cpu_set_add(mask, i); | |
177 | if (r < 0) | |
178 | return r; | |
179 | } | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
1808f768 MS |
184 | static const char* const mpol_table[] = { |
185 | [MPOL_DEFAULT] = "default", | |
186 | [MPOL_PREFERRED] = "preferred", | |
187 | [MPOL_BIND] = "bind", | |
188 | [MPOL_INTERLEAVE] = "interleave", | |
189 | [MPOL_LOCAL] = "local", | |
190 | }; | |
191 | ||
192 | DEFINE_STRING_TABLE_LOOKUP(mpol, int); |