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