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