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