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