]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
1808f768 | 2 | |
1808f768 MS |
3 | #include <sched.h> |
4 | ||
5 | #include "alloc-util.h" | |
6 | #include "cpu-set-util.h" | |
332d387f MS |
7 | #include "dirent-util.h" |
8 | #include "fd-util.h" | |
1808f768 | 9 | #include "fileio.h" |
93a1f792 | 10 | #include "log.h" |
1808f768 MS |
11 | #include "missing_syscall.h" |
12 | #include "numa-util.h" | |
c94f6ab1 | 13 | #include "parse-util.h" |
1808f768 MS |
14 | #include "stdio-util.h" |
15 | #include "string-table.h" | |
d9ccf6b3 | 16 | #include "string-util.h" |
1808f768 MS |
17 | |
18 | bool numa_policy_is_valid(const NUMAPolicy *policy) { | |
19 | assert(policy); | |
20 | ||
21 | if (!mpol_is_valid(numa_policy_get_type(policy))) | |
22 | return false; | |
23 | ||
24 | if (!policy->nodes.set && | |
25 | !IN_SET(numa_policy_get_type(policy), MPOL_DEFAULT, MPOL_LOCAL, MPOL_PREFERRED)) | |
26 | return false; | |
27 | ||
28 | if (policy->nodes.set && | |
29 | numa_policy_get_type(policy) == MPOL_PREFERRED && | |
30 | CPU_COUNT_S(policy->nodes.allocated, policy->nodes.set) != 1) | |
31 | return false; | |
32 | ||
33 | return true; | |
34 | } | |
35 | ||
36 | static int numa_policy_to_mempolicy(const NUMAPolicy *policy, unsigned long *ret_maxnode, unsigned long **ret_nodes) { | |
37 | unsigned node, bits = 0, ulong_bits; | |
38 | _cleanup_free_ unsigned long *out = NULL; | |
39 | ||
40 | assert(policy); | |
41 | assert(ret_maxnode); | |
42 | assert(ret_nodes); | |
43 | ||
44 | if (IN_SET(numa_policy_get_type(policy), MPOL_DEFAULT, MPOL_LOCAL) || | |
45 | (numa_policy_get_type(policy) == MPOL_PREFERRED && !policy->nodes.set)) { | |
46 | *ret_nodes = NULL; | |
47 | *ret_maxnode = 0; | |
48 | return 0; | |
49 | } | |
50 | ||
51 | bits = policy->nodes.allocated * 8; | |
52 | ulong_bits = sizeof(unsigned long) * 8; | |
53 | ||
54 | out = new0(unsigned long, DIV_ROUND_UP(policy->nodes.allocated, sizeof(unsigned long))); | |
55 | if (!out) | |
56 | return -ENOMEM; | |
57 | ||
58 | /* We don't make any assumptions about internal type libc is using to store NUMA node mask. | |
59 | Hence we need to convert the node mask to the representation expected by set_mempolicy() */ | |
60 | for (node = 0; node < bits; node++) | |
61 | if (CPU_ISSET_S(node, policy->nodes.allocated, policy->nodes.set)) | |
62 | out[node / ulong_bits] |= 1ul << (node % ulong_bits); | |
63 | ||
64 | *ret_nodes = TAKE_PTR(out); | |
65 | *ret_maxnode = bits + 1; | |
66 | return 0; | |
67 | } | |
68 | ||
69 | int apply_numa_policy(const NUMAPolicy *policy) { | |
70 | int r; | |
71 | _cleanup_free_ unsigned long *nodes = NULL; | |
72 | unsigned long maxnode; | |
73 | ||
74 | assert(policy); | |
75 | ||
70923ed3 | 76 | if (get_mempolicy(NULL, NULL, 0, NULL, 0) < 0 && errno == ENOSYS) |
1808f768 MS |
77 | return -EOPNOTSUPP; |
78 | ||
79 | if (!numa_policy_is_valid(policy)) | |
80 | return -EINVAL; | |
81 | ||
82 | r = numa_policy_to_mempolicy(policy, &maxnode, &nodes); | |
83 | if (r < 0) | |
84 | return r; | |
85 | ||
86 | r = set_mempolicy(numa_policy_get_type(policy), nodes, maxnode); | |
87 | if (r < 0) | |
88 | return -errno; | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) { | |
296fe3d5 | 94 | _cleanup_(cpu_set_done) CPUSet s = {}; |
1808f768 | 95 | int r; |
1808f768 MS |
96 | |
97 | assert(policy); | |
98 | assert(ret); | |
99 | ||
296fe3d5 | 100 | for (size_t i = 0; i < policy->nodes.allocated * 8; i++) { |
1808f768 MS |
101 | _cleanup_free_ char *l = NULL; |
102 | char p[STRLEN("/sys/devices/system/node/node//cpulist") + DECIMAL_STR_MAX(size_t) + 1]; | |
1808f768 MS |
103 | |
104 | if (!CPU_ISSET_S(i, policy->nodes.allocated, policy->nodes.set)) | |
105 | continue; | |
106 | ||
107 | xsprintf(p, "/sys/devices/system/node/node%zu/cpulist", i); | |
108 | ||
109 | r = read_one_line_file(p, &l); | |
110 | if (r < 0) | |
111 | return r; | |
112 | ||
296fe3d5 | 113 | _cleanup_(cpu_set_done) CPUSet part = {}; |
1808f768 MS |
114 | r = parse_cpu_set(l, &part); |
115 | if (r < 0) | |
116 | return r; | |
117 | ||
f4093f43 | 118 | r = cpu_set_add_set(&s, &part); |
1808f768 MS |
119 | if (r < 0) |
120 | return r; | |
121 | } | |
122 | ||
088d71f8 | 123 | *ret = TAKE_STRUCT(s); |
1808f768 MS |
124 | |
125 | return 0; | |
126 | } | |
127 | ||
332d387f MS |
128 | static int numa_max_node(void) { |
129 | _cleanup_closedir_ DIR *d = NULL; | |
332d387f MS |
130 | int r, max_node = 0; |
131 | ||
132 | d = opendir("/sys/devices/system/node"); | |
133 | if (!d) | |
134 | return -errno; | |
135 | ||
136 | FOREACH_DIRENT(de, d, break) { | |
137 | int node; | |
138 | const char *n; | |
139 | ||
332d387f MS |
140 | if (de->d_type != DT_DIR) |
141 | continue; | |
142 | ||
143 | n = startswith(de->d_name, "node"); | |
144 | if (!n) | |
145 | continue; | |
146 | ||
147 | r = safe_atoi(n, &node); | |
148 | if (r < 0) | |
149 | continue; | |
150 | ||
151 | if (node > max_node) | |
152 | max_node = node; | |
153 | } | |
154 | ||
155 | return max_node; | |
156 | } | |
157 | ||
158 | int numa_mask_add_all(CPUSet *mask) { | |
159 | int m; | |
160 | ||
161 | assert(mask); | |
162 | ||
163 | m = numa_max_node(); | |
164 | if (m < 0) { | |
165 | log_debug_errno(m, "Failed to determine maximum NUMA node index, assuming 1023: %m"); | |
166 | m = 1023; /* CONFIG_NODES_SHIFT is set to 10 on x86_64, i.e. 1024 NUMA nodes in total */ | |
167 | } | |
168 | ||
169 | for (int i = 0; i <= m; i++) { | |
170 | int r; | |
171 | ||
172 | r = cpu_set_add(mask, i); | |
173 | if (r < 0) | |
174 | return r; | |
175 | } | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
1808f768 MS |
180 | static const char* const mpol_table[] = { |
181 | [MPOL_DEFAULT] = "default", | |
182 | [MPOL_PREFERRED] = "preferred", | |
183 | [MPOL_BIND] = "bind", | |
184 | [MPOL_INTERLEAVE] = "interleave", | |
185 | [MPOL_LOCAL] = "local", | |
186 | }; | |
187 | ||
188 | DEFINE_STRING_TABLE_LOOKUP(mpol, int); |