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>
17 #include <urcu/compiler.h>
19 #include "common/align.h"
20 #include "common/logging.h"
21 #include "common/smp.h"
23 static int num_possible_cpus_cache
;
25 #if (defined(__GLIBC__) || defined( __UCLIBC__))
26 int get_num_possible_cpus_fallback(void)
28 /* On Linux, when some processors are offline
29 * _SC_NPROCESSORS_CONF counts the offline
30 * processors, whereas _SC_NPROCESSORS_ONLN
31 * does not. If we used _SC_NPROCESSORS_ONLN,
32 * getcpu() could return a value greater than
33 * this sysconf, in which case the arrays
34 * indexed by processor would overflow.
36 return sysconf(_SC_NPROCESSORS_CONF
);
42 * The MUSL libc implementation of the _SC_NPROCESSORS_CONF sysconf does not
43 * return the number of configured CPUs in the system but relies on the cpu
44 * affinity mask of the current task.
46 * So instead we use a strategy similar to GLIBC's, counting the cpu
47 * directories in "/sys/devices/system/cpu" and fallback on the value from
48 * sysconf if it fails.
55 #include <sys/types.h>
57 #define __max(a,b) ((a)>(b)?(a):(b))
59 int get_num_possible_cpus_fallback(void)
65 cpudir
= opendir("/sys/devices/system/cpu");
70 * Count the number of directories named "cpu" followed by and
71 * integer. This is the same strategy as glibc uses.
73 while ((entry
= readdir(cpudir
))) {
74 if (entry
->d_type
== DT_DIR
&&
75 strncmp(entry
->d_name
, "cpu", 3) == 0) {
78 unsigned long cpu_num
;
80 cpu_num
= strtoul(entry
->d_name
+ 3, &endptr
, 10);
81 if ((cpu_num
< ULONG_MAX
) && (endptr
!= entry
->d_name
+ 3)
82 && (*endptr
== '\0')) {
90 * Get the sysconf value as a fallback. Keep the highest number.
92 return __max(sysconf(_SC_NPROCESSORS_CONF
), count
);
97 * Get the CPU possible mask string from sysfs.
99 * buf: the buffer where the mask will be read.
100 * max_bytes: the maximum number of bytes to write in the buffer.
102 * Returns the number of bytes read or -1 on error.
104 int get_possible_cpu_mask_from_sysfs(char *buf
, size_t max_bytes
)
106 ssize_t bytes_read
= 0;
107 size_t total_bytes_read
= 0;
113 fd
= open("/sys/devices/system/cpu/possible", O_RDONLY
);
118 bytes_read
= read(fd
, buf
+ total_bytes_read
,
119 max_bytes
- total_bytes_read
);
121 if (bytes_read
< 0) {
122 if (errno
== EINTR
) {
123 continue; /* retry operation */
129 total_bytes_read
+= bytes_read
;
130 assert(total_bytes_read
<= max_bytes
);
131 } while (max_bytes
> total_bytes_read
&& bytes_read
> 0);
137 * Make sure the mask read is a null terminated string.
139 if (total_bytes_read
< max_bytes
)
140 buf
[total_bytes_read
] = '\0';
142 buf
[max_bytes
- 1] = '\0';
144 return total_bytes_read
;
148 * Get the number of CPUs from the possible cpu mask.
150 * pmask: the mask to parse.
151 * len: the len of the mask excluding '\0'.
153 * Returns the number of possible CPUs from the mask or 0 on error.
155 int get_num_possible_cpus_from_mask(const char *pmask
, size_t len
)
158 unsigned long cpu_index
;
161 /* We need at least one char to read */
165 /* Start from the end to read the last CPU index. */
166 for (i
= len
- 1; i
> 0; i
--) {
167 /* Break when we hit the first separator. */
168 if ((pmask
[i
] == ',') || (pmask
[i
] == '-')) {
174 cpu_index
= strtoul(&pmask
[i
], &endptr
, 10);
177 * If we read a CPU index, increment it by one to return a number of
180 if ((&pmask
[i
] != endptr
) && (cpu_index
< INT_MAX
))
181 return (int) cpu_index
+ 1;
187 static void _get_num_possible_cpus(void)
190 int buf_len
= LTTNG_UST_PAGE_SIZE
;
193 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
194 ret
= get_possible_cpu_mask_from_sysfs((char *) &buf
, buf_len
);
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.041014 seconds and 4 git commands to generate.