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