Commit | Line | Data |
---|---|---|
0a65ebfb MD |
1 | // SPDX-License-Identifier: MIT |
2 | /* | |
3 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> | |
5 | */ | |
6 | ||
7 | #define _LGPL_SOURCE | |
8 | #include <assert.h> | |
9 | #include <ctype.h> | |
10 | #include <dirent.h> | |
11 | #include <errno.h> | |
12 | #include <fcntl.h> | |
13 | #include <limits.h> | |
14 | #include <unistd.h> | |
15 | #include <pthread.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <stdio.h> | |
19 | #include <sys/types.h> | |
67337c4a | 20 | #include <side/macros.h> |
0a65ebfb MD |
21 | |
22 | #include "smp.h" | |
23 | ||
24 | #define __max(a,b) ((a)>(b)?(a):(b)) | |
25 | ||
67337c4a | 26 | #define SIDE_CPUMASK_SIZE 4096 |
0a65ebfb MD |
27 | |
28 | static int possible_cpus_array_len_cache; | |
29 | ||
30 | static | |
31 | int _get_max_cpuid_from_sysfs(const char *path) | |
32 | { | |
33 | long max_cpuid = -1; | |
34 | ||
35 | DIR *cpudir; | |
36 | struct dirent *entry; | |
37 | ||
38 | assert(path); | |
39 | ||
40 | cpudir = opendir(path); | |
41 | if (cpudir == NULL) | |
42 | goto end; | |
43 | ||
44 | /* | |
45 | * Iterate on all directories named "cpu" followed by an integer. | |
46 | */ | |
47 | while ((entry = readdir(cpudir))) { | |
48 | if (entry->d_type == DT_DIR && | |
49 | strncmp(entry->d_name, "cpu", 3) == 0) { | |
50 | ||
51 | char *endptr; | |
52 | long cpu_id; | |
53 | ||
54 | cpu_id = strtol(entry->d_name + 3, &endptr, 10); | |
55 | if ((cpu_id < LONG_MAX) && (endptr != entry->d_name + 3) | |
56 | && (*endptr == '\0')) { | |
57 | if (cpu_id > max_cpuid) | |
58 | max_cpuid = cpu_id; | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | if (closedir(cpudir)) | |
64 | perror("closedir"); | |
65 | ||
66 | /* | |
67 | * If the max CPU id is out of bound, set it to -1 so it results in a | |
68 | * CPU num of 0. | |
69 | */ | |
70 | if (max_cpuid < 0 || max_cpuid > INT_MAX) | |
71 | max_cpuid = -1; | |
72 | ||
73 | end: | |
74 | return max_cpuid; | |
75 | } | |
76 | ||
77 | /* | |
78 | * Get the highest CPU id from sysfs. | |
79 | * | |
80 | * Iterate on all the folders in "/sys/devices/system/cpu" that start with | |
81 | * "cpu" followed by an integer, keep the highest CPU id encountered during | |
82 | * this iteration and add 1 to get a number of CPUs. | |
83 | * | |
84 | * Returns the highest CPU id, or -1 on error. | |
85 | */ | |
86 | static | |
87 | int get_max_cpuid_from_sysfs(void) | |
88 | { | |
89 | return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu"); | |
90 | } | |
91 | ||
92 | /* | |
93 | * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible", | |
94 | * iterate on all the folders in "/sys/devices/system/cpu" that start with | |
95 | * "cpu" followed by an integer, keep the highest CPU id encountered during | |
96 | * this iteration and add 1 to get a number of CPUs. | |
97 | * | |
98 | * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and | |
99 | * return the highest one. | |
100 | * | |
101 | * On Linux, using the value from sysconf can be unreliable since the way it | |
102 | * counts CPUs varies between C libraries and even between versions of the same | |
103 | * library. If we used it directly, getcpu() could return a value greater than | |
104 | * this sysconf, in which case the arrays indexed by processor would overflow. | |
105 | * | |
106 | * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF | |
107 | * sysconf does not return the number of configured CPUs in the system but | |
108 | * relies on the cpu affinity mask of the current task. | |
109 | * | |
110 | * Returns 0 or less on error. | |
111 | */ | |
112 | static | |
113 | int get_num_possible_cpus_fallback(void) | |
114 | { | |
115 | /* | |
116 | * Get the sysconf value as a last resort. Keep the highest number. | |
117 | */ | |
118 | return __max(sysconf(_SC_NPROCESSORS_CONF), get_max_cpuid_from_sysfs() + 1); | |
119 | } | |
120 | ||
121 | /* | |
122 | * Get a CPU mask string from sysfs. | |
123 | * | |
124 | * buf: the buffer where the mask will be read. | |
125 | * max_bytes: the maximum number of bytes to write in the buffer. | |
126 | * path: file path to read the mask from. | |
127 | * | |
128 | * Returns the number of bytes read or -1 on error. | |
129 | */ | |
130 | static | |
131 | int get_cpu_mask_from_sysfs(char *buf, size_t max_bytes, const char *path) | |
132 | { | |
133 | ssize_t bytes_read = 0; | |
134 | size_t total_bytes_read = 0; | |
135 | int fd = -1, ret = -1; | |
136 | ||
137 | assert(path); | |
138 | ||
139 | if (buf == NULL) | |
140 | goto end; | |
141 | ||
142 | fd = open(path, O_RDONLY); | |
143 | if (fd < 0) | |
144 | goto end; | |
145 | ||
146 | do { | |
147 | bytes_read = read(fd, buf + total_bytes_read, | |
148 | max_bytes - total_bytes_read); | |
149 | ||
150 | if (bytes_read < 0) { | |
151 | if (errno == EINTR) { | |
152 | continue; /* retry operation */ | |
153 | } else { | |
154 | goto end; | |
155 | } | |
156 | } | |
157 | ||
158 | total_bytes_read += bytes_read; | |
159 | assert(total_bytes_read <= max_bytes); | |
160 | } while (max_bytes > total_bytes_read && bytes_read > 0); | |
161 | ||
162 | /* | |
163 | * Make sure the mask read is a null terminated string. | |
164 | */ | |
165 | if (total_bytes_read < max_bytes) | |
166 | buf[total_bytes_read] = '\0'; | |
167 | else | |
168 | buf[max_bytes - 1] = '\0'; | |
169 | ||
170 | if (total_bytes_read > INT_MAX) | |
171 | goto end; | |
172 | ret = (int) total_bytes_read; | |
173 | end: | |
174 | if (fd >= 0 && close(fd) < 0) | |
175 | perror("close"); | |
176 | return ret; | |
177 | } | |
178 | ||
179 | /* | |
180 | * Get the CPU possible mask string from sysfs. | |
181 | * | |
182 | * buf: the buffer where the mask will be read. | |
183 | * max_bytes: the maximum number of bytes to write in the buffer. | |
184 | * | |
185 | * Returns the number of bytes read or -1 on error. | |
186 | */ | |
187 | static | |
188 | int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) | |
189 | { | |
190 | return get_cpu_mask_from_sysfs(buf, max_bytes, | |
191 | "/sys/devices/system/cpu/possible"); | |
192 | } | |
193 | ||
194 | /* | |
195 | * Get the highest CPU id from a CPU mask. | |
196 | * | |
197 | * pmask: the mask to parse. | |
198 | * len: the len of the mask excluding '\0'. | |
199 | * | |
200 | * Returns the highest CPU id from the mask or -1 on error. | |
201 | */ | |
202 | static | |
203 | int get_max_cpuid_from_mask(const char *pmask, size_t len) | |
204 | { | |
205 | ssize_t i; | |
206 | unsigned long cpu_index; | |
207 | char *endptr; | |
208 | ||
209 | /* We need at least one char to read */ | |
210 | if (len < 1) | |
211 | goto error; | |
212 | ||
213 | /* Start from the end to read the last CPU index. */ | |
214 | for (i = len - 1; i > 0; i--) { | |
215 | /* Break when we hit the first separator. */ | |
216 | if ((pmask[i] == ',') || (pmask[i] == '-')) { | |
217 | i++; | |
218 | break; | |
219 | } | |
220 | } | |
221 | ||
222 | cpu_index = strtoul(&pmask[i], &endptr, 10); | |
223 | ||
224 | if ((&pmask[i] != endptr) && (cpu_index < INT_MAX)) | |
225 | return (int) cpu_index; | |
226 | ||
227 | error: | |
228 | return -1; | |
229 | } | |
230 | ||
231 | static void update_possible_cpus_array_len_cache(void) | |
232 | { | |
67337c4a | 233 | char buf[SIDE_CPUMASK_SIZE]; |
0a65ebfb MD |
234 | int ret; |
235 | ||
236 | /* Get the possible cpu mask from sysfs, fallback to sysconf. */ | |
67337c4a | 237 | ret = get_possible_cpu_mask_from_sysfs((char *) &buf, SIDE_CPUMASK_SIZE); |
0a65ebfb MD |
238 | if (ret <= 0) |
239 | goto fallback; | |
240 | ||
241 | /* Parse the possible cpu mask, on failure fallback to sysconf. */ | |
242 | ret = get_max_cpuid_from_mask((char *) &buf, ret); | |
243 | if (ret >= 0) { | |
244 | /* Add 1 to convert from max cpuid to an array len. */ | |
245 | ret++; | |
246 | goto end; | |
247 | } | |
248 | ||
249 | fallback: | |
250 | /* Fallback to sysconf. */ | |
251 | ret = get_num_possible_cpus_fallback(); | |
252 | ||
253 | end: | |
254 | /* If all methods failed, don't store the value. */ | |
255 | if (ret < 1) | |
256 | return; | |
257 | ||
258 | possible_cpus_array_len_cache = ret; | |
259 | } | |
260 | ||
261 | /* | |
262 | * Returns the length of an array that could contain a per-CPU element for each | |
263 | * possible CPU id for the lifetime of the process. | |
264 | * | |
265 | * We currently assume CPU ids are contiguous up the maximum CPU id. | |
266 | * | |
267 | * If the cache is not yet initialized, get the value from | |
268 | * "/sys/devices/system/cpu/possible" or fallback to sysconf and cache it. | |
269 | * | |
270 | * If all methods fail, don't populate the cache and return 0. | |
271 | */ | |
272 | int get_possible_cpus_array_len(void) | |
273 | { | |
67337c4a | 274 | if (side_unlikely(!possible_cpus_array_len_cache)) |
0a65ebfb MD |
275 | update_possible_cpus_array_len_cache(); |
276 | ||
277 | return possible_cpus_array_len_cache; | |
278 | } |