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 num_possible_cpus_cache
;
31 * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible",
32 * iterate on all the folders in "/sys/devices/system/cpu" that start with
33 * "cpu" followed by an integer, keep the highest CPU id encountered during
34 * this iteration and add 1 to get a number of CPUs.
36 * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and
37 * return the highest one.
39 * On Linux, using the value from sysconf can be unreliable since the way it
40 * counts CPUs varies between C libraries and even between versions of the same
41 * library. If we used it directly, getcpu() could return a value greater than
42 * this sysconf, in which case the arrays indexed by processor would overflow.
44 * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF
45 * sysconf does not return the number of configured CPUs in the system but
46 * relies on the cpu affinity mask of the current task.
48 * Returns 0 or less on error.
50 int get_num_possible_cpus_fallback(void)
57 cpudir
= opendir("/sys/devices/system/cpu");
62 * Iterate on all directories named "cpu" followed by an integer.
64 while ((entry
= readdir(cpudir
))) {
65 if (entry
->d_type
== DT_DIR
&&
66 strncmp(entry
->d_name
, "cpu", 3) == 0) {
71 cpu_id
= strtol(entry
->d_name
+ 3, &endptr
, 10);
72 if ((cpu_id
< LONG_MAX
) && (endptr
!= entry
->d_name
+ 3)
73 && (*endptr
== '\0')) {
74 if (cpu_id
> max_cpuid
)
81 * If the max CPU id is out of bound, set it to -1 so it results in a
84 if (max_cpuid
< 0 || max_cpuid
> INT_MAX
)
89 * Get the sysconf value as a last resort. Keep the highest number.
91 return __max(sysconf(_SC_NPROCESSORS_CONF
), max_cpuid
+ 1);
95 * Get the CPU possible mask string from sysfs.
97 * buf: the buffer where the mask will be read.
98 * max_bytes: the maximum number of bytes to write in the buffer.
100 * Returns the number of bytes read or -1 on error.
102 int get_possible_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
)
104 ssize_t bytes_read
= 0;
105 size_t total_bytes_read
= 0;
106 int fd
= -1, ret
= -1;
111 fd
= open("/sys/devices/system/cpu/possible", O_RDONLY
);
116 bytes_read
= read(fd
, buf
+ total_bytes_read
,
117 max_bytes
- total_bytes_read
);
119 if (bytes_read
< 0) {
120 if (errno
== EINTR
) {
121 continue; /* retry operation */
127 total_bytes_read
+= bytes_read
;
128 assert(total_bytes_read
<= max_bytes
);
129 } while (max_bytes
> total_bytes_read
&& bytes_read
> 0);
132 * Make sure the mask read is a null terminated string.
134 if (total_bytes_read
< max_bytes
)
135 buf
[total_bytes_read
] = '\0';
137 buf
[max_bytes
- 1] = '\0';
139 if (total_bytes_read
> INT_MAX
)
141 ret
= (int) total_bytes_read
;
143 if (fd
>= 0 && close(fd
) < 0)
149 * Get the number of CPUs from the possible cpu mask.
151 * pmask: the mask to parse.
152 * len: the len of the mask excluding '\0'.
154 * Returns the number of possible CPUs from the mask or 0 on error.
156 int get_num_possible_cpus_from_mask(const char *pmask
, size_t len
)
159 unsigned long cpu_index
;
162 /* We need at least one char to read */
166 /* Start from the end to read the last CPU index. */
167 for (i
= len
- 1; i
> 0; i
--) {
168 /* Break when we hit the first separator. */
169 if ((pmask
[i
] == ',') || (pmask
[i
] == '-')) {
175 cpu_index
= strtoul(&pmask
[i
], &endptr
, 10);
178 * If we read a CPU index, increment it by one to return a number of
181 if ((&pmask
[i
] != endptr
) && (cpu_index
< INT_MAX
))
182 return (int) cpu_index
+ 1;
188 static void _get_num_possible_cpus(void)
191 char buf
[LTTNG_UST_CPUMASK_SIZE
];
193 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
194 ret
= get_possible_cpu_mask_from_sysfs((char *) &buf
, LTTNG_UST_CPUMASK_SIZE
);
198 /* Parse the possible cpu mask, on failure fallback to sysconf. */
199 ret
= get_num_possible_cpus_from_mask((char *) &buf
, ret
);
204 /* Fallback to sysconf. */
205 ret
= get_num_possible_cpus_fallback();
208 /* If all methods failed, don't store the value. */
212 num_possible_cpus_cache
= ret
;
216 * Returns the total number of CPUs in the system. If the cache is not yet
217 * initialized, get the value from "/sys/devices/system/cpu/possible" or
218 * fallback to sysconf and cache it.
220 * If all methods fail, don't populate the cache and return 0.
222 int num_possible_cpus(void)
224 if (caa_unlikely(!num_possible_cpus_cache
))
225 _get_num_possible_cpus();
227 return num_possible_cpus_cache
;
This page took 0.034571 seconds and 5 git commands to generate.