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