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