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