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