Move to kernel style SPDX license identifiers
[lttng-ust.git] / libringbuffer / getcpu.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _LTTNG_GETCPU_H
8 #define _LTTNG_GETCPU_H
9
10 #include <urcu/compiler.h>
11 #include <urcu/system.h>
12 #include <urcu/arch.h>
13
14 void lttng_ust_getcpu_init(void);
15
16 extern int (*lttng_get_cpu)(void);
17
18 #ifdef LTTNG_UST_DEBUG_VALGRIND
19
20 /*
21 * Fallback on cpu 0 if liblttng-ust is build with Valgrind support.
22 * get_cpu() returns the current CPU number. It may change due to
23 * migration, so it is only statistically accurate.
24 */
25 static inline
26 int lttng_ust_get_cpu_internal(void)
27 {
28 return 0;
29 }
30
31 #else
32
33 /*
34 * sched_getcpu.
35 */
36 #ifdef __linux__
37
38 #if !HAVE_SCHED_GETCPU
39 #include <sys/syscall.h>
40 #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache)
41 /*
42 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
43 */
44 static inline
45 int lttng_ust_get_cpu_internal(void)
46 {
47 int cpu, ret;
48
49 ret = __getcpu(&cpu, NULL, NULL);
50 if (caa_unlikely(ret < 0))
51 return 0;
52 return cpu;
53 }
54 #else /* HAVE_SCHED_GETCPU */
55 #include <sched.h>
56
57 /*
58 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
59 */
60 static inline
61 int lttng_ust_get_cpu_internal(void)
62 {
63 int cpu;
64
65 cpu = sched_getcpu();
66 if (caa_unlikely(cpu < 0))
67 return 0;
68 return cpu;
69 }
70 #endif /* HAVE_SCHED_GETCPU */
71
72 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
73
74 /*
75 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
76 * number 0, with the assocated performance degradation on SMP.
77 */
78 static inline
79 int lttng_ust_get_cpu_internal(void)
80 {
81 return 0;
82 }
83
84 #else
85 #error "Please add support for your OS into liblttng-ust/compat.h."
86 #endif
87
88 #endif
89
90 static inline
91 int lttng_ust_get_cpu(void)
92 {
93 int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu);
94
95 if (caa_likely(!getcpu)) {
96 return lttng_ust_get_cpu_internal();
97 } else {
98 return getcpu();
99 }
100 }
101
102 #endif /* _LTTNG_GETCPU_H */
This page took 0.031015 seconds and 4 git commands to generate.