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>
20 #include <urcu/compiler.h>
22 #define URCU_CPUMASK_SIZE 4096
24 #if defined(HAVE_SYSCONF)
25 static inline int get_num_possible_cpus_sysconf(void)
27 return sysconf(_SC_NPROCESSORS_CONF
);
31 * On platforms without sysconf(), always return -1.
33 static inline int get_num_possible_cpus_sysconf(void)
41 * Get the highest CPU id from sysfs.
43 * Iterate on all the folders in "/sys/devices/system/cpu" that start with
44 * "cpu" followed by an integer, keep the highest CPU id encountered during
45 * this iteration and add 1 to get a number of CPUs.
47 * Returns the highest CPU id, or -1 on error.
49 static inline int _get_max_cpuid_from_sysfs(const char *path
)
58 cpudir
= opendir(path
);
63 * Iterate on all directories named "cpu" followed by an integer.
65 while ((entry
= readdir(cpudir
))) {
66 if (entry
->d_type
== DT_DIR
&&
67 strncmp(entry
->d_name
, "cpu", 3) == 0) {
72 cpu_id
= strtol(entry
->d_name
+ 3, &endptr
, 10);
73 if ((cpu_id
< LONG_MAX
) && (endptr
!= entry
->d_name
+ 3)
74 && (*endptr
== '\0')) {
75 if (cpu_id
> max_cpuid
)
85 * If the max CPU id is out of bound, set it to -1 so it results in a
88 if (max_cpuid
< 0 || max_cpuid
> INT_MAX
)
95 static inline int get_max_cpuid_from_sysfs(void)
97 return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu");
102 * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible",
103 * iterate on all the folders in "/sys/devices/system/cpu" that start with
104 * "cpu" followed by an integer, keep the highest CPU id encountered during
105 * this iteration and add 1 to get a number of CPUs.
107 * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and
108 * return the highest one.
110 * On Linux, using the value from sysconf can be unreliable since the way it
111 * counts CPUs varies between C libraries and even between versions of the same
112 * library. If we used it directly, getcpu() could return a value greater than
113 * this sysconf, in which case the arrays indexed by processor would overflow.
115 * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF
116 * sysconf does not return the number of configured CPUs in the system but
117 * relies on the cpu affinity mask of the current task.
119 * Returns 0 or less on error.
121 static inline int get_num_possible_cpus_fallback(void)
124 * Get the sysconf value as a last resort. Keep the highest number.
126 return caa_max(get_num_possible_cpus_sysconf(), get_max_cpuid_from_sysfs() + 1);
130 * Get a CPU mask string from sysfs.
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.
136 * Returns the number of bytes read or -1 on error.
138 static inline int get_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
, const char *path
)
140 ssize_t bytes_read
= 0;
141 size_t total_bytes_read
= 0;
142 int fd
= -1, ret
= -1;
149 fd
= open(path
, O_RDONLY
);
154 bytes_read
= read(fd
, buf
+ total_bytes_read
,
155 max_bytes
- total_bytes_read
);
157 if (bytes_read
< 0) {
158 if (errno
== EINTR
) {
159 continue; /* retry operation */
165 total_bytes_read
+= bytes_read
;
166 assert(total_bytes_read
<= max_bytes
);
167 } while (max_bytes
> total_bytes_read
&& bytes_read
> 0);
170 * Make sure the mask read is a null terminated string.
172 if (total_bytes_read
< max_bytes
)
173 buf
[total_bytes_read
] = '\0';
175 buf
[max_bytes
- 1] = '\0';
177 if (total_bytes_read
> INT_MAX
)
180 ret
= (int) total_bytes_read
;
183 if (fd
>= 0 && close(fd
) < 0)
190 * Get the CPU possible mask string from sysfs.
192 * buf: the buffer where the mask will be read.
193 * max_bytes: the maximum number of bytes to write in the buffer.
195 * Returns the number of bytes read or -1 on error.
197 static inline int get_possible_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
)
199 return get_cpu_mask_from_sysfs(buf
, max_bytes
,
200 "/sys/devices/system/cpu/possible");
204 * Get the highest CPU id from the possible CPU mask.
206 * pmask: the mask to parse.
207 * len: the len of the mask excluding '\0'.
209 * Returns the highest CPU id from the mask or -1 on error.
211 static inline int get_max_cpuid_from_mask(const char *pmask
, size_t len
)
214 unsigned long cpu_index
;
217 /* We need at least one char to read */
221 /* Start from the end to read the last CPU index. */
222 for (i
= len
- 1; i
> 0; i
--) {
223 /* Break when we hit the first separator. */
224 if ((pmask
[i
] == ',') || (pmask
[i
] == '-')) {
230 cpu_index
= strtoul(&pmask
[i
], &endptr
, 10);
232 if ((&pmask
[i
] != endptr
) && (cpu_index
< INT_MAX
))
233 return (int) cpu_index
;
240 * On Linux try sysfs first and fallback to sysconf.
242 static inline int get_possible_cpus_array_len(void)
245 char buf
[URCU_CPUMASK_SIZE
];
247 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
248 ret
= get_possible_cpu_mask_from_sysfs((char *) &buf
, URCU_CPUMASK_SIZE
);
252 /* Parse the possible cpu mask, on failure fallback to sysconf. */
253 ret
= get_max_cpuid_from_mask((char *) &buf
, ret
);
255 /* Add 1 to convert from max cpuid to an array len. */
261 /* Fallback to sysconf. */
262 ret
= get_num_possible_cpus_fallback();
269 * On other platforms, only use sysconf.
271 static inline int get_possible_cpus_array_len(void)
273 return get_num_possible_cpus_sysconf();
This page took 0.034245 seconds and 4 git commands to generate.