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