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, | |
193183fb | 67 | unsigned int read_timer_interval, |
ef9ff354 MD |
68 | int **shm_fd, int **wait_fd, |
69 | uint64_t **memory_map_size); | |
193183fb MD |
70 | |
71 | /* channel_handle_create - for consumer. */ | |
72 | extern | |
38fae1d3 | 73 | struct lttng_ust_shm_handle *channel_handle_create(int shm_fd, int wait_fd, |
193183fb MD |
74 | uint64_t memory_map_size); |
75 | ||
76 | /* channel_handle_add_stream - for consumer. */ | |
77 | extern | |
38fae1d3 | 78 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
193183fb | 79 | int shm_fd, int wait_fd, uint64_t memory_map_size); |
852c2936 MD |
80 | |
81 | /* | |
a3f61e7f MD |
82 | * channel_destroy finalizes all channel's buffers, waits for readers to |
83 | * release all references, and destroys the channel. | |
852c2936 MD |
84 | */ |
85 | extern | |
a3f61e7f | 86 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 87 | int shadow); |
852c2936 MD |
88 | |
89 | ||
90 | /* Buffer read operations */ | |
91 | ||
92 | /* | |
93 | * Iteration on channel cpumask needs to issue a read barrier to match the write | |
94 | * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu | |
95 | * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is | |
96 | * only performed at channel destruction. | |
97 | */ | |
98 | #define for_each_channel_cpu(cpu, chan) \ | |
a6352fd4 | 99 | for_each_possible_cpu(cpu) |
852c2936 | 100 | |
4cfec15c MD |
101 | extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
102 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 103 | struct channel *chan, int cpu, |
38fae1d3 | 104 | struct lttng_ust_shm_handle *handle, |
ef9ff354 MD |
105 | int **shm_fd, int **wait_fd, |
106 | uint64_t **memory_map_size); | |
4cfec15c | 107 | extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 108 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 109 | int shadow); |
4cfec15c | 110 | extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 111 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 112 | int shadow); |
852c2936 MD |
113 | |
114 | /* | |
115 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
116 | */ | |
4cfec15c | 117 | extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 118 | unsigned long *consumed, |
1d498196 | 119 | unsigned long *produced, |
38fae1d3 | 120 | struct lttng_ust_shm_handle *handle); |
4cfec15c | 121 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 122 | unsigned long consumed_new, |
38fae1d3 | 123 | struct lttng_ust_shm_handle *handle); |
852c2936 | 124 | |
4cfec15c | 125 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 126 | unsigned long consumed, |
38fae1d3 | 127 | struct lttng_ust_shm_handle *handle); |
4cfec15c | 128 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 129 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
130 | |
131 | /* | |
132 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers | |
133 | * to read sub-buffers sequentially. | |
134 | */ | |
4cfec15c | 135 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 136 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
137 | { |
138 | int ret; | |
139 | ||
140 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, | |
1d498196 | 141 | &buf->prod_snapshot, handle); |
852c2936 MD |
142 | if (ret) |
143 | return ret; | |
1d498196 | 144 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle); |
852c2936 MD |
145 | return ret; |
146 | } | |
147 | ||
1d498196 | 148 | static inline |
4cfec15c | 149 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 150 | struct lttng_ust_shm_handle *handle) |
852c2936 | 151 | { |
1d498196 | 152 | lib_ring_buffer_put_subbuf(buf, handle); |
852c2936 | 153 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, |
1d498196 | 154 | shmp(handle, buf->backend.chan)), handle); |
852c2936 MD |
155 | } |
156 | ||
157 | extern void channel_reset(struct channel *chan); | |
4cfec15c | 158 | extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 159 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
160 | |
161 | static inline | |
4cfec15c MD |
162 | unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
163 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
164 | { |
165 | return v_read(config, &buf->offset); | |
166 | } | |
167 | ||
168 | static inline | |
4cfec15c MD |
169 | unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config, |
170 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 171 | { |
14641deb | 172 | return uatomic_read(&buf->consumed); |
852c2936 MD |
173 | } |
174 | ||
175 | /* | |
176 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
177 | * ordering enforced with respect to trace teardown). | |
178 | */ | |
179 | static inline | |
4cfec15c MD |
180 | int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config, |
181 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 182 | { |
14641deb | 183 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
184 | /* |
185 | * Read finalized before counters. | |
186 | */ | |
14641deb | 187 | cmm_smp_rmb(); |
852c2936 MD |
188 | return finalized; |
189 | } | |
190 | ||
191 | static inline | |
192 | int lib_ring_buffer_channel_is_finalized(const struct channel *chan) | |
193 | { | |
194 | return chan->finalized; | |
195 | } | |
196 | ||
197 | static inline | |
198 | int lib_ring_buffer_channel_is_disabled(const struct channel *chan) | |
199 | { | |
14641deb | 200 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
201 | } |
202 | ||
203 | static inline | |
204 | unsigned long lib_ring_buffer_get_read_data_size( | |
4cfec15c MD |
205 | const struct lttng_ust_lib_ring_buffer_config *config, |
206 | struct lttng_ust_lib_ring_buffer *buf, | |
38fae1d3 | 207 | struct lttng_ust_shm_handle *handle) |
852c2936 | 208 | { |
1d498196 | 209 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
210 | } |
211 | ||
212 | static inline | |
213 | unsigned long lib_ring_buffer_get_records_count( | |
4cfec15c MD |
214 | const struct lttng_ust_lib_ring_buffer_config *config, |
215 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
216 | { |
217 | return v_read(config, &buf->records_count); | |
218 | } | |
219 | ||
220 | static inline | |
221 | unsigned long lib_ring_buffer_get_records_overrun( | |
4cfec15c MD |
222 | const struct lttng_ust_lib_ring_buffer_config *config, |
223 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
224 | { |
225 | return v_read(config, &buf->records_overrun); | |
226 | } | |
227 | ||
228 | static inline | |
229 | unsigned long lib_ring_buffer_get_records_lost_full( | |
4cfec15c MD |
230 | const struct lttng_ust_lib_ring_buffer_config *config, |
231 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
232 | { |
233 | return v_read(config, &buf->records_lost_full); | |
234 | } | |
235 | ||
236 | static inline | |
237 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
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_lost_wrap); | |
242 | } | |
243 | ||
244 | static inline | |
245 | unsigned long lib_ring_buffer_get_records_lost_big( | |
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_lost_big); | |
250 | } | |
251 | ||
252 | static inline | |
253 | unsigned long lib_ring_buffer_get_records_read( | |
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->backend.records_read); | |
258 | } | |
259 | ||
e92f3e28 | 260 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |