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 MD |
23 | |
24 | #ifdef UST_VALGRIND | |
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 | ||
dbafbbf5 MD |
39 | /* |
40 | * sched_getcpu. | |
41 | */ | |
42 | #ifdef __linux__ | |
43 | ||
44 | #ifdef __UCLIBC__ | |
45 | #include <sys/syscall.h> | |
46 | #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache) | |
47 | /* | |
48 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. | |
49 | */ | |
50 | static inline | |
51 | int lttng_ust_get_cpu(void) | |
52 | { | |
53 | int cpu, ret; | |
54 | ||
55 | ret = __getcpu(&cpu, NULL, NULL); | |
56 | if (caa_unlikely(ret < 0)) | |
57 | return 0; | |
58 | return c; | |
59 | } | |
60 | #else /* __UCLIBC__ */ | |
61 | #include <sched.h> | |
62 | ||
2b2d6ff7 MD |
63 | /* |
64 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. | |
65 | */ | |
66 | static inline | |
67 | int lttng_ust_get_cpu(void) | |
68 | { | |
69 | int cpu; | |
70 | ||
71 | cpu = sched_getcpu(); | |
72 | if (caa_unlikely(cpu < 0)) | |
73 | return 0; | |
74 | return cpu; | |
75 | } | |
dbafbbf5 MD |
76 | #endif /* __UCLIBC__ */ |
77 | ||
78 | #elif defined(__FreeBSD__) | |
79 | ||
80 | /* | |
81 | * FreeBSD does not allow query of CPU ID. Always use CPU number 0, with | |
82 | * the assocated performance degradation on SMP. | |
83 | */ | |
84 | static inline | |
85 | int lttng_ust_get_cpu(void) | |
86 | { | |
87 | return 0; | |
88 | } | |
89 | ||
90 | #else | |
91 | #error "Please add support for your OS into liblttng-ust/compat.h." | |
92 | #endif | |
2b2d6ff7 MD |
93 | |
94 | #endif | |
95 | ||
96 | #endif /* _LTTNG_GETCPU_H */ |