| 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 | __attribute__((visibility("hidden"))); |
| 16 | |
| 17 | extern int (*lttng_get_cpu)(void) |
| 18 | __attribute__((visibility("hidden"))); |
| 19 | |
| 20 | #ifdef LTTNG_UST_DEBUG_VALGRIND |
| 21 | |
| 22 | /* |
| 23 | * Fallback on cpu 0 if liblttng-ust is build with Valgrind support. |
| 24 | * get_cpu() returns the current CPU number. It may change due to |
| 25 | * migration, so it is only statistically accurate. |
| 26 | */ |
| 27 | static inline |
| 28 | int lttng_ust_get_cpu_internal(void) |
| 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | #else |
| 34 | |
| 35 | /* |
| 36 | * sched_getcpu. |
| 37 | */ |
| 38 | #ifdef __linux__ |
| 39 | |
| 40 | #if !HAVE_SCHED_GETCPU |
| 41 | #include <sys/syscall.h> |
| 42 | #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache) |
| 43 | /* |
| 44 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. |
| 45 | */ |
| 46 | static inline |
| 47 | int lttng_ust_get_cpu_internal(void) |
| 48 | { |
| 49 | int cpu, ret; |
| 50 | |
| 51 | ret = __getcpu(&cpu, NULL, NULL); |
| 52 | if (caa_unlikely(ret < 0)) |
| 53 | return 0; |
| 54 | return cpu; |
| 55 | } |
| 56 | #else /* HAVE_SCHED_GETCPU */ |
| 57 | #include <sched.h> |
| 58 | |
| 59 | /* |
| 60 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. |
| 61 | */ |
| 62 | static inline |
| 63 | int lttng_ust_get_cpu_internal(void) |
| 64 | { |
| 65 | int cpu; |
| 66 | |
| 67 | cpu = sched_getcpu(); |
| 68 | if (caa_unlikely(cpu < 0)) |
| 69 | return 0; |
| 70 | return cpu; |
| 71 | } |
| 72 | #endif /* HAVE_SCHED_GETCPU */ |
| 73 | |
| 74 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) |
| 75 | |
| 76 | /* |
| 77 | * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU |
| 78 | * number 0, with the assocated performance degradation on SMP. |
| 79 | */ |
| 80 | static inline |
| 81 | int lttng_ust_get_cpu_internal(void) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | #else |
| 87 | #error "Please add support for your OS into liblttng-ust/compat.h." |
| 88 | #endif |
| 89 | |
| 90 | #endif |
| 91 | |
| 92 | static inline |
| 93 | int lttng_ust_get_cpu(void) |
| 94 | { |
| 95 | int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu); |
| 96 | |
| 97 | if (caa_likely(!getcpu)) { |
| 98 | return lttng_ust_get_cpu_internal(); |
| 99 | } else { |
| 100 | return getcpu(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #endif /* _LTTNG_GETCPU_H */ |