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