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