fix: removed accidental VLA in _get_num_possible_cpus()
[lttng-ust.git] / libringbuffer / smp.c
CommitLineData
a6352fd4 1/*
4318ae1b 2 * libringbuffer/smp.c
a6352fd4 3 *
e92f3e28 4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13d2c5ba 5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
a6352fd4 6 *
e92f3e28
MD
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a6352fd4
MD
20 */
21
5ad63a16 22#define _GNU_SOURCE
3fbec7dc 23#define _LGPL_SOURCE
f1d40e23
MJ
24#include <assert.h>
25#include <ctype.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <limits.h>
a6352fd4 29#include <pthread.h>
f1d40e23
MJ
30#include <stdlib.h>
31#include <unistd.h>
32
bb6e798c 33#include "align.h"
a6352fd4 34#include "smp.h"
f1d40e23 35#include "usterr-signal-safe.h"
a6352fd4
MD
36
37int __num_possible_cpus;
38
13d2c5ba 39#if (defined(__GLIBC__) || defined( __UCLIBC__))
f1d40e23 40int get_num_possible_cpus_fallback(void)
a6352fd4 41{
a6352fd4
MD
42 /* On Linux, when some processors are offline
43 * _SC_NPROCESSORS_CONF counts the offline
44 * processors, whereas _SC_NPROCESSORS_ONLN
45 * does not. If we used _SC_NPROCESSORS_ONLN,
46 * getcpu() could return a value greater than
47 * this sysconf, in which case the arrays
48 * indexed by processor would overflow.
49 */
f1d40e23 50 return sysconf(_SC_NPROCESSORS_CONF);
a6352fd4 51}
13d2c5ba
MJ
52
53#else
54
55/*
56 * The MUSL libc implementation of the _SC_NPROCESSORS_CONF sysconf does not
57 * return the number of configured CPUs in the system but relies on the cpu
58 * affinity mask of the current task.
59 *
60 * So instead we use a strategy similar to GLIBC's, counting the cpu
61 * directories in "/sys/devices/system/cpu" and fallback on the value from
62 * sysconf if it fails.
63 */
64
65#include <dirent.h>
66#include <limits.h>
67#include <stdlib.h>
68#include <string.h>
69#include <sys/types.h>
70
71#define __max(a,b) ((a)>(b)?(a):(b))
72
f1d40e23 73int get_num_possible_cpus_fallback(void)
13d2c5ba 74{
f1d40e23 75 int count = 0;
13d2c5ba
MJ
76 DIR *cpudir;
77 struct dirent *entry;
78
79 cpudir = opendir("/sys/devices/system/cpu");
80 if (cpudir == NULL)
81 goto end;
82
83 /*
84 * Count the number of directories named "cpu" followed by and
85 * integer. This is the same strategy as glibc uses.
86 */
87 while ((entry = readdir(cpudir))) {
88 if (entry->d_type == DT_DIR &&
89 strncmp(entry->d_name, "cpu", 3) == 0) {
90
91 char *endptr;
92 unsigned long cpu_num;
93
94 cpu_num = strtoul(entry->d_name + 3, &endptr, 10);
95 if ((cpu_num < ULONG_MAX) && (endptr != entry->d_name + 3)
96 && (*endptr == '\0')) {
97 count++;
98 }
99 }
100 }
101
102end:
103 /*
104 * Get the sysconf value as a fallback. Keep the highest number.
105 */
f1d40e23
MJ
106 return __max(sysconf(_SC_NPROCESSORS_CONF), count);
107}
108#endif
109
110/*
111 * Get the CPU possible mask string from sysfs.
112 *
113 * buf: the buffer where the mask will be read.
114 * max_bytes: the maximum number of bytes to write in the buffer.
115 *
116 * Returns the number of bytes read or -1 on error.
117 */
118int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes)
119{
120 ssize_t bytes_read = 0;
121 size_t total_bytes_read = 0;
122 int fd = -1, ret = -1;
123
124 if (buf == NULL)
125 goto end;
126
127 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
128 if (fd < 0)
129 goto end;
130
131 do {
132 bytes_read = read(fd, buf + total_bytes_read,
133 max_bytes - total_bytes_read);
134
135 if (bytes_read < 0) {
136 if (errno == EINTR) {
137 continue; /* retry operation */
138 } else {
139 goto end;
140 }
141 }
142
143 total_bytes_read += bytes_read;
144 assert(total_bytes_read <= max_bytes);
145 } while (max_bytes > total_bytes_read && bytes_read > 0);
146
147 /*
148 * Make sure the mask read is a null terminated string.
149 */
150 if (total_bytes_read < max_bytes)
151 buf[total_bytes_read] = '\0';
152 else
153 buf[max_bytes - 1] = '\0';
154
155 if (total_bytes_read > INT_MAX)
156 goto end;
157
158 ret = (int) total_bytes_read;
159
160end:
161 if (fd >= 0 && close(fd) < 0)
162 PERROR("close");
163
164 return ret;
165}
166
167/*
168 * Get the number of CPUs from the possible cpu mask.
169 *
170 * pmask: the mask to parse.
171 * len: the len of the mask excluding '\0'.
172 *
173 * Returns the number of possible CPUs from the mask or 0 on error.
174 */
175int get_num_possible_cpus_from_mask(const char *pmask, size_t len)
176{
177 ssize_t i;
178 unsigned long cpu_index;
179 char *endptr;
180
181 /* We need at least one char to read */
182 if (len < 1)
183 goto error;
184
185 /* Start from the end to read the last CPU index. */
186 for (i = len - 1; i > 0; i--) {
187 /* Break when we hit the first separator. */
188 if ((pmask[i] == ',') || (pmask[i] == '-')) {
189 i++;
190 break;
191 }
192 }
193
194 cpu_index = strtoul(&pmask[i], &endptr, 10);
13d2c5ba
MJ
195
196 /*
f1d40e23
MJ
197 * If we read a CPU index, increment it by one to return a number of
198 * CPUs.
13d2c5ba 199 */
f1d40e23
MJ
200 if ((&pmask[i] != endptr) && (cpu_index < INT_MAX))
201 return (int) cpu_index + 1;
202
203error:
204 return 0;
205}
206
207void _get_num_possible_cpus(void)
208{
209 int ret;
bb6e798c 210 char buf[LTTNG_UST_CPUMASK_SIZE];
f1d40e23
MJ
211
212 /* Get the possible cpu mask from sysfs, fallback to sysconf. */
bb6e798c 213 ret = get_possible_cpu_mask_from_sysfs((char *) &buf, LTTNG_UST_CPUMASK_SIZE);
f1d40e23
MJ
214 if (ret <= 0)
215 goto fallback;
216
217 /* Parse the possible cpu mask, on failure fallback to sysconf. */
218 ret = get_num_possible_cpus_from_mask((char *) &buf, ret);
219 if (ret > 0)
220 goto end;
221
222fallback:
223 /* Fallback to sysconf. */
224 ret = get_num_possible_cpus_fallback();
225
226end:
227 /* If all methods failed, don't store the value. */
228 if (ret < 1)
13d2c5ba 229 return;
f1d40e23
MJ
230
231 __num_possible_cpus = ret;
13d2c5ba 232}
This page took 0.038166 seconds and 4 git commands to generate.