Fix: wrapper random documentation
[lttng-modules.git] / lib / ringbuffer / frontend.h
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
9f36eaed 2 *
886d51a3 3 * lib/ringbuffer/frontend.h
f3bc08c5
MD
4 *
5 * Ring Buffer Library Synchronization Header (API).
6 *
886d51a3
MD
7 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
f3bc08c5 9 * See ring_buffer_frontend.c for more information on wait-free algorithms.
f3bc08c5
MD
10 */
11
9f36eaed
MJ
12#ifndef _LIB_RING_BUFFER_FRONTEND_H
13#define _LIB_RING_BUFFER_FRONTEND_H
14
f3bc08c5
MD
15#include <linux/pipe_fs_i.h>
16#include <linux/rcupdate.h>
17#include <linux/cpumask.h>
18#include <linux/module.h>
19#include <linux/bitops.h>
20#include <linux/splice.h>
21#include <linux/string.h>
22#include <linux/timer.h>
23#include <linux/sched.h>
24#include <linux/cache.h>
25#include <linux/time.h>
26#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/stat.h>
29#include <linux/cpu.h>
30#include <linux/fs.h>
31
32#include <asm/atomic.h>
33#include <asm/local.h>
34
35/* Internal helpers */
5671a661 36#include <wrapper/ringbuffer/frontend_internal.h>
f3bc08c5 37
b055caba
FG
38/* Max ring buffer nesting count, see lib_ring_buffer_get_cpu(). */
39#define RING_BUFFER_MAX_NESTING 4
40
f3bc08c5
MD
41/* Buffer creation/removal and setup operations */
42
43/*
44 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
45 * padding to let readers get those sub-buffers. Used for live streaming.
46 *
47 * read_timer_interval is the time interval (in us) to wake up pending readers.
48 *
49 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
50 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
51 * be set to NULL for other backends.
52 */
53
54extern
55struct channel *channel_create(const struct lib_ring_buffer_config *config,
56 const char *name, void *priv,
57 void *buf_addr,
58 size_t subbuf_size, size_t num_subbuf,
59 unsigned int switch_timer_interval,
60 unsigned int read_timer_interval);
61
62/*
63 * channel_destroy returns the private data pointer. It finalizes all channel's
64 * buffers, waits for readers to release all references, and destroys the
65 * channel.
66 */
67extern
68void *channel_destroy(struct channel *chan);
69
70
71/* Buffer read operations */
72
73/*
74 * Iteration on channel cpumask needs to issue a read barrier to match the write
75 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
76 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
77 * only performed at channel destruction.
78 */
79#define for_each_channel_cpu(cpu, chan) \
80 for ((cpu) = -1; \
81 ({ (cpu) = cpumask_next(cpu, (chan)->backend.cpumask); \
82 smp_read_barrier_depends(); (cpu) < nr_cpu_ids; });)
83
84extern struct lib_ring_buffer *channel_get_ring_buffer(
85 const struct lib_ring_buffer_config *config,
86 struct channel *chan, int cpu);
87extern int lib_ring_buffer_open_read(struct lib_ring_buffer *buf);
88extern void lib_ring_buffer_release_read(struct lib_ring_buffer *buf);
89
90/*
91 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
92 */
93extern int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf,
94 unsigned long *consumed,
95 unsigned long *produced);
4dce5a48
JG
96extern int lib_ring_buffer_snapshot_sample_positions(
97 struct lib_ring_buffer *buf,
98 unsigned long *consumed,
99 unsigned long *produced);
f3bc08c5
MD
100extern void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf,
101 unsigned long consumed_new);
102
103extern int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf,
104 unsigned long consumed);
105extern void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf);
106
64af2437
MD
107void lib_ring_buffer_set_quiescent_channel(struct channel *chan);
108void lib_ring_buffer_clear_quiescent_channel(struct channel *chan);
109
f3bc08c5
MD
110/*
111 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
112 * to read sub-buffers sequentially.
113 */
114static inline int lib_ring_buffer_get_next_subbuf(struct lib_ring_buffer *buf)
115{
116 int ret;
117
118 ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
119 &buf->prod_snapshot);
120 if (ret)
121 return ret;
122 ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot);
123 return ret;
124}
125
126static inline void lib_ring_buffer_put_next_subbuf(struct lib_ring_buffer *buf)
127{
128 lib_ring_buffer_put_subbuf(buf);
129 lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot,
130 buf->backend.chan));
131}
132
133extern void channel_reset(struct channel *chan);
134extern void lib_ring_buffer_reset(struct lib_ring_buffer *buf);
135
136static inline
137unsigned long lib_ring_buffer_get_offset(const struct lib_ring_buffer_config *config,
138 struct lib_ring_buffer *buf)
139{
140 return v_read(config, &buf->offset);
141}
142
143static inline
144unsigned long lib_ring_buffer_get_consumed(const struct lib_ring_buffer_config *config,
145 struct lib_ring_buffer *buf)
146{
147 return atomic_long_read(&buf->consumed);
148}
149
150/*
151 * Must call lib_ring_buffer_is_finalized before reading counters (memory
152 * ordering enforced with respect to trace teardown).
153 */
154static inline
155int lib_ring_buffer_is_finalized(const struct lib_ring_buffer_config *config,
156 struct lib_ring_buffer *buf)
157{
a8f2d0c7 158 int finalized = READ_ONCE(buf->finalized);
f3bc08c5
MD
159 /*
160 * Read finalized before counters.
161 */
162 smp_rmb();
163 return finalized;
164}
165
24cedcfe
MD
166static inline
167int lib_ring_buffer_channel_is_finalized(const struct channel *chan)
168{
169 return chan->finalized;
170}
171
254ec7bc
MD
172static inline
173int lib_ring_buffer_channel_is_disabled(const struct channel *chan)
174{
175 return atomic_read(&chan->record_disabled);
176}
177
f3bc08c5
MD
178static inline
179unsigned long lib_ring_buffer_get_read_data_size(
180 const struct lib_ring_buffer_config *config,
181 struct lib_ring_buffer *buf)
182{
183 return subbuffer_get_read_data_size(config, &buf->backend);
184}
185
186static inline
187unsigned long lib_ring_buffer_get_records_count(
188 const struct lib_ring_buffer_config *config,
189 struct lib_ring_buffer *buf)
190{
191 return v_read(config, &buf->records_count);
192}
193
194static inline
195unsigned long lib_ring_buffer_get_records_overrun(
196 const struct lib_ring_buffer_config *config,
197 struct lib_ring_buffer *buf)
198{
199 return v_read(config, &buf->records_overrun);
200}
201
202static inline
203unsigned long lib_ring_buffer_get_records_lost_full(
204 const struct lib_ring_buffer_config *config,
205 struct lib_ring_buffer *buf)
206{
207 return v_read(config, &buf->records_lost_full);
208}
209
210static inline
211unsigned long lib_ring_buffer_get_records_lost_wrap(
212 const struct lib_ring_buffer_config *config,
213 struct lib_ring_buffer *buf)
214{
215 return v_read(config, &buf->records_lost_wrap);
216}
217
218static inline
219unsigned long lib_ring_buffer_get_records_lost_big(
220 const struct lib_ring_buffer_config *config,
221 struct lib_ring_buffer *buf)
222{
223 return v_read(config, &buf->records_lost_big);
224}
225
226static inline
227unsigned long lib_ring_buffer_get_records_read(
228 const struct lib_ring_buffer_config *config,
229 struct lib_ring_buffer *buf)
230{
231 return v_read(config, &buf->backend.records_read);
232}
233
886d51a3 234#endif /* _LIB_RING_BUFFER_FRONTEND_H */
This page took 0.043529 seconds and 4 git commands to generate.