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