2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _LTTNG_GETCPU_H
8 #define _LTTNG_GETCPU_H
10 #include <urcu/compiler.h>
11 #include <urcu/system.h>
12 #include <urcu/arch.h>
14 #include <lttng/ust-getcpu.h>
18 * Initialize the getcpu plugin if it's present.
20 void lttng_ust_getcpu_plugin_init(void)
21 __attribute__((visibility("hidden")));
24 * Function pointer to the user provided getcpu callback, can be set at library
25 * initialization by a dlopened plugin or at runtime by a user by calling
26 * lttng_ust_getcpu_override() from the public API.
28 extern int (*lttng_ust_get_cpu_sym
)(void)
29 __attribute__((visibility("hidden")));
31 #ifdef LTTNG_UST_DEBUG_VALGRIND
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.
39 int lttng_ust_get_cpu_internal(void)
51 #if !HAVE_SCHED_GETCPU
52 #include <sys/syscall.h>
53 #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache)
55 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
58 int lttng_ust_get_cpu_internal(void)
62 ret
= __getcpu(&cpu
, NULL
, NULL
);
63 if (caa_unlikely(ret
< 0))
67 #else /* HAVE_SCHED_GETCPU */
71 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
74 int lttng_ust_get_cpu_internal(void)
79 if (caa_unlikely(cpu
< 0))
83 #endif /* HAVE_SCHED_GETCPU */
85 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
88 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
89 * number 0, with the assocated performance degradation on SMP.
92 int lttng_ust_get_cpu_internal(void)
98 #error "Please add support for your OS into liblttng-ust/compat.h."
104 int lttng_ust_get_cpu(void)
106 int (*lttng_ust_get_cpu_current
)(void) = CMM_LOAD_SHARED(lttng_ust_get_cpu_sym
);
109 * Fallback to the internal getcpu implementation if no override was
112 if (caa_likely(!lttng_ust_get_cpu_current
)) {
113 return lttng_ust_get_cpu_internal();
115 return lttng_ust_get_cpu_current();
119 #endif /* _LTTNG_GETCPU_H */