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