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