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 | 30 | */ |
b4051ad8 | 31 | #include <stddef.h> |
fb31eb73 | 32 | #include <stdint.h> |
852c2936 | 33 | |
14641deb MD |
34 | #include <urcu/compiler.h> |
35 | #include <urcu/uatomic.h> | |
36 | ||
a6352fd4 | 37 | #include "smp.h" |
852c2936 | 38 | /* Internal helpers */ |
4931a13e | 39 | #include "frontend_internal.h" |
852c2936 MD |
40 | |
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. | |
a3f61e7f MD |
52 | * |
53 | * priv_data (output) is set to a pointer into a "priv_data_len"-sized | |
54 | * memory area for client-specific data. This memory is managed by lib | |
55 | * ring buffer. priv_data_align is the alignment required for the | |
56 | * private data area. | |
852c2936 MD |
57 | */ |
58 | ||
59 | extern | |
4cfec15c | 60 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
61 | const char *name, |
62 | void **priv_data, | |
63 | size_t priv_data_align, | |
64 | size_t priv_data_size, | |
d028eddb | 65 | void *priv_data_init, |
431d5cf0 MD |
66 | void *buf_addr, |
67 | size_t subbuf_size, size_t num_subbuf, | |
68 | unsigned int switch_timer_interval, | |
a9ff648c | 69 | unsigned int read_timer_interval, |
b2c5f61a MD |
70 | const int *stream_fds, int nr_stream_fds, |
71 | int64_t blocking_timeout); | |
852c2936 MD |
72 | |
73 | /* | |
a3f61e7f MD |
74 | * channel_destroy finalizes all channel's buffers, waits for readers to |
75 | * release all references, and destroys the channel. | |
852c2936 MD |
76 | */ |
77 | extern | |
a3f61e7f | 78 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
74d81a6c | 79 | int consumer); |
852c2936 MD |
80 | |
81 | ||
82 | /* Buffer read operations */ | |
83 | ||
84 | /* | |
85 | * Iteration on channel cpumask needs to issue a read barrier to match the write | |
86 | * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu | |
87 | * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is | |
88 | * only performed at channel destruction. | |
89 | */ | |
90 | #define for_each_channel_cpu(cpu, chan) \ | |
a6352fd4 | 91 | for_each_possible_cpu(cpu) |
852c2936 | 92 | |
4cfec15c MD |
93 | extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
94 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 95 | struct channel *chan, int cpu, |
38fae1d3 | 96 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
97 | int *shm_fd, int *wait_fd, |
98 | int *wakeup_fd, | |
99 | uint64_t *memory_map_size); | |
100 | extern | |
ff0f5728 MD |
101 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
102 | struct channel *chan, | |
103 | struct lttng_ust_shm_handle *handle); | |
104 | extern | |
105 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
106 | struct channel *chan, | |
107 | struct lttng_ust_shm_handle *handle); | |
108 | extern | |
109 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
74d81a6c MD |
110 | struct channel *chan, |
111 | struct lttng_ust_shm_handle *handle, | |
112 | int cpu); | |
113 | extern | |
ff0f5728 | 114 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
74d81a6c MD |
115 | struct channel *chan, |
116 | struct lttng_ust_shm_handle *handle, | |
117 | int cpu); | |
118 | ||
4cfec15c | 119 | extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 120 | struct lttng_ust_shm_handle *handle); |
4cfec15c | 121 | extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 122 | struct lttng_ust_shm_handle *handle); |
852c2936 | 123 | |
03d2d293 MD |
124 | /* |
125 | * Initialize signals for ring buffer. Should be called early e.g. by | |
126 | * main() in the program to affect all threads. | |
127 | */ | |
128 | void lib_ringbuffer_signal_init(void); | |
129 | ||
852c2936 MD |
130 | /* |
131 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
132 | */ | |
4cfec15c | 133 | extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 134 | unsigned long *consumed, |
1d498196 | 135 | unsigned long *produced, |
38fae1d3 | 136 | struct lttng_ust_shm_handle *handle); |
f45930b7 JG |
137 | extern int lib_ring_buffer_snapshot_sample_positions( |
138 | struct lttng_ust_lib_ring_buffer *buf, | |
139 | unsigned long *consumed, | |
140 | unsigned long *produced, | |
141 | struct lttng_ust_shm_handle *handle); | |
4cfec15c | 142 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 143 | unsigned long consumed_new, |
38fae1d3 | 144 | struct lttng_ust_shm_handle *handle); |
852c2936 | 145 | |
4cfec15c | 146 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 147 | unsigned long consumed, |
38fae1d3 | 148 | struct lttng_ust_shm_handle *handle); |
4cfec15c | 149 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 150 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
151 | |
152 | /* | |
153 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers | |
154 | * to read sub-buffers sequentially. | |
155 | */ | |
4cfec15c | 156 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 157 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
158 | { |
159 | int ret; | |
160 | ||
161 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, | |
1d498196 | 162 | &buf->prod_snapshot, handle); |
852c2936 MD |
163 | if (ret) |
164 | return ret; | |
1d498196 | 165 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle); |
852c2936 MD |
166 | return ret; |
167 | } | |
168 | ||
1d498196 | 169 | static inline |
4cfec15c | 170 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 171 | struct lttng_ust_shm_handle *handle) |
852c2936 | 172 | { |
15500a1b MD |
173 | struct channel *chan; |
174 | ||
175 | chan = shmp(handle, buf->backend.chan); | |
176 | if (!chan) | |
177 | return; | |
1d498196 | 178 | lib_ring_buffer_put_subbuf(buf, handle); |
15500a1b MD |
179 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan), |
180 | handle); | |
852c2936 MD |
181 | } |
182 | ||
183 | extern void channel_reset(struct channel *chan); | |
4cfec15c | 184 | extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 185 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
186 | |
187 | static inline | |
4cfec15c MD |
188 | unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
189 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
190 | { |
191 | return v_read(config, &buf->offset); | |
192 | } | |
193 | ||
194 | static inline | |
4cfec15c MD |
195 | unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config, |
196 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 197 | { |
14641deb | 198 | return uatomic_read(&buf->consumed); |
852c2936 MD |
199 | } |
200 | ||
201 | /* | |
202 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
203 | * ordering enforced with respect to trace teardown). | |
204 | */ | |
205 | static inline | |
4cfec15c MD |
206 | int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config, |
207 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 208 | { |
14641deb | 209 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
210 | /* |
211 | * Read finalized before counters. | |
212 | */ | |
14641deb | 213 | cmm_smp_rmb(); |
852c2936 MD |
214 | return finalized; |
215 | } | |
216 | ||
217 | static inline | |
218 | int lib_ring_buffer_channel_is_finalized(const struct channel *chan) | |
219 | { | |
220 | return chan->finalized; | |
221 | } | |
222 | ||
223 | static inline | |
224 | int lib_ring_buffer_channel_is_disabled(const struct channel *chan) | |
225 | { | |
14641deb | 226 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
227 | } |
228 | ||
229 | static inline | |
230 | unsigned long lib_ring_buffer_get_read_data_size( | |
4cfec15c MD |
231 | const struct lttng_ust_lib_ring_buffer_config *config, |
232 | struct lttng_ust_lib_ring_buffer *buf, | |
38fae1d3 | 233 | struct lttng_ust_shm_handle *handle) |
852c2936 | 234 | { |
1d498196 | 235 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
236 | } |
237 | ||
238 | static inline | |
239 | unsigned long lib_ring_buffer_get_records_count( | |
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->records_count); | |
244 | } | |
245 | ||
246 | static inline | |
247 | unsigned long lib_ring_buffer_get_records_overrun( | |
4cfec15c MD |
248 | const struct lttng_ust_lib_ring_buffer_config *config, |
249 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
250 | { |
251 | return v_read(config, &buf->records_overrun); | |
252 | } | |
253 | ||
254 | static inline | |
255 | unsigned long lib_ring_buffer_get_records_lost_full( | |
4cfec15c MD |
256 | const struct lttng_ust_lib_ring_buffer_config *config, |
257 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
258 | { |
259 | return v_read(config, &buf->records_lost_full); | |
260 | } | |
261 | ||
262 | static inline | |
263 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
4cfec15c MD |
264 | const struct lttng_ust_lib_ring_buffer_config *config, |
265 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
266 | { |
267 | return v_read(config, &buf->records_lost_wrap); | |
268 | } | |
269 | ||
270 | static inline | |
271 | unsigned long lib_ring_buffer_get_records_lost_big( | |
4cfec15c MD |
272 | const struct lttng_ust_lib_ring_buffer_config *config, |
273 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
274 | { |
275 | return v_read(config, &buf->records_lost_big); | |
276 | } | |
277 | ||
278 | static inline | |
279 | unsigned long lib_ring_buffer_get_records_read( | |
4cfec15c MD |
280 | const struct lttng_ust_lib_ring_buffer_config *config, |
281 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
282 | { |
283 | return v_read(config, &buf->backend.records_read); | |
284 | } | |
285 | ||
e92f3e28 | 286 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |