2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
19 #include <sys/types.h>
21 #include <urcu/compiler.h>
23 #include "common/logging.h"
24 #include "common/smp.h"
26 #define __max(a,b) ((a)>(b)?(a):(b))
28 static int possible_cpus_array_len_cache
;
31 * Get the highest CPU id from sysfs.
33 * Iterate on all the folders in "/sys/devices/system/cpu" that start with
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.
37 * Returns the highest CPU id, or -1 on error.
39 int get_max_cpuid_from_sysfs(void)
41 return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu");
44 int _get_max_cpuid_from_sysfs(const char *path
)
53 cpudir
= opendir(path
);
58 * Iterate on all directories named "cpu" followed by an integer.
60 while ((entry
= readdir(cpudir
))) {
61 if (entry
->d_type
== DT_DIR
&&
62 strncmp(entry
->d_name
, "cpu", 3) == 0) {
67 cpu_id
= strtol(entry
->d_name
+ 3, &endptr
, 10);
68 if ((cpu_id
< LONG_MAX
) && (endptr
!= entry
->d_name
+ 3)
69 && (*endptr
== '\0')) {
70 if (cpu_id
> max_cpuid
)
80 * If the max CPU id is out of bound, set it to -1 so it results in a
83 if (max_cpuid
< 0 || max_cpuid
> INT_MAX
)
91 * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible",
92 * iterate on all the folders in "/sys/devices/system/cpu" that start with
93 * "cpu" followed by an integer, keep the highest CPU id encountered during
94 * this iteration and add 1 to get a number of CPUs.
96 * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and
97 * return the highest one.
99 * On Linux, using the value from sysconf can be unreliable since the way it
100 * counts CPUs varies between C libraries and even between versions of the same
101 * library. If we used it directly, getcpu() could return a value greater than
102 * this sysconf, in which case the arrays indexed by processor would overflow.
104 * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF
105 * sysconf does not return the number of configured CPUs in the system but
106 * relies on the cpu affinity mask of the current task.
108 * Returns 0 or less on error.
110 int get_num_possible_cpus_fallback(void)
113 * Get the sysconf value as a last resort. Keep the highest number.
115 return __max(sysconf(_SC_NPROCESSORS_CONF
), get_max_cpuid_from_sysfs() + 1);
119 * Get the CPU possible mask string from sysfs.
121 * buf: the buffer where the mask will be read.
122 * max_bytes: the maximum number of bytes to write in the buffer.
124 * Returns the number of bytes read or -1 on error.
126 int get_possible_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
)
128 return get_cpu_mask_from_sysfs(buf
, max_bytes
,
129 "/sys/devices/system/cpu/possible");
133 * Get a CPU mask string from sysfs.
135 * buf: the buffer where the mask will be read.
136 * max_bytes: the maximum number of bytes to write in the buffer.
137 * path: file path to read the mask from.
139 * Returns the number of bytes read or -1 on error.
141 int get_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
, const char *path
)
143 ssize_t bytes_read
= 0;
144 size_t total_bytes_read
= 0;
145 int fd
= -1, ret
= -1;
152 fd
= open(path
, O_RDONLY
);
157 bytes_read
= read(fd
, buf
+ total_bytes_read
,
158 max_bytes
- total_bytes_read
);
160 if (bytes_read
< 0) {
161 if (errno
== EINTR
) {
162 continue; /* retry operation */
168 total_bytes_read
+= bytes_read
;
169 assert(total_bytes_read
<= max_bytes
);
170 } while (max_bytes
> total_bytes_read
&& bytes_read
> 0);
173 * Make sure the mask read is a null terminated string.
175 if (total_bytes_read
< max_bytes
)
176 buf
[total_bytes_read
] = '\0';
178 buf
[max_bytes
- 1] = '\0';
180 if (total_bytes_read
> INT_MAX
)
182 ret
= (int) total_bytes_read
;
184 if (fd
>= 0 && close(fd
) < 0)
190 * Get the highest CPU id from a CPU mask.
192 * pmask: the mask to parse.
193 * len: the len of the mask excluding '\0'.
195 * Returns the highest CPU id from the mask or -1 on error.
197 int get_max_cpuid_from_mask(const char *pmask
, size_t len
)
200 unsigned long cpu_index
;
203 /* We need at least one char to read */
207 /* Start from the end to read the last CPU index. */
208 for (i
= len
- 1; i
> 0; i
--) {
209 /* Break when we hit the first separator. */
210 if ((pmask
[i
] == ',') || (pmask
[i
] == '-')) {
216 cpu_index
= strtoul(&pmask
[i
], &endptr
, 10);
218 if ((&pmask
[i
] != endptr
) && (cpu_index
< INT_MAX
))
219 return (int) cpu_index
;
225 static void update_possible_cpus_array_len_cache(void)
228 char buf
[LTTNG_UST_CPUMASK_SIZE
];
230 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
231 ret
= get_possible_cpu_mask_from_sysfs((char *) &buf
, LTTNG_UST_CPUMASK_SIZE
);
235 /* Parse the possible cpu mask, on failure fallback to sysconf. */
236 ret
= get_max_cpuid_from_mask((char *) &buf
, ret
);
238 /* Add 1 to convert from max cpuid to an array len. */
244 /* Fallback to sysconf. */
245 ret
= get_num_possible_cpus_fallback();
248 /* If all methods failed, don't store the value. */
252 possible_cpus_array_len_cache
= ret
;
256 * Returns the length of an array that could contain a per-CPU element for each
257 * possible CPU id for the lifetime of the process.
259 * We currently assume CPU ids are contiguous up the maximum CPU id.
261 * If the cache is not yet initialized, get the value from
262 * "/sys/devices/system/cpu/possible" or fallback to sysconf and cache it.
264 * If all methods fail, don't populate the cache and return 0.
266 int get_possible_cpus_array_len(void)
268 if (caa_unlikely(!possible_cpus_array_len_cache
))
269 update_possible_cpus_array_len_cache();
271 return possible_cpus_array_len_cache
;