2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _UST_COMMON_GETCPU_H
8 #define _UST_COMMON_GETCPU_H
10 #include <urcu/compiler.h>
11 #include <urcu/system.h>
12 #include <urcu/arch.h>
14 #include <lttng/ust-getcpu.h>
17 * Function pointer to the user provided getcpu callback, can be set at library
18 * initialization by a dlopened plugin or at runtime by a user by calling
19 * lttng_ust_getcpu_override() from the public API.
21 * This is an ABI symbol of liblttng-ust-common accessed by other libraries
22 * through the static inline function in this file. It is initialised in the
23 * liblttng-ust-common constructor.
25 extern int (*lttng_ust_get_cpu_sym
)(void);
27 #ifdef LTTNG_UST_DEBUG_VALGRIND
30 * Fallback on cpu 0 if liblttng-ust is build with Valgrind support.
31 * get_cpu() returns the current CPU number. It may change due to
32 * migration, so it is only statistically accurate.
35 int lttng_ust_get_cpu_internal(void)
47 #if !HAVE_SCHED_GETCPU
48 #include <sys/syscall.h>
49 #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache)
51 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
54 int lttng_ust_get_cpu_internal(void)
58 ret
= __getcpu(&cpu
, NULL
, NULL
);
59 if (caa_unlikely(ret
< 0))
63 #else /* HAVE_SCHED_GETCPU */
67 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
70 int lttng_ust_get_cpu_internal(void)
75 if (caa_unlikely(cpu
< 0))
79 #endif /* HAVE_SCHED_GETCPU */
81 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
84 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
85 * number 0, with the assocated performance degradation on SMP.
88 int lttng_ust_get_cpu_internal(void)
94 #error "Please add support for your OS into liblttng-ust/compat.h."
100 int lttng_ust_get_cpu(void)
102 int (*lttng_ust_get_cpu_current
)(void) = CMM_LOAD_SHARED(lttng_ust_get_cpu_sym
);
105 * Fallback to the internal getcpu implementation if no override was
108 if (caa_likely(!lttng_ust_get_cpu_current
)) {
109 return lttng_ust_get_cpu_internal();
111 return lttng_ust_get_cpu_current();
115 #endif /* _LTTNG_GETCPU_H */