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