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>
18 #include <urcu/compiler.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;
108 int fd
= -1, ret
= -1;
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);
134 * Make sure the mask read is a null terminated string.
136 if (total_bytes_read
< max_bytes
)
137 buf
[total_bytes_read
] = '\0';
139 buf
[max_bytes
- 1] = '\0';
141 if (total_bytes_read
> INT_MAX
)
143 ret
= (int) total_bytes_read
;
145 if (fd
>= 0 && close(fd
) < 0)
151 * Get the number of CPUs from the possible cpu mask.
153 * pmask: the mask to parse.
154 * len: the len of the mask excluding '\0'.
156 * Returns the number of possible CPUs from the mask or 0 on error.
158 int get_num_possible_cpus_from_mask(const char *pmask
, size_t len
)
161 unsigned long cpu_index
;
164 /* We need at least one char to read */
168 /* Start from the end to read the last CPU index. */
169 for (i
= len
- 1; i
> 0; i
--) {
170 /* Break when we hit the first separator. */
171 if ((pmask
[i
] == ',') || (pmask
[i
] == '-')) {
177 cpu_index
= strtoul(&pmask
[i
], &endptr
, 10);
180 * If we read a CPU index, increment it by one to return a number of
183 if ((&pmask
[i
] != endptr
) && (cpu_index
< INT_MAX
))
184 return (int) cpu_index
+ 1;
190 static void _get_num_possible_cpus(void)
193 char buf
[LTTNG_UST_CPUMASK_SIZE
];
195 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
196 ret
= get_possible_cpu_mask_from_sysfs((char *) &buf
, LTTNG_UST_CPUMASK_SIZE
);
200 /* Parse the possible cpu mask, on failure fallback to sysconf. */
201 ret
= get_num_possible_cpus_from_mask((char *) &buf
, ret
);
206 /* Fallback to sysconf. */
207 ret
= get_num_possible_cpus_fallback();
210 /* If all methods failed, don't store the value. */
214 num_possible_cpus_cache
= ret
;
218 * Returns the total number of CPUs in the system. If the cache is not yet
219 * initialized, get the value from "/sys/devices/system/cpu/possible" or
220 * fallback to sysconf and cache it.
222 * If all methods fail, don't populate the cache and return 0.
224 int num_possible_cpus(void)
226 if (caa_unlikely(!num_possible_cpus_cache
))
227 _get_num_possible_cpus();
229 return num_possible_cpus_cache
;
This page took 0.03414 seconds and 4 git commands to generate.