Fix: fields should be initialized to NULL
[lttng-ust.git] / libringbuffer / ring_buffer_frontend.c
CommitLineData
852c2936
MD
1/*
2 * ring_buffer_frontend.c
3 *
e92f3e28
MD
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 *
852c2936
MD
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
852c2936
MD
52 */
53
5ad63a16 54#define _GNU_SOURCE
a6352fd4 55#include <sys/types.h>
431d5cf0
MD
56#include <sys/mman.h>
57#include <sys/stat.h>
03d2d293 58#include <unistd.h>
431d5cf0 59#include <fcntl.h>
03d2d293
MD
60#include <signal.h>
61#include <time.h>
14641deb 62#include <urcu/compiler.h>
a6352fd4 63#include <urcu/ref.h>
8c90a710 64#include <urcu/tls-compat.h>
35897f8b 65#include <helper.h>
14641deb 66
a6352fd4 67#include "smp.h"
4318ae1b 68#include <lttng/ringbuffer-config.h>
2fed87ae 69#include "vatomic.h"
4931a13e
MD
70#include "backend.h"
71#include "frontend.h"
a6352fd4 72#include "shm.h"
f645cfa7 73#include "tlsfixup.h"
bdcf8d82 74#include "../liblttng-ust/compat.h" /* For ENODATA */
852c2936 75
431d5cf0
MD
76#ifndef max
77#define max(a, b) ((a) > (b) ? (a) : (b))
78#endif
79
64493e4f
MD
80/* Print DBG() messages about events lost only every 1048576 hits */
81#define DBG_PRINT_NR_LOST (1UL << 20)
82
03d2d293
MD
83#define LTTNG_UST_RB_SIG SIGRTMIN
84#define LTTNG_UST_RB_SIG_TEARDOWN SIGRTMIN + 1
85#define CLOCKID CLOCK_MONOTONIC
86
2432c3c9
MD
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
852c2936
MD
103/*
104 * Internal structure representing offsets to use at a sub-buffer switch.
105 */
106struct 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
8c90a710 113DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting);
852c2936
MD
114
115static
116void lib_ring_buffer_print_errors(struct channel *chan,
009769ca
MD
117 struct lttng_ust_lib_ring_buffer *buf, int cpu,
118 struct lttng_ust_shm_handle *handle);
852c2936 119
03d2d293
MD
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 */
126struct timer_signal_data {
127 pthread_t tid; /* thread id managing signals */
128 int setup_done;
129 int qs_done;
130};
131
132static struct timer_signal_data timer_signal;
133
852c2936
MD
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 */
4cfec15c 143void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 144 struct lttng_ust_shm_handle *handle)
852c2936 145{
1d498196 146 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 147 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
148 unsigned int i;
149
150 /*
151 * Reset iterator first. It will put the subbuffer if it currently holds
152 * it.
153 */
852c2936
MD
154 v_set(config, &buf->offset, 0);
155 for (i = 0; i < chan->backend.num_subbuf; i++) {
4746ae29
MD
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);
852c2936 159 }
a6352fd4
MD
160 uatomic_set(&buf->consumed, 0);
161 uatomic_set(&buf->record_disabled, 0);
852c2936 162 v_set(config, &buf->last_tsc, 0);
1d498196 163 lib_ring_buffer_backend_reset(&buf->backend, handle);
852c2936
MD
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}
852c2936
MD
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 */
182void channel_reset(struct channel *chan)
183{
184 /*
185 * Reset iterators first. Will put the subbuffer if held for reading.
186 */
a6352fd4 187 uatomic_set(&chan->record_disabled, 0);
852c2936
MD
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}
852c2936
MD
194
195/*
196 * Must be called under cpu hotplug protection.
197 */
4cfec15c 198int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf,
a6352fd4 199 struct channel_backend *chanb, int cpu,
38fae1d3 200 struct lttng_ust_shm_handle *handle,
1d498196 201 struct shm_object *shmobj)
852c2936 202{
4cfec15c 203 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
14641deb 204 struct channel *chan = caa_container_of(chanb, struct channel, backend);
a3f61e7f 205 void *priv = channel_get_private(chan);
852c2936 206 size_t subbuf_header_size;
2fed87ae 207 uint64_t tsc;
852c2936
MD
208 int ret;
209
210 /* Test for cpu hotplug */
211 if (buf->backend.allocated)
212 return 0;
213
a6352fd4 214 ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend,
1d498196 215 cpu, handle, shmobj);
852c2936
MD
216 if (ret)
217 return ret;
218
1d498196
MD
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)) {
852c2936
MD
224 ret = -ENOMEM;
225 goto free_chanbuf;
226 }
227
1d498196
MD
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)) {
852c2936
MD
233 ret = -ENOMEM;
234 goto free_commit;
235 }
236
852c2936
MD
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);
4746ae29 243 subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id);
1d498196
MD
244 tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan));
245 config->cb.buffer_begin(buf, tsc, 0, handle);
4746ae29 246 v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc);
852c2936
MD
247
248 if (config->cb.buffer_create) {
1d498196 249 ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle);
852c2936
MD
250 if (ret)
251 goto free_init;
252 }
852c2936 253 buf->backend.allocated = 1;
852c2936
MD
254 return 0;
255
256 /* Error handling */
257free_init:
a6352fd4 258 /* commit_cold will be freed by shm teardown */
852c2936 259free_commit:
a6352fd4 260 /* commit_hot will be freed by shm teardown */
852c2936 261free_chanbuf:
852c2936
MD
262 return ret;
263}
264
1d498196 265#if 0
852c2936
MD
266static void switch_buffer_timer(unsigned long data)
267{
4cfec15c 268 struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data;
1d498196 269 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 270 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
271
272 /*
273 * Only flush buffers periodically if readers are active.
274 */
74d81a6c 275 if (uatomic_read(&buf->active_readers))
1d498196 276 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle);
852c2936 277
a6352fd4
MD
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);
852c2936 285}
1d498196 286#endif //0
852c2936 287
03d2d293
MD
288static
289void lib_ring_buffer_channel_switch_timer(int sig, siginfo_t *si, void *uc)
852c2936 290{
03d2d293
MD
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;
852c2936 301
03d2d293
MD
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
322static
323void 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
341static
342void *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 */
376static
377void lib_ring_buffer_setup_timer_thread(void)
378{
379 pthread_t thread;
380 int ret;
381
382 if (timer_signal.setup_done)
852c2936 383 return;
03d2d293
MD
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;
852c2936
MD
396}
397
03d2d293
MD
398/*
399 * Called with ust_lock() held.
400 */
401static
402void lib_ring_buffer_channel_switch_timer_start(struct channel *chan)
852c2936 403{
03d2d293
MD
404 struct sigevent sev;
405 struct itimerspec its;
406 int ret;
852c2936 407
03d2d293 408 if (!chan->switch_timer_interval || chan->switch_timer_enabled)
852c2936
MD
409 return;
410
03d2d293
MD
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 */
437static
438void 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;
852c2936
MD
490}
491
1d498196 492#if 0
852c2936
MD
493/*
494 * Polling timer to check the channels for data.
495 */
496static void read_buffer_timer(unsigned long data)
497{
4cfec15c 498 struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data;
1d498196 499 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 500 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
501
502 CHAN_WARN_ON(chan, !buf->backend.allocated);
503
74d81a6c 504 if (uatomic_read(&buf->active_readers))
852c2936 505 && lib_ring_buffer_poll_deliver(config, buf, chan)) {
a6352fd4
MD
506 //TODO
507 //wake_up_interruptible(&buf->read_wait);
508 //wake_up_interruptible(&chan->read_wait);
852c2936
MD
509 }
510
a6352fd4
MD
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);
852c2936 518}
1d498196 519#endif //0
852c2936 520
4cfec15c 521static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 522 struct lttng_ust_shm_handle *handle)
852c2936 523{
1d498196 524 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 525 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
526
527 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
528 || !chan->read_timer_interval
529 || buf->read_timer_enabled)
530 return;
531
a6352fd4
MD
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;
852c2936 537
a6352fd4
MD
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);
852c2936
MD
542 buf->read_timer_enabled = 1;
543}
544
4cfec15c 545static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 546 struct lttng_ust_shm_handle *handle)
852c2936 547{
1d498196 548 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 549 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
550
551 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
552 || !chan->read_timer_interval
553 || !buf->read_timer_enabled)
554 return;
555
a6352fd4
MD
556 //TODO
557 //del_timer_sync(&buf->read_timer);
852c2936
MD
558 /*
559 * do one more check to catch data that has been written in the last
560 * timer period.
561 */
1d498196 562 if (lib_ring_buffer_poll_deliver(config, buf, chan, handle)) {
a6352fd4
MD
563 //TODO
564 //wake_up_interruptible(&buf->read_wait);
565 //wake_up_interruptible(&chan->read_wait);
852c2936
MD
566 }
567 buf->read_timer_enabled = 0;
568}
569
1d498196 570static void channel_unregister_notifiers(struct channel *chan,
38fae1d3 571 struct lttng_ust_shm_handle *handle)
852c2936 572{
4cfec15c 573 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
574 int cpu;
575
03d2d293 576 lib_ring_buffer_channel_switch_timer_stop(chan);
852c2936 577 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
852c2936 578 for_each_possible_cpu(cpu) {
4cfec15c 579 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp);
a6352fd4 580
1d498196 581 lib_ring_buffer_stop_read_timer(buf, handle);
852c2936 582 }
852c2936 583 } else {
4cfec15c 584 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp);
852c2936 585
1d498196 586 lib_ring_buffer_stop_read_timer(buf, handle);
852c2936 587 }
8d8a24c8 588 //channel_backend_unregister_notifiers(&chan->backend);
852c2936
MD
589}
590
009769ca
MD
591static 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
612static void channel_free(struct channel *chan,
613 struct lttng_ust_shm_handle *handle)
852c2936 614{
74d81a6c 615 channel_backend_free(&chan->backend, handle);
431d5cf0 616 /* chan is freed by shm teardown */
1d498196
MD
617 shm_object_table_destroy(handle->table);
618 free(handle);
852c2936
MD
619}
620
621/**
622 * channel_create - Create channel.
623 * @config: ring buffer instance configuration
624 * @name: name of the channel
a3f61e7f
MD
625 * @priv_data: ring buffer client private data area pointer (output)
626 * @priv_data_size: length, in bytes, of the private data area.
d028eddb 627 * @priv_data_init: initialization data for private data.
852c2936
MD
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 */
4cfec15c 641struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f
MD
642 const char *name,
643 void **priv_data,
644 size_t priv_data_align,
645 size_t priv_data_size,
d028eddb 646 void *priv_data_init,
a3f61e7f 647 void *buf_addr, size_t subbuf_size,
852c2936 648 size_t num_subbuf, unsigned int switch_timer_interval,
74d81a6c 649 unsigned int read_timer_interval)
852c2936 650{
1d498196 651 int ret, cpu;
a3f61e7f 652 size_t shmsize, chansize;
852c2936 653 struct channel *chan;
38fae1d3 654 struct lttng_ust_shm_handle *handle;
1d498196 655 struct shm_object *shmobj;
74d81a6c
MD
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;
852c2936
MD
662
663 if (lib_ring_buffer_check_config(config, switch_timer_interval,
664 read_timer_interval))
665 return NULL;
666
38fae1d3 667 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
431d5cf0
MD
668 if (!handle)
669 return NULL;
670
1d498196
MD
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;
852c2936 675
1d498196
MD
676 /* Calculate the shm allocation layout */
677 shmsize = sizeof(struct channel);
c1fca457 678 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp));
74d81a6c 679 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams;
a3f61e7f 680 chansize = shmsize;
74d81a6c
MD
681 if (priv_data_align)
682 shmsize += offset_align(shmsize, priv_data_align);
a3f61e7f 683 shmsize += priv_data_size;
a6352fd4 684
74d81a6c
MD
685 /* Allocate normal memory for channel (not shared) */
686 shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM);
b5a14697
MD
687 if (!shmobj)
688 goto error_append;
57773204 689 /* struct channel is at object 0, offset 0 (hardcoded) */
a3f61e7f 690 set_shmp(handle->chan, zalloc_shm(shmobj, chansize));
57773204
MD
691 assert(handle->chan._ref.index == 0);
692 assert(handle->chan._ref.offset == 0);
1d498196 693 chan = shmp(handle, handle->chan);
a6352fd4 694 if (!chan)
1d498196 695 goto error_append;
74d81a6c 696 chan->nr_streams = nr_streams;
a6352fd4 697
a3f61e7f
MD
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);
d028eddb 708 memcpy(*priv_data, priv_data_init, priv_data_size);
a3f61e7f
MD
709 } else {
710 chan->priv_data_offset = -1;
74d81a6c
MD
711 if (priv_data)
712 *priv_data = NULL;
a3f61e7f
MD
713 }
714
715 ret = channel_backend_init(&chan->backend, name, config,
1d498196 716 subbuf_size, num_subbuf, handle);
852c2936 717 if (ret)
1d498196 718 goto error_backend_init;
852c2936 719
03d2d293 720 chan->handle = handle;
852c2936 721 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
03d2d293
MD
722 chan->switch_timer_interval = switch_timer_interval;
723
a6352fd4 724 //TODO
03d2d293 725 //chan->read_timer_interval = read_timer_interval;
a6352fd4
MD
726 //init_waitqueue_head(&chan->read_wait);
727 //init_waitqueue_head(&chan->hp_wait);
852c2936 728
03d2d293 729 lib_ring_buffer_channel_switch_timer_start(chan);
852c2936 730 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
852c2936
MD
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 */
852c2936 736 for_each_possible_cpu(cpu) {
4cfec15c 737 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp);
1d498196 738 lib_ring_buffer_start_read_timer(buf, handle);
852c2936 739 }
852c2936 740 } else {
4cfec15c 741 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp);
852c2936 742
1d498196 743 lib_ring_buffer_start_read_timer(buf, handle);
852c2936 744 }
431d5cf0 745 return handle;
852c2936 746
1d498196
MD
747error_backend_init:
748error_append:
749 shm_object_table_destroy(handle->table);
750error_table_alloc:
431d5cf0 751 free(handle);
852c2936
MD
752 return NULL;
753}
852c2936 754
74d81a6c 755struct lttng_ust_shm_handle *channel_handle_create(void *data,
ff0f5728
MD
756 uint64_t memory_map_size,
757 int wakeup_fd)
193183fb 758{
38fae1d3 759 struct lttng_ust_shm_handle *handle;
193183fb
MD
760 struct shm_object *object;
761
38fae1d3 762 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
193183fb
MD
763 if (!handle)
764 return NULL;
765
766 /* Allocate table for channel + per-cpu buffers */
767 handle->table = shm_object_table_create(1 + num_possible_cpus());
768 if (!handle->table)
769 goto error_table_alloc;
770 /* Add channel object */
74d81a6c 771 object = shm_object_table_append_mem(handle->table, data,
ff0f5728 772 memory_map_size, wakeup_fd);
193183fb
MD
773 if (!object)
774 goto error_table_object;
57773204
MD
775 /* struct channel is at object 0, offset 0 (hardcoded) */
776 handle->chan._ref.index = 0;
777 handle->chan._ref.offset = 0;
193183fb
MD
778 return handle;
779
780error_table_object:
781 shm_object_table_destroy(handle->table);
782error_table_alloc:
783 free(handle);
784 return NULL;
785}
786
38fae1d3 787int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
74d81a6c
MD
788 int shm_fd, int wakeup_fd, uint32_t stream_nr,
789 uint64_t memory_map_size)
193183fb
MD
790{
791 struct shm_object *object;
792
793 /* Add stream object */
74d81a6c
MD
794 object = shm_object_table_append_shm(handle->table,
795 shm_fd, wakeup_fd, stream_nr,
796 memory_map_size);
193183fb 797 if (!object)
74d81a6c 798 return -EINVAL;
193183fb
MD
799 return 0;
800}
801
74d81a6c
MD
802unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle)
803{
804 assert(handle->table);
805 return handle->table->allocated_len - 1;
806}
807
852c2936 808static
74d81a6c 809void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle)
852c2936 810{
74d81a6c 811 channel_free(chan, handle);
852c2936
MD
812}
813
814/**
815 * channel_destroy - Finalize, wait for q.s. and destroy channel.
816 * @chan: channel to destroy
817 *
818 * Holds cpu hotplug.
431d5cf0
MD
819 * Call "destroy" callback, finalize channels, decrement the channel
820 * reference count. Note that when readers have completed data
821 * consumption of finalized channels, get_subbuf() will return -ENODATA.
a3f61e7f 822 * They should release their handle at that point.
852c2936 823 */
a3f61e7f 824void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
74d81a6c 825 int consumer)
852c2936 826{
74d81a6c
MD
827 if (consumer) {
828 /*
829 * Note: the consumer takes care of finalizing and
830 * switching the buffers.
831 */
832 channel_unregister_notifiers(chan, handle);
3d0ef9f6
MD
833 /*
834 * The consumer prints errors.
835 */
836 channel_print_errors(chan, handle);
824f40b8
MD
837 }
838
431d5cf0
MD
839 /*
840 * sessiond/consumer are keeping a reference on the shm file
841 * descriptor directly. No need to refcount.
842 */
74d81a6c 843 channel_release(chan, handle);
a3f61e7f 844 return;
852c2936 845}
852c2936 846
4cfec15c
MD
847struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
848 const struct lttng_ust_lib_ring_buffer_config *config,
1d498196 849 struct channel *chan, int cpu,
38fae1d3 850 struct lttng_ust_shm_handle *handle,
74d81a6c
MD
851 int *shm_fd, int *wait_fd,
852 int *wakeup_fd,
853 uint64_t *memory_map_size)
852c2936 854{
381c0f1e
MD
855 struct shm_ref *ref;
856
857 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
74d81a6c 858 cpu = 0;
381c0f1e 859 } else {
e095d803
MD
860 if (cpu >= num_possible_cpus())
861 return NULL;
381c0f1e 862 }
74d81a6c
MD
863 ref = &chan->backend.buf[cpu].shmp._ref;
864 *shm_fd = shm_get_shm_fd(handle, ref);
865 *wait_fd = shm_get_wait_fd(handle, ref);
866 *wakeup_fd = shm_get_wakeup_fd(handle, ref);
867 if (shm_get_shm_size(handle, ref, memory_map_size))
868 return NULL;
869 return shmp(handle, chan->backend.buf[cpu].shmp);
852c2936 870}
852c2936 871
ff0f5728
MD
872int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
873 struct channel *chan,
874 struct lttng_ust_shm_handle *handle)
875{
876 struct shm_ref *ref;
877
878 ref = &handle->chan._ref;
879 return shm_close_wait_fd(handle, ref);
880}
881
882int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
883 struct channel *chan,
884 struct lttng_ust_shm_handle *handle)
885{
886 struct shm_ref *ref;
887
888 ref = &handle->chan._ref;
889 return shm_close_wakeup_fd(handle, ref);
890}
891
892int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
893 struct channel *chan,
894 struct lttng_ust_shm_handle *handle,
895 int cpu)
852c2936 896{
74d81a6c
MD
897 struct shm_ref *ref;
898
899 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
900 cpu = 0;
901 } else {
902 if (cpu >= num_possible_cpus())
903 return -EINVAL;
824f40b8 904 }
74d81a6c
MD
905 ref = &chan->backend.buf[cpu].shmp._ref;
906 return shm_close_wait_fd(handle, ref);
907}
908
ff0f5728 909int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
910 struct channel *chan,
911 struct lttng_ust_shm_handle *handle,
912 int cpu)
913{
914 struct shm_ref *ref;
915
916 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
917 cpu = 0;
918 } else {
919 if (cpu >= num_possible_cpus())
920 return -EINVAL;
921 }
922 ref = &chan->backend.buf[cpu].shmp._ref;
923 return shm_close_wakeup_fd(handle, ref);
924}
925
926int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
927 struct lttng_ust_shm_handle *handle)
928{
a6352fd4 929 if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0)
852c2936 930 return -EBUSY;
a6352fd4 931 cmm_smp_mb();
852c2936
MD
932 return 0;
933}
852c2936 934
4cfec15c 935void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
74d81a6c 936 struct lttng_ust_shm_handle *handle)
852c2936 937{
1d498196 938 struct channel *chan = shmp(handle, buf->backend.chan);
852c2936 939
a6352fd4
MD
940 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
941 cmm_smp_mb();
942 uatomic_dec(&buf->active_readers);
852c2936
MD
943}
944
945/**
946 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
947 * @buf: ring buffer
948 * @consumed: consumed count indicating the position where to read
949 * @produced: produced count, indicates position when to stop reading
950 *
951 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
952 * data to read at consumed position, or 0 if the get operation succeeds.
852c2936
MD
953 */
954
4cfec15c 955int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
1d498196 956 unsigned long *consumed, unsigned long *produced,
38fae1d3 957 struct lttng_ust_shm_handle *handle)
852c2936 958{
1d498196 959 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 960 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
961 unsigned long consumed_cur, write_offset;
962 int finalized;
963
14641deb 964 finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
965 /*
966 * Read finalized before counters.
967 */
a6352fd4
MD
968 cmm_smp_rmb();
969 consumed_cur = uatomic_read(&buf->consumed);
852c2936
MD
970 /*
971 * No need to issue a memory barrier between consumed count read and
972 * write offset read, because consumed count can only change
973 * concurrently in overwrite mode, and we keep a sequence counter
974 * identifier derived from the write offset to check we are getting
975 * the same sub-buffer we are expecting (the sub-buffers are atomically
976 * "tagged" upon writes, tags are checked upon read).
977 */
978 write_offset = v_read(config, &buf->offset);
979
980 /*
981 * Check that we are not about to read the same subbuffer in
982 * which the writer head is.
983 */
984 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
985 == 0)
986 goto nodata;
987
988 *consumed = consumed_cur;
989 *produced = subbuf_trunc(write_offset, chan);
990
991 return 0;
992
993nodata:
994 /*
995 * The memory barriers __wait_event()/wake_up_interruptible() take care
996 * of "raw_spin_is_locked" memory ordering.
997 */
998 if (finalized)
999 return -ENODATA;
852c2936
MD
1000 else
1001 return -EAGAIN;
1002}
852c2936
MD
1003
1004/**
1005 * lib_ring_buffer_put_snapshot - move consumed counter forward
1006 * @buf: ring buffer
1007 * @consumed_new: new consumed count value
1008 */
4cfec15c 1009void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1d498196 1010 unsigned long consumed_new,
38fae1d3 1011 struct lttng_ust_shm_handle *handle)
852c2936 1012{
4cfec15c 1013 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1d498196 1014 struct channel *chan = shmp(handle, bufb->chan);
852c2936
MD
1015 unsigned long consumed;
1016
74d81a6c 1017 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
852c2936
MD
1018
1019 /*
1020 * Only push the consumed value forward.
1021 * If the consumed cmpxchg fails, this is because we have been pushed by
1022 * the writer in flight recorder mode.
1023 */
a6352fd4 1024 consumed = uatomic_read(&buf->consumed);
852c2936 1025 while ((long) consumed - (long) consumed_new < 0)
a6352fd4
MD
1026 consumed = uatomic_cmpxchg(&buf->consumed, consumed,
1027 consumed_new);
852c2936 1028}
852c2936
MD
1029
1030/**
1031 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
1032 * @buf: ring buffer
1033 * @consumed: consumed count indicating the position where to read
1034 *
1035 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
1036 * data to read at consumed position, or 0 if the get operation succeeds.
852c2936 1037 */
4cfec15c 1038int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1d498196 1039 unsigned long consumed,
38fae1d3 1040 struct lttng_ust_shm_handle *handle)
852c2936 1041{
1d498196 1042 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 1043 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1044 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
1045 int ret;
1046 int finalized;
1047
1048retry:
14641deb 1049 finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
1050 /*
1051 * Read finalized before counters.
1052 */
a6352fd4
MD
1053 cmm_smp_rmb();
1054 consumed_cur = uatomic_read(&buf->consumed);
852c2936 1055 consumed_idx = subbuf_index(consumed, chan);
4746ae29 1056 commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb);
852c2936
MD
1057 /*
1058 * Make sure we read the commit count before reading the buffer
1059 * data and the write offset. Correct consumed offset ordering
1060 * wrt commit count is insured by the use of cmpxchg to update
1061 * the consumed offset.
852c2936 1062 */
a6352fd4
MD
1063 /*
1064 * Local rmb to match the remote wmb to read the commit count
1065 * before the buffer data and the write offset.
1066 */
1067 cmm_smp_rmb();
852c2936
MD
1068
1069 write_offset = v_read(config, &buf->offset);
1070
1071 /*
1072 * Check that the buffer we are getting is after or at consumed_cur
1073 * position.
1074 */
1075 if ((long) subbuf_trunc(consumed, chan)
1076 - (long) subbuf_trunc(consumed_cur, chan) < 0)
1077 goto nodata;
1078
1079 /*
1080 * Check that the subbuffer we are trying to consume has been
1081 * already fully committed.
1082 */
1083 if (((commit_count - chan->backend.subbuf_size)
1084 & chan->commit_count_mask)
1085 - (buf_trunc(consumed_cur, chan)
1086 >> chan->backend.num_subbuf_order)
1087 != 0)
1088 goto nodata;
1089
1090 /*
1091 * Check that we are not about to read the same subbuffer in
1092 * which the writer head is.
1093 */
1094 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
1095 == 0)
1096 goto nodata;
1097
1098 /*
1099 * Failure to get the subbuffer causes a busy-loop retry without going
1100 * to a wait queue. These are caused by short-lived race windows where
1101 * the writer is getting access to a subbuffer we were trying to get
1102 * access to. Also checks that the "consumed" buffer count we are
1103 * looking for matches the one contained in the subbuffer id.
1104 */
1105 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
1d498196
MD
1106 consumed_idx, buf_trunc_val(consumed, chan),
1107 handle);
852c2936
MD
1108 if (ret)
1109 goto retry;
1110 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
1111
1112 buf->get_subbuf_consumed = consumed;
1113 buf->get_subbuf = 1;
1114
1115 return 0;
1116
1117nodata:
1118 /*
1119 * The memory barriers __wait_event()/wake_up_interruptible() take care
1120 * of "raw_spin_is_locked" memory ordering.
1121 */
1122 if (finalized)
1123 return -ENODATA;
852c2936
MD
1124 else
1125 return -EAGAIN;
1126}
852c2936
MD
1127
1128/**
1129 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
1130 * @buf: ring buffer
1131 */
4cfec15c 1132void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 1133 struct lttng_ust_shm_handle *handle)
852c2936 1134{
4cfec15c 1135 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
1d498196 1136 struct channel *chan = shmp(handle, bufb->chan);
4cfec15c 1137 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1138 unsigned long read_sb_bindex, consumed_idx, consumed;
1139
74d81a6c 1140 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
852c2936
MD
1141
1142 if (!buf->get_subbuf) {
1143 /*
1144 * Reader puts a subbuffer it did not get.
1145 */
1146 CHAN_WARN_ON(chan, 1);
1147 return;
1148 }
1149 consumed = buf->get_subbuf_consumed;
1150 buf->get_subbuf = 0;
1151
1152 /*
1153 * Clear the records_unread counter. (overruns counter)
1154 * Can still be non-zero if a file reader simply grabbed the data
1155 * without using iterators.
1156 * Can be below zero if an iterator is used on a snapshot more than
1157 * once.
1158 */
1159 read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1160 v_add(config, v_read(config,
4746ae29 1161 &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread),
852c2936 1162 &bufb->records_read);
4746ae29 1163 v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0);
852c2936
MD
1164 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
1165 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
1166 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
1167
1168 /*
1169 * Exchange the reader subbuffer with the one we put in its place in the
1170 * writer subbuffer table. Expect the original consumed count. If
1171 * update_read_sb_index fails, this is because the writer updated the
1172 * subbuffer concurrently. We should therefore keep the subbuffer we
1173 * currently have: it has become invalid to try reading this sub-buffer
1174 * consumed count value anyway.
1175 */
1176 consumed_idx = subbuf_index(consumed, chan);
1177 update_read_sb_index(config, &buf->backend, &chan->backend,
1d498196
MD
1178 consumed_idx, buf_trunc_val(consumed, chan),
1179 handle);
852c2936
MD
1180 /*
1181 * update_read_sb_index return value ignored. Don't exchange sub-buffer
1182 * if the writer concurrently updated it.
1183 */
1184}
852c2936
MD
1185
1186/*
1187 * cons_offset is an iterator on all subbuffer offsets between the reader
1188 * position and the writer position. (inclusive)
1189 */
1190static
4cfec15c 1191void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1192 struct channel *chan,
1193 unsigned long cons_offset,
1d498196 1194 int cpu,
38fae1d3 1195 struct lttng_ust_shm_handle *handle)
852c2936 1196{
4cfec15c 1197 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1198 unsigned long cons_idx, commit_count, commit_count_sb;
1199
1200 cons_idx = subbuf_index(cons_offset, chan);
4746ae29
MD
1201 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc);
1202 commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb);
852c2936
MD
1203
1204 if (subbuf_offset(commit_count, chan) != 0)
4d3c9523 1205 DBG("ring buffer %s, cpu %d: "
852c2936
MD
1206 "commit count in subbuffer %lu,\n"
1207 "expecting multiples of %lu bytes\n"
1208 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
1209 chan->backend.name, cpu, cons_idx,
1210 chan->backend.subbuf_size,
1211 commit_count, commit_count_sb);
1212
4d3c9523 1213 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
852c2936
MD
1214 chan->backend.name, cpu, commit_count);
1215}
1216
1217static
4cfec15c 1218void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf,
852c2936 1219 struct channel *chan,
1d498196 1220 void *priv, int cpu,
38fae1d3 1221 struct lttng_ust_shm_handle *handle)
852c2936 1222{
4cfec15c 1223 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1224 unsigned long write_offset, cons_offset;
1225
852c2936
MD
1226 /*
1227 * No need to order commit_count, write_offset and cons_offset reads
1228 * because we execute at teardown when no more writer nor reader
1229 * references are left.
1230 */
1231 write_offset = v_read(config, &buf->offset);
a6352fd4 1232 cons_offset = uatomic_read(&buf->consumed);
852c2936 1233 if (write_offset != cons_offset)
4d3c9523 1234 DBG("ring buffer %s, cpu %d: "
852c2936
MD
1235 "non-consumed data\n"
1236 " [ %lu bytes written, %lu bytes read ]\n",
1237 chan->backend.name, cpu, write_offset, cons_offset);
1238
a6352fd4 1239 for (cons_offset = uatomic_read(&buf->consumed);
852c2936
MD
1240 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
1241 chan)
1242 - cons_offset) > 0;
1243 cons_offset = subbuf_align(cons_offset, chan))
1244 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
1d498196 1245 cpu, handle);
852c2936
MD
1246}
1247
1248static
1249void lib_ring_buffer_print_errors(struct channel *chan,
009769ca
MD
1250 struct lttng_ust_lib_ring_buffer *buf, int cpu,
1251 struct lttng_ust_shm_handle *handle)
852c2936 1252{
4cfec15c 1253 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
a3f61e7f 1254 void *priv = channel_get_private(chan);
852c2936 1255
a1360615
MD
1256 if (!strcmp(chan->backend.name, "relay-metadata-mmap")) {
1257 DBG("ring buffer %s: %lu records written, "
1258 "%lu records overrun\n",
1259 chan->backend.name,
1260 v_read(config, &buf->records_count),
1261 v_read(config, &buf->records_overrun));
1262 } else {
1263 DBG("ring buffer %s, cpu %d: %lu records written, "
1264 "%lu records overrun\n",
1265 chan->backend.name, cpu,
1266 v_read(config, &buf->records_count),
1267 v_read(config, &buf->records_overrun));
1268
1269 if (v_read(config, &buf->records_lost_full)
1270 || v_read(config, &buf->records_lost_wrap)
1271 || v_read(config, &buf->records_lost_big))
1272 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1273 " [ %lu buffer full, %lu nest buffer wrap-around, "
1274 "%lu event too big ]\n",
1275 chan->backend.name, cpu,
1276 v_read(config, &buf->records_lost_full),
1277 v_read(config, &buf->records_lost_wrap),
1278 v_read(config, &buf->records_lost_big));
1279 }
1d498196 1280 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle);
852c2936
MD
1281}
1282
1283/*
1284 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1285 *
1286 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1287 */
1288static
4cfec15c 1289void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1290 struct channel *chan,
1291 struct switch_offsets *offsets,
2fed87ae 1292 uint64_t tsc,
38fae1d3 1293 struct lttng_ust_shm_handle *handle)
852c2936 1294{
4cfec15c 1295 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1296 unsigned long oldidx = subbuf_index(offsets->old, chan);
1297 unsigned long commit_count;
1298
1d498196 1299 config->cb.buffer_begin(buf, tsc, oldidx, handle);
852c2936
MD
1300
1301 /*
1302 * Order all writes to buffer before the commit count update that will
1303 * determine that the subbuffer is full.
1304 */
a6352fd4 1305 cmm_smp_wmb();
852c2936 1306 v_add(config, config->cb.subbuffer_header_size(),
4746ae29
MD
1307 &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1308 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
852c2936
MD
1309 /* Check if the written buffer has to be delivered */
1310 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1d498196 1311 commit_count, oldidx, handle);
852c2936
MD
1312 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1313 offsets->old, commit_count,
1d498196
MD
1314 config->cb.subbuffer_header_size(),
1315 handle);
852c2936
MD
1316}
1317
1318/*
1319 * lib_ring_buffer_switch_old_end: switch old subbuffer
1320 *
1321 * Note : offset_old should never be 0 here. It is ok, because we never perform
1322 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1323 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1324 * subbuffer.
1325 */
1326static
4cfec15c 1327void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1328 struct channel *chan,
1329 struct switch_offsets *offsets,
2fed87ae 1330 uint64_t tsc,
38fae1d3 1331 struct lttng_ust_shm_handle *handle)
852c2936 1332{
4cfec15c 1333 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1334 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1335 unsigned long commit_count, padding_size, data_size;
1336
1337 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1338 padding_size = chan->backend.subbuf_size - data_size;
1d498196
MD
1339 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size,
1340 handle);
852c2936
MD
1341
1342 /*
1343 * Order all writes to buffer before the commit count update that will
1344 * determine that the subbuffer is full.
1345 */
a6352fd4 1346 cmm_smp_wmb();
4746ae29
MD
1347 v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1348 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
852c2936 1349 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1d498196 1350 commit_count, oldidx, handle);
852c2936
MD
1351 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1352 offsets->old, commit_count,
1d498196 1353 padding_size, handle);
852c2936
MD
1354}
1355
1356/*
1357 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1358 *
1359 * This code can be executed unordered : writers may already have written to the
1360 * sub-buffer before this code gets executed, caution. The commit makes sure
1361 * that this code is executed before the deliver of this sub-buffer.
1362 */
1363static
4cfec15c 1364void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1365 struct channel *chan,
1366 struct switch_offsets *offsets,
2fed87ae 1367 uint64_t tsc,
38fae1d3 1368 struct lttng_ust_shm_handle *handle)
852c2936 1369{
4cfec15c 1370 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1371 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1372 unsigned long commit_count;
1373
1d498196 1374 config->cb.buffer_begin(buf, tsc, beginidx, handle);
852c2936
MD
1375
1376 /*
1377 * Order all writes to buffer before the commit count update that will
1378 * determine that the subbuffer is full.
1379 */
a6352fd4 1380 cmm_smp_wmb();
852c2936 1381 v_add(config, config->cb.subbuffer_header_size(),
4746ae29
MD
1382 &shmp_index(handle, buf->commit_hot, beginidx)->cc);
1383 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc);
852c2936
MD
1384 /* Check if the written buffer has to be delivered */
1385 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1d498196 1386 commit_count, beginidx, handle);
852c2936
MD
1387 lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx,
1388 offsets->begin, commit_count,
1d498196
MD
1389 config->cb.subbuffer_header_size(),
1390 handle);
852c2936
MD
1391}
1392
1393/*
1394 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1395 *
1396 * The only remaining threads could be the ones with pending commits. They will
1397 * have to do the deliver themselves.
1398 */
1399static
4cfec15c 1400void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf,
1d498196
MD
1401 struct channel *chan,
1402 struct switch_offsets *offsets,
2fed87ae 1403 uint64_t tsc,
38fae1d3 1404 struct lttng_ust_shm_handle *handle)
852c2936 1405{
4cfec15c 1406 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1407 unsigned long endidx = subbuf_index(offsets->end - 1, chan);
1408 unsigned long commit_count, padding_size, data_size;
1409
1410 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1411 padding_size = chan->backend.subbuf_size - data_size;
1d498196
MD
1412 subbuffer_set_data_size(config, &buf->backend, endidx, data_size,
1413 handle);
852c2936
MD
1414
1415 /*
1416 * Order all writes to buffer before the commit count update that will
1417 * determine that the subbuffer is full.
1418 */
a6352fd4 1419 cmm_smp_wmb();
4746ae29
MD
1420 v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc);
1421 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc);
852c2936 1422 lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1,
1d498196 1423 commit_count, endidx, handle);
852c2936
MD
1424 lib_ring_buffer_write_commit_counter(config, buf, chan, endidx,
1425 offsets->end, commit_count,
1d498196 1426 padding_size, handle);
852c2936
MD
1427}
1428
1429/*
1430 * Returns :
1431 * 0 if ok
1432 * !0 if execution must be aborted.
1433 */
1434static
1435int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
4cfec15c 1436 struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1437 struct channel *chan,
1438 struct switch_offsets *offsets,
2fed87ae 1439 uint64_t *tsc)
852c2936 1440{
4cfec15c 1441 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1442 unsigned long off;
1443
1444 offsets->begin = v_read(config, &buf->offset);
1445 offsets->old = offsets->begin;
1446 offsets->switch_old_start = 0;
1447 off = subbuf_offset(offsets->begin, chan);
1448
1449 *tsc = config->cb.ring_buffer_clock_read(chan);
1450
1451 /*
1452 * Ensure we flush the header of an empty subbuffer when doing the
1453 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1454 * total data gathering duration even if there were no records saved
1455 * after the last buffer switch.
1456 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1457 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1458 * subbuffer header as appropriate.
1459 * The next record that reserves space will be responsible for
1460 * populating the following subbuffer header. We choose not to populate
1461 * the next subbuffer header here because we want to be able to use
a6352fd4
MD
1462 * SWITCH_ACTIVE for periodical buffer flush, which must
1463 * guarantee that all the buffer content (records and header
1464 * timestamps) are visible to the reader. This is required for
1465 * quiescence guarantees for the fusion merge.
852c2936
MD
1466 */
1467 if (mode == SWITCH_FLUSH || off > 0) {
b5a3dfa5 1468 if (caa_unlikely(off == 0)) {
c352d3a4
MD
1469 /*
1470 * A final flush that encounters an empty
1471 * sub-buffer cannot switch buffer if a
1472 * reader is located within this sub-buffer.
1473 * Anyway, the purpose of final flushing of a
1474 * sub-buffer at offset 0 is to handle the case
1475 * of entirely empty stream.
1476 */
1477 if (caa_unlikely(subbuf_trunc(offsets->begin, chan)
1478 - subbuf_trunc((unsigned long)
1479 uatomic_read(&buf->consumed), chan)
1480 >= chan->backend.buf_size))
1481 return -1;
852c2936
MD
1482 /*
1483 * The client does not save any header information.
1484 * Don't switch empty subbuffer on finalize, because it
1485 * is invalid to deliver a completely empty subbuffer.
1486 */
1487 if (!config->cb.subbuffer_header_size())
1488 return -1;
1489 /*
1490 * Need to write the subbuffer start header on finalize.
1491 */
1492 offsets->switch_old_start = 1;
1493 }
1494 offsets->begin = subbuf_align(offsets->begin, chan);
1495 } else
1496 return -1; /* we do not have to switch : buffer is empty */
1497 /* Note: old points to the next subbuf at offset 0 */
1498 offsets->end = offsets->begin;
1499 return 0;
1500}
1501
1502/*
1503 * Force a sub-buffer switch. This operation is completely reentrant : can be
1504 * called while tracing is active with absolutely no lock held.
1505 *
1506 * Note, however, that as a v_cmpxchg is used for some atomic
1507 * operations, this function must be called from the CPU which owns the buffer
1508 * for a ACTIVE flush.
1509 */
4cfec15c 1510void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode,
38fae1d3 1511 struct lttng_ust_shm_handle *handle)
852c2936 1512{
1d498196 1513 struct channel *chan = shmp(handle, buf->backend.chan);
4cfec15c 1514 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
852c2936
MD
1515 struct switch_offsets offsets;
1516 unsigned long oldidx;
2fed87ae 1517 uint64_t tsc;
852c2936
MD
1518
1519 offsets.size = 0;
1520
1521 /*
1522 * Perform retryable operations.
1523 */
1524 do {
1525 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
1526 &tsc))
1527 return; /* Switch not needed */
1528 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
1529 != offsets.old);
1530
1531 /*
1532 * Atomically update last_tsc. This update races against concurrent
1533 * atomic updates, but the race will always cause supplementary full TSC
1534 * records, never the opposite (missing a full TSC record when it would
1535 * be needed).
1536 */
1537 save_last_tsc(config, buf, tsc);
1538
1539 /*
1540 * Push the reader if necessary
1541 */
1542 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
1543
1544 oldidx = subbuf_index(offsets.old, chan);
1d498196 1545 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle);
852c2936
MD
1546
1547 /*
1548 * May need to populate header start on SWITCH_FLUSH.
1549 */
1550 if (offsets.switch_old_start) {
1d498196 1551 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle);
852c2936
MD
1552 offsets.old += config->cb.subbuffer_header_size();
1553 }
1554
1555 /*
1556 * Switch old subbuffer.
1557 */
1d498196 1558 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle);
852c2936 1559}
852c2936
MD
1560
1561/*
1562 * Returns :
1563 * 0 if ok
1564 * -ENOSPC if event size is too large for packet.
1565 * -ENOBUFS if there is currently not enough space in buffer for the event.
1566 * -EIO if data cannot be written into the buffer for any other reason.
1567 */
1568static
4cfec15c 1569int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf,
852c2936
MD
1570 struct channel *chan,
1571 struct switch_offsets *offsets,
4cfec15c 1572 struct lttng_ust_lib_ring_buffer_ctx *ctx)
852c2936 1573{
4cfec15c 1574 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
38fae1d3 1575 struct lttng_ust_shm_handle *handle = ctx->handle;
852c2936
MD
1576 unsigned long reserve_commit_diff;
1577
1578 offsets->begin = v_read(config, &buf->offset);
1579 offsets->old = offsets->begin;
1580 offsets->switch_new_start = 0;
1581 offsets->switch_new_end = 0;
1582 offsets->switch_old_end = 0;
1583 offsets->pre_header_padding = 0;
1584
1585 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
1586 if ((int64_t) ctx->tsc == -EIO)
1587 return -EIO;
1588
1589 if (last_tsc_overflow(config, buf, ctx->tsc))
1590 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
1591
b5a3dfa5 1592 if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
852c2936
MD
1593 offsets->switch_new_start = 1; /* For offsets->begin */
1594 } else {
1595 offsets->size = config->cb.record_header_size(config, chan,
1596 offsets->begin,
1597 &offsets->pre_header_padding,
1598 ctx);
1599 offsets->size +=
1600 lib_ring_buffer_align(offsets->begin + offsets->size,
1601 ctx->largest_align)
1602 + ctx->data_size;
b5a3dfa5 1603 if (caa_unlikely(subbuf_offset(offsets->begin, chan) +
852c2936
MD
1604 offsets->size > chan->backend.subbuf_size)) {
1605 offsets->switch_old_end = 1; /* For offsets->old */
1606 offsets->switch_new_start = 1; /* For offsets->begin */
1607 }
1608 }
b5a3dfa5 1609 if (caa_unlikely(offsets->switch_new_start)) {
852c2936
MD
1610 unsigned long sb_index;
1611
1612 /*
1613 * We are typically not filling the previous buffer completely.
1614 */
b5a3dfa5 1615 if (caa_likely(offsets->switch_old_end))
852c2936
MD
1616 offsets->begin = subbuf_align(offsets->begin, chan);
1617 offsets->begin = offsets->begin
1618 + config->cb.subbuffer_header_size();
1619 /* Test new buffer integrity */
1620 sb_index = subbuf_index(offsets->begin, chan);
1621 reserve_commit_diff =
1622 (buf_trunc(offsets->begin, chan)
1623 >> chan->backend.num_subbuf_order)
1624 - ((unsigned long) v_read(config,
4746ae29 1625 &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb)
852c2936 1626 & chan->commit_count_mask);
b5a3dfa5 1627 if (caa_likely(reserve_commit_diff == 0)) {
852c2936 1628 /* Next subbuffer not being written to. */
b5a3dfa5 1629 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
852c2936
MD
1630 subbuf_trunc(offsets->begin, chan)
1631 - subbuf_trunc((unsigned long)
a6352fd4 1632 uatomic_read(&buf->consumed), chan)
852c2936 1633 >= chan->backend.buf_size)) {
64493e4f
MD
1634 unsigned long nr_lost;
1635
852c2936
MD
1636 /*
1637 * We do not overwrite non consumed buffers
1638 * and we are full : record is lost.
1639 */
64493e4f 1640 nr_lost = v_read(config, &buf->records_lost_full);
852c2936 1641 v_inc(config, &buf->records_lost_full);
64493e4f
MD
1642 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
1643 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1644 nr_lost + 1, chan->backend.name,
1645 buf->backend.cpu);
1646 }
852c2936
MD
1647 return -ENOBUFS;
1648 } else {
1649 /*
1650 * Next subbuffer not being written to, and we
1651 * are either in overwrite mode or the buffer is
1652 * not full. It's safe to write in this new
1653 * subbuffer.
1654 */
1655 }
1656 } else {
64493e4f
MD
1657 unsigned long nr_lost;
1658
852c2936
MD
1659 /*
1660 * Next subbuffer reserve offset does not match the
1661 * commit offset. Drop record in producer-consumer and
1662 * overwrite mode. Caused by either a writer OOPS or too
1663 * many nested writes over a reserve/commit pair.
1664 */
64493e4f 1665 nr_lost = v_read(config, &buf->records_lost_wrap);
852c2936 1666 v_inc(config, &buf->records_lost_wrap);
64493e4f
MD
1667 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
1668 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1669 nr_lost + 1, chan->backend.name,
1670 buf->backend.cpu);
1671 }
852c2936
MD
1672 return -EIO;
1673 }
1674 offsets->size =
1675 config->cb.record_header_size(config, chan,
1676 offsets->begin,
1677 &offsets->pre_header_padding,
1678 ctx);
1679 offsets->size +=
1680 lib_ring_buffer_align(offsets->begin + offsets->size,
1681 ctx->largest_align)
1682 + ctx->data_size;
b5a3dfa5 1683 if (caa_unlikely(subbuf_offset(offsets->begin, chan)
852c2936 1684 + offsets->size > chan->backend.subbuf_size)) {
64493e4f
MD
1685 unsigned long nr_lost;
1686
852c2936
MD
1687 /*
1688 * Record too big for subbuffers, report error, don't
1689 * complete the sub-buffer switch.
1690 */
64493e4f 1691 nr_lost = v_read(config, &buf->records_lost_big);
852c2936 1692 v_inc(config, &buf->records_lost_big);
64493e4f
MD
1693 if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) {
1694 DBG("%lu or more records lost in (%s:%d) record size "
1695 " of %zu bytes is too large for buffer\n",
1696 nr_lost + 1, chan->backend.name,
1697 buf->backend.cpu, offsets->size);
1698 }
852c2936
MD
1699 return -ENOSPC;
1700 } else {
1701 /*
1702 * We just made a successful buffer switch and the
1703 * record fits in the new subbuffer. Let's write.
1704 */
1705 }
1706 } else {
1707 /*
1708 * Record fits in the current buffer and we are not on a switch
1709 * boundary. It's safe to write.
1710 */
1711 }
1712 offsets->end = offsets->begin + offsets->size;
1713
b5a3dfa5 1714 if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) {
852c2936
MD
1715 /*
1716 * The offset_end will fall at the very beginning of the next
1717 * subbuffer.
1718 */
1719 offsets->switch_new_end = 1; /* For offsets->begin */
1720 }
1721 return 0;
1722}
1723
1724/**
1725 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1726 * @ctx: ring buffer context.
1727 *
1728 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1729 * -EIO for other errors, else returns 0.
1730 * It will take care of sub-buffer switching.
1731 */
4cfec15c 1732int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx)
852c2936
MD
1733{
1734 struct channel *chan = ctx->chan;
38fae1d3 1735 struct lttng_ust_shm_handle *handle = ctx->handle;
4cfec15c
MD
1736 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1737 struct lttng_ust_lib_ring_buffer *buf;
852c2936
MD
1738 struct switch_offsets offsets;
1739 int ret;
1740
1741 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
1d498196 1742 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
852c2936 1743 else
1d498196 1744 buf = shmp(handle, chan->backend.buf[0].shmp);
852c2936
MD
1745 ctx->buf = buf;
1746
1747 offsets.size = 0;
1748
1749 do {
1750 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
1751 ctx);
b5a3dfa5 1752 if (caa_unlikely(ret))
852c2936 1753 return ret;
b5a3dfa5 1754 } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
852c2936
MD
1755 offsets.end)
1756 != offsets.old));
1757
1758 /*
1759 * Atomically update last_tsc. This update races against concurrent
1760 * atomic updates, but the race will always cause supplementary full TSC
1761 * records, never the opposite (missing a full TSC record when it would
1762 * be needed).
1763 */
1764 save_last_tsc(config, buf, ctx->tsc);
1765
1766 /*
1767 * Push the reader if necessary
1768 */
1769 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
1770
1771 /*
1772 * Clear noref flag for this subbuffer.
1773 */
1774 lib_ring_buffer_clear_noref(config, &buf->backend,
1d498196
MD
1775 subbuf_index(offsets.end - 1, chan),
1776 handle);
852c2936
MD
1777
1778 /*
1779 * Switch old subbuffer if needed.
1780 */
b5a3dfa5 1781 if (caa_unlikely(offsets.switch_old_end)) {
852c2936 1782 lib_ring_buffer_clear_noref(config, &buf->backend,
1d498196
MD
1783 subbuf_index(offsets.old - 1, chan),
1784 handle);
1785 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle);
852c2936
MD
1786 }
1787
1788 /*
1789 * Populate new subbuffer.
1790 */
b5a3dfa5 1791 if (caa_unlikely(offsets.switch_new_start))
1d498196 1792 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle);
852c2936 1793
b5a3dfa5 1794 if (caa_unlikely(offsets.switch_new_end))
1d498196 1795 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle);
852c2936
MD
1796
1797 ctx->slot_size = offsets.size;
1798 ctx->pre_offset = offsets.begin;
1799 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
1800 return 0;
1801}
f645cfa7
MD
1802
1803/*
1804 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1805 */
1806void lttng_fixup_ringbuffer_tls(void)
1807{
8c90a710 1808 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting)));
f645cfa7 1809}
03d2d293
MD
1810
1811void lib_ringbuffer_signal_init(void)
1812{
1813 sigset_t mask;
1814 int ret;
1815
1816 /*
1817 * Block signal for entire process, so only our thread processes
1818 * it.
1819 */
1820 rb_setmask(&mask);
1821 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
1822 if (ret) {
1823 errno = ret;
1824 PERROR("pthread_sigmask");
1825 }
1826}
This page took 0.111031 seconds and 4 git commands to generate.