Move to kernel style SPDX license identifiers
[lttng-ust.git] / libringbuffer / ring_buffer_frontend.c
CommitLineData
852c2936 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
852c2936 3 *
e92f3e28
MD
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
852c2936
MD
6 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
7 * recorder (overwrite) modes. See thesis:
8 *
9 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
10 * dissertation, Ecole Polytechnique de Montreal.
11 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
12 *
13 * - Algorithm presentation in Chapter 5:
14 * "Lockless Multi-Core High-Throughput Buffering".
15 * - Algorithm formal verification in Section 8.6:
16 * "Formal verification of LTTng"
17 *
18 * Author:
19 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
20 *
21 * Inspired from LTT and RelayFS:
22 * Karim Yaghmour <karim@opersys.com>
23 * Tom Zanussi <zanussi@us.ibm.com>
24 * Bob Wisniewski <bob@watson.ibm.com>
25 * And from K42 :
26 * Bob Wisniewski <bob@watson.ibm.com>
27 *
28 * Buffer reader semantic :
29 *
30 * - get_subbuf_size
31 * while buffer is not finalized and empty
32 * - get_subbuf
33 * - if return value != 0, continue
34 * - splice one subbuffer worth of data to a pipe
35 * - splice the data from pipe to disk/network
36 * - put_subbuf
852c2936
MD
37 */
38
3fbec7dc 39#define _LGPL_SOURCE
a6352fd4 40#include <sys/types.h>
431d5cf0
MD
41#include <sys/mman.h>
42#include <sys/stat.h>
03d2d293 43#include <unistd.h>
431d5cf0 44#include <fcntl.h>
03d2d293
MD
45#include <signal.h>
46#include <time.h>
932b85fa 47#include <stdbool.h>
fb31eb73 48#include <stdint.h>
14641deb 49#include <urcu/compiler.h>
a6352fd4 50#include <urcu/ref.h>
8c90a710 51#include <urcu/tls-compat.h>
327d9447 52#include <poll.h>
35897f8b 53#include <helper.h>
14641deb 54
a6352fd4 55#include "smp.h"
4318ae1b 56#include <lttng/ringbuffer-config.h>
2fed87ae 57#include "vatomic.h"
4931a13e
MD
58#include "backend.h"
59#include "frontend.h"
a6352fd4 60#include "shm.h"
6f97f9c2 61#include "rb-init.h"
bdcf8d82 62#include "../liblttng-ust/compat.h" /* For ENODATA */
852c2936 63
64493e4f
MD
64/* Print DBG() messages about events lost only every 1048576 hits */
65#define DBG_PRINT_NR_LOST (1UL << 20)
66
34a91bdb
MD
67#define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
68#define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
69#define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
03d2d293 70#define CLOCKID CLOCK_MONOTONIC
dddba398
MD
71#define LTTNG_UST_RING_BUFFER_GET_RETRY 10
72#define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
6f97f9c2 73#define RETRY_DELAY_MS 100 /* 100 ms. */
03d2d293 74
a9ff648c
MD
75/*
76 * Non-static to ensure the compiler does not optimize away the xor.
77 */
78uint8_t lttng_crash_magic_xor[] = RB_CRASH_DUMP_ABI_MAGIC_XOR;
79
2432c3c9
MD
80/*
81 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
82 * close(2) to close the fd returned by shm_open.
83 * shm_unlink releases the shared memory object name.
84 * ftruncate(2) sets the size of the memory object.
85 * mmap/munmap maps the shared memory obj to a virtual address in the
86 * calling proceess (should be done both in libust and consumer).
87 * See shm_overview(7) for details.
88 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
89 * a UNIX socket.
90 *
91 * Since we don't need to access the object using its name, we can
92 * immediately shm_unlink(3) it, and only keep the handle with its file
93 * descriptor.
94 */
95
852c2936
MD
96/*
97 * Internal structure representing offsets to use at a sub-buffer switch.
98 */
99struct switch_offsets {
100 unsigned long begin, end, old;
101 size_t pre_header_padding, size;
1ad21f70
MD
102 unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1,
103 switch_old_end:1;
852c2936
MD
104};
105
16adecf1 106DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting);
852c2936 107
cb7378b3
MD
108/*
109 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
110 * close.
111 */
112static pthread_mutex_t wakeup_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
113
852c2936
MD
114static
115void lib_ring_buffer_print_errors(struct channel *chan,
009769ca
MD
116 struct lttng_ust_lib_ring_buffer *buf, int cpu,
117 struct lttng_ust_shm_handle *handle);
852c2936 118
03d2d293
MD
119/*
120 * Handle timer teardown race wrt memory free of private data by
121 * ring buffer signals are handled by a single thread, which permits
122 * a synchronization point between handling of each signal.
64bf51a6 123 * Protected by the lock within the structure.
03d2d293
MD
124 */
125struct timer_signal_data {
126 pthread_t tid; /* thread id managing signals */
127 int setup_done;
128 int qs_done;
64bf51a6 129 pthread_mutex_t lock;
03d2d293
MD
130};
131
64bf51a6
MD
132static struct timer_signal_data timer_signal = {
133 .tid = 0,
134 .setup_done = 0,
135 .qs_done = 0,
136 .lock = PTHREAD_MUTEX_INITIALIZER,
137};
03d2d293 138
b2c5f61a 139static bool lttng_ust_allow_blocking;
6f97f9c2 140
b2c5f61a 141void lttng_ust_ringbuffer_set_allow_blocking(void)
6f97f9c2 142{
b2c5f61a
MD
143 lttng_ust_allow_blocking = true;
144}
145
146/* Get blocking timeout, in ms */
147static int lttng_ust_ringbuffer_get_timeout(struct channel *chan)
148{
149 if (!lttng_ust_allow_blocking)
150 return 0;
151 return chan->u.s.blocking_timeout_ms;
6f97f9c2
MD
152}
153
852c2936
MD
154/**
155 * lib_ring_buffer_reset - Reset ring buffer to initial values.
156 * @buf: Ring buffer.
157 *
158 * Effectively empty the ring buffer. Should be called when the buffer is not
159 * used for writing. The ring buffer can be opened for reading, but the reader
160 * should not be using the iterator concurrently with reset. The previous
161 * current iterator record is reset.
162 */
4cfec15c 163void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 164 struct lttng_ust_shm_handle *handle)
852c2936 165{
34daae3e
MD
166 struct channel *chan;
167 const struct lttng_ust_lib_ring_buffer_config *config;
852c2936
MD
168 unsigned int i;
169
34daae3e
MD
170 chan = shmp(handle, buf->backend.chan);
171 if (!chan)
15500a1b 172 return;
34daae3e 173 config = &chan->backend.config;
852c2936
MD
174 /*
175 * Reset iterator first. It will put the subbuffer if it currently holds
176 * it.
177 */
852c2936
MD
178 v_set(config, &buf->offset, 0);
179 for (i = 0; i < chan->backend.num_subbuf; i++) {
15500a1b
MD
180 struct commit_counters_hot *cc_hot;
181 struct commit_counters_cold *cc_cold;
6c737d05 182 uint64_t *ts_end;
15500a1b
MD
183
184 cc_hot = shmp_index(handle, buf->commit_hot, i);
185 if (!cc_hot)
186 return;
187 cc_cold = shmp_index(handle, buf->commit_cold, i);
188 if (!cc_cold)
189 return;
6c737d05
MD
190 ts_end = shmp_index(handle, buf->ts_end, i);
191 if (!ts_end)
192 return;
15500a1b
MD
193 v_set(config, &cc_hot->cc, 0);
194 v_set(config, &cc_hot->seq, 0);
195 v_set(config, &cc_cold->cc_sb, 0);
6c737d05 196 *ts_end = 0;
852c2936 197 }
a6352fd4
MD
198 uatomic_set(&buf->consumed, 0);
199 uatomic_set(&buf->record_disabled, 0);
852c2936 200 v_set(config, &buf->last_tsc, 0);
1d498196 201 lib_ring_buffer_backend_reset(&buf->backend, handle);
852c2936
MD
202 /* Don't reset number of active readers */
203 v_set(config, &buf->records_lost_full, 0);
204 v_set(config, &buf->records_lost_wrap, 0);
205 v_set(config, &buf->records_lost_big, 0);
206 v_set(config, &buf->records_count, 0);
207 v_set(config, &buf->records_overrun, 0);
208 buf->finalized = 0;
209}
852c2936
MD
210
211/**
212 * channel_reset - Reset channel to initial values.
213 * @chan: Channel.
214 *
215 * Effectively empty the channel. Should be called when the channel is not used
216 * for writing. The channel can be opened for reading, but the reader should not
217 * be using the iterator concurrently with reset. The previous current iterator
218 * record is reset.
219 */
220void channel_reset(struct channel *chan)
221{
222 /*
223 * Reset iterators first. Will put the subbuffer if held for reading.
224 */
a6352fd4 225 uatomic_set(&chan->record_disabled, 0);
852c2936
MD
226 /* Don't reset commit_count_mask, still valid */
227 channel_backend_reset(&chan->backend);
228 /* Don't reset switch/read timer interval */
229 /* Don't reset notifiers and notifier enable bits */
230 /* Don't reset reader reference count */
231}
852c2936 232
a9ff648c
MD
233static
234void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config *config,
235 struct lttng_crash_abi *crash_abi,
236 struct lttng_ust_lib_ring_buffer *buf,
237 struct channel_backend *chanb,
238 struct shm_object *shmobj,
239 struct lttng_ust_shm_handle *handle)
240{
241 int i;
242
243 for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++)
244 crash_abi->magic[i] = lttng_crash_magic_xor[i] ^ 0xFF;
245 crash_abi->mmap_length = shmobj->memory_map_size;
246 crash_abi->endian = RB_CRASH_ENDIAN;
247 crash_abi->major = RB_CRASH_DUMP_ABI_MAJOR;
248 crash_abi->minor = RB_CRASH_DUMP_ABI_MINOR;
249 crash_abi->word_size = sizeof(unsigned long);
250 crash_abi->layout_type = LTTNG_CRASH_TYPE_UST;
251
252 /* Offset of fields */
253 crash_abi->offset.prod_offset =
254 (uint32_t) ((char *) &buf->offset - (char *) buf);
255 crash_abi->offset.consumed_offset =
256 (uint32_t) ((char *) &buf->consumed - (char *) buf);
257 crash_abi->offset.commit_hot_array =
258 (uint32_t) ((char *) shmp(handle, buf->commit_hot) - (char *) buf);
259 crash_abi->offset.commit_hot_seq =
260 offsetof(struct commit_counters_hot, seq);
261 crash_abi->offset.buf_wsb_array =
262 (uint32_t) ((char *) shmp(handle, buf->backend.buf_wsb) - (char *) buf);
263 crash_abi->offset.buf_wsb_id =
264 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer, id);
265 crash_abi->offset.sb_array =
266 (uint32_t) ((char *) shmp(handle, buf->backend.array) - (char *) buf);
267 crash_abi->offset.sb_array_shmp_offset =
268 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp,
269 shmp._ref.offset);
270 crash_abi->offset.sb_backend_p_offset =
271 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages,
272 p._ref.offset);
273
274 /* Field length */
275 crash_abi->length.prod_offset = sizeof(buf->offset);
276 crash_abi->length.consumed_offset = sizeof(buf->consumed);
277 crash_abi->length.commit_hot_seq =
278 sizeof(((struct commit_counters_hot *) NULL)->seq);
279 crash_abi->length.buf_wsb_id =
280 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer *) NULL)->id);
281 crash_abi->length.sb_array_shmp_offset =
282 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp *) NULL)->shmp._ref.offset);
283 crash_abi->length.sb_backend_p_offset =
284 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages *) NULL)->p._ref.offset);
285
286 /* Array stride */
287 crash_abi->stride.commit_hot_array =
288 sizeof(struct commit_counters_hot);
289 crash_abi->stride.buf_wsb_array =
290 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer);
291 crash_abi->stride.sb_array =
292 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp);
293
294 /* Buffer constants */
295 crash_abi->buf_size = chanb->buf_size;
296 crash_abi->subbuf_size = chanb->subbuf_size;
297 crash_abi->num_subbuf = chanb->num_subbuf;
298 crash_abi->mode = (uint32_t) chanb->config.mode;
299
300 if (config->cb.content_size_field) {
301 size_t offset, length;
302
303 config->cb.content_size_field(config, &offset, &length);
304 crash_abi->offset.content_size = offset;
305 crash_abi->length.content_size = length;
306 } else {
307 crash_abi->offset.content_size = 0;
308 crash_abi->length.content_size = 0;
309 }
310 if (config->cb.packet_size_field) {
311 size_t offset, length;
312
313 config->cb.packet_size_field(config, &offset, &length);
314 crash_abi->offset.packet_size = offset;
315 crash_abi->length.packet_size = length;
316 } else {
317 crash_abi->offset.packet_size = 0;
318 crash_abi->length.packet_size = 0;
319 }
320}
321
852c2936
MD
322/*
323 * Must be called under cpu hotplug protection.
324 */
4cfec15c 325int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf,
a6352fd4 326 struct channel_backend *chanb, int cpu,
38fae1d3 327 struct lttng_ust_shm_handle *handle,
1d498196 328 struct shm_object *shmobj)
852c2936 329{
4cfec15c 330 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
14641deb 331 struct channel *chan = caa_container_of(chanb, struct channel, backend);
15500a1b
MD
332 struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb;
333 struct channel *shmp_chan;
334 struct commit_counters_hot *cc_hot;
a3f61e7f 335 void *priv = channel_get_private(chan);
852c2936 336 size_t subbuf_header_size;
2fed87ae 337 uint64_t tsc;
852c2936
MD
338 int ret;
339
340 /* Test for cpu hotplug */
341 if (buf->backend.allocated)
342 return 0;
343
1d498196
MD
344 align_shm(shmobj, __alignof__(struct commit_counters_hot));
345 set_shmp(buf->commit_hot,
346 zalloc_shm(shmobj,
347 sizeof(struct commit_counters_hot) * chan->backend.num_subbuf));
348 if (!shmp(handle, buf->commit_hot)) {
a9ff648c 349 return -ENOMEM;
852c2936
MD
350 }
351
1d498196
MD
352 align_shm(shmobj, __alignof__(struct commit_counters_cold));
353 set_shmp(buf->commit_cold,
354 zalloc_shm(shmobj,
355 sizeof(struct commit_counters_cold) * chan->backend.num_subbuf));
356 if (!shmp(handle, buf->commit_cold)) {
852c2936
MD
357 ret = -ENOMEM;
358 goto free_commit;
359 }
360
6c737d05
MD
361 align_shm(shmobj, __alignof__(uint64_t));
362 set_shmp(buf->ts_end,
363 zalloc_shm(shmobj,
364 sizeof(uint64_t) * chan->backend.num_subbuf));
365 if (!shmp(handle, buf->ts_end)) {
366 ret = -ENOMEM;
367 goto free_commit_cold;
368 }
369
370
a9ff648c
MD
371 ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend,
372 cpu, handle, shmobj);
373 if (ret) {
374 goto free_init;
375 }
376
852c2936
MD
377 /*
378 * Write the subbuffer header for first subbuffer so we know the total
379 * duration of data gathering.
380 */
381 subbuf_header_size = config->cb.subbuffer_header_size();
382 v_set(config, &buf->offset, subbuf_header_size);
15500a1b
MD
383 wsb = shmp_index(handle, buf->backend.buf_wsb, 0);
384 if (!wsb) {
385 ret = -EPERM;
386 goto free_chanbuf;
387 }
388 subbuffer_id_clear_noref(config, &wsb->id);
389 shmp_chan = shmp(handle, buf->backend.chan);
390 if (!shmp_chan) {
391 ret = -EPERM;
392 goto free_chanbuf;
393 }
394 tsc = config->cb.ring_buffer_clock_read(shmp_chan);
1d498196 395 config->cb.buffer_begin(buf, tsc, 0, handle);
15500a1b
MD
396 cc_hot = shmp_index(handle, buf->commit_hot, 0);
397 if (!cc_hot) {
398 ret = -EPERM;
399 goto free_chanbuf;
400 }
401 v_add(config, subbuf_header_size, &cc_hot->cc);
402 v_add(config, subbuf_header_size, &cc_hot->seq);
852c2936
MD
403
404 if (config->cb.buffer_create) {
1d498196 405 ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle);
852c2936 406 if (ret)
a9ff648c 407 goto free_chanbuf;
852c2936 408 }
a9ff648c
MD
409
410 init_crash_abi(config, &buf->crash_abi, buf, chanb, shmobj, handle);
411
852c2936 412 buf->backend.allocated = 1;
852c2936
MD
413 return 0;
414
415 /* Error handling */
416free_init:
6c737d05
MD
417 /* ts_end will be freed by shm teardown */
418free_commit_cold:
a6352fd4 419 /* commit_cold will be freed by shm teardown */
852c2936 420free_commit:
a6352fd4 421 /* commit_hot will be freed by shm teardown */
852c2936 422free_chanbuf:
852c2936
MD
423 return ret;
424}
425
03d2d293
MD
426static
427void lib_ring_buffer_channel_switch_timer(int sig, siginfo_t *si, void *uc)
852c2936 428{
03d2d293
MD
429 const struct lttng_ust_lib_ring_buffer_config *config;
430 struct lttng_ust_shm_handle *handle;
431 struct channel *chan;
432 int cpu;
433
434 assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self());
435
436 chan = si->si_value.sival_ptr;
437 handle = chan->handle;
438 config = &chan->backend.config;
852c2936 439
34a91bdb 440 DBG("Switch timer for channel %p\n", chan);
03d2d293 441
4dcd7e80
MD
442 /*
443 * Only flush buffers periodically if readers are active.
444 */
cb7378b3 445 pthread_mutex_lock(&wakeup_fd_mutex);
03d2d293
MD
446 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
447 for_each_possible_cpu(cpu) {
448 struct lttng_ust_lib_ring_buffer *buf =
449 shmp(handle, chan->backend.buf[cpu].shmp);
34daae3e
MD
450
451 if (!buf)
15500a1b 452 goto end;
4dcd7e80
MD
453 if (uatomic_read(&buf->active_readers))
454 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
455 chan->handle);
03d2d293
MD
456 }
457 } else {
458 struct lttng_ust_lib_ring_buffer *buf =
459 shmp(handle, chan->backend.buf[0].shmp);
460
34daae3e 461 if (!buf)
15500a1b 462 goto end;
34a91bdb
MD
463 if (uatomic_read(&buf->active_readers))
464 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
465 chan->handle);
466 }
15500a1b 467end:
34a91bdb
MD
468 pthread_mutex_unlock(&wakeup_fd_mutex);
469 return;
470}
471
b07cd987
MD
472static
473int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config *config,
474 struct lttng_ust_lib_ring_buffer *buf,
475 struct channel *chan,
476 struct lttng_ust_shm_handle *handle)
477{
478 unsigned long consumed_old, consumed_idx, commit_count, write_offset;
15500a1b 479 struct commit_counters_cold *cc_cold;
b07cd987
MD
480
481 consumed_old = uatomic_read(&buf->consumed);
482 consumed_idx = subbuf_index(consumed_old, chan);
15500a1b
MD
483 cc_cold = shmp_index(handle, buf->commit_cold, consumed_idx);
484 if (!cc_cold)
485 return 0;
486 commit_count = v_read(config, &cc_cold->cc_sb);
b07cd987
MD
487 /*
488 * No memory barrier here, since we are only interested
489 * in a statistically correct polling result. The next poll will
490 * get the data is we are racing. The mb() that ensures correct
491 * memory order is in get_subbuf.
492 */
493 write_offset = v_read(config, &buf->offset);
494
495 /*
496 * Check that the subbuffer we are trying to consume has been
497 * already fully committed.
498 */
499
500 if (((commit_count - chan->backend.subbuf_size)
501 & chan->commit_count_mask)
502 - (buf_trunc(consumed_old, chan)
503 >> chan->backend.num_subbuf_order)
504 != 0)
505 return 0;
506
507 /*
508 * Check that we are not about to read the same subbuffer in
509 * which the writer head is.
510 */
511 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_old, chan)
512 == 0)
513 return 0;
514
515 return 1;
516}
517
518static
519void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer *buf,
520 struct lttng_ust_shm_handle *handle)
521{
522 int wakeup_fd = shm_get_wakeup_fd(handle, &buf->self._ref);
523 sigset_t sigpipe_set, pending_set, old_set;
524 int ret, sigpipe_was_pending = 0;
525
526 if (wakeup_fd < 0)
527 return;
528
529 /*
530 * Wake-up the other end by writing a null byte in the pipe
531 * (non-blocking). Important note: Because writing into the
532 * pipe is non-blocking (and therefore we allow dropping wakeup
533 * data, as long as there is wakeup data present in the pipe
534 * buffer to wake up the consumer), the consumer should perform
535 * the following sequence for waiting:
536 * 1) empty the pipe (reads).
537 * 2) check if there is data in the buffer.
538 * 3) wait on the pipe (poll).
539 *
540 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
541 * that might be already pending. If a bogus SIGPIPE is sent to
542 * the entire process concurrently by a malicious user, it may
543 * be simply discarded.
544 */
545 ret = sigemptyset(&pending_set);
546 assert(!ret);
547 /*
548 * sigpending returns the mask of signals that are _both_
549 * blocked for the thread _and_ pending for either the thread or
550 * the entire process.
551 */
552 ret = sigpending(&pending_set);
553 assert(!ret);
554 sigpipe_was_pending = sigismember(&pending_set, SIGPIPE);
555 /*
556 * If sigpipe was pending, it means it was already blocked, so
557 * no need to block it.
558 */
559 if (!sigpipe_was_pending) {
560 ret = sigemptyset(&sigpipe_set);
561 assert(!ret);
562 ret = sigaddset(&sigpipe_set, SIGPIPE);
563 assert(!ret);
564 ret = pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set);
565 assert(!ret);
566 }
567 do {
568 ret = write(wakeup_fd, "", 1);
569 } while (ret == -1L && errno == EINTR);
570 if (ret == -1L && errno == EPIPE && !sigpipe_was_pending) {
571 struct timespec timeout = { 0, 0 };
572 do {
573 ret = sigtimedwait(&sigpipe_set, NULL,
574 &timeout);
575 } while (ret == -1L && errno == EINTR);
576 }
577 if (!sigpipe_was_pending) {
578 ret = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
579 assert(!ret);
580 }
581}
582
34a91bdb
MD
583static
584void lib_ring_buffer_channel_do_read(struct channel *chan)
585{
586 const struct lttng_ust_lib_ring_buffer_config *config;
587 struct lttng_ust_shm_handle *handle;
588 int cpu;
589
590 handle = chan->handle;
591 config = &chan->backend.config;
592
593 /*
594 * Only flush buffers periodically if readers are active.
595 */
596 pthread_mutex_lock(&wakeup_fd_mutex);
597 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
598 for_each_possible_cpu(cpu) {
599 struct lttng_ust_lib_ring_buffer *buf =
600 shmp(handle, chan->backend.buf[cpu].shmp);
601
34daae3e 602 if (!buf)
15500a1b 603 goto end;
34a91bdb
MD
604 if (uatomic_read(&buf->active_readers)
605 && lib_ring_buffer_poll_deliver(config, buf,
606 chan, handle)) {
607 lib_ring_buffer_wakeup(buf, handle);
608 }
609 }
610 } else {
611 struct lttng_ust_lib_ring_buffer *buf =
612 shmp(handle, chan->backend.buf[0].shmp);
613
34daae3e 614 if (!buf)
15500a1b 615 goto end;
34a91bdb
MD
616 if (uatomic_read(&buf->active_readers)
617 && lib_ring_buffer_poll_deliver(config, buf,
618 chan, handle)) {
619 lib_ring_buffer_wakeup(buf, handle);
620 }
03d2d293 621 }
15500a1b 622end:
cb7378b3 623 pthread_mutex_unlock(&wakeup_fd_mutex);
34a91bdb
MD
624}
625
626static
627void lib_ring_buffer_channel_read_timer(int sig, siginfo_t *si, void *uc)
628{
629 struct channel *chan;
630
631 assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self());
632 chan = si->si_value.sival_ptr;
633 DBG("Read timer for channel %p\n", chan);
634 lib_ring_buffer_channel_do_read(chan);
03d2d293
MD
635 return;
636}
637
638static
639void rb_setmask(sigset_t *mask)
640{
641 int ret;
642
643 ret = sigemptyset(mask);
644 if (ret) {
645 PERROR("sigemptyset");
646 }
34a91bdb
MD
647 ret = sigaddset(mask, LTTNG_UST_RB_SIG_FLUSH);
648 if (ret) {
649 PERROR("sigaddset");
650 }
651 ret = sigaddset(mask, LTTNG_UST_RB_SIG_READ);
03d2d293
MD
652 if (ret) {
653 PERROR("sigaddset");
654 }
655 ret = sigaddset(mask, LTTNG_UST_RB_SIG_TEARDOWN);
656 if (ret) {
657 PERROR("sigaddset");
658 }
659}
660
661static
662void *sig_thread(void *arg)
663{
664 sigset_t mask;
665 siginfo_t info;
666 int signr;
667
668 /* Only self thread will receive signal mask. */
669 rb_setmask(&mask);
670 CMM_STORE_SHARED(timer_signal.tid, pthread_self());
671
672 for (;;) {
673 signr = sigwaitinfo(&mask, &info);
674 if (signr == -1) {
34a91bdb
MD
675 if (errno != EINTR)
676 PERROR("sigwaitinfo");
03d2d293
MD
677 continue;
678 }
34a91bdb 679 if (signr == LTTNG_UST_RB_SIG_FLUSH) {
03d2d293
MD
680 lib_ring_buffer_channel_switch_timer(info.si_signo,
681 &info, NULL);
34a91bdb
MD
682 } else if (signr == LTTNG_UST_RB_SIG_READ) {
683 lib_ring_buffer_channel_read_timer(info.si_signo,
684 &info, NULL);
03d2d293
MD
685 } else if (signr == LTTNG_UST_RB_SIG_TEARDOWN) {
686 cmm_smp_mb();
687 CMM_STORE_SHARED(timer_signal.qs_done, 1);
688 cmm_smp_mb();
689 } else {
690 ERR("Unexptected signal %d\n", info.si_signo);
691 }
692 }
693 return NULL;
694}
695
696/*
03d2d293
MD
697 * Ensure only a single thread listens on the timer signal.
698 */
699static
700void lib_ring_buffer_setup_timer_thread(void)
701{
702 pthread_t thread;
703 int ret;
704
64bf51a6 705 pthread_mutex_lock(&timer_signal.lock);
03d2d293 706 if (timer_signal.setup_done)
64bf51a6 707 goto end;
03d2d293
MD
708
709 ret = pthread_create(&thread, NULL, &sig_thread, NULL);
710 if (ret) {
711 errno = ret;
712 PERROR("pthread_create");
713 }
714 ret = pthread_detach(thread);
715 if (ret) {
716 errno = ret;
717 PERROR("pthread_detach");
718 }
719 timer_signal.setup_done = 1;
64bf51a6
MD
720end:
721 pthread_mutex_unlock(&timer_signal.lock);
852c2936
MD
722}
723
03d2d293 724/*
64bf51a6 725 * Wait for signal-handling thread quiescent state.
03d2d293 726 */
64bf51a6
MD
727static
728void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr)
729{
730 sigset_t pending_set;
731 int ret;
732
733 /*
734 * We need to be the only thread interacting with the thread
735 * that manages signals for teardown synchronization.
736 */
737 pthread_mutex_lock(&timer_signal.lock);
738
739 /*
740 * Ensure we don't have any signal queued for this channel.
741 */
742 for (;;) {
743 ret = sigemptyset(&pending_set);
744 if (ret == -1) {
745 PERROR("sigemptyset");
746 }
747 ret = sigpending(&pending_set);
748 if (ret == -1) {
749 PERROR("sigpending");
750 }
751 if (!sigismember(&pending_set, signr))
752 break;
753 caa_cpu_relax();
754 }
755
756 /*
757 * From this point, no new signal handler will be fired that
758 * would try to access "chan". However, we still need to wait
759 * for any currently executing handler to complete.
760 */
761 cmm_smp_mb();
762 CMM_STORE_SHARED(timer_signal.qs_done, 0);
763 cmm_smp_mb();
764
765 /*
766 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
767 * thread wakes up.
768 */
769 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN);
770
771 while (!CMM_LOAD_SHARED(timer_signal.qs_done))
772 caa_cpu_relax();
773 cmm_smp_mb();
774
775 pthread_mutex_unlock(&timer_signal.lock);
776}
777
03d2d293
MD
778static
779void lib_ring_buffer_channel_switch_timer_start(struct channel *chan)
852c2936 780{
03d2d293
MD
781 struct sigevent sev;
782 struct itimerspec its;
783 int ret;
852c2936 784
03d2d293 785 if (!chan->switch_timer_interval || chan->switch_timer_enabled)
852c2936
MD
786 return;
787
03d2d293
MD
788 chan->switch_timer_enabled = 1;
789
790 lib_ring_buffer_setup_timer_thread();
791
ba702529 792 memset(&sev, 0, sizeof(sev));
03d2d293 793 sev.sigev_notify = SIGEV_SIGNAL;
34a91bdb 794 sev.sigev_signo = LTTNG_UST_RB_SIG_FLUSH;
03d2d293
MD
795 sev.sigev_value.sival_ptr = chan;
796 ret = timer_create(CLOCKID, &sev, &chan->switch_timer);
797 if (ret == -1) {
798 PERROR("timer_create");
799 }
800
801 its.it_value.tv_sec = chan->switch_timer_interval / 1000000;
f8921d33 802 its.it_value.tv_nsec = (chan->switch_timer_interval % 1000000) * 1000;
03d2d293
MD
803 its.it_interval.tv_sec = its.it_value.tv_sec;
804 its.it_interval.tv_nsec = its.it_value.tv_nsec;
805
806 ret = timer_settime(chan->switch_timer, 0, &its, NULL);
807 if (ret == -1) {
808 PERROR("timer_settime");
809 }
810}
811
03d2d293
MD
812static
813void lib_ring_buffer_channel_switch_timer_stop(struct channel *chan)
814{
34a91bdb 815 int ret;
03d2d293
MD
816
817 if (!chan->switch_timer_interval || !chan->switch_timer_enabled)
818 return;
819
820 ret = timer_delete(chan->switch_timer);
821 if (ret == -1) {
822 PERROR("timer_delete");
823 }
824
64bf51a6 825 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH);
03d2d293
MD
826
827 chan->switch_timer = 0;
828 chan->switch_timer_enabled = 0;
852c2936
MD
829}
830
34a91bdb
MD
831static
832void lib_ring_buffer_channel_read_timer_start(struct channel *chan)
852c2936 833{
4cfec15c 834 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
34a91bdb
MD
835 struct sigevent sev;
836 struct itimerspec its;
837 int ret;
852c2936 838
34a91bdb
MD
839 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
840 || !chan->read_timer_interval || chan->read_timer_enabled)
841 return;
852c2936 842
34a91bdb 843 chan->read_timer_enabled = 1;
852c2936 844
34a91bdb 845 lib_ring_buffer_setup_timer_thread();
852c2936 846
34a91bdb
MD
847 sev.sigev_notify = SIGEV_SIGNAL;
848 sev.sigev_signo = LTTNG_UST_RB_SIG_READ;
849 sev.sigev_value.sival_ptr = chan;
850 ret = timer_create(CLOCKID, &sev, &chan->read_timer);
851 if (ret == -1) {
852 PERROR("timer_create");
853 }
852c2936 854
34a91bdb 855 its.it_value.tv_sec = chan->read_timer_interval / 1000000;
f8921d33 856 its.it_value.tv_nsec = (chan->read_timer_interval % 1000000) * 1000;
34a91bdb
MD
857 its.it_interval.tv_sec = its.it_value.tv_sec;
858 its.it_interval.tv_nsec = its.it_value.tv_nsec;
852c2936 859
34a91bdb
MD
860 ret = timer_settime(chan->read_timer, 0, &its, NULL);
861 if (ret == -1) {
862 PERROR("timer_settime");
863 }
852c2936
MD
864}
865
34a91bdb
MD
866static
867void lib_ring_buffer_channel_read_timer_stop(struct channel *chan)
852c2936 868{
4cfec15c 869 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
34a91bdb 870 int ret;
852c2936
MD
871
872 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
34a91bdb 873 || !chan->read_timer_interval || !chan->read_timer_enabled)
852c2936
MD
874 return;
875
34a91bdb
MD
876 ret = timer_delete(chan->read_timer);
877 if (ret == -1) {
878 PERROR("timer_delete");
879 }
880
852c2936
MD
881 /*
882 * do one more check to catch data that has been written in the last
883 * timer period.
884 */
34a91bdb
MD
885 lib_ring_buffer_channel_do_read(chan);
886
64bf51a6 887 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ);
34a91bdb
MD
888
889 chan->read_timer = 0;
890 chan->read_timer_enabled = 0;
852c2936
MD
891}
892
1d498196 893static void channel_unregister_notifiers(struct channel *chan,
38fae1d3 894 struct lttng_ust_shm_handle *handle)
852c2936 895{
03d2d293 896 lib_ring_buffer_channel_switch_timer_stop(chan);
34a91bdb 897 lib_ring_buffer_channel_read_timer_stop(chan);
852c2936
MD
898}
899
009769ca
MD
900static void channel_print_errors(struct channel *chan,
901 struct lttng_ust_shm_handle *handle)
902{
903 const struct lttng_ust_lib_ring_buffer_config *config =
904 &chan->backend.config;
905 int cpu;
906
907 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
908 for_each_possible_cpu(cpu) {
909 struct lttng_ust_lib_ring_buffer *buf =
910 shmp(handle, chan->backend.buf[cpu].shmp);
15500a1b
MD
911 if (buf)
912 lib_ring_buffer_print_errors(chan, buf, cpu, handle);
009769ca
MD
913 }
914 } else {
915 struct lttng_ust_lib_ring_buffer *buf =
916 shmp(handle, chan->backend.buf[0].shmp);
917
15500a1b
MD
918 if (buf)
919 lib_ring_buffer_print_errors(chan, buf, -1, handle);
009769ca
MD
920 }
921}
922
923static void channel_free(struct channel *chan,
6548fca4
MD
924 struct lttng_ust_shm_handle *handle,
925 int consumer)
852c2936 926{
74d81a6c 927 channel_backend_free(&chan->backend, handle);
431d5cf0 928 /* chan is freed by shm teardown */
6548fca4 929 shm_object_table_destroy(handle->table, consumer);
1d498196 930 free(handle);
852c2936
MD
931}
932
933/**
934 * channel_create - Create channel.
935 * @config: ring buffer instance configuration
936 * @name: name of the channel
a3f61e7f
MD
937 * @priv_data: ring buffer client private data area pointer (output)
938 * @priv_data_size: length, in bytes, of the private data area.
d028eddb 939 * @priv_data_init: initialization data for private data.
852c2936
MD
940 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
941 * address mapping. It is used only by RING_BUFFER_STATIC
942 * configuration. It can be set to NULL for other backends.
943 * @subbuf_size: subbuffer size
944 * @num_subbuf: number of subbuffers
945 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
946 * padding to let readers get those sub-buffers.
947 * Used for live streaming.
948 * @read_timer_interval: Time interval (in us) to wake up pending readers.
5ea386c3
MD
949 * @stream_fds: array of stream file descriptors.
950 * @nr_stream_fds: number of file descriptors in array.
852c2936
MD
951 *
952 * Holds cpu hotplug.
953 * Returns NULL on failure.
954 */
4cfec15c 955struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f
MD
956 const char *name,
957 void **priv_data,
958 size_t priv_data_align,
959 size_t priv_data_size,
d028eddb 960 void *priv_data_init,
a3f61e7f 961 void *buf_addr, size_t subbuf_size,
852c2936 962 size_t num_subbuf, unsigned int switch_timer_interval,
a9ff648c 963 unsigned int read_timer_interval,
b2c5f61a
MD
964 const int *stream_fds, int nr_stream_fds,
965 int64_t blocking_timeout)
852c2936 966{
24622edc 967 int ret;
a3f61e7f 968 size_t shmsize, chansize;
852c2936 969 struct channel *chan;
38fae1d3 970 struct lttng_ust_shm_handle *handle;
1d498196 971 struct shm_object *shmobj;
74d81a6c 972 unsigned int nr_streams;
b2c5f61a 973 int64_t blocking_timeout_ms;
74d81a6c
MD
974
975 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
976 nr_streams = num_possible_cpus();
977 else
978 nr_streams = 1;
852c2936 979
5ea386c3
MD
980 if (nr_stream_fds != nr_streams)
981 return NULL;
982
b2c5f61a
MD
983 if (blocking_timeout < -1) {
984 return NULL;
985 }
986 /* usec to msec */
987 if (blocking_timeout == -1) {
988 blocking_timeout_ms = -1;
989 } else {
990 blocking_timeout_ms = blocking_timeout / 1000;
991 if (blocking_timeout_ms != (int32_t) blocking_timeout_ms) {
992 return NULL;
993 }
994 }
995
852c2936
MD
996 if (lib_ring_buffer_check_config(config, switch_timer_interval,
997 read_timer_interval))
998 return NULL;
999
38fae1d3 1000 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
431d5cf0
MD
1001 if (!handle)
1002 return NULL;
1003
1d498196
MD
1004 /* Allocate table for channel + per-cpu buffers */
1005 handle->table = shm_object_table_create(1 + num_possible_cpus());
1006 if (!handle->table)
1007 goto error_table_alloc;
852c2936 1008
1d498196
MD
1009 /* Calculate the shm allocation layout */
1010 shmsize = sizeof(struct channel);
b72687b8 1011 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp));
74d81a6c 1012 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams;
a3f61e7f 1013 chansize = shmsize;
74d81a6c 1014 if (priv_data_align)
b72687b8 1015 shmsize += lttng_ust_offset_align(shmsize, priv_data_align);
a3f61e7f 1016 shmsize += priv_data_size;
a6352fd4 1017
74d81a6c 1018 /* Allocate normal memory for channel (not shared) */
a9ff648c 1019 shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM,
4b68c31f 1020 -1, -1);
b5a14697
MD
1021 if (!shmobj)
1022 goto error_append;
57773204 1023 /* struct channel is at object 0, offset 0 (hardcoded) */
a3f61e7f 1024 set_shmp(handle->chan, zalloc_shm(shmobj, chansize));
57773204
MD
1025 assert(handle->chan._ref.index == 0);
1026 assert(handle->chan._ref.offset == 0);
1d498196 1027 chan = shmp(handle, handle->chan);
a6352fd4 1028 if (!chan)
1d498196 1029 goto error_append;
74d81a6c 1030 chan->nr_streams = nr_streams;
a6352fd4 1031
a3f61e7f
MD
1032 /* space for private data */
1033 if (priv_data_size) {
1034 DECLARE_SHMP(void, priv_data_alloc);
1035
1036 align_shm(shmobj, priv_data_align);
1037 chan->priv_data_offset = shmobj->allocated_len;
1038 set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size));
1039 if (!shmp(handle, priv_data_alloc))
1040 goto error_append;
1041 *priv_data = channel_get_private(chan);
d028eddb 1042 memcpy(*priv_data, priv_data_init, priv_data_size);
a3f61e7f
MD
1043 } else {
1044 chan->priv_data_offset = -1;
74d81a6c
MD
1045 if (priv_data)
1046 *priv_data = NULL;
a3f61e7f
MD
1047 }
1048
b2c5f61a
MD
1049 chan->u.s.blocking_timeout_ms = (int32_t) blocking_timeout_ms;
1050
a3f61e7f 1051 ret = channel_backend_init(&chan->backend, name, config,
a9ff648c 1052 subbuf_size, num_subbuf, handle,
5ea386c3 1053 stream_fds);
852c2936 1054 if (ret)
1d498196 1055 goto error_backend_init;
852c2936 1056
03d2d293 1057 chan->handle = handle;
852c2936 1058 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
852c2936 1059
34a91bdb
MD
1060 chan->switch_timer_interval = switch_timer_interval;
1061 chan->read_timer_interval = read_timer_interval;
03d2d293 1062 lib_ring_buffer_channel_switch_timer_start(chan);
34a91bdb 1063 lib_ring_buffer_channel_read_timer_start(chan);
852c2936 1064
431d5cf0 1065 return handle;
852c2936 1066
1d498196
MD
1067error_backend_init:
1068error_append:
6548fca4 1069 shm_object_table_destroy(handle->table, 1);
1d498196 1070error_table_alloc:
431d5cf0 1071 free(handle);
852c2936
MD
1072 return NULL;
1073}
852c2936 1074
74d81a6c 1075struct lttng_ust_shm_handle *channel_handle_create(void *data,
ff0f5728
MD
1076 uint64_t memory_map_size,
1077 int wakeup_fd)
193183fb 1078{
38fae1d3 1079 struct lttng_ust_shm_handle *handle;
193183fb
MD
1080 struct shm_object *object;
1081
38fae1d3 1082 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
193183fb
MD
1083 if (!handle)
1084 return NULL;
1085
1086 /* Allocate table for channel + per-cpu buffers */
1087 handle->table = shm_object_table_create(1 + num_possible_cpus());
1088 if (!handle->table)
1089 goto error_table_alloc;
1090 /* Add channel object */
74d81a6c 1091 object = shm_object_table_append_mem(handle->table, data,
ff0f5728 1092 memory_map_size, wakeup_fd);
193183fb
MD
1093 if (!object)
1094 goto error_table_object;
57773204
MD
1095 /* struct channel is at object 0, offset 0 (hardcoded) */
1096 handle->chan._ref.index = 0;
1097 handle->chan._ref.offset = 0;
193183fb
MD
1098 return handle;
1099
1100error_table_object:
6548fca4 1101 shm_object_table_destroy(handle->table, 0);
193183fb
MD
1102error_table_alloc:
1103 free(handle);
1104 return NULL;
1105}
1106
38fae1d3 1107int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
74d81a6c
MD
1108 int shm_fd, int wakeup_fd, uint32_t stream_nr,
1109 uint64_t memory_map_size)
193183fb
MD
1110{
1111 struct shm_object *object;
1112
1113 /* Add stream object */
74d81a6c
MD
1114 object = shm_object_table_append_shm(handle->table,
1115 shm_fd, wakeup_fd, stream_nr,
1116 memory_map_size);
193183fb 1117 if (!object)
74d81a6c 1118 return -EINVAL;
193183fb
MD
1119 return 0;
1120}
1121
74d81a6c
MD
1122unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle)
1123{
1124 assert(handle->table);
1125 return handle->table->allocated_len - 1;
1126}
1127
852c2936 1128static
6548fca4
MD
1129void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle,
1130 int consumer)
852c2936 1131{
6548fca4 1132 channel_free(chan, handle, consumer);
852c2936
MD
1133}
1134
1135/**
1136 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1137 * @chan: channel to destroy
1138 *
1139 * Holds cpu hotplug.
431d5cf0
MD
1140 * Call "destroy" callback, finalize channels, decrement the channel
1141 * reference count. Note that when readers have completed data
1142 * consumption of finalized channels, get_subbuf() will return -ENODATA.
a60af3a5 1143 * They should release their handle at that point.
852c2936 1144 */
a3f61e7f 1145void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
74d81a6c 1146 int consumer)
852c2936 1147{
74d81a6c
MD
1148 if (consumer) {
1149 /*
1150 * Note: the consumer takes care of finalizing and
1151 * switching the buffers.
1152 */
1153 channel_unregister_notifiers(chan, handle);
3d0ef9f6
MD
1154 /*
1155 * The consumer prints errors.
1156 */
1157 channel_print_errors(chan, handle);
824f40b8
MD
1158 }
1159
431d5cf0
MD
1160 /*
1161 * sessiond/consumer are keeping a reference on the shm file
1162 * descriptor directly. No need to refcount.
1163 */
6548fca4 1164 channel_release(chan, handle, consumer);
a3f61e7f 1165 return;
852c2936 1166}
852c2936 1167
4cfec15c
MD
1168struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
1169 const struct lttng_ust_lib_ring_buffer_config *config,
1d498196 1170 struct channel *chan, int cpu,
38fae1d3 1171 struct lttng_ust_shm_handle *handle,
74d81a6c
MD
1172 int *shm_fd, int *wait_fd,
1173 int *wakeup_fd,
1174 uint64_t *memory_map_size)
852c2936 1175{
381c0f1e
MD
1176 struct shm_ref *ref;
1177
1178 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
74d81a6c 1179 cpu = 0;
381c0f1e 1180 } else {
e095d803
MD
1181 if (cpu >= num_possible_cpus())
1182 return NULL;
381c0f1e 1183 }
74d81a6c
MD
1184 ref = &chan->backend.buf[cpu].shmp._ref;
1185 *shm_fd = shm_get_shm_fd(handle, ref);
1186 *wait_fd = shm_get_wait_fd(handle, ref);
1187 *wakeup_fd = shm_get_wakeup_fd(handle, ref);
1188 if (shm_get_shm_size(handle, ref, memory_map_size))
1189 return NULL;
1190 return shmp(handle, chan->backend.buf[cpu].shmp);
852c2936 1191}
852c2936 1192
ff0f5728
MD
1193int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1194 struct channel *chan,
1195 struct lttng_ust_shm_handle *handle)
1196{
1197 struct shm_ref *ref;
1198
1199 ref = &handle->chan._ref;
1200 return shm_close_wait_fd(handle, ref);
1201}
1202
1203int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1204 struct channel *chan,
1205 struct lttng_ust_shm_handle *handle)
1206{
1207 struct shm_ref *ref;
1208
1209 ref = &handle->chan._ref;
1210 return shm_close_wakeup_fd(handle, ref);
1211}
1212
1213int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
1214 struct channel *chan,
1215 struct lttng_ust_shm_handle *handle,
1216 int cpu)
852c2936 1217{
74d81a6c
MD
1218 struct shm_ref *ref;
1219
1220 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1221 cpu = 0;
1222 } else {
1223 if (cpu >= num_possible_cpus())
1224 return -EINVAL;
824f40b8 1225 }
74d81a6c
MD
1226 ref = &chan->backend.buf[cpu].shmp._ref;
1227 return shm_close_wait_fd(handle, ref);
1228}
1229
ff0f5728 1230int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
1231 struct channel *chan,
1232 struct lttng_ust_shm_handle *handle,
1233 int cpu)
1234{
1235 struct shm_ref *ref;
cb7378b3 1236 int ret;
74d81a6c
MD
1237
1238 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1239 cpu = 0;
1240 } else {
1241 if (cpu >= num_possible_cpus())
1242 return -EINVAL;
1243 }
1244 ref = &chan->backend.buf[cpu].shmp._ref;
cb7378b3
MD
1245 pthread_mutex_lock(&wakeup_fd_mutex);
1246 ret = shm_close_wakeup_fd(handle, ref);
1247 pthread_mutex_unlock(&wakeup_fd_mutex);
1248 return ret;
74d81a6c
MD
1249}
1250
1251int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
1252 struct lttng_ust_shm_handle *handle)
1253{
a6352fd4 1254 if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0)
852c2936 1255 return -EBUSY;
a6352fd4 1256 cmm_smp_mb();
852c2936
MD
1257 return 0;
1258}
852c2936 1259
4cfec15c 1260void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
74d81a6c 1261 struct lttng_ust_shm_handle *handle)
852c2936 1262{
1d498196 1263 struct channel *chan = shmp(handle, buf->backend.chan);
852c2936 1264
15500a1b
MD
1265 if (!chan)
1266 return;
a6352fd4
MD
1267 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
1268 cmm_smp_mb();
1269 uatomic_dec(&buf->active_readers);
852c2936
MD
1270}
1271
1272/**
1273 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1274 * @buf: ring buffer
1275 * @consumed: consumed count indicating the position where to read
1276 * @produced: produced count, indicates position when to stop reading
1277 *
1278 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1279 * data to read at consumed position, or 0 if the get operation succeeds.
852c2936
MD
1280 */
1281
4cfec15c 1282int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
1d498196 1283 unsigned long *consumed, unsigned long *produced,
38fae1d3 1284 struct lttng_ust_shm_handle *handle)
852c2936 1285{
15500a1b
MD
1286 struct channel *chan;
1287 const struct lttng_ust_lib_ring_buffer_config *config;
852c2936
MD
1288 unsigned long consumed_cur, write_offset;
1289 int finalized;
1290
15500a1b
MD
1291 chan = shmp(handle, buf->backend.chan);
1292 if (!chan)
1293 return -EPERM;
1294 config = &chan->backend.config;
14641deb 1295 finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
1296 /*
1297 * Read finalized before counters.
1298 */
a6352fd4
MD
1299 cmm_smp_rmb();
1300 consumed_cur = uatomic_read(&buf->consumed);
852c2936
MD
1301 /*
1302 * No need to issue a memory barrier between consumed count read and
1303 * write offset read, because consumed count can only change
1304 * concurrently in overwrite mode, and we keep a sequence counter
1305 * identifier derived from the write offset to check we are getting
1306 * the same sub-buffer we are expecting (the sub-buffers are atomically
1307 * "tagged" upon writes, tags are checked upon read).
1308 */
1309 write_offset = v_read(config, &buf->offset);
1310
1311 /*
1312 * Check that we are not about to read the same subbuffer in
1313 * which the writer head is.
1314 */
1315 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1316 == 0)
1317 goto nodata;
1318
1319 *consumed = consumed_cur;
1320 *produced = subbuf_trunc(write_offset, chan);
1321
1322 return 0;
1323
1324nodata:
1325 /*
1326 * The memory barriers __wait_event()/wake_up_interruptible() take care
1327 * of "raw_spin_is_locked" memory ordering.
1328 */
1329 if (finalized)
1330 return -ENODATA;
852c2936
MD
1331 else
1332 return -EAGAIN;
1333}
852c2936 1334
f45930b7
JG
1335/**
1336 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1337 * are saved regardless of whether the consumed and produced positions are
1338 * in the same subbuffer.
1339 * @buf: ring buffer
1340 * @consumed: consumed byte count indicating the last position read
1341 * @produced: produced byte count indicating the last position written
1342 *
1343 * This function is meant to provide information on the exact producer and
1344 * consumer positions without regard for the "snapshot" feature.
1345 */
1346int lib_ring_buffer_snapshot_sample_positions(
1347 struct lttng_ust_lib_ring_buffer *buf,
1348 unsigned long *consumed, unsigned long *produced,
1349 struct lttng_ust_shm_handle *handle)
1350{
1351 struct channel *chan;
1352 const struct lttng_ust_lib_ring_buffer_config *config;
f45930b7
JG
1353
1354 chan = shmp(handle, buf->backend.chan);
1355 if (!chan)
1356 return -EPERM;
1357 config = &chan->backend.config;
1358 cmm_smp_rmb();
1359 *consumed = uatomic_read(&buf->consumed);
1360 /*
1361 * No need to issue a memory barrier between consumed count read and
1362 * write offset read, because consumed count can only change
1363 * concurrently in overwrite mode, and we keep a sequence counter
1364 * identifier derived from the write offset to check we are getting
1365 * the same sub-buffer we are expecting (the sub-buffers are atomically
1366 * "tagged" upon writes, tags are checked upon read).
1367 */
1368 *produced = v_read(config, &buf->offset);
1369 return 0;
1370}
1371
852c2936 1372/**
d1a996f5 1373 * lib_ring_buffer_move_consumer - move consumed counter forward
852c2936
MD
1374 * @buf: ring buffer
1375 * @consumed_new: new consumed count value
1376 */
4cfec15c 1377void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1d498196 1378 unsigned long consumed_new,
38fae1d3 1379 struct lttng_ust_shm_handle *handle)
852c2936 1380{
4cfec15c 1381 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
15500a1b 1382 struct channel *chan;
852c2936
MD
1383 unsigned long consumed;
1384
15500a1b
MD
1385 chan = shmp(handle, bufb->chan);
1386 if (!chan)
1387 return;
74d81a6c 1388 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
852c2936
MD
1389
1390 /*
1391 * Only push the consumed value forward.
1392 * If the consumed cmpxchg fails, this is because we have been pushed by
1393 * the writer in flight recorder mode.
1394 */
a6352fd4 1395 consumed = uatomic_read(&buf->consumed);
852c2936 1396 while ((long) consumed - (long) consumed_new < 0)
a6352fd4
MD
1397 consumed = uatomic_cmpxchg(&buf->consumed, consumed,
1398 consumed_new);
852c2936 1399}
852c2936
MD
1400
1401/**
1402 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1403 * @buf: ring buffer
1404 * @consumed: consumed count indicating the position where to read
1405 *
1406 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1407 * data to read at consumed position, or 0 if the get operation succeeds.
852c2936 1408 */
4cfec15c 1409int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1d498196 1410 unsigned long consumed,
38fae1d3 1411 struct lttng_ust_shm_handle *handle)
852c2936 1412{
15500a1b
MD
1413 struct channel *chan;
1414 const struct lttng_ust_lib_ring_buffer_config *config;
852c2936 1415 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
dddba398 1416 int ret, finalized, nr_retry = LTTNG_UST_RING_BUFFER_GET_RETRY;
15500a1b 1417 struct commit_counters_cold *cc_cold;
852c2936 1418
15500a1b
MD
1419 chan = shmp(handle, buf->backend.chan);
1420 if (!chan)
1421 return -EPERM;
1422 config = &chan->backend.config;
852c2936 1423retry:
14641deb 1424 finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
1425 /*
1426 * Read finalized before counters.
1427 */
a6352fd4
MD
1428 cmm_smp_rmb();
1429 consumed_cur = uatomic_read(&buf->consumed);
852c2936 1430 consumed_idx = subbuf_index(consumed, chan);
15500a1b
MD
1431 cc_cold = shmp_index(handle, buf->commit_cold, consumed_idx);
1432 if (!cc_cold)
1433 return -EPERM;
1434 commit_count = v_read(config, &cc_cold->cc_sb);
852c2936
MD
1435 /*
1436 * Make sure we read the commit count before reading the buffer
1437 * data and the write offset. Correct consumed offset ordering
1438 * wrt commit count is insured by the use of cmpxchg to update
1439 * the consumed offset.
852c2936 1440 */
a6352fd4
MD
1441 /*
1442 * Local rmb to match the remote wmb to read the commit count
1443 * before the buffer data and the write offset.
1444 */
1445 cmm_smp_rmb();
852c2936
MD
1446
1447 write_offset = v_read(config, &buf->offset);
1448
1449 /*
1450 * Check that the buffer we are getting is after or at consumed_cur
1451 * position.
1452 */
1453 if ((long) subbuf_trunc(consumed, chan)
1454 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1455 goto nodata;
1456
1457 /*
1458 * Check that the subbuffer we are trying to consume has been
dddba398
MD
1459 * already fully committed. There are a few causes that can make
1460 * this unavailability situation occur:
1461 *
1462 * Temporary (short-term) situation:
1463 * - Application is running on a different CPU, between reserve
1464 * and commit ring buffer operations,
1465 * - Application is preempted between reserve and commit ring
1466 * buffer operations,
1467 *
1468 * Long-term situation:
1469 * - Application is stopped (SIGSTOP) between reserve and commit
1470 * ring buffer operations. Could eventually be resumed by
1471 * SIGCONT.
1472 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1473 * reserve and commit ring buffer operation.
1474 *
1475 * From a consumer perspective, handling short-term
1476 * unavailability situations is performed by retrying a few
1477 * times after a delay. Handling long-term unavailability
1478 * situations is handled by failing to get the sub-buffer.
1479 *
1480 * In all of those situations, if the application is taking a
1481 * long time to perform its commit after ring buffer space
1482 * reservation, we can end up in a situation where the producer
1483 * will fill the ring buffer and try to write into the same
1484 * sub-buffer again (which has a missing commit). This is
1485 * handled by the producer in the sub-buffer switch handling
1486 * code of the reserve routine by detecting unbalanced
1487 * reserve/commit counters and discarding all further events
1488 * until the situation is resolved in those situations. Two
1489 * scenarios can occur:
1490 *
1491 * 1) The application causing the reserve/commit counters to be
1492 * unbalanced has been terminated. In this situation, all
1493 * further events will be discarded in the buffers, and no
1494 * further buffer data will be readable by the consumer
1495 * daemon. Tearing down the UST tracing session and starting
1496 * anew is a work-around for those situations. Note that this
1497 * only affects per-UID tracing. In per-PID tracing, the
1498 * application vanishes with the termination, and therefore
1499 * no more data needs to be written to the buffers.
1500 * 2) The application causing the unbalance has been delayed for
1501 * a long time, but will eventually try to increment the
1502 * commit counter after eventually writing to the sub-buffer.
1503 * This situation can cause events to be discarded until the
1504 * application resumes its operations.
852c2936
MD
1505 */
1506 if (((commit_count - chan->backend.subbuf_size)
1507 & chan->commit_count_mask)
0bade047 1508 - (buf_trunc(consumed, chan)
852c2936 1509 >> chan->backend.num_subbuf_order)
dddba398
MD
1510 != 0) {
1511 if (nr_retry-- > 0) {
1512 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1513 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1514 goto retry;
1515 } else {
1516 goto nodata;
1517 }
1518 }
852c2936
MD
1519
1520 /*
1521 * Check that we are not about to read the same subbuffer in
1522 * which the writer head is.
1523 */
0bade047 1524 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan)
852c2936
MD
1525 == 0)
1526 goto nodata;
1527
1528 /*
1529 * Failure to get the subbuffer causes a busy-loop retry without going
1530 * to a wait queue. These are caused by short-lived race windows where
1531 * the writer is getting access to a subbuffer we were trying to get
1532 * access to. Also checks that the "consumed" buffer count we are
1533 * looking for matches the one contained in the subbuffer id.
dddba398
MD
1534 *
1535 * The short-lived race window described here can be affected by
1536 * application signals and preemption, thus requiring to bound
1537 * the loop to a maximum number of retry.
852c2936
MD
1538 */
1539 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1d498196
MD
1540 consumed_idx, buf_trunc_val(consumed, chan),
1541 handle);
dddba398
MD
1542 if (ret) {
1543 if (nr_retry-- > 0) {
1544 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1545 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1546 goto retry;
1547 } else {
1548 goto nodata;
1549 }
1550 }
852c2936
MD
1551 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1552
1553 buf->get_subbuf_consumed = consumed;
1554 buf->get_subbuf = 1;
1555
1556 return 0;
1557
1558nodata:
1559 /*
1560 * The memory barriers __wait_event()/wake_up_interruptible() take care
1561 * of "raw_spin_is_locked" memory ordering.
1562 */
1563 if (finalized)
1564 return -ENODATA;
852c2936
MD
1565 else
1566 return -EAGAIN;
1567}
852c2936
MD
1568
1569/**
1570 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1571 * @buf: ring buffer
1572 */
4cfec15c 1573void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 1574 struct lttng_ust_shm_handle *handle)
852c2936 1575{
4cfec15c 1576 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
15500a1b
MD
1577 struct channel *chan;
1578 const struct lttng_ust_lib_ring_buffer_config *config;
1579 unsigned long sb_bindex, consumed_idx, consumed;
1580 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
1581 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
852c2936 1582
15500a1b
MD
1583 chan = shmp(handle, bufb->chan);
1584 if (!chan)
1585 return;
1586 config = &chan->backend.config;
74d81a6c 1587 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
852c2936
MD
1588
1589 if (!buf->get_subbuf) {
1590 /*
1591 * Reader puts a subbuffer it did not get.
1592 */
1593 CHAN_WARN_ON(chan, 1);
1594 return;
1595 }
1596 consumed = buf->get_subbuf_consumed;
1597 buf->get_subbuf = 0;
1598
1599 /*
1600 * Clear the records_unread counter. (overruns counter)
1601 * Can still be non-zero if a file reader simply grabbed the data
1602 * without using iterators.
1603 * Can be below zero if an iterator is used on a snapshot more than
1604 * once.
1605 */
15500a1b
MD
1606 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1607 rpages = shmp_index(handle, bufb->array, sb_bindex);
1608 if (!rpages)
1609 return;
1610 backend_pages = shmp(handle, rpages->shmp);
1611 if (!backend_pages)
1612 return;
1613 v_add(config, v_read(config, &backend_pages->records_unread),
1614 &bufb->records_read);
1615 v_set(config, &backend_pages->records_unread, 0);
852c2936
MD
1616 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1617 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1618 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1619
1620 /*
1621 * Exchange the reader subbuffer with the one we put in its place in the
1622 * writer subbuffer table. Expect the original consumed count. If
1623 * update_read_sb_index fails, this is because the writer updated the
1624 * subbuffer concurrently. We should therefore keep the subbuffer we
1625 * currently have: it has become invalid to try reading this sub-buffer
1626 * consumed count value anyway.
1627 */
1628 consumed_idx = subbuf_index(consumed, chan);
1629 update_read_sb_index(config, &buf->backend, &chan->backend,
1d498196
MD
1630 consumed_idx, buf_trunc_val(consumed, chan),
1631 handle);
852c2936
MD
1632 /*
1633 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1634 * if the writer concurrently updated it.
1635 */
1636}
852c2936
MD
1637
1638/*
1639 * cons_offset is an iterator on all subbuffer offsets between the reader
1640 * position and the writer position. (inclusive)
1641 */
1642static
4cfec15c 1643void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1644 struct channel *chan,
1645 unsigned long cons_offset,
1d498196 1646 int cpu,
38fae1d3 1647 struct lttng_ust_shm_handle *handle)
852c2936 1648{
4cfec15c 1649 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936 1650 unsigned long cons_idx, commit_count, commit_count_sb;
15500a1b
MD
1651 struct commit_counters_hot *cc_hot;
1652 struct commit_counters_cold *cc_cold;
852c2936
MD
1653
1654 cons_idx = subbuf_index(cons_offset, chan);
15500a1b
MD
1655 cc_hot = shmp_index(handle, buf->commit_hot, cons_idx);
1656 if (!cc_hot)
1657 return;
1658 cc_cold = shmp_index(handle, buf->commit_cold, cons_idx);
1659 if (!cc_cold)
1660 return;
1661 commit_count = v_read(config, &cc_hot->cc);
1662 commit_count_sb = v_read(config, &cc_cold->cc_sb);
852c2936
MD
1663
1664 if (subbuf_offset(commit_count, chan) != 0)
4d3c9523 1665 DBG("ring buffer %s, cpu %d: "
852c2936
MD
1666 "commit count in subbuffer %lu,\n"
1667 "expecting multiples of %lu bytes\n"
1668 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1669 chan->backend.name, cpu, cons_idx,
1670 chan->backend.subbuf_size,
1671 commit_count, commit_count_sb);
1672
4d3c9523 1673 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
852c2936
MD
1674 chan->backend.name, cpu, commit_count);
1675}
1676
1677static
4cfec15c 1678void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf,
852c2936 1679 struct channel *chan,
1d498196 1680 void *priv, int cpu,
38fae1d3 1681 struct lttng_ust_shm_handle *handle)
852c2936 1682{
4cfec15c 1683 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1684 unsigned long write_offset, cons_offset;
1685
852c2936
MD
1686 /*
1687 * No need to order commit_count, write_offset and cons_offset reads
1688 * because we execute at teardown when no more writer nor reader
1689 * references are left.
1690 */
1691 write_offset = v_read(config, &buf->offset);
a6352fd4 1692 cons_offset = uatomic_read(&buf->consumed);
852c2936 1693 if (write_offset != cons_offset)
4d3c9523 1694 DBG("ring buffer %s, cpu %d: "
852c2936
MD
1695 "non-consumed data\n"
1696 " [ %lu bytes written, %lu bytes read ]\n",
1697 chan->backend.name, cpu, write_offset, cons_offset);
1698
a6352fd4 1699 for (cons_offset = uatomic_read(&buf->consumed);
852c2936
MD
1700 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1701 chan)
1702 - cons_offset) > 0;
1703 cons_offset = subbuf_align(cons_offset, chan))
1704 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1d498196 1705 cpu, handle);
852c2936
MD
1706}
1707
1708static
1709void lib_ring_buffer_print_errors(struct channel *chan,
009769ca
MD
1710 struct lttng_ust_lib_ring_buffer *buf, int cpu,
1711 struct lttng_ust_shm_handle *handle)
852c2936 1712{
4cfec15c 1713 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
a3f61e7f 1714 void *priv = channel_get_private(chan);
852c2936 1715
a1360615
MD
1716 if (!strcmp(chan->backend.name, "relay-metadata-mmap")) {
1717 DBG("ring buffer %s: %lu records written, "
1718 "%lu records overrun\n",
1719 chan->backend.name,
1720 v_read(config, &buf->records_count),
1721 v_read(config, &buf->records_overrun));
1722 } else {
1723 DBG("ring buffer %s, cpu %d: %lu records written, "
1724 "%lu records overrun\n",
1725 chan->backend.name, cpu,
1726 v_read(config, &buf->records_count),
1727 v_read(config, &buf->records_overrun));
1728
1729 if (v_read(config, &buf->records_lost_full)
1730 || v_read(config, &buf->records_lost_wrap)
1731 || v_read(config, &buf->records_lost_big))
1732 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1733 " [ %lu buffer full, %lu nest buffer wrap-around, "
1734 "%lu event too big ]\n",
1735 chan->backend.name, cpu,
1736 v_read(config, &buf->records_lost_full),
1737 v_read(config, &buf->records_lost_wrap),
1738 v_read(config, &buf->records_lost_big));
1739 }
1d498196 1740 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle);
852c2936
MD
1741}
1742
1743/*
1744 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1745 *
48f22436
MD
1746 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1747 * active or at buffer finalization (destroy).
852c2936
MD
1748 */
1749static
4cfec15c 1750void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1751 struct channel *chan,
1752 struct switch_offsets *offsets,
2fed87ae 1753 uint64_t tsc,
38fae1d3 1754 struct lttng_ust_shm_handle *handle)
852c2936 1755{
4cfec15c 1756 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1757 unsigned long oldidx = subbuf_index(offsets->old, chan);
1758 unsigned long commit_count;
3d3c3833 1759 struct commit_counters_hot *cc_hot;
852c2936 1760
1d498196 1761 config->cb.buffer_begin(buf, tsc, oldidx, handle);
852c2936
MD
1762
1763 /*
1764 * Order all writes to buffer before the commit count update that will
1765 * determine that the subbuffer is full.
1766 */
a6352fd4 1767 cmm_smp_wmb();
3d3c3833 1768 cc_hot = shmp_index(handle, buf->commit_hot, oldidx);
15500a1b
MD
1769 if (!cc_hot)
1770 return;
852c2936 1771 v_add(config, config->cb.subbuffer_header_size(),
3d3c3833
MD
1772 &cc_hot->cc);
1773 commit_count = v_read(config, &cc_hot->cc);
852c2936
MD
1774 /* Check if the written buffer has to be delivered */
1775 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1b7b0501 1776 commit_count, oldidx, handle, tsc);
d2fe4771 1777 lib_ring_buffer_write_commit_counter(config, buf, chan,
80249235 1778 offsets->old + config->cb.subbuffer_header_size(),
3d3c3833 1779 commit_count, handle, cc_hot);
852c2936
MD
1780}
1781
1782/*
1783 * lib_ring_buffer_switch_old_end: switch old subbuffer
1784 *
1785 * Note : offset_old should never be 0 here. It is ok, because we never perform
1786 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1787 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1788 * subbuffer.
1789 */
1790static
4cfec15c 1791void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1792 struct channel *chan,
1793 struct switch_offsets *offsets,
2fed87ae 1794 uint64_t tsc,
38fae1d3 1795 struct lttng_ust_shm_handle *handle)
852c2936 1796{
4cfec15c 1797 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1798 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1799 unsigned long commit_count, padding_size, data_size;
3d3c3833 1800 struct commit_counters_hot *cc_hot;
6c737d05 1801 uint64_t *ts_end;
852c2936
MD
1802
1803 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1804 padding_size = chan->backend.subbuf_size - data_size;
1d498196
MD
1805 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size,
1806 handle);
852c2936 1807
6c737d05
MD
1808 ts_end = shmp_index(handle, buf->ts_end, oldidx);
1809 if (!ts_end)
1810 return;
852c2936 1811 /*
6c737d05
MD
1812 * This is the last space reservation in that sub-buffer before
1813 * it gets delivered. This provides exclusive access to write to
1814 * this sub-buffer's ts_end. There are also no concurrent
1815 * readers of that ts_end because delivery of that sub-buffer is
1816 * postponed until the commit counter is incremented for the
1817 * current space reservation.
1818 */
1819 *ts_end = tsc;
1820
1821 /*
1822 * Order all writes to buffer and store to ts_end before the commit
1823 * count update that will determine that the subbuffer is full.
852c2936 1824 */
a6352fd4 1825 cmm_smp_wmb();
3d3c3833 1826 cc_hot = shmp_index(handle, buf->commit_hot, oldidx);
15500a1b
MD
1827 if (!cc_hot)
1828 return;
3d3c3833
MD
1829 v_add(config, padding_size, &cc_hot->cc);
1830 commit_count = v_read(config, &cc_hot->cc);
852c2936 1831 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1b7b0501 1832 commit_count, oldidx, handle, tsc);
d2fe4771
MD
1833 lib_ring_buffer_write_commit_counter(config, buf, chan,
1834 offsets->old + padding_size, commit_count, handle,
3d3c3833 1835 cc_hot);
852c2936
MD
1836}
1837
1838/*
1839 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1840 *
1841 * This code can be executed unordered : writers may already have written to the
1842 * sub-buffer before this code gets executed, caution. The commit makes sure
1843 * that this code is executed before the deliver of this sub-buffer.
1844 */
1845static
4cfec15c 1846void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1847 struct channel *chan,
1848 struct switch_offsets *offsets,
2fed87ae 1849 uint64_t tsc,
38fae1d3 1850 struct lttng_ust_shm_handle *handle)
852c2936 1851{
4cfec15c 1852 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1853 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1854 unsigned long commit_count;
3d3c3833 1855 struct commit_counters_hot *cc_hot;
852c2936 1856
1d498196 1857 config->cb.buffer_begin(buf, tsc, beginidx, handle);
852c2936
MD
1858
1859 /*
1860 * Order all writes to buffer before the commit count update that will
1861 * determine that the subbuffer is full.
1862 */
a6352fd4 1863 cmm_smp_wmb();
3d3c3833 1864 cc_hot = shmp_index(handle, buf->commit_hot, beginidx);
15500a1b
MD
1865 if (!cc_hot)
1866 return;
1867 v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc);
3d3c3833 1868 commit_count = v_read(config, &cc_hot->cc);
852c2936
MD
1869 /* Check if the written buffer has to be delivered */
1870 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1b7b0501 1871 commit_count, beginidx, handle, tsc);
d2fe4771 1872 lib_ring_buffer_write_commit_counter(config, buf, chan,
80249235 1873 offsets->begin + config->cb.subbuffer_header_size(),
3d3c3833 1874 commit_count, handle, cc_hot);
852c2936
MD
1875}
1876
1ad21f70
MD
1877/*
1878 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1879 *
3962118d
MD
1880 * Calls subbuffer_set_data_size() to set the data size of the current
1881 * sub-buffer. We do not need to perform check_deliver nor commit here,
1882 * since this task will be done by the "commit" of the event for which
1883 * we are currently doing the space reservation.
1ad21f70
MD
1884 */
1885static
1886void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf,
1887 struct channel *chan,
1888 struct switch_offsets *offsets,
1889 uint64_t tsc,
1890 struct lttng_ust_shm_handle *handle)
1891{
1892 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
3962118d 1893 unsigned long endidx, data_size;
6c737d05 1894 uint64_t *ts_end;
1ad21f70 1895
3962118d 1896 endidx = subbuf_index(offsets->end - 1, chan);
1ad21f70 1897 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1ad21f70
MD
1898 subbuffer_set_data_size(config, &buf->backend, endidx, data_size,
1899 handle);
6c737d05
MD
1900 ts_end = shmp_index(handle, buf->ts_end, endidx);
1901 if (!ts_end)
1902 return;
1903 /*
1904 * This is the last space reservation in that sub-buffer before
1905 * it gets delivered. This provides exclusive access to write to
1906 * this sub-buffer's ts_end. There are also no concurrent
1907 * readers of that ts_end because delivery of that sub-buffer is
1908 * postponed until the commit counter is incremented for the
1909 * current space reservation.
1910 */
1911 *ts_end = tsc;
1ad21f70
MD
1912}
1913
852c2936
MD
1914/*
1915 * Returns :
1916 * 0 if ok
1917 * !0 if execution must be aborted.
1918 */
1919static
1920int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
4cfec15c 1921 struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1922 struct channel *chan,
1923 struct switch_offsets *offsets,
2dac206d
MD
1924 uint64_t *tsc,
1925 struct lttng_ust_shm_handle *handle)
852c2936 1926{
4cfec15c 1927 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
2dac206d 1928 unsigned long off, reserve_commit_diff;
852c2936
MD
1929
1930 offsets->begin = v_read(config, &buf->offset);
1931 offsets->old = offsets->begin;
1932 offsets->switch_old_start = 0;
1933 off = subbuf_offset(offsets->begin, chan);
1934
1935 *tsc = config->cb.ring_buffer_clock_read(chan);
1936
1937 /*
1938 * Ensure we flush the header of an empty subbuffer when doing the
1939 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1940 * total data gathering duration even if there were no records saved
1941 * after the last buffer switch.
1942 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1943 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1944 * subbuffer header as appropriate.
1945 * The next record that reserves space will be responsible for
1946 * populating the following subbuffer header. We choose not to populate
1947 * the next subbuffer header here because we want to be able to use
a6352fd4
MD
1948 * SWITCH_ACTIVE for periodical buffer flush, which must
1949 * guarantee that all the buffer content (records and header
1950 * timestamps) are visible to the reader. This is required for
1951 * quiescence guarantees for the fusion merge.
852c2936 1952 */
2dac206d
MD
1953 if (mode != SWITCH_FLUSH && !off)
1954 return -1; /* we do not have to switch : buffer is empty */
1955
1956 if (caa_unlikely(off == 0)) {
1957 unsigned long sb_index, commit_count;
15500a1b 1958 struct commit_counters_cold *cc_cold;
2dac206d
MD
1959
1960 /*
48f22436
MD
1961 * We are performing a SWITCH_FLUSH. There may be concurrent
1962 * writes into the buffer if e.g. invoked while performing a
1963 * snapshot on an active trace.
2dac206d 1964 *
48f22436
MD
1965 * If the client does not save any header information
1966 * (sub-buffer header size == 0), don't switch empty subbuffer
1967 * on finalize, because it is invalid to deliver a completely
1968 * empty subbuffer.
2dac206d
MD
1969 */
1970 if (!config->cb.subbuffer_header_size())
1971 return -1;
1972
1973 /* Test new buffer integrity */
1974 sb_index = subbuf_index(offsets->begin, chan);
15500a1b
MD
1975 cc_cold = shmp_index(handle, buf->commit_cold, sb_index);
1976 if (!cc_cold)
1977 return -1;
1978 commit_count = v_read(config, &cc_cold->cc_sb);
2dac206d
MD
1979 reserve_commit_diff =
1980 (buf_trunc(offsets->begin, chan)
1981 >> chan->backend.num_subbuf_order)
1982 - (commit_count & chan->commit_count_mask);
1983 if (caa_likely(reserve_commit_diff == 0)) {
1984 /* Next subbuffer not being written to. */
1985 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1986 subbuf_trunc(offsets->begin, chan)
1987 - subbuf_trunc((unsigned long)
1988 uatomic_read(&buf->consumed), chan)
1989 >= chan->backend.buf_size)) {
1990 /*
1991 * We do not overwrite non consumed buffers
1992 * and we are full : don't switch.
1993 */
852c2936 1994 return -1;
2dac206d
MD
1995 } else {
1996 /*
1997 * Next subbuffer not being written to, and we
1998 * are either in overwrite mode or the buffer is
1999 * not full. It's safe to write in this new
2000 * subbuffer.
2001 */
2002 }
2003 } else {
852c2936 2004 /*
2dac206d
MD
2005 * Next subbuffer reserve offset does not match the
2006 * commit offset. Don't perform switch in
2007 * producer-consumer and overwrite mode. Caused by
2008 * either a writer OOPS or too many nested writes over a
2009 * reserve/commit pair.
852c2936 2010 */
2dac206d 2011 return -1;
852c2936 2012 }
2dac206d
MD
2013
2014 /*
2015 * Need to write the subbuffer start header on finalize.
2016 */
2017 offsets->switch_old_start = 1;
2018 }
2019 offsets->begin = subbuf_align(offsets->begin, chan);
852c2936
MD
2020 /* Note: old points to the next subbuf at offset 0 */
2021 offsets->end = offsets->begin;
2022 return 0;
2023}
2024
2025/*
2026 * Force a sub-buffer switch. This operation is completely reentrant : can be
2027 * called while tracing is active with absolutely no lock held.
2028 *
ef1a13e0
MD
2029 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2030 * some atomic operations, this function must be called from the CPU
2031 * which owns the buffer for a ACTIVE flush. However, for
2032 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2033 * from any CPU.
852c2936 2034 */
4cfec15c 2035void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode,
38fae1d3 2036 struct lttng_ust_shm_handle *handle)
852c2936 2037{
15500a1b
MD
2038 struct channel *chan;
2039 const struct lttng_ust_lib_ring_buffer_config *config;
852c2936
MD
2040 struct switch_offsets offsets;
2041 unsigned long oldidx;
2fed87ae 2042 uint64_t tsc;
852c2936 2043
15500a1b
MD
2044 chan = shmp(handle, buf->backend.chan);
2045 if (!chan)
2046 return;
2047 config = &chan->backend.config;
2048
852c2936
MD
2049 offsets.size = 0;
2050
2051 /*
2052 * Perform retryable operations.
2053 */
2054 do {
2055 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
2dac206d 2056 &tsc, handle))
852c2936
MD
2057 return; /* Switch not needed */
2058 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
2059 != offsets.old);
2060
2061 /*
2062 * Atomically update last_tsc. This update races against concurrent
2063 * atomic updates, but the race will always cause supplementary full TSC
2064 * records, never the opposite (missing a full TSC record when it would
2065 * be needed).
2066 */
2067 save_last_tsc(config, buf, tsc);
2068
2069 /*
2070 * Push the reader if necessary
2071 */
2072 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
2073
2074 oldidx = subbuf_index(offsets.old, chan);
1d498196 2075 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle);
852c2936
MD
2076
2077 /*
2078 * May need to populate header start on SWITCH_FLUSH.
2079 */
2080 if (offsets.switch_old_start) {
1d498196 2081 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle);
852c2936
MD
2082 offsets.old += config->cb.subbuffer_header_size();
2083 }
2084
2085 /*
2086 * Switch old subbuffer.
2087 */
1d498196 2088 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle);
852c2936 2089}
852c2936 2090
6f97f9c2
MD
2091static
2092bool handle_blocking_retry(int *timeout_left_ms)
2093{
2094 int timeout = *timeout_left_ms, delay;
2095
2096 if (caa_likely(!timeout))
2097 return false; /* Do not retry, discard event. */
2098 if (timeout < 0) /* Wait forever. */
2099 delay = RETRY_DELAY_MS;
2100 else
2101 delay = min_t(int, timeout, RETRY_DELAY_MS);
2102 (void) poll(NULL, 0, delay);
2103 if (timeout > 0)
2104 *timeout_left_ms -= delay;
2105 return true; /* Retry. */
2106}
2107
852c2936
MD
2108/*
2109 * Returns :
2110 * 0 if ok
2111 * -ENOSPC if event size is too large for packet.
2112 * -ENOBUFS if there is currently not enough space in buffer for the event.
2113 * -EIO if data cannot be written into the buffer for any other reason.
2114 */
2115static
4cfec15c 2116int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
2117 struct channel *chan,
2118 struct switch_offsets *offsets,
e56bb47c
MD
2119 struct lttng_ust_lib_ring_buffer_ctx *ctx,
2120 void *client_ctx)
852c2936 2121{
4cfec15c 2122 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
38fae1d3 2123 struct lttng_ust_shm_handle *handle = ctx->handle;
e185cf4b 2124 unsigned long reserve_commit_diff, offset_cmp;
b2c5f61a 2125 int timeout_left_ms = lttng_ust_ringbuffer_get_timeout(chan);
852c2936 2126
e185cf4b
MD
2127retry:
2128 offsets->begin = offset_cmp = v_read(config, &buf->offset);
852c2936
MD
2129 offsets->old = offsets->begin;
2130 offsets->switch_new_start = 0;
1ad21f70 2131 offsets->switch_new_end = 0;
852c2936
MD
2132 offsets->switch_old_end = 0;
2133 offsets->pre_header_padding = 0;
2134
2135 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
2136 if ((int64_t) ctx->tsc == -EIO)
2137 return -EIO;
2138
2139 if (last_tsc_overflow(config, buf, ctx->tsc))
2140 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
2141
b5a3dfa5 2142 if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
852c2936
MD
2143 offsets->switch_new_start = 1; /* For offsets->begin */
2144 } else {
2145 offsets->size = config->cb.record_header_size(config, chan,
2146 offsets->begin,
2147 &offsets->pre_header_padding,
e56bb47c 2148 ctx, client_ctx);
852c2936
MD
2149 offsets->size +=
2150 lib_ring_buffer_align(offsets->begin + offsets->size,
2151 ctx->largest_align)
2152 + ctx->data_size;
b5a3dfa5 2153 if (caa_unlikely(subbuf_offset(offsets->begin, chan) +
852c2936
MD
2154 offsets->size > chan->backend.subbuf_size)) {
2155 offsets->switch_old_end = 1; /* For offsets->old */
2156 offsets->switch_new_start = 1; /* For offsets->begin */
2157 }
2158 }
b5a3dfa5 2159 if (caa_unlikely(offsets->switch_new_start)) {
e185cf4b 2160 unsigned long sb_index, commit_count;
15500a1b 2161 struct commit_counters_cold *cc_cold;
852c2936
MD
2162
2163 /*
2164 * We are typically not filling the previous buffer completely.
2165 */
b5a3dfa5 2166 if (caa_likely(offsets->switch_old_end))
852c2936
MD
2167 offsets->begin = subbuf_align(offsets->begin, chan);
2168 offsets->begin = offsets->begin
2169 + config->cb.subbuffer_header_size();
2170 /* Test new buffer integrity */
2171 sb_index = subbuf_index(offsets->begin, chan);
e185cf4b
MD
2172 /*
2173 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2174 * lib_ring_buffer_check_deliver() has the matching
2175 * memory barriers required around commit_cold cc_sb
2176 * updates to ensure reserve and commit counter updates
2177 * are not seen reordered when updated by another CPU.
2178 */
2179 cmm_smp_rmb();
15500a1b
MD
2180 cc_cold = shmp_index(handle, buf->commit_cold, sb_index);
2181 if (!cc_cold)
2182 return -1;
2183 commit_count = v_read(config, &cc_cold->cc_sb);
e185cf4b
MD
2184 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2185 cmm_smp_rmb();
2186 if (caa_unlikely(offset_cmp != v_read(config, &buf->offset))) {
2187 /*
2188 * The reserve counter have been concurrently updated
2189 * while we read the commit counter. This means the
2190 * commit counter we read might not match buf->offset
2191 * due to concurrent update. We therefore need to retry.
2192 */
2193 goto retry;
2194 }
852c2936
MD
2195 reserve_commit_diff =
2196 (buf_trunc(offsets->begin, chan)
2197 >> chan->backend.num_subbuf_order)
e185cf4b 2198 - (commit_count & chan->commit_count_mask);
b5a3dfa5 2199 if (caa_likely(reserve_commit_diff == 0)) {
852c2936 2200 /* Next subbuffer not being written to. */
b5a3dfa5 2201 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
852c2936
MD
2202 subbuf_trunc(offsets->begin, chan)
2203 - subbuf_trunc((unsigned long)
a6352fd4 2204 uatomic_read(&buf->consumed), chan)
852c2936 2205 >= chan->backend.buf_size)) {
64493e4f
MD
2206 unsigned long nr_lost;
2207
6f97f9c2
MD
2208 if (handle_blocking_retry(&timeout_left_ms))
2209 goto retry;
2210
852c2936
MD
2211 /*
2212 * We do not overwrite non consumed buffers
2213 * and we are full : record is lost.
2214 */
64493e4f 2215 nr_lost = v_read(config, &buf->records_lost_full);
852c2936 2216 v_inc(config, &buf->records_lost_full);
64493e4f
MD
2217 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2218 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2219 nr_lost + 1, chan->backend.name,
2220 buf->backend.cpu);
2221 }
852c2936
MD
2222 return -ENOBUFS;
2223 } else {
2224 /*
2225 * Next subbuffer not being written to, and we
2226 * are either in overwrite mode or the buffer is
2227 * not full. It's safe to write in this new
2228 * subbuffer.
2229 */
2230 }
2231 } else {
64493e4f
MD
2232 unsigned long nr_lost;
2233
852c2936
MD
2234 /*
2235 * Next subbuffer reserve offset does not match the
e185cf4b
MD
2236 * commit offset, and this did not involve update to the
2237 * reserve counter. Drop record in producer-consumer and
852c2936
MD
2238 * overwrite mode. Caused by either a writer OOPS or too
2239 * many nested writes over a reserve/commit pair.
2240 */
64493e4f 2241 nr_lost = v_read(config, &buf->records_lost_wrap);
852c2936 2242 v_inc(config, &buf->records_lost_wrap);
64493e4f
MD
2243 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2244 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2245 nr_lost + 1, chan->backend.name,
2246 buf->backend.cpu);
2247 }
852c2936
MD
2248 return -EIO;
2249 }
2250 offsets->size =
2251 config->cb.record_header_size(config, chan,
2252 offsets->begin,
2253 &offsets->pre_header_padding,
e56bb47c 2254 ctx, client_ctx);
852c2936
MD
2255 offsets->size +=
2256 lib_ring_buffer_align(offsets->begin + offsets->size,
2257 ctx->largest_align)
2258 + ctx->data_size;
b5a3dfa5 2259 if (caa_unlikely(subbuf_offset(offsets->begin, chan)
852c2936 2260 + offsets->size > chan->backend.subbuf_size)) {
64493e4f
MD
2261 unsigned long nr_lost;
2262
852c2936
MD
2263 /*
2264 * Record too big for subbuffers, report error, don't
2265 * complete the sub-buffer switch.
2266 */
64493e4f 2267 nr_lost = v_read(config, &buf->records_lost_big);
852c2936 2268 v_inc(config, &buf->records_lost_big);
64493e4f
MD
2269 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2270 DBG("%lu or more records lost in (%s:%d) record size "
2271 " of %zu bytes is too large for buffer\n",
2272 nr_lost + 1, chan->backend.name,
2273 buf->backend.cpu, offsets->size);
2274 }
852c2936
MD
2275 return -ENOSPC;
2276 } else {
2277 /*
2278 * We just made a successful buffer switch and the
2279 * record fits in the new subbuffer. Let's write.
2280 */
2281 }
2282 } else {
2283 /*
2284 * Record fits in the current buffer and we are not on a switch
2285 * boundary. It's safe to write.
2286 */
2287 }
2288 offsets->end = offsets->begin + offsets->size;
1ad21f70
MD
2289
2290 if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) {
2291 /*
2292 * The offset_end will fall at the very beginning of the next
2293 * subbuffer.
2294 */
2295 offsets->switch_new_end = 1; /* For offsets->begin */
2296 }
852c2936
MD
2297 return 0;
2298}
2299
2300/**
2301 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2302 * @ctx: ring buffer context.
2303 *
2304 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2305 * -EIO for other errors, else returns 0.
2306 * It will take care of sub-buffer switching.
2307 */
e56bb47c
MD
2308int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx,
2309 void *client_ctx)
852c2936
MD
2310{
2311 struct channel *chan = ctx->chan;
38fae1d3 2312 struct lttng_ust_shm_handle *handle = ctx->handle;
4cfec15c
MD
2313 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
2314 struct lttng_ust_lib_ring_buffer *buf;
852c2936
MD
2315 struct switch_offsets offsets;
2316 int ret;
2317
2318 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
1d498196 2319 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
852c2936 2320 else
1d498196 2321 buf = shmp(handle, chan->backend.buf[0].shmp);
15500a1b
MD
2322 if (!buf)
2323 return -EIO;
852c2936
MD
2324 ctx->buf = buf;
2325
2326 offsets.size = 0;
2327
2328 do {
2329 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
e56bb47c 2330 ctx, client_ctx);
b5a3dfa5 2331 if (caa_unlikely(ret))
852c2936 2332 return ret;
b5a3dfa5 2333 } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
852c2936
MD
2334 offsets.end)
2335 != offsets.old));
2336
2337 /*
2338 * Atomically update last_tsc. This update races against concurrent
2339 * atomic updates, but the race will always cause supplementary full TSC
2340 * records, never the opposite (missing a full TSC record when it would
2341 * be needed).
2342 */
2343 save_last_tsc(config, buf, ctx->tsc);
2344
2345 /*
2346 * Push the reader if necessary
2347 */
2348 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
2349
2350 /*
2351 * Clear noref flag for this subbuffer.
2352 */
2353 lib_ring_buffer_clear_noref(config, &buf->backend,
1d498196
MD
2354 subbuf_index(offsets.end - 1, chan),
2355 handle);
852c2936
MD
2356
2357 /*
2358 * Switch old subbuffer if needed.
2359 */
b5a3dfa5 2360 if (caa_unlikely(offsets.switch_old_end)) {
852c2936 2361 lib_ring_buffer_clear_noref(config, &buf->backend,
1d498196
MD
2362 subbuf_index(offsets.old - 1, chan),
2363 handle);
2364 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle);
852c2936
MD
2365 }
2366
2367 /*
2368 * Populate new subbuffer.
2369 */
b5a3dfa5 2370 if (caa_unlikely(offsets.switch_new_start))
1d498196 2371 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle);
852c2936 2372
1ad21f70
MD
2373 if (caa_unlikely(offsets.switch_new_end))
2374 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle);
2375
852c2936
MD
2376 ctx->slot_size = offsets.size;
2377 ctx->pre_offset = offsets.begin;
2378 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
2379 return 0;
2380}
f645cfa7 2381
b07cd987
MD
2382static
2383void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config *config,
2384 struct lttng_ust_lib_ring_buffer *buf,
2385 unsigned long commit_count,
2386 unsigned long idx,
2387 struct lttng_ust_shm_handle *handle)
2388{
15500a1b
MD
2389 struct commit_counters_hot *cc_hot;
2390
2391 if (config->oops != RING_BUFFER_OOPS_CONSISTENCY)
2392 return;
2393 cc_hot = shmp_index(handle, buf->commit_hot, idx);
2394 if (!cc_hot)
2395 return;
2396 v_set(config, &cc_hot->seq, commit_count);
b07cd987
MD
2397}
2398
9c995331
MD
2399/*
2400 * The ring buffer can count events recorded and overwritten per buffer,
2401 * but it is disabled by default due to its performance overhead.
2402 */
2403#ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2404static
2405void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config,
2406 struct lttng_ust_lib_ring_buffer *buf,
2407 unsigned long idx,
2408 struct lttng_ust_shm_handle *handle)
2409{
2410 v_add(config, subbuffer_get_records_count(config,
2411 &buf->backend, idx, handle),
2412 &buf->records_count);
2413 v_add(config, subbuffer_count_records_overrun(config,
2414 &buf->backend, idx, handle),
2415 &buf->records_overrun);
2416}
2417#else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2418static
2419void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config,
2420 struct lttng_ust_lib_ring_buffer *buf,
2421 unsigned long idx,
2422 struct lttng_ust_shm_handle *handle)
2423{
2424}
2425#endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2426
b07cd987
MD
2427void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config *config,
2428 struct lttng_ust_lib_ring_buffer *buf,
2429 struct channel *chan,
2430 unsigned long offset,
2431 unsigned long commit_count,
2432 unsigned long idx,
2433 struct lttng_ust_shm_handle *handle,
2434 uint64_t tsc)
2435{
2436 unsigned long old_commit_count = commit_count
2437 - chan->backend.subbuf_size;
15500a1b 2438 struct commit_counters_cold *cc_cold;
b07cd987
MD
2439
2440 /*
2441 * If we succeeded at updating cc_sb below, we are the subbuffer
2442 * writer delivering the subbuffer. Deals with concurrent
2443 * updates of the "cc" value without adding a add_return atomic
2444 * operation to the fast path.
2445 *
2446 * We are doing the delivery in two steps:
2447 * - First, we cmpxchg() cc_sb to the new value
2448 * old_commit_count + 1. This ensures that we are the only
2449 * subbuffer user successfully filling the subbuffer, but we
2450 * do _not_ set the cc_sb value to "commit_count" yet.
2451 * Therefore, other writers that would wrap around the ring
2452 * buffer and try to start writing to our subbuffer would
2453 * have to drop records, because it would appear as
2454 * non-filled.
2455 * We therefore have exclusive access to the subbuffer control
2456 * structures. This mutual exclusion with other writers is
2457 * crucially important to perform record overruns count in
2458 * flight recorder mode locklessly.
2459 * - When we are ready to release the subbuffer (either for
2460 * reading or for overrun by other writers), we simply set the
2461 * cc_sb value to "commit_count" and perform delivery.
2462 *
2463 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2464 * This guarantees that old_commit_count + 1 != commit_count.
2465 */
2466
2467 /*
2468 * Order prior updates to reserve count prior to the
2469 * commit_cold cc_sb update.
2470 */
2471 cmm_smp_wmb();
15500a1b
MD
2472 cc_cold = shmp_index(handle, buf->commit_cold, idx);
2473 if (!cc_cold)
2474 return;
2475 if (caa_likely(v_cmpxchg(config, &cc_cold->cc_sb,
b07cd987
MD
2476 old_commit_count, old_commit_count + 1)
2477 == old_commit_count)) {
6c737d05
MD
2478 uint64_t *ts_end;
2479
b07cd987
MD
2480 /*
2481 * Start of exclusive subbuffer access. We are
2482 * guaranteed to be the last writer in this subbuffer
2483 * and any other writer trying to access this subbuffer
2484 * in this state is required to drop records.
6c737d05
MD
2485 *
2486 * We can read the ts_end for the current sub-buffer
2487 * which has been saved by the very last space
2488 * reservation for the current sub-buffer.
2489 *
2490 * Order increment of commit counter before reading ts_end.
b07cd987 2491 */
6c737d05
MD
2492 cmm_smp_mb();
2493 ts_end = shmp_index(handle, buf->ts_end, idx);
2494 if (!ts_end)
2495 return;
9c995331 2496 deliver_count_events(config, buf, idx, handle);
6c737d05 2497 config->cb.buffer_end(buf, *ts_end, idx,
b07cd987
MD
2498 lib_ring_buffer_get_data_size(config,
2499 buf,
2500 idx,
2501 handle),
2502 handle);
2503
2504 /*
2505 * Increment the packet counter while we have exclusive
2506 * access.
2507 */
2508 subbuffer_inc_packet_count(config, &buf->backend, idx, handle);
2509
2510 /*
2511 * Set noref flag and offset for this subbuffer id.
2512 * Contains a memory barrier that ensures counter stores
2513 * are ordered before set noref and offset.
2514 */
2515 lib_ring_buffer_set_noref_offset(config, &buf->backend, idx,
2516 buf_trunc_val(offset, chan), handle);
2517
2518 /*
2519 * Order set_noref and record counter updates before the
2520 * end of subbuffer exclusive access. Orders with
2521 * respect to writers coming into the subbuffer after
2522 * wrap around, and also order wrt concurrent readers.
2523 */
2524 cmm_smp_mb();
2525 /* End of exclusive subbuffer access */
15500a1b 2526 v_set(config, &cc_cold->cc_sb, commit_count);
b07cd987
MD
2527 /*
2528 * Order later updates to reserve count after
2529 * the commit cold cc_sb update.
2530 */
2531 cmm_smp_wmb();
2532 lib_ring_buffer_vmcore_check_deliver(config, buf,
2533 commit_count, idx, handle);
2534
2535 /*
2536 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2537 */
2538 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER
2539 && uatomic_read(&buf->active_readers)
2540 && lib_ring_buffer_poll_deliver(config, buf, chan, handle)) {
2541 lib_ring_buffer_wakeup(buf, handle);
2542 }
2543 }
2544}
2545
f645cfa7
MD
2546/*
2547 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2548 */
2549void lttng_fixup_ringbuffer_tls(void)
2550{
8c90a710 2551 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting)));
f645cfa7 2552}
03d2d293
MD
2553
2554void lib_ringbuffer_signal_init(void)
2555{
2556 sigset_t mask;
2557 int ret;
2558
2559 /*
2560 * Block signal for entire process, so only our thread processes
2561 * it.
2562 */
2563 rb_setmask(&mask);
2564 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
2565 if (ret) {
2566 errno = ret;
2567 PERROR("pthread_sigmask");
2568 }
2569}
This page took 0.158162 seconds and 4 git commands to generate.