Rename "tsc" to "timestamp"
[lttng-ust.git] / libringbuffer / ring_buffer_frontend.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
7 * recorder (overwrite) modes. See thesis:
8 *
9 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
10 * dissertation, Ecole Polytechnique de Montreal.
11 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
12 *
13 * - Algorithm presentation in Chapter 5:
14 * "Lockless Multi-Core High-Throughput Buffering".
15 * - Algorithm formal verification in Section 8.6:
16 * "Formal verification of LTTng"
17 *
18 * Author:
19 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
20 *
21 * Inspired from LTT and RelayFS:
22 * Karim Yaghmour <karim@opersys.com>
23 * Tom Zanussi <zanussi@us.ibm.com>
24 * Bob Wisniewski <bob@watson.ibm.com>
25 * And from K42 :
26 * Bob Wisniewski <bob@watson.ibm.com>
27 *
28 * Buffer reader semantic :
29 *
30 * - get_subbuf_size
31 * while buffer is not finalized and empty
32 * - get_subbuf
33 * - if return value != 0, continue
34 * - splice one subbuffer worth of data to a pipe
35 * - splice the data from pipe to disk/network
36 * - put_subbuf
37 */
38
39 #define _LGPL_SOURCE
40 #include <sys/types.h>
41 #include <sys/mman.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <signal.h>
46 #include <time.h>
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include <urcu/compiler.h>
50 #include <urcu/ref.h>
51 #include <urcu/tls-compat.h>
52 #include <poll.h>
53 #include <ust-helper.h>
54
55 #include <lttng/align.h>
56 #include "smp.h"
57 #include <lttng/ringbuffer-config.h>
58 #include "vatomic.h"
59 #include "backend.h"
60 #include "frontend.h"
61 #include "shm.h"
62 #include "rb-init.h"
63 #include "../liblttng-ust/compat.h" /* For ENODATA */
64
65 /* Print DBG() messages about events lost only every 1048576 hits */
66 #define DBG_PRINT_NR_LOST (1UL << 20)
67
68 #define LTTNG_UST_RB_SIG_FLUSH SIGRTMIN
69 #define LTTNG_UST_RB_SIG_READ SIGRTMIN + 1
70 #define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 2
71 #define CLOCKID CLOCK_MONOTONIC
72 #define LTTNG_UST_RING_BUFFER_GET_RETRY 10
73 #define LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS 10
74 #define RETRY_DELAY_MS 100 /* 100 ms. */
75
76 /*
77 * Non-static to ensure the compiler does not optimize away the xor.
78 */
79 uint8_t lttng_crash_magic_xor[] = RB_CRASH_DUMP_ABI_MAGIC_XOR;
80
81 /*
82 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
83 * close(2) to close the fd returned by shm_open.
84 * shm_unlink releases the shared memory object name.
85 * ftruncate(2) sets the size of the memory object.
86 * mmap/munmap maps the shared memory obj to a virtual address in the
87 * calling proceess (should be done both in libust and consumer).
88 * See shm_overview(7) for details.
89 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
90 * a UNIX socket.
91 *
92 * Since we don't need to access the object using its name, we can
93 * immediately shm_unlink(3) it, and only keep the handle with its file
94 * descriptor.
95 */
96
97 /*
98 * Internal structure representing offsets to use at a sub-buffer switch.
99 */
100 struct switch_offsets {
101 unsigned long begin, end, old;
102 size_t pre_header_padding, size;
103 unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1,
104 switch_old_end:1;
105 };
106
107 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting);
108
109 /*
110 * wakeup_fd_mutex protects wakeup fd use by timer from concurrent
111 * close.
112 */
113 static pthread_mutex_t wakeup_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
114
115 static
116 void lib_ring_buffer_print_errors(struct channel *chan,
117 struct lttng_ust_lib_ring_buffer *buf, int cpu,
118 struct lttng_ust_shm_handle *handle);
119
120 /*
121 * Handle timer teardown race wrt memory free of private data by
122 * ring buffer signals are handled by a single thread, which permits
123 * a synchronization point between handling of each signal.
124 * Protected by the lock within the structure.
125 */
126 struct timer_signal_data {
127 pthread_t tid; /* thread id managing signals */
128 int setup_done;
129 int qs_done;
130 pthread_mutex_t lock;
131 };
132
133 static struct timer_signal_data timer_signal = {
134 .tid = 0,
135 .setup_done = 0,
136 .qs_done = 0,
137 .lock = PTHREAD_MUTEX_INITIALIZER,
138 };
139
140 static bool lttng_ust_allow_blocking;
141
142 void lttng_ust_ringbuffer_set_allow_blocking(void)
143 {
144 lttng_ust_allow_blocking = true;
145 }
146
147 /* Get blocking timeout, in ms */
148 static int lttng_ust_ringbuffer_get_timeout(struct channel *chan)
149 {
150 if (!lttng_ust_allow_blocking)
151 return 0;
152 return chan->u.s.blocking_timeout_ms;
153 }
154
155 /**
156 * lib_ring_buffer_reset - Reset ring buffer to initial values.
157 * @buf: Ring buffer.
158 *
159 * Effectively empty the ring buffer. Should be called when the buffer is not
160 * used for writing. The ring buffer can be opened for reading, but the reader
161 * should not be using the iterator concurrently with reset. The previous
162 * current iterator record is reset.
163 */
164 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
165 struct lttng_ust_shm_handle *handle)
166 {
167 struct channel *chan;
168 const struct lttng_ust_lib_ring_buffer_config *config;
169 unsigned int i;
170
171 chan = shmp(handle, buf->backend.chan);
172 if (!chan)
173 return;
174 config = &chan->backend.config;
175 /*
176 * Reset iterator first. It will put the subbuffer if it currently holds
177 * it.
178 */
179 v_set(config, &buf->offset, 0);
180 for (i = 0; i < chan->backend.num_subbuf; i++) {
181 struct commit_counters_hot *cc_hot;
182 struct commit_counters_cold *cc_cold;
183 uint64_t *ts_end;
184
185 cc_hot = shmp_index(handle, buf->commit_hot, i);
186 if (!cc_hot)
187 return;
188 cc_cold = shmp_index(handle, buf->commit_cold, i);
189 if (!cc_cold)
190 return;
191 ts_end = shmp_index(handle, buf->ts_end, i);
192 if (!ts_end)
193 return;
194 v_set(config, &cc_hot->cc, 0);
195 v_set(config, &cc_hot->seq, 0);
196 v_set(config, &cc_cold->cc_sb, 0);
197 *ts_end = 0;
198 }
199 uatomic_set(&buf->consumed, 0);
200 uatomic_set(&buf->record_disabled, 0);
201 v_set(config, &buf->last_tsc, 0);
202 lib_ring_buffer_backend_reset(&buf->backend, handle);
203 /* Don't reset number of active readers */
204 v_set(config, &buf->records_lost_full, 0);
205 v_set(config, &buf->records_lost_wrap, 0);
206 v_set(config, &buf->records_lost_big, 0);
207 v_set(config, &buf->records_count, 0);
208 v_set(config, &buf->records_overrun, 0);
209 buf->finalized = 0;
210 }
211
212 /**
213 * channel_reset - Reset channel to initial values.
214 * @chan: Channel.
215 *
216 * Effectively empty the channel. Should be called when the channel is not used
217 * for writing. The channel can be opened for reading, but the reader should not
218 * be using the iterator concurrently with reset. The previous current iterator
219 * record is reset.
220 */
221 void channel_reset(struct channel *chan)
222 {
223 /*
224 * Reset iterators first. Will put the subbuffer if held for reading.
225 */
226 uatomic_set(&chan->record_disabled, 0);
227 /* Don't reset commit_count_mask, still valid */
228 channel_backend_reset(&chan->backend);
229 /* Don't reset switch/read timer interval */
230 /* Don't reset notifiers and notifier enable bits */
231 /* Don't reset reader reference count */
232 }
233
234 static
235 void init_crash_abi(const struct lttng_ust_lib_ring_buffer_config *config,
236 struct lttng_crash_abi *crash_abi,
237 struct lttng_ust_lib_ring_buffer *buf,
238 struct channel_backend *chanb,
239 struct shm_object *shmobj,
240 struct lttng_ust_shm_handle *handle)
241 {
242 int i;
243
244 for (i = 0; i < RB_CRASH_DUMP_ABI_MAGIC_LEN; i++)
245 crash_abi->magic[i] = lttng_crash_magic_xor[i] ^ 0xFF;
246 crash_abi->mmap_length = shmobj->memory_map_size;
247 crash_abi->endian = RB_CRASH_ENDIAN;
248 crash_abi->major = RB_CRASH_DUMP_ABI_MAJOR;
249 crash_abi->minor = RB_CRASH_DUMP_ABI_MINOR;
250 crash_abi->word_size = sizeof(unsigned long);
251 crash_abi->layout_type = LTTNG_CRASH_TYPE_UST;
252
253 /* Offset of fields */
254 crash_abi->offset.prod_offset =
255 (uint32_t) ((char *) &buf->offset - (char *) buf);
256 crash_abi->offset.consumed_offset =
257 (uint32_t) ((char *) &buf->consumed - (char *) buf);
258 crash_abi->offset.commit_hot_array =
259 (uint32_t) ((char *) shmp(handle, buf->commit_hot) - (char *) buf);
260 crash_abi->offset.commit_hot_seq =
261 offsetof(struct commit_counters_hot, seq);
262 crash_abi->offset.buf_wsb_array =
263 (uint32_t) ((char *) shmp(handle, buf->backend.buf_wsb) - (char *) buf);
264 crash_abi->offset.buf_wsb_id =
265 offsetof(struct lttng_ust_lib_ring_buffer_backend_subbuffer, id);
266 crash_abi->offset.sb_array =
267 (uint32_t) ((char *) shmp(handle, buf->backend.array) - (char *) buf);
268 crash_abi->offset.sb_array_shmp_offset =
269 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp,
270 shmp._ref.offset);
271 crash_abi->offset.sb_backend_p_offset =
272 offsetof(struct lttng_ust_lib_ring_buffer_backend_pages,
273 p._ref.offset);
274
275 /* Field length */
276 crash_abi->length.prod_offset = sizeof(buf->offset);
277 crash_abi->length.consumed_offset = sizeof(buf->consumed);
278 crash_abi->length.commit_hot_seq =
279 sizeof(((struct commit_counters_hot *) NULL)->seq);
280 crash_abi->length.buf_wsb_id =
281 sizeof(((struct lttng_ust_lib_ring_buffer_backend_subbuffer *) NULL)->id);
282 crash_abi->length.sb_array_shmp_offset =
283 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages_shmp *) NULL)->shmp._ref.offset);
284 crash_abi->length.sb_backend_p_offset =
285 sizeof(((struct lttng_ust_lib_ring_buffer_backend_pages *) NULL)->p._ref.offset);
286
287 /* Array stride */
288 crash_abi->stride.commit_hot_array =
289 sizeof(struct commit_counters_hot);
290 crash_abi->stride.buf_wsb_array =
291 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer);
292 crash_abi->stride.sb_array =
293 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp);
294
295 /* Buffer constants */
296 crash_abi->buf_size = chanb->buf_size;
297 crash_abi->subbuf_size = chanb->subbuf_size;
298 crash_abi->num_subbuf = chanb->num_subbuf;
299 crash_abi->mode = (uint32_t) chanb->config.mode;
300
301 if (config->cb.content_size_field) {
302 size_t offset, length;
303
304 config->cb.content_size_field(config, &offset, &length);
305 crash_abi->offset.content_size = offset;
306 crash_abi->length.content_size = length;
307 } else {
308 crash_abi->offset.content_size = 0;
309 crash_abi->length.content_size = 0;
310 }
311 if (config->cb.packet_size_field) {
312 size_t offset, length;
313
314 config->cb.packet_size_field(config, &offset, &length);
315 crash_abi->offset.packet_size = offset;
316 crash_abi->length.packet_size = length;
317 } else {
318 crash_abi->offset.packet_size = 0;
319 crash_abi->length.packet_size = 0;
320 }
321 }
322
323 /*
324 * Must be called under cpu hotplug protection.
325 */
326 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf,
327 struct channel_backend *chanb, int cpu,
328 struct lttng_ust_shm_handle *handle,
329 struct shm_object *shmobj)
330 {
331 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
332 struct channel *chan = caa_container_of(chanb, struct channel, backend);
333 struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb;
334 struct channel *shmp_chan;
335 struct commit_counters_hot *cc_hot;
336 void *priv = channel_get_private(chan);
337 size_t subbuf_header_size;
338 uint64_t tsc;
339 int ret;
340
341 /* Test for cpu hotplug */
342 if (buf->backend.allocated)
343 return 0;
344
345 align_shm(shmobj, __alignof__(struct commit_counters_hot));
346 set_shmp(buf->commit_hot,
347 zalloc_shm(shmobj,
348 sizeof(struct commit_counters_hot) * chan->backend.num_subbuf));
349 if (!shmp(handle, buf->commit_hot)) {
350 return -ENOMEM;
351 }
352
353 align_shm(shmobj, __alignof__(struct commit_counters_cold));
354 set_shmp(buf->commit_cold,
355 zalloc_shm(shmobj,
356 sizeof(struct commit_counters_cold) * chan->backend.num_subbuf));
357 if (!shmp(handle, buf->commit_cold)) {
358 ret = -ENOMEM;
359 goto free_commit;
360 }
361
362 align_shm(shmobj, __alignof__(uint64_t));
363 set_shmp(buf->ts_end,
364 zalloc_shm(shmobj,
365 sizeof(uint64_t) * chan->backend.num_subbuf));
366 if (!shmp(handle, buf->ts_end)) {
367 ret = -ENOMEM;
368 goto free_commit_cold;
369 }
370
371
372 ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend,
373 cpu, handle, shmobj);
374 if (ret) {
375 goto free_init;
376 }
377
378 /*
379 * Write the subbuffer header for first subbuffer so we know the total
380 * duration of data gathering.
381 */
382 subbuf_header_size = config->cb.subbuffer_header_size();
383 v_set(config, &buf->offset, subbuf_header_size);
384 wsb = shmp_index(handle, buf->backend.buf_wsb, 0);
385 if (!wsb) {
386 ret = -EPERM;
387 goto free_chanbuf;
388 }
389 subbuffer_id_clear_noref(config, &wsb->id);
390 shmp_chan = shmp(handle, buf->backend.chan);
391 if (!shmp_chan) {
392 ret = -EPERM;
393 goto free_chanbuf;
394 }
395 tsc = config->cb.ring_buffer_clock_read(shmp_chan);
396 config->cb.buffer_begin(buf, tsc, 0, handle);
397 cc_hot = shmp_index(handle, buf->commit_hot, 0);
398 if (!cc_hot) {
399 ret = -EPERM;
400 goto free_chanbuf;
401 }
402 v_add(config, subbuf_header_size, &cc_hot->cc);
403 v_add(config, subbuf_header_size, &cc_hot->seq);
404
405 if (config->cb.buffer_create) {
406 ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle);
407 if (ret)
408 goto free_chanbuf;
409 }
410
411 init_crash_abi(config, &buf->crash_abi, buf, chanb, shmobj, handle);
412
413 buf->backend.allocated = 1;
414 return 0;
415
416 /* Error handling */
417 free_init:
418 /* ts_end will be freed by shm teardown */
419 free_commit_cold:
420 /* commit_cold will be freed by shm teardown */
421 free_commit:
422 /* commit_hot will be freed by shm teardown */
423 free_chanbuf:
424 return ret;
425 }
426
427 static
428 void lib_ring_buffer_channel_switch_timer(int sig, siginfo_t *si, void *uc)
429 {
430 const struct lttng_ust_lib_ring_buffer_config *config;
431 struct lttng_ust_shm_handle *handle;
432 struct channel *chan;
433 int cpu;
434
435 assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self());
436
437 chan = si->si_value.sival_ptr;
438 handle = chan->handle;
439 config = &chan->backend.config;
440
441 DBG("Switch timer for channel %p\n", chan);
442
443 /*
444 * Only flush buffers periodically if readers are active.
445 */
446 pthread_mutex_lock(&wakeup_fd_mutex);
447 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
448 for_each_possible_cpu(cpu) {
449 struct lttng_ust_lib_ring_buffer *buf =
450 shmp(handle, chan->backend.buf[cpu].shmp);
451
452 if (!buf)
453 goto end;
454 if (uatomic_read(&buf->active_readers))
455 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
456 chan->handle);
457 }
458 } else {
459 struct lttng_ust_lib_ring_buffer *buf =
460 shmp(handle, chan->backend.buf[0].shmp);
461
462 if (!buf)
463 goto end;
464 if (uatomic_read(&buf->active_readers))
465 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE,
466 chan->handle);
467 }
468 end:
469 pthread_mutex_unlock(&wakeup_fd_mutex);
470 return;
471 }
472
473 static
474 int lib_ring_buffer_poll_deliver(const struct lttng_ust_lib_ring_buffer_config *config,
475 struct lttng_ust_lib_ring_buffer *buf,
476 struct channel *chan,
477 struct lttng_ust_shm_handle *handle)
478 {
479 unsigned long consumed_old, consumed_idx, commit_count, write_offset;
480 struct commit_counters_cold *cc_cold;
481
482 consumed_old = uatomic_read(&buf->consumed);
483 consumed_idx = subbuf_index(consumed_old, chan);
484 cc_cold = shmp_index(handle, buf->commit_cold, consumed_idx);
485 if (!cc_cold)
486 return 0;
487 commit_count = v_read(config, &cc_cold->cc_sb);
488 /*
489 * No memory barrier here, since we are only interested
490 * in a statistically correct polling result. The next poll will
491 * get the data is we are racing. The mb() that ensures correct
492 * memory order is in get_subbuf.
493 */
494 write_offset = v_read(config, &buf->offset);
495
496 /*
497 * Check that the subbuffer we are trying to consume has been
498 * already fully committed.
499 */
500
501 if (((commit_count - chan->backend.subbuf_size)
502 & chan->commit_count_mask)
503 - (buf_trunc(consumed_old, chan)
504 >> chan->backend.num_subbuf_order)
505 != 0)
506 return 0;
507
508 /*
509 * Check that we are not about to read the same subbuffer in
510 * which the writer head is.
511 */
512 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_old, chan)
513 == 0)
514 return 0;
515
516 return 1;
517 }
518
519 static
520 void lib_ring_buffer_wakeup(struct lttng_ust_lib_ring_buffer *buf,
521 struct lttng_ust_shm_handle *handle)
522 {
523 int wakeup_fd = shm_get_wakeup_fd(handle, &buf->self._ref);
524 sigset_t sigpipe_set, pending_set, old_set;
525 int ret, sigpipe_was_pending = 0;
526
527 if (wakeup_fd < 0)
528 return;
529
530 /*
531 * Wake-up the other end by writing a null byte in the pipe
532 * (non-blocking). Important note: Because writing into the
533 * pipe is non-blocking (and therefore we allow dropping wakeup
534 * data, as long as there is wakeup data present in the pipe
535 * buffer to wake up the consumer), the consumer should perform
536 * the following sequence for waiting:
537 * 1) empty the pipe (reads).
538 * 2) check if there is data in the buffer.
539 * 3) wait on the pipe (poll).
540 *
541 * Discard the SIGPIPE from write(), not disturbing any SIGPIPE
542 * that might be already pending. If a bogus SIGPIPE is sent to
543 * the entire process concurrently by a malicious user, it may
544 * be simply discarded.
545 */
546 ret = sigemptyset(&pending_set);
547 assert(!ret);
548 /*
549 * sigpending returns the mask of signals that are _both_
550 * blocked for the thread _and_ pending for either the thread or
551 * the entire process.
552 */
553 ret = sigpending(&pending_set);
554 assert(!ret);
555 sigpipe_was_pending = sigismember(&pending_set, SIGPIPE);
556 /*
557 * If sigpipe was pending, it means it was already blocked, so
558 * no need to block it.
559 */
560 if (!sigpipe_was_pending) {
561 ret = sigemptyset(&sigpipe_set);
562 assert(!ret);
563 ret = sigaddset(&sigpipe_set, SIGPIPE);
564 assert(!ret);
565 ret = pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set);
566 assert(!ret);
567 }
568 do {
569 ret = write(wakeup_fd, "", 1);
570 } while (ret == -1L && errno == EINTR);
571 if (ret == -1L && errno == EPIPE && !sigpipe_was_pending) {
572 struct timespec timeout = { 0, 0 };
573 do {
574 ret = sigtimedwait(&sigpipe_set, NULL,
575 &timeout);
576 } while (ret == -1L && errno == EINTR);
577 }
578 if (!sigpipe_was_pending) {
579 ret = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
580 assert(!ret);
581 }
582 }
583
584 static
585 void lib_ring_buffer_channel_do_read(struct channel *chan)
586 {
587 const struct lttng_ust_lib_ring_buffer_config *config;
588 struct lttng_ust_shm_handle *handle;
589 int cpu;
590
591 handle = chan->handle;
592 config = &chan->backend.config;
593
594 /*
595 * Only flush buffers periodically if readers are active.
596 */
597 pthread_mutex_lock(&wakeup_fd_mutex);
598 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
599 for_each_possible_cpu(cpu) {
600 struct lttng_ust_lib_ring_buffer *buf =
601 shmp(handle, chan->backend.buf[cpu].shmp);
602
603 if (!buf)
604 goto end;
605 if (uatomic_read(&buf->active_readers)
606 && lib_ring_buffer_poll_deliver(config, buf,
607 chan, handle)) {
608 lib_ring_buffer_wakeup(buf, handle);
609 }
610 }
611 } else {
612 struct lttng_ust_lib_ring_buffer *buf =
613 shmp(handle, chan->backend.buf[0].shmp);
614
615 if (!buf)
616 goto end;
617 if (uatomic_read(&buf->active_readers)
618 && lib_ring_buffer_poll_deliver(config, buf,
619 chan, handle)) {
620 lib_ring_buffer_wakeup(buf, handle);
621 }
622 }
623 end:
624 pthread_mutex_unlock(&wakeup_fd_mutex);
625 }
626
627 static
628 void lib_ring_buffer_channel_read_timer(int sig, siginfo_t *si, void *uc)
629 {
630 struct channel *chan;
631
632 assert(CMM_LOAD_SHARED(timer_signal.tid) == pthread_self());
633 chan = si->si_value.sival_ptr;
634 DBG("Read timer for channel %p\n", chan);
635 lib_ring_buffer_channel_do_read(chan);
636 return;
637 }
638
639 static
640 void rb_setmask(sigset_t *mask)
641 {
642 int ret;
643
644 ret = sigemptyset(mask);
645 if (ret) {
646 PERROR("sigemptyset");
647 }
648 ret = sigaddset(mask, LTTNG_UST_RB_SIG_FLUSH);
649 if (ret) {
650 PERROR("sigaddset");
651 }
652 ret = sigaddset(mask, LTTNG_UST_RB_SIG_READ);
653 if (ret) {
654 PERROR("sigaddset");
655 }
656 ret = sigaddset(mask, LTTNG_UST_RB_SIG_TEARDOWN);
657 if (ret) {
658 PERROR("sigaddset");
659 }
660 }
661
662 static
663 void *sig_thread(void *arg)
664 {
665 sigset_t mask;
666 siginfo_t info;
667 int signr;
668
669 /* Only self thread will receive signal mask. */
670 rb_setmask(&mask);
671 CMM_STORE_SHARED(timer_signal.tid, pthread_self());
672
673 for (;;) {
674 signr = sigwaitinfo(&mask, &info);
675 if (signr == -1) {
676 if (errno != EINTR)
677 PERROR("sigwaitinfo");
678 continue;
679 }
680 if (signr == LTTNG_UST_RB_SIG_FLUSH) {
681 lib_ring_buffer_channel_switch_timer(info.si_signo,
682 &info, NULL);
683 } else if (signr == LTTNG_UST_RB_SIG_READ) {
684 lib_ring_buffer_channel_read_timer(info.si_signo,
685 &info, NULL);
686 } else if (signr == LTTNG_UST_RB_SIG_TEARDOWN) {
687 cmm_smp_mb();
688 CMM_STORE_SHARED(timer_signal.qs_done, 1);
689 cmm_smp_mb();
690 } else {
691 ERR("Unexptected signal %d\n", info.si_signo);
692 }
693 }
694 return NULL;
695 }
696
697 /*
698 * Ensure only a single thread listens on the timer signal.
699 */
700 static
701 void lib_ring_buffer_setup_timer_thread(void)
702 {
703 pthread_t thread;
704 int ret;
705
706 pthread_mutex_lock(&timer_signal.lock);
707 if (timer_signal.setup_done)
708 goto end;
709
710 ret = pthread_create(&thread, NULL, &sig_thread, NULL);
711 if (ret) {
712 errno = ret;
713 PERROR("pthread_create");
714 }
715 ret = pthread_detach(thread);
716 if (ret) {
717 errno = ret;
718 PERROR("pthread_detach");
719 }
720 timer_signal.setup_done = 1;
721 end:
722 pthread_mutex_unlock(&timer_signal.lock);
723 }
724
725 /*
726 * Wait for signal-handling thread quiescent state.
727 */
728 static
729 void lib_ring_buffer_wait_signal_thread_qs(unsigned int signr)
730 {
731 sigset_t pending_set;
732 int ret;
733
734 /*
735 * We need to be the only thread interacting with the thread
736 * that manages signals for teardown synchronization.
737 */
738 pthread_mutex_lock(&timer_signal.lock);
739
740 /*
741 * Ensure we don't have any signal queued for this channel.
742 */
743 for (;;) {
744 ret = sigemptyset(&pending_set);
745 if (ret == -1) {
746 PERROR("sigemptyset");
747 }
748 ret = sigpending(&pending_set);
749 if (ret == -1) {
750 PERROR("sigpending");
751 }
752 if (!sigismember(&pending_set, signr))
753 break;
754 caa_cpu_relax();
755 }
756
757 /*
758 * From this point, no new signal handler will be fired that
759 * would try to access "chan". However, we still need to wait
760 * for any currently executing handler to complete.
761 */
762 cmm_smp_mb();
763 CMM_STORE_SHARED(timer_signal.qs_done, 0);
764 cmm_smp_mb();
765
766 /*
767 * Kill with LTTNG_UST_RB_SIG_TEARDOWN, so signal management
768 * thread wakes up.
769 */
770 kill(getpid(), LTTNG_UST_RB_SIG_TEARDOWN);
771
772 while (!CMM_LOAD_SHARED(timer_signal.qs_done))
773 caa_cpu_relax();
774 cmm_smp_mb();
775
776 pthread_mutex_unlock(&timer_signal.lock);
777 }
778
779 static
780 void lib_ring_buffer_channel_switch_timer_start(struct channel *chan)
781 {
782 struct sigevent sev;
783 struct itimerspec its;
784 int ret;
785
786 if (!chan->switch_timer_interval || chan->switch_timer_enabled)
787 return;
788
789 chan->switch_timer_enabled = 1;
790
791 lib_ring_buffer_setup_timer_thread();
792
793 memset(&sev, 0, sizeof(sev));
794 sev.sigev_notify = SIGEV_SIGNAL;
795 sev.sigev_signo = LTTNG_UST_RB_SIG_FLUSH;
796 sev.sigev_value.sival_ptr = chan;
797 ret = timer_create(CLOCKID, &sev, &chan->switch_timer);
798 if (ret == -1) {
799 PERROR("timer_create");
800 }
801
802 its.it_value.tv_sec = chan->switch_timer_interval / 1000000;
803 its.it_value.tv_nsec = (chan->switch_timer_interval % 1000000) * 1000;
804 its.it_interval.tv_sec = its.it_value.tv_sec;
805 its.it_interval.tv_nsec = its.it_value.tv_nsec;
806
807 ret = timer_settime(chan->switch_timer, 0, &its, NULL);
808 if (ret == -1) {
809 PERROR("timer_settime");
810 }
811 }
812
813 static
814 void lib_ring_buffer_channel_switch_timer_stop(struct channel *chan)
815 {
816 int ret;
817
818 if (!chan->switch_timer_interval || !chan->switch_timer_enabled)
819 return;
820
821 ret = timer_delete(chan->switch_timer);
822 if (ret == -1) {
823 PERROR("timer_delete");
824 }
825
826 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH);
827
828 chan->switch_timer = 0;
829 chan->switch_timer_enabled = 0;
830 }
831
832 static
833 void lib_ring_buffer_channel_read_timer_start(struct channel *chan)
834 {
835 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
836 struct sigevent sev;
837 struct itimerspec its;
838 int ret;
839
840 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
841 || !chan->read_timer_interval || chan->read_timer_enabled)
842 return;
843
844 chan->read_timer_enabled = 1;
845
846 lib_ring_buffer_setup_timer_thread();
847
848 sev.sigev_notify = SIGEV_SIGNAL;
849 sev.sigev_signo = LTTNG_UST_RB_SIG_READ;
850 sev.sigev_value.sival_ptr = chan;
851 ret = timer_create(CLOCKID, &sev, &chan->read_timer);
852 if (ret == -1) {
853 PERROR("timer_create");
854 }
855
856 its.it_value.tv_sec = chan->read_timer_interval / 1000000;
857 its.it_value.tv_nsec = (chan->read_timer_interval % 1000000) * 1000;
858 its.it_interval.tv_sec = its.it_value.tv_sec;
859 its.it_interval.tv_nsec = its.it_value.tv_nsec;
860
861 ret = timer_settime(chan->read_timer, 0, &its, NULL);
862 if (ret == -1) {
863 PERROR("timer_settime");
864 }
865 }
866
867 static
868 void lib_ring_buffer_channel_read_timer_stop(struct channel *chan)
869 {
870 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
871 int ret;
872
873 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
874 || !chan->read_timer_interval || !chan->read_timer_enabled)
875 return;
876
877 ret = timer_delete(chan->read_timer);
878 if (ret == -1) {
879 PERROR("timer_delete");
880 }
881
882 /*
883 * do one more check to catch data that has been written in the last
884 * timer period.
885 */
886 lib_ring_buffer_channel_do_read(chan);
887
888 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ);
889
890 chan->read_timer = 0;
891 chan->read_timer_enabled = 0;
892 }
893
894 static void channel_unregister_notifiers(struct channel *chan,
895 struct lttng_ust_shm_handle *handle)
896 {
897 lib_ring_buffer_channel_switch_timer_stop(chan);
898 lib_ring_buffer_channel_read_timer_stop(chan);
899 }
900
901 static void channel_print_errors(struct channel *chan,
902 struct lttng_ust_shm_handle *handle)
903 {
904 const struct lttng_ust_lib_ring_buffer_config *config =
905 &chan->backend.config;
906 int cpu;
907
908 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
909 for_each_possible_cpu(cpu) {
910 struct lttng_ust_lib_ring_buffer *buf =
911 shmp(handle, chan->backend.buf[cpu].shmp);
912 if (buf)
913 lib_ring_buffer_print_errors(chan, buf, cpu, handle);
914 }
915 } else {
916 struct lttng_ust_lib_ring_buffer *buf =
917 shmp(handle, chan->backend.buf[0].shmp);
918
919 if (buf)
920 lib_ring_buffer_print_errors(chan, buf, -1, handle);
921 }
922 }
923
924 static void channel_free(struct channel *chan,
925 struct lttng_ust_shm_handle *handle,
926 int consumer)
927 {
928 channel_backend_free(&chan->backend, handle);
929 /* chan is freed by shm teardown */
930 shm_object_table_destroy(handle->table, consumer);
931 free(handle);
932 }
933
934 /**
935 * channel_create - Create channel.
936 * @config: ring buffer instance configuration
937 * @name: name of the channel
938 * @priv_data: ring buffer client private data area pointer (output)
939 * @priv_data_size: length, in bytes, of the private data area.
940 * @priv_data_init: initialization data for private data.
941 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
942 * address mapping. It is used only by RING_BUFFER_STATIC
943 * configuration. It can be set to NULL for other backends.
944 * @subbuf_size: subbuffer size
945 * @num_subbuf: number of subbuffers
946 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
947 * padding to let readers get those sub-buffers.
948 * Used for live streaming.
949 * @read_timer_interval: Time interval (in us) to wake up pending readers.
950 * @stream_fds: array of stream file descriptors.
951 * @nr_stream_fds: number of file descriptors in array.
952 *
953 * Holds cpu hotplug.
954 * Returns NULL on failure.
955 */
956 struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
957 const char *name,
958 void **priv_data,
959 size_t priv_data_align,
960 size_t priv_data_size,
961 void *priv_data_init,
962 void *buf_addr, size_t subbuf_size,
963 size_t num_subbuf, unsigned int switch_timer_interval,
964 unsigned int read_timer_interval,
965 const int *stream_fds, int nr_stream_fds,
966 int64_t blocking_timeout)
967 {
968 int ret;
969 size_t shmsize, chansize;
970 struct channel *chan;
971 struct lttng_ust_shm_handle *handle;
972 struct shm_object *shmobj;
973 unsigned int nr_streams;
974 int64_t blocking_timeout_ms;
975
976 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
977 nr_streams = num_possible_cpus();
978 else
979 nr_streams = 1;
980
981 if (nr_stream_fds != nr_streams)
982 return NULL;
983
984 if (blocking_timeout < -1) {
985 return NULL;
986 }
987 /* usec to msec */
988 if (blocking_timeout == -1) {
989 blocking_timeout_ms = -1;
990 } else {
991 blocking_timeout_ms = blocking_timeout / 1000;
992 if (blocking_timeout_ms != (int32_t) blocking_timeout_ms) {
993 return NULL;
994 }
995 }
996
997 if (lib_ring_buffer_check_config(config, switch_timer_interval,
998 read_timer_interval))
999 return NULL;
1000
1001 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
1002 if (!handle)
1003 return NULL;
1004
1005 /* Allocate table for channel + per-cpu buffers */
1006 handle->table = shm_object_table_create(1 + num_possible_cpus());
1007 if (!handle->table)
1008 goto error_table_alloc;
1009
1010 /* Calculate the shm allocation layout */
1011 shmsize = sizeof(struct channel);
1012 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp));
1013 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams;
1014 chansize = shmsize;
1015 if (priv_data_align)
1016 shmsize += lttng_ust_offset_align(shmsize, priv_data_align);
1017 shmsize += priv_data_size;
1018
1019 /* Allocate normal memory for channel (not shared) */
1020 shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM,
1021 -1, -1);
1022 if (!shmobj)
1023 goto error_append;
1024 /* struct channel is at object 0, offset 0 (hardcoded) */
1025 set_shmp(handle->chan, zalloc_shm(shmobj, chansize));
1026 assert(handle->chan._ref.index == 0);
1027 assert(handle->chan._ref.offset == 0);
1028 chan = shmp(handle, handle->chan);
1029 if (!chan)
1030 goto error_append;
1031 chan->nr_streams = nr_streams;
1032
1033 /* space for private data */
1034 if (priv_data_size) {
1035 DECLARE_SHMP(void, priv_data_alloc);
1036
1037 align_shm(shmobj, priv_data_align);
1038 chan->priv_data_offset = shmobj->allocated_len;
1039 set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size));
1040 if (!shmp(handle, priv_data_alloc))
1041 goto error_append;
1042 *priv_data = channel_get_private(chan);
1043 memcpy(*priv_data, priv_data_init, priv_data_size);
1044 } else {
1045 chan->priv_data_offset = -1;
1046 if (priv_data)
1047 *priv_data = NULL;
1048 }
1049
1050 chan->u.s.blocking_timeout_ms = (int32_t) blocking_timeout_ms;
1051
1052 ret = channel_backend_init(&chan->backend, name, config,
1053 subbuf_size, num_subbuf, handle,
1054 stream_fds);
1055 if (ret)
1056 goto error_backend_init;
1057
1058 chan->handle = handle;
1059 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
1060
1061 chan->switch_timer_interval = switch_timer_interval;
1062 chan->read_timer_interval = read_timer_interval;
1063 lib_ring_buffer_channel_switch_timer_start(chan);
1064 lib_ring_buffer_channel_read_timer_start(chan);
1065
1066 return handle;
1067
1068 error_backend_init:
1069 error_append:
1070 shm_object_table_destroy(handle->table, 1);
1071 error_table_alloc:
1072 free(handle);
1073 return NULL;
1074 }
1075
1076 struct lttng_ust_shm_handle *channel_handle_create(void *data,
1077 uint64_t memory_map_size,
1078 int wakeup_fd)
1079 {
1080 struct lttng_ust_shm_handle *handle;
1081 struct shm_object *object;
1082
1083 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
1084 if (!handle)
1085 return NULL;
1086
1087 /* Allocate table for channel + per-cpu buffers */
1088 handle->table = shm_object_table_create(1 + num_possible_cpus());
1089 if (!handle->table)
1090 goto error_table_alloc;
1091 /* Add channel object */
1092 object = shm_object_table_append_mem(handle->table, data,
1093 memory_map_size, wakeup_fd);
1094 if (!object)
1095 goto error_table_object;
1096 /* struct channel is at object 0, offset 0 (hardcoded) */
1097 handle->chan._ref.index = 0;
1098 handle->chan._ref.offset = 0;
1099 return handle;
1100
1101 error_table_object:
1102 shm_object_table_destroy(handle->table, 0);
1103 error_table_alloc:
1104 free(handle);
1105 return NULL;
1106 }
1107
1108 int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
1109 int shm_fd, int wakeup_fd, uint32_t stream_nr,
1110 uint64_t memory_map_size)
1111 {
1112 struct shm_object *object;
1113
1114 /* Add stream object */
1115 object = shm_object_table_append_shm(handle->table,
1116 shm_fd, wakeup_fd, stream_nr,
1117 memory_map_size);
1118 if (!object)
1119 return -EINVAL;
1120 return 0;
1121 }
1122
1123 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle)
1124 {
1125 assert(handle->table);
1126 return handle->table->allocated_len - 1;
1127 }
1128
1129 static
1130 void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle,
1131 int consumer)
1132 {
1133 channel_free(chan, handle, consumer);
1134 }
1135
1136 /**
1137 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1138 * @chan: channel to destroy
1139 *
1140 * Holds cpu hotplug.
1141 * Call "destroy" callback, finalize channels, decrement the channel
1142 * reference count. Note that when readers have completed data
1143 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1144 * They should release their handle at that point.
1145 */
1146 void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
1147 int consumer)
1148 {
1149 if (consumer) {
1150 /*
1151 * Note: the consumer takes care of finalizing and
1152 * switching the buffers.
1153 */
1154 channel_unregister_notifiers(chan, handle);
1155 /*
1156 * The consumer prints errors.
1157 */
1158 channel_print_errors(chan, handle);
1159 }
1160
1161 /*
1162 * sessiond/consumer are keeping a reference on the shm file
1163 * descriptor directly. No need to refcount.
1164 */
1165 channel_release(chan, handle, consumer);
1166 return;
1167 }
1168
1169 struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
1170 const struct lttng_ust_lib_ring_buffer_config *config,
1171 struct channel *chan, int cpu,
1172 struct lttng_ust_shm_handle *handle,
1173 int *shm_fd, int *wait_fd,
1174 int *wakeup_fd,
1175 uint64_t *memory_map_size)
1176 {
1177 struct shm_ref *ref;
1178
1179 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1180 cpu = 0;
1181 } else {
1182 if (cpu >= num_possible_cpus())
1183 return NULL;
1184 }
1185 ref = &chan->backend.buf[cpu].shmp._ref;
1186 *shm_fd = shm_get_shm_fd(handle, ref);
1187 *wait_fd = shm_get_wait_fd(handle, ref);
1188 *wakeup_fd = shm_get_wakeup_fd(handle, ref);
1189 if (shm_get_shm_size(handle, ref, memory_map_size))
1190 return NULL;
1191 return shmp(handle, chan->backend.buf[cpu].shmp);
1192 }
1193
1194 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1195 struct channel *chan,
1196 struct lttng_ust_shm_handle *handle)
1197 {
1198 struct shm_ref *ref;
1199
1200 ref = &handle->chan._ref;
1201 return shm_close_wait_fd(handle, ref);
1202 }
1203
1204 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1205 struct channel *chan,
1206 struct lttng_ust_shm_handle *handle)
1207 {
1208 struct shm_ref *ref;
1209
1210 ref = &handle->chan._ref;
1211 return shm_close_wakeup_fd(handle, ref);
1212 }
1213
1214 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1215 struct channel *chan,
1216 struct lttng_ust_shm_handle *handle,
1217 int cpu)
1218 {
1219 struct shm_ref *ref;
1220
1221 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1222 cpu = 0;
1223 } else {
1224 if (cpu >= num_possible_cpus())
1225 return -EINVAL;
1226 }
1227 ref = &chan->backend.buf[cpu].shmp._ref;
1228 return shm_close_wait_fd(handle, ref);
1229 }
1230
1231 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1232 struct channel *chan,
1233 struct lttng_ust_shm_handle *handle,
1234 int cpu)
1235 {
1236 struct shm_ref *ref;
1237 int ret;
1238
1239 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1240 cpu = 0;
1241 } else {
1242 if (cpu >= num_possible_cpus())
1243 return -EINVAL;
1244 }
1245 ref = &chan->backend.buf[cpu].shmp._ref;
1246 pthread_mutex_lock(&wakeup_fd_mutex);
1247 ret = shm_close_wakeup_fd(handle, ref);
1248 pthread_mutex_unlock(&wakeup_fd_mutex);
1249 return ret;
1250 }
1251
1252 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
1253 struct lttng_ust_shm_handle *handle)
1254 {
1255 if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0)
1256 return -EBUSY;
1257 cmm_smp_mb();
1258 return 0;
1259 }
1260
1261 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
1262 struct lttng_ust_shm_handle *handle)
1263 {
1264 struct channel *chan = shmp(handle, buf->backend.chan);
1265
1266 if (!chan)
1267 return;
1268 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
1269 cmm_smp_mb();
1270 uatomic_dec(&buf->active_readers);
1271 }
1272
1273 /**
1274 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1275 * @buf: ring buffer
1276 * @consumed: consumed count indicating the position where to read
1277 * @produced: produced count, indicates position when to stop reading
1278 *
1279 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1280 * data to read at consumed position, or 0 if the get operation succeeds.
1281 */
1282
1283 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
1284 unsigned long *consumed, unsigned long *produced,
1285 struct lttng_ust_shm_handle *handle)
1286 {
1287 struct channel *chan;
1288 const struct lttng_ust_lib_ring_buffer_config *config;
1289 unsigned long consumed_cur, write_offset;
1290 int finalized;
1291
1292 chan = shmp(handle, buf->backend.chan);
1293 if (!chan)
1294 return -EPERM;
1295 config = &chan->backend.config;
1296 finalized = CMM_ACCESS_ONCE(buf->finalized);
1297 /*
1298 * Read finalized before counters.
1299 */
1300 cmm_smp_rmb();
1301 consumed_cur = uatomic_read(&buf->consumed);
1302 /*
1303 * No need to issue a memory barrier between consumed count read and
1304 * write offset read, because consumed count can only change
1305 * concurrently in overwrite mode, and we keep a sequence counter
1306 * identifier derived from the write offset to check we are getting
1307 * the same sub-buffer we are expecting (the sub-buffers are atomically
1308 * "tagged" upon writes, tags are checked upon read).
1309 */
1310 write_offset = v_read(config, &buf->offset);
1311
1312 /*
1313 * Check that we are not about to read the same subbuffer in
1314 * which the writer head is.
1315 */
1316 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1317 == 0)
1318 goto nodata;
1319
1320 *consumed = consumed_cur;
1321 *produced = subbuf_trunc(write_offset, chan);
1322
1323 return 0;
1324
1325 nodata:
1326 /*
1327 * The memory barriers __wait_event()/wake_up_interruptible() take care
1328 * of "raw_spin_is_locked" memory ordering.
1329 */
1330 if (finalized)
1331 return -ENODATA;
1332 else
1333 return -EAGAIN;
1334 }
1335
1336 /**
1337 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1338 * are saved regardless of whether the consumed and produced positions are
1339 * in the same subbuffer.
1340 * @buf: ring buffer
1341 * @consumed: consumed byte count indicating the last position read
1342 * @produced: produced byte count indicating the last position written
1343 *
1344 * This function is meant to provide information on the exact producer and
1345 * consumer positions without regard for the "snapshot" feature.
1346 */
1347 int lib_ring_buffer_snapshot_sample_positions(
1348 struct lttng_ust_lib_ring_buffer *buf,
1349 unsigned long *consumed, unsigned long *produced,
1350 struct lttng_ust_shm_handle *handle)
1351 {
1352 struct channel *chan;
1353 const struct lttng_ust_lib_ring_buffer_config *config;
1354
1355 chan = shmp(handle, buf->backend.chan);
1356 if (!chan)
1357 return -EPERM;
1358 config = &chan->backend.config;
1359 cmm_smp_rmb();
1360 *consumed = uatomic_read(&buf->consumed);
1361 /*
1362 * No need to issue a memory barrier between consumed count read and
1363 * write offset read, because consumed count can only change
1364 * concurrently in overwrite mode, and we keep a sequence counter
1365 * identifier derived from the write offset to check we are getting
1366 * the same sub-buffer we are expecting (the sub-buffers are atomically
1367 * "tagged" upon writes, tags are checked upon read).
1368 */
1369 *produced = v_read(config, &buf->offset);
1370 return 0;
1371 }
1372
1373 /**
1374 * lib_ring_buffer_move_consumer - move consumed counter forward
1375 * @buf: ring buffer
1376 * @consumed_new: new consumed count value
1377 */
1378 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1379 unsigned long consumed_new,
1380 struct lttng_ust_shm_handle *handle)
1381 {
1382 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1383 struct channel *chan;
1384 unsigned long consumed;
1385
1386 chan = shmp(handle, bufb->chan);
1387 if (!chan)
1388 return;
1389 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
1390
1391 /*
1392 * Only push the consumed value forward.
1393 * If the consumed cmpxchg fails, this is because we have been pushed by
1394 * the writer in flight recorder mode.
1395 */
1396 consumed = uatomic_read(&buf->consumed);
1397 while ((long) consumed - (long) consumed_new < 0)
1398 consumed = uatomic_cmpxchg(&buf->consumed, consumed,
1399 consumed_new);
1400 }
1401
1402 /**
1403 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1404 * @buf: ring buffer
1405 * @consumed: consumed count indicating the position where to read
1406 *
1407 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1408 * data to read at consumed position, or 0 if the get operation succeeds.
1409 */
1410 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1411 unsigned long consumed,
1412 struct lttng_ust_shm_handle *handle)
1413 {
1414 struct channel *chan;
1415 const struct lttng_ust_lib_ring_buffer_config *config;
1416 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
1417 int ret, finalized, nr_retry = LTTNG_UST_RING_BUFFER_GET_RETRY;
1418 struct commit_counters_cold *cc_cold;
1419
1420 chan = shmp(handle, buf->backend.chan);
1421 if (!chan)
1422 return -EPERM;
1423 config = &chan->backend.config;
1424 retry:
1425 finalized = CMM_ACCESS_ONCE(buf->finalized);
1426 /*
1427 * Read finalized before counters.
1428 */
1429 cmm_smp_rmb();
1430 consumed_cur = uatomic_read(&buf->consumed);
1431 consumed_idx = subbuf_index(consumed, chan);
1432 cc_cold = shmp_index(handle, buf->commit_cold, consumed_idx);
1433 if (!cc_cold)
1434 return -EPERM;
1435 commit_count = v_read(config, &cc_cold->cc_sb);
1436 /*
1437 * Make sure we read the commit count before reading the buffer
1438 * data and the write offset. Correct consumed offset ordering
1439 * wrt commit count is insured by the use of cmpxchg to update
1440 * the consumed offset.
1441 */
1442 /*
1443 * Local rmb to match the remote wmb to read the commit count
1444 * before the buffer data and the write offset.
1445 */
1446 cmm_smp_rmb();
1447
1448 write_offset = v_read(config, &buf->offset);
1449
1450 /*
1451 * Check that the buffer we are getting is after or at consumed_cur
1452 * position.
1453 */
1454 if ((long) subbuf_trunc(consumed, chan)
1455 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1456 goto nodata;
1457
1458 /*
1459 * Check that the subbuffer we are trying to consume has been
1460 * already fully committed. There are a few causes that can make
1461 * this unavailability situation occur:
1462 *
1463 * Temporary (short-term) situation:
1464 * - Application is running on a different CPU, between reserve
1465 * and commit ring buffer operations,
1466 * - Application is preempted between reserve and commit ring
1467 * buffer operations,
1468 *
1469 * Long-term situation:
1470 * - Application is stopped (SIGSTOP) between reserve and commit
1471 * ring buffer operations. Could eventually be resumed by
1472 * SIGCONT.
1473 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1474 * reserve and commit ring buffer operation.
1475 *
1476 * From a consumer perspective, handling short-term
1477 * unavailability situations is performed by retrying a few
1478 * times after a delay. Handling long-term unavailability
1479 * situations is handled by failing to get the sub-buffer.
1480 *
1481 * In all of those situations, if the application is taking a
1482 * long time to perform its commit after ring buffer space
1483 * reservation, we can end up in a situation where the producer
1484 * will fill the ring buffer and try to write into the same
1485 * sub-buffer again (which has a missing commit). This is
1486 * handled by the producer in the sub-buffer switch handling
1487 * code of the reserve routine by detecting unbalanced
1488 * reserve/commit counters and discarding all further events
1489 * until the situation is resolved in those situations. Two
1490 * scenarios can occur:
1491 *
1492 * 1) The application causing the reserve/commit counters to be
1493 * unbalanced has been terminated. In this situation, all
1494 * further events will be discarded in the buffers, and no
1495 * further buffer data will be readable by the consumer
1496 * daemon. Tearing down the UST tracing session and starting
1497 * anew is a work-around for those situations. Note that this
1498 * only affects per-UID tracing. In per-PID tracing, the
1499 * application vanishes with the termination, and therefore
1500 * no more data needs to be written to the buffers.
1501 * 2) The application causing the unbalance has been delayed for
1502 * a long time, but will eventually try to increment the
1503 * commit counter after eventually writing to the sub-buffer.
1504 * This situation can cause events to be discarded until the
1505 * application resumes its operations.
1506 */
1507 if (((commit_count - chan->backend.subbuf_size)
1508 & chan->commit_count_mask)
1509 - (buf_trunc(consumed, chan)
1510 >> chan->backend.num_subbuf_order)
1511 != 0) {
1512 if (nr_retry-- > 0) {
1513 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1514 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1515 goto retry;
1516 } else {
1517 goto nodata;
1518 }
1519 }
1520
1521 /*
1522 * Check that we are not about to read the same subbuffer in
1523 * which the writer head is.
1524 */
1525 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan)
1526 == 0)
1527 goto nodata;
1528
1529 /*
1530 * Failure to get the subbuffer causes a busy-loop retry without going
1531 * to a wait queue. These are caused by short-lived race windows where
1532 * the writer is getting access to a subbuffer we were trying to get
1533 * access to. Also checks that the "consumed" buffer count we are
1534 * looking for matches the one contained in the subbuffer id.
1535 *
1536 * The short-lived race window described here can be affected by
1537 * application signals and preemption, thus requiring to bound
1538 * the loop to a maximum number of retry.
1539 */
1540 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1541 consumed_idx, buf_trunc_val(consumed, chan),
1542 handle);
1543 if (ret) {
1544 if (nr_retry-- > 0) {
1545 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1546 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1547 goto retry;
1548 } else {
1549 goto nodata;
1550 }
1551 }
1552 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1553
1554 buf->get_subbuf_consumed = consumed;
1555 buf->get_subbuf = 1;
1556
1557 return 0;
1558
1559 nodata:
1560 /*
1561 * The memory barriers __wait_event()/wake_up_interruptible() take care
1562 * of "raw_spin_is_locked" memory ordering.
1563 */
1564 if (finalized)
1565 return -ENODATA;
1566 else
1567 return -EAGAIN;
1568 }
1569
1570 /**
1571 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1572 * @buf: ring buffer
1573 */
1574 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1575 struct lttng_ust_shm_handle *handle)
1576 {
1577 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1578 struct channel *chan;
1579 const struct lttng_ust_lib_ring_buffer_config *config;
1580 unsigned long sb_bindex, consumed_idx, consumed;
1581 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
1582 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
1583
1584 chan = shmp(handle, bufb->chan);
1585 if (!chan)
1586 return;
1587 config = &chan->backend.config;
1588 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
1589
1590 if (!buf->get_subbuf) {
1591 /*
1592 * Reader puts a subbuffer it did not get.
1593 */
1594 CHAN_WARN_ON(chan, 1);
1595 return;
1596 }
1597 consumed = buf->get_subbuf_consumed;
1598 buf->get_subbuf = 0;
1599
1600 /*
1601 * Clear the records_unread counter. (overruns counter)
1602 * Can still be non-zero if a file reader simply grabbed the data
1603 * without using iterators.
1604 * Can be below zero if an iterator is used on a snapshot more than
1605 * once.
1606 */
1607 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1608 rpages = shmp_index(handle, bufb->array, sb_bindex);
1609 if (!rpages)
1610 return;
1611 backend_pages = shmp(handle, rpages->shmp);
1612 if (!backend_pages)
1613 return;
1614 v_add(config, v_read(config, &backend_pages->records_unread),
1615 &bufb->records_read);
1616 v_set(config, &backend_pages->records_unread, 0);
1617 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1618 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1619 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1620
1621 /*
1622 * Exchange the reader subbuffer with the one we put in its place in the
1623 * writer subbuffer table. Expect the original consumed count. If
1624 * update_read_sb_index fails, this is because the writer updated the
1625 * subbuffer concurrently. We should therefore keep the subbuffer we
1626 * currently have: it has become invalid to try reading this sub-buffer
1627 * consumed count value anyway.
1628 */
1629 consumed_idx = subbuf_index(consumed, chan);
1630 update_read_sb_index(config, &buf->backend, &chan->backend,
1631 consumed_idx, buf_trunc_val(consumed, chan),
1632 handle);
1633 /*
1634 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1635 * if the writer concurrently updated it.
1636 */
1637 }
1638
1639 /*
1640 * cons_offset is an iterator on all subbuffer offsets between the reader
1641 * position and the writer position. (inclusive)
1642 */
1643 static
1644 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf,
1645 struct channel *chan,
1646 unsigned long cons_offset,
1647 int cpu,
1648 struct lttng_ust_shm_handle *handle)
1649 {
1650 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1651 unsigned long cons_idx, commit_count, commit_count_sb;
1652 struct commit_counters_hot *cc_hot;
1653 struct commit_counters_cold *cc_cold;
1654
1655 cons_idx = subbuf_index(cons_offset, chan);
1656 cc_hot = shmp_index(handle, buf->commit_hot, cons_idx);
1657 if (!cc_hot)
1658 return;
1659 cc_cold = shmp_index(handle, buf->commit_cold, cons_idx);
1660 if (!cc_cold)
1661 return;
1662 commit_count = v_read(config, &cc_hot->cc);
1663 commit_count_sb = v_read(config, &cc_cold->cc_sb);
1664
1665 if (subbuf_offset(commit_count, chan) != 0)
1666 DBG("ring buffer %s, cpu %d: "
1667 "commit count in subbuffer %lu,\n"
1668 "expecting multiples of %lu bytes\n"
1669 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1670 chan->backend.name, cpu, cons_idx,
1671 chan->backend.subbuf_size,
1672 commit_count, commit_count_sb);
1673
1674 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1675 chan->backend.name, cpu, commit_count);
1676 }
1677
1678 static
1679 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf,
1680 struct channel *chan,
1681 void *priv, int cpu,
1682 struct lttng_ust_shm_handle *handle)
1683 {
1684 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1685 unsigned long write_offset, cons_offset;
1686
1687 /*
1688 * No need to order commit_count, write_offset and cons_offset reads
1689 * because we execute at teardown when no more writer nor reader
1690 * references are left.
1691 */
1692 write_offset = v_read(config, &buf->offset);
1693 cons_offset = uatomic_read(&buf->consumed);
1694 if (write_offset != cons_offset)
1695 DBG("ring buffer %s, cpu %d: "
1696 "non-consumed data\n"
1697 " [ %lu bytes written, %lu bytes read ]\n",
1698 chan->backend.name, cpu, write_offset, cons_offset);
1699
1700 for (cons_offset = uatomic_read(&buf->consumed);
1701 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1702 chan)
1703 - cons_offset) > 0;
1704 cons_offset = subbuf_align(cons_offset, chan))
1705 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1706 cpu, handle);
1707 }
1708
1709 static
1710 void lib_ring_buffer_print_errors(struct channel *chan,
1711 struct lttng_ust_lib_ring_buffer *buf, int cpu,
1712 struct lttng_ust_shm_handle *handle)
1713 {
1714 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1715 void *priv = channel_get_private(chan);
1716
1717 if (!strcmp(chan->backend.name, "relay-metadata-mmap")) {
1718 DBG("ring buffer %s: %lu records written, "
1719 "%lu records overrun\n",
1720 chan->backend.name,
1721 v_read(config, &buf->records_count),
1722 v_read(config, &buf->records_overrun));
1723 } else {
1724 DBG("ring buffer %s, cpu %d: %lu records written, "
1725 "%lu records overrun\n",
1726 chan->backend.name, cpu,
1727 v_read(config, &buf->records_count),
1728 v_read(config, &buf->records_overrun));
1729
1730 if (v_read(config, &buf->records_lost_full)
1731 || v_read(config, &buf->records_lost_wrap)
1732 || v_read(config, &buf->records_lost_big))
1733 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1734 " [ %lu buffer full, %lu nest buffer wrap-around, "
1735 "%lu event too big ]\n",
1736 chan->backend.name, cpu,
1737 v_read(config, &buf->records_lost_full),
1738 v_read(config, &buf->records_lost_wrap),
1739 v_read(config, &buf->records_lost_big));
1740 }
1741 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle);
1742 }
1743
1744 /*
1745 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1746 *
1747 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1748 * active or at buffer finalization (destroy).
1749 */
1750 static
1751 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf,
1752 struct channel *chan,
1753 struct switch_offsets *offsets,
1754 uint64_t tsc,
1755 struct lttng_ust_shm_handle *handle)
1756 {
1757 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1758 unsigned long oldidx = subbuf_index(offsets->old, chan);
1759 unsigned long commit_count;
1760 struct commit_counters_hot *cc_hot;
1761
1762 config->cb.buffer_begin(buf, tsc, oldidx, handle);
1763
1764 /*
1765 * Order all writes to buffer before the commit count update that will
1766 * determine that the subbuffer is full.
1767 */
1768 cmm_smp_wmb();
1769 cc_hot = shmp_index(handle, buf->commit_hot, oldidx);
1770 if (!cc_hot)
1771 return;
1772 v_add(config, config->cb.subbuffer_header_size(),
1773 &cc_hot->cc);
1774 commit_count = v_read(config, &cc_hot->cc);
1775 /* Check if the written buffer has to be delivered */
1776 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1777 commit_count, oldidx, handle, tsc);
1778 lib_ring_buffer_write_commit_counter(config, buf, chan,
1779 offsets->old + config->cb.subbuffer_header_size(),
1780 commit_count, handle, cc_hot);
1781 }
1782
1783 /*
1784 * lib_ring_buffer_switch_old_end: switch old subbuffer
1785 *
1786 * Note : offset_old should never be 0 here. It is ok, because we never perform
1787 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1788 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1789 * subbuffer.
1790 */
1791 static
1792 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf,
1793 struct channel *chan,
1794 struct switch_offsets *offsets,
1795 uint64_t tsc,
1796 struct lttng_ust_shm_handle *handle)
1797 {
1798 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1799 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1800 unsigned long commit_count, padding_size, data_size;
1801 struct commit_counters_hot *cc_hot;
1802 uint64_t *ts_end;
1803
1804 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1805 padding_size = chan->backend.subbuf_size - data_size;
1806 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size,
1807 handle);
1808
1809 ts_end = shmp_index(handle, buf->ts_end, oldidx);
1810 if (!ts_end)
1811 return;
1812 /*
1813 * This is the last space reservation in that sub-buffer before
1814 * it gets delivered. This provides exclusive access to write to
1815 * this sub-buffer's ts_end. There are also no concurrent
1816 * readers of that ts_end because delivery of that sub-buffer is
1817 * postponed until the commit counter is incremented for the
1818 * current space reservation.
1819 */
1820 *ts_end = tsc;
1821
1822 /*
1823 * Order all writes to buffer and store to ts_end before the commit
1824 * count update that will determine that the subbuffer is full.
1825 */
1826 cmm_smp_wmb();
1827 cc_hot = shmp_index(handle, buf->commit_hot, oldidx);
1828 if (!cc_hot)
1829 return;
1830 v_add(config, padding_size, &cc_hot->cc);
1831 commit_count = v_read(config, &cc_hot->cc);
1832 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1833 commit_count, oldidx, handle, tsc);
1834 lib_ring_buffer_write_commit_counter(config, buf, chan,
1835 offsets->old + padding_size, commit_count, handle,
1836 cc_hot);
1837 }
1838
1839 /*
1840 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1841 *
1842 * This code can be executed unordered : writers may already have written to the
1843 * sub-buffer before this code gets executed, caution. The commit makes sure
1844 * that this code is executed before the deliver of this sub-buffer.
1845 */
1846 static
1847 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf,
1848 struct channel *chan,
1849 struct switch_offsets *offsets,
1850 uint64_t tsc,
1851 struct lttng_ust_shm_handle *handle)
1852 {
1853 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1854 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1855 unsigned long commit_count;
1856 struct commit_counters_hot *cc_hot;
1857
1858 config->cb.buffer_begin(buf, tsc, beginidx, handle);
1859
1860 /*
1861 * Order all writes to buffer before the commit count update that will
1862 * determine that the subbuffer is full.
1863 */
1864 cmm_smp_wmb();
1865 cc_hot = shmp_index(handle, buf->commit_hot, beginidx);
1866 if (!cc_hot)
1867 return;
1868 v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc);
1869 commit_count = v_read(config, &cc_hot->cc);
1870 /* Check if the written buffer has to be delivered */
1871 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1872 commit_count, beginidx, handle, tsc);
1873 lib_ring_buffer_write_commit_counter(config, buf, chan,
1874 offsets->begin + config->cb.subbuffer_header_size(),
1875 commit_count, handle, cc_hot);
1876 }
1877
1878 /*
1879 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1880 *
1881 * Calls subbuffer_set_data_size() to set the data size of the current
1882 * sub-buffer. We do not need to perform check_deliver nor commit here,
1883 * since this task will be done by the "commit" of the event for which
1884 * we are currently doing the space reservation.
1885 */
1886 static
1887 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf,
1888 struct channel *chan,
1889 struct switch_offsets *offsets,
1890 uint64_t tsc,
1891 struct lttng_ust_shm_handle *handle)
1892 {
1893 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1894 unsigned long endidx, data_size;
1895 uint64_t *ts_end;
1896
1897 endidx = subbuf_index(offsets->end - 1, chan);
1898 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1899 subbuffer_set_data_size(config, &buf->backend, endidx, data_size,
1900 handle);
1901 ts_end = shmp_index(handle, buf->ts_end, endidx);
1902 if (!ts_end)
1903 return;
1904 /*
1905 * This is the last space reservation in that sub-buffer before
1906 * it gets delivered. This provides exclusive access to write to
1907 * this sub-buffer's ts_end. There are also no concurrent
1908 * readers of that ts_end because delivery of that sub-buffer is
1909 * postponed until the commit counter is incremented for the
1910 * current space reservation.
1911 */
1912 *ts_end = tsc;
1913 }
1914
1915 /*
1916 * Returns :
1917 * 0 if ok
1918 * !0 if execution must be aborted.
1919 */
1920 static
1921 int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
1922 struct lttng_ust_lib_ring_buffer *buf,
1923 struct channel *chan,
1924 struct switch_offsets *offsets,
1925 uint64_t *tsc,
1926 struct lttng_ust_shm_handle *handle)
1927 {
1928 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1929 unsigned long off, reserve_commit_diff;
1930
1931 offsets->begin = v_read(config, &buf->offset);
1932 offsets->old = offsets->begin;
1933 offsets->switch_old_start = 0;
1934 off = subbuf_offset(offsets->begin, chan);
1935
1936 *tsc = config->cb.ring_buffer_clock_read(chan);
1937
1938 /*
1939 * Ensure we flush the header of an empty subbuffer when doing the
1940 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1941 * total data gathering duration even if there were no records saved
1942 * after the last buffer switch.
1943 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1944 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1945 * subbuffer header as appropriate.
1946 * The next record that reserves space will be responsible for
1947 * populating the following subbuffer header. We choose not to populate
1948 * the next subbuffer header here because we want to be able to use
1949 * SWITCH_ACTIVE for periodical buffer flush, which must
1950 * guarantee that all the buffer content (records and header
1951 * timestamps) are visible to the reader. This is required for
1952 * quiescence guarantees for the fusion merge.
1953 */
1954 if (mode != SWITCH_FLUSH && !off)
1955 return -1; /* we do not have to switch : buffer is empty */
1956
1957 if (caa_unlikely(off == 0)) {
1958 unsigned long sb_index, commit_count;
1959 struct commit_counters_cold *cc_cold;
1960
1961 /*
1962 * We are performing a SWITCH_FLUSH. There may be concurrent
1963 * writes into the buffer if e.g. invoked while performing a
1964 * snapshot on an active trace.
1965 *
1966 * If the client does not save any header information
1967 * (sub-buffer header size == 0), don't switch empty subbuffer
1968 * on finalize, because it is invalid to deliver a completely
1969 * empty subbuffer.
1970 */
1971 if (!config->cb.subbuffer_header_size())
1972 return -1;
1973
1974 /* Test new buffer integrity */
1975 sb_index = subbuf_index(offsets->begin, chan);
1976 cc_cold = shmp_index(handle, buf->commit_cold, sb_index);
1977 if (!cc_cold)
1978 return -1;
1979 commit_count = v_read(config, &cc_cold->cc_sb);
1980 reserve_commit_diff =
1981 (buf_trunc(offsets->begin, chan)
1982 >> chan->backend.num_subbuf_order)
1983 - (commit_count & chan->commit_count_mask);
1984 if (caa_likely(reserve_commit_diff == 0)) {
1985 /* Next subbuffer not being written to. */
1986 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1987 subbuf_trunc(offsets->begin, chan)
1988 - subbuf_trunc((unsigned long)
1989 uatomic_read(&buf->consumed), chan)
1990 >= chan->backend.buf_size)) {
1991 /*
1992 * We do not overwrite non consumed buffers
1993 * and we are full : don't switch.
1994 */
1995 return -1;
1996 } else {
1997 /*
1998 * Next subbuffer not being written to, and we
1999 * are either in overwrite mode or the buffer is
2000 * not full. It's safe to write in this new
2001 * subbuffer.
2002 */
2003 }
2004 } else {
2005 /*
2006 * Next subbuffer reserve offset does not match the
2007 * commit offset. Don't perform switch in
2008 * producer-consumer and overwrite mode. Caused by
2009 * either a writer OOPS or too many nested writes over a
2010 * reserve/commit pair.
2011 */
2012 return -1;
2013 }
2014
2015 /*
2016 * Need to write the subbuffer start header on finalize.
2017 */
2018 offsets->switch_old_start = 1;
2019 }
2020 offsets->begin = subbuf_align(offsets->begin, chan);
2021 /* Note: old points to the next subbuf at offset 0 */
2022 offsets->end = offsets->begin;
2023 return 0;
2024 }
2025
2026 /*
2027 * Force a sub-buffer switch. This operation is completely reentrant : can be
2028 * called while tracing is active with absolutely no lock held.
2029 *
2030 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2031 * some atomic operations, this function must be called from the CPU
2032 * which owns the buffer for a ACTIVE flush. However, for
2033 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2034 * from any CPU.
2035 */
2036 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode,
2037 struct lttng_ust_shm_handle *handle)
2038 {
2039 struct channel *chan;
2040 const struct lttng_ust_lib_ring_buffer_config *config;
2041 struct switch_offsets offsets;
2042 unsigned long oldidx;
2043 uint64_t tsc;
2044
2045 chan = shmp(handle, buf->backend.chan);
2046 if (!chan)
2047 return;
2048 config = &chan->backend.config;
2049
2050 offsets.size = 0;
2051
2052 /*
2053 * Perform retryable operations.
2054 */
2055 do {
2056 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
2057 &tsc, handle))
2058 return; /* Switch not needed */
2059 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
2060 != offsets.old);
2061
2062 /*
2063 * Atomically update last_tsc. This update races against concurrent
2064 * atomic updates, but the race will always cause supplementary full TSC
2065 * records, never the opposite (missing a full TSC record when it would
2066 * be needed).
2067 */
2068 save_last_tsc(config, buf, tsc);
2069
2070 /*
2071 * Push the reader if necessary
2072 */
2073 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
2074
2075 oldidx = subbuf_index(offsets.old, chan);
2076 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle);
2077
2078 /*
2079 * May need to populate header start on SWITCH_FLUSH.
2080 */
2081 if (offsets.switch_old_start) {
2082 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle);
2083 offsets.old += config->cb.subbuffer_header_size();
2084 }
2085
2086 /*
2087 * Switch old subbuffer.
2088 */
2089 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle);
2090 }
2091
2092 static
2093 bool handle_blocking_retry(int *timeout_left_ms)
2094 {
2095 int timeout = *timeout_left_ms, delay;
2096
2097 if (caa_likely(!timeout))
2098 return false; /* Do not retry, discard event. */
2099 if (timeout < 0) /* Wait forever. */
2100 delay = RETRY_DELAY_MS;
2101 else
2102 delay = min_t(int, timeout, RETRY_DELAY_MS);
2103 (void) poll(NULL, 0, delay);
2104 if (timeout > 0)
2105 *timeout_left_ms -= delay;
2106 return true; /* Retry. */
2107 }
2108
2109 /*
2110 * Returns :
2111 * 0 if ok
2112 * -ENOSPC if event size is too large for packet.
2113 * -ENOBUFS if there is currently not enough space in buffer for the event.
2114 * -EIO if data cannot be written into the buffer for any other reason.
2115 */
2116 static
2117 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf,
2118 struct channel *chan,
2119 struct switch_offsets *offsets,
2120 struct lttng_ust_lib_ring_buffer_ctx *ctx,
2121 void *client_ctx)
2122 {
2123 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
2124 struct lttng_ust_shm_handle *handle = ctx->handle;
2125 unsigned long reserve_commit_diff, offset_cmp;
2126 int timeout_left_ms = lttng_ust_ringbuffer_get_timeout(chan);
2127
2128 retry:
2129 offsets->begin = offset_cmp = v_read(config, &buf->offset);
2130 offsets->old = offsets->begin;
2131 offsets->switch_new_start = 0;
2132 offsets->switch_new_end = 0;
2133 offsets->switch_old_end = 0;
2134 offsets->pre_header_padding = 0;
2135
2136 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
2137 if ((int64_t) ctx->tsc == -EIO)
2138 return -EIO;
2139
2140 if (last_tsc_overflow(config, buf, ctx->tsc))
2141 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
2142
2143 if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
2144 offsets->switch_new_start = 1; /* For offsets->begin */
2145 } else {
2146 offsets->size = config->cb.record_header_size(config, chan,
2147 offsets->begin,
2148 &offsets->pre_header_padding,
2149 ctx, client_ctx);
2150 offsets->size +=
2151 lib_ring_buffer_align(offsets->begin + offsets->size,
2152 ctx->largest_align)
2153 + ctx->data_size;
2154 if (caa_unlikely(subbuf_offset(offsets->begin, chan) +
2155 offsets->size > chan->backend.subbuf_size)) {
2156 offsets->switch_old_end = 1; /* For offsets->old */
2157 offsets->switch_new_start = 1; /* For offsets->begin */
2158 }
2159 }
2160 if (caa_unlikely(offsets->switch_new_start)) {
2161 unsigned long sb_index, commit_count;
2162 struct commit_counters_cold *cc_cold;
2163
2164 /*
2165 * We are typically not filling the previous buffer completely.
2166 */
2167 if (caa_likely(offsets->switch_old_end))
2168 offsets->begin = subbuf_align(offsets->begin, chan);
2169 offsets->begin = offsets->begin
2170 + config->cb.subbuffer_header_size();
2171 /* Test new buffer integrity */
2172 sb_index = subbuf_index(offsets->begin, chan);
2173 /*
2174 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2175 * lib_ring_buffer_check_deliver() has the matching
2176 * memory barriers required around commit_cold cc_sb
2177 * updates to ensure reserve and commit counter updates
2178 * are not seen reordered when updated by another CPU.
2179 */
2180 cmm_smp_rmb();
2181 cc_cold = shmp_index(handle, buf->commit_cold, sb_index);
2182 if (!cc_cold)
2183 return -1;
2184 commit_count = v_read(config, &cc_cold->cc_sb);
2185 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2186 cmm_smp_rmb();
2187 if (caa_unlikely(offset_cmp != v_read(config, &buf->offset))) {
2188 /*
2189 * The reserve counter have been concurrently updated
2190 * while we read the commit counter. This means the
2191 * commit counter we read might not match buf->offset
2192 * due to concurrent update. We therefore need to retry.
2193 */
2194 goto retry;
2195 }
2196 reserve_commit_diff =
2197 (buf_trunc(offsets->begin, chan)
2198 >> chan->backend.num_subbuf_order)
2199 - (commit_count & chan->commit_count_mask);
2200 if (caa_likely(reserve_commit_diff == 0)) {
2201 /* Next subbuffer not being written to. */
2202 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
2203 subbuf_trunc(offsets->begin, chan)
2204 - subbuf_trunc((unsigned long)
2205 uatomic_read(&buf->consumed), chan)
2206 >= chan->backend.buf_size)) {
2207 unsigned long nr_lost;
2208
2209 if (handle_blocking_retry(&timeout_left_ms))
2210 goto retry;
2211
2212 /*
2213 * We do not overwrite non consumed buffers
2214 * and we are full : record is lost.
2215 */
2216 nr_lost = v_read(config, &buf->records_lost_full);
2217 v_inc(config, &buf->records_lost_full);
2218 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2219 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2220 nr_lost + 1, chan->backend.name,
2221 buf->backend.cpu);
2222 }
2223 return -ENOBUFS;
2224 } else {
2225 /*
2226 * Next subbuffer not being written to, and we
2227 * are either in overwrite mode or the buffer is
2228 * not full. It's safe to write in this new
2229 * subbuffer.
2230 */
2231 }
2232 } else {
2233 unsigned long nr_lost;
2234
2235 /*
2236 * Next subbuffer reserve offset does not match the
2237 * commit offset, and this did not involve update to the
2238 * reserve counter. Drop record in producer-consumer and
2239 * overwrite mode. Caused by either a writer OOPS or too
2240 * many nested writes over a reserve/commit pair.
2241 */
2242 nr_lost = v_read(config, &buf->records_lost_wrap);
2243 v_inc(config, &buf->records_lost_wrap);
2244 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2245 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2246 nr_lost + 1, chan->backend.name,
2247 buf->backend.cpu);
2248 }
2249 return -EIO;
2250 }
2251 offsets->size =
2252 config->cb.record_header_size(config, chan,
2253 offsets->begin,
2254 &offsets->pre_header_padding,
2255 ctx, client_ctx);
2256 offsets->size +=
2257 lib_ring_buffer_align(offsets->begin + offsets->size,
2258 ctx->largest_align)
2259 + ctx->data_size;
2260 if (caa_unlikely(subbuf_offset(offsets->begin, chan)
2261 + offsets->size > chan->backend.subbuf_size)) {
2262 unsigned long nr_lost;
2263
2264 /*
2265 * Record too big for subbuffers, report error, don't
2266 * complete the sub-buffer switch.
2267 */
2268 nr_lost = v_read(config, &buf->records_lost_big);
2269 v_inc(config, &buf->records_lost_big);
2270 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2271 DBG("%lu or more records lost in (%s:%d) record size "
2272 " of %zu bytes is too large for buffer\n",
2273 nr_lost + 1, chan->backend.name,
2274 buf->backend.cpu, offsets->size);
2275 }
2276 return -ENOSPC;
2277 } else {
2278 /*
2279 * We just made a successful buffer switch and the
2280 * record fits in the new subbuffer. Let's write.
2281 */
2282 }
2283 } else {
2284 /*
2285 * Record fits in the current buffer and we are not on a switch
2286 * boundary. It's safe to write.
2287 */
2288 }
2289 offsets->end = offsets->begin + offsets->size;
2290
2291 if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) {
2292 /*
2293 * The offset_end will fall at the very beginning of the next
2294 * subbuffer.
2295 */
2296 offsets->switch_new_end = 1; /* For offsets->begin */
2297 }
2298 return 0;
2299 }
2300
2301 /**
2302 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2303 * @ctx: ring buffer context.
2304 *
2305 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2306 * -EIO for other errors, else returns 0.
2307 * It will take care of sub-buffer switching.
2308 */
2309 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx,
2310 void *client_ctx)
2311 {
2312 struct channel *chan = ctx->chan;
2313 struct lttng_ust_shm_handle *handle = ctx->handle;
2314 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
2315 struct lttng_ust_lib_ring_buffer *buf;
2316 struct switch_offsets offsets;
2317 int ret;
2318
2319 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
2320 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
2321 else
2322 buf = shmp(handle, chan->backend.buf[0].shmp);
2323 if (!buf)
2324 return -EIO;
2325 ctx->buf = buf;
2326
2327 offsets.size = 0;
2328
2329 do {
2330 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
2331 ctx, client_ctx);
2332 if (caa_unlikely(ret))
2333 return ret;
2334 } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
2335 offsets.end)
2336 != offsets.old));
2337
2338 /*
2339 * Atomically update last_tsc. This update races against concurrent
2340 * atomic updates, but the race will always cause supplementary full TSC
2341 * records, never the opposite (missing a full TSC record when it would
2342 * be needed).
2343 */
2344 save_last_tsc(config, buf, ctx->tsc);
2345
2346 /*
2347 * Push the reader if necessary
2348 */
2349 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
2350
2351 /*
2352 * Clear noref flag for this subbuffer.
2353 */
2354 lib_ring_buffer_clear_noref(config, &buf->backend,
2355 subbuf_index(offsets.end - 1, chan),
2356 handle);
2357
2358 /*
2359 * Switch old subbuffer if needed.
2360 */
2361 if (caa_unlikely(offsets.switch_old_end)) {
2362 lib_ring_buffer_clear_noref(config, &buf->backend,
2363 subbuf_index(offsets.old - 1, chan),
2364 handle);
2365 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle);
2366 }
2367
2368 /*
2369 * Populate new subbuffer.
2370 */
2371 if (caa_unlikely(offsets.switch_new_start))
2372 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle);
2373
2374 if (caa_unlikely(offsets.switch_new_end))
2375 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle);
2376
2377 ctx->slot_size = offsets.size;
2378 ctx->pre_offset = offsets.begin;
2379 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
2380 return 0;
2381 }
2382
2383 static
2384 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config *config,
2385 struct lttng_ust_lib_ring_buffer *buf,
2386 unsigned long commit_count,
2387 unsigned long idx,
2388 struct lttng_ust_shm_handle *handle)
2389 {
2390 struct commit_counters_hot *cc_hot;
2391
2392 if (config->oops != RING_BUFFER_OOPS_CONSISTENCY)
2393 return;
2394 cc_hot = shmp_index(handle, buf->commit_hot, idx);
2395 if (!cc_hot)
2396 return;
2397 v_set(config, &cc_hot->seq, commit_count);
2398 }
2399
2400 /*
2401 * The ring buffer can count events recorded and overwritten per buffer,
2402 * but it is disabled by default due to its performance overhead.
2403 */
2404 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2405 static
2406 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config,
2407 struct lttng_ust_lib_ring_buffer *buf,
2408 unsigned long idx,
2409 struct lttng_ust_shm_handle *handle)
2410 {
2411 v_add(config, subbuffer_get_records_count(config,
2412 &buf->backend, idx, handle),
2413 &buf->records_count);
2414 v_add(config, subbuffer_count_records_overrun(config,
2415 &buf->backend, idx, handle),
2416 &buf->records_overrun);
2417 }
2418 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2419 static
2420 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config,
2421 struct lttng_ust_lib_ring_buffer *buf,
2422 unsigned long idx,
2423 struct lttng_ust_shm_handle *handle)
2424 {
2425 }
2426 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2427
2428 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config *config,
2429 struct lttng_ust_lib_ring_buffer *buf,
2430 struct channel *chan,
2431 unsigned long offset,
2432 unsigned long commit_count,
2433 unsigned long idx,
2434 struct lttng_ust_shm_handle *handle,
2435 uint64_t tsc)
2436 {
2437 unsigned long old_commit_count = commit_count
2438 - chan->backend.subbuf_size;
2439 struct commit_counters_cold *cc_cold;
2440
2441 /*
2442 * If we succeeded at updating cc_sb below, we are the subbuffer
2443 * writer delivering the subbuffer. Deals with concurrent
2444 * updates of the "cc" value without adding a add_return atomic
2445 * operation to the fast path.
2446 *
2447 * We are doing the delivery in two steps:
2448 * - First, we cmpxchg() cc_sb to the new value
2449 * old_commit_count + 1. This ensures that we are the only
2450 * subbuffer user successfully filling the subbuffer, but we
2451 * do _not_ set the cc_sb value to "commit_count" yet.
2452 * Therefore, other writers that would wrap around the ring
2453 * buffer and try to start writing to our subbuffer would
2454 * have to drop records, because it would appear as
2455 * non-filled.
2456 * We therefore have exclusive access to the subbuffer control
2457 * structures. This mutual exclusion with other writers is
2458 * crucially important to perform record overruns count in
2459 * flight recorder mode locklessly.
2460 * - When we are ready to release the subbuffer (either for
2461 * reading or for overrun by other writers), we simply set the
2462 * cc_sb value to "commit_count" and perform delivery.
2463 *
2464 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2465 * This guarantees that old_commit_count + 1 != commit_count.
2466 */
2467
2468 /*
2469 * Order prior updates to reserve count prior to the
2470 * commit_cold cc_sb update.
2471 */
2472 cmm_smp_wmb();
2473 cc_cold = shmp_index(handle, buf->commit_cold, idx);
2474 if (!cc_cold)
2475 return;
2476 if (caa_likely(v_cmpxchg(config, &cc_cold->cc_sb,
2477 old_commit_count, old_commit_count + 1)
2478 == old_commit_count)) {
2479 uint64_t *ts_end;
2480
2481 /*
2482 * Start of exclusive subbuffer access. We are
2483 * guaranteed to be the last writer in this subbuffer
2484 * and any other writer trying to access this subbuffer
2485 * in this state is required to drop records.
2486 *
2487 * We can read the ts_end for the current sub-buffer
2488 * which has been saved by the very last space
2489 * reservation for the current sub-buffer.
2490 *
2491 * Order increment of commit counter before reading ts_end.
2492 */
2493 cmm_smp_mb();
2494 ts_end = shmp_index(handle, buf->ts_end, idx);
2495 if (!ts_end)
2496 return;
2497 deliver_count_events(config, buf, idx, handle);
2498 config->cb.buffer_end(buf, *ts_end, idx,
2499 lib_ring_buffer_get_data_size(config,
2500 buf,
2501 idx,
2502 handle),
2503 handle);
2504
2505 /*
2506 * Increment the packet counter while we have exclusive
2507 * access.
2508 */
2509 subbuffer_inc_packet_count(config, &buf->backend, idx, handle);
2510
2511 /*
2512 * Set noref flag and offset for this subbuffer id.
2513 * Contains a memory barrier that ensures counter stores
2514 * are ordered before set noref and offset.
2515 */
2516 lib_ring_buffer_set_noref_offset(config, &buf->backend, idx,
2517 buf_trunc_val(offset, chan), handle);
2518
2519 /*
2520 * Order set_noref and record counter updates before the
2521 * end of subbuffer exclusive access. Orders with
2522 * respect to writers coming into the subbuffer after
2523 * wrap around, and also order wrt concurrent readers.
2524 */
2525 cmm_smp_mb();
2526 /* End of exclusive subbuffer access */
2527 v_set(config, &cc_cold->cc_sb, commit_count);
2528 /*
2529 * Order later updates to reserve count after
2530 * the commit cold cc_sb update.
2531 */
2532 cmm_smp_wmb();
2533 lib_ring_buffer_vmcore_check_deliver(config, buf,
2534 commit_count, idx, handle);
2535
2536 /*
2537 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2538 */
2539 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER
2540 && uatomic_read(&buf->active_readers)
2541 && lib_ring_buffer_poll_deliver(config, buf, chan, handle)) {
2542 lib_ring_buffer_wakeup(buf, handle);
2543 }
2544 }
2545 }
2546
2547 /*
2548 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2549 */
2550 void lttng_fixup_ringbuffer_tls(void)
2551 {
2552 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting)));
2553 }
2554
2555 void lib_ringbuffer_signal_init(void)
2556 {
2557 sigset_t mask;
2558 int ret;
2559
2560 /*
2561 * Block signal for entire process, so only our thread processes
2562 * it.
2563 */
2564 rb_setmask(&mask);
2565 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
2566 if (ret) {
2567 errno = ret;
2568 PERROR("pthread_sigmask");
2569 }
2570 }
This page took 0.115603 seconds and 4 git commands to generate.