Commit | Line | Data |
---|---|---|
ebabbf58 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
ebabbf58 MD |
3 | * |
4 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> | |
ebabbf58 MD |
6 | */ |
7 | ||
ebabbf58 | 8 | #define _LGPL_SOURCE |
022c53ed MJ |
9 | #include <assert.h> |
10 | #include <ctype.h> | |
08a0398c | 11 | #include <dirent.h> |
022c53ed MJ |
12 | #include <errno.h> |
13 | #include <fcntl.h> | |
73beabae | 14 | #include <limits.h> |
ebabbf58 MD |
15 | #include <unistd.h> |
16 | #include <pthread.h> | |
022c53ed | 17 | #include <stdlib.h> |
08a0398c MJ |
18 | #include <string.h> |
19 | #include <sys/types.h> | |
ebabbf58 | 20 | |
74cc1f59 MJ |
21 | #include <urcu/compiler.h> |
22 | ||
022c53ed | 23 | #include "common/logging.h" |
74cc1f59 MJ |
24 | #include "common/smp.h" |
25 | ||
08a0398c | 26 | #define __max(a,b) ((a)>(b)?(a):(b)) |
ebabbf58 | 27 | |
79030bb7 | 28 | static int possible_cpus_array_len_cache; |
ebabbf58 MD |
29 | |
30 | /* | |
08a0398c MJ |
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. | |
35 | * | |
36 | * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and | |
37 | * return the highest one. | |
38 | * | |
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. | |
43 | * | |
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. | |
ebabbf58 | 47 | * |
08a0398c | 48 | * Returns 0 or less on error. |
ebabbf58 | 49 | */ |
022c53ed | 50 | int get_num_possible_cpus_fallback(void) |
ebabbf58 | 51 | { |
08a0398c MJ |
52 | long max_cpuid = -1; |
53 | ||
ebabbf58 MD |
54 | DIR *cpudir; |
55 | struct dirent *entry; | |
56 | ||
57 | cpudir = opendir("/sys/devices/system/cpu"); | |
58 | if (cpudir == NULL) | |
59 | goto end; | |
60 | ||
61 | /* | |
08a0398c | 62 | * Iterate on all directories named "cpu" followed by an integer. |
ebabbf58 MD |
63 | */ |
64 | while ((entry = readdir(cpudir))) { | |
65 | if (entry->d_type == DT_DIR && | |
66 | strncmp(entry->d_name, "cpu", 3) == 0) { | |
67 | ||
68 | char *endptr; | |
08a0398c | 69 | long cpu_id; |
ebabbf58 | 70 | |
08a0398c MJ |
71 | cpu_id = strtol(entry->d_name + 3, &endptr, 10); |
72 | if ((cpu_id < LONG_MAX) && (endptr != entry->d_name + 3) | |
ebabbf58 | 73 | && (*endptr == '\0')) { |
08a0398c MJ |
74 | if (cpu_id > max_cpuid) |
75 | max_cpuid = cpu_id; | |
ebabbf58 MD |
76 | } |
77 | } | |
78 | } | |
79 | ||
08a0398c MJ |
80 | /* |
81 | * If the max CPU id is out of bound, set it to -1 so it results in a | |
82 | * CPU num of 0. | |
83 | */ | |
84 | if (max_cpuid < 0 || max_cpuid > INT_MAX) | |
85 | max_cpuid = -1; | |
86 | ||
ebabbf58 MD |
87 | end: |
88 | /* | |
08a0398c | 89 | * Get the sysconf value as a last resort. Keep the highest number. |
ebabbf58 | 90 | */ |
08a0398c | 91 | return __max(sysconf(_SC_NPROCESSORS_CONF), max_cpuid + 1); |
022c53ed | 92 | } |
022c53ed MJ |
93 | |
94 | /* | |
95 | * Get the CPU possible mask string from sysfs. | |
96 | * | |
97 | * buf: the buffer where the mask will be read. | |
98 | * max_bytes: the maximum number of bytes to write in the buffer. | |
99 | * | |
100 | * Returns the number of bytes read or -1 on error. | |
101 | */ | |
102 | int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) | |
103 | { | |
104 | ssize_t bytes_read = 0; | |
105 | size_t total_bytes_read = 0; | |
377f1df5 | 106 | int fd = -1, ret = -1; |
022c53ed MJ |
107 | |
108 | if (buf == NULL) | |
377f1df5 | 109 | goto end; |
022c53ed MJ |
110 | |
111 | fd = open("/sys/devices/system/cpu/possible", O_RDONLY); | |
112 | if (fd < 0) | |
377f1df5 | 113 | goto end; |
022c53ed MJ |
114 | |
115 | do { | |
116 | bytes_read = read(fd, buf + total_bytes_read, | |
117 | max_bytes - total_bytes_read); | |
118 | ||
119 | if (bytes_read < 0) { | |
120 | if (errno == EINTR) { | |
121 | continue; /* retry operation */ | |
122 | } else { | |
377f1df5 | 123 | goto end; |
022c53ed MJ |
124 | } |
125 | } | |
126 | ||
127 | total_bytes_read += bytes_read; | |
128 | assert(total_bytes_read <= max_bytes); | |
129 | } while (max_bytes > total_bytes_read && bytes_read > 0); | |
130 | ||
022c53ed MJ |
131 | /* |
132 | * Make sure the mask read is a null terminated string. | |
133 | */ | |
134 | if (total_bytes_read < max_bytes) | |
135 | buf[total_bytes_read] = '\0'; | |
136 | else | |
137 | buf[max_bytes - 1] = '\0'; | |
138 | ||
377f1df5 MD |
139 | if (total_bytes_read > INT_MAX) |
140 | goto end; | |
141 | ret = (int) total_bytes_read; | |
142 | end: | |
143 | if (fd >= 0 && close(fd) < 0) | |
144 | PERROR("close"); | |
145 | return ret; | |
022c53ed MJ |
146 | } |
147 | ||
148 | /* | |
79030bb7 | 149 | * Get the highest CPU id from a CPU mask. |
022c53ed MJ |
150 | * |
151 | * pmask: the mask to parse. | |
152 | * len: the len of the mask excluding '\0'. | |
153 | * | |
79030bb7 | 154 | * Returns the highest CPU id from the mask or -1 on error. |
022c53ed | 155 | */ |
79030bb7 | 156 | int get_max_cpuid_from_mask(const char *pmask, size_t len) |
022c53ed MJ |
157 | { |
158 | ssize_t i; | |
159 | unsigned long cpu_index; | |
160 | char *endptr; | |
161 | ||
162 | /* We need at least one char to read */ | |
163 | if (len < 1) | |
164 | goto error; | |
165 | ||
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] == '-')) { | |
170 | i++; | |
171 | break; | |
172 | } | |
173 | } | |
174 | ||
175 | cpu_index = strtoul(&pmask[i], &endptr, 10); | |
ebabbf58 MD |
176 | |
177 | /* | |
022c53ed MJ |
178 | * If we read a CPU index, increment it by one to return a number of |
179 | * CPUs. | |
ebabbf58 | 180 | */ |
022c53ed | 181 | if ((&pmask[i] != endptr) && (cpu_index < INT_MAX)) |
79030bb7 | 182 | return (int) cpu_index; |
022c53ed MJ |
183 | |
184 | error: | |
79030bb7 | 185 | return -1; |
022c53ed MJ |
186 | } |
187 | ||
79030bb7 | 188 | static void update_possible_cpus_array_len_cache(void) |
022c53ed MJ |
189 | { |
190 | int ret; | |
73beabae | 191 | char buf[LTTNG_UST_CPUMASK_SIZE]; |
022c53ed MJ |
192 | |
193 | /* Get the possible cpu mask from sysfs, fallback to sysconf. */ | |
73beabae | 194 | ret = get_possible_cpu_mask_from_sysfs((char *) &buf, LTTNG_UST_CPUMASK_SIZE); |
022c53ed MJ |
195 | if (ret <= 0) |
196 | goto fallback; | |
197 | ||
198 | /* Parse the possible cpu mask, on failure fallback to sysconf. */ | |
79030bb7 MJ |
199 | ret = get_max_cpuid_from_mask((char *) &buf, ret); |
200 | if (ret >= 0) { | |
201 | /* Add 1 to convert from max cpuid to an array len. */ | |
202 | ret++; | |
022c53ed | 203 | goto end; |
79030bb7 | 204 | } |
022c53ed MJ |
205 | |
206 | fallback: | |
207 | /* Fallback to sysconf. */ | |
208 | ret = get_num_possible_cpus_fallback(); | |
209 | ||
210 | end: | |
211 | /* If all methods failed, don't store the value. */ | |
212 | if (ret < 1) | |
ebabbf58 | 213 | return; |
022c53ed | 214 | |
79030bb7 | 215 | possible_cpus_array_len_cache = ret; |
ebabbf58 | 216 | } |
74cc1f59 MJ |
217 | |
218 | /* | |
79030bb7 MJ |
219 | * Returns the length of an array that could contain a per-CPU element for each |
220 | * possible CPU id for the lifetime of the process. | |
221 | * | |
222 | * We currently assume CPU ids are contiguous up the maximum CPU id. | |
223 | * | |
224 | * If the cache is not yet initialized, get the value from | |
225 | * "/sys/devices/system/cpu/possible" or fallback to sysconf and cache it. | |
74cc1f59 | 226 | * |
022c53ed | 227 | * If all methods fail, don't populate the cache and return 0. |
74cc1f59 | 228 | */ |
79030bb7 | 229 | int get_possible_cpus_array_len(void) |
74cc1f59 | 230 | { |
79030bb7 MJ |
231 | if (caa_unlikely(!possible_cpus_array_len_cache)) |
232 | update_possible_cpus_array_len_cache(); | |
74cc1f59 | 233 | |
79030bb7 | 234 | return possible_cpus_array_len_cache; |
74cc1f59 | 235 | } |