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