fix: add missing closedir in _get_max_cpuid_from_sysfs()
[lttng-ust.git] / src / common / smp.c
CommitLineData
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
66dbdc34
MJ
9#include <assert.h>
10#include <ctype.h>
217babc9 11#include <dirent.h>
66dbdc34
MJ
12#include <errno.h>
13#include <fcntl.h>
feac7204 14#include <limits.h>
ebabbf58
MD
15#include <unistd.h>
16#include <pthread.h>
66dbdc34 17#include <stdlib.h>
217babc9
MJ
18#include <string.h>
19#include <sys/types.h>
ebabbf58 20
74cc1f59
MJ
21#include <urcu/compiler.h>
22
66dbdc34 23#include "common/logging.h"
74cc1f59
MJ
24#include "common/smp.h"
25
217babc9 26#define __max(a,b) ((a)>(b)?(a):(b))
ebabbf58 27
a616fb4e 28static int possible_cpus_array_len_cache;
ebabbf58
MD
29
30/*
fb0f6ca9
MJ
31 * Get the highest CPU id from sysfs.
32 *
33 * Iterate on all the folders in "/sys/devices/system/cpu" that start with
217babc9
MJ
34 * "cpu" followed by an integer, keep the highest CPU id encountered during
35 * this iteration and add 1 to get a number of CPUs.
36 *
fb0f6ca9 37 * Returns the highest CPU id, or -1 on error.
ebabbf58 38 */
fb0f6ca9
MJ
39int get_max_cpuid_from_sysfs(void)
40{
41 return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu");
42}
43
44int _get_max_cpuid_from_sysfs(const char *path)
ebabbf58 45{
217babc9
MJ
46 long max_cpuid = -1;
47
ebabbf58
MD
48 DIR *cpudir;
49 struct dirent *entry;
50
fb0f6ca9
MJ
51 assert(path);
52
53 cpudir = opendir(path);
ebabbf58
MD
54 if (cpudir == NULL)
55 goto end;
56
57 /*
217babc9 58 * Iterate on all directories named "cpu" followed by an integer.
ebabbf58
MD
59 */
60 while ((entry = readdir(cpudir))) {
61 if (entry->d_type == DT_DIR &&
62 strncmp(entry->d_name, "cpu", 3) == 0) {
63
64 char *endptr;
217babc9 65 long cpu_id;
ebabbf58 66
217babc9
MJ
67 cpu_id = strtol(entry->d_name + 3, &endptr, 10);
68 if ((cpu_id < LONG_MAX) && (endptr != entry->d_name + 3)
ebabbf58 69 && (*endptr == '\0')) {
217babc9
MJ
70 if (cpu_id > max_cpuid)
71 max_cpuid = cpu_id;
ebabbf58
MD
72 }
73 }
74 }
75
85f8af7b
MJ
76 if (closedir(cpudir))
77 PERROR("closedir");
78
217babc9
MJ
79 /*
80 * If the max CPU id is out of bound, set it to -1 so it results in a
81 * CPU num of 0.
82 */
83 if (max_cpuid < 0 || max_cpuid > INT_MAX)
84 max_cpuid = -1;
85
ebabbf58 86end:
fb0f6ca9
MJ
87 return max_cpuid;
88}
89
90/*
91 * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible",
92 * iterate on all the folders in "/sys/devices/system/cpu" that start with
93 * "cpu" followed by an integer, keep the highest CPU id encountered during
94 * this iteration and add 1 to get a number of CPUs.
95 *
96 * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and
97 * return the highest one.
98 *
99 * On Linux, using the value from sysconf can be unreliable since the way it
100 * counts CPUs varies between C libraries and even between versions of the same
101 * library. If we used it directly, getcpu() could return a value greater than
102 * this sysconf, in which case the arrays indexed by processor would overflow.
103 *
104 * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF
105 * sysconf does not return the number of configured CPUs in the system but
106 * relies on the cpu affinity mask of the current task.
107 *
108 * Returns 0 or less on error.
109 */
110int get_num_possible_cpus_fallback(void)
111{
ebabbf58 112 /*
217babc9 113 * Get the sysconf value as a last resort. Keep the highest number.
ebabbf58 114 */
fb0f6ca9 115 return __max(sysconf(_SC_NPROCESSORS_CONF), get_max_cpuid_from_sysfs() + 1);
66dbdc34 116}
66dbdc34
MJ
117
118/*
119 * Get the CPU possible mask string from sysfs.
120 *
121 * buf: the buffer where the mask will be read.
122 * max_bytes: the maximum number of bytes to write in the buffer.
123 *
124 * Returns the number of bytes read or -1 on error.
125 */
126int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes)
fb0f6ca9
MJ
127{
128 return get_cpu_mask_from_sysfs(buf, max_bytes,
129 "/sys/devices/system/cpu/possible");
130}
131
132/*
133 * Get a CPU mask string from sysfs.
134 *
135 * buf: the buffer where the mask will be read.
136 * max_bytes: the maximum number of bytes to write in the buffer.
137 * path: file path to read the mask from.
138 *
139 * Returns the number of bytes read or -1 on error.
140 */
141int get_cpu_mask_from_sysfs(char *buf, size_t max_bytes, const char *path)
66dbdc34
MJ
142{
143 ssize_t bytes_read = 0;
144 size_t total_bytes_read = 0;
0446ebf3 145 int fd = -1, ret = -1;
66dbdc34 146
fb0f6ca9
MJ
147 assert(path);
148
66dbdc34 149 if (buf == NULL)
0446ebf3 150 goto end;
66dbdc34 151
fb0f6ca9 152 fd = open(path, O_RDONLY);
66dbdc34 153 if (fd < 0)
0446ebf3 154 goto end;
66dbdc34
MJ
155
156 do {
157 bytes_read = read(fd, buf + total_bytes_read,
158 max_bytes - total_bytes_read);
159
160 if (bytes_read < 0) {
161 if (errno == EINTR) {
162 continue; /* retry operation */
163 } else {
0446ebf3 164 goto end;
66dbdc34
MJ
165 }
166 }
167
168 total_bytes_read += bytes_read;
169 assert(total_bytes_read <= max_bytes);
170 } while (max_bytes > total_bytes_read && bytes_read > 0);
171
66dbdc34
MJ
172 /*
173 * Make sure the mask read is a null terminated string.
174 */
175 if (total_bytes_read < max_bytes)
176 buf[total_bytes_read] = '\0';
177 else
178 buf[max_bytes - 1] = '\0';
179
0446ebf3
MD
180 if (total_bytes_read > INT_MAX)
181 goto end;
182 ret = (int) total_bytes_read;
183end:
184 if (fd >= 0 && close(fd) < 0)
185 PERROR("close");
186 return ret;
66dbdc34
MJ
187}
188
189/*
a616fb4e 190 * Get the highest CPU id from a CPU mask.
66dbdc34
MJ
191 *
192 * pmask: the mask to parse.
193 * len: the len of the mask excluding '\0'.
194 *
a616fb4e 195 * Returns the highest CPU id from the mask or -1 on error.
66dbdc34 196 */
a616fb4e 197int get_max_cpuid_from_mask(const char *pmask, size_t len)
66dbdc34
MJ
198{
199 ssize_t i;
200 unsigned long cpu_index;
201 char *endptr;
202
203 /* We need at least one char to read */
204 if (len < 1)
205 goto error;
206
207 /* Start from the end to read the last CPU index. */
208 for (i = len - 1; i > 0; i--) {
209 /* Break when we hit the first separator. */
210 if ((pmask[i] == ',') || (pmask[i] == '-')) {
211 i++;
212 break;
213 }
214 }
215
216 cpu_index = strtoul(&pmask[i], &endptr, 10);
ebabbf58
MD
217
218 /*
66dbdc34
MJ
219 * If we read a CPU index, increment it by one to return a number of
220 * CPUs.
ebabbf58 221 */
66dbdc34 222 if ((&pmask[i] != endptr) && (cpu_index < INT_MAX))
a616fb4e 223 return (int) cpu_index;
66dbdc34
MJ
224
225error:
a616fb4e 226 return -1;
66dbdc34
MJ
227}
228
a616fb4e 229static void update_possible_cpus_array_len_cache(void)
66dbdc34
MJ
230{
231 int ret;
feac7204 232 char buf[LTTNG_UST_CPUMASK_SIZE];
66dbdc34
MJ
233
234 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
feac7204 235 ret = get_possible_cpu_mask_from_sysfs((char *) &buf, LTTNG_UST_CPUMASK_SIZE);
66dbdc34
MJ
236 if (ret <= 0)
237 goto fallback;
238
239 /* Parse the possible cpu mask, on failure fallback to sysconf. */
a616fb4e
MJ
240 ret = get_max_cpuid_from_mask((char *) &buf, ret);
241 if (ret >= 0) {
242 /* Add 1 to convert from max cpuid to an array len. */
243 ret++;
66dbdc34 244 goto end;
a616fb4e 245 }
66dbdc34
MJ
246
247fallback:
248 /* Fallback to sysconf. */
249 ret = get_num_possible_cpus_fallback();
250
251end:
252 /* If all methods failed, don't store the value. */
253 if (ret < 1)
ebabbf58 254 return;
66dbdc34 255
a616fb4e 256 possible_cpus_array_len_cache = ret;
ebabbf58 257}
74cc1f59
MJ
258
259/*
a616fb4e
MJ
260 * Returns the length of an array that could contain a per-CPU element for each
261 * possible CPU id for the lifetime of the process.
262 *
263 * We currently assume CPU ids are contiguous up the maximum CPU id.
264 *
265 * If the cache is not yet initialized, get the value from
266 * "/sys/devices/system/cpu/possible" or fallback to sysconf and cache it.
74cc1f59 267 *
66dbdc34 268 * If all methods fail, don't populate the cache and return 0.
74cc1f59 269 */
a616fb4e 270int get_possible_cpus_array_len(void)
74cc1f59 271{
a616fb4e
MJ
272 if (caa_unlikely(!possible_cpus_array_len_cache))
273 update_possible_cpus_array_len_cache();
74cc1f59 274
a616fb4e 275 return possible_cpus_array_len_cache;
74cc1f59 276}
This page took 0.040319 seconds and 4 git commands to generate.