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