Fix: uninitialized variable in lib_ring_buffer_channel_switch_timer_start
[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 memset(&sev, 0, sizeof(sev));
808 sev.sigev_notify = SIGEV_SIGNAL;
809 sev.sigev_signo = LTTNG_UST_RB_SIG_FLUSH;
810 sev.sigev_value.sival_ptr = chan;
811 ret = timer_create(CLOCKID, &sev, &chan->switch_timer);
812 if (ret == -1) {
813 PERROR("timer_create");
814 }
815
816 its.it_value.tv_sec = chan->switch_timer_interval / 1000000;
817 its.it_value.tv_nsec = (chan->switch_timer_interval % 1000000) * 1000;
818 its.it_interval.tv_sec = its.it_value.tv_sec;
819 its.it_interval.tv_nsec = its.it_value.tv_nsec;
820
821 ret = timer_settime(chan->switch_timer, 0, &its, NULL);
822 if (ret == -1) {
823 PERROR("timer_settime");
824 }
825 }
826
827 static
828 void lib_ring_buffer_channel_switch_timer_stop(struct channel *chan)
829 {
830 int ret;
831
832 if (!chan->switch_timer_interval || !chan->switch_timer_enabled)
833 return;
834
835 ret = timer_delete(chan->switch_timer);
836 if (ret == -1) {
837 PERROR("timer_delete");
838 }
839
840 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_FLUSH);
841
842 chan->switch_timer = 0;
843 chan->switch_timer_enabled = 0;
844 }
845
846 static
847 void lib_ring_buffer_channel_read_timer_start(struct channel *chan)
848 {
849 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
850 struct sigevent sev;
851 struct itimerspec its;
852 int ret;
853
854 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
855 || !chan->read_timer_interval || chan->read_timer_enabled)
856 return;
857
858 chan->read_timer_enabled = 1;
859
860 lib_ring_buffer_setup_timer_thread();
861
862 sev.sigev_notify = SIGEV_SIGNAL;
863 sev.sigev_signo = LTTNG_UST_RB_SIG_READ;
864 sev.sigev_value.sival_ptr = chan;
865 ret = timer_create(CLOCKID, &sev, &chan->read_timer);
866 if (ret == -1) {
867 PERROR("timer_create");
868 }
869
870 its.it_value.tv_sec = chan->read_timer_interval / 1000000;
871 its.it_value.tv_nsec = (chan->read_timer_interval % 1000000) * 1000;
872 its.it_interval.tv_sec = its.it_value.tv_sec;
873 its.it_interval.tv_nsec = its.it_value.tv_nsec;
874
875 ret = timer_settime(chan->read_timer, 0, &its, NULL);
876 if (ret == -1) {
877 PERROR("timer_settime");
878 }
879 }
880
881 static
882 void lib_ring_buffer_channel_read_timer_stop(struct channel *chan)
883 {
884 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
885 int ret;
886
887 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
888 || !chan->read_timer_interval || !chan->read_timer_enabled)
889 return;
890
891 ret = timer_delete(chan->read_timer);
892 if (ret == -1) {
893 PERROR("timer_delete");
894 }
895
896 /*
897 * do one more check to catch data that has been written in the last
898 * timer period.
899 */
900 lib_ring_buffer_channel_do_read(chan);
901
902 lib_ring_buffer_wait_signal_thread_qs(LTTNG_UST_RB_SIG_READ);
903
904 chan->read_timer = 0;
905 chan->read_timer_enabled = 0;
906 }
907
908 static void channel_unregister_notifiers(struct channel *chan,
909 struct lttng_ust_shm_handle *handle)
910 {
911 lib_ring_buffer_channel_switch_timer_stop(chan);
912 lib_ring_buffer_channel_read_timer_stop(chan);
913 }
914
915 static void channel_print_errors(struct channel *chan,
916 struct lttng_ust_shm_handle *handle)
917 {
918 const struct lttng_ust_lib_ring_buffer_config *config =
919 &chan->backend.config;
920 int cpu;
921
922 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
923 for_each_possible_cpu(cpu) {
924 struct lttng_ust_lib_ring_buffer *buf =
925 shmp(handle, chan->backend.buf[cpu].shmp);
926 if (buf)
927 lib_ring_buffer_print_errors(chan, buf, cpu, handle);
928 }
929 } else {
930 struct lttng_ust_lib_ring_buffer *buf =
931 shmp(handle, chan->backend.buf[0].shmp);
932
933 if (buf)
934 lib_ring_buffer_print_errors(chan, buf, -1, handle);
935 }
936 }
937
938 static void channel_free(struct channel *chan,
939 struct lttng_ust_shm_handle *handle,
940 int consumer)
941 {
942 channel_backend_free(&chan->backend, handle);
943 /* chan is freed by shm teardown */
944 shm_object_table_destroy(handle->table, consumer);
945 free(handle);
946 }
947
948 /**
949 * channel_create - Create channel.
950 * @config: ring buffer instance configuration
951 * @name: name of the channel
952 * @priv_data: ring buffer client private data area pointer (output)
953 * @priv_data_size: length, in bytes, of the private data area.
954 * @priv_data_init: initialization data for private data.
955 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
956 * address mapping. It is used only by RING_BUFFER_STATIC
957 * configuration. It can be set to NULL for other backends.
958 * @subbuf_size: subbuffer size
959 * @num_subbuf: number of subbuffers
960 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
961 * padding to let readers get those sub-buffers.
962 * Used for live streaming.
963 * @read_timer_interval: Time interval (in us) to wake up pending readers.
964 * @stream_fds: array of stream file descriptors.
965 * @nr_stream_fds: number of file descriptors in array.
966 *
967 * Holds cpu hotplug.
968 * Returns NULL on failure.
969 */
970 struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
971 const char *name,
972 void **priv_data,
973 size_t priv_data_align,
974 size_t priv_data_size,
975 void *priv_data_init,
976 void *buf_addr, size_t subbuf_size,
977 size_t num_subbuf, unsigned int switch_timer_interval,
978 unsigned int read_timer_interval,
979 const int *stream_fds, int nr_stream_fds,
980 int64_t blocking_timeout)
981 {
982 int ret;
983 size_t shmsize, chansize;
984 struct channel *chan;
985 struct lttng_ust_shm_handle *handle;
986 struct shm_object *shmobj;
987 unsigned int nr_streams;
988 int64_t blocking_timeout_ms;
989
990 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
991 nr_streams = num_possible_cpus();
992 else
993 nr_streams = 1;
994
995 if (nr_stream_fds != nr_streams)
996 return NULL;
997
998 if (blocking_timeout < -1) {
999 return NULL;
1000 }
1001 /* usec to msec */
1002 if (blocking_timeout == -1) {
1003 blocking_timeout_ms = -1;
1004 } else {
1005 blocking_timeout_ms = blocking_timeout / 1000;
1006 if (blocking_timeout_ms != (int32_t) blocking_timeout_ms) {
1007 return NULL;
1008 }
1009 }
1010
1011 if (lib_ring_buffer_check_config(config, switch_timer_interval,
1012 read_timer_interval))
1013 return NULL;
1014
1015 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
1016 if (!handle)
1017 return NULL;
1018
1019 /* Allocate table for channel + per-cpu buffers */
1020 handle->table = shm_object_table_create(1 + num_possible_cpus());
1021 if (!handle->table)
1022 goto error_table_alloc;
1023
1024 /* Calculate the shm allocation layout */
1025 shmsize = sizeof(struct channel);
1026 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp));
1027 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams;
1028 chansize = shmsize;
1029 if (priv_data_align)
1030 shmsize += lttng_ust_offset_align(shmsize, priv_data_align);
1031 shmsize += priv_data_size;
1032
1033 /* Allocate normal memory for channel (not shared) */
1034 shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM,
1035 -1, -1);
1036 if (!shmobj)
1037 goto error_append;
1038 /* struct channel is at object 0, offset 0 (hardcoded) */
1039 set_shmp(handle->chan, zalloc_shm(shmobj, chansize));
1040 assert(handle->chan._ref.index == 0);
1041 assert(handle->chan._ref.offset == 0);
1042 chan = shmp(handle, handle->chan);
1043 if (!chan)
1044 goto error_append;
1045 chan->nr_streams = nr_streams;
1046
1047 /* space for private data */
1048 if (priv_data_size) {
1049 DECLARE_SHMP(void, priv_data_alloc);
1050
1051 align_shm(shmobj, priv_data_align);
1052 chan->priv_data_offset = shmobj->allocated_len;
1053 set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size));
1054 if (!shmp(handle, priv_data_alloc))
1055 goto error_append;
1056 *priv_data = channel_get_private(chan);
1057 memcpy(*priv_data, priv_data_init, priv_data_size);
1058 } else {
1059 chan->priv_data_offset = -1;
1060 if (priv_data)
1061 *priv_data = NULL;
1062 }
1063
1064 chan->u.s.blocking_timeout_ms = (int32_t) blocking_timeout_ms;
1065
1066 ret = channel_backend_init(&chan->backend, name, config,
1067 subbuf_size, num_subbuf, handle,
1068 stream_fds);
1069 if (ret)
1070 goto error_backend_init;
1071
1072 chan->handle = handle;
1073 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
1074
1075 chan->switch_timer_interval = switch_timer_interval;
1076 chan->read_timer_interval = read_timer_interval;
1077 lib_ring_buffer_channel_switch_timer_start(chan);
1078 lib_ring_buffer_channel_read_timer_start(chan);
1079
1080 return handle;
1081
1082 error_backend_init:
1083 error_append:
1084 shm_object_table_destroy(handle->table, 1);
1085 error_table_alloc:
1086 free(handle);
1087 return NULL;
1088 }
1089
1090 struct lttng_ust_shm_handle *channel_handle_create(void *data,
1091 uint64_t memory_map_size,
1092 int wakeup_fd)
1093 {
1094 struct lttng_ust_shm_handle *handle;
1095 struct shm_object *object;
1096
1097 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
1098 if (!handle)
1099 return NULL;
1100
1101 /* Allocate table for channel + per-cpu buffers */
1102 handle->table = shm_object_table_create(1 + num_possible_cpus());
1103 if (!handle->table)
1104 goto error_table_alloc;
1105 /* Add channel object */
1106 object = shm_object_table_append_mem(handle->table, data,
1107 memory_map_size, wakeup_fd);
1108 if (!object)
1109 goto error_table_object;
1110 /* struct channel is at object 0, offset 0 (hardcoded) */
1111 handle->chan._ref.index = 0;
1112 handle->chan._ref.offset = 0;
1113 return handle;
1114
1115 error_table_object:
1116 shm_object_table_destroy(handle->table, 0);
1117 error_table_alloc:
1118 free(handle);
1119 return NULL;
1120 }
1121
1122 int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
1123 int shm_fd, int wakeup_fd, uint32_t stream_nr,
1124 uint64_t memory_map_size)
1125 {
1126 struct shm_object *object;
1127
1128 /* Add stream object */
1129 object = shm_object_table_append_shm(handle->table,
1130 shm_fd, wakeup_fd, stream_nr,
1131 memory_map_size);
1132 if (!object)
1133 return -EINVAL;
1134 return 0;
1135 }
1136
1137 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle)
1138 {
1139 assert(handle->table);
1140 return handle->table->allocated_len - 1;
1141 }
1142
1143 static
1144 void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle,
1145 int consumer)
1146 {
1147 channel_free(chan, handle, consumer);
1148 }
1149
1150 /**
1151 * channel_destroy - Finalize, wait for q.s. and destroy channel.
1152 * @chan: channel to destroy
1153 *
1154 * Holds cpu hotplug.
1155 * Call "destroy" callback, finalize channels, decrement the channel
1156 * reference count. Note that when readers have completed data
1157 * consumption of finalized channels, get_subbuf() will return -ENODATA.
1158 * They should release their handle at that point.
1159 */
1160 void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
1161 int consumer)
1162 {
1163 if (consumer) {
1164 /*
1165 * Note: the consumer takes care of finalizing and
1166 * switching the buffers.
1167 */
1168 channel_unregister_notifiers(chan, handle);
1169 /*
1170 * The consumer prints errors.
1171 */
1172 channel_print_errors(chan, handle);
1173 }
1174
1175 /*
1176 * sessiond/consumer are keeping a reference on the shm file
1177 * descriptor directly. No need to refcount.
1178 */
1179 channel_release(chan, handle, consumer);
1180 return;
1181 }
1182
1183 struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
1184 const struct lttng_ust_lib_ring_buffer_config *config,
1185 struct channel *chan, int cpu,
1186 struct lttng_ust_shm_handle *handle,
1187 int *shm_fd, int *wait_fd,
1188 int *wakeup_fd,
1189 uint64_t *memory_map_size)
1190 {
1191 struct shm_ref *ref;
1192
1193 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1194 cpu = 0;
1195 } else {
1196 if (cpu >= num_possible_cpus())
1197 return NULL;
1198 }
1199 ref = &chan->backend.buf[cpu].shmp._ref;
1200 *shm_fd = shm_get_shm_fd(handle, ref);
1201 *wait_fd = shm_get_wait_fd(handle, ref);
1202 *wakeup_fd = shm_get_wakeup_fd(handle, ref);
1203 if (shm_get_shm_size(handle, ref, memory_map_size))
1204 return NULL;
1205 return shmp(handle, chan->backend.buf[cpu].shmp);
1206 }
1207
1208 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1209 struct channel *chan,
1210 struct lttng_ust_shm_handle *handle)
1211 {
1212 struct shm_ref *ref;
1213
1214 ref = &handle->chan._ref;
1215 return shm_close_wait_fd(handle, ref);
1216 }
1217
1218 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1219 struct channel *chan,
1220 struct lttng_ust_shm_handle *handle)
1221 {
1222 struct shm_ref *ref;
1223
1224 ref = &handle->chan._ref;
1225 return shm_close_wakeup_fd(handle, ref);
1226 }
1227
1228 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1229 struct channel *chan,
1230 struct lttng_ust_shm_handle *handle,
1231 int cpu)
1232 {
1233 struct shm_ref *ref;
1234
1235 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1236 cpu = 0;
1237 } else {
1238 if (cpu >= num_possible_cpus())
1239 return -EINVAL;
1240 }
1241 ref = &chan->backend.buf[cpu].shmp._ref;
1242 return shm_close_wait_fd(handle, ref);
1243 }
1244
1245 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
1246 struct channel *chan,
1247 struct lttng_ust_shm_handle *handle,
1248 int cpu)
1249 {
1250 struct shm_ref *ref;
1251 int ret;
1252
1253 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
1254 cpu = 0;
1255 } else {
1256 if (cpu >= num_possible_cpus())
1257 return -EINVAL;
1258 }
1259 ref = &chan->backend.buf[cpu].shmp._ref;
1260 pthread_mutex_lock(&wakeup_fd_mutex);
1261 ret = shm_close_wakeup_fd(handle, ref);
1262 pthread_mutex_unlock(&wakeup_fd_mutex);
1263 return ret;
1264 }
1265
1266 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
1267 struct lttng_ust_shm_handle *handle)
1268 {
1269 if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0)
1270 return -EBUSY;
1271 cmm_smp_mb();
1272 return 0;
1273 }
1274
1275 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
1276 struct lttng_ust_shm_handle *handle)
1277 {
1278 struct channel *chan = shmp(handle, buf->backend.chan);
1279
1280 if (!chan)
1281 return;
1282 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
1283 cmm_smp_mb();
1284 uatomic_dec(&buf->active_readers);
1285 }
1286
1287 /**
1288 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
1289 * @buf: ring buffer
1290 * @consumed: consumed count indicating the position where to read
1291 * @produced: produced count, indicates position when to stop reading
1292 *
1293 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1294 * data to read at consumed position, or 0 if the get operation succeeds.
1295 */
1296
1297 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
1298 unsigned long *consumed, unsigned long *produced,
1299 struct lttng_ust_shm_handle *handle)
1300 {
1301 struct channel *chan;
1302 const struct lttng_ust_lib_ring_buffer_config *config;
1303 unsigned long consumed_cur, write_offset;
1304 int finalized;
1305
1306 chan = shmp(handle, buf->backend.chan);
1307 if (!chan)
1308 return -EPERM;
1309 config = &chan->backend.config;
1310 finalized = CMM_ACCESS_ONCE(buf->finalized);
1311 /*
1312 * Read finalized before counters.
1313 */
1314 cmm_smp_rmb();
1315 consumed_cur = uatomic_read(&buf->consumed);
1316 /*
1317 * No need to issue a memory barrier between consumed count read and
1318 * write offset read, because consumed count can only change
1319 * concurrently in overwrite mode, and we keep a sequence counter
1320 * identifier derived from the write offset to check we are getting
1321 * the same sub-buffer we are expecting (the sub-buffers are atomically
1322 * "tagged" upon writes, tags are checked upon read).
1323 */
1324 write_offset = v_read(config, &buf->offset);
1325
1326 /*
1327 * Check that we are not about to read the same subbuffer in
1328 * which the writer head is.
1329 */
1330 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1331 == 0)
1332 goto nodata;
1333
1334 *consumed = consumed_cur;
1335 *produced = subbuf_trunc(write_offset, chan);
1336
1337 return 0;
1338
1339 nodata:
1340 /*
1341 * The memory barriers __wait_event()/wake_up_interruptible() take care
1342 * of "raw_spin_is_locked" memory ordering.
1343 */
1344 if (finalized)
1345 return -ENODATA;
1346 else
1347 return -EAGAIN;
1348 }
1349
1350 /**
1351 * Performs the same function as lib_ring_buffer_snapshot(), but the positions
1352 * are saved regardless of whether the consumed and produced positions are
1353 * in the same subbuffer.
1354 * @buf: ring buffer
1355 * @consumed: consumed byte count indicating the last position read
1356 * @produced: produced byte count indicating the last position written
1357 *
1358 * This function is meant to provide information on the exact producer and
1359 * consumer positions without regard for the "snapshot" feature.
1360 */
1361 int lib_ring_buffer_snapshot_sample_positions(
1362 struct lttng_ust_lib_ring_buffer *buf,
1363 unsigned long *consumed, unsigned long *produced,
1364 struct lttng_ust_shm_handle *handle)
1365 {
1366 struct channel *chan;
1367 const struct lttng_ust_lib_ring_buffer_config *config;
1368
1369 chan = shmp(handle, buf->backend.chan);
1370 if (!chan)
1371 return -EPERM;
1372 config = &chan->backend.config;
1373 cmm_smp_rmb();
1374 *consumed = uatomic_read(&buf->consumed);
1375 /*
1376 * No need to issue a memory barrier between consumed count read and
1377 * write offset read, because consumed count can only change
1378 * concurrently in overwrite mode, and we keep a sequence counter
1379 * identifier derived from the write offset to check we are getting
1380 * the same sub-buffer we are expecting (the sub-buffers are atomically
1381 * "tagged" upon writes, tags are checked upon read).
1382 */
1383 *produced = v_read(config, &buf->offset);
1384 return 0;
1385 }
1386
1387 /**
1388 * lib_ring_buffer_move_consumer - move consumed counter forward
1389 * @buf: ring buffer
1390 * @consumed_new: new consumed count value
1391 */
1392 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1393 unsigned long consumed_new,
1394 struct lttng_ust_shm_handle *handle)
1395 {
1396 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1397 struct channel *chan;
1398 unsigned long consumed;
1399
1400 chan = shmp(handle, bufb->chan);
1401 if (!chan)
1402 return;
1403 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
1404
1405 /*
1406 * Only push the consumed value forward.
1407 * If the consumed cmpxchg fails, this is because we have been pushed by
1408 * the writer in flight recorder mode.
1409 */
1410 consumed = uatomic_read(&buf->consumed);
1411 while ((long) consumed - (long) consumed_new < 0)
1412 consumed = uatomic_cmpxchg(&buf->consumed, consumed,
1413 consumed_new);
1414 }
1415
1416 /**
1417 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1418 * @buf: ring buffer
1419 * @consumed: consumed count indicating the position where to read
1420 *
1421 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1422 * data to read at consumed position, or 0 if the get operation succeeds.
1423 */
1424 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1425 unsigned long consumed,
1426 struct lttng_ust_shm_handle *handle)
1427 {
1428 struct channel *chan;
1429 const struct lttng_ust_lib_ring_buffer_config *config;
1430 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
1431 int ret, finalized, nr_retry = LTTNG_UST_RING_BUFFER_GET_RETRY;
1432 struct commit_counters_cold *cc_cold;
1433
1434 chan = shmp(handle, buf->backend.chan);
1435 if (!chan)
1436 return -EPERM;
1437 config = &chan->backend.config;
1438 retry:
1439 finalized = CMM_ACCESS_ONCE(buf->finalized);
1440 /*
1441 * Read finalized before counters.
1442 */
1443 cmm_smp_rmb();
1444 consumed_cur = uatomic_read(&buf->consumed);
1445 consumed_idx = subbuf_index(consumed, chan);
1446 cc_cold = shmp_index(handle, buf->commit_cold, consumed_idx);
1447 if (!cc_cold)
1448 return -EPERM;
1449 commit_count = v_read(config, &cc_cold->cc_sb);
1450 /*
1451 * Make sure we read the commit count before reading the buffer
1452 * data and the write offset. Correct consumed offset ordering
1453 * wrt commit count is insured by the use of cmpxchg to update
1454 * the consumed offset.
1455 */
1456 /*
1457 * Local rmb to match the remote wmb to read the commit count
1458 * before the buffer data and the write offset.
1459 */
1460 cmm_smp_rmb();
1461
1462 write_offset = v_read(config, &buf->offset);
1463
1464 /*
1465 * Check that the buffer we are getting is after or at consumed_cur
1466 * position.
1467 */
1468 if ((long) subbuf_trunc(consumed, chan)
1469 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1470 goto nodata;
1471
1472 /*
1473 * Check that the subbuffer we are trying to consume has been
1474 * already fully committed. There are a few causes that can make
1475 * this unavailability situation occur:
1476 *
1477 * Temporary (short-term) situation:
1478 * - Application is running on a different CPU, between reserve
1479 * and commit ring buffer operations,
1480 * - Application is preempted between reserve and commit ring
1481 * buffer operations,
1482 *
1483 * Long-term situation:
1484 * - Application is stopped (SIGSTOP) between reserve and commit
1485 * ring buffer operations. Could eventually be resumed by
1486 * SIGCONT.
1487 * - Application is killed (SIGTERM, SIGINT, SIGKILL) between
1488 * reserve and commit ring buffer operation.
1489 *
1490 * From a consumer perspective, handling short-term
1491 * unavailability situations is performed by retrying a few
1492 * times after a delay. Handling long-term unavailability
1493 * situations is handled by failing to get the sub-buffer.
1494 *
1495 * In all of those situations, if the application is taking a
1496 * long time to perform its commit after ring buffer space
1497 * reservation, we can end up in a situation where the producer
1498 * will fill the ring buffer and try to write into the same
1499 * sub-buffer again (which has a missing commit). This is
1500 * handled by the producer in the sub-buffer switch handling
1501 * code of the reserve routine by detecting unbalanced
1502 * reserve/commit counters and discarding all further events
1503 * until the situation is resolved in those situations. Two
1504 * scenarios can occur:
1505 *
1506 * 1) The application causing the reserve/commit counters to be
1507 * unbalanced has been terminated. In this situation, all
1508 * further events will be discarded in the buffers, and no
1509 * further buffer data will be readable by the consumer
1510 * daemon. Tearing down the UST tracing session and starting
1511 * anew is a work-around for those situations. Note that this
1512 * only affects per-UID tracing. In per-PID tracing, the
1513 * application vanishes with the termination, and therefore
1514 * no more data needs to be written to the buffers.
1515 * 2) The application causing the unbalance has been delayed for
1516 * a long time, but will eventually try to increment the
1517 * commit counter after eventually writing to the sub-buffer.
1518 * This situation can cause events to be discarded until the
1519 * application resumes its operations.
1520 */
1521 if (((commit_count - chan->backend.subbuf_size)
1522 & chan->commit_count_mask)
1523 - (buf_trunc(consumed, chan)
1524 >> chan->backend.num_subbuf_order)
1525 != 0) {
1526 if (nr_retry-- > 0) {
1527 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1528 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1529 goto retry;
1530 } else {
1531 goto nodata;
1532 }
1533 }
1534
1535 /*
1536 * Check that we are not about to read the same subbuffer in
1537 * which the writer head is.
1538 */
1539 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed, chan)
1540 == 0)
1541 goto nodata;
1542
1543 /*
1544 * Failure to get the subbuffer causes a busy-loop retry without going
1545 * to a wait queue. These are caused by short-lived race windows where
1546 * the writer is getting access to a subbuffer we were trying to get
1547 * access to. Also checks that the "consumed" buffer count we are
1548 * looking for matches the one contained in the subbuffer id.
1549 *
1550 * The short-lived race window described here can be affected by
1551 * application signals and preemption, thus requiring to bound
1552 * the loop to a maximum number of retry.
1553 */
1554 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1555 consumed_idx, buf_trunc_val(consumed, chan),
1556 handle);
1557 if (ret) {
1558 if (nr_retry-- > 0) {
1559 if (nr_retry <= (LTTNG_UST_RING_BUFFER_GET_RETRY >> 1))
1560 (void) poll(NULL, 0, LTTNG_UST_RING_BUFFER_RETRY_DELAY_MS);
1561 goto retry;
1562 } else {
1563 goto nodata;
1564 }
1565 }
1566 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1567
1568 buf->get_subbuf_consumed = consumed;
1569 buf->get_subbuf = 1;
1570
1571 return 0;
1572
1573 nodata:
1574 /*
1575 * The memory barriers __wait_event()/wake_up_interruptible() take care
1576 * of "raw_spin_is_locked" memory ordering.
1577 */
1578 if (finalized)
1579 return -ENODATA;
1580 else
1581 return -EAGAIN;
1582 }
1583
1584 /**
1585 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1586 * @buf: ring buffer
1587 */
1588 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1589 struct lttng_ust_shm_handle *handle)
1590 {
1591 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1592 struct channel *chan;
1593 const struct lttng_ust_lib_ring_buffer_config *config;
1594 unsigned long sb_bindex, consumed_idx, consumed;
1595 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
1596 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
1597
1598 chan = shmp(handle, bufb->chan);
1599 if (!chan)
1600 return;
1601 config = &chan->backend.config;
1602 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
1603
1604 if (!buf->get_subbuf) {
1605 /*
1606 * Reader puts a subbuffer it did not get.
1607 */
1608 CHAN_WARN_ON(chan, 1);
1609 return;
1610 }
1611 consumed = buf->get_subbuf_consumed;
1612 buf->get_subbuf = 0;
1613
1614 /*
1615 * Clear the records_unread counter. (overruns counter)
1616 * Can still be non-zero if a file reader simply grabbed the data
1617 * without using iterators.
1618 * Can be below zero if an iterator is used on a snapshot more than
1619 * once.
1620 */
1621 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1622 rpages = shmp_index(handle, bufb->array, sb_bindex);
1623 if (!rpages)
1624 return;
1625 backend_pages = shmp(handle, rpages->shmp);
1626 if (!backend_pages)
1627 return;
1628 v_add(config, v_read(config, &backend_pages->records_unread),
1629 &bufb->records_read);
1630 v_set(config, &backend_pages->records_unread, 0);
1631 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1632 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1633 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1634
1635 /*
1636 * Exchange the reader subbuffer with the one we put in its place in the
1637 * writer subbuffer table. Expect the original consumed count. If
1638 * update_read_sb_index fails, this is because the writer updated the
1639 * subbuffer concurrently. We should therefore keep the subbuffer we
1640 * currently have: it has become invalid to try reading this sub-buffer
1641 * consumed count value anyway.
1642 */
1643 consumed_idx = subbuf_index(consumed, chan);
1644 update_read_sb_index(config, &buf->backend, &chan->backend,
1645 consumed_idx, buf_trunc_val(consumed, chan),
1646 handle);
1647 /*
1648 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1649 * if the writer concurrently updated it.
1650 */
1651 }
1652
1653 /*
1654 * cons_offset is an iterator on all subbuffer offsets between the reader
1655 * position and the writer position. (inclusive)
1656 */
1657 static
1658 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf,
1659 struct channel *chan,
1660 unsigned long cons_offset,
1661 int cpu,
1662 struct lttng_ust_shm_handle *handle)
1663 {
1664 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1665 unsigned long cons_idx, commit_count, commit_count_sb;
1666 struct commit_counters_hot *cc_hot;
1667 struct commit_counters_cold *cc_cold;
1668
1669 cons_idx = subbuf_index(cons_offset, chan);
1670 cc_hot = shmp_index(handle, buf->commit_hot, cons_idx);
1671 if (!cc_hot)
1672 return;
1673 cc_cold = shmp_index(handle, buf->commit_cold, cons_idx);
1674 if (!cc_cold)
1675 return;
1676 commit_count = v_read(config, &cc_hot->cc);
1677 commit_count_sb = v_read(config, &cc_cold->cc_sb);
1678
1679 if (subbuf_offset(commit_count, chan) != 0)
1680 DBG("ring buffer %s, cpu %d: "
1681 "commit count in subbuffer %lu,\n"
1682 "expecting multiples of %lu bytes\n"
1683 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1684 chan->backend.name, cpu, cons_idx,
1685 chan->backend.subbuf_size,
1686 commit_count, commit_count_sb);
1687
1688 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
1689 chan->backend.name, cpu, commit_count);
1690 }
1691
1692 static
1693 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf,
1694 struct channel *chan,
1695 void *priv, int cpu,
1696 struct lttng_ust_shm_handle *handle)
1697 {
1698 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1699 unsigned long write_offset, cons_offset;
1700
1701 /*
1702 * No need to order commit_count, write_offset and cons_offset reads
1703 * because we execute at teardown when no more writer nor reader
1704 * references are left.
1705 */
1706 write_offset = v_read(config, &buf->offset);
1707 cons_offset = uatomic_read(&buf->consumed);
1708 if (write_offset != cons_offset)
1709 DBG("ring buffer %s, cpu %d: "
1710 "non-consumed data\n"
1711 " [ %lu bytes written, %lu bytes read ]\n",
1712 chan->backend.name, cpu, write_offset, cons_offset);
1713
1714 for (cons_offset = uatomic_read(&buf->consumed);
1715 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1716 chan)
1717 - cons_offset) > 0;
1718 cons_offset = subbuf_align(cons_offset, chan))
1719 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1720 cpu, handle);
1721 }
1722
1723 static
1724 void lib_ring_buffer_print_errors(struct channel *chan,
1725 struct lttng_ust_lib_ring_buffer *buf, int cpu,
1726 struct lttng_ust_shm_handle *handle)
1727 {
1728 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1729 void *priv = channel_get_private(chan);
1730
1731 if (!strcmp(chan->backend.name, "relay-metadata-mmap")) {
1732 DBG("ring buffer %s: %lu records written, "
1733 "%lu records overrun\n",
1734 chan->backend.name,
1735 v_read(config, &buf->records_count),
1736 v_read(config, &buf->records_overrun));
1737 } else {
1738 DBG("ring buffer %s, cpu %d: %lu records written, "
1739 "%lu records overrun\n",
1740 chan->backend.name, cpu,
1741 v_read(config, &buf->records_count),
1742 v_read(config, &buf->records_overrun));
1743
1744 if (v_read(config, &buf->records_lost_full)
1745 || v_read(config, &buf->records_lost_wrap)
1746 || v_read(config, &buf->records_lost_big))
1747 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1748 " [ %lu buffer full, %lu nest buffer wrap-around, "
1749 "%lu event too big ]\n",
1750 chan->backend.name, cpu,
1751 v_read(config, &buf->records_lost_full),
1752 v_read(config, &buf->records_lost_wrap),
1753 v_read(config, &buf->records_lost_big));
1754 }
1755 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle);
1756 }
1757
1758 /*
1759 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1760 *
1761 * Only executed by SWITCH_FLUSH, which can be issued while tracing is
1762 * active or at buffer finalization (destroy).
1763 */
1764 static
1765 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf,
1766 struct channel *chan,
1767 struct switch_offsets *offsets,
1768 uint64_t tsc,
1769 struct lttng_ust_shm_handle *handle)
1770 {
1771 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1772 unsigned long oldidx = subbuf_index(offsets->old, chan);
1773 unsigned long commit_count;
1774 struct commit_counters_hot *cc_hot;
1775
1776 config->cb.buffer_begin(buf, tsc, oldidx, handle);
1777
1778 /*
1779 * Order all writes to buffer before the commit count update that will
1780 * determine that the subbuffer is full.
1781 */
1782 cmm_smp_wmb();
1783 cc_hot = shmp_index(handle, buf->commit_hot, oldidx);
1784 if (!cc_hot)
1785 return;
1786 v_add(config, config->cb.subbuffer_header_size(),
1787 &cc_hot->cc);
1788 commit_count = v_read(config, &cc_hot->cc);
1789 /* Check if the written buffer has to be delivered */
1790 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1791 commit_count, oldidx, handle, tsc);
1792 lib_ring_buffer_write_commit_counter(config, buf, chan,
1793 offsets->old + config->cb.subbuffer_header_size(),
1794 commit_count, handle, cc_hot);
1795 }
1796
1797 /*
1798 * lib_ring_buffer_switch_old_end: switch old subbuffer
1799 *
1800 * Note : offset_old should never be 0 here. It is ok, because we never perform
1801 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1802 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1803 * subbuffer.
1804 */
1805 static
1806 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf,
1807 struct channel *chan,
1808 struct switch_offsets *offsets,
1809 uint64_t tsc,
1810 struct lttng_ust_shm_handle *handle)
1811 {
1812 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1813 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1814 unsigned long commit_count, padding_size, data_size;
1815 struct commit_counters_hot *cc_hot;
1816 uint64_t *ts_end;
1817
1818 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1819 padding_size = chan->backend.subbuf_size - data_size;
1820 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size,
1821 handle);
1822
1823 ts_end = shmp_index(handle, buf->ts_end, oldidx);
1824 if (!ts_end)
1825 return;
1826 /*
1827 * This is the last space reservation in that sub-buffer before
1828 * it gets delivered. This provides exclusive access to write to
1829 * this sub-buffer's ts_end. There are also no concurrent
1830 * readers of that ts_end because delivery of that sub-buffer is
1831 * postponed until the commit counter is incremented for the
1832 * current space reservation.
1833 */
1834 *ts_end = tsc;
1835
1836 /*
1837 * Order all writes to buffer and store to ts_end before the commit
1838 * count update that will determine that the subbuffer is full.
1839 */
1840 cmm_smp_wmb();
1841 cc_hot = shmp_index(handle, buf->commit_hot, oldidx);
1842 if (!cc_hot)
1843 return;
1844 v_add(config, padding_size, &cc_hot->cc);
1845 commit_count = v_read(config, &cc_hot->cc);
1846 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1847 commit_count, oldidx, handle, tsc);
1848 lib_ring_buffer_write_commit_counter(config, buf, chan,
1849 offsets->old + padding_size, commit_count, handle,
1850 cc_hot);
1851 }
1852
1853 /*
1854 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1855 *
1856 * This code can be executed unordered : writers may already have written to the
1857 * sub-buffer before this code gets executed, caution. The commit makes sure
1858 * that this code is executed before the deliver of this sub-buffer.
1859 */
1860 static
1861 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf,
1862 struct channel *chan,
1863 struct switch_offsets *offsets,
1864 uint64_t tsc,
1865 struct lttng_ust_shm_handle *handle)
1866 {
1867 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1868 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1869 unsigned long commit_count;
1870 struct commit_counters_hot *cc_hot;
1871
1872 config->cb.buffer_begin(buf, tsc, beginidx, handle);
1873
1874 /*
1875 * Order all writes to buffer before the commit count update that will
1876 * determine that the subbuffer is full.
1877 */
1878 cmm_smp_wmb();
1879 cc_hot = shmp_index(handle, buf->commit_hot, beginidx);
1880 if (!cc_hot)
1881 return;
1882 v_add(config, config->cb.subbuffer_header_size(), &cc_hot->cc);
1883 commit_count = v_read(config, &cc_hot->cc);
1884 /* Check if the written buffer has to be delivered */
1885 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1886 commit_count, beginidx, handle, tsc);
1887 lib_ring_buffer_write_commit_counter(config, buf, chan,
1888 offsets->begin + config->cb.subbuffer_header_size(),
1889 commit_count, handle, cc_hot);
1890 }
1891
1892 /*
1893 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1894 *
1895 * Calls subbuffer_set_data_size() to set the data size of the current
1896 * sub-buffer. We do not need to perform check_deliver nor commit here,
1897 * since this task will be done by the "commit" of the event for which
1898 * we are currently doing the space reservation.
1899 */
1900 static
1901 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf,
1902 struct channel *chan,
1903 struct switch_offsets *offsets,
1904 uint64_t tsc,
1905 struct lttng_ust_shm_handle *handle)
1906 {
1907 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1908 unsigned long endidx, data_size;
1909 uint64_t *ts_end;
1910
1911 endidx = subbuf_index(offsets->end - 1, chan);
1912 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1913 subbuffer_set_data_size(config, &buf->backend, endidx, data_size,
1914 handle);
1915 ts_end = shmp_index(handle, buf->ts_end, endidx);
1916 if (!ts_end)
1917 return;
1918 /*
1919 * This is the last space reservation in that sub-buffer before
1920 * it gets delivered. This provides exclusive access to write to
1921 * this sub-buffer's ts_end. There are also no concurrent
1922 * readers of that ts_end because delivery of that sub-buffer is
1923 * postponed until the commit counter is incremented for the
1924 * current space reservation.
1925 */
1926 *ts_end = tsc;
1927 }
1928
1929 /*
1930 * Returns :
1931 * 0 if ok
1932 * !0 if execution must be aborted.
1933 */
1934 static
1935 int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
1936 struct lttng_ust_lib_ring_buffer *buf,
1937 struct channel *chan,
1938 struct switch_offsets *offsets,
1939 uint64_t *tsc,
1940 struct lttng_ust_shm_handle *handle)
1941 {
1942 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1943 unsigned long off, reserve_commit_diff;
1944
1945 offsets->begin = v_read(config, &buf->offset);
1946 offsets->old = offsets->begin;
1947 offsets->switch_old_start = 0;
1948 off = subbuf_offset(offsets->begin, chan);
1949
1950 *tsc = config->cb.ring_buffer_clock_read(chan);
1951
1952 /*
1953 * Ensure we flush the header of an empty subbuffer when doing the
1954 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1955 * total data gathering duration even if there were no records saved
1956 * after the last buffer switch.
1957 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1958 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1959 * subbuffer header as appropriate.
1960 * The next record that reserves space will be responsible for
1961 * populating the following subbuffer header. We choose not to populate
1962 * the next subbuffer header here because we want to be able to use
1963 * SWITCH_ACTIVE for periodical buffer flush, which must
1964 * guarantee that all the buffer content (records and header
1965 * timestamps) are visible to the reader. This is required for
1966 * quiescence guarantees for the fusion merge.
1967 */
1968 if (mode != SWITCH_FLUSH && !off)
1969 return -1; /* we do not have to switch : buffer is empty */
1970
1971 if (caa_unlikely(off == 0)) {
1972 unsigned long sb_index, commit_count;
1973 struct commit_counters_cold *cc_cold;
1974
1975 /*
1976 * We are performing a SWITCH_FLUSH. There may be concurrent
1977 * writes into the buffer if e.g. invoked while performing a
1978 * snapshot on an active trace.
1979 *
1980 * If the client does not save any header information
1981 * (sub-buffer header size == 0), don't switch empty subbuffer
1982 * on finalize, because it is invalid to deliver a completely
1983 * empty subbuffer.
1984 */
1985 if (!config->cb.subbuffer_header_size())
1986 return -1;
1987
1988 /* Test new buffer integrity */
1989 sb_index = subbuf_index(offsets->begin, chan);
1990 cc_cold = shmp_index(handle, buf->commit_cold, sb_index);
1991 if (!cc_cold)
1992 return -1;
1993 commit_count = v_read(config, &cc_cold->cc_sb);
1994 reserve_commit_diff =
1995 (buf_trunc(offsets->begin, chan)
1996 >> chan->backend.num_subbuf_order)
1997 - (commit_count & chan->commit_count_mask);
1998 if (caa_likely(reserve_commit_diff == 0)) {
1999 /* Next subbuffer not being written to. */
2000 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
2001 subbuf_trunc(offsets->begin, chan)
2002 - subbuf_trunc((unsigned long)
2003 uatomic_read(&buf->consumed), chan)
2004 >= chan->backend.buf_size)) {
2005 /*
2006 * We do not overwrite non consumed buffers
2007 * and we are full : don't switch.
2008 */
2009 return -1;
2010 } else {
2011 /*
2012 * Next subbuffer not being written to, and we
2013 * are either in overwrite mode or the buffer is
2014 * not full. It's safe to write in this new
2015 * subbuffer.
2016 */
2017 }
2018 } else {
2019 /*
2020 * Next subbuffer reserve offset does not match the
2021 * commit offset. Don't perform switch in
2022 * producer-consumer and overwrite mode. Caused by
2023 * either a writer OOPS or too many nested writes over a
2024 * reserve/commit pair.
2025 */
2026 return -1;
2027 }
2028
2029 /*
2030 * Need to write the subbuffer start header on finalize.
2031 */
2032 offsets->switch_old_start = 1;
2033 }
2034 offsets->begin = subbuf_align(offsets->begin, chan);
2035 /* Note: old points to the next subbuf at offset 0 */
2036 offsets->end = offsets->begin;
2037 return 0;
2038 }
2039
2040 /*
2041 * Force a sub-buffer switch. This operation is completely reentrant : can be
2042 * called while tracing is active with absolutely no lock held.
2043 *
2044 * For RING_BUFFER_SYNC_PER_CPU ring buffers, as a v_cmpxchg is used for
2045 * some atomic operations, this function must be called from the CPU
2046 * which owns the buffer for a ACTIVE flush. However, for
2047 * RING_BUFFER_SYNC_GLOBAL ring buffers, this function can be called
2048 * from any CPU.
2049 */
2050 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode,
2051 struct lttng_ust_shm_handle *handle)
2052 {
2053 struct channel *chan;
2054 const struct lttng_ust_lib_ring_buffer_config *config;
2055 struct switch_offsets offsets;
2056 unsigned long oldidx;
2057 uint64_t tsc;
2058
2059 chan = shmp(handle, buf->backend.chan);
2060 if (!chan)
2061 return;
2062 config = &chan->backend.config;
2063
2064 offsets.size = 0;
2065
2066 /*
2067 * Perform retryable operations.
2068 */
2069 do {
2070 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
2071 &tsc, handle))
2072 return; /* Switch not needed */
2073 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
2074 != offsets.old);
2075
2076 /*
2077 * Atomically update last_tsc. This update races against concurrent
2078 * atomic updates, but the race will always cause supplementary full TSC
2079 * records, never the opposite (missing a full TSC record when it would
2080 * be needed).
2081 */
2082 save_last_tsc(config, buf, tsc);
2083
2084 /*
2085 * Push the reader if necessary
2086 */
2087 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
2088
2089 oldidx = subbuf_index(offsets.old, chan);
2090 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle);
2091
2092 /*
2093 * May need to populate header start on SWITCH_FLUSH.
2094 */
2095 if (offsets.switch_old_start) {
2096 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle);
2097 offsets.old += config->cb.subbuffer_header_size();
2098 }
2099
2100 /*
2101 * Switch old subbuffer.
2102 */
2103 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle);
2104 }
2105
2106 static
2107 bool handle_blocking_retry(int *timeout_left_ms)
2108 {
2109 int timeout = *timeout_left_ms, delay;
2110
2111 if (caa_likely(!timeout))
2112 return false; /* Do not retry, discard event. */
2113 if (timeout < 0) /* Wait forever. */
2114 delay = RETRY_DELAY_MS;
2115 else
2116 delay = min_t(int, timeout, RETRY_DELAY_MS);
2117 (void) poll(NULL, 0, delay);
2118 if (timeout > 0)
2119 *timeout_left_ms -= delay;
2120 return true; /* Retry. */
2121 }
2122
2123 /*
2124 * Returns :
2125 * 0 if ok
2126 * -ENOSPC if event size is too large for packet.
2127 * -ENOBUFS if there is currently not enough space in buffer for the event.
2128 * -EIO if data cannot be written into the buffer for any other reason.
2129 */
2130 static
2131 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf,
2132 struct channel *chan,
2133 struct switch_offsets *offsets,
2134 struct lttng_ust_lib_ring_buffer_ctx *ctx,
2135 void *client_ctx)
2136 {
2137 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
2138 struct lttng_ust_shm_handle *handle = ctx->handle;
2139 unsigned long reserve_commit_diff, offset_cmp;
2140 int timeout_left_ms = lttng_ust_ringbuffer_get_timeout(chan);
2141
2142 retry:
2143 offsets->begin = offset_cmp = v_read(config, &buf->offset);
2144 offsets->old = offsets->begin;
2145 offsets->switch_new_start = 0;
2146 offsets->switch_new_end = 0;
2147 offsets->switch_old_end = 0;
2148 offsets->pre_header_padding = 0;
2149
2150 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
2151 if ((int64_t) ctx->tsc == -EIO)
2152 return -EIO;
2153
2154 if (last_tsc_overflow(config, buf, ctx->tsc))
2155 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
2156
2157 if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
2158 offsets->switch_new_start = 1; /* For offsets->begin */
2159 } else {
2160 offsets->size = config->cb.record_header_size(config, chan,
2161 offsets->begin,
2162 &offsets->pre_header_padding,
2163 ctx, client_ctx);
2164 offsets->size +=
2165 lib_ring_buffer_align(offsets->begin + offsets->size,
2166 ctx->largest_align)
2167 + ctx->data_size;
2168 if (caa_unlikely(subbuf_offset(offsets->begin, chan) +
2169 offsets->size > chan->backend.subbuf_size)) {
2170 offsets->switch_old_end = 1; /* For offsets->old */
2171 offsets->switch_new_start = 1; /* For offsets->begin */
2172 }
2173 }
2174 if (caa_unlikely(offsets->switch_new_start)) {
2175 unsigned long sb_index, commit_count;
2176 struct commit_counters_cold *cc_cold;
2177
2178 /*
2179 * We are typically not filling the previous buffer completely.
2180 */
2181 if (caa_likely(offsets->switch_old_end))
2182 offsets->begin = subbuf_align(offsets->begin, chan);
2183 offsets->begin = offsets->begin
2184 + config->cb.subbuffer_header_size();
2185 /* Test new buffer integrity */
2186 sb_index = subbuf_index(offsets->begin, chan);
2187 /*
2188 * Read buf->offset before buf->commit_cold[sb_index].cc_sb.
2189 * lib_ring_buffer_check_deliver() has the matching
2190 * memory barriers required around commit_cold cc_sb
2191 * updates to ensure reserve and commit counter updates
2192 * are not seen reordered when updated by another CPU.
2193 */
2194 cmm_smp_rmb();
2195 cc_cold = shmp_index(handle, buf->commit_cold, sb_index);
2196 if (!cc_cold)
2197 return -1;
2198 commit_count = v_read(config, &cc_cold->cc_sb);
2199 /* Read buf->commit_cold[sb_index].cc_sb before buf->offset. */
2200 cmm_smp_rmb();
2201 if (caa_unlikely(offset_cmp != v_read(config, &buf->offset))) {
2202 /*
2203 * The reserve counter have been concurrently updated
2204 * while we read the commit counter. This means the
2205 * commit counter we read might not match buf->offset
2206 * due to concurrent update. We therefore need to retry.
2207 */
2208 goto retry;
2209 }
2210 reserve_commit_diff =
2211 (buf_trunc(offsets->begin, chan)
2212 >> chan->backend.num_subbuf_order)
2213 - (commit_count & chan->commit_count_mask);
2214 if (caa_likely(reserve_commit_diff == 0)) {
2215 /* Next subbuffer not being written to. */
2216 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
2217 subbuf_trunc(offsets->begin, chan)
2218 - subbuf_trunc((unsigned long)
2219 uatomic_read(&buf->consumed), chan)
2220 >= chan->backend.buf_size)) {
2221 unsigned long nr_lost;
2222
2223 if (handle_blocking_retry(&timeout_left_ms))
2224 goto retry;
2225
2226 /*
2227 * We do not overwrite non consumed buffers
2228 * and we are full : record is lost.
2229 */
2230 nr_lost = v_read(config, &buf->records_lost_full);
2231 v_inc(config, &buf->records_lost_full);
2232 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2233 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
2234 nr_lost + 1, chan->backend.name,
2235 buf->backend.cpu);
2236 }
2237 return -ENOBUFS;
2238 } else {
2239 /*
2240 * Next subbuffer not being written to, and we
2241 * are either in overwrite mode or the buffer is
2242 * not full. It's safe to write in this new
2243 * subbuffer.
2244 */
2245 }
2246 } else {
2247 unsigned long nr_lost;
2248
2249 /*
2250 * Next subbuffer reserve offset does not match the
2251 * commit offset, and this did not involve update to the
2252 * reserve counter. Drop record in producer-consumer and
2253 * overwrite mode. Caused by either a writer OOPS or too
2254 * many nested writes over a reserve/commit pair.
2255 */
2256 nr_lost = v_read(config, &buf->records_lost_wrap);
2257 v_inc(config, &buf->records_lost_wrap);
2258 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2259 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
2260 nr_lost + 1, chan->backend.name,
2261 buf->backend.cpu);
2262 }
2263 return -EIO;
2264 }
2265 offsets->size =
2266 config->cb.record_header_size(config, chan,
2267 offsets->begin,
2268 &offsets->pre_header_padding,
2269 ctx, client_ctx);
2270 offsets->size +=
2271 lib_ring_buffer_align(offsets->begin + offsets->size,
2272 ctx->largest_align)
2273 + ctx->data_size;
2274 if (caa_unlikely(subbuf_offset(offsets->begin, chan)
2275 + offsets->size > chan->backend.subbuf_size)) {
2276 unsigned long nr_lost;
2277
2278 /*
2279 * Record too big for subbuffers, report error, don't
2280 * complete the sub-buffer switch.
2281 */
2282 nr_lost = v_read(config, &buf->records_lost_big);
2283 v_inc(config, &buf->records_lost_big);
2284 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
2285 DBG("%lu or more records lost in (%s:%d) record size "
2286 " of %zu bytes is too large for buffer\n",
2287 nr_lost + 1, chan->backend.name,
2288 buf->backend.cpu, offsets->size);
2289 }
2290 return -ENOSPC;
2291 } else {
2292 /*
2293 * We just made a successful buffer switch and the
2294 * record fits in the new subbuffer. Let's write.
2295 */
2296 }
2297 } else {
2298 /*
2299 * Record fits in the current buffer and we are not on a switch
2300 * boundary. It's safe to write.
2301 */
2302 }
2303 offsets->end = offsets->begin + offsets->size;
2304
2305 if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) {
2306 /*
2307 * The offset_end will fall at the very beginning of the next
2308 * subbuffer.
2309 */
2310 offsets->switch_new_end = 1; /* For offsets->begin */
2311 }
2312 return 0;
2313 }
2314
2315 /**
2316 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
2317 * @ctx: ring buffer context.
2318 *
2319 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
2320 * -EIO for other errors, else returns 0.
2321 * It will take care of sub-buffer switching.
2322 */
2323 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx,
2324 void *client_ctx)
2325 {
2326 struct channel *chan = ctx->chan;
2327 struct lttng_ust_shm_handle *handle = ctx->handle;
2328 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
2329 struct lttng_ust_lib_ring_buffer *buf;
2330 struct switch_offsets offsets;
2331 int ret;
2332
2333 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
2334 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
2335 else
2336 buf = shmp(handle, chan->backend.buf[0].shmp);
2337 if (!buf)
2338 return -EIO;
2339 ctx->buf = buf;
2340
2341 offsets.size = 0;
2342
2343 do {
2344 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
2345 ctx, client_ctx);
2346 if (caa_unlikely(ret))
2347 return ret;
2348 } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
2349 offsets.end)
2350 != offsets.old));
2351
2352 /*
2353 * Atomically update last_tsc. This update races against concurrent
2354 * atomic updates, but the race will always cause supplementary full TSC
2355 * records, never the opposite (missing a full TSC record when it would
2356 * be needed).
2357 */
2358 save_last_tsc(config, buf, ctx->tsc);
2359
2360 /*
2361 * Push the reader if necessary
2362 */
2363 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
2364
2365 /*
2366 * Clear noref flag for this subbuffer.
2367 */
2368 lib_ring_buffer_clear_noref(config, &buf->backend,
2369 subbuf_index(offsets.end - 1, chan),
2370 handle);
2371
2372 /*
2373 * Switch old subbuffer if needed.
2374 */
2375 if (caa_unlikely(offsets.switch_old_end)) {
2376 lib_ring_buffer_clear_noref(config, &buf->backend,
2377 subbuf_index(offsets.old - 1, chan),
2378 handle);
2379 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle);
2380 }
2381
2382 /*
2383 * Populate new subbuffer.
2384 */
2385 if (caa_unlikely(offsets.switch_new_start))
2386 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle);
2387
2388 if (caa_unlikely(offsets.switch_new_end))
2389 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle);
2390
2391 ctx->slot_size = offsets.size;
2392 ctx->pre_offset = offsets.begin;
2393 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
2394 return 0;
2395 }
2396
2397 static
2398 void lib_ring_buffer_vmcore_check_deliver(const struct lttng_ust_lib_ring_buffer_config *config,
2399 struct lttng_ust_lib_ring_buffer *buf,
2400 unsigned long commit_count,
2401 unsigned long idx,
2402 struct lttng_ust_shm_handle *handle)
2403 {
2404 struct commit_counters_hot *cc_hot;
2405
2406 if (config->oops != RING_BUFFER_OOPS_CONSISTENCY)
2407 return;
2408 cc_hot = shmp_index(handle, buf->commit_hot, idx);
2409 if (!cc_hot)
2410 return;
2411 v_set(config, &cc_hot->seq, commit_count);
2412 }
2413
2414 /*
2415 * The ring buffer can count events recorded and overwritten per buffer,
2416 * but it is disabled by default due to its performance overhead.
2417 */
2418 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
2419 static
2420 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config,
2421 struct lttng_ust_lib_ring_buffer *buf,
2422 unsigned long idx,
2423 struct lttng_ust_shm_handle *handle)
2424 {
2425 v_add(config, subbuffer_get_records_count(config,
2426 &buf->backend, idx, handle),
2427 &buf->records_count);
2428 v_add(config, subbuffer_count_records_overrun(config,
2429 &buf->backend, idx, handle),
2430 &buf->records_overrun);
2431 }
2432 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
2433 static
2434 void deliver_count_events(const struct lttng_ust_lib_ring_buffer_config *config,
2435 struct lttng_ust_lib_ring_buffer *buf,
2436 unsigned long idx,
2437 struct lttng_ust_shm_handle *handle)
2438 {
2439 }
2440 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
2441
2442 void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config *config,
2443 struct lttng_ust_lib_ring_buffer *buf,
2444 struct channel *chan,
2445 unsigned long offset,
2446 unsigned long commit_count,
2447 unsigned long idx,
2448 struct lttng_ust_shm_handle *handle,
2449 uint64_t tsc)
2450 {
2451 unsigned long old_commit_count = commit_count
2452 - chan->backend.subbuf_size;
2453 struct commit_counters_cold *cc_cold;
2454
2455 /*
2456 * If we succeeded at updating cc_sb below, we are the subbuffer
2457 * writer delivering the subbuffer. Deals with concurrent
2458 * updates of the "cc" value without adding a add_return atomic
2459 * operation to the fast path.
2460 *
2461 * We are doing the delivery in two steps:
2462 * - First, we cmpxchg() cc_sb to the new value
2463 * old_commit_count + 1. This ensures that we are the only
2464 * subbuffer user successfully filling the subbuffer, but we
2465 * do _not_ set the cc_sb value to "commit_count" yet.
2466 * Therefore, other writers that would wrap around the ring
2467 * buffer and try to start writing to our subbuffer would
2468 * have to drop records, because it would appear as
2469 * non-filled.
2470 * We therefore have exclusive access to the subbuffer control
2471 * structures. This mutual exclusion with other writers is
2472 * crucially important to perform record overruns count in
2473 * flight recorder mode locklessly.
2474 * - When we are ready to release the subbuffer (either for
2475 * reading or for overrun by other writers), we simply set the
2476 * cc_sb value to "commit_count" and perform delivery.
2477 *
2478 * The subbuffer size is least 2 bytes (minimum size: 1 page).
2479 * This guarantees that old_commit_count + 1 != commit_count.
2480 */
2481
2482 /*
2483 * Order prior updates to reserve count prior to the
2484 * commit_cold cc_sb update.
2485 */
2486 cmm_smp_wmb();
2487 cc_cold = shmp_index(handle, buf->commit_cold, idx);
2488 if (!cc_cold)
2489 return;
2490 if (caa_likely(v_cmpxchg(config, &cc_cold->cc_sb,
2491 old_commit_count, old_commit_count + 1)
2492 == old_commit_count)) {
2493 uint64_t *ts_end;
2494
2495 /*
2496 * Start of exclusive subbuffer access. We are
2497 * guaranteed to be the last writer in this subbuffer
2498 * and any other writer trying to access this subbuffer
2499 * in this state is required to drop records.
2500 *
2501 * We can read the ts_end for the current sub-buffer
2502 * which has been saved by the very last space
2503 * reservation for the current sub-buffer.
2504 *
2505 * Order increment of commit counter before reading ts_end.
2506 */
2507 cmm_smp_mb();
2508 ts_end = shmp_index(handle, buf->ts_end, idx);
2509 if (!ts_end)
2510 return;
2511 deliver_count_events(config, buf, idx, handle);
2512 config->cb.buffer_end(buf, *ts_end, idx,
2513 lib_ring_buffer_get_data_size(config,
2514 buf,
2515 idx,
2516 handle),
2517 handle);
2518
2519 /*
2520 * Increment the packet counter while we have exclusive
2521 * access.
2522 */
2523 subbuffer_inc_packet_count(config, &buf->backend, idx, handle);
2524
2525 /*
2526 * Set noref flag and offset for this subbuffer id.
2527 * Contains a memory barrier that ensures counter stores
2528 * are ordered before set noref and offset.
2529 */
2530 lib_ring_buffer_set_noref_offset(config, &buf->backend, idx,
2531 buf_trunc_val(offset, chan), handle);
2532
2533 /*
2534 * Order set_noref and record counter updates before the
2535 * end of subbuffer exclusive access. Orders with
2536 * respect to writers coming into the subbuffer after
2537 * wrap around, and also order wrt concurrent readers.
2538 */
2539 cmm_smp_mb();
2540 /* End of exclusive subbuffer access */
2541 v_set(config, &cc_cold->cc_sb, commit_count);
2542 /*
2543 * Order later updates to reserve count after
2544 * the commit cold cc_sb update.
2545 */
2546 cmm_smp_wmb();
2547 lib_ring_buffer_vmcore_check_deliver(config, buf,
2548 commit_count, idx, handle);
2549
2550 /*
2551 * RING_BUFFER_WAKEUP_BY_WRITER wakeup is not lock-free.
2552 */
2553 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER
2554 && uatomic_read(&buf->active_readers)
2555 && lib_ring_buffer_poll_deliver(config, buf, chan, handle)) {
2556 lib_ring_buffer_wakeup(buf, handle);
2557 }
2558 }
2559 }
2560
2561 /*
2562 * Force a read (imply TLS fixup for dlopen) of TLS variables.
2563 */
2564 void lttng_fixup_ringbuffer_tls(void)
2565 {
2566 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting)));
2567 }
2568
2569 void lib_ringbuffer_signal_init(void)
2570 {
2571 sigset_t mask;
2572 int ret;
2573
2574 /*
2575 * Block signal for entire process, so only our thread processes
2576 * it.
2577 */
2578 rb_setmask(&mask);
2579 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
2580 if (ret) {
2581 errno = ret;
2582 PERROR("pthread_sigmask");
2583 }
2584 }
This page took 0.152429 seconds and 4 git commands to generate.