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