Commit | Line | Data |
---|---|---|
2b2d6ff7 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
2b2d6ff7 | 3 | * |
c0c0989a | 4 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2b2d6ff7 MD |
5 | */ |
6 | ||
c0c0989a MJ |
7 | #ifndef _LTTNG_GETCPU_H |
8 | #define _LTTNG_GETCPU_H | |
9 | ||
2b2d6ff7 | 10 | #include <urcu/compiler.h> |
5e1b7b8b MD |
11 | #include <urcu/system.h> |
12 | #include <urcu/arch.h> | |
13 | ||
071dec43 MJ |
14 | #include "ust-helper.h" |
15 | ||
16 | LTTNG_HIDDEN | |
5e1b7b8b MD |
17 | void lttng_ust_getcpu_init(void); |
18 | ||
071dec43 | 19 | LTTNG_HIDDEN |
5e1b7b8b | 20 | extern int (*lttng_get_cpu)(void); |
2b2d6ff7 | 21 | |
fdb4af10 | 22 | #ifdef LTTNG_UST_DEBUG_VALGRIND |
2b2d6ff7 MD |
23 | |
24 | /* | |
25 | * Fallback on cpu 0 if liblttng-ust is build with Valgrind support. | |
26 | * get_cpu() returns the current CPU number. It may change due to | |
27 | * migration, so it is only statistically accurate. | |
28 | */ | |
29 | static inline | |
5e1b7b8b | 30 | int lttng_ust_get_cpu_internal(void) |
2b2d6ff7 MD |
31 | { |
32 | return 0; | |
33 | } | |
34 | ||
35 | #else | |
36 | ||
08bf1cc1 MD |
37 | /* |
38 | * sched_getcpu. | |
39 | */ | |
40 | #ifdef __linux__ | |
41 | ||
787364e8 | 42 | #if !HAVE_SCHED_GETCPU |
08bf1cc1 MD |
43 | #include <sys/syscall.h> |
44 | #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache) | |
45 | /* | |
46 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. | |
47 | */ | |
48 | static inline | |
5e1b7b8b | 49 | int lttng_ust_get_cpu_internal(void) |
08bf1cc1 MD |
50 | { |
51 | int cpu, ret; | |
52 | ||
53 | ret = __getcpu(&cpu, NULL, NULL); | |
54 | if (caa_unlikely(ret < 0)) | |
55 | return 0; | |
787364e8 | 56 | return cpu; |
08bf1cc1 | 57 | } |
787364e8 | 58 | #else /* HAVE_SCHED_GETCPU */ |
08bf1cc1 MD |
59 | #include <sched.h> |
60 | ||
2b2d6ff7 MD |
61 | /* |
62 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. | |
63 | */ | |
64 | static inline | |
5e1b7b8b | 65 | int lttng_ust_get_cpu_internal(void) |
2b2d6ff7 MD |
66 | { |
67 | int cpu; | |
68 | ||
69 | cpu = sched_getcpu(); | |
70 | if (caa_unlikely(cpu < 0)) | |
71 | return 0; | |
72 | return cpu; | |
73 | } | |
787364e8 | 74 | #endif /* HAVE_SCHED_GETCPU */ |
08bf1cc1 | 75 | |
4327cb7d | 76 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) |
08bf1cc1 MD |
77 | |
78 | /* | |
4327cb7d MD |
79 | * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU |
80 | * number 0, with the assocated performance degradation on SMP. | |
08bf1cc1 MD |
81 | */ |
82 | static inline | |
5e1b7b8b | 83 | int lttng_ust_get_cpu_internal(void) |
08bf1cc1 MD |
84 | { |
85 | return 0; | |
86 | } | |
87 | ||
88 | #else | |
89 | #error "Please add support for your OS into liblttng-ust/compat.h." | |
90 | #endif | |
2b2d6ff7 MD |
91 | |
92 | #endif | |
93 | ||
5e1b7b8b MD |
94 | static inline |
95 | int lttng_ust_get_cpu(void) | |
96 | { | |
97 | int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu); | |
98 | ||
99 | if (caa_likely(!getcpu)) { | |
100 | return lttng_ust_get_cpu_internal(); | |
101 | } else { | |
102 | return getcpu(); | |
103 | } | |
104 | } | |
105 | ||
2b2d6ff7 | 106 | #endif /* _LTTNG_GETCPU_H */ |