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