/src/dovecot/src/lib/cpu-count.c
Line | Count | Source |
1 | | #include "lib.h" |
2 | | #include "cpu-count.h" |
3 | | |
4 | | #ifdef HAVE_SCHED_H |
5 | | # define __USE_GNU |
6 | | /* _GNU_SOURCE: for musl's sched.h to expose cpuset/CPU_* */ |
7 | | # define _GNU_SOURCE |
8 | | # include <sched.h> |
9 | | # ifdef HAVE_SYS_CPUSET_H |
10 | | # include <sys/cpuset.h> |
11 | | # endif |
12 | | #endif |
13 | | |
14 | | int cpu_count_get(int *cpu_count_r, const char **error_r) |
15 | 0 | { |
16 | 0 | int result; |
17 | 0 | #if defined(HAVE_SCHED_GETAFFINITY) |
18 | 0 | cpu_set_t cs; |
19 | 0 | CPU_ZERO(&cs); |
20 | 0 | if (sched_getaffinity(0, sizeof(cs), &cs) < 0) { |
21 | 0 | *error_r = t_strdup_printf("sched_getaffinity() failed: %m"); |
22 | 0 | return -1; |
23 | 0 | } |
24 | 0 | result = CPU_COUNT(&cs); |
25 | | #elif defined(HAVE_CPUSET_GETAFFINITY) |
26 | | cpuset_t cs; |
27 | | CPU_CLR(sizeof(cs), &cs); |
28 | | if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, |
29 | | sizeof(cs), &cs) < 0) { |
30 | | *error_r = t_strdup_printf("cpuset_getaffinity() failed: %m"); |
31 | | return -1; |
32 | | } |
33 | | result = CPU_COUNT(&cs); |
34 | | #else |
35 | | *cpu_count_r = 0; |
36 | | *error_r = "Cannot get CPU count"; |
37 | | return -1; |
38 | | #endif |
39 | 0 | *cpu_count_r = result; |
40 | 0 | return 0; |
41 | 0 | } |