Fix: add missing poll.h include
[lttng-ust.git] / libringbuffer / ring_buffer_frontend.c
CommitLineData
852c2936
MD
1/*
2 * ring_buffer_frontend.c
3 *
e92f3e28
MD
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
852c2936
MD
20 *
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
23 *
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
27 *
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
32 *
33 * Author:
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
35 *
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
40 * And from K42 :
41 * Bob Wisniewski <bob@watson.ibm.com>
42 *
43 * Buffer reader semantic :
44 *
45 * - get_subbuf_size
46 * while buffer is not finalized and empty
47 * - get_subbuf
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
51 * - put_subbuf
852c2936
MD
52 */
53
5ad63a16 54#define _GNU_SOURCE
a6352fd4 55#include <sys/types.h>
431d5cf0
MD
56#include <sys/mman.h>
57#include <sys/stat.h>
03d2d293 58#include <unistd.h>
431d5cf0 59#include <fcntl.h>
03d2d293
MD
60#include <signal.h>
61#include <time.h>
14641deb 62#include <urcu/compiler.h>
a6352fd4 63#include <urcu/ref.h>
8c90a710 64#include <urcu/tls-compat.h>
b3bfefa2 65#include <poll.h>
35897f8b 66#include <helper.h>
14641deb 67
a6352fd4 68#include "smp.h"
4318ae1b 69#include <lttng/ringbuffer-config.h>
2fed87ae 70#include "vatomic.h"
4931a13e
MD
71#include "backend.h"
72#include "frontend.h"
a6352fd4 73#include "shm.h"
f645cfa7 74#include "tlsfixup.h"
bdcf8d82 75#include "../liblttng-ust/compat.h" /* For ENODATA */
852c2936 76
431d5cf0
MD
77#ifndef max
78#define max(a, b) ((a) > (b) ? (a) : (b))
79#endif
80
64493e4f
MD
81/* Print DBG() messages about events lost only every 1048576 hits */
82#define DBG_PRINT_NR_LOST (1UL << 20)
83
34a91bdb
MD
84#define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
85#define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
86#define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
03d2d293 87#define CLOCKID CLOCK_MONOTONIC
87edc338
MD
88#define LTTNG_UST_RING_BUFFER_GET_RETRY 10
89#define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
03d2d293 90
2432c3c9
MD
91/*
92 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
93 * close(2) to close the fd returned by shm_open.
94 * shm_unlink releases the shared memory object name.
95 * ftruncate(2) sets the size of the memory object.
96 * mmap/munmap maps the shared memory obj to a virtual address in the
97 * calling proceess (should be done both in libust and consumer).
98 * See shm_overview(7) for details.
99 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
100 * a UNIX socket.
101 *
102 * Since we don't need to access the object using its name, we can
103 * immediately shm_unlink(3) it, and only keep the handle with its file
104 * descriptor.
105 */
106
852c2936
MD
107/*
108 * Internal structure representing offsets to use at a sub-buffer switch.
109 */
110struct switch_offsets {
111 unsigned long begin, end, old;
112 size_t pre_header_padding, size;
1ad21f70
MD
113 unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1,
114 switch_old_end:1;
852c2936
MD
115};
116
8c90a710 117DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting);
852c2936 118
cb7378b3
MD
119/*
120 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
121 * close.
122 */
123static pthread_mutex_t wakeup_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
124
852c2936
MD
125static
126void lib_ring_buffer_print_errors(struct channel *chan,
009769ca
MD
127 struct lttng_ust_lib_ring_buffer *buf, int cpu,
128 struct lttng_ust_shm_handle *handle);
852c2936 129
03d2d293
MD
130/*
131 * Handle timer teardown race wrt memory free of private data by
132 * ring buffer signals are handled by a single thread, which permits
133 * a synchronization point between handling of each signal.
64bf51a6 134 * Protected by the lock within the structure.
03d2d293
MD
135 */
136struct timer_signal_data {
137 pthread_t tid; /* thread id managing signals */
138 int setup_done;
139 int qs_done;
64bf51a6 140 pthread_mutex_t lock;
03d2d293
MD
141};
142
64bf51a6
MD
143static struct timer_signal_data timer_signal = {
144 .tid = 0,
145 .setup_done = 0,
146 .qs_done = 0,
147 .lock = PTHREAD_MUTEX_INITIALIZER,
148};
03d2d293 149
852c2936
MD
150/**
151 * lib_ring_buffer_reset - Reset ring buffer to initial values.
152 * @buf: Ring buffer.
153 *
154 * Effectively empty the ring buffer. Should be called when the buffer is not
155 * used for writing. The ring buffer can be opened for reading, but the reader
156 * should not be using the iterator concurrently with reset. The previous
157 * current iterator record is reset.
158 */
4cfec15c 159void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 160 struct lttng_ust_shm_handle *handle)
852c2936 161{
1d498196 162 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 163 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
164 unsigned int i;
165
166 /*
167 * Reset iterator first. It will put the subbuffer if it currently holds
168 * it.
169 */
852c2936
MD
170 v_set(config, &buf->offset, 0);
171 for (i = 0; i < chan->backend.num_subbuf; i++) {
4746ae29
MD
172 v_set(config, &shmp_index(handle, buf->commit_hot, i)->cc, 0);
173 v_set(config, &shmp_index(handle, buf->commit_hot, i)->seq, 0);
174 v_set(config, &shmp_index(handle, buf->commit_cold, i)->cc_sb, 0);
852c2936 175 }
a6352fd4
MD
176 uatomic_set(&buf->consumed, 0);
177 uatomic_set(&buf->record_disabled, 0);
852c2936 178 v_set(config, &buf->last_tsc, 0);
1d498196 179 lib_ring_buffer_backend_reset(&buf->backend, handle);
852c2936
MD
180 /* Don't reset number of active readers */
181 v_set(config, &buf->records_lost_full, 0);
182 v_set(config, &buf->records_lost_wrap, 0);
183 v_set(config, &buf->records_lost_big, 0);
184 v_set(config, &buf->records_count, 0);
185 v_set(config, &buf->records_overrun, 0);
186 buf->finalized = 0;
187}
852c2936
MD
188
189/**
190 * channel_reset - Reset channel to initial values.
191 * @chan: Channel.
192 *
193 * Effectively empty the channel. Should be called when the channel is not used
194 * for writing. The channel can be opened for reading, but the reader should not
195 * be using the iterator concurrently with reset. The previous current iterator
196 * record is reset.
197 */
198void channel_reset(struct channel *chan)
199{
200 /*
201 * Reset iterators first. Will put the subbuffer if held for reading.
202 */
a6352fd4 203 uatomic_set(&chan->record_disabled, 0);
852c2936
MD
204 /* Don't reset commit_count_mask, still valid */
205 channel_backend_reset(&chan->backend);
206 /* Don't reset switch/read timer interval */
207 /* Don't reset notifiers and notifier enable bits */
208 /* Don't reset reader reference count */
209}
852c2936
MD
210
211/*
212 * Must be called under cpu hotplug protection.
213 */
4cfec15c 214int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf,
a6352fd4 215 struct channel_backend *chanb, int cpu,
38fae1d3 216 struct lttng_ust_shm_handle *handle,
1d498196 217 struct shm_object *shmobj)
852c2936 218{
4cfec15c 219 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
14641deb 220 struct channel *chan = caa_container_of(chanb, struct channel, backend);
a3f61e7f 221 void *priv = channel_get_private(chan);
852c2936 222 size_t subbuf_header_size;
2fed87ae 223 uint64_t tsc;
852c2936
MD
224 int ret;
225
226 /* Test for cpu hotplug */
227 if (buf->backend.allocated)
228 return 0;
229
a6352fd4 230 ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend,
1d498196 231 cpu, handle, shmobj);
852c2936
MD
232 if (ret)
233 return ret;
234
1d498196
MD
235 align_shm(shmobj, __alignof__(struct commit_counters_hot));
236 set_shmp(buf->commit_hot,
237 zalloc_shm(shmobj,
238 sizeof(struct commit_counters_hot) * chan->backend.num_subbuf));
239 if (!shmp(handle, buf->commit_hot)) {
852c2936
MD
240 ret = -ENOMEM;
241 goto free_chanbuf;
242 }
243
1d498196
MD
244 align_shm(shmobj, __alignof__(struct commit_counters_cold));
245 set_shmp(buf->commit_cold,
246 zalloc_shm(shmobj,
247 sizeof(struct commit_counters_cold) * chan->backend.num_subbuf));
248 if (!shmp(handle, buf->commit_cold)) {
852c2936
MD
249 ret = -ENOMEM;
250 goto free_commit;
251 }
252
852c2936
MD
253 /*
254 * Write the subbuffer header for first subbuffer so we know the total
255 * duration of data gathering.
256 */
257 subbuf_header_size = config->cb.subbuffer_header_size();
258 v_set(config, &buf->offset, subbuf_header_size);
4746ae29 259 subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id);
1d498196
MD
260 tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan));
261 config->cb.buffer_begin(buf, tsc, 0, handle);
4746ae29 262 v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc);
852c2936
MD
263
264 if (config->cb.buffer_create) {
1d498196 265 ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle);
852c2936
MD
266 if (ret)
267 goto free_init;
268 }
852c2936 269 buf->backend.allocated = 1;
852c2936
MD
270 return 0;
271
272 /* Error handling */
273free_init:
a6352fd4 274 /* commit_cold will be freed by shm teardown */
852c2936 275free_commit:
a6352fd4 276 /* commit_hot will be freed by shm teardown */
852c2936 277free_chanbuf:
852c2936
MD
278 return ret;
279}
280
03d2d293
MD
281static
282void lib_ring_buffer_channel_switch_timer(int sig, siginfo_t *si, void *uc)
852c2936 283{
03d2d293
MD
284 const struct lttng_ust_lib_ring_buffer_config *config;
285 struct lttng_ust_shm_handle *handle;
286 struct channel *chan;
287 int cpu;
288
289 assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self());
290
291 chan = si->si_value.sival_ptr;
292 handle = chan->handle;
293 config = &chan->backend.config;
852c2936 294
34a91bdb 295 DBG("Switch timer for channel %p\n", chan);
03d2d293 296
4dcd7e80
MD
297 /*
298 * Only flush buffers periodically if readers are active.
299 */
cb7378b3 300 pthread_mutex_lock(&wakeup_fd_mutex);
03d2d293
MD
301 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
302 for_each_possible_cpu(cpu) {
303 struct lttng_ust_lib_ring_buffer *buf =
304 shmp(handle, chan->backend.buf[cpu].shmp);
4dcd7e80
MD
305 if (uatomic_read(&buf->active_readers))
306 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
307 chan->handle);
03d2d293
MD
308 }
309 } else {
310 struct lttng_ust_lib_ring_buffer *buf =
311 shmp(handle, chan->backend.buf[0].shmp);
312
34a91bdb
MD
313 if (uatomic_read(&buf->active_readers))
314 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
315 chan->handle);
316 }
317 pthread_mutex_unlock(&wakeup_fd_mutex);
318 return;
319}
320
321static
322void lib_ring_buffer_channel_do_read(struct channel *chan)
323{
324 const struct lttng_ust_lib_ring_buffer_config *config;
325 struct lttng_ust_shm_handle *handle;
326 int cpu;
327
328 handle = chan->handle;
329 config = &chan->backend.config;
330
331 /*
332 * Only flush buffers periodically if readers are active.
333 */
334 pthread_mutex_lock(&wakeup_fd_mutex);
335 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
336 for_each_possible_cpu(cpu) {
337 struct lttng_ust_lib_ring_buffer *buf =
338 shmp(handle, chan->backend.buf[cpu].shmp);
339
340 if (uatomic_read(&buf->active_readers)
341 && lib_ring_buffer_poll_deliver(config, buf,
342 chan, handle)) {
343 lib_ring_buffer_wakeup(buf, handle);
344 }
345 }
346 } else {
347 struct lttng_ust_lib_ring_buffer *buf =
348 shmp(handle, chan->backend.buf[0].shmp);
349
350 if (uatomic_read(&buf->active_readers)
351 && lib_ring_buffer_poll_deliver(config, buf,
352 chan, handle)) {
353 lib_ring_buffer_wakeup(buf, handle);
354 }
03d2d293 355 }
cb7378b3 356 pthread_mutex_unlock(&wakeup_fd_mutex);
34a91bdb
MD
357}
358
359static
360void lib_ring_buffer_channel_read_timer(int sig, siginfo_t *si, void *uc)
361{
362 struct channel *chan;
363
364 assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self());
365 chan = si->si_value.sival_ptr;
366 DBG("Read timer for channel %p\n", chan);
367 lib_ring_buffer_channel_do_read(chan);
03d2d293
MD
368 return;
369}
370
371static
372void rb_setmask(sigset_t *mask)
373{
374 int ret;
375
376 ret = sigemptyset(mask);
377 if (ret) {
378 PERROR("sigemptyset");
379 }
34a91bdb
MD
380 ret = sigaddset(mask, LTTNG_UST_RB_SIG_FLUSH);
381 if (ret) {
382 PERROR("sigaddset");
383 }
384 ret = sigaddset(mask, LTTNG_UST_RB_SIG_READ);
03d2d293
MD
385 if (ret) {
386 PERROR("sigaddset");
387 }
388 ret = sigaddset(mask, LTTNG_UST_RB_SIG_TEARDOWN);
389 if (ret) {
390 PERROR("sigaddset");
391 }
392}
393
394static
395void *sig_thread(void *arg)
396{
397 sigset_t mask;
398 siginfo_t info;
399 int signr;
400
401 /* Only self thread will receive signal mask. */
402 rb_setmask(&mask);
403 CMM_STORE_SHARED(timer_signal.tid, pthread_self());
404
405 for (;;) {
406 signr = sigwaitinfo(&mask, &info);
407 if (signr == -1) {
34a91bdb
MD
408 if (errno != EINTR)
409 PERROR("sigwaitinfo");
03d2d293
MD
410 continue;
411 }
34a91bdb 412 if (signr == LTTNG_UST_RB_SIG_FLUSH) {
03d2d293
MD
413 lib_ring_buffer_channel_switch_timer(info.si_signo,
414 &info, NULL);
34a91bdb
MD
415 } else if (signr == LTTNG_UST_RB_SIG_READ) {
416 lib_ring_buffer_channel_read_timer(info.si_signo,
417 &info, NULL);
03d2d293
MD
418 } else if (signr == LTTNG_UST_RB_SIG_TEARDOWN) {
419 cmm_smp_mb();
420 CMM_STORE_SHARED(timer_signal.qs_done, 1);
421 cmm_smp_mb();
422 } else {
423 ERR("Unexptected signal %d\n", info.si_signo);
424 }
425 }
426 return NULL;
427}
428
429/*
03d2d293
MD
430 * Ensure only a single thread listens on the timer signal.
431 */
432static
433void lib_ring_buffer_setup_timer_thread(void)
434{
435 pthread_t thread;
436 int ret;
437
64bf51a6 438 pthread_mutex_lock(&timer_signal.lock);
03d2d293 439 if (timer_signal.setup_done)
64bf51a6 440 goto end;
03d2d293
MD
441
442 ret = pthread_create(&thread, NULL, &sig_thread, NULL);
443 if (ret) {
444 errno = ret;
445 PERROR("pthread_create");
446 }
447 ret = pthread_detach(thread);
448 if (ret) {
449 errno = ret;
450 PERROR("pthread_detach");
451 }
452 timer_signal.setup_done = 1;
64bf51a6
MD
453end:
454 pthread_mutex_unlock(&timer_signal.lock);
852c2936
MD
455}
456
03d2d293 457/*
64bf51a6 458 * Wait for signal-handling thread quiescent state.
03d2d293 459 */
64bf51a6
MD
460static
461void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr)
462{
463 sigset_t pending_set;
464 int ret;
465
466 /*
467 * We need to be the only thread interacting with the thread
468 * that manages signals for teardown synchronization.
469 */
470 pthread_mutex_lock(&timer_signal.lock);
471
472 /*
473 * Ensure we don't have any signal queued for this channel.
474 */
475 for (;;) {
476 ret = sigemptyset(&pending_set);
477 if (ret == -1) {
478 PERROR("sigemptyset");
479 }
480 ret = sigpending(&pending_set);
481 if (ret == -1) {
482 PERROR("sigpending");
483 }
484 if (!sigismember(&pending_set, signr))
485 break;
486 caa_cpu_relax();
487 }
488
489 /*
490 * From this point, no new signal handler will be fired that
491 * would try to access "chan". However, we still need to wait
492 * for any currently executing handler to complete.
493 */
494 cmm_smp_mb();
495 CMM_STORE_SHARED(timer_signal.qs_done, 0);
496 cmm_smp_mb();
497
498 /*
499 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
500 * thread wakes up.
501 */
502 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN);
503
504 while (!CMM_LOAD_SHARED(timer_signal.qs_done))
505 caa_cpu_relax();
506 cmm_smp_mb();
507
508 pthread_mutex_unlock(&timer_signal.lock);
509}
510
03d2d293
MD
511static
512void lib_ring_buffer_channel_switch_timer_start(struct channel *chan)
852c2936 513{
03d2d293
MD
514 struct sigevent sev;
515 struct itimerspec its;
516 int ret;
852c2936 517
03d2d293 518 if (!chan->switch_timer_interval || chan->switch_timer_enabled)
852c2936
MD
519 return;
520
03d2d293
MD
521 chan->switch_timer_enabled = 1;
522
523 lib_ring_buffer_setup_timer_thread();
524
525 sev.sigev_notify = SIGEV_SIGNAL;
34a91bdb 526 sev.sigev_signo = LTTNG_UST_RB_SIG_FLUSH;
03d2d293
MD
527 sev.sigev_value.sival_ptr = chan;
528 ret = timer_create(CLOCKID, &sev, &chan->switch_timer);
529 if (ret == -1) {
530 PERROR("timer_create");
531 }
532
533 its.it_value.tv_sec = chan->switch_timer_interval / 1000000;
534 its.it_value.tv_nsec = chan->switch_timer_interval % 1000000;
535 its.it_interval.tv_sec = its.it_value.tv_sec;
536 its.it_interval.tv_nsec = its.it_value.tv_nsec;
537
538 ret = timer_settime(chan->switch_timer, 0, &its, NULL);
539 if (ret == -1) {
540 PERROR("timer_settime");
541 }
542}
543
03d2d293
MD
544static
545void lib_ring_buffer_channel_switch_timer_stop(struct channel *chan)
546{
34a91bdb 547 int ret;
03d2d293
MD
548
549 if (!chan->switch_timer_interval || !chan->switch_timer_enabled)
550 return;
551
552 ret = timer_delete(chan->switch_timer);
553 if (ret == -1) {
554 PERROR("timer_delete");
555 }
556
64bf51a6 557 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH);
03d2d293
MD
558
559 chan->switch_timer = 0;
560 chan->switch_timer_enabled = 0;
852c2936
MD
561}
562
34a91bdb
MD
563static
564void lib_ring_buffer_channel_read_timer_start(struct channel *chan)
852c2936 565{
4cfec15c 566 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
34a91bdb
MD
567 struct sigevent sev;
568 struct itimerspec its;
569 int ret;
852c2936 570
34a91bdb
MD
571 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
572 || !chan->read_timer_interval || chan->read_timer_enabled)
573 return;
852c2936 574
34a91bdb 575 chan->read_timer_enabled = 1;
852c2936 576
34a91bdb 577 lib_ring_buffer_setup_timer_thread();
852c2936 578
34a91bdb
MD
579 sev.sigev_notify = SIGEV_SIGNAL;
580 sev.sigev_signo = LTTNG_UST_RB_SIG_READ;
581 sev.sigev_value.sival_ptr = chan;
582 ret = timer_create(CLOCKID, &sev, &chan->read_timer);
583 if (ret == -1) {
584 PERROR("timer_create");
585 }
852c2936 586
34a91bdb
MD
587 its.it_value.tv_sec = chan->read_timer_interval / 1000000;
588 its.it_value.tv_nsec = chan->read_timer_interval % 1000000;
589 its.it_interval.tv_sec = its.it_value.tv_sec;
590 its.it_interval.tv_nsec = its.it_value.tv_nsec;
852c2936 591
34a91bdb
MD
592 ret = timer_settime(chan->read_timer, 0, &its, NULL);
593 if (ret == -1) {
594 PERROR("timer_settime");
595 }
852c2936
MD
596}
597
34a91bdb
MD
598static
599void lib_ring_buffer_channel_read_timer_stop(struct channel *chan)
852c2936 600{
4cfec15c 601 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
34a91bdb 602 int ret;
852c2936
MD
603
604 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
34a91bdb 605 || !chan->read_timer_interval || !chan->read_timer_enabled)
852c2936
MD
606 return;
607
34a91bdb
MD
608 ret = timer_delete(chan->read_timer);
609 if (ret == -1) {
610 PERROR("timer_delete");
611 }
612
852c2936
MD
613 /*
614 * do one more check to catch data that has been written in the last
615 * timer period.
616 */
34a91bdb
MD
617 lib_ring_buffer_channel_do_read(chan);
618
64bf51a6 619 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ);
34a91bdb
MD
620
621 chan->read_timer = 0;
622 chan->read_timer_enabled = 0;
852c2936
MD
623}
624
1d498196 625static void channel_unregister_notifiers(struct channel *chan,
38fae1d3 626 struct lttng_ust_shm_handle *handle)
852c2936 627{
03d2d293 628 lib_ring_buffer_channel_switch_timer_stop(chan);
34a91bdb 629 lib_ring_buffer_channel_read_timer_stop(chan);
852c2936
MD
630}
631
009769ca
MD
632static void channel_print_errors(struct channel *chan,
633 struct lttng_ust_shm_handle *handle)
634{
635 const struct lttng_ust_lib_ring_buffer_config *config =
636 &chan->backend.config;
637 int cpu;
638
639 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
640 for_each_possible_cpu(cpu) {
641 struct lttng_ust_lib_ring_buffer *buf =
642 shmp(handle, chan->backend.buf[cpu].shmp);
643 lib_ring_buffer_print_errors(chan, buf, cpu, handle);
644 }
645 } else {
646 struct lttng_ust_lib_ring_buffer *buf =
647 shmp(handle, chan->backend.buf[0].shmp);
648
649 lib_ring_buffer_print_errors(chan, buf, -1, handle);
650 }
651}
652
653static void channel_free(struct channel *chan,
654 struct lttng_ust_shm_handle *handle)
852c2936 655{
74d81a6c 656 channel_backend_free(&chan->backend, handle);
431d5cf0 657 /* chan is freed by shm teardown */
1d498196
MD
658 shm_object_table_destroy(handle->table);
659 free(handle);
852c2936
MD
660}
661
662/**
663 * channel_create - Create channel.
664 * @config: ring buffer instance configuration
665 * @name: name of the channel
a3f61e7f
MD
666 * @priv_data: ring buffer client private data area pointer (output)
667 * @priv_data_size: length, in bytes, of the private data area.
d028eddb 668 * @priv_data_init: initialization data for private data.
852c2936
MD
669 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
670 * address mapping. It is used only by RING_BUFFER_STATIC
671 * configuration. It can be set to NULL for other backends.
672 * @subbuf_size: subbuffer size
673 * @num_subbuf: number of subbuffers
674 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
675 * padding to let readers get those sub-buffers.
676 * Used for live streaming.
677 * @read_timer_interval: Time interval (in us) to wake up pending readers.
678 *
679 * Holds cpu hotplug.
680 * Returns NULL on failure.
681 */
4cfec15c 682struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f
MD
683 const char *name,
684 void **priv_data,
685 size_t priv_data_align,
686 size_t priv_data_size,
d028eddb 687 void *priv_data_init,
a3f61e7f 688 void *buf_addr, size_t subbuf_size,
852c2936 689 size_t num_subbuf, unsigned int switch_timer_interval,
74d81a6c 690 unsigned int read_timer_interval)
852c2936 691{
24622edc 692 int ret;
a3f61e7f 693 size_t shmsize, chansize;
852c2936 694 struct channel *chan;
38fae1d3 695 struct lttng_ust_shm_handle *handle;
1d498196 696 struct shm_object *shmobj;
74d81a6c
MD
697 unsigned int nr_streams;
698
699 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
700 nr_streams = num_possible_cpus();
701 else
702 nr_streams = 1;
852c2936
MD
703
704 if (lib_ring_buffer_check_config(config, switch_timer_interval,
705 read_timer_interval))
706 return NULL;
707
38fae1d3 708 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
431d5cf0
MD
709 if (!handle)
710 return NULL;
711
1d498196
MD
712 /* Allocate table for channel + per-cpu buffers */
713 handle->table = shm_object_table_create(1 + num_possible_cpus());
714 if (!handle->table)
715 goto error_table_alloc;
852c2936 716
1d498196
MD
717 /* Calculate the shm allocation layout */
718 shmsize = sizeof(struct channel);
c1fca457 719 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp));
74d81a6c 720 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams;
a3f61e7f 721 chansize = shmsize;
74d81a6c
MD
722 if (priv_data_align)
723 shmsize += offset_align(shmsize, priv_data_align);
a3f61e7f 724 shmsize += priv_data_size;
a6352fd4 725
74d81a6c
MD
726 /* Allocate normal memory for channel (not shared) */
727 shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM);
b5a14697
MD
728 if (!shmobj)
729 goto error_append;
57773204 730 /* struct channel is at object 0, offset 0 (hardcoded) */
a3f61e7f 731 set_shmp(handle->chan, zalloc_shm(shmobj, chansize));
57773204
MD
732 assert(handle->chan._ref.index == 0);
733 assert(handle->chan._ref.offset == 0);
1d498196 734 chan = shmp(handle, handle->chan);
a6352fd4 735 if (!chan)
1d498196 736 goto error_append;
74d81a6c 737 chan->nr_streams = nr_streams;
a6352fd4 738
a3f61e7f
MD
739 /* space for private data */
740 if (priv_data_size) {
741 DECLARE_SHMP(void, priv_data_alloc);
742
743 align_shm(shmobj, priv_data_align);
744 chan->priv_data_offset = shmobj->allocated_len;
745 set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size));
746 if (!shmp(handle, priv_data_alloc))
747 goto error_append;
748 *priv_data = channel_get_private(chan);
d028eddb 749 memcpy(*priv_data, priv_data_init, priv_data_size);
a3f61e7f
MD
750 } else {
751 chan->priv_data_offset = -1;
74d81a6c
MD
752 if (priv_data)
753 *priv_data = NULL;
a3f61e7f
MD
754 }
755
756 ret = channel_backend_init(&chan->backend, name, config,
1d498196 757 subbuf_size, num_subbuf, handle);
852c2936 758 if (ret)
1d498196 759 goto error_backend_init;
852c2936 760
03d2d293 761 chan->handle = handle;
852c2936 762 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
852c2936 763
34a91bdb
MD
764 chan->switch_timer_interval = switch_timer_interval;
765 chan->read_timer_interval = read_timer_interval;
03d2d293 766 lib_ring_buffer_channel_switch_timer_start(chan);
34a91bdb 767 lib_ring_buffer_channel_read_timer_start(chan);
852c2936 768
431d5cf0 769 return handle;
852c2936 770
1d498196
MD
771error_backend_init:
772error_append:
773 shm_object_table_destroy(handle->table);
774error_table_alloc:
431d5cf0 775 free(handle);
852c2936
MD
776 return NULL;
777}
852c2936 778
74d81a6c 779struct lttng_ust_shm_handle *channel_handle_create(void *data,
ff0f5728
MD
780 uint64_t memory_map_size,
781 int wakeup_fd)
193183fb 782{
38fae1d3 783 struct lttng_ust_shm_handle *handle;
193183fb
MD
784 struct shm_object *object;
785
38fae1d3 786 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
193183fb
MD
787 if (!handle)
788 return NULL;
789
790 /* Allocate table for channel + per-cpu buffers */
791 handle->table = shm_object_table_create(1 + num_possible_cpus());
792 if (!handle->table)
793 goto error_table_alloc;
794 /* Add channel object */
74d81a6c 795 object = shm_object_table_append_mem(handle->table, data,
ff0f5728 796 memory_map_size, wakeup_fd);
193183fb
MD
797 if (!object)
798 goto error_table_object;
57773204
MD
799 /* struct channel is at object 0, offset 0 (hardcoded) */
800 handle->chan._ref.index = 0;
801 handle->chan._ref.offset = 0;
193183fb
MD
802 return handle;
803
804error_table_object:
805 shm_object_table_destroy(handle->table);
806error_table_alloc:
807 free(handle);
808 return NULL;
809}
810
38fae1d3 811int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
74d81a6c
MD
812 int shm_fd, int wakeup_fd, uint32_t stream_nr,
813 uint64_t memory_map_size)
193183fb
MD
814{
815 struct shm_object *object;
816
817 /* Add stream object */
74d81a6c
MD
818 object = shm_object_table_append_shm(handle->table,
819 shm_fd, wakeup_fd, stream_nr,
820 memory_map_size);
193183fb 821 if (!object)
74d81a6c 822 return -EINVAL;
193183fb
MD
823 return 0;
824}
825
74d81a6c
MD
826unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle)
827{
828 assert(handle->table);
829 return handle->table->allocated_len - 1;
830}
831
852c2936 832static
74d81a6c 833void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle)
852c2936 834{
74d81a6c 835 channel_free(chan, handle);
852c2936
MD
836}
837
838/**
839 * channel_destroy - Finalize, wait for q.s. and destroy channel.
840 * @chan: channel to destroy
841 *
842 * Holds cpu hotplug.
431d5cf0
MD
843 * Call "destroy" callback, finalize channels, decrement the channel
844 * reference count. Note that when readers have completed data
845 * consumption of finalized channels, get_subbuf() will return -ENODATA.
a3f61e7f 846 * They should release their handle at that point.
852c2936 847 */
a3f61e7f 848void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
74d81a6c 849 int consumer)
852c2936 850{
74d81a6c
MD
851 if (consumer) {
852 /*
853 * Note: the consumer takes care of finalizing and
854 * switching the buffers.
855 */
856 channel_unregister_notifiers(chan, handle);
3d0ef9f6
MD
857 /*
858 * The consumer prints errors.
859 */
860 channel_print_errors(chan, handle);
824f40b8
MD
861 }
862
431d5cf0
MD
863 /*
864 * sessiond/consumer are keeping a reference on the shm file
865 * descriptor directly. No need to refcount.
866 */
74d81a6c 867 channel_release(chan, handle);
a3f61e7f 868 return;
852c2936 869}
852c2936 870
4cfec15c
MD
871struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
872 const struct lttng_ust_lib_ring_buffer_config *config,
1d498196 873 struct channel *chan, int cpu,
38fae1d3 874 struct lttng_ust_shm_handle *handle,
74d81a6c
MD
875 int *shm_fd, int *wait_fd,
876 int *wakeup_fd,
877 uint64_t *memory_map_size)
852c2936 878{
381c0f1e
MD
879 struct shm_ref *ref;
880
881 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
74d81a6c 882 cpu = 0;
381c0f1e 883 } else {
e095d803
MD
884 if (cpu >= num_possible_cpus())
885 return NULL;
381c0f1e 886 }
74d81a6c
MD
887 ref = &chan->backend.buf[cpu].shmp._ref;
888 *shm_fd = shm_get_shm_fd(handle, ref);
889 *wait_fd = shm_get_wait_fd(handle, ref);
890 *wakeup_fd = shm_get_wakeup_fd(handle, ref);
891 if (shm_get_shm_size(handle, ref, memory_map_size))
892 return NULL;
893 return shmp(handle, chan->backend.buf[cpu].shmp);
852c2936 894}
852c2936 895
ff0f5728
MD
896int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
897 struct channel *chan,
898 struct lttng_ust_shm_handle *handle)
899{
900 struct shm_ref *ref;
901
902 ref = &handle->chan._ref;
903 return shm_close_wait_fd(handle, ref);
904}
905
906int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
907 struct channel *chan,
908 struct lttng_ust_shm_handle *handle)
909{
910 struct shm_ref *ref;
911
912 ref = &handle->chan._ref;
913 return shm_close_wakeup_fd(handle, ref);
914}
915
916int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
917 struct channel *chan,
918 struct lttng_ust_shm_handle *handle,
919 int cpu)
852c2936 920{
74d81a6c
MD
921 struct shm_ref *ref;
922
923 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
924 cpu = 0;
925 } else {
926 if (cpu >= num_possible_cpus())
927 return -EINVAL;
824f40b8 928 }
74d81a6c
MD
929 ref = &chan->backend.buf[cpu].shmp._ref;
930 return shm_close_wait_fd(handle, ref);
931}
932
ff0f5728 933int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
934 struct channel *chan,
935 struct lttng_ust_shm_handle *handle,
936 int cpu)
937{
938 struct shm_ref *ref;
cb7378b3 939 int ret;
74d81a6c
MD
940
941 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
942 cpu = 0;
943 } else {
944 if (cpu >= num_possible_cpus())
945 return -EINVAL;
946 }
947 ref = &chan->backend.buf[cpu].shmp._ref;
cb7378b3
MD
948 pthread_mutex_lock(&wakeup_fd_mutex);
949 ret = shm_close_wakeup_fd(handle, ref);
950 pthread_mutex_unlock(&wakeup_fd_mutex);
951 return ret;
74d81a6c
MD
952}
953
954int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
955 struct lttng_ust_shm_handle *handle)
956{
a6352fd4 957 if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0)
852c2936 958 return -EBUSY;
a6352fd4 959 cmm_smp_mb();
852c2936
MD
960 return 0;
961}
852c2936 962
4cfec15c 963void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
74d81a6c 964 struct lttng_ust_shm_handle *handle)
852c2936 965{
1d498196 966 struct channel *chan = shmp(handle, buf->backend.chan);
852c2936 967
a6352fd4
MD
968 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
969 cmm_smp_mb();
970 uatomic_dec(&buf->active_readers);
852c2936
MD
971}
972
973/**
974 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
975 * @buf: ring buffer
976 * @consumed: consumed count indicating the position where to read
977 * @produced: produced count, indicates position when to stop reading
978 *
979 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
980 * data to read at consumed position, or 0 if the get operation succeeds.
852c2936
MD
981 */
982
4cfec15c 983int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
1d498196 984 unsigned long *consumed, unsigned long *produced,
38fae1d3 985 struct lttng_ust_shm_handle *handle)
852c2936 986{
1d498196 987 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 988 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
989 unsigned long consumed_cur, write_offset;
990 int finalized;
991
14641deb 992 finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
993 /*
994 * Read finalized before counters.
995 */
a6352fd4
MD
996 cmm_smp_rmb();
997 consumed_cur = uatomic_read(&buf->consumed);
852c2936
MD
998 /*
999 * No need to issue a memory barrier between consumed count read and
1000 * write offset read, because consumed count can only change
1001 * concurrently in overwrite mode, and we keep a sequence counter
1002 * identifier derived from the write offset to check we are getting
1003 * the same sub-buffer we are expecting (the sub-buffers are atomically
1004 * "tagged" upon writes, tags are checked upon read).
1005 */
1006 write_offset = v_read(config, &buf->offset);
1007
1008 /*
1009 * Check that we are not about to read the same subbuffer in
1010 * which the writer head is.
1011 */
1012 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1013 == 0)
1014 goto nodata;
1015
1016 *consumed = consumed_cur;
1017 *produced = subbuf_trunc(write_offset, chan);
1018
1019 return 0;
1020
1021nodata:
1022 /*
1023 * The memory barriers __wait_event()/wake_up_interruptible() take care
1024 * of "raw_spin_is_locked" memory ordering.
1025 */
1026 if (finalized)
1027 return -ENODATA;
852c2936
MD
1028 else
1029 return -EAGAIN;
1030}
852c2936
MD
1031
1032/**
d1a996f5 1033 * lib_ring_buffer_move_consumer - move consumed counter forward
852c2936
MD
1034 * @buf: ring buffer
1035 * @consumed_new: new consumed count value
1036 */
4cfec15c 1037void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1d498196 1038 unsigned long consumed_new,
38fae1d3 1039 struct lttng_ust_shm_handle *handle)
852c2936 1040{
4cfec15c 1041 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1d498196 1042 struct channel *chan = shmp(handle, bufb->chan);
852c2936
MD
1043 unsigned long consumed;
1044
74d81a6c 1045 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
852c2936
MD
1046
1047 /*
1048 * Only push the consumed value forward.
1049 * If the consumed cmpxchg fails, this is because we have been pushed by
1050 * the writer in flight recorder mode.
1051 */
a6352fd4 1052 consumed = uatomic_read(&buf->consumed);
852c2936 1053 while ((long) consumed - (long) consumed_new < 0)
a6352fd4
MD
1054 consumed = uatomic_cmpxchg(&buf->consumed, consumed,
1055 consumed_new);
852c2936 1056}
852c2936
MD
1057
1058/**
1059 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1060 * @buf: ring buffer
1061 * @consumed: consumed count indicating the position where to read
1062 *
1063 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1064 * data to read at consumed position, or 0 if the get operation succeeds.
852c2936 1065 */
4cfec15c 1066int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1d498196 1067 unsigned long consumed,
38fae1d3 1068 struct lttng_ust_shm_handle *handle)
852c2936 1069{
1d498196 1070 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 1071 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936 1072 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
87edc338 1073 int ret, finalized, nr_retry = LTTNG_UST_RING_BUFFER_GET_RETRY;
852c2936
MD
1074
1075retry:
14641deb 1076 finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
1077 /*
1078 * Read finalized before counters.
1079 */
a6352fd4
MD
1080 cmm_smp_rmb();
1081 consumed_cur = uatomic_read(&buf->consumed);
852c2936 1082 consumed_idx = subbuf_index(consumed, chan);
4746ae29 1083 commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb);
852c2936
MD
1084 /*
1085 * Make sure we read the commit count before reading the buffer
1086 * data and the write offset. Correct consumed offset ordering
1087 * wrt commit count is insured by the use of cmpxchg to update
1088 * the consumed offset.
852c2936 1089 */
a6352fd4
MD
1090 /*
1091 * Local rmb to match the remote wmb to read the commit count
1092 * before the buffer data and the write offset.
1093 */
1094 cmm_smp_rmb();
852c2936
MD
1095
1096 write_offset = v_read(config, &buf->offset);
1097
1098 /*
1099 * Check that the buffer we are getting is after or at consumed_cur
1100 * position.
1101 */
1102 if ((long) subbuf_trunc(consumed, chan)
1103 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1104 goto nodata;
1105
1106 /*
1107 * Check that the subbuffer we are trying to consume has been
87edc338
MD
1108 * already fully committed. There are a few causes that can make
1109 * this unavailability situation occur:
1110 *
1111 * Temporary (short-term) situation:
1112 * - Application is running on a different CPU, between reserve
1113 * and commit ring buffer operations,
1114 * - Application is preempted between reserve and commit ring
1115 * buffer operations,
1116 *
1117 * Long-term situation:
1118 * - Application is stopped (SIGSTOP) between reserve and commit
1119 * ring buffer operations. Could eventually be resumed by
1120 * SIGCONT.
1121 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1122 * reserve and commit ring buffer operation.
1123 *
1124 * From a consumer perspective, handling short-term
1125 * unavailability situations is performed by retrying a few
1126 * times after a delay. Handling long-term unavailability
1127 * situations is handled by failing to get the sub-buffer.
1128 *
1129 * In all of those situations, if the application is taking a
1130 * long time to perform its commit after ring buffer space
1131 * reservation, we can end up in a situation where the producer
1132 * will fill the ring buffer and try to write into the same
1133 * sub-buffer again (which has a missing commit). This is
1134 * handled by the producer in the sub-buffer switch handling
1135 * code of the reserve routine by detecting unbalanced
1136 * reserve/commit counters and discarding all further events
1137 * until the situation is resolved in those situations. Two
1138 * scenarios can occur:
1139 *
1140 * 1) The application causing the reserve/commit counters to be
1141 * unbalanced has been terminated. In this situation, all
1142 * further events will be discarded in the buffers, and no
1143 * further buffer data will be readable by the consumer
1144 * daemon. Tearing down the UST tracing session and starting
1145 * anew is a work-around for those situations. Note that this
1146 * only affects per-UID tracing. In per-PID tracing, the
1147 * application vanishes with the termination, and therefore
1148 * no more data needs to be written to the buffers.
1149 * 2) The application causing the unbalance has been delayed for
1150 * a long time, but will eventually try to increment the
1151 * commit counter after eventually writing to the sub-buffer.
1152 * This situation can cause events to be discarded until the
1153 * application resumes its operations.
852c2936
MD
1154 */
1155 if (((commit_count - chan->backend.subbuf_size)
1156 & chan->commit_count_mask)
0bade047 1157 - (buf_trunc(consumed, chan)
852c2936 1158 >> chan->backend.num_subbuf_order)
87edc338
MD
1159 != 0) {
1160 if (nr_retry-- > 0) {
1161 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1162 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1163 goto retry;
1164 } else {
1165 goto nodata;
1166 }
1167 }
852c2936
MD
1168
1169 /*
1170 * Check that we are not about to read the same subbuffer in
1171 * which the writer head is.
1172 */
0bade047 1173 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan)
852c2936
MD
1174 == 0)
1175 goto nodata;
1176
1177 /*
1178 * Failure to get the subbuffer causes a busy-loop retry without going
1179 * to a wait queue. These are caused by short-lived race windows where
1180 * the writer is getting access to a subbuffer we were trying to get
1181 * access to. Also checks that the "consumed" buffer count we are
1182 * looking for matches the one contained in the subbuffer id.
87edc338
MD
1183 *
1184 * The short-lived race window described here can be affected by
1185 * application signals and preemption, thus requiring to bound
1186 * the loop to a maximum number of retry.
852c2936
MD
1187 */
1188 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1d498196
MD
1189 consumed_idx, buf_trunc_val(consumed, chan),
1190 handle);
87edc338
MD
1191 if (ret) {
1192 if (nr_retry-- > 0) {
1193 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1194 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1195 goto retry;
1196 } else {
1197 goto nodata;
1198 }
1199 }
852c2936
MD
1200 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1201
1202 buf->get_subbuf_consumed = consumed;
1203 buf->get_subbuf = 1;
1204
1205 return 0;
1206
1207nodata:
1208 /*
1209 * The memory barriers __wait_event()/wake_up_interruptible() take care
1210 * of "raw_spin_is_locked" memory ordering.
1211 */
1212 if (finalized)
1213 return -ENODATA;
852c2936
MD
1214 else
1215 return -EAGAIN;
1216}
852c2936
MD
1217
1218/**
1219 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1220 * @buf: ring buffer
1221 */
4cfec15c 1222void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 1223 struct lttng_ust_shm_handle *handle)
852c2936 1224{
4cfec15c 1225 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1d498196 1226 struct channel *chan = shmp(handle, bufb->chan);
4cfec15c 1227 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1228 unsigned long read_sb_bindex, consumed_idx, consumed;
1229
74d81a6c 1230 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
852c2936
MD
1231
1232 if (!buf->get_subbuf) {
1233 /*
1234 * Reader puts a subbuffer it did not get.
1235 */
1236 CHAN_WARN_ON(chan, 1);
1237 return;
1238 }
1239 consumed = buf->get_subbuf_consumed;
1240 buf->get_subbuf = 0;
1241
1242 /*
1243 * Clear the records_unread counter. (overruns counter)
1244 * Can still be non-zero if a file reader simply grabbed the data
1245 * without using iterators.
1246 * Can be below zero if an iterator is used on a snapshot more than
1247 * once.
1248 */
1249 read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1250 v_add(config, v_read(config,
4746ae29 1251 &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread),
852c2936 1252 &bufb->records_read);
4746ae29 1253 v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0);
852c2936
MD
1254 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1255 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1256 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1257
1258 /*
1259 * Exchange the reader subbuffer with the one we put in its place in the
1260 * writer subbuffer table. Expect the original consumed count. If
1261 * update_read_sb_index fails, this is because the writer updated the
1262 * subbuffer concurrently. We should therefore keep the subbuffer we
1263 * currently have: it has become invalid to try reading this sub-buffer
1264 * consumed count value anyway.
1265 */
1266 consumed_idx = subbuf_index(consumed, chan);
1267 update_read_sb_index(config, &buf->backend, &chan->backend,
1d498196
MD
1268 consumed_idx, buf_trunc_val(consumed, chan),
1269 handle);
852c2936
MD
1270 /*
1271 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1272 * if the writer concurrently updated it.
1273 */
1274}
852c2936
MD
1275
1276/*
1277 * cons_offset is an iterator on all subbuffer offsets between the reader
1278 * position and the writer position. (inclusive)
1279 */
1280static
4cfec15c 1281void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1282 struct channel *chan,
1283 unsigned long cons_offset,
1d498196 1284 int cpu,
38fae1d3 1285 struct lttng_ust_shm_handle *handle)
852c2936 1286{
4cfec15c 1287 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1288 unsigned long cons_idx, commit_count, commit_count_sb;
1289
1290 cons_idx = subbuf_index(cons_offset, chan);
4746ae29
MD
1291 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc);
1292 commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb);
852c2936
MD
1293
1294 if (subbuf_offset(commit_count, chan) != 0)
4d3c9523 1295 DBG("ring buffer %s, cpu %d: "
852c2936
MD
1296 "commit count in subbuffer %lu,\n"
1297 "expecting multiples of %lu bytes\n"
1298 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1299 chan->backend.name, cpu, cons_idx,
1300 chan->backend.subbuf_size,
1301 commit_count, commit_count_sb);
1302
4d3c9523 1303 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
852c2936
MD
1304 chan->backend.name, cpu, commit_count);
1305}
1306
1307static
4cfec15c 1308void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf,
852c2936 1309 struct channel *chan,
1d498196 1310 void *priv, int cpu,
38fae1d3 1311 struct lttng_ust_shm_handle *handle)
852c2936 1312{
4cfec15c 1313 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1314 unsigned long write_offset, cons_offset;
1315
852c2936
MD
1316 /*
1317 * No need to order commit_count, write_offset and cons_offset reads
1318 * because we execute at teardown when no more writer nor reader
1319 * references are left.
1320 */
1321 write_offset = v_read(config, &buf->offset);
a6352fd4 1322 cons_offset = uatomic_read(&buf->consumed);
852c2936 1323 if (write_offset != cons_offset)
4d3c9523 1324 DBG("ring buffer %s, cpu %d: "
852c2936
MD
1325 "non-consumed data\n"
1326 " [ %lu bytes written, %lu bytes read ]\n",
1327 chan->backend.name, cpu, write_offset, cons_offset);
1328
a6352fd4 1329 for (cons_offset = uatomic_read(&buf->consumed);
852c2936
MD
1330 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1331 chan)
1332 - cons_offset) > 0;
1333 cons_offset = subbuf_align(cons_offset, chan))
1334 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1d498196 1335 cpu, handle);
852c2936
MD
1336}
1337
1338static
1339void lib_ring_buffer_print_errors(struct channel *chan,
009769ca
MD
1340 struct lttng_ust_lib_ring_buffer *buf, int cpu,
1341 struct lttng_ust_shm_handle *handle)
852c2936 1342{
4cfec15c 1343 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
a3f61e7f 1344 void *priv = channel_get_private(chan);
852c2936 1345
a1360615
MD
1346 if (!strcmp(chan->backend.name, "relay-metadata-mmap")) {
1347 DBG("ring buffer %s: %lu records written, "
1348 "%lu records overrun\n",
1349 chan->backend.name,
1350 v_read(config, &buf->records_count),
1351 v_read(config, &buf->records_overrun));
1352 } else {
1353 DBG("ring buffer %s, cpu %d: %lu records written, "
1354 "%lu records overrun\n",
1355 chan->backend.name, cpu,
1356 v_read(config, &buf->records_count),
1357 v_read(config, &buf->records_overrun));
1358
1359 if (v_read(config, &buf->records_lost_full)
1360 || v_read(config, &buf->records_lost_wrap)
1361 || v_read(config, &buf->records_lost_big))
1362 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1363 " [ %lu buffer full, %lu nest buffer wrap-around, "
1364 "%lu event too big ]\n",
1365 chan->backend.name, cpu,
1366 v_read(config, &buf->records_lost_full),
1367 v_read(config, &buf->records_lost_wrap),
1368 v_read(config, &buf->records_lost_big));
1369 }
1d498196 1370 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle);
852c2936
MD
1371}
1372
1373/*
1374 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1375 *
1376 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1377 */
1378static
4cfec15c 1379void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1380 struct channel *chan,
1381 struct switch_offsets *offsets,
2fed87ae 1382 uint64_t tsc,
38fae1d3 1383 struct lttng_ust_shm_handle *handle)
852c2936 1384{
4cfec15c 1385 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1386 unsigned long oldidx = subbuf_index(offsets->old, chan);
1387 unsigned long commit_count;
1388
1d498196 1389 config->cb.buffer_begin(buf, tsc, oldidx, handle);
852c2936
MD
1390
1391 /*
1392 * Order all writes to buffer before the commit count update that will
1393 * determine that the subbuffer is full.
1394 */
a6352fd4 1395 cmm_smp_wmb();
852c2936 1396 v_add(config, config->cb.subbuffer_header_size(),
4746ae29
MD
1397 &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1398 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
852c2936
MD
1399 /* Check if the written buffer has to be delivered */
1400 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1b7b0501 1401 commit_count, oldidx, handle, tsc);
852c2936 1402 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
80249235
MD
1403 offsets->old + config->cb.subbuffer_header_size(),
1404 commit_count, handle);
852c2936
MD
1405}
1406
1407/*
1408 * lib_ring_buffer_switch_old_end: switch old subbuffer
1409 *
1410 * Note : offset_old should never be 0 here. It is ok, because we never perform
1411 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1412 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1413 * subbuffer.
1414 */
1415static
4cfec15c 1416void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1417 struct channel *chan,
1418 struct switch_offsets *offsets,
2fed87ae 1419 uint64_t tsc,
38fae1d3 1420 struct lttng_ust_shm_handle *handle)
852c2936 1421{
4cfec15c 1422 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1423 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1424 unsigned long commit_count, padding_size, data_size;
1425
1426 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1427 padding_size = chan->backend.subbuf_size - data_size;
1d498196
MD
1428 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size,
1429 handle);
852c2936
MD
1430
1431 /*
1432 * Order all writes to buffer before the commit count update that will
1433 * determine that the subbuffer is full.
1434 */
a6352fd4 1435 cmm_smp_wmb();
4746ae29
MD
1436 v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1437 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
852c2936 1438 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1b7b0501 1439 commit_count, oldidx, handle, tsc);
852c2936 1440 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
80249235 1441 offsets->old + padding_size, commit_count, handle);
852c2936
MD
1442}
1443
1444/*
1445 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1446 *
1447 * This code can be executed unordered : writers may already have written to the
1448 * sub-buffer before this code gets executed, caution. The commit makes sure
1449 * that this code is executed before the deliver of this sub-buffer.
1450 */
1451static
4cfec15c 1452void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1453 struct channel *chan,
1454 struct switch_offsets *offsets,
2fed87ae 1455 uint64_t tsc,
38fae1d3 1456 struct lttng_ust_shm_handle *handle)
852c2936 1457{
4cfec15c 1458 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1459 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1460 unsigned long commit_count;
1461
1d498196 1462 config->cb.buffer_begin(buf, tsc, beginidx, handle);
852c2936
MD
1463
1464 /*
1465 * Order all writes to buffer before the commit count update that will
1466 * determine that the subbuffer is full.
1467 */
a6352fd4 1468 cmm_smp_wmb();
852c2936 1469 v_add(config, config->cb.subbuffer_header_size(),
4746ae29
MD
1470 &shmp_index(handle, buf->commit_hot, beginidx)->cc);
1471 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc);
852c2936
MD
1472 /* Check if the written buffer has to be delivered */
1473 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1b7b0501 1474 commit_count, beginidx, handle, tsc);
852c2936 1475 lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx,
80249235
MD
1476 offsets->begin + config->cb.subbuffer_header_size(),
1477 commit_count, handle);
852c2936
MD
1478}
1479
1ad21f70
MD
1480/*
1481 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1482 *
3962118d
MD
1483 * Calls subbuffer_set_data_size() to set the data size of the current
1484 * sub-buffer. We do not need to perform check_deliver nor commit here,
1485 * since this task will be done by the "commit" of the event for which
1486 * we are currently doing the space reservation.
1ad21f70
MD
1487 */
1488static
1489void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf,
1490 struct channel *chan,
1491 struct switch_offsets *offsets,
1492 uint64_t tsc,
1493 struct lttng_ust_shm_handle *handle)
1494{
1495 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
3962118d 1496 unsigned long endidx, data_size;
1ad21f70 1497
3962118d 1498 endidx = subbuf_index(offsets->end - 1, chan);
1ad21f70 1499 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1ad21f70
MD
1500 subbuffer_set_data_size(config, &buf->backend, endidx, data_size,
1501 handle);
1ad21f70
MD
1502}
1503
852c2936
MD
1504/*
1505 * Returns :
1506 * 0 if ok
1507 * !0 if execution must be aborted.
1508 */
1509static
1510int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
4cfec15c 1511 struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1512 struct channel *chan,
1513 struct switch_offsets *offsets,
2dac206d
MD
1514 uint64_t *tsc,
1515 struct lttng_ust_shm_handle *handle)
852c2936 1516{
4cfec15c 1517 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
2dac206d 1518 unsigned long off, reserve_commit_diff;
852c2936
MD
1519
1520 offsets->begin = v_read(config, &buf->offset);
1521 offsets->old = offsets->begin;
1522 offsets->switch_old_start = 0;
1523 off = subbuf_offset(offsets->begin, chan);
1524
1525 *tsc = config->cb.ring_buffer_clock_read(chan);
1526
1527 /*
1528 * Ensure we flush the header of an empty subbuffer when doing the
1529 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1530 * total data gathering duration even if there were no records saved
1531 * after the last buffer switch.
1532 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1533 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1534 * subbuffer header as appropriate.
1535 * The next record that reserves space will be responsible for
1536 * populating the following subbuffer header. We choose not to populate
1537 * the next subbuffer header here because we want to be able to use
a6352fd4
MD
1538 * SWITCH_ACTIVE for periodical buffer flush, which must
1539 * guarantee that all the buffer content (records and header
1540 * timestamps) are visible to the reader. This is required for
1541 * quiescence guarantees for the fusion merge.
852c2936 1542 */
2dac206d
MD
1543 if (mode != SWITCH_FLUSH && !off)
1544 return -1; /* we do not have to switch : buffer is empty */
1545
1546 if (caa_unlikely(off == 0)) {
1547 unsigned long sb_index, commit_count;
1548
1549 /*
1550 * We are performing a SWITCH_FLUSH. At this stage, there are no
1551 * concurrent writes into the buffer.
1552 *
1553 * The client does not save any header information. Don't
1554 * switch empty subbuffer on finalize, because it is invalid to
1555 * deliver a completely empty subbuffer.
1556 */
1557 if (!config->cb.subbuffer_header_size())
1558 return -1;
1559
1560 /* Test new buffer integrity */
1561 sb_index = subbuf_index(offsets->begin, chan);
1562 commit_count = v_read(config,
1563 &shmp_index(handle, buf->commit_cold,
1564 sb_index)->cc_sb);
1565 reserve_commit_diff =
1566 (buf_trunc(offsets->begin, chan)
1567 >> chan->backend.num_subbuf_order)
1568 - (commit_count & chan->commit_count_mask);
1569 if (caa_likely(reserve_commit_diff == 0)) {
1570 /* Next subbuffer not being written to. */
1571 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1572 subbuf_trunc(offsets->begin, chan)
1573 - subbuf_trunc((unsigned long)
1574 uatomic_read(&buf->consumed), chan)
1575 >= chan->backend.buf_size)) {
1576 /*
1577 * We do not overwrite non consumed buffers
1578 * and we are full : don't switch.
1579 */
852c2936 1580 return -1;
2dac206d
MD
1581 } else {
1582 /*
1583 * Next subbuffer not being written to, and we
1584 * are either in overwrite mode or the buffer is
1585 * not full. It's safe to write in this new
1586 * subbuffer.
1587 */
1588 }
1589 } else {
852c2936 1590 /*
2dac206d
MD
1591 * Next subbuffer reserve offset does not match the
1592 * commit offset. Don't perform switch in
1593 * producer-consumer and overwrite mode. Caused by
1594 * either a writer OOPS or too many nested writes over a
1595 * reserve/commit pair.
852c2936 1596 */
2dac206d 1597 return -1;
852c2936 1598 }
2dac206d
MD
1599
1600 /*
1601 * Need to write the subbuffer start header on finalize.
1602 */
1603 offsets->switch_old_start = 1;
1604 }
1605 offsets->begin = subbuf_align(offsets->begin, chan);
852c2936
MD
1606 /* Note: old points to the next subbuf at offset 0 */
1607 offsets->end = offsets->begin;
1608 return 0;
1609}
1610
1611/*
1612 * Force a sub-buffer switch. This operation is completely reentrant : can be
1613 * called while tracing is active with absolutely no lock held.
1614 *
1615 * Note, however, that as a v_cmpxchg is used for some atomic
1616 * operations, this function must be called from the CPU which owns the buffer
1617 * for a ACTIVE flush.
1618 */
4cfec15c 1619void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode,
38fae1d3 1620 struct lttng_ust_shm_handle *handle)
852c2936 1621{
1d498196 1622 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 1623 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1624 struct switch_offsets offsets;
1625 unsigned long oldidx;
2fed87ae 1626 uint64_t tsc;
852c2936
MD
1627
1628 offsets.size = 0;
1629
1630 /*
1631 * Perform retryable operations.
1632 */
1633 do {
1634 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
2dac206d 1635 &tsc, handle))
852c2936
MD
1636 return; /* Switch not needed */
1637 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
1638 != offsets.old);
1639
1640 /*
1641 * Atomically update last_tsc. This update races against concurrent
1642 * atomic updates, but the race will always cause supplementary full TSC
1643 * records, never the opposite (missing a full TSC record when it would
1644 * be needed).
1645 */
1646 save_last_tsc(config, buf, tsc);
1647
1648 /*
1649 * Push the reader if necessary
1650 */
1651 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
1652
1653 oldidx = subbuf_index(offsets.old, chan);
1d498196 1654 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle);
852c2936
MD
1655
1656 /*
1657 * May need to populate header start on SWITCH_FLUSH.
1658 */
1659 if (offsets.switch_old_start) {
1d498196 1660 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle);
852c2936
MD
1661 offsets.old += config->cb.subbuffer_header_size();
1662 }
1663
1664 /*
1665 * Switch old subbuffer.
1666 */
1d498196 1667 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle);
852c2936 1668}
852c2936
MD
1669
1670/*
1671 * Returns :
1672 * 0 if ok
1673 * -ENOSPC if event size is too large for packet.
1674 * -ENOBUFS if there is currently not enough space in buffer for the event.
1675 * -EIO if data cannot be written into the buffer for any other reason.
1676 */
1677static
4cfec15c 1678int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1679 struct channel *chan,
1680 struct switch_offsets *offsets,
4cfec15c 1681 struct lttng_ust_lib_ring_buffer_ctx *ctx)
852c2936 1682{
4cfec15c 1683 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
38fae1d3 1684 struct lttng_ust_shm_handle *handle = ctx->handle;
e185cf4b 1685 unsigned long reserve_commit_diff, offset_cmp;
852c2936 1686
e185cf4b
MD
1687retry:
1688 offsets->begin = offset_cmp = v_read(config, &buf->offset);
852c2936
MD
1689 offsets->old = offsets->begin;
1690 offsets->switch_new_start = 0;
1ad21f70 1691 offsets->switch_new_end = 0;
852c2936
MD
1692 offsets->switch_old_end = 0;
1693 offsets->pre_header_padding = 0;
1694
1695 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
1696 if ((int64_t) ctx->tsc == -EIO)
1697 return -EIO;
1698
1699 if (last_tsc_overflow(config, buf, ctx->tsc))
1700 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
1701
b5a3dfa5 1702 if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
852c2936
MD
1703 offsets->switch_new_start = 1; /* For offsets->begin */
1704 } else {
1705 offsets->size = config->cb.record_header_size(config, chan,
1706 offsets->begin,
1707 &offsets->pre_header_padding,
1708 ctx);
1709 offsets->size +=
1710 lib_ring_buffer_align(offsets->begin + offsets->size,
1711 ctx->largest_align)
1712 + ctx->data_size;
b5a3dfa5 1713 if (caa_unlikely(subbuf_offset(offsets->begin, chan) +
852c2936
MD
1714 offsets->size > chan->backend.subbuf_size)) {
1715 offsets->switch_old_end = 1; /* For offsets->old */
1716 offsets->switch_new_start = 1; /* For offsets->begin */
1717 }
1718 }
b5a3dfa5 1719 if (caa_unlikely(offsets->switch_new_start)) {
e185cf4b 1720 unsigned long sb_index, commit_count;
852c2936
MD
1721
1722 /*
1723 * We are typically not filling the previous buffer completely.
1724 */
b5a3dfa5 1725 if (caa_likely(offsets->switch_old_end))
852c2936
MD
1726 offsets->begin = subbuf_align(offsets->begin, chan);
1727 offsets->begin = offsets->begin
1728 + config->cb.subbuffer_header_size();
1729 /* Test new buffer integrity */
1730 sb_index = subbuf_index(offsets->begin, chan);
e185cf4b
MD
1731 /*
1732 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
1733 * lib_ring_buffer_check_deliver() has the matching
1734 * memory barriers required around commit_cold cc_sb
1735 * updates to ensure reserve and commit counter updates
1736 * are not seen reordered when updated by another CPU.
1737 */
1738 cmm_smp_rmb();
1739 commit_count = v_read(config,
1740 &shmp_index(handle, buf->commit_cold,
1741 sb_index)->cc_sb);
1742 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
1743 cmm_smp_rmb();
1744 if (caa_unlikely(offset_cmp != v_read(config, &buf->offset))) {
1745 /*
1746 * The reserve counter have been concurrently updated
1747 * while we read the commit counter. This means the
1748 * commit counter we read might not match buf->offset
1749 * due to concurrent update. We therefore need to retry.
1750 */
1751 goto retry;
1752 }
852c2936
MD
1753 reserve_commit_diff =
1754 (buf_trunc(offsets->begin, chan)
1755 >> chan->backend.num_subbuf_order)
e185cf4b 1756 - (commit_count & chan->commit_count_mask);
b5a3dfa5 1757 if (caa_likely(reserve_commit_diff == 0)) {
852c2936 1758 /* Next subbuffer not being written to. */
b5a3dfa5 1759 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
852c2936
MD
1760 subbuf_trunc(offsets->begin, chan)
1761 - subbuf_trunc((unsigned long)
a6352fd4 1762 uatomic_read(&buf->consumed), chan)
852c2936 1763 >= chan->backend.buf_size)) {
64493e4f
MD
1764 unsigned long nr_lost;
1765
852c2936
MD
1766 /*
1767 * We do not overwrite non consumed buffers
1768 * and we are full : record is lost.
1769 */
64493e4f 1770 nr_lost = v_read(config, &buf->records_lost_full);
852c2936 1771 v_inc(config, &buf->records_lost_full);
64493e4f
MD
1772 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
1773 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1774 nr_lost + 1, chan->backend.name,
1775 buf->backend.cpu);
1776 }
852c2936
MD
1777 return -ENOBUFS;
1778 } else {
1779 /*
1780 * Next subbuffer not being written to, and we
1781 * are either in overwrite mode or the buffer is
1782 * not full. It's safe to write in this new
1783 * subbuffer.
1784 */
1785 }
1786 } else {
64493e4f
MD
1787 unsigned long nr_lost;
1788
852c2936
MD
1789 /*
1790 * Next subbuffer reserve offset does not match the
e185cf4b
MD
1791 * commit offset, and this did not involve update to the
1792 * reserve counter. Drop record in producer-consumer and
852c2936
MD
1793 * overwrite mode. Caused by either a writer OOPS or too
1794 * many nested writes over a reserve/commit pair.
1795 */
64493e4f 1796 nr_lost = v_read(config, &buf->records_lost_wrap);
852c2936 1797 v_inc(config, &buf->records_lost_wrap);
64493e4f
MD
1798 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
1799 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1800 nr_lost + 1, chan->backend.name,
1801 buf->backend.cpu);
1802 }
852c2936
MD
1803 return -EIO;
1804 }
1805 offsets->size =
1806 config->cb.record_header_size(config, chan,
1807 offsets->begin,
1808 &offsets->pre_header_padding,
1809 ctx);
1810 offsets->size +=
1811 lib_ring_buffer_align(offsets->begin + offsets->size,
1812 ctx->largest_align)
1813 + ctx->data_size;
b5a3dfa5 1814 if (caa_unlikely(subbuf_offset(offsets->begin, chan)
852c2936 1815 + offsets->size > chan->backend.subbuf_size)) {
64493e4f
MD
1816 unsigned long nr_lost;
1817
852c2936
MD
1818 /*
1819 * Record too big for subbuffers, report error, don't
1820 * complete the sub-buffer switch.
1821 */
64493e4f 1822 nr_lost = v_read(config, &buf->records_lost_big);
852c2936 1823 v_inc(config, &buf->records_lost_big);
64493e4f
MD
1824 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
1825 DBG("%lu or more records lost in (%s:%d) record size "
1826 " of %zu bytes is too large for buffer\n",
1827 nr_lost + 1, chan->backend.name,
1828 buf->backend.cpu, offsets->size);
1829 }
852c2936
MD
1830 return -ENOSPC;
1831 } else {
1832 /*
1833 * We just made a successful buffer switch and the
1834 * record fits in the new subbuffer. Let's write.
1835 */
1836 }
1837 } else {
1838 /*
1839 * Record fits in the current buffer and we are not on a switch
1840 * boundary. It's safe to write.
1841 */
1842 }
1843 offsets->end = offsets->begin + offsets->size;
1ad21f70
MD
1844
1845 if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) {
1846 /*
1847 * The offset_end will fall at the very beginning of the next
1848 * subbuffer.
1849 */
1850 offsets->switch_new_end = 1; /* For offsets->begin */
1851 }
852c2936
MD
1852 return 0;
1853}
1854
1855/**
1856 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1857 * @ctx: ring buffer context.
1858 *
1859 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1860 * -EIO for other errors, else returns 0.
1861 * It will take care of sub-buffer switching.
1862 */
4cfec15c 1863int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx)
852c2936
MD
1864{
1865 struct channel *chan = ctx->chan;
38fae1d3 1866 struct lttng_ust_shm_handle *handle = ctx->handle;
4cfec15c
MD
1867 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1868 struct lttng_ust_lib_ring_buffer *buf;
852c2936
MD
1869 struct switch_offsets offsets;
1870 int ret;
1871
1872 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
1d498196 1873 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
852c2936 1874 else
1d498196 1875 buf = shmp(handle, chan->backend.buf[0].shmp);
852c2936
MD
1876 ctx->buf = buf;
1877
1878 offsets.size = 0;
1879
1880 do {
1881 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
1882 ctx);
b5a3dfa5 1883 if (caa_unlikely(ret))
852c2936 1884 return ret;
b5a3dfa5 1885 } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
852c2936
MD
1886 offsets.end)
1887 != offsets.old));
1888
1889 /*
1890 * Atomically update last_tsc. This update races against concurrent
1891 * atomic updates, but the race will always cause supplementary full TSC
1892 * records, never the opposite (missing a full TSC record when it would
1893 * be needed).
1894 */
1895 save_last_tsc(config, buf, ctx->tsc);
1896
1897 /*
1898 * Push the reader if necessary
1899 */
1900 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
1901
1902 /*
1903 * Clear noref flag for this subbuffer.
1904 */
1905 lib_ring_buffer_clear_noref(config, &buf->backend,
1d498196
MD
1906 subbuf_index(offsets.end - 1, chan),
1907 handle);
852c2936
MD
1908
1909 /*
1910 * Switch old subbuffer if needed.
1911 */
b5a3dfa5 1912 if (caa_unlikely(offsets.switch_old_end)) {
852c2936 1913 lib_ring_buffer_clear_noref(config, &buf->backend,
1d498196
MD
1914 subbuf_index(offsets.old - 1, chan),
1915 handle);
1916 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle);
852c2936
MD
1917 }
1918
1919 /*
1920 * Populate new subbuffer.
1921 */
b5a3dfa5 1922 if (caa_unlikely(offsets.switch_new_start))
1d498196 1923 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle);
852c2936 1924
1ad21f70
MD
1925 if (caa_unlikely(offsets.switch_new_end))
1926 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle);
1927
852c2936
MD
1928 ctx->slot_size = offsets.size;
1929 ctx->pre_offset = offsets.begin;
1930 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
1931 return 0;
1932}
f645cfa7
MD
1933
1934/*
1935 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1936 */
1937void lttng_fixup_ringbuffer_tls(void)
1938{
8c90a710 1939 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting)));
f645cfa7 1940}
03d2d293
MD
1941
1942void lib_ringbuffer_signal_init(void)
1943{
1944 sigset_t mask;
1945 int ret;
1946
1947 /*
1948 * Block signal for entire process, so only our thread processes
1949 * it.
1950 */
1951 rb_setmask(&mask);
1952 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
1953 if (ret) {
1954 errno = ret;
1955 PERROR("pthread_sigmask");
1956 }
1957}
This page took 0.127429 seconds and 4 git commands to generate.