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