Fix: v.u.d might be uninitialized
[deliverable/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>
5de7c318 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
a6352fd4 24#include <unistd.h>
a6352fd4
MD
25#include <pthread.h>
26#include "smp.h"
27
28int __num_possible_cpus;
29
5de7c318 30#if (defined(__GLIBC__) || defined( __UCLIBC__))
a6352fd4
MD
31void _get_num_possible_cpus(void)
32{
33 int result;
34
35 /* On Linux, when some processors are offline
36 * _SC_NPROCESSORS_CONF counts the offline
37 * processors, whereas _SC_NPROCESSORS_ONLN
38 * does not. If we used _SC_NPROCESSORS_ONLN,
39 * getcpu() could return a value greater than
40 * this sysconf, in which case the arrays
41 * indexed by processor would overflow.
42 */
43 result = sysconf(_SC_NPROCESSORS_CONF);
44 if (result == -1)
45 return;
46 __num_possible_cpus = result;
47}
5de7c318
MJ
48
49#else
50
51/*
52 * The MUSL libc implementation of the _SC_NPROCESSORS_CONF sysconf does not
53 * return the number of configured CPUs in the system but relies on the cpu
54 * affinity mask of the current task.
55 *
56 * So instead we use a strategy similar to GLIBC's, counting the cpu
57 * directories in "/sys/devices/system/cpu" and fallback on the value from
58 * sysconf if it fails.
59 */
60
61#include <dirent.h>
62#include <limits.h>
63#include <stdlib.h>
64#include <string.h>
65#include <sys/types.h>
66
67#define __max(a,b) ((a)>(b)?(a):(b))
68
69void _get_num_possible_cpus(void)
70{
71 int result, count = 0;
72 DIR *cpudir;
73 struct dirent *entry;
74
75 cpudir = opendir("/sys/devices/system/cpu");
76 if (cpudir == NULL)
77 goto end;
78
79 /*
80 * Count the number of directories named "cpu" followed by and
81 * integer. This is the same strategy as glibc uses.
82 */
83 while ((entry = readdir(cpudir))) {
84 if (entry->d_type == DT_DIR &&
85 strncmp(entry->d_name, "cpu", 3) == 0) {
86
87 char *endptr;
88 unsigned long cpu_num;
89
90 cpu_num = strtoul(entry->d_name + 3, &endptr, 10);
91 if ((cpu_num < ULONG_MAX) && (endptr != entry->d_name + 3)
92 && (*endptr == '\0')) {
93 count++;
94 }
95 }
96 }
97
98end:
99 /*
100 * Get the sysconf value as a fallback. Keep the highest number.
101 */
102 result = __max(sysconf(_SC_NPROCESSORS_CONF), count);
103
104 /*
105 * If both methods failed, don't store the value.
106 */
107 if (result < 1)
108 return;
109 __num_possible_cpus = result;
110}
111#endif
This page took 0.060973 seconds and 5 git commands to generate.