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