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