Implement getcpu override
[lttng-ust.git] / libringbuffer / getcpu.h
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
22 #include <urcu/compiler.h>
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);
29
30 #ifdef LTTNG_UST_DEBUG_VALGRIND
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
38 int lttng_ust_get_cpu_internal(void)
39 {
40 return 0;
41 }
42
43 #else
44
45 /*
46 * sched_getcpu.
47 */
48 #ifdef __linux__
49
50 /* old uClibc versions didn't have sched_getcpu */
51 #if defined(__UCLIBC__) && __UCLIBC_MAJOR__ == 0 && \
52 (__UCLIBC_MINOR__ < 9 || \
53 (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ <= 32))
54 #include <sys/syscall.h>
55 #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache)
56 /*
57 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
58 */
59 static inline
60 int lttng_ust_get_cpu_internal(void)
61 {
62 int cpu, ret;
63
64 ret = __getcpu(&cpu, NULL, NULL);
65 if (caa_unlikely(ret < 0))
66 return 0;
67 return c;
68 }
69 #else /* __UCLIBC__ */
70 #include <sched.h>
71
72 /*
73 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
74 */
75 static inline
76 int lttng_ust_get_cpu_internal(void)
77 {
78 int cpu;
79
80 cpu = sched_getcpu();
81 if (caa_unlikely(cpu < 0))
82 return 0;
83 return cpu;
84 }
85 #endif /* __UCLIBC__ */
86
87 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
88
89 /*
90 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
91 * number 0, with the assocated performance degradation on SMP.
92 */
93 static inline
94 int lttng_ust_get_cpu_internal(void)
95 {
96 return 0;
97 }
98
99 #else
100 #error "Please add support for your OS into liblttng-ust/compat.h."
101 #endif
102
103 #endif
104
105 static inline
106 int lttng_ust_get_cpu(void)
107 {
108 int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu);
109
110 if (caa_likely(!getcpu)) {
111 return lttng_ust_get_cpu_internal();
112 } else {
113 return getcpu();
114 }
115 }
116
117 #endif /* _LTTNG_GETCPU_H */
This page took 0.033668 seconds and 5 git commands to generate.