Move the clock plugin implementation to liblttng-ust-common
[lttng-ust.git] / src / lib / lttng-ust / getcpu.h
CommitLineData
2b2d6ff7 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
2b2d6ff7 3 *
c0c0989a 4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2b2d6ff7
MD
5 */
6
c0c0989a
MJ
7#ifndef _LTTNG_GETCPU_H
8#define _LTTNG_GETCPU_H
9
2b2d6ff7 10#include <urcu/compiler.h>
5e1b7b8b
MD
11#include <urcu/system.h>
12#include <urcu/arch.h>
13
247c2ecd
MJ
14#include <lttng/ust-getcpu.h>
15
16
17/*
18 * Initialize the getcpu plugin if it's present.
19 */
20void lttng_ust_getcpu_plugin_init(void)
1d18d519 21 __attribute__((visibility("hidden")));
5e1b7b8b 22
247c2ecd
MJ
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 */
28extern int (*lttng_ust_get_cpu_sym)(void)
1d18d519 29 __attribute__((visibility("hidden")));
2b2d6ff7 30
fdb4af10 31#ifdef LTTNG_UST_DEBUG_VALGRIND
2b2d6ff7
MD
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 */
38static inline
5e1b7b8b 39int lttng_ust_get_cpu_internal(void)
2b2d6ff7
MD
40{
41 return 0;
42}
43
44#else
45
08bf1cc1
MD
46/*
47 * sched_getcpu.
48 */
49#ifdef __linux__
50
787364e8 51#if !HAVE_SCHED_GETCPU
08bf1cc1
MD
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 */
57static inline
5e1b7b8b 58int lttng_ust_get_cpu_internal(void)
08bf1cc1
MD
59{
60 int cpu, ret;
61
62 ret = __getcpu(&cpu, NULL, NULL);
63 if (caa_unlikely(ret < 0))
64 return 0;
787364e8 65 return cpu;
08bf1cc1 66}
787364e8 67#else /* HAVE_SCHED_GETCPU */
08bf1cc1
MD
68#include <sched.h>
69
2b2d6ff7
MD
70/*
71 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
72 */
73static inline
5e1b7b8b 74int lttng_ust_get_cpu_internal(void)
2b2d6ff7
MD
75{
76 int cpu;
77
78 cpu = sched_getcpu();
79 if (caa_unlikely(cpu < 0))
80 return 0;
81 return cpu;
82}
787364e8 83#endif /* HAVE_SCHED_GETCPU */
08bf1cc1 84
4327cb7d 85#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
08bf1cc1
MD
86
87/*
4327cb7d
MD
88 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
89 * number 0, with the assocated performance degradation on SMP.
08bf1cc1
MD
90 */
91static inline
5e1b7b8b 92int lttng_ust_get_cpu_internal(void)
08bf1cc1
MD
93{
94 return 0;
95}
96
97#else
98#error "Please add support for your OS into liblttng-ust/compat.h."
99#endif
2b2d6ff7
MD
100
101#endif
102
5e1b7b8b
MD
103static inline
104int lttng_ust_get_cpu(void)
105{
247c2ecd 106 int (*lttng_ust_get_cpu_current)(void) = CMM_LOAD_SHARED(lttng_ust_get_cpu_sym);
5e1b7b8b 107
247c2ecd
MJ
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)) {
5e1b7b8b
MD
113 return lttng_ust_get_cpu_internal();
114 } else {
247c2ecd 115 return lttng_ust_get_cpu_current();
5e1b7b8b
MD
116 }
117}
118
2b2d6ff7 119#endif /* _LTTNG_GETCPU_H */
This page took 0.038566 seconds and 4 git commands to generate.