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