Fix: pass private data to context callbacks
[lttng-ust.git] / libringbuffer / 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
1d18d519
MJ
14void lttng_ust_getcpu_init(void)
15 __attribute__((visibility("hidden")));
5e1b7b8b 16
1d18d519
MJ
17extern int (*lttng_get_cpu)(void)
18 __attribute__((visibility("hidden")));
2b2d6ff7 19
fdb4af10 20#ifdef LTTNG_UST_DEBUG_VALGRIND
2b2d6ff7
MD
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 */
27static inline
5e1b7b8b 28int lttng_ust_get_cpu_internal(void)
2b2d6ff7
MD
29{
30 return 0;
31}
32
33#else
34
08bf1cc1
MD
35/*
36 * sched_getcpu.
37 */
38#ifdef __linux__
39
787364e8 40#if !HAVE_SCHED_GETCPU
08bf1cc1
MD
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 */
46static inline
5e1b7b8b 47int lttng_ust_get_cpu_internal(void)
08bf1cc1
MD
48{
49 int cpu, ret;
50
51 ret = __getcpu(&cpu, NULL, NULL);
52 if (caa_unlikely(ret < 0))
53 return 0;
787364e8 54 return cpu;
08bf1cc1 55}
787364e8 56#else /* HAVE_SCHED_GETCPU */
08bf1cc1
MD
57#include <sched.h>
58
2b2d6ff7
MD
59/*
60 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
61 */
62static inline
5e1b7b8b 63int lttng_ust_get_cpu_internal(void)
2b2d6ff7
MD
64{
65 int cpu;
66
67 cpu = sched_getcpu();
68 if (caa_unlikely(cpu < 0))
69 return 0;
70 return cpu;
71}
787364e8 72#endif /* HAVE_SCHED_GETCPU */
08bf1cc1 73
4327cb7d 74#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
08bf1cc1
MD
75
76/*
4327cb7d
MD
77 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
78 * number 0, with the assocated performance degradation on SMP.
08bf1cc1
MD
79 */
80static inline
5e1b7b8b 81int lttng_ust_get_cpu_internal(void)
08bf1cc1
MD
82{
83 return 0;
84}
85
86#else
87#error "Please add support for your OS into liblttng-ust/compat.h."
88#endif
2b2d6ff7
MD
89
90#endif
91
5e1b7b8b
MD
92static inline
93int 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
2b2d6ff7 104#endif /* _LTTNG_GETCPU_H */
This page took 0.03308 seconds and 4 git commands to generate.