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