Reactivate error printing
[lttng-ust.git] / libringbuffer / ring_buffer_frontend.c
1 /*
2 * ring_buffer_frontend.c
3 *
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 *
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
23 *
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
27 *
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
32 *
33 * Author:
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
35 *
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
40 * And from K42 :
41 * Bob Wisniewski <bob@watson.ibm.com>
42 *
43 * Buffer reader semantic :
44 *
45 * - get_subbuf_size
46 * while buffer is not finalized and empty
47 * - get_subbuf
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
51 * - put_subbuf
52 */
53
54 #define _GNU_SOURCE
55 #include <sys/types.h>
56 #include <sys/mman.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <urcu/compiler.h>
60 #include <urcu/ref.h>
61 #include <urcu/tls-compat.h>
62 #include <helper.h>
63
64 #include "smp.h"
65 #include <lttng/ringbuffer-config.h>
66 #include "vatomic.h"
67 #include "backend.h"
68 #include "frontend.h"
69 #include "shm.h"
70 #include "tlsfixup.h"
71 #include "../liblttng-ust/compat.h" /* For ENODATA */
72
73 #ifndef max
74 #define max(a, b) ((a) > (b) ? (a) : (b))
75 #endif
76
77 /* Print DBG() messages about events lost only every 1048576 hits */
78 #define DBG_PRINT_NR_LOST (1UL << 20)
79
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
96 /*
97 * Internal structure representing offsets to use at a sub-buffer switch.
98 */
99 struct 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
106 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting);
107
108 static
109 void lib_ring_buffer_print_errors(struct channel *chan,
110 struct lttng_ust_lib_ring_buffer *buf, int cpu,
111 struct lttng_ust_shm_handle *handle);
112
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 */
122 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
123 struct lttng_ust_shm_handle *handle)
124 {
125 struct channel *chan = shmp(handle, buf->backend.chan);
126 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
127 unsigned int i;
128
129 /*
130 * Reset iterator first. It will put the subbuffer if it currently holds
131 * it.
132 */
133 v_set(config, &buf->offset, 0);
134 for (i = 0; i < chan->backend.num_subbuf; i++) {
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);
138 }
139 uatomic_set(&buf->consumed, 0);
140 uatomic_set(&buf->record_disabled, 0);
141 v_set(config, &buf->last_tsc, 0);
142 lib_ring_buffer_backend_reset(&buf->backend, handle);
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 }
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 */
161 void channel_reset(struct channel *chan)
162 {
163 /*
164 * Reset iterators first. Will put the subbuffer if held for reading.
165 */
166 uatomic_set(&chan->record_disabled, 0);
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 }
173
174 /*
175 * Must be called under cpu hotplug protection.
176 */
177 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf,
178 struct channel_backend *chanb, int cpu,
179 struct lttng_ust_shm_handle *handle,
180 struct shm_object *shmobj)
181 {
182 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
183 struct channel *chan = caa_container_of(chanb, struct channel, backend);
184 void *priv = channel_get_private(chan);
185 size_t subbuf_header_size;
186 uint64_t tsc;
187 int ret;
188
189 /* Test for cpu hotplug */
190 if (buf->backend.allocated)
191 return 0;
192
193 ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend,
194 cpu, handle, shmobj);
195 if (ret)
196 return ret;
197
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)) {
203 ret = -ENOMEM;
204 goto free_chanbuf;
205 }
206
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)) {
212 ret = -ENOMEM;
213 goto free_commit;
214 }
215
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);
222 subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id);
223 tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan));
224 config->cb.buffer_begin(buf, tsc, 0, handle);
225 v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc);
226
227 if (config->cb.buffer_create) {
228 ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle);
229 if (ret)
230 goto free_init;
231 }
232 buf->backend.allocated = 1;
233 return 0;
234
235 /* Error handling */
236 free_init:
237 /* commit_cold will be freed by shm teardown */
238 free_commit:
239 /* commit_hot will be freed by shm teardown */
240 free_chanbuf:
241 return ret;
242 }
243
244 #if 0
245 static void switch_buffer_timer(unsigned long data)
246 {
247 struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data;
248 struct channel *chan = shmp(handle, buf->backend.chan);
249 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
250
251 /*
252 * Only flush buffers periodically if readers are active.
253 */
254 if (uatomic_read(&buf->active_readers))
255 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle);
256
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);
264 }
265 #endif //0
266
267 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer *buf,
268 struct lttng_ust_shm_handle *handle)
269 {
270 struct channel *chan = shmp(handle, buf->backend.chan);
271 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
272
273 if (!chan->switch_timer_interval || buf->switch_timer_enabled)
274 return;
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);
284 buf->switch_timer_enabled = 1;
285 }
286
287 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer *buf,
288 struct lttng_ust_shm_handle *handle)
289 {
290 struct channel *chan = shmp(handle, buf->backend.chan);
291
292 if (!chan->switch_timer_interval || !buf->switch_timer_enabled)
293 return;
294
295 //TODO
296 //del_timer_sync(&buf->switch_timer);
297 buf->switch_timer_enabled = 0;
298 }
299
300 #if 0
301 /*
302 * Polling timer to check the channels for data.
303 */
304 static void read_buffer_timer(unsigned long data)
305 {
306 struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data;
307 struct channel *chan = shmp(handle, buf->backend.chan);
308 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
309
310 CHAN_WARN_ON(chan, !buf->backend.allocated);
311
312 if (uatomic_read(&buf->active_readers))
313 && lib_ring_buffer_poll_deliver(config, buf, chan)) {
314 //TODO
315 //wake_up_interruptible(&buf->read_wait);
316 //wake_up_interruptible(&chan->read_wait);
317 }
318
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);
326 }
327 #endif //0
328
329 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer *buf,
330 struct lttng_ust_shm_handle *handle)
331 {
332 struct channel *chan = shmp(handle, buf->backend.chan);
333 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
334
335 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
336 || !chan->read_timer_interval
337 || buf->read_timer_enabled)
338 return;
339
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;
345
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);
350 buf->read_timer_enabled = 1;
351 }
352
353 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer *buf,
354 struct lttng_ust_shm_handle *handle)
355 {
356 struct channel *chan = shmp(handle, buf->backend.chan);
357 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
358
359 if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER
360 || !chan->read_timer_interval
361 || !buf->read_timer_enabled)
362 return;
363
364 //TODO
365 //del_timer_sync(&buf->read_timer);
366 /*
367 * do one more check to catch data that has been written in the last
368 * timer period.
369 */
370 if (lib_ring_buffer_poll_deliver(config, buf, chan, handle)) {
371 //TODO
372 //wake_up_interruptible(&buf->read_wait);
373 //wake_up_interruptible(&chan->read_wait);
374 }
375 buf->read_timer_enabled = 0;
376 }
377
378 static void channel_unregister_notifiers(struct channel *chan,
379 struct lttng_ust_shm_handle *handle)
380 {
381 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
382 int cpu;
383
384 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
385 for_each_possible_cpu(cpu) {
386 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp);
387
388 lib_ring_buffer_stop_switch_timer(buf, handle);
389 lib_ring_buffer_stop_read_timer(buf, handle);
390 }
391 } else {
392 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp);
393
394 lib_ring_buffer_stop_switch_timer(buf, handle);
395 lib_ring_buffer_stop_read_timer(buf, handle);
396 }
397 //channel_backend_unregister_notifiers(&chan->backend);
398 }
399
400 static 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
421 static void channel_free(struct channel *chan,
422 struct lttng_ust_shm_handle *handle)
423 {
424 channel_print_errors(chan, handle);
425 channel_backend_free(&chan->backend, handle);
426 /* chan is freed by shm teardown */
427 shm_object_table_destroy(handle->table);
428 free(handle);
429 }
430
431 /**
432 * channel_create - Create channel.
433 * @config: ring buffer instance configuration
434 * @name: name of the channel
435 * @priv_data: ring buffer client private data area pointer (output)
436 * @priv_data_size: length, in bytes, of the private data area.
437 * @priv_data_init: initialization data for private data.
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 */
451 struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
452 const char *name,
453 void **priv_data,
454 size_t priv_data_align,
455 size_t priv_data_size,
456 void *priv_data_init,
457 void *buf_addr, size_t subbuf_size,
458 size_t num_subbuf, unsigned int switch_timer_interval,
459 unsigned int read_timer_interval)
460 {
461 int ret, cpu;
462 size_t shmsize, chansize;
463 struct channel *chan;
464 struct lttng_ust_shm_handle *handle;
465 struct shm_object *shmobj;
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;
472
473 if (lib_ring_buffer_check_config(config, switch_timer_interval,
474 read_timer_interval))
475 return NULL;
476
477 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
478 if (!handle)
479 return NULL;
480
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;
485
486 /* Calculate the shm allocation layout */
487 shmsize = sizeof(struct channel);
488 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp));
489 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams;
490 chansize = shmsize;
491 if (priv_data_align)
492 shmsize += offset_align(shmsize, priv_data_align);
493 shmsize += priv_data_size;
494
495 /* Allocate normal memory for channel (not shared) */
496 shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM);
497 if (!shmobj)
498 goto error_append;
499 /* struct channel is at object 0, offset 0 (hardcoded) */
500 set_shmp(handle->chan, zalloc_shm(shmobj, chansize));
501 assert(handle->chan._ref.index == 0);
502 assert(handle->chan._ref.offset == 0);
503 chan = shmp(handle, handle->chan);
504 if (!chan)
505 goto error_append;
506 chan->nr_streams = nr_streams;
507
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);
518 memcpy(*priv_data, priv_data_init, priv_data_size);
519 } else {
520 chan->priv_data_offset = -1;
521 if (priv_data)
522 *priv_data = NULL;
523 }
524
525 ret = channel_backend_init(&chan->backend, name, config,
526 subbuf_size, num_subbuf, handle);
527 if (ret)
528 goto error_backend_init;
529
530 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
531 //TODO
532 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
533 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
534 //TODO
535 //init_waitqueue_head(&chan->read_wait);
536 //init_waitqueue_head(&chan->hp_wait);
537
538 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
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 */
544 for_each_possible_cpu(cpu) {
545 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp);
546 lib_ring_buffer_start_switch_timer(buf, handle);
547 lib_ring_buffer_start_read_timer(buf, handle);
548 }
549 } else {
550 struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp);
551
552 lib_ring_buffer_start_switch_timer(buf, handle);
553 lib_ring_buffer_start_read_timer(buf, handle);
554 }
555 return handle;
556
557 error_backend_init:
558 error_append:
559 shm_object_table_destroy(handle->table);
560 error_table_alloc:
561 free(handle);
562 return NULL;
563 }
564
565 struct lttng_ust_shm_handle *channel_handle_create(void *data,
566 uint64_t memory_map_size)
567 {
568 struct lttng_ust_shm_handle *handle;
569 struct shm_object *object;
570
571 handle = zmalloc(sizeof(struct lttng_ust_shm_handle));
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 */
580 object = shm_object_table_append_mem(handle->table, data,
581 memory_map_size);
582 if (!object)
583 goto error_table_object;
584 /* struct channel is at object 0, offset 0 (hardcoded) */
585 handle->chan._ref.index = 0;
586 handle->chan._ref.offset = 0;
587 return handle;
588
589 error_table_object:
590 shm_object_table_destroy(handle->table);
591 error_table_alloc:
592 free(handle);
593 return NULL;
594 }
595
596 int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
597 int shm_fd, int wakeup_fd, uint32_t stream_nr,
598 uint64_t memory_map_size)
599 {
600 struct shm_object *object;
601
602 /* Add stream object */
603 object = shm_object_table_append_shm(handle->table,
604 shm_fd, wakeup_fd, stream_nr,
605 memory_map_size);
606 if (!object)
607 return -EINVAL;
608 return 0;
609 }
610
611 unsigned 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
617 static
618 void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle)
619 {
620 channel_free(chan, handle);
621 }
622
623 /**
624 * channel_destroy - Finalize, wait for q.s. and destroy channel.
625 * @chan: channel to destroy
626 *
627 * Holds cpu hotplug.
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.
631 * They should release their handle at that point.
632 */
633 void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
634 int consumer)
635 {
636 if (consumer) {
637 /*
638 * Note: the consumer takes care of finalizing and
639 * switching the buffers.
640 */
641 channel_unregister_notifiers(chan, handle);
642 }
643
644 /*
645 * sessiond/consumer are keeping a reference on the shm file
646 * descriptor directly. No need to refcount.
647 */
648 channel_release(chan, handle);
649 return;
650 }
651
652 struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
653 const struct lttng_ust_lib_ring_buffer_config *config,
654 struct channel *chan, int cpu,
655 struct lttng_ust_shm_handle *handle,
656 int *shm_fd, int *wait_fd,
657 int *wakeup_fd,
658 uint64_t *memory_map_size)
659 {
660 struct shm_ref *ref;
661
662 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
663 cpu = 0;
664 } else {
665 if (cpu >= num_possible_cpus())
666 return NULL;
667 }
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);
675 }
676
677 int 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)
681 {
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;
689 }
690 ref = &chan->backend.buf[cpu].shmp._ref;
691 return shm_close_wait_fd(handle, ref);
692 }
693
694 int 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
711 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
712 struct lttng_ust_shm_handle *handle)
713 {
714 if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0)
715 return -EBUSY;
716 cmm_smp_mb();
717 return 0;
718 }
719
720 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
721 struct lttng_ust_shm_handle *handle)
722 {
723 struct channel *chan = shmp(handle, buf->backend.chan);
724
725 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
726 cmm_smp_mb();
727 uatomic_dec(&buf->active_readers);
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.
738 */
739
740 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
741 unsigned long *consumed, unsigned long *produced,
742 struct lttng_ust_shm_handle *handle)
743 {
744 struct channel *chan = shmp(handle, buf->backend.chan);
745 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
746 unsigned long consumed_cur, write_offset;
747 int finalized;
748
749 finalized = CMM_ACCESS_ONCE(buf->finalized);
750 /*
751 * Read finalized before counters.
752 */
753 cmm_smp_rmb();
754 consumed_cur = uatomic_read(&buf->consumed);
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
778 nodata:
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;
785 else
786 return -EAGAIN;
787 }
788
789 /**
790 * lib_ring_buffer_put_snapshot - move consumed counter forward
791 * @buf: ring buffer
792 * @consumed_new: new consumed count value
793 */
794 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
795 unsigned long consumed_new,
796 struct lttng_ust_shm_handle *handle)
797 {
798 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
799 struct channel *chan = shmp(handle, bufb->chan);
800 unsigned long consumed;
801
802 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
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 */
809 consumed = uatomic_read(&buf->consumed);
810 while ((long) consumed - (long) consumed_new < 0)
811 consumed = uatomic_cmpxchg(&buf->consumed, consumed,
812 consumed_new);
813 }
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.
822 */
823 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
824 unsigned long consumed,
825 struct lttng_ust_shm_handle *handle)
826 {
827 struct channel *chan = shmp(handle, buf->backend.chan);
828 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
829 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
830 int ret;
831 int finalized;
832
833 retry:
834 finalized = CMM_ACCESS_ONCE(buf->finalized);
835 /*
836 * Read finalized before counters.
837 */
838 cmm_smp_rmb();
839 consumed_cur = uatomic_read(&buf->consumed);
840 consumed_idx = subbuf_index(consumed, chan);
841 commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb);
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.
847 */
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();
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,
891 consumed_idx, buf_trunc_val(consumed, chan),
892 handle);
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
902 nodata:
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;
909 else
910 return -EAGAIN;
911 }
912
913 /**
914 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
915 * @buf: ring buffer
916 */
917 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
918 struct lttng_ust_shm_handle *handle)
919 {
920 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
921 struct channel *chan = shmp(handle, bufb->chan);
922 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
923 unsigned long read_sb_bindex, consumed_idx, consumed;
924
925 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
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,
946 &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread),
947 &bufb->records_read);
948 v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0);
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,
963 consumed_idx, buf_trunc_val(consumed, chan),
964 handle);
965 /*
966 * update_read_sb_index return value ignored. Don't exchange sub-buffer
967 * if the writer concurrently updated it.
968 */
969 }
970
971 /*
972 * cons_offset is an iterator on all subbuffer offsets between the reader
973 * position and the writer position. (inclusive)
974 */
975 static
976 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf,
977 struct channel *chan,
978 unsigned long cons_offset,
979 int cpu,
980 struct lttng_ust_shm_handle *handle)
981 {
982 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
983 unsigned long cons_idx, commit_count, commit_count_sb;
984
985 cons_idx = subbuf_index(cons_offset, chan);
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);
988
989 if (subbuf_offset(commit_count, chan) != 0)
990 DBG("ring buffer %s, cpu %d: "
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
998 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
999 chan->backend.name, cpu, commit_count);
1000 }
1001
1002 static
1003 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf,
1004 struct channel *chan,
1005 void *priv, int cpu,
1006 struct lttng_ust_shm_handle *handle)
1007 {
1008 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1009 unsigned long write_offset, cons_offset;
1010
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);
1017 cons_offset = uatomic_read(&buf->consumed);
1018 if (write_offset != cons_offset)
1019 DBG("ring buffer %s, cpu %d: "
1020 "non-consumed data\n"
1021 " [ %lu bytes written, %lu bytes read ]\n",
1022 chan->backend.name, cpu, write_offset, cons_offset);
1023
1024 for (cons_offset = uatomic_read(&buf->consumed);
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,
1030 cpu, handle);
1031 }
1032
1033 static
1034 void lib_ring_buffer_print_errors(struct channel *chan,
1035 struct lttng_ust_lib_ring_buffer *buf, int cpu,
1036 struct lttng_ust_shm_handle *handle)
1037 {
1038 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1039 void *priv = channel_get_private(chan);
1040
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 }
1065 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle);
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 */
1073 static
1074 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf,
1075 struct channel *chan,
1076 struct switch_offsets *offsets,
1077 uint64_t tsc,
1078 struct lttng_ust_shm_handle *handle)
1079 {
1080 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1081 unsigned long oldidx = subbuf_index(offsets->old, chan);
1082 unsigned long commit_count;
1083
1084 config->cb.buffer_begin(buf, tsc, oldidx, handle);
1085
1086 /*
1087 * Order all writes to buffer before the commit count update that will
1088 * determine that the subbuffer is full.
1089 */
1090 cmm_smp_wmb();
1091 v_add(config, config->cb.subbuffer_header_size(),
1092 &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1093 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1094 /* Check if the written buffer has to be delivered */
1095 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1096 commit_count, oldidx, handle);
1097 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1098 offsets->old, commit_count,
1099 config->cb.subbuffer_header_size(),
1100 handle);
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 */
1111 static
1112 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf,
1113 struct channel *chan,
1114 struct switch_offsets *offsets,
1115 uint64_t tsc,
1116 struct lttng_ust_shm_handle *handle)
1117 {
1118 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
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;
1124 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size,
1125 handle);
1126
1127 /*
1128 * Order all writes to buffer before the commit count update that will
1129 * determine that the subbuffer is full.
1130 */
1131 cmm_smp_wmb();
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);
1134 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1135 commit_count, oldidx, handle);
1136 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1137 offsets->old, commit_count,
1138 padding_size, handle);
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 */
1148 static
1149 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf,
1150 struct channel *chan,
1151 struct switch_offsets *offsets,
1152 uint64_t tsc,
1153 struct lttng_ust_shm_handle *handle)
1154 {
1155 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1156 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1157 unsigned long commit_count;
1158
1159 config->cb.buffer_begin(buf, tsc, beginidx, handle);
1160
1161 /*
1162 * Order all writes to buffer before the commit count update that will
1163 * determine that the subbuffer is full.
1164 */
1165 cmm_smp_wmb();
1166 v_add(config, config->cb.subbuffer_header_size(),
1167 &shmp_index(handle, buf->commit_hot, beginidx)->cc);
1168 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc);
1169 /* Check if the written buffer has to be delivered */
1170 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1171 commit_count, beginidx, handle);
1172 lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx,
1173 offsets->begin, commit_count,
1174 config->cb.subbuffer_header_size(),
1175 handle);
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 */
1184 static
1185 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf,
1186 struct channel *chan,
1187 struct switch_offsets *offsets,
1188 uint64_t tsc,
1189 struct lttng_ust_shm_handle *handle)
1190 {
1191 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
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;
1197 subbuffer_set_data_size(config, &buf->backend, endidx, data_size,
1198 handle);
1199
1200 /*
1201 * Order all writes to buffer before the commit count update that will
1202 * determine that the subbuffer is full.
1203 */
1204 cmm_smp_wmb();
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);
1207 lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1,
1208 commit_count, endidx, handle);
1209 lib_ring_buffer_write_commit_counter(config, buf, chan, endidx,
1210 offsets->end, commit_count,
1211 padding_size, handle);
1212 }
1213
1214 /*
1215 * Returns :
1216 * 0 if ok
1217 * !0 if execution must be aborted.
1218 */
1219 static
1220 int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
1221 struct lttng_ust_lib_ring_buffer *buf,
1222 struct channel *chan,
1223 struct switch_offsets *offsets,
1224 uint64_t *tsc)
1225 {
1226 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
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
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.
1251 */
1252 if (mode == SWITCH_FLUSH || off > 0) {
1253 if (caa_unlikely(off == 0)) {
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;
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 */
1295 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode,
1296 struct lttng_ust_shm_handle *handle)
1297 {
1298 struct channel *chan = shmp(handle, buf->backend.chan);
1299 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1300 struct switch_offsets offsets;
1301 unsigned long oldidx;
1302 uint64_t tsc;
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);
1330 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle);
1331
1332 /*
1333 * May need to populate header start on SWITCH_FLUSH.
1334 */
1335 if (offsets.switch_old_start) {
1336 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle);
1337 offsets.old += config->cb.subbuffer_header_size();
1338 }
1339
1340 /*
1341 * Switch old subbuffer.
1342 */
1343 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle);
1344 }
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 */
1353 static
1354 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf,
1355 struct channel *chan,
1356 struct switch_offsets *offsets,
1357 struct lttng_ust_lib_ring_buffer_ctx *ctx)
1358 {
1359 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1360 struct lttng_ust_shm_handle *handle = ctx->handle;
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
1377 if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
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;
1388 if (caa_unlikely(subbuf_offset(offsets->begin, chan) +
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 }
1394 if (caa_unlikely(offsets->switch_new_start)) {
1395 unsigned long sb_index;
1396
1397 /*
1398 * We are typically not filling the previous buffer completely.
1399 */
1400 if (caa_likely(offsets->switch_old_end))
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,
1410 &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb)
1411 & chan->commit_count_mask);
1412 if (caa_likely(reserve_commit_diff == 0)) {
1413 /* Next subbuffer not being written to. */
1414 if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1415 subbuf_trunc(offsets->begin, chan)
1416 - subbuf_trunc((unsigned long)
1417 uatomic_read(&buf->consumed), chan)
1418 >= chan->backend.buf_size)) {
1419 unsigned long nr_lost;
1420
1421 /*
1422 * We do not overwrite non consumed buffers
1423 * and we are full : record is lost.
1424 */
1425 nr_lost = v_read(config, &buf->records_lost_full);
1426 v_inc(config, &buf->records_lost_full);
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 }
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 {
1442 unsigned long nr_lost;
1443
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 */
1450 nr_lost = v_read(config, &buf->records_lost_wrap);
1451 v_inc(config, &buf->records_lost_wrap);
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 }
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;
1468 if (caa_unlikely(subbuf_offset(offsets->begin, chan)
1469 + offsets->size > chan->backend.subbuf_size)) {
1470 unsigned long nr_lost;
1471
1472 /*
1473 * Record too big for subbuffers, report error, don't
1474 * complete the sub-buffer switch.
1475 */
1476 nr_lost = v_read(config, &buf->records_lost_big);
1477 v_inc(config, &buf->records_lost_big);
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 }
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
1499 if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) {
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 */
1517 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx)
1518 {
1519 struct channel *chan = ctx->chan;
1520 struct lttng_ust_shm_handle *handle = ctx->handle;
1521 const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
1522 struct lttng_ust_lib_ring_buffer *buf;
1523 struct switch_offsets offsets;
1524 int ret;
1525
1526 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
1527 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
1528 else
1529 buf = shmp(handle, chan->backend.buf[0].shmp);
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);
1537 if (caa_unlikely(ret))
1538 return ret;
1539 } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
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,
1560 subbuf_index(offsets.end - 1, chan),
1561 handle);
1562
1563 /*
1564 * Switch old subbuffer if needed.
1565 */
1566 if (caa_unlikely(offsets.switch_old_end)) {
1567 lib_ring_buffer_clear_noref(config, &buf->backend,
1568 subbuf_index(offsets.old - 1, chan),
1569 handle);
1570 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle);
1571 }
1572
1573 /*
1574 * Populate new subbuffer.
1575 */
1576 if (caa_unlikely(offsets.switch_new_start))
1577 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle);
1578
1579 if (caa_unlikely(offsets.switch_new_end))
1580 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle);
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 }
1587
1588 /*
1589 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1590 */
1591 void lttng_fixup_ringbuffer_tls(void)
1592 {
1593 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting)));
1594 }
This page took 0.060811 seconds and 5 git commands to generate.