| 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 | #include <lttng/ust-getcpu.h> |
| 15 | |
| 16 | |
| 17 | /* |
| 18 | * Initialize the getcpu plugin if it's present. |
| 19 | */ |
| 20 | void lttng_ust_getcpu_plugin_init(void) |
| 21 | __attribute__((visibility("hidden"))); |
| 22 | |
| 23 | /* |
| 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. |
| 27 | */ |
| 28 | extern int (*lttng_ust_get_cpu_sym)(void) |
| 29 | __attribute__((visibility("hidden"))); |
| 30 | |
| 31 | #ifdef LTTNG_UST_DEBUG_VALGRIND |
| 32 | |
| 33 | /* |
| 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. |
| 37 | */ |
| 38 | static inline |
| 39 | int lttng_ust_get_cpu_internal(void) |
| 40 | { |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | #else |
| 45 | |
| 46 | /* |
| 47 | * sched_getcpu. |
| 48 | */ |
| 49 | #ifdef __linux__ |
| 50 | |
| 51 | #if !HAVE_SCHED_GETCPU |
| 52 | #include <sys/syscall.h> |
| 53 | #define __getcpu(cpu, node, cache) syscall(__NR_getcpu, cpu, node, cache) |
| 54 | /* |
| 55 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. |
| 56 | */ |
| 57 | static inline |
| 58 | int lttng_ust_get_cpu_internal(void) |
| 59 | { |
| 60 | int cpu, ret; |
| 61 | |
| 62 | ret = __getcpu(&cpu, NULL, NULL); |
| 63 | if (caa_unlikely(ret < 0)) |
| 64 | return 0; |
| 65 | return cpu; |
| 66 | } |
| 67 | #else /* HAVE_SCHED_GETCPU */ |
| 68 | #include <sched.h> |
| 69 | |
| 70 | /* |
| 71 | * If getcpu is not implemented in the kernel, use cpu 0 as fallback. |
| 72 | */ |
| 73 | static inline |
| 74 | int lttng_ust_get_cpu_internal(void) |
| 75 | { |
| 76 | int cpu; |
| 77 | |
| 78 | cpu = sched_getcpu(); |
| 79 | if (caa_unlikely(cpu < 0)) |
| 80 | return 0; |
| 81 | return cpu; |
| 82 | } |
| 83 | #endif /* HAVE_SCHED_GETCPU */ |
| 84 | |
| 85 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__)) |
| 86 | |
| 87 | /* |
| 88 | * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU |
| 89 | * number 0, with the assocated performance degradation on SMP. |
| 90 | */ |
| 91 | static inline |
| 92 | int lttng_ust_get_cpu_internal(void) |
| 93 | { |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | #else |
| 98 | #error "Please add support for your OS into liblttng-ust/compat.h." |
| 99 | #endif |
| 100 | |
| 101 | #endif |
| 102 | |
| 103 | static inline |
| 104 | int lttng_ust_get_cpu(void) |
| 105 | { |
| 106 | int (*lttng_ust_get_cpu_current)(void) = CMM_LOAD_SHARED(lttng_ust_get_cpu_sym); |
| 107 | |
| 108 | /* |
| 109 | * Fallback to the internal getcpu implementation if no override was |
| 110 | * provided the user. |
| 111 | */ |
| 112 | if (caa_likely(!lttng_ust_get_cpu_current)) { |
| 113 | return lttng_ust_get_cpu_internal(); |
| 114 | } else { |
| 115 | return lttng_ust_get_cpu_current(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | #endif /* _LTTNG_GETCPU_H */ |