Cleanup warnings
[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 <ust/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 lib_ring_buffer *buf, int cpu,
89 struct shm_handle *handle);
90
91 /*
92 * Must be called under cpu hotplug protection.
93 */
94 void lib_ring_buffer_free(struct lib_ring_buffer *buf,
95 struct 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 lib_ring_buffer *buf,
116 struct shm_handle *handle)
117 {
118 struct channel *chan = shmp(handle, buf->backend.chan);
119 const struct 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 lib_ring_buffer *buf,
171 struct channel_backend *chanb, int cpu,
172 struct shm_handle *handle,
173 struct shm_object *shmobj)
174 {
175 const struct lib_ring_buffer_config *config = &chanb->config;
176 struct channel *chan = caa_container_of(chanb, struct channel, backend);
177 void *priv = chanb->priv;
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 lib_ring_buffer *buf = (struct lib_ring_buffer *)data;
246 struct channel *chan = shmp(handle, buf->backend.chan);
247 const struct 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 lib_ring_buffer *buf,
266 struct shm_handle *handle)
267 {
268 struct channel *chan = shmp(handle, buf->backend.chan);
269 //const struct 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 lib_ring_buffer *buf,
286 struct 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 lib_ring_buffer *buf = (struct lib_ring_buffer *)data;
305 struct channel *chan = shmp(handle, buf->backend.chan);
306 const struct 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 lib_ring_buffer *buf,
328 struct shm_handle *handle)
329 {
330 struct channel *chan = shmp(handle, buf->backend.chan);
331 const struct 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 lib_ring_buffer *buf,
352 struct shm_handle *handle)
353 {
354 struct channel *chan = shmp(handle, buf->backend.chan);
355 const struct 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 shm_handle *handle)
378 {
379 const struct 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 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 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 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: ring buffer client private data
413 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
414 * address mapping. It is used only by RING_BUFFER_STATIC
415 * configuration. It can be set to NULL for other backends.
416 * @subbuf_size: subbuffer size
417 * @num_subbuf: number of subbuffers
418 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
419 * padding to let readers get those sub-buffers.
420 * Used for live streaming.
421 * @read_timer_interval: Time interval (in us) to wake up pending readers.
422 *
423 * Holds cpu hotplug.
424 * Returns NULL on failure.
425 */
426 struct shm_handle *channel_create(const struct lib_ring_buffer_config *config,
427 const char *name, void *priv, void *buf_addr,
428 size_t subbuf_size,
429 size_t num_subbuf, unsigned int switch_timer_interval,
430 unsigned int read_timer_interval,
431 int *shm_fd, int *wait_fd, uint64_t *memory_map_size)
432 {
433 int ret, cpu;
434 size_t shmsize;
435 struct channel *chan;
436 struct shm_handle *handle;
437 struct shm_object *shmobj;
438 struct shm_ref *ref;
439
440 if (lib_ring_buffer_check_config(config, switch_timer_interval,
441 read_timer_interval))
442 return NULL;
443
444 handle = zmalloc(sizeof(struct shm_handle));
445 if (!handle)
446 return NULL;
447
448 /* Allocate table for channel + per-cpu buffers */
449 handle->table = shm_object_table_create(1 + num_possible_cpus());
450 if (!handle->table)
451 goto error_table_alloc;
452
453 /* Calculate the shm allocation layout */
454 shmsize = sizeof(struct channel);
455 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
456 shmsize += sizeof(struct lib_ring_buffer_shmp) * num_possible_cpus();
457 else
458 shmsize += sizeof(struct lib_ring_buffer_shmp);
459
460 shmobj = shm_object_table_append(handle->table, shmsize);
461 if (!shmobj)
462 goto error_append;
463 set_shmp(handle->chan, zalloc_shm(shmobj, sizeof(struct channel)));
464 chan = shmp(handle, handle->chan);
465 if (!chan)
466 goto error_append;
467
468 ret = channel_backend_init(&chan->backend, name, config, priv,
469 subbuf_size, num_subbuf, handle);
470 if (ret)
471 goto error_backend_init;
472
473 chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order);
474 //TODO
475 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
476 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
477 //TODO
478 //init_waitqueue_head(&chan->read_wait);
479 //init_waitqueue_head(&chan->hp_wait);
480
481 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
482 /*
483 * In case of non-hotplug cpu, if the ring-buffer is allocated
484 * in early initcall, it will not be notified of secondary cpus.
485 * In that off case, we need to allocate for all possible cpus.
486 */
487 for_each_possible_cpu(cpu) {
488 struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp);
489 lib_ring_buffer_start_switch_timer(buf, handle);
490 lib_ring_buffer_start_read_timer(buf, handle);
491 }
492 } else {
493 struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp);
494
495 lib_ring_buffer_start_switch_timer(buf, handle);
496 lib_ring_buffer_start_read_timer(buf, handle);
497 }
498 ref = &handle->chan._ref;
499 shm_get_object_data(handle, ref, shm_fd, wait_fd, memory_map_size);
500 return handle;
501
502 error_backend_init:
503 error_append:
504 shm_object_table_destroy(handle->table);
505 error_table_alloc:
506 free(handle);
507 return NULL;
508 }
509
510 struct shm_handle *channel_handle_create(int shm_fd, int wait_fd,
511 uint64_t memory_map_size)
512 {
513 struct shm_handle *handle;
514 struct shm_object *object;
515
516 handle = zmalloc(sizeof(struct shm_handle));
517 if (!handle)
518 return NULL;
519
520 /* Allocate table for channel + per-cpu buffers */
521 handle->table = shm_object_table_create(1 + num_possible_cpus());
522 if (!handle->table)
523 goto error_table_alloc;
524 /* Add channel object */
525 object = shm_object_table_append_shadow(handle->table,
526 shm_fd, wait_fd, memory_map_size);
527 if (!object)
528 goto error_table_object;
529
530 return handle;
531
532 error_table_object:
533 shm_object_table_destroy(handle->table);
534 error_table_alloc:
535 free(handle);
536 return NULL;
537 }
538
539 int channel_handle_add_stream(struct shm_handle *handle,
540 int shm_fd, int wait_fd, uint64_t memory_map_size)
541 {
542 struct shm_object *object;
543
544 /* Add stream object */
545 object = shm_object_table_append_shadow(handle->table,
546 shm_fd, wait_fd, memory_map_size);
547 if (!object)
548 return -1;
549 return 0;
550 }
551
552 static
553 void channel_release(struct channel *chan, struct shm_handle *handle,
554 int shadow)
555 {
556 channel_free(chan, handle, shadow);
557 }
558
559 /**
560 * channel_destroy - Finalize, wait for q.s. and destroy channel.
561 * @chan: channel to destroy
562 *
563 * Holds cpu hotplug.
564 * Call "destroy" callback, finalize channels, decrement the channel
565 * reference count. Note that when readers have completed data
566 * consumption of finalized channels, get_subbuf() will return -ENODATA.
567 * They should release their handle at that point. Returns the private
568 * data pointer.
569 */
570 void *channel_destroy(struct channel *chan, struct shm_handle *handle,
571 int shadow)
572 {
573 const struct lib_ring_buffer_config *config = &chan->backend.config;
574 void *priv;
575 int cpu;
576
577 if (shadow) {
578 channel_release(chan, handle, shadow);
579 return NULL;
580 }
581
582 channel_unregister_notifiers(chan, handle);
583
584 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
585 for_each_channel_cpu(cpu, chan) {
586 struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp);
587
588 if (config->cb.buffer_finalize)
589 config->cb.buffer_finalize(buf,
590 chan->backend.priv,
591 cpu, handle);
592 if (buf->backend.allocated)
593 lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH,
594 handle);
595 /*
596 * Perform flush before writing to finalized.
597 */
598 cmm_smp_wmb();
599 CMM_ACCESS_ONCE(buf->finalized) = 1;
600 //wake_up_interruptible(&buf->read_wait);
601 }
602 } else {
603 struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp);
604
605 if (config->cb.buffer_finalize)
606 config->cb.buffer_finalize(buf, chan->backend.priv, -1, handle);
607 if (buf->backend.allocated)
608 lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH,
609 handle);
610 /*
611 * Perform flush before writing to finalized.
612 */
613 cmm_smp_wmb();
614 CMM_ACCESS_ONCE(buf->finalized) = 1;
615 //wake_up_interruptible(&buf->read_wait);
616 }
617 CMM_ACCESS_ONCE(chan->finalized) = 1;
618 //wake_up_interruptible(&chan->hp_wait);
619 //wake_up_interruptible(&chan->read_wait);
620 /*
621 * sessiond/consumer are keeping a reference on the shm file
622 * descriptor directly. No need to refcount.
623 */
624 priv = chan->backend.priv;
625 channel_release(chan, handle, shadow);
626 return priv;
627 }
628
629 struct lib_ring_buffer *channel_get_ring_buffer(
630 const struct lib_ring_buffer_config *config,
631 struct channel *chan, int cpu,
632 struct shm_handle *handle,
633 int *shm_fd, int *wait_fd,
634 uint64_t *memory_map_size)
635 {
636 struct shm_ref *ref;
637
638 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) {
639 ref = &chan->backend.buf[0].shmp._ref;
640 shm_get_object_data(handle, ref, shm_fd, wait_fd,
641 memory_map_size);
642 return shmp(handle, chan->backend.buf[0].shmp);
643 } else {
644 if (cpu >= num_possible_cpus())
645 return NULL;
646 ref = &chan->backend.buf[cpu].shmp._ref;
647 shm_get_object_data(handle, ref, shm_fd, wait_fd,
648 memory_map_size);
649 return shmp(handle, chan->backend.buf[cpu].shmp);
650 }
651 }
652
653 int lib_ring_buffer_open_read(struct lib_ring_buffer *buf,
654 struct shm_handle *handle,
655 int shadow)
656 {
657 if (shadow) {
658 if (uatomic_cmpxchg(&buf->active_shadow_readers, 0, 1) != 0)
659 return -EBUSY;
660 cmm_smp_mb();
661 return 0;
662 }
663 if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0)
664 return -EBUSY;
665 cmm_smp_mb();
666 return 0;
667 }
668
669 void lib_ring_buffer_release_read(struct lib_ring_buffer *buf,
670 struct shm_handle *handle,
671 int shadow)
672 {
673 struct channel *chan = shmp(handle, buf->backend.chan);
674
675 if (shadow) {
676 CHAN_WARN_ON(chan, uatomic_read(&buf->active_shadow_readers) != 1);
677 cmm_smp_mb();
678 uatomic_dec(&buf->active_shadow_readers);
679 return;
680 }
681 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1);
682 cmm_smp_mb();
683 uatomic_dec(&buf->active_readers);
684 }
685
686 /**
687 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
688 * @buf: ring buffer
689 * @consumed: consumed count indicating the position where to read
690 * @produced: produced count, indicates position when to stop reading
691 *
692 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
693 * data to read at consumed position, or 0 if the get operation succeeds.
694 */
695
696 int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf,
697 unsigned long *consumed, unsigned long *produced,
698 struct shm_handle *handle)
699 {
700 struct channel *chan = shmp(handle, buf->backend.chan);
701 const struct lib_ring_buffer_config *config = &chan->backend.config;
702 unsigned long consumed_cur, write_offset;
703 int finalized;
704
705 finalized = CMM_ACCESS_ONCE(buf->finalized);
706 /*
707 * Read finalized before counters.
708 */
709 cmm_smp_rmb();
710 consumed_cur = uatomic_read(&buf->consumed);
711 /*
712 * No need to issue a memory barrier between consumed count read and
713 * write offset read, because consumed count can only change
714 * concurrently in overwrite mode, and we keep a sequence counter
715 * identifier derived from the write offset to check we are getting
716 * the same sub-buffer we are expecting (the sub-buffers are atomically
717 * "tagged" upon writes, tags are checked upon read).
718 */
719 write_offset = v_read(config, &buf->offset);
720
721 /*
722 * Check that we are not about to read the same subbuffer in
723 * which the writer head is.
724 */
725 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
726 == 0)
727 goto nodata;
728
729 *consumed = consumed_cur;
730 *produced = subbuf_trunc(write_offset, chan);
731
732 return 0;
733
734 nodata:
735 /*
736 * The memory barriers __wait_event()/wake_up_interruptible() take care
737 * of "raw_spin_is_locked" memory ordering.
738 */
739 if (finalized)
740 return -ENODATA;
741 else
742 return -EAGAIN;
743 }
744
745 /**
746 * lib_ring_buffer_put_snapshot - move consumed counter forward
747 * @buf: ring buffer
748 * @consumed_new: new consumed count value
749 */
750 void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf,
751 unsigned long consumed_new,
752 struct shm_handle *handle)
753 {
754 struct lib_ring_buffer_backend *bufb = &buf->backend;
755 struct channel *chan = shmp(handle, bufb->chan);
756 unsigned long consumed;
757
758 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1
759 && uatomic_read(&buf->active_shadow_readers) != 1);
760
761 /*
762 * Only push the consumed value forward.
763 * If the consumed cmpxchg fails, this is because we have been pushed by
764 * the writer in flight recorder mode.
765 */
766 consumed = uatomic_read(&buf->consumed);
767 while ((long) consumed - (long) consumed_new < 0)
768 consumed = uatomic_cmpxchg(&buf->consumed, consumed,
769 consumed_new);
770 }
771
772 /**
773 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
774 * @buf: ring buffer
775 * @consumed: consumed count indicating the position where to read
776 *
777 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
778 * data to read at consumed position, or 0 if the get operation succeeds.
779 */
780 int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf,
781 unsigned long consumed,
782 struct shm_handle *handle)
783 {
784 struct channel *chan = shmp(handle, buf->backend.chan);
785 const struct lib_ring_buffer_config *config = &chan->backend.config;
786 unsigned long consumed_cur, consumed_idx, commit_count, write_offset;
787 int ret;
788 int finalized;
789
790 retry:
791 finalized = CMM_ACCESS_ONCE(buf->finalized);
792 /*
793 * Read finalized before counters.
794 */
795 cmm_smp_rmb();
796 consumed_cur = uatomic_read(&buf->consumed);
797 consumed_idx = subbuf_index(consumed, chan);
798 commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb);
799 /*
800 * Make sure we read the commit count before reading the buffer
801 * data and the write offset. Correct consumed offset ordering
802 * wrt commit count is insured by the use of cmpxchg to update
803 * the consumed offset.
804 */
805 /*
806 * Local rmb to match the remote wmb to read the commit count
807 * before the buffer data and the write offset.
808 */
809 cmm_smp_rmb();
810
811 write_offset = v_read(config, &buf->offset);
812
813 /*
814 * Check that the buffer we are getting is after or at consumed_cur
815 * position.
816 */
817 if ((long) subbuf_trunc(consumed, chan)
818 - (long) subbuf_trunc(consumed_cur, chan) < 0)
819 goto nodata;
820
821 /*
822 * Check that the subbuffer we are trying to consume has been
823 * already fully committed.
824 */
825 if (((commit_count - chan->backend.subbuf_size)
826 & chan->commit_count_mask)
827 - (buf_trunc(consumed_cur, chan)
828 >> chan->backend.num_subbuf_order)
829 != 0)
830 goto nodata;
831
832 /*
833 * Check that we are not about to read the same subbuffer in
834 * which the writer head is.
835 */
836 if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan)
837 == 0)
838 goto nodata;
839
840 /*
841 * Failure to get the subbuffer causes a busy-loop retry without going
842 * to a wait queue. These are caused by short-lived race windows where
843 * the writer is getting access to a subbuffer we were trying to get
844 * access to. Also checks that the "consumed" buffer count we are
845 * looking for matches the one contained in the subbuffer id.
846 */
847 ret = update_read_sb_index(config, &buf->backend, &chan->backend,
848 consumed_idx, buf_trunc_val(consumed, chan),
849 handle);
850 if (ret)
851 goto retry;
852 subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id);
853
854 buf->get_subbuf_consumed = consumed;
855 buf->get_subbuf = 1;
856
857 return 0;
858
859 nodata:
860 /*
861 * The memory barriers __wait_event()/wake_up_interruptible() take care
862 * of "raw_spin_is_locked" memory ordering.
863 */
864 if (finalized)
865 return -ENODATA;
866 else
867 return -EAGAIN;
868 }
869
870 /**
871 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
872 * @buf: ring buffer
873 */
874 void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf,
875 struct shm_handle *handle)
876 {
877 struct lib_ring_buffer_backend *bufb = &buf->backend;
878 struct channel *chan = shmp(handle, bufb->chan);
879 const struct lib_ring_buffer_config *config = &chan->backend.config;
880 unsigned long read_sb_bindex, consumed_idx, consumed;
881
882 CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1
883 && uatomic_read(&buf->active_shadow_readers) != 1);
884
885 if (!buf->get_subbuf) {
886 /*
887 * Reader puts a subbuffer it did not get.
888 */
889 CHAN_WARN_ON(chan, 1);
890 return;
891 }
892 consumed = buf->get_subbuf_consumed;
893 buf->get_subbuf = 0;
894
895 /*
896 * Clear the records_unread counter. (overruns counter)
897 * Can still be non-zero if a file reader simply grabbed the data
898 * without using iterators.
899 * Can be below zero if an iterator is used on a snapshot more than
900 * once.
901 */
902 read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
903 v_add(config, v_read(config,
904 &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread),
905 &bufb->records_read);
906 v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0);
907 CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE
908 && subbuffer_id_is_noref(config, bufb->buf_rsb.id));
909 subbuffer_id_set_noref(config, &bufb->buf_rsb.id);
910
911 /*
912 * Exchange the reader subbuffer with the one we put in its place in the
913 * writer subbuffer table. Expect the original consumed count. If
914 * update_read_sb_index fails, this is because the writer updated the
915 * subbuffer concurrently. We should therefore keep the subbuffer we
916 * currently have: it has become invalid to try reading this sub-buffer
917 * consumed count value anyway.
918 */
919 consumed_idx = subbuf_index(consumed, chan);
920 update_read_sb_index(config, &buf->backend, &chan->backend,
921 consumed_idx, buf_trunc_val(consumed, chan),
922 handle);
923 /*
924 * update_read_sb_index return value ignored. Don't exchange sub-buffer
925 * if the writer concurrently updated it.
926 */
927 }
928
929 /*
930 * cons_offset is an iterator on all subbuffer offsets between the reader
931 * position and the writer position. (inclusive)
932 */
933 static
934 void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf,
935 struct channel *chan,
936 unsigned long cons_offset,
937 int cpu,
938 struct shm_handle *handle)
939 {
940 const struct lib_ring_buffer_config *config = &chan->backend.config;
941 unsigned long cons_idx, commit_count, commit_count_sb;
942
943 cons_idx = subbuf_index(cons_offset, chan);
944 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc);
945 commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb);
946
947 if (subbuf_offset(commit_count, chan) != 0)
948 ERRMSG("ring buffer %s, cpu %d: "
949 "commit count in subbuffer %lu,\n"
950 "expecting multiples of %lu bytes\n"
951 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
952 chan->backend.name, cpu, cons_idx,
953 chan->backend.subbuf_size,
954 commit_count, commit_count_sb);
955
956 ERRMSG("ring buffer: %s, cpu %d: %lu bytes committed\n",
957 chan->backend.name, cpu, commit_count);
958 }
959
960 static
961 void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf,
962 struct channel *chan,
963 void *priv, int cpu,
964 struct shm_handle *handle)
965 {
966 const struct lib_ring_buffer_config *config = &chan->backend.config;
967 unsigned long write_offset, cons_offset;
968
969 /*
970 * Can be called in the error path of allocation when
971 * trans_channel_data is not yet set.
972 */
973 if (!chan)
974 return;
975 /*
976 * No need to order commit_count, write_offset and cons_offset reads
977 * because we execute at teardown when no more writer nor reader
978 * references are left.
979 */
980 write_offset = v_read(config, &buf->offset);
981 cons_offset = uatomic_read(&buf->consumed);
982 if (write_offset != cons_offset)
983 ERRMSG("ring buffer %s, cpu %d: "
984 "non-consumed data\n"
985 " [ %lu bytes written, %lu bytes read ]\n",
986 chan->backend.name, cpu, write_offset, cons_offset);
987
988 for (cons_offset = uatomic_read(&buf->consumed);
989 (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset),
990 chan)
991 - cons_offset) > 0;
992 cons_offset = subbuf_align(cons_offset, chan))
993 lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset,
994 cpu, handle);
995 }
996
997 static
998 void lib_ring_buffer_print_errors(struct channel *chan,
999 struct lib_ring_buffer *buf, int cpu,
1000 struct shm_handle *handle)
1001 {
1002 const struct lib_ring_buffer_config *config = &chan->backend.config;
1003 void *priv = chan->backend.priv;
1004
1005 ERRMSG("ring buffer %s, cpu %d: %lu records written, "
1006 "%lu records overrun\n",
1007 chan->backend.name, cpu,
1008 v_read(config, &buf->records_count),
1009 v_read(config, &buf->records_overrun));
1010
1011 if (v_read(config, &buf->records_lost_full)
1012 || v_read(config, &buf->records_lost_wrap)
1013 || v_read(config, &buf->records_lost_big))
1014 ERRMSG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1015 " [ %lu buffer full, %lu nest buffer wrap-around, "
1016 "%lu event too big ]\n",
1017 chan->backend.name, cpu,
1018 v_read(config, &buf->records_lost_full),
1019 v_read(config, &buf->records_lost_wrap),
1020 v_read(config, &buf->records_lost_big));
1021
1022 lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle);
1023 }
1024
1025 /*
1026 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1027 *
1028 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1029 */
1030 static
1031 void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf,
1032 struct channel *chan,
1033 struct switch_offsets *offsets,
1034 u64 tsc,
1035 struct shm_handle *handle)
1036 {
1037 const struct lib_ring_buffer_config *config = &chan->backend.config;
1038 unsigned long oldidx = subbuf_index(offsets->old, chan);
1039 unsigned long commit_count;
1040
1041 config->cb.buffer_begin(buf, tsc, oldidx, handle);
1042
1043 /*
1044 * Order all writes to buffer before the commit count update that will
1045 * determine that the subbuffer is full.
1046 */
1047 cmm_smp_wmb();
1048 v_add(config, config->cb.subbuffer_header_size(),
1049 &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1050 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1051 /* Check if the written buffer has to be delivered */
1052 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old,
1053 commit_count, oldidx, handle);
1054 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1055 offsets->old, commit_count,
1056 config->cb.subbuffer_header_size(),
1057 handle);
1058 }
1059
1060 /*
1061 * lib_ring_buffer_switch_old_end: switch old subbuffer
1062 *
1063 * Note : offset_old should never be 0 here. It is ok, because we never perform
1064 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1065 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1066 * subbuffer.
1067 */
1068 static
1069 void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf,
1070 struct channel *chan,
1071 struct switch_offsets *offsets,
1072 u64 tsc,
1073 struct shm_handle *handle)
1074 {
1075 const struct lib_ring_buffer_config *config = &chan->backend.config;
1076 unsigned long oldidx = subbuf_index(offsets->old - 1, chan);
1077 unsigned long commit_count, padding_size, data_size;
1078
1079 data_size = subbuf_offset(offsets->old - 1, chan) + 1;
1080 padding_size = chan->backend.subbuf_size - data_size;
1081 subbuffer_set_data_size(config, &buf->backend, oldidx, data_size,
1082 handle);
1083
1084 /*
1085 * Order all writes to buffer before the commit count update that will
1086 * determine that the subbuffer is full.
1087 */
1088 cmm_smp_wmb();
1089 v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1090 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc);
1091 lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1,
1092 commit_count, oldidx, handle);
1093 lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx,
1094 offsets->old, commit_count,
1095 padding_size, handle);
1096 }
1097
1098 /*
1099 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1100 *
1101 * This code can be executed unordered : writers may already have written to the
1102 * sub-buffer before this code gets executed, caution. The commit makes sure
1103 * that this code is executed before the deliver of this sub-buffer.
1104 */
1105 static
1106 void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf,
1107 struct channel *chan,
1108 struct switch_offsets *offsets,
1109 u64 tsc,
1110 struct shm_handle *handle)
1111 {
1112 const struct lib_ring_buffer_config *config = &chan->backend.config;
1113 unsigned long beginidx = subbuf_index(offsets->begin, chan);
1114 unsigned long commit_count;
1115
1116 config->cb.buffer_begin(buf, tsc, beginidx, handle);
1117
1118 /*
1119 * Order all writes to buffer before the commit count update that will
1120 * determine that the subbuffer is full.
1121 */
1122 cmm_smp_wmb();
1123 v_add(config, config->cb.subbuffer_header_size(),
1124 &shmp_index(handle, buf->commit_hot, beginidx)->cc);
1125 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc);
1126 /* Check if the written buffer has to be delivered */
1127 lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin,
1128 commit_count, beginidx, handle);
1129 lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx,
1130 offsets->begin, commit_count,
1131 config->cb.subbuffer_header_size(),
1132 handle);
1133 }
1134
1135 /*
1136 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1137 *
1138 * The only remaining threads could be the ones with pending commits. They will
1139 * have to do the deliver themselves.
1140 */
1141 static
1142 void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf,
1143 struct channel *chan,
1144 struct switch_offsets *offsets,
1145 u64 tsc,
1146 struct shm_handle *handle)
1147 {
1148 const struct lib_ring_buffer_config *config = &chan->backend.config;
1149 unsigned long endidx = subbuf_index(offsets->end - 1, chan);
1150 unsigned long commit_count, padding_size, data_size;
1151
1152 data_size = subbuf_offset(offsets->end - 1, chan) + 1;
1153 padding_size = chan->backend.subbuf_size - data_size;
1154 subbuffer_set_data_size(config, &buf->backend, endidx, data_size,
1155 handle);
1156
1157 /*
1158 * Order all writes to buffer before the commit count update that will
1159 * determine that the subbuffer is full.
1160 */
1161 cmm_smp_wmb();
1162 v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc);
1163 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc);
1164 lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1,
1165 commit_count, endidx, handle);
1166 lib_ring_buffer_write_commit_counter(config, buf, chan, endidx,
1167 offsets->end, commit_count,
1168 padding_size, handle);
1169 }
1170
1171 /*
1172 * Returns :
1173 * 0 if ok
1174 * !0 if execution must be aborted.
1175 */
1176 static
1177 int lib_ring_buffer_try_switch_slow(enum switch_mode mode,
1178 struct lib_ring_buffer *buf,
1179 struct channel *chan,
1180 struct switch_offsets *offsets,
1181 u64 *tsc)
1182 {
1183 const struct lib_ring_buffer_config *config = &chan->backend.config;
1184 unsigned long off;
1185
1186 offsets->begin = v_read(config, &buf->offset);
1187 offsets->old = offsets->begin;
1188 offsets->switch_old_start = 0;
1189 off = subbuf_offset(offsets->begin, chan);
1190
1191 *tsc = config->cb.ring_buffer_clock_read(chan);
1192
1193 /*
1194 * Ensure we flush the header of an empty subbuffer when doing the
1195 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1196 * total data gathering duration even if there were no records saved
1197 * after the last buffer switch.
1198 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1199 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1200 * subbuffer header as appropriate.
1201 * The next record that reserves space will be responsible for
1202 * populating the following subbuffer header. We choose not to populate
1203 * the next subbuffer header here because we want to be able to use
1204 * SWITCH_ACTIVE for periodical buffer flush, which must
1205 * guarantee that all the buffer content (records and header
1206 * timestamps) are visible to the reader. This is required for
1207 * quiescence guarantees for the fusion merge.
1208 */
1209 if (mode == SWITCH_FLUSH || off > 0) {
1210 if (unlikely(off == 0)) {
1211 /*
1212 * The client does not save any header information.
1213 * Don't switch empty subbuffer on finalize, because it
1214 * is invalid to deliver a completely empty subbuffer.
1215 */
1216 if (!config->cb.subbuffer_header_size())
1217 return -1;
1218 /*
1219 * Need to write the subbuffer start header on finalize.
1220 */
1221 offsets->switch_old_start = 1;
1222 }
1223 offsets->begin = subbuf_align(offsets->begin, chan);
1224 } else
1225 return -1; /* we do not have to switch : buffer is empty */
1226 /* Note: old points to the next subbuf at offset 0 */
1227 offsets->end = offsets->begin;
1228 return 0;
1229 }
1230
1231 /*
1232 * Force a sub-buffer switch. This operation is completely reentrant : can be
1233 * called while tracing is active with absolutely no lock held.
1234 *
1235 * Note, however, that as a v_cmpxchg is used for some atomic
1236 * operations, this function must be called from the CPU which owns the buffer
1237 * for a ACTIVE flush.
1238 */
1239 void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode,
1240 struct shm_handle *handle)
1241 {
1242 struct channel *chan = shmp(handle, buf->backend.chan);
1243 const struct lib_ring_buffer_config *config = &chan->backend.config;
1244 struct switch_offsets offsets;
1245 unsigned long oldidx;
1246 u64 tsc;
1247
1248 offsets.size = 0;
1249
1250 /*
1251 * Perform retryable operations.
1252 */
1253 do {
1254 if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets,
1255 &tsc))
1256 return; /* Switch not needed */
1257 } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end)
1258 != offsets.old);
1259
1260 /*
1261 * Atomically update last_tsc. This update races against concurrent
1262 * atomic updates, but the race will always cause supplementary full TSC
1263 * records, never the opposite (missing a full TSC record when it would
1264 * be needed).
1265 */
1266 save_last_tsc(config, buf, tsc);
1267
1268 /*
1269 * Push the reader if necessary
1270 */
1271 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old);
1272
1273 oldidx = subbuf_index(offsets.old, chan);
1274 lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle);
1275
1276 /*
1277 * May need to populate header start on SWITCH_FLUSH.
1278 */
1279 if (offsets.switch_old_start) {
1280 lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle);
1281 offsets.old += config->cb.subbuffer_header_size();
1282 }
1283
1284 /*
1285 * Switch old subbuffer.
1286 */
1287 lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle);
1288 }
1289
1290 /*
1291 * Returns :
1292 * 0 if ok
1293 * -ENOSPC if event size is too large for packet.
1294 * -ENOBUFS if there is currently not enough space in buffer for the event.
1295 * -EIO if data cannot be written into the buffer for any other reason.
1296 */
1297 static
1298 int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf,
1299 struct channel *chan,
1300 struct switch_offsets *offsets,
1301 struct lib_ring_buffer_ctx *ctx)
1302 {
1303 const struct lib_ring_buffer_config *config = &chan->backend.config;
1304 struct shm_handle *handle = ctx->handle;
1305 unsigned long reserve_commit_diff;
1306
1307 offsets->begin = v_read(config, &buf->offset);
1308 offsets->old = offsets->begin;
1309 offsets->switch_new_start = 0;
1310 offsets->switch_new_end = 0;
1311 offsets->switch_old_end = 0;
1312 offsets->pre_header_padding = 0;
1313
1314 ctx->tsc = config->cb.ring_buffer_clock_read(chan);
1315 if ((int64_t) ctx->tsc == -EIO)
1316 return -EIO;
1317
1318 if (last_tsc_overflow(config, buf, ctx->tsc))
1319 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
1320
1321 if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) {
1322 offsets->switch_new_start = 1; /* For offsets->begin */
1323 } else {
1324 offsets->size = config->cb.record_header_size(config, chan,
1325 offsets->begin,
1326 &offsets->pre_header_padding,
1327 ctx);
1328 offsets->size +=
1329 lib_ring_buffer_align(offsets->begin + offsets->size,
1330 ctx->largest_align)
1331 + ctx->data_size;
1332 if (unlikely(subbuf_offset(offsets->begin, chan) +
1333 offsets->size > chan->backend.subbuf_size)) {
1334 offsets->switch_old_end = 1; /* For offsets->old */
1335 offsets->switch_new_start = 1; /* For offsets->begin */
1336 }
1337 }
1338 if (unlikely(offsets->switch_new_start)) {
1339 unsigned long sb_index;
1340
1341 /*
1342 * We are typically not filling the previous buffer completely.
1343 */
1344 if (likely(offsets->switch_old_end))
1345 offsets->begin = subbuf_align(offsets->begin, chan);
1346 offsets->begin = offsets->begin
1347 + config->cb.subbuffer_header_size();
1348 /* Test new buffer integrity */
1349 sb_index = subbuf_index(offsets->begin, chan);
1350 reserve_commit_diff =
1351 (buf_trunc(offsets->begin, chan)
1352 >> chan->backend.num_subbuf_order)
1353 - ((unsigned long) v_read(config,
1354 &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb)
1355 & chan->commit_count_mask);
1356 if (likely(reserve_commit_diff == 0)) {
1357 /* Next subbuffer not being written to. */
1358 if (unlikely(config->mode != RING_BUFFER_OVERWRITE &&
1359 subbuf_trunc(offsets->begin, chan)
1360 - subbuf_trunc((unsigned long)
1361 uatomic_read(&buf->consumed), chan)
1362 >= chan->backend.buf_size)) {
1363 /*
1364 * We do not overwrite non consumed buffers
1365 * and we are full : record is lost.
1366 */
1367 v_inc(config, &buf->records_lost_full);
1368 return -ENOBUFS;
1369 } else {
1370 /*
1371 * Next subbuffer not being written to, and we
1372 * are either in overwrite mode or the buffer is
1373 * not full. It's safe to write in this new
1374 * subbuffer.
1375 */
1376 }
1377 } else {
1378 /*
1379 * Next subbuffer reserve offset does not match the
1380 * commit offset. Drop record in producer-consumer and
1381 * overwrite mode. Caused by either a writer OOPS or too
1382 * many nested writes over a reserve/commit pair.
1383 */
1384 v_inc(config, &buf->records_lost_wrap);
1385 return -EIO;
1386 }
1387 offsets->size =
1388 config->cb.record_header_size(config, chan,
1389 offsets->begin,
1390 &offsets->pre_header_padding,
1391 ctx);
1392 offsets->size +=
1393 lib_ring_buffer_align(offsets->begin + offsets->size,
1394 ctx->largest_align)
1395 + ctx->data_size;
1396 if (unlikely(subbuf_offset(offsets->begin, chan)
1397 + offsets->size > chan->backend.subbuf_size)) {
1398 /*
1399 * Record too big for subbuffers, report error, don't
1400 * complete the sub-buffer switch.
1401 */
1402 v_inc(config, &buf->records_lost_big);
1403 return -ENOSPC;
1404 } else {
1405 /*
1406 * We just made a successful buffer switch and the
1407 * record fits in the new subbuffer. Let's write.
1408 */
1409 }
1410 } else {
1411 /*
1412 * Record fits in the current buffer and we are not on a switch
1413 * boundary. It's safe to write.
1414 */
1415 }
1416 offsets->end = offsets->begin + offsets->size;
1417
1418 if (unlikely(subbuf_offset(offsets->end, chan) == 0)) {
1419 /*
1420 * The offset_end will fall at the very beginning of the next
1421 * subbuffer.
1422 */
1423 offsets->switch_new_end = 1; /* For offsets->begin */
1424 }
1425 return 0;
1426 }
1427
1428 /**
1429 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1430 * @ctx: ring buffer context.
1431 *
1432 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1433 * -EIO for other errors, else returns 0.
1434 * It will take care of sub-buffer switching.
1435 */
1436 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx)
1437 {
1438 struct channel *chan = ctx->chan;
1439 struct shm_handle *handle = ctx->handle;
1440 const struct lib_ring_buffer_config *config = &chan->backend.config;
1441 struct lib_ring_buffer *buf;
1442 struct switch_offsets offsets;
1443 int ret;
1444
1445 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
1446 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
1447 else
1448 buf = shmp(handle, chan->backend.buf[0].shmp);
1449 ctx->buf = buf;
1450
1451 offsets.size = 0;
1452
1453 do {
1454 ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets,
1455 ctx);
1456 if (unlikely(ret))
1457 return ret;
1458 } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old,
1459 offsets.end)
1460 != offsets.old));
1461
1462 /*
1463 * Atomically update last_tsc. This update races against concurrent
1464 * atomic updates, but the race will always cause supplementary full TSC
1465 * records, never the opposite (missing a full TSC record when it would
1466 * be needed).
1467 */
1468 save_last_tsc(config, buf, ctx->tsc);
1469
1470 /*
1471 * Push the reader if necessary
1472 */
1473 lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1);
1474
1475 /*
1476 * Clear noref flag for this subbuffer.
1477 */
1478 lib_ring_buffer_clear_noref(config, &buf->backend,
1479 subbuf_index(offsets.end - 1, chan),
1480 handle);
1481
1482 /*
1483 * Switch old subbuffer if needed.
1484 */
1485 if (unlikely(offsets.switch_old_end)) {
1486 lib_ring_buffer_clear_noref(config, &buf->backend,
1487 subbuf_index(offsets.old - 1, chan),
1488 handle);
1489 lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle);
1490 }
1491
1492 /*
1493 * Populate new subbuffer.
1494 */
1495 if (unlikely(offsets.switch_new_start))
1496 lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle);
1497
1498 if (unlikely(offsets.switch_new_end))
1499 lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle);
1500
1501 ctx->slot_size = offsets.size;
1502 ctx->pre_offset = offsets.begin;
1503 ctx->buf_offset = offsets.begin + offsets.pre_header_padding;
1504 return 0;
1505 }
This page took 0.089303 seconds and 5 git commands to generate.