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