Commit | Line | Data |
---|---|---|
2b2d6ff7 MD |
1 | #ifndef _LTTNG_GETCPU_H |
2 | #define _LTTNG_GETCPU_H | |
3 | ||
4 | /* | |
5 | * Copyright (c) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; only | |
10 | * version 2.1 of the License. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
2b2d6ff7 | 22 | #include <urcu/compiler.h> |
5e1b7b8b MD |
23 | #include <urcu/system.h> |
24 | #include <urcu/arch.h> | |
25 | ||
26 | void lttng_ust_getcpu_init(void); | |
27 | ||
28 | extern int (*lttng_get_cpu)(void); | |
2b2d6ff7 | 29 | |
fdb4af10 | 30 | #ifdef LTTNG_UST_DEBUG_VALGRIND |
2b2d6ff7 MD |
31 | |
32 | /* | |
33 | * Fallback on cpu 0 if liblttng-ust is build with Valgrind support. | |
34 | * get_cpu() returns the current CPU number. It may change due to | |
35 | * migration, so it is only statistically accurate. | |
36 | */ | |
37 | static inline | |
5e1b7b8b | 38 | int lttng_ust_get_cpu_internal(void) |
2b2d6ff7 MD |
39 | { |
40 | return 0; | |
41 | } | |
42 | ||
43 | #else | |
44 | ||
08bf1cc1 MD |
45 | /* |
46 | * sched_getcpu. | |
47 | */ | |
48 | #ifdef __linux__ | |
49 | ||
787364e8 | 50 | #if !HAVE_SCHED_GETCPU |
08bf1cc1 MD |
51 | #include <sys/syscall.h> |
52 | #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache) | |
53 | /* | |
54 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. | |
55 | */ | |
56 | static inline | |
5e1b7b8b | 57 | int lttng_ust_get_cpu_internal(void) |
08bf1cc1 MD |
58 | { |
59 | int cpu, ret; | |
60 | ||
61 | ret = __getcpu(&cpu, NULL, NULL); | |
62 | if (caa_unlikely(ret < 0)) | |
63 | return 0; | |
787364e8 | 64 | return cpu; |
08bf1cc1 | 65 | } |
787364e8 | 66 | #else /* HAVE_SCHED_GETCPU */ |
08bf1cc1 MD |
67 | #include <sched.h> |
68 | ||
2b2d6ff7 MD |
69 | /* |
70 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. | |
71 | */ | |
72 | static inline | |
5e1b7b8b | 73 | int lttng_ust_get_cpu_internal(void) |
2b2d6ff7 MD |
74 | { |
75 | int cpu; | |
76 | ||
77 | cpu = sched_getcpu(); | |
78 | if (caa_unlikely(cpu < 0)) | |
79 | return 0; | |
80 | return cpu; | |
81 | } | |
787364e8 | 82 | #endif /* HAVE_SCHED_GETCPU */ |
08bf1cc1 | 83 | |
4327cb7d | 84 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) |
08bf1cc1 MD |
85 | |
86 | /* | |
4327cb7d MD |
87 | * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU |
88 | * number 0, with the assocated performance degradation on SMP. | |
08bf1cc1 MD |
89 | */ |
90 | static inline | |
5e1b7b8b | 91 | int lttng_ust_get_cpu_internal(void) |
08bf1cc1 MD |
92 | { |
93 | return 0; | |
94 | } | |
95 | ||
96 | #else | |
97 | #error "Please add support for your OS into liblttng-ust/compat.h." | |
98 | #endif | |
2b2d6ff7 MD |
99 | |
100 | #endif | |
101 | ||
5e1b7b8b MD |
102 | static inline |
103 | int lttng_ust_get_cpu(void) | |
104 | { | |
105 | int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu); | |
106 | ||
107 | if (caa_likely(!getcpu)) { | |
108 | return lttng_ust_get_cpu_internal(); | |
109 | } else { | |
110 | return getcpu(); | |
111 | } | |
112 | } | |
113 | ||
2b2d6ff7 | 114 | #endif /* _LTTNG_GETCPU_H */ |