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