Hide libringbuffer private symbols
[lttng-ust.git] / libringbuffer / getcpu.h
... / ...
CommitLineData
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 "ust-helper.h"
15
16LTTNG_HIDDEN
17void lttng_ust_getcpu_init(void);
18
19LTTNG_HIDDEN
20extern int (*lttng_get_cpu)(void);
21
22#ifdef LTTNG_UST_DEBUG_VALGRIND
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
30int lttng_ust_get_cpu_internal(void)
31{
32 return 0;
33}
34
35#else
36
37/*
38 * sched_getcpu.
39 */
40#ifdef __linux__
41
42#if !HAVE_SCHED_GETCPU
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
49int lttng_ust_get_cpu_internal(void)
50{
51 int cpu, ret;
52
53 ret = __getcpu(&cpu, NULL, NULL);
54 if (caa_unlikely(ret < 0))
55 return 0;
56 return cpu;
57}
58#else /* HAVE_SCHED_GETCPU */
59#include <sched.h>
60
61/*
62 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
63 */
64static inline
65int lttng_ust_get_cpu_internal(void)
66{
67 int cpu;
68
69 cpu = sched_getcpu();
70 if (caa_unlikely(cpu < 0))
71 return 0;
72 return cpu;
73}
74#endif /* HAVE_SCHED_GETCPU */
75
76#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
77
78/*
79 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
80 * number 0, with the assocated performance degradation on SMP.
81 */
82static inline
83int lttng_ust_get_cpu_internal(void)
84{
85 return 0;
86}
87
88#else
89#error "Please add support for your OS into liblttng-ust/compat.h."
90#endif
91
92#endif
93
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
106#endif /* _LTTNG_GETCPU_H */
This page took 0.022173 seconds and 4 git commands to generate.