Move struct ltt_channel to shm for consumer flush
[lttng-ust.git] / libringbuffer / frontend.h
CommitLineData
852c2936
MD
1#ifndef _LINUX_RING_BUFFER_FRONTEND_H
2#define _LINUX_RING_BUFFER_FRONTEND_H
3
4/*
5 * linux/ringbuffer/frontend.h
6 *
7 * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Ring Buffer Library Synchronization Header (API).
10 *
11 * Author:
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 *
14 * See ring_buffer_frontend.c for more information on wait-free algorithms.
15 *
16 * Dual LGPL v2.1/GPL v2 license.
17 */
18
14641deb
MD
19#include <urcu/compiler.h>
20#include <urcu/uatomic.h>
21
a6352fd4 22#include "smp.h"
852c2936 23/* Internal helpers */
4931a13e 24#include "frontend_internal.h"
852c2936
MD
25
26/* Buffer creation/removal and setup operations */
27
28/*
29 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
30 * padding to let readers get those sub-buffers. Used for live streaming.
31 *
32 * read_timer_interval is the time interval (in us) to wake up pending readers.
33 *
34 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
35 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
36 * be set to NULL for other backends.
a3f61e7f
MD
37 *
38 * priv_data (output) is set to a pointer into a "priv_data_len"-sized
39 * memory area for client-specific data. This memory is managed by lib
40 * ring buffer. priv_data_align is the alignment required for the
41 * private data area.
852c2936
MD
42 */
43
44extern
4cfec15c 45struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f
MD
46 const char *name,
47 void **priv_data,
48 size_t priv_data_align,
49 size_t priv_data_size,
431d5cf0
MD
50 void *buf_addr,
51 size_t subbuf_size, size_t num_subbuf,
52 unsigned int switch_timer_interval,
193183fb
MD
53 unsigned int read_timer_interval,
54 int *shm_fd, int *wait_fd,
55 uint64_t *memory_map_size);
56
57/* channel_handle_create - for consumer. */
58extern
38fae1d3 59struct lttng_ust_shm_handle *channel_handle_create(int shm_fd, int wait_fd,
193183fb
MD
60 uint64_t memory_map_size);
61
62/* channel_handle_add_stream - for consumer. */
63extern
38fae1d3 64int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
193183fb 65 int shm_fd, int wait_fd, uint64_t memory_map_size);
852c2936
MD
66
67/*
a3f61e7f
MD
68 * channel_destroy finalizes all channel's buffers, waits for readers to
69 * release all references, and destroys the channel.
852c2936
MD
70 */
71extern
a3f61e7f 72void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
824f40b8 73 int shadow);
852c2936
MD
74
75
76/* Buffer read operations */
77
78/*
79 * Iteration on channel cpumask needs to issue a read barrier to match the write
80 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
81 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
82 * only performed at channel destruction.
83 */
84#define for_each_channel_cpu(cpu, chan) \
a6352fd4 85 for_each_possible_cpu(cpu)
852c2936 86
4cfec15c
MD
87extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
88 const struct lttng_ust_lib_ring_buffer_config *config,
1d498196 89 struct channel *chan, int cpu,
38fae1d3 90 struct lttng_ust_shm_handle *handle,
381c0f1e
MD
91 int *shm_fd, int *wait_fd,
92 uint64_t *memory_map_size);
4cfec15c 93extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 94 struct lttng_ust_shm_handle *handle,
824f40b8 95 int shadow);
4cfec15c 96extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 97 struct lttng_ust_shm_handle *handle,
824f40b8 98 int shadow);
852c2936
MD
99
100/*
101 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
102 */
4cfec15c 103extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
852c2936 104 unsigned long *consumed,
1d498196 105 unsigned long *produced,
38fae1d3 106 struct lttng_ust_shm_handle *handle);
4cfec15c 107extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1d498196 108 unsigned long consumed_new,
38fae1d3 109 struct lttng_ust_shm_handle *handle);
852c2936 110
4cfec15c 111extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1d498196 112 unsigned long consumed,
38fae1d3 113 struct lttng_ust_shm_handle *handle);
4cfec15c 114extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 115 struct lttng_ust_shm_handle *handle);
852c2936
MD
116
117/*
118 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
119 * to read sub-buffers sequentially.
120 */
4cfec15c 121static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 122 struct lttng_ust_shm_handle *handle)
852c2936
MD
123{
124 int ret;
125
126 ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1d498196 127 &buf->prod_snapshot, handle);
852c2936
MD
128 if (ret)
129 return ret;
1d498196 130 ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle);
852c2936
MD
131 return ret;
132}
133
1d498196 134static inline
4cfec15c 135void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 136 struct lttng_ust_shm_handle *handle)
852c2936 137{
1d498196 138 lib_ring_buffer_put_subbuf(buf, handle);
852c2936 139 lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot,
1d498196 140 shmp(handle, buf->backend.chan)), handle);
852c2936
MD
141}
142
143extern void channel_reset(struct channel *chan);
4cfec15c 144extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 145 struct lttng_ust_shm_handle *handle);
852c2936
MD
146
147static inline
4cfec15c
MD
148unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config,
149 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
150{
151 return v_read(config, &buf->offset);
152}
153
154static inline
4cfec15c
MD
155unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config,
156 struct lttng_ust_lib_ring_buffer *buf)
852c2936 157{
14641deb 158 return uatomic_read(&buf->consumed);
852c2936
MD
159}
160
161/*
162 * Must call lib_ring_buffer_is_finalized before reading counters (memory
163 * ordering enforced with respect to trace teardown).
164 */
165static inline
4cfec15c
MD
166int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config,
167 struct lttng_ust_lib_ring_buffer *buf)
852c2936 168{
14641deb 169 int finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
170 /*
171 * Read finalized before counters.
172 */
14641deb 173 cmm_smp_rmb();
852c2936
MD
174 return finalized;
175}
176
177static inline
178int lib_ring_buffer_channel_is_finalized(const struct channel *chan)
179{
180 return chan->finalized;
181}
182
183static inline
184int lib_ring_buffer_channel_is_disabled(const struct channel *chan)
185{
14641deb 186 return uatomic_read(&chan->record_disabled);
852c2936
MD
187}
188
189static inline
190unsigned long lib_ring_buffer_get_read_data_size(
4cfec15c
MD
191 const struct lttng_ust_lib_ring_buffer_config *config,
192 struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 193 struct lttng_ust_shm_handle *handle)
852c2936 194{
1d498196 195 return subbuffer_get_read_data_size(config, &buf->backend, handle);
852c2936
MD
196}
197
198static inline
199unsigned long lib_ring_buffer_get_records_count(
4cfec15c
MD
200 const struct lttng_ust_lib_ring_buffer_config *config,
201 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
202{
203 return v_read(config, &buf->records_count);
204}
205
206static inline
207unsigned long lib_ring_buffer_get_records_overrun(
4cfec15c
MD
208 const struct lttng_ust_lib_ring_buffer_config *config,
209 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
210{
211 return v_read(config, &buf->records_overrun);
212}
213
214static inline
215unsigned long lib_ring_buffer_get_records_lost_full(
4cfec15c
MD
216 const struct lttng_ust_lib_ring_buffer_config *config,
217 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
218{
219 return v_read(config, &buf->records_lost_full);
220}
221
222static inline
223unsigned long lib_ring_buffer_get_records_lost_wrap(
4cfec15c
MD
224 const struct lttng_ust_lib_ring_buffer_config *config,
225 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
226{
227 return v_read(config, &buf->records_lost_wrap);
228}
229
230static inline
231unsigned long lib_ring_buffer_get_records_lost_big(
4cfec15c
MD
232 const struct lttng_ust_lib_ring_buffer_config *config,
233 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
234{
235 return v_read(config, &buf->records_lost_big);
236}
237
238static inline
239unsigned long lib_ring_buffer_get_records_read(
4cfec15c
MD
240 const struct lttng_ust_lib_ring_buffer_config *config,
241 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
242{
243 return v_read(config, &buf->backend.records_read);
244}
245
246#endif /* _LINUX_RING_BUFFER_FRONTEND_H */
This page took 0.033548 seconds and 4 git commands to generate.