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