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