Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | /* |
2 | * ring_buffer_frontend.c | |
3 | * | |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; only | |
9 | * version 2.1 of the License. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | * | |
852c2936 MD |
20 | * |
21 | * Ring buffer wait-free buffer synchronization. Producer-consumer and flight | |
22 | * recorder (overwrite) modes. See thesis: | |
23 | * | |
24 | * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D. | |
25 | * dissertation, Ecole Polytechnique de Montreal. | |
26 | * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf | |
27 | * | |
28 | * - Algorithm presentation in Chapter 5: | |
29 | * "Lockless Multi-Core High-Throughput Buffering". | |
30 | * - Algorithm formal verification in Section 8.6: | |
31 | * "Formal verification of LTTng" | |
32 | * | |
33 | * Author: | |
34 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
35 | * | |
36 | * Inspired from LTT and RelayFS: | |
37 | * Karim Yaghmour <karim@opersys.com> | |
38 | * Tom Zanussi <zanussi@us.ibm.com> | |
39 | * Bob Wisniewski <bob@watson.ibm.com> | |
40 | * And from K42 : | |
41 | * Bob Wisniewski <bob@watson.ibm.com> | |
42 | * | |
43 | * Buffer reader semantic : | |
44 | * | |
45 | * - get_subbuf_size | |
46 | * while buffer is not finalized and empty | |
47 | * - get_subbuf | |
48 | * - if return value != 0, continue | |
49 | * - splice one subbuffer worth of data to a pipe | |
50 | * - splice the data from pipe to disk/network | |
51 | * - put_subbuf | |
852c2936 MD |
52 | */ |
53 | ||
5ad63a16 | 54 | #define _GNU_SOURCE |
a6352fd4 | 55 | #include <sys/types.h> |
431d5cf0 MD |
56 | #include <sys/mman.h> |
57 | #include <sys/stat.h> | |
58 | #include <fcntl.h> | |
14641deb | 59 | #include <urcu/compiler.h> |
a6352fd4 | 60 | #include <urcu/ref.h> |
35897f8b | 61 | #include <helper.h> |
14641deb | 62 | |
a6352fd4 | 63 | #include "smp.h" |
4318ae1b | 64 | #include <lttng/ringbuffer-config.h> |
2fed87ae | 65 | #include "vatomic.h" |
4931a13e MD |
66 | #include "backend.h" |
67 | #include "frontend.h" | |
a6352fd4 | 68 | #include "shm.h" |
f645cfa7 | 69 | #include "tlsfixup.h" |
852c2936 | 70 | |
431d5cf0 MD |
71 | #ifndef max |
72 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
73 | #endif | |
74 | ||
64493e4f MD |
75 | /* Print DBG() messages about events lost only every 1048576 hits */ |
76 | #define DBG_PRINT_NR_LOST (1UL << 20) | |
77 | ||
2432c3c9 MD |
78 | /* |
79 | * Use POSIX SHM: shm_open(3) and shm_unlink(3). | |
80 | * close(2) to close the fd returned by shm_open. | |
81 | * shm_unlink releases the shared memory object name. | |
82 | * ftruncate(2) sets the size of the memory object. | |
83 | * mmap/munmap maps the shared memory obj to a virtual address in the | |
84 | * calling proceess (should be done both in libust and consumer). | |
85 | * See shm_overview(7) for details. | |
86 | * Pass file descriptor returned by shm_open(3) to ltt-sessiond through | |
87 | * a UNIX socket. | |
88 | * | |
89 | * Since we don't need to access the object using its name, we can | |
90 | * immediately shm_unlink(3) it, and only keep the handle with its file | |
91 | * descriptor. | |
92 | */ | |
93 | ||
852c2936 MD |
94 | /* |
95 | * Internal structure representing offsets to use at a sub-buffer switch. | |
96 | */ | |
97 | struct switch_offsets { | |
98 | unsigned long begin, end, old; | |
99 | size_t pre_header_padding, size; | |
100 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, | |
101 | switch_old_end:1; | |
102 | }; | |
103 | ||
a6352fd4 | 104 | __thread unsigned int lib_ring_buffer_nesting; |
852c2936 | 105 | |
45e9e699 MD |
106 | /* |
107 | * TODO: this is unused. Errors are saved within the ring buffer. | |
108 | * Eventually, allow consumerd to print these errors. | |
109 | */ | |
852c2936 MD |
110 | static |
111 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 112 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
b68d3dc0 MD |
113 | struct lttng_ust_shm_handle *handle) |
114 | __attribute__((unused)); | |
852c2936 | 115 | |
852c2936 MD |
116 | /** |
117 | * lib_ring_buffer_reset - Reset ring buffer to initial values. | |
118 | * @buf: Ring buffer. | |
119 | * | |
120 | * Effectively empty the ring buffer. Should be called when the buffer is not | |
121 | * used for writing. The ring buffer can be opened for reading, but the reader | |
122 | * should not be using the iterator concurrently with reset. The previous | |
123 | * current iterator record is reset. | |
124 | */ | |
4cfec15c | 125 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 126 | struct lttng_ust_shm_handle *handle) |
852c2936 | 127 | { |
1d498196 | 128 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 129 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
130 | unsigned int i; |
131 | ||
132 | /* | |
133 | * Reset iterator first. It will put the subbuffer if it currently holds | |
134 | * it. | |
135 | */ | |
852c2936 MD |
136 | v_set(config, &buf->offset, 0); |
137 | for (i = 0; i < chan->backend.num_subbuf; i++) { | |
4746ae29 MD |
138 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->cc, 0); |
139 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->seq, 0); | |
140 | v_set(config, &shmp_index(handle, buf->commit_cold, i)->cc_sb, 0); | |
852c2936 | 141 | } |
a6352fd4 MD |
142 | uatomic_set(&buf->consumed, 0); |
143 | uatomic_set(&buf->record_disabled, 0); | |
852c2936 | 144 | v_set(config, &buf->last_tsc, 0); |
1d498196 | 145 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
852c2936 MD |
146 | /* Don't reset number of active readers */ |
147 | v_set(config, &buf->records_lost_full, 0); | |
148 | v_set(config, &buf->records_lost_wrap, 0); | |
149 | v_set(config, &buf->records_lost_big, 0); | |
150 | v_set(config, &buf->records_count, 0); | |
151 | v_set(config, &buf->records_overrun, 0); | |
152 | buf->finalized = 0; | |
153 | } | |
852c2936 MD |
154 | |
155 | /** | |
156 | * channel_reset - Reset channel to initial values. | |
157 | * @chan: Channel. | |
158 | * | |
159 | * Effectively empty the channel. Should be called when the channel is not used | |
160 | * for writing. The channel can be opened for reading, but the reader should not | |
161 | * be using the iterator concurrently with reset. The previous current iterator | |
162 | * record is reset. | |
163 | */ | |
164 | void channel_reset(struct channel *chan) | |
165 | { | |
166 | /* | |
167 | * Reset iterators first. Will put the subbuffer if held for reading. | |
168 | */ | |
a6352fd4 | 169 | uatomic_set(&chan->record_disabled, 0); |
852c2936 MD |
170 | /* Don't reset commit_count_mask, still valid */ |
171 | channel_backend_reset(&chan->backend); | |
172 | /* Don't reset switch/read timer interval */ | |
173 | /* Don't reset notifiers and notifier enable bits */ | |
174 | /* Don't reset reader reference count */ | |
175 | } | |
852c2936 MD |
176 | |
177 | /* | |
178 | * Must be called under cpu hotplug protection. | |
179 | */ | |
4cfec15c | 180 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
a6352fd4 | 181 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 182 | struct lttng_ust_shm_handle *handle, |
1d498196 | 183 | struct shm_object *shmobj) |
852c2936 | 184 | { |
4cfec15c | 185 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
14641deb | 186 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
a3f61e7f | 187 | void *priv = channel_get_private(chan); |
852c2936 | 188 | size_t subbuf_header_size; |
2fed87ae | 189 | uint64_t tsc; |
852c2936 MD |
190 | int ret; |
191 | ||
192 | /* Test for cpu hotplug */ | |
193 | if (buf->backend.allocated) | |
194 | return 0; | |
195 | ||
a6352fd4 | 196 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
1d498196 | 197 | cpu, handle, shmobj); |
852c2936 MD |
198 | if (ret) |
199 | return ret; | |
200 | ||
1d498196 MD |
201 | align_shm(shmobj, __alignof__(struct commit_counters_hot)); |
202 | set_shmp(buf->commit_hot, | |
203 | zalloc_shm(shmobj, | |
204 | sizeof(struct commit_counters_hot) * chan->backend.num_subbuf)); | |
205 | if (!shmp(handle, buf->commit_hot)) { | |
852c2936 MD |
206 | ret = -ENOMEM; |
207 | goto free_chanbuf; | |
208 | } | |
209 | ||
1d498196 MD |
210 | align_shm(shmobj, __alignof__(struct commit_counters_cold)); |
211 | set_shmp(buf->commit_cold, | |
212 | zalloc_shm(shmobj, | |
213 | sizeof(struct commit_counters_cold) * chan->backend.num_subbuf)); | |
214 | if (!shmp(handle, buf->commit_cold)) { | |
852c2936 MD |
215 | ret = -ENOMEM; |
216 | goto free_commit; | |
217 | } | |
218 | ||
852c2936 MD |
219 | /* |
220 | * Write the subbuffer header for first subbuffer so we know the total | |
221 | * duration of data gathering. | |
222 | */ | |
223 | subbuf_header_size = config->cb.subbuffer_header_size(); | |
224 | v_set(config, &buf->offset, subbuf_header_size); | |
4746ae29 | 225 | subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id); |
1d498196 MD |
226 | tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan)); |
227 | config->cb.buffer_begin(buf, tsc, 0, handle); | |
4746ae29 | 228 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc); |
852c2936 MD |
229 | |
230 | if (config->cb.buffer_create) { | |
1d498196 | 231 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
852c2936 MD |
232 | if (ret) |
233 | goto free_init; | |
234 | } | |
852c2936 | 235 | buf->backend.allocated = 1; |
852c2936 MD |
236 | return 0; |
237 | ||
238 | /* Error handling */ | |
239 | free_init: | |
a6352fd4 | 240 | /* commit_cold will be freed by shm teardown */ |
852c2936 | 241 | free_commit: |
a6352fd4 | 242 | /* commit_hot will be freed by shm teardown */ |
852c2936 | 243 | free_chanbuf: |
852c2936 MD |
244 | return ret; |
245 | } | |
246 | ||
1d498196 | 247 | #if 0 |
852c2936 MD |
248 | static void switch_buffer_timer(unsigned long data) |
249 | { | |
4cfec15c | 250 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 251 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 252 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
253 | |
254 | /* | |
255 | * Only flush buffers periodically if readers are active. | |
256 | */ | |
824f40b8 | 257 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
1d498196 | 258 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle); |
852c2936 | 259 | |
a6352fd4 MD |
260 | //TODO timers |
261 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
262 | // mod_timer_pinned(&buf->switch_timer, | |
263 | // jiffies + chan->switch_timer_interval); | |
264 | //else | |
265 | // mod_timer(&buf->switch_timer, | |
266 | // jiffies + chan->switch_timer_interval); | |
852c2936 | 267 | } |
1d498196 | 268 | #endif //0 |
852c2936 | 269 | |
4cfec15c | 270 | static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 271 | struct lttng_ust_shm_handle *handle) |
852c2936 | 272 | { |
1d498196 | 273 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 274 | //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
275 | |
276 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) | |
277 | return; | |
a6352fd4 MD |
278 | //TODO |
279 | //init_timer(&buf->switch_timer); | |
280 | //buf->switch_timer.function = switch_buffer_timer; | |
281 | //buf->switch_timer.expires = jiffies + chan->switch_timer_interval; | |
282 | //buf->switch_timer.data = (unsigned long)buf; | |
283 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
284 | // add_timer_on(&buf->switch_timer, buf->backend.cpu); | |
285 | //else | |
286 | // add_timer(&buf->switch_timer); | |
852c2936 MD |
287 | buf->switch_timer_enabled = 1; |
288 | } | |
289 | ||
4cfec15c | 290 | static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 291 | struct lttng_ust_shm_handle *handle) |
852c2936 | 292 | { |
1d498196 | 293 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 MD |
294 | |
295 | if (!chan->switch_timer_interval || !buf->switch_timer_enabled) | |
296 | return; | |
297 | ||
a6352fd4 MD |
298 | //TODO |
299 | //del_timer_sync(&buf->switch_timer); | |
852c2936 MD |
300 | buf->switch_timer_enabled = 0; |
301 | } | |
302 | ||
1d498196 | 303 | #if 0 |
852c2936 MD |
304 | /* |
305 | * Polling timer to check the channels for data. | |
306 | */ | |
307 | static void read_buffer_timer(unsigned long data) | |
308 | { | |
4cfec15c | 309 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 310 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 311 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
312 | |
313 | CHAN_WARN_ON(chan, !buf->backend.allocated); | |
314 | ||
824f40b8 | 315 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
852c2936 | 316 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
a6352fd4 MD |
317 | //TODO |
318 | //wake_up_interruptible(&buf->read_wait); | |
319 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
320 | } |
321 | ||
a6352fd4 MD |
322 | //TODO |
323 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
324 | // mod_timer_pinned(&buf->read_timer, | |
325 | // jiffies + chan->read_timer_interval); | |
326 | //else | |
327 | // mod_timer(&buf->read_timer, | |
328 | // jiffies + chan->read_timer_interval); | |
852c2936 | 329 | } |
1d498196 | 330 | #endif //0 |
852c2936 | 331 | |
4cfec15c | 332 | static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 333 | struct lttng_ust_shm_handle *handle) |
852c2936 | 334 | { |
1d498196 | 335 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 336 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
337 | |
338 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
339 | || !chan->read_timer_interval | |
340 | || buf->read_timer_enabled) | |
341 | return; | |
342 | ||
a6352fd4 MD |
343 | //TODO |
344 | //init_timer(&buf->read_timer); | |
345 | //buf->read_timer.function = read_buffer_timer; | |
346 | //buf->read_timer.expires = jiffies + chan->read_timer_interval; | |
347 | //buf->read_timer.data = (unsigned long)buf; | |
852c2936 | 348 | |
a6352fd4 MD |
349 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
350 | // add_timer_on(&buf->read_timer, buf->backend.cpu); | |
351 | //else | |
352 | // add_timer(&buf->read_timer); | |
852c2936 MD |
353 | buf->read_timer_enabled = 1; |
354 | } | |
355 | ||
4cfec15c | 356 | static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 357 | struct lttng_ust_shm_handle *handle) |
852c2936 | 358 | { |
1d498196 | 359 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 360 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
361 | |
362 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
363 | || !chan->read_timer_interval | |
364 | || !buf->read_timer_enabled) | |
365 | return; | |
366 | ||
a6352fd4 MD |
367 | //TODO |
368 | //del_timer_sync(&buf->read_timer); | |
852c2936 MD |
369 | /* |
370 | * do one more check to catch data that has been written in the last | |
371 | * timer period. | |
372 | */ | |
1d498196 | 373 | if (lib_ring_buffer_poll_deliver(config, buf, chan, handle)) { |
a6352fd4 MD |
374 | //TODO |
375 | //wake_up_interruptible(&buf->read_wait); | |
376 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
377 | } |
378 | buf->read_timer_enabled = 0; | |
379 | } | |
380 | ||
1d498196 | 381 | static void channel_unregister_notifiers(struct channel *chan, |
38fae1d3 | 382 | struct lttng_ust_shm_handle *handle) |
852c2936 | 383 | { |
4cfec15c | 384 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
385 | int cpu; |
386 | ||
852c2936 | 387 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
852c2936 | 388 | for_each_possible_cpu(cpu) { |
4cfec15c | 389 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
a6352fd4 | 390 | |
1d498196 MD |
391 | lib_ring_buffer_stop_switch_timer(buf, handle); |
392 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 393 | } |
852c2936 | 394 | } else { |
4cfec15c | 395 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 396 | |
1d498196 MD |
397 | lib_ring_buffer_stop_switch_timer(buf, handle); |
398 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 399 | } |
8d8a24c8 | 400 | //channel_backend_unregister_notifiers(&chan->backend); |
852c2936 MD |
401 | } |
402 | ||
38fae1d3 | 403 | static void channel_free(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 404 | int shadow) |
852c2936 | 405 | { |
824f40b8 MD |
406 | if (!shadow) |
407 | channel_backend_free(&chan->backend, handle); | |
431d5cf0 | 408 | /* chan is freed by shm teardown */ |
1d498196 MD |
409 | shm_object_table_destroy(handle->table); |
410 | free(handle); | |
852c2936 MD |
411 | } |
412 | ||
413 | /** | |
414 | * channel_create - Create channel. | |
415 | * @config: ring buffer instance configuration | |
416 | * @name: name of the channel | |
a3f61e7f MD |
417 | * @priv_data: ring buffer client private data area pointer (output) |
418 | * @priv_data_size: length, in bytes, of the private data area. | |
d028eddb | 419 | * @priv_data_init: initialization data for private data. |
852c2936 MD |
420 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
421 | * address mapping. It is used only by RING_BUFFER_STATIC | |
422 | * configuration. It can be set to NULL for other backends. | |
423 | * @subbuf_size: subbuffer size | |
424 | * @num_subbuf: number of subbuffers | |
425 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
426 | * padding to let readers get those sub-buffers. | |
427 | * Used for live streaming. | |
428 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
429 | * | |
430 | * Holds cpu hotplug. | |
431 | * Returns NULL on failure. | |
432 | */ | |
4cfec15c | 433 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
434 | const char *name, |
435 | void **priv_data, | |
436 | size_t priv_data_align, | |
437 | size_t priv_data_size, | |
d028eddb | 438 | void *priv_data_init, |
a3f61e7f | 439 | void *buf_addr, size_t subbuf_size, |
852c2936 | 440 | size_t num_subbuf, unsigned int switch_timer_interval, |
193183fb | 441 | unsigned int read_timer_interval, |
ef9ff354 | 442 | int **shm_fd, int **wait_fd, uint64_t **memory_map_size) |
852c2936 | 443 | { |
1d498196 | 444 | int ret, cpu; |
a3f61e7f | 445 | size_t shmsize, chansize; |
852c2936 | 446 | struct channel *chan; |
38fae1d3 | 447 | struct lttng_ust_shm_handle *handle; |
1d498196 | 448 | struct shm_object *shmobj; |
193183fb | 449 | struct shm_ref *ref; |
852c2936 MD |
450 | |
451 | if (lib_ring_buffer_check_config(config, switch_timer_interval, | |
452 | read_timer_interval)) | |
453 | return NULL; | |
454 | ||
38fae1d3 | 455 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
431d5cf0 MD |
456 | if (!handle) |
457 | return NULL; | |
458 | ||
1d498196 MD |
459 | /* Allocate table for channel + per-cpu buffers */ |
460 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
461 | if (!handle->table) | |
462 | goto error_table_alloc; | |
852c2936 | 463 | |
1d498196 MD |
464 | /* Calculate the shm allocation layout */ |
465 | shmsize = sizeof(struct channel); | |
c1fca457 | 466 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
1d498196 | 467 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
4cfec15c | 468 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * num_possible_cpus(); |
1d498196 | 469 | else |
4cfec15c | 470 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp); |
a3f61e7f MD |
471 | chansize = shmsize; |
472 | shmsize += offset_align(shmsize, priv_data_align); | |
473 | shmsize += priv_data_size; | |
a6352fd4 | 474 | |
1d498196 | 475 | shmobj = shm_object_table_append(handle->table, shmsize); |
b5a14697 MD |
476 | if (!shmobj) |
477 | goto error_append; | |
57773204 | 478 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
a3f61e7f | 479 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
57773204 MD |
480 | assert(handle->chan._ref.index == 0); |
481 | assert(handle->chan._ref.offset == 0); | |
1d498196 | 482 | chan = shmp(handle, handle->chan); |
a6352fd4 | 483 | if (!chan) |
1d498196 | 484 | goto error_append; |
a6352fd4 | 485 | |
a3f61e7f MD |
486 | /* space for private data */ |
487 | if (priv_data_size) { | |
488 | DECLARE_SHMP(void, priv_data_alloc); | |
489 | ||
490 | align_shm(shmobj, priv_data_align); | |
491 | chan->priv_data_offset = shmobj->allocated_len; | |
492 | set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size)); | |
493 | if (!shmp(handle, priv_data_alloc)) | |
494 | goto error_append; | |
495 | *priv_data = channel_get_private(chan); | |
d028eddb | 496 | memcpy(*priv_data, priv_data_init, priv_data_size); |
a3f61e7f MD |
497 | } else { |
498 | chan->priv_data_offset = -1; | |
499 | *priv_data = NULL; | |
500 | } | |
501 | ||
502 | ret = channel_backend_init(&chan->backend, name, config, | |
1d498196 | 503 | subbuf_size, num_subbuf, handle); |
852c2936 | 504 | if (ret) |
1d498196 | 505 | goto error_backend_init; |
852c2936 MD |
506 | |
507 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); | |
a6352fd4 MD |
508 | //TODO |
509 | //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); | |
510 | //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); | |
a6352fd4 MD |
511 | //TODO |
512 | //init_waitqueue_head(&chan->read_wait); | |
513 | //init_waitqueue_head(&chan->hp_wait); | |
852c2936 MD |
514 | |
515 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
852c2936 MD |
516 | /* |
517 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
518 | * in early initcall, it will not be notified of secondary cpus. | |
519 | * In that off case, we need to allocate for all possible cpus. | |
520 | */ | |
852c2936 | 521 | for_each_possible_cpu(cpu) { |
4cfec15c | 522 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
1d498196 MD |
523 | lib_ring_buffer_start_switch_timer(buf, handle); |
524 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 525 | } |
852c2936 | 526 | } else { |
4cfec15c | 527 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 528 | |
1d498196 MD |
529 | lib_ring_buffer_start_switch_timer(buf, handle); |
530 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 531 | } |
193183fb MD |
532 | ref = &handle->chan._ref; |
533 | shm_get_object_data(handle, ref, shm_fd, wait_fd, memory_map_size); | |
431d5cf0 | 534 | return handle; |
852c2936 | 535 | |
1d498196 MD |
536 | error_backend_init: |
537 | error_append: | |
538 | shm_object_table_destroy(handle->table); | |
539 | error_table_alloc: | |
431d5cf0 | 540 | free(handle); |
852c2936 MD |
541 | return NULL; |
542 | } | |
852c2936 | 543 | |
38fae1d3 | 544 | struct lttng_ust_shm_handle *channel_handle_create(int shm_fd, int wait_fd, |
193183fb MD |
545 | uint64_t memory_map_size) |
546 | { | |
38fae1d3 | 547 | struct lttng_ust_shm_handle *handle; |
193183fb MD |
548 | struct shm_object *object; |
549 | ||
38fae1d3 | 550 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
193183fb MD |
551 | if (!handle) |
552 | return NULL; | |
553 | ||
554 | /* Allocate table for channel + per-cpu buffers */ | |
555 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
556 | if (!handle->table) | |
557 | goto error_table_alloc; | |
558 | /* Add channel object */ | |
559 | object = shm_object_table_append_shadow(handle->table, | |
560 | shm_fd, wait_fd, memory_map_size); | |
561 | if (!object) | |
562 | goto error_table_object; | |
57773204 MD |
563 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
564 | handle->chan._ref.index = 0; | |
565 | handle->chan._ref.offset = 0; | |
193183fb MD |
566 | return handle; |
567 | ||
568 | error_table_object: | |
569 | shm_object_table_destroy(handle->table); | |
570 | error_table_alloc: | |
571 | free(handle); | |
572 | return NULL; | |
573 | } | |
574 | ||
38fae1d3 | 575 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
193183fb MD |
576 | int shm_fd, int wait_fd, uint64_t memory_map_size) |
577 | { | |
578 | struct shm_object *object; | |
579 | ||
580 | /* Add stream object */ | |
581 | object = shm_object_table_append_shadow(handle->table, | |
582 | shm_fd, wait_fd, memory_map_size); | |
583 | if (!object) | |
584 | return -1; | |
585 | return 0; | |
586 | } | |
587 | ||
852c2936 | 588 | static |
38fae1d3 | 589 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 590 | int shadow) |
852c2936 | 591 | { |
824f40b8 | 592 | channel_free(chan, handle, shadow); |
852c2936 MD |
593 | } |
594 | ||
595 | /** | |
596 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
597 | * @chan: channel to destroy | |
598 | * | |
599 | * Holds cpu hotplug. | |
431d5cf0 MD |
600 | * Call "destroy" callback, finalize channels, decrement the channel |
601 | * reference count. Note that when readers have completed data | |
602 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
a3f61e7f | 603 | * They should release their handle at that point. |
852c2936 | 604 | */ |
a3f61e7f | 605 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 606 | int shadow) |
852c2936 | 607 | { |
824f40b8 MD |
608 | if (shadow) { |
609 | channel_release(chan, handle, shadow); | |
a3f61e7f | 610 | return; |
824f40b8 MD |
611 | } |
612 | ||
1d498196 | 613 | channel_unregister_notifiers(chan, handle); |
852c2936 | 614 | |
45e9e699 MD |
615 | /* |
616 | * Note: the consumer takes care of finalizing and switching the | |
617 | * buffers. | |
618 | */ | |
852c2936 | 619 | |
431d5cf0 MD |
620 | /* |
621 | * sessiond/consumer are keeping a reference on the shm file | |
622 | * descriptor directly. No need to refcount. | |
623 | */ | |
824f40b8 | 624 | channel_release(chan, handle, shadow); |
a3f61e7f | 625 | return; |
852c2936 | 626 | } |
852c2936 | 627 | |
4cfec15c MD |
628 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
629 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 630 | struct channel *chan, int cpu, |
38fae1d3 | 631 | struct lttng_ust_shm_handle *handle, |
ef9ff354 MD |
632 | int **shm_fd, int **wait_fd, |
633 | uint64_t **memory_map_size) | |
852c2936 | 634 | { |
381c0f1e MD |
635 | struct shm_ref *ref; |
636 | ||
637 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
638 | ref = &chan->backend.buf[0].shmp._ref; | |
639 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
640 | memory_map_size); | |
1d498196 | 641 | return shmp(handle, chan->backend.buf[0].shmp); |
381c0f1e | 642 | } else { |
e095d803 MD |
643 | if (cpu >= num_possible_cpus()) |
644 | return NULL; | |
381c0f1e MD |
645 | ref = &chan->backend.buf[cpu].shmp._ref; |
646 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
647 | memory_map_size); | |
1d498196 | 648 | return shmp(handle, chan->backend.buf[cpu].shmp); |
381c0f1e | 649 | } |
852c2936 | 650 | } |
852c2936 | 651 | |
4cfec15c | 652 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 653 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 654 | int shadow) |
852c2936 | 655 | { |
824f40b8 MD |
656 | if (shadow) { |
657 | if (uatomic_cmpxchg(&buf->active_shadow_readers, 0, 1) != 0) | |
658 | return -EBUSY; | |
659 | cmm_smp_mb(); | |
660 | return 0; | |
661 | } | |
a6352fd4 | 662 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
852c2936 | 663 | return -EBUSY; |
a6352fd4 | 664 | cmm_smp_mb(); |
852c2936 MD |
665 | return 0; |
666 | } | |
852c2936 | 667 | |
4cfec15c | 668 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 669 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 670 | int shadow) |
852c2936 | 671 | { |
1d498196 | 672 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 | 673 | |
824f40b8 MD |
674 | if (shadow) { |
675 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_shadow_readers) != 1); | |
676 | cmm_smp_mb(); | |
677 | uatomic_dec(&buf->active_shadow_readers); | |
678 | return; | |
679 | } | |
a6352fd4 MD |
680 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
681 | cmm_smp_mb(); | |
682 | uatomic_dec(&buf->active_readers); | |
852c2936 MD |
683 | } |
684 | ||
685 | /** | |
686 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
687 | * @buf: ring buffer | |
688 | * @consumed: consumed count indicating the position where to read | |
689 | * @produced: produced count, indicates position when to stop reading | |
690 | * | |
691 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
692 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
693 | */ |
694 | ||
4cfec15c | 695 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 696 | unsigned long *consumed, unsigned long *produced, |
38fae1d3 | 697 | struct lttng_ust_shm_handle *handle) |
852c2936 | 698 | { |
1d498196 | 699 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 700 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
701 | unsigned long consumed_cur, write_offset; |
702 | int finalized; | |
703 | ||
14641deb | 704 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
705 | /* |
706 | * Read finalized before counters. | |
707 | */ | |
a6352fd4 MD |
708 | cmm_smp_rmb(); |
709 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 MD |
710 | /* |
711 | * No need to issue a memory barrier between consumed count read and | |
712 | * write offset read, because consumed count can only change | |
713 | * concurrently in overwrite mode, and we keep a sequence counter | |
714 | * identifier derived from the write offset to check we are getting | |
715 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
716 | * "tagged" upon writes, tags are checked upon read). | |
717 | */ | |
718 | write_offset = v_read(config, &buf->offset); | |
719 | ||
720 | /* | |
721 | * Check that we are not about to read the same subbuffer in | |
722 | * which the writer head is. | |
723 | */ | |
724 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
725 | == 0) | |
726 | goto nodata; | |
727 | ||
728 | *consumed = consumed_cur; | |
729 | *produced = subbuf_trunc(write_offset, chan); | |
730 | ||
731 | return 0; | |
732 | ||
733 | nodata: | |
734 | /* | |
735 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
736 | * of "raw_spin_is_locked" memory ordering. | |
737 | */ | |
738 | if (finalized) | |
739 | return -ENODATA; | |
852c2936 MD |
740 | else |
741 | return -EAGAIN; | |
742 | } | |
852c2936 MD |
743 | |
744 | /** | |
745 | * lib_ring_buffer_put_snapshot - move consumed counter forward | |
746 | * @buf: ring buffer | |
747 | * @consumed_new: new consumed count value | |
748 | */ | |
4cfec15c | 749 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 750 | unsigned long consumed_new, |
38fae1d3 | 751 | struct lttng_ust_shm_handle *handle) |
852c2936 | 752 | { |
4cfec15c | 753 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 754 | struct channel *chan = shmp(handle, bufb->chan); |
852c2936 MD |
755 | unsigned long consumed; |
756 | ||
824f40b8 MD |
757 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
758 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
759 | |
760 | /* | |
761 | * Only push the consumed value forward. | |
762 | * If the consumed cmpxchg fails, this is because we have been pushed by | |
763 | * the writer in flight recorder mode. | |
764 | */ | |
a6352fd4 | 765 | consumed = uatomic_read(&buf->consumed); |
852c2936 | 766 | while ((long) consumed - (long) consumed_new < 0) |
a6352fd4 MD |
767 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
768 | consumed_new); | |
852c2936 | 769 | } |
852c2936 MD |
770 | |
771 | /** | |
772 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading | |
773 | * @buf: ring buffer | |
774 | * @consumed: consumed count indicating the position where to read | |
775 | * | |
776 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
777 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 | 778 | */ |
4cfec15c | 779 | int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 780 | unsigned long consumed, |
38fae1d3 | 781 | struct lttng_ust_shm_handle *handle) |
852c2936 | 782 | { |
1d498196 | 783 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 784 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
785 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
786 | int ret; | |
787 | int finalized; | |
788 | ||
789 | retry: | |
14641deb | 790 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
791 | /* |
792 | * Read finalized before counters. | |
793 | */ | |
a6352fd4 MD |
794 | cmm_smp_rmb(); |
795 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 | 796 | consumed_idx = subbuf_index(consumed, chan); |
4746ae29 | 797 | commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb); |
852c2936 MD |
798 | /* |
799 | * Make sure we read the commit count before reading the buffer | |
800 | * data and the write offset. Correct consumed offset ordering | |
801 | * wrt commit count is insured by the use of cmpxchg to update | |
802 | * the consumed offset. | |
852c2936 | 803 | */ |
a6352fd4 MD |
804 | /* |
805 | * Local rmb to match the remote wmb to read the commit count | |
806 | * before the buffer data and the write offset. | |
807 | */ | |
808 | cmm_smp_rmb(); | |
852c2936 MD |
809 | |
810 | write_offset = v_read(config, &buf->offset); | |
811 | ||
812 | /* | |
813 | * Check that the buffer we are getting is after or at consumed_cur | |
814 | * position. | |
815 | */ | |
816 | if ((long) subbuf_trunc(consumed, chan) | |
817 | - (long) subbuf_trunc(consumed_cur, chan) < 0) | |
818 | goto nodata; | |
819 | ||
820 | /* | |
821 | * Check that the subbuffer we are trying to consume has been | |
822 | * already fully committed. | |
823 | */ | |
824 | if (((commit_count - chan->backend.subbuf_size) | |
825 | & chan->commit_count_mask) | |
826 | - (buf_trunc(consumed_cur, chan) | |
827 | >> chan->backend.num_subbuf_order) | |
828 | != 0) | |
829 | goto nodata; | |
830 | ||
831 | /* | |
832 | * Check that we are not about to read the same subbuffer in | |
833 | * which the writer head is. | |
834 | */ | |
835 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
836 | == 0) | |
837 | goto nodata; | |
838 | ||
839 | /* | |
840 | * Failure to get the subbuffer causes a busy-loop retry without going | |
841 | * to a wait queue. These are caused by short-lived race windows where | |
842 | * the writer is getting access to a subbuffer we were trying to get | |
843 | * access to. Also checks that the "consumed" buffer count we are | |
844 | * looking for matches the one contained in the subbuffer id. | |
845 | */ | |
846 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
847 | consumed_idx, buf_trunc_val(consumed, chan), |
848 | handle); | |
852c2936 MD |
849 | if (ret) |
850 | goto retry; | |
851 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); | |
852 | ||
853 | buf->get_subbuf_consumed = consumed; | |
854 | buf->get_subbuf = 1; | |
855 | ||
856 | return 0; | |
857 | ||
858 | nodata: | |
859 | /* | |
860 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
861 | * of "raw_spin_is_locked" memory ordering. | |
862 | */ | |
863 | if (finalized) | |
864 | return -ENODATA; | |
852c2936 MD |
865 | else |
866 | return -EAGAIN; | |
867 | } | |
852c2936 MD |
868 | |
869 | /** | |
870 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access | |
871 | * @buf: ring buffer | |
872 | */ | |
4cfec15c | 873 | void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 874 | struct lttng_ust_shm_handle *handle) |
852c2936 | 875 | { |
4cfec15c | 876 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 877 | struct channel *chan = shmp(handle, bufb->chan); |
4cfec15c | 878 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
879 | unsigned long read_sb_bindex, consumed_idx, consumed; |
880 | ||
824f40b8 MD |
881 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
882 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
883 | |
884 | if (!buf->get_subbuf) { | |
885 | /* | |
886 | * Reader puts a subbuffer it did not get. | |
887 | */ | |
888 | CHAN_WARN_ON(chan, 1); | |
889 | return; | |
890 | } | |
891 | consumed = buf->get_subbuf_consumed; | |
892 | buf->get_subbuf = 0; | |
893 | ||
894 | /* | |
895 | * Clear the records_unread counter. (overruns counter) | |
896 | * Can still be non-zero if a file reader simply grabbed the data | |
897 | * without using iterators. | |
898 | * Can be below zero if an iterator is used on a snapshot more than | |
899 | * once. | |
900 | */ | |
901 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
902 | v_add(config, v_read(config, | |
4746ae29 | 903 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
852c2936 | 904 | &bufb->records_read); |
4746ae29 | 905 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
852c2936 MD |
906 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
907 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
908 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
909 | ||
910 | /* | |
911 | * Exchange the reader subbuffer with the one we put in its place in the | |
912 | * writer subbuffer table. Expect the original consumed count. If | |
913 | * update_read_sb_index fails, this is because the writer updated the | |
914 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
915 | * currently have: it has become invalid to try reading this sub-buffer | |
916 | * consumed count value anyway. | |
917 | */ | |
918 | consumed_idx = subbuf_index(consumed, chan); | |
919 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
920 | consumed_idx, buf_trunc_val(consumed, chan), |
921 | handle); | |
852c2936 MD |
922 | /* |
923 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
924 | * if the writer concurrently updated it. | |
925 | */ | |
926 | } | |
852c2936 MD |
927 | |
928 | /* | |
929 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
930 | * position and the writer position. (inclusive) | |
931 | */ | |
932 | static | |
4cfec15c | 933 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
934 | struct channel *chan, |
935 | unsigned long cons_offset, | |
1d498196 | 936 | int cpu, |
38fae1d3 | 937 | struct lttng_ust_shm_handle *handle) |
852c2936 | 938 | { |
4cfec15c | 939 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
940 | unsigned long cons_idx, commit_count, commit_count_sb; |
941 | ||
942 | cons_idx = subbuf_index(cons_offset, chan); | |
4746ae29 MD |
943 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc); |
944 | commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb); | |
852c2936 MD |
945 | |
946 | if (subbuf_offset(commit_count, chan) != 0) | |
4d3c9523 | 947 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
948 | "commit count in subbuffer %lu,\n" |
949 | "expecting multiples of %lu bytes\n" | |
950 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
951 | chan->backend.name, cpu, cons_idx, | |
952 | chan->backend.subbuf_size, | |
953 | commit_count, commit_count_sb); | |
954 | ||
4d3c9523 | 955 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
852c2936 MD |
956 | chan->backend.name, cpu, commit_count); |
957 | } | |
958 | ||
959 | static | |
4cfec15c | 960 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 961 | struct channel *chan, |
1d498196 | 962 | void *priv, int cpu, |
38fae1d3 | 963 | struct lttng_ust_shm_handle *handle) |
852c2936 | 964 | { |
4cfec15c | 965 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
966 | unsigned long write_offset, cons_offset; |
967 | ||
852c2936 MD |
968 | /* |
969 | * No need to order commit_count, write_offset and cons_offset reads | |
970 | * because we execute at teardown when no more writer nor reader | |
971 | * references are left. | |
972 | */ | |
973 | write_offset = v_read(config, &buf->offset); | |
a6352fd4 | 974 | cons_offset = uatomic_read(&buf->consumed); |
852c2936 | 975 | if (write_offset != cons_offset) |
4d3c9523 | 976 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
977 | "non-consumed data\n" |
978 | " [ %lu bytes written, %lu bytes read ]\n", | |
979 | chan->backend.name, cpu, write_offset, cons_offset); | |
980 | ||
a6352fd4 | 981 | for (cons_offset = uatomic_read(&buf->consumed); |
852c2936 MD |
982 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
983 | chan) | |
984 | - cons_offset) > 0; | |
985 | cons_offset = subbuf_align(cons_offset, chan)) | |
986 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1d498196 | 987 | cpu, handle); |
852c2936 MD |
988 | } |
989 | ||
990 | static | |
991 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 992 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
38fae1d3 | 993 | struct lttng_ust_shm_handle *handle) |
852c2936 | 994 | { |
4cfec15c | 995 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
a3f61e7f | 996 | void *priv = channel_get_private(chan); |
852c2936 | 997 | |
a1360615 MD |
998 | if (!strcmp(chan->backend.name, "relay-metadata-mmap")) { |
999 | DBG("ring buffer %s: %lu records written, " | |
1000 | "%lu records overrun\n", | |
1001 | chan->backend.name, | |
1002 | v_read(config, &buf->records_count), | |
1003 | v_read(config, &buf->records_overrun)); | |
1004 | } else { | |
1005 | DBG("ring buffer %s, cpu %d: %lu records written, " | |
1006 | "%lu records overrun\n", | |
1007 | chan->backend.name, cpu, | |
1008 | v_read(config, &buf->records_count), | |
1009 | v_read(config, &buf->records_overrun)); | |
1010 | ||
1011 | if (v_read(config, &buf->records_lost_full) | |
1012 | || v_read(config, &buf->records_lost_wrap) | |
1013 | || v_read(config, &buf->records_lost_big)) | |
1014 | DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n" | |
1015 | " [ %lu buffer full, %lu nest buffer wrap-around, " | |
1016 | "%lu event too big ]\n", | |
1017 | chan->backend.name, cpu, | |
1018 | v_read(config, &buf->records_lost_full), | |
1019 | v_read(config, &buf->records_lost_wrap), | |
1020 | v_read(config, &buf->records_lost_big)); | |
1021 | } | |
1d498196 | 1022 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
852c2936 MD |
1023 | } |
1024 | ||
1025 | /* | |
1026 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. | |
1027 | * | |
1028 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. | |
1029 | */ | |
1030 | static | |
4cfec15c | 1031 | void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1032 | struct channel *chan, |
1033 | struct switch_offsets *offsets, | |
2fed87ae | 1034 | uint64_t tsc, |
38fae1d3 | 1035 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1036 | { |
4cfec15c | 1037 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1038 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
1039 | unsigned long commit_count; | |
1040 | ||
1d498196 | 1041 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
852c2936 MD |
1042 | |
1043 | /* | |
1044 | * Order all writes to buffer before the commit count update that will | |
1045 | * determine that the subbuffer is full. | |
1046 | */ | |
a6352fd4 | 1047 | cmm_smp_wmb(); |
852c2936 | 1048 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1049 | &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1050 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 MD |
1051 | /* Check if the written buffer has to be delivered */ |
1052 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, | |
1d498196 | 1053 | commit_count, oldidx, handle); |
852c2936 MD |
1054 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1055 | offsets->old, commit_count, | |
1d498196 MD |
1056 | config->cb.subbuffer_header_size(), |
1057 | handle); | |
852c2936 MD |
1058 | } |
1059 | ||
1060 | /* | |
1061 | * lib_ring_buffer_switch_old_end: switch old subbuffer | |
1062 | * | |
1063 | * Note : offset_old should never be 0 here. It is ok, because we never perform | |
1064 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller | |
1065 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty | |
1066 | * subbuffer. | |
1067 | */ | |
1068 | static | |
4cfec15c | 1069 | void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1070 | struct channel *chan, |
1071 | struct switch_offsets *offsets, | |
2fed87ae | 1072 | uint64_t tsc, |
38fae1d3 | 1073 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1074 | { |
4cfec15c | 1075 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1076 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
1077 | unsigned long commit_count, padding_size, data_size; | |
1078 | ||
1079 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; | |
1080 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1081 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
1082 | handle); | |
852c2936 MD |
1083 | |
1084 | /* | |
1085 | * Order all writes to buffer before the commit count update that will | |
1086 | * determine that the subbuffer is full. | |
1087 | */ | |
a6352fd4 | 1088 | cmm_smp_wmb(); |
4746ae29 MD |
1089 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1090 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 | 1091 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
1d498196 | 1092 | commit_count, oldidx, handle); |
852c2936 MD |
1093 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1094 | offsets->old, commit_count, | |
1d498196 | 1095 | padding_size, handle); |
852c2936 MD |
1096 | } |
1097 | ||
1098 | /* | |
1099 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. | |
1100 | * | |
1101 | * This code can be executed unordered : writers may already have written to the | |
1102 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1103 | * that this code is executed before the deliver of this sub-buffer. | |
1104 | */ | |
1105 | static | |
4cfec15c | 1106 | void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1107 | struct channel *chan, |
1108 | struct switch_offsets *offsets, | |
2fed87ae | 1109 | uint64_t tsc, |
38fae1d3 | 1110 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1111 | { |
4cfec15c | 1112 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1113 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
1114 | unsigned long commit_count; | |
1115 | ||
1d498196 | 1116 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
852c2936 MD |
1117 | |
1118 | /* | |
1119 | * Order all writes to buffer before the commit count update that will | |
1120 | * determine that the subbuffer is full. | |
1121 | */ | |
a6352fd4 | 1122 | cmm_smp_wmb(); |
852c2936 | 1123 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1124 | &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
1125 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc); | |
852c2936 MD |
1126 | /* Check if the written buffer has to be delivered */ |
1127 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, | |
1d498196 | 1128 | commit_count, beginidx, handle); |
852c2936 MD |
1129 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
1130 | offsets->begin, commit_count, | |
1d498196 MD |
1131 | config->cb.subbuffer_header_size(), |
1132 | handle); | |
852c2936 MD |
1133 | } |
1134 | ||
1135 | /* | |
1136 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer | |
1137 | * | |
1138 | * The only remaining threads could be the ones with pending commits. They will | |
1139 | * have to do the deliver themselves. | |
1140 | */ | |
1141 | static | |
4cfec15c | 1142 | void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 MD |
1143 | struct channel *chan, |
1144 | struct switch_offsets *offsets, | |
2fed87ae | 1145 | uint64_t tsc, |
38fae1d3 | 1146 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1147 | { |
4cfec15c | 1148 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1149 | unsigned long endidx = subbuf_index(offsets->end - 1, chan); |
1150 | unsigned long commit_count, padding_size, data_size; | |
1151 | ||
1152 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; | |
1153 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1154 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
1155 | handle); | |
852c2936 MD |
1156 | |
1157 | /* | |
1158 | * Order all writes to buffer before the commit count update that will | |
1159 | * determine that the subbuffer is full. | |
1160 | */ | |
a6352fd4 | 1161 | cmm_smp_wmb(); |
4746ae29 MD |
1162 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
1163 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc); | |
852c2936 | 1164 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1, |
1d498196 | 1165 | commit_count, endidx, handle); |
852c2936 MD |
1166 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, |
1167 | offsets->end, commit_count, | |
1d498196 | 1168 | padding_size, handle); |
852c2936 MD |
1169 | } |
1170 | ||
1171 | /* | |
1172 | * Returns : | |
1173 | * 0 if ok | |
1174 | * !0 if execution must be aborted. | |
1175 | */ | |
1176 | static | |
1177 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, | |
4cfec15c | 1178 | struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1179 | struct channel *chan, |
1180 | struct switch_offsets *offsets, | |
2fed87ae | 1181 | uint64_t *tsc) |
852c2936 | 1182 | { |
4cfec15c | 1183 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1184 | unsigned long off; |
1185 | ||
1186 | offsets->begin = v_read(config, &buf->offset); | |
1187 | offsets->old = offsets->begin; | |
1188 | offsets->switch_old_start = 0; | |
1189 | off = subbuf_offset(offsets->begin, chan); | |
1190 | ||
1191 | *tsc = config->cb.ring_buffer_clock_read(chan); | |
1192 | ||
1193 | /* | |
1194 | * Ensure we flush the header of an empty subbuffer when doing the | |
1195 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the | |
1196 | * total data gathering duration even if there were no records saved | |
1197 | * after the last buffer switch. | |
1198 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. | |
1199 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of | |
1200 | * subbuffer header as appropriate. | |
1201 | * The next record that reserves space will be responsible for | |
1202 | * populating the following subbuffer header. We choose not to populate | |
1203 | * the next subbuffer header here because we want to be able to use | |
a6352fd4 MD |
1204 | * SWITCH_ACTIVE for periodical buffer flush, which must |
1205 | * guarantee that all the buffer content (records and header | |
1206 | * timestamps) are visible to the reader. This is required for | |
1207 | * quiescence guarantees for the fusion merge. | |
852c2936 MD |
1208 | */ |
1209 | if (mode == SWITCH_FLUSH || off > 0) { | |
b5a3dfa5 | 1210 | if (caa_unlikely(off == 0)) { |
852c2936 MD |
1211 | /* |
1212 | * The client does not save any header information. | |
1213 | * Don't switch empty subbuffer on finalize, because it | |
1214 | * is invalid to deliver a completely empty subbuffer. | |
1215 | */ | |
1216 | if (!config->cb.subbuffer_header_size()) | |
1217 | return -1; | |
1218 | /* | |
1219 | * Need to write the subbuffer start header on finalize. | |
1220 | */ | |
1221 | offsets->switch_old_start = 1; | |
1222 | } | |
1223 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1224 | } else | |
1225 | return -1; /* we do not have to switch : buffer is empty */ | |
1226 | /* Note: old points to the next subbuf at offset 0 */ | |
1227 | offsets->end = offsets->begin; | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | /* | |
1232 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1233 | * called while tracing is active with absolutely no lock held. | |
1234 | * | |
1235 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1236 | * operations, this function must be called from the CPU which owns the buffer | |
1237 | * for a ACTIVE flush. | |
1238 | */ | |
4cfec15c | 1239 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
38fae1d3 | 1240 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1241 | { |
1d498196 | 1242 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 1243 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1244 | struct switch_offsets offsets; |
1245 | unsigned long oldidx; | |
2fed87ae | 1246 | uint64_t tsc; |
852c2936 MD |
1247 | |
1248 | offsets.size = 0; | |
1249 | ||
1250 | /* | |
1251 | * Perform retryable operations. | |
1252 | */ | |
1253 | do { | |
1254 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
1255 | &tsc)) | |
1256 | return; /* Switch not needed */ | |
1257 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1258 | != offsets.old); | |
1259 | ||
1260 | /* | |
1261 | * Atomically update last_tsc. This update races against concurrent | |
1262 | * atomic updates, but the race will always cause supplementary full TSC | |
1263 | * records, never the opposite (missing a full TSC record when it would | |
1264 | * be needed). | |
1265 | */ | |
1266 | save_last_tsc(config, buf, tsc); | |
1267 | ||
1268 | /* | |
1269 | * Push the reader if necessary | |
1270 | */ | |
1271 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1272 | ||
1273 | oldidx = subbuf_index(offsets.old, chan); | |
1d498196 | 1274 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
852c2936 MD |
1275 | |
1276 | /* | |
1277 | * May need to populate header start on SWITCH_FLUSH. | |
1278 | */ | |
1279 | if (offsets.switch_old_start) { | |
1d498196 | 1280 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
852c2936 MD |
1281 | offsets.old += config->cb.subbuffer_header_size(); |
1282 | } | |
1283 | ||
1284 | /* | |
1285 | * Switch old subbuffer. | |
1286 | */ | |
1d498196 | 1287 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
852c2936 | 1288 | } |
852c2936 MD |
1289 | |
1290 | /* | |
1291 | * Returns : | |
1292 | * 0 if ok | |
1293 | * -ENOSPC if event size is too large for packet. | |
1294 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1295 | * -EIO if data cannot be written into the buffer for any other reason. | |
1296 | */ | |
1297 | static | |
4cfec15c | 1298 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1299 | struct channel *chan, |
1300 | struct switch_offsets *offsets, | |
4cfec15c | 1301 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 | 1302 | { |
4cfec15c | 1303 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
38fae1d3 | 1304 | struct lttng_ust_shm_handle *handle = ctx->handle; |
852c2936 MD |
1305 | unsigned long reserve_commit_diff; |
1306 | ||
1307 | offsets->begin = v_read(config, &buf->offset); | |
1308 | offsets->old = offsets->begin; | |
1309 | offsets->switch_new_start = 0; | |
1310 | offsets->switch_new_end = 0; | |
1311 | offsets->switch_old_end = 0; | |
1312 | offsets->pre_header_padding = 0; | |
1313 | ||
1314 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
1315 | if ((int64_t) ctx->tsc == -EIO) | |
1316 | return -EIO; | |
1317 | ||
1318 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
1319 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
1320 | ||
b5a3dfa5 | 1321 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
852c2936 MD |
1322 | offsets->switch_new_start = 1; /* For offsets->begin */ |
1323 | } else { | |
1324 | offsets->size = config->cb.record_header_size(config, chan, | |
1325 | offsets->begin, | |
1326 | &offsets->pre_header_padding, | |
1327 | ctx); | |
1328 | offsets->size += | |
1329 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1330 | ctx->largest_align) | |
1331 | + ctx->data_size; | |
b5a3dfa5 | 1332 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
852c2936 MD |
1333 | offsets->size > chan->backend.subbuf_size)) { |
1334 | offsets->switch_old_end = 1; /* For offsets->old */ | |
1335 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1336 | } | |
1337 | } | |
b5a3dfa5 | 1338 | if (caa_unlikely(offsets->switch_new_start)) { |
852c2936 MD |
1339 | unsigned long sb_index; |
1340 | ||
1341 | /* | |
1342 | * We are typically not filling the previous buffer completely. | |
1343 | */ | |
b5a3dfa5 | 1344 | if (caa_likely(offsets->switch_old_end)) |
852c2936 MD |
1345 | offsets->begin = subbuf_align(offsets->begin, chan); |
1346 | offsets->begin = offsets->begin | |
1347 | + config->cb.subbuffer_header_size(); | |
1348 | /* Test new buffer integrity */ | |
1349 | sb_index = subbuf_index(offsets->begin, chan); | |
1350 | reserve_commit_diff = | |
1351 | (buf_trunc(offsets->begin, chan) | |
1352 | >> chan->backend.num_subbuf_order) | |
1353 | - ((unsigned long) v_read(config, | |
4746ae29 | 1354 | &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb) |
852c2936 | 1355 | & chan->commit_count_mask); |
b5a3dfa5 | 1356 | if (caa_likely(reserve_commit_diff == 0)) { |
852c2936 | 1357 | /* Next subbuffer not being written to. */ |
b5a3dfa5 | 1358 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
852c2936 MD |
1359 | subbuf_trunc(offsets->begin, chan) |
1360 | - subbuf_trunc((unsigned long) | |
a6352fd4 | 1361 | uatomic_read(&buf->consumed), chan) |
852c2936 | 1362 | >= chan->backend.buf_size)) { |
64493e4f MD |
1363 | unsigned long nr_lost; |
1364 | ||
852c2936 MD |
1365 | /* |
1366 | * We do not overwrite non consumed buffers | |
1367 | * and we are full : record is lost. | |
1368 | */ | |
64493e4f | 1369 | nr_lost = v_read(config, &buf->records_lost_full); |
852c2936 | 1370 | v_inc(config, &buf->records_lost_full); |
64493e4f MD |
1371 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1372 | DBG("%lu or more records lost in (%s:%d) (buffer full)\n", | |
1373 | nr_lost + 1, chan->backend.name, | |
1374 | buf->backend.cpu); | |
1375 | } | |
852c2936 MD |
1376 | return -ENOBUFS; |
1377 | } else { | |
1378 | /* | |
1379 | * Next subbuffer not being written to, and we | |
1380 | * are either in overwrite mode or the buffer is | |
1381 | * not full. It's safe to write in this new | |
1382 | * subbuffer. | |
1383 | */ | |
1384 | } | |
1385 | } else { | |
64493e4f MD |
1386 | unsigned long nr_lost; |
1387 | ||
852c2936 MD |
1388 | /* |
1389 | * Next subbuffer reserve offset does not match the | |
1390 | * commit offset. Drop record in producer-consumer and | |
1391 | * overwrite mode. Caused by either a writer OOPS or too | |
1392 | * many nested writes over a reserve/commit pair. | |
1393 | */ | |
64493e4f | 1394 | nr_lost = v_read(config, &buf->records_lost_wrap); |
852c2936 | 1395 | v_inc(config, &buf->records_lost_wrap); |
64493e4f MD |
1396 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1397 | DBG("%lu or more records lost in (%s:%d) (wrap-around)\n", | |
1398 | nr_lost + 1, chan->backend.name, | |
1399 | buf->backend.cpu); | |
1400 | } | |
852c2936 MD |
1401 | return -EIO; |
1402 | } | |
1403 | offsets->size = | |
1404 | config->cb.record_header_size(config, chan, | |
1405 | offsets->begin, | |
1406 | &offsets->pre_header_padding, | |
1407 | ctx); | |
1408 | offsets->size += | |
1409 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1410 | ctx->largest_align) | |
1411 | + ctx->data_size; | |
b5a3dfa5 | 1412 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
852c2936 | 1413 | + offsets->size > chan->backend.subbuf_size)) { |
64493e4f MD |
1414 | unsigned long nr_lost; |
1415 | ||
852c2936 MD |
1416 | /* |
1417 | * Record too big for subbuffers, report error, don't | |
1418 | * complete the sub-buffer switch. | |
1419 | */ | |
64493e4f | 1420 | nr_lost = v_read(config, &buf->records_lost_big); |
852c2936 | 1421 | v_inc(config, &buf->records_lost_big); |
64493e4f MD |
1422 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1423 | DBG("%lu or more records lost in (%s:%d) record size " | |
1424 | " of %zu bytes is too large for buffer\n", | |
1425 | nr_lost + 1, chan->backend.name, | |
1426 | buf->backend.cpu, offsets->size); | |
1427 | } | |
852c2936 MD |
1428 | return -ENOSPC; |
1429 | } else { | |
1430 | /* | |
1431 | * We just made a successful buffer switch and the | |
1432 | * record fits in the new subbuffer. Let's write. | |
1433 | */ | |
1434 | } | |
1435 | } else { | |
1436 | /* | |
1437 | * Record fits in the current buffer and we are not on a switch | |
1438 | * boundary. It's safe to write. | |
1439 | */ | |
1440 | } | |
1441 | offsets->end = offsets->begin + offsets->size; | |
1442 | ||
b5a3dfa5 | 1443 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
852c2936 MD |
1444 | /* |
1445 | * The offset_end will fall at the very beginning of the next | |
1446 | * subbuffer. | |
1447 | */ | |
1448 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
1449 | } | |
1450 | return 0; | |
1451 | } | |
1452 | ||
1453 | /** | |
1454 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
1455 | * @ctx: ring buffer context. | |
1456 | * | |
1457 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, | |
1458 | * -EIO for other errors, else returns 0. | |
1459 | * It will take care of sub-buffer switching. | |
1460 | */ | |
4cfec15c | 1461 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 MD |
1462 | { |
1463 | struct channel *chan = ctx->chan; | |
38fae1d3 | 1464 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c MD |
1465 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
1466 | struct lttng_ust_lib_ring_buffer *buf; | |
852c2936 MD |
1467 | struct switch_offsets offsets; |
1468 | int ret; | |
1469 | ||
1470 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 1471 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 1472 | else |
1d498196 | 1473 | buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 MD |
1474 | ctx->buf = buf; |
1475 | ||
1476 | offsets.size = 0; | |
1477 | ||
1478 | do { | |
1479 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, | |
1480 | ctx); | |
b5a3dfa5 | 1481 | if (caa_unlikely(ret)) |
852c2936 | 1482 | return ret; |
b5a3dfa5 | 1483 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
852c2936 MD |
1484 | offsets.end) |
1485 | != offsets.old)); | |
1486 | ||
1487 | /* | |
1488 | * Atomically update last_tsc. This update races against concurrent | |
1489 | * atomic updates, but the race will always cause supplementary full TSC | |
1490 | * records, never the opposite (missing a full TSC record when it would | |
1491 | * be needed). | |
1492 | */ | |
1493 | save_last_tsc(config, buf, ctx->tsc); | |
1494 | ||
1495 | /* | |
1496 | * Push the reader if necessary | |
1497 | */ | |
1498 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
1499 | ||
1500 | /* | |
1501 | * Clear noref flag for this subbuffer. | |
1502 | */ | |
1503 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
1504 | subbuf_index(offsets.end - 1, chan), |
1505 | handle); | |
852c2936 MD |
1506 | |
1507 | /* | |
1508 | * Switch old subbuffer if needed. | |
1509 | */ | |
b5a3dfa5 | 1510 | if (caa_unlikely(offsets.switch_old_end)) { |
852c2936 | 1511 | lib_ring_buffer_clear_noref(config, &buf->backend, |
1d498196 MD |
1512 | subbuf_index(offsets.old - 1, chan), |
1513 | handle); | |
1514 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); | |
852c2936 MD |
1515 | } |
1516 | ||
1517 | /* | |
1518 | * Populate new subbuffer. | |
1519 | */ | |
b5a3dfa5 | 1520 | if (caa_unlikely(offsets.switch_new_start)) |
1d498196 | 1521 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 | 1522 | |
b5a3dfa5 | 1523 | if (caa_unlikely(offsets.switch_new_end)) |
1d498196 | 1524 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 MD |
1525 | |
1526 | ctx->slot_size = offsets.size; | |
1527 | ctx->pre_offset = offsets.begin; | |
1528 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
1529 | return 0; | |
1530 | } | |
f645cfa7 MD |
1531 | |
1532 | /* | |
1533 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
1534 | */ | |
1535 | void lttng_fixup_ringbuffer_tls(void) | |
1536 | { | |
1537 | asm volatile ("" : : "m" (lib_ring_buffer_nesting)); | |
1538 | } |