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