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)
40 * Get the highest CPU id from sysfs.
42 * Iterate on all the folders in "/sys/devices/system/cpu" that start with
43 * "cpu" followed by an integer, keep the highest CPU id encountered during
44 * this iteration and add 1 to get a number of CPUs.
46 * Returns the highest CPU id, or -1 on error.
48 static inline int _get_max_cpuid_from_sysfs(const char *path
)
57 cpudir
= opendir(path
);
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
)
84 * If the max CPU id is out of bound, set it to -1 so it results in a
87 if (max_cpuid
< 0 || max_cpuid
> INT_MAX
)
94 static inline int get_max_cpuid_from_sysfs(void)
96 return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu");
101 * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible",
102 * iterate on all the folders in "/sys/devices/system/cpu" that start with
103 * "cpu" followed by an integer, keep the highest CPU id encountered during
104 * this iteration and add 1 to get a number of CPUs.
106 * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and
107 * return the highest one.
109 * On Linux, using the value from sysconf can be unreliable since the way it
110 * counts CPUs varies between C libraries and even between versions of the same
111 * library. If we used it directly, getcpu() could return a value greater than
112 * this sysconf, in which case the arrays indexed by processor would overflow.
114 * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF
115 * sysconf does not return the number of configured CPUs in the system but
116 * relies on the cpu affinity mask of the current task.
118 * Returns 0 or less on error.
120 static inline int get_num_possible_cpus_fallback(void)
123 * Get the sysconf value as a last resort. Keep the highest number.
125 return caa_max(get_num_possible_cpus_sysconf(), get_max_cpuid_from_sysfs() + 1);
129 * Get a CPU mask string from sysfs.
131 * buf: the buffer where the mask will be read.
132 * max_bytes: the maximum number of bytes to write in the buffer.
133 * path: file path to read the mask from.
135 * Returns the number of bytes read or -1 on error.
137 static inline int get_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
, const char *path
)
139 ssize_t bytes_read
= 0;
140 size_t total_bytes_read
= 0;
141 int fd
= -1, ret
= -1;
148 fd
= open(path
, O_RDONLY
);
153 bytes_read
= read(fd
, buf
+ total_bytes_read
,
154 max_bytes
- total_bytes_read
);
156 if (bytes_read
< 0) {
157 if (errno
== EINTR
) {
158 continue; /* retry operation */
164 total_bytes_read
+= bytes_read
;
165 assert(total_bytes_read
<= max_bytes
);
166 } while (max_bytes
> total_bytes_read
&& bytes_read
> 0);
169 * Make sure the mask read is a null terminated string.
171 if (total_bytes_read
< max_bytes
)
172 buf
[total_bytes_read
] = '\0';
174 buf
[max_bytes
- 1] = '\0';
176 if (total_bytes_read
> INT_MAX
)
179 ret
= (int) total_bytes_read
;
182 if (fd
>= 0 && close(fd
) < 0)
189 * Get the CPU possible mask string from sysfs.
191 * buf: the buffer where the mask will be read.
192 * max_bytes: the maximum number of bytes to write in the buffer.
194 * Returns the number of bytes read or -1 on error.
196 static inline int get_possible_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
)
198 return get_cpu_mask_from_sysfs(buf
, max_bytes
,
199 "/sys/devices/system/cpu/possible");
203 * Get the highest CPU id from the possible CPU mask.
205 * pmask: the mask to parse.
206 * len: the len of the mask excluding '\0'.
208 * Returns the highest CPU id from the mask or -1 on error.
210 static inline int get_max_cpuid_from_mask(const char *pmask
, size_t len
)
213 unsigned long cpu_index
;
216 /* We need at least one char to read */
220 /* Start from the end to read the last CPU index. */
221 for (i
= len
- 1; i
> 0; i
--) {
222 /* Break when we hit the first separator. */
223 if ((pmask
[i
] == ',') || (pmask
[i
] == '-')) {
229 cpu_index
= strtoul(&pmask
[i
], &endptr
, 10);
231 if ((&pmask
[i
] != endptr
) && (cpu_index
< INT_MAX
))
232 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.03566 seconds and 5 git commands to generate.