3 * LTTng userspace tracer buffering system
5 * Copyright (C) 2009 - Pierre-Marc Fournier (pierre-marc dot fournier at polymtl dot ca)
6 * Copyright (C) 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <ust/kernelcompat.h>
28 #include <kcompat/kref.h>
32 #include "tracercore.h"
35 static DEFINE_MUTEX(ust_buffers_channels_mutex
);
36 static LIST_HEAD(ust_buffers_channels
);
38 static int ust_buffers_init_buffer(struct ltt_trace_struct
*trace
,
39 struct ust_channel
*ltt_chan
,
40 struct ust_buffer
*buf
,
41 unsigned int n_subbufs
);
43 static int ust_buffers_alloc_buf(struct ust_buffer
*buf
, size_t *size
)
48 *size
= PAGE_ALIGN(*size
);
50 result
= buf
->shmid
= shmget(getpid(), *size
, IPC_CREAT
| IPC_EXCL
| 0700);
51 if(result
== -1 && errno
== EINVAL
) {
52 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
55 else if(result
== -1) {
60 ptr
= shmat(buf
->shmid
, NULL
, 0);
61 if(ptr
== (void *) -1) {
66 /* Already mark the shared memory for destruction. This will occur only
67 * when all users have detached.
69 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
76 buf
->buf_size
= *size
;
81 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
89 static struct ust_buffer
*ust_buffers_create_buf(struct ust_channel
*channel
)
93 result
= ust_buffers_alloc_buf(channel
->buf
, &channel
->alloc_size
);
97 ((struct ust_buffer
*)channel
->buf
)->chan
= channel
;
98 kref_get(&channel
->kref
);
105 static void ust_buffers_destroy_channel(struct kref
*kref
)
107 struct ust_channel
*chan
= container_of(kref
, struct ust_channel
, kref
);
111 static void ust_buffers_destroy_buf(struct ust_buffer
*buf
)
113 struct ust_channel
*chan
= buf
->chan
;
116 result
= munmap(buf
->buf_data
, buf
->buf_size
);
122 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
125 /* called from kref_put */
126 static void ust_buffers_remove_buf(struct kref
*kref
)
128 struct ust_buffer
*buf
= container_of(kref
, struct ust_buffer
, kref
);
129 ust_buffers_destroy_buf(buf
);
132 static struct ust_buffer
*ust_buffers_open_buf(struct ust_channel
*chan
)
134 struct ust_buffer
*buf
= NULL
;
137 buf
= ust_buffers_create_buf(chan
);
141 kref_init(&buf
->kref
);
143 err
= ust_buffers_init_buffer(chan
->trace
, chan
, buf
, chan
->subbuf_cnt
);
150 /* FIXME: decrementally destroy on error? */
154 * ust_buffers_close_buf - close a channel buffer
157 static void ust_buffers_close_buf(struct ust_buffer
*buf
)
159 kref_put(&buf
->kref
, ust_buffers_remove_buf
);
162 int ust_buffers_channel_open(struct ust_channel
*chan
, size_t subbuf_size
, size_t subbuf_cnt
)
164 if(subbuf_size
== 0 || subbuf_cnt
== 0)
167 chan
->version
= UST_CHANNEL_VERSION
;
168 chan
->subbuf_cnt
= subbuf_cnt
;
169 chan
->subbuf_size
= subbuf_size
;
170 chan
->subbuf_size_order
= get_count_order(subbuf_size
);
171 chan
->alloc_size
= FIX_SIZE(subbuf_size
* subbuf_cnt
);
172 kref_init(&chan
->kref
);
174 mutex_lock(&ust_buffers_channels_mutex
);
175 chan
->buf
= ust_buffers_open_buf(chan
);
178 list_add(&chan
->list
, &ust_buffers_channels
);
179 mutex_unlock(&ust_buffers_channels_mutex
);
184 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
185 mutex_unlock(&ust_buffers_channels_mutex
);
189 void ust_buffers_channel_close(struct ust_channel
*chan
)
194 mutex_lock(&ust_buffers_channels_mutex
);
196 ust_buffers_close_buf(chan
->buf
);
198 list_del(&chan
->list
);
199 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
200 mutex_unlock(&ust_buffers_channels_mutex
);
203 /* _ust_buffers_write()
205 * @buf: destination buffer
206 * @offset: offset in destination
207 * @src: source buffer
208 * @len: length of source
209 * @cpy: already copied
212 void _ust_buffers_write(struct ust_buffer
*buf
, size_t offset
,
213 const void *src
, size_t len
, ssize_t cpy
)
219 WARN_ON(offset
>= buf
->buf_size
);
221 cpy
= min_t(size_t, len
, buf
->buf_size
- offset
);
222 ust_buffers_do_copy(buf
->buf_data
+ offset
, src
, cpy
);
223 } while (unlikely(len
!= cpy
));
227 * ltt_buffers_offset_address - get address of a location within the buffer
229 * @offset : offset within the buffer.
231 * Return the address where a given offset is located.
232 * Should be used to get the current subbuffer header pointer. Given we know
233 * it's never on a page boundary, it's safe to write directly to this address,
234 * as long as the write is never bigger than a page size.
236 void *ltt_buffers_offset_address(struct ust_buffer
*buf
, size_t offset
)
238 return ((char *)buf
->buf_data
)+offset
;
246 * Last TSC comparison functions. Check if the current TSC overflows
247 * LTT_TSC_BITS bits from the last TSC read. Reads and writes last_tsc
251 /* FIXME: does this test work properly? */
252 #if (BITS_PER_LONG == 32)
253 static inline void save_last_tsc(struct ust_buffer
*ltt_buf
,
256 ltt_buf
->last_tsc
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
259 static inline int last_tsc_overflow(struct ust_buffer
*ltt_buf
,
262 unsigned long tsc_shifted
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
264 if (unlikely((tsc_shifted
- ltt_buf
->last_tsc
)))
270 static inline void save_last_tsc(struct ust_buffer
*ltt_buf
,
273 ltt_buf
->last_tsc
= (unsigned long)tsc
;
276 static inline int last_tsc_overflow(struct ust_buffer
*ltt_buf
,
279 if (unlikely((tsc
- ltt_buf
->last_tsc
) >> LTT_TSC_BITS
))
287 * A switch is done during tracing or as a final flush after tracing (so it
288 * won't write in the new sub-buffer).
290 enum force_switch_mode
{ FORCE_ACTIVE
, FORCE_FLUSH
};
292 static void ust_buffers_destroy_buffer(struct ust_channel
*ltt_chan
);
294 static void ltt_force_switch(struct ust_buffer
*buf
,
295 enum force_switch_mode mode
);
300 static void ltt_buffer_begin_callback(struct ust_buffer
*buf
,
301 u64 tsc
, unsigned int subbuf_idx
)
303 struct ust_channel
*channel
= buf
->chan
;
304 struct ltt_subbuffer_header
*header
=
305 (struct ltt_subbuffer_header
*)
306 ltt_buffers_offset_address(buf
,
307 subbuf_idx
* buf
->chan
->subbuf_size
);
309 header
->cycle_count_begin
= tsc
;
310 header
->lost_size
= 0xFFFFFFFF; /* for debugging */
311 header
->buf_size
= buf
->chan
->subbuf_size
;
312 ltt_write_trace_header(channel
->trace
, header
);
316 * offset is assumed to never be 0 here : never deliver a completely empty
317 * subbuffer. The lost size is between 0 and subbuf_size-1.
319 static notrace
void ltt_buffer_end_callback(struct ust_buffer
*buf
,
320 u64 tsc
, unsigned int offset
, unsigned int subbuf_idx
)
322 struct ltt_subbuffer_header
*header
=
323 (struct ltt_subbuffer_header
*)
324 ltt_buffers_offset_address(buf
,
325 subbuf_idx
* buf
->chan
->subbuf_size
);
327 header
->lost_size
= SUBBUF_OFFSET((buf
->chan
->subbuf_size
- offset
),
329 header
->cycle_count_end
= tsc
;
330 header
->events_lost
= local_read(&buf
->events_lost
);
331 header
->subbuf_corrupt
= local_read(&buf
->corrupted_subbuffers
);
335 void (*wake_consumer
)(void *, int) = NULL
;
337 void relay_set_wake_consumer(void (*wake
)(void *, int))
339 wake_consumer
= wake
;
342 void relay_wake_consumer(void *arg
, int finished
)
345 wake_consumer(arg
, finished
);
348 static notrace
void ltt_deliver(struct ust_buffer
*buf
, unsigned int subbuf_idx
,
353 //ust// #ifdef CONFIG_LTT_VMCORE
354 local_set(&buf
->commit_seq
[subbuf_idx
], commit_count
);
357 /* wakeup consumer */
358 result
= write(buf
->data_ready_fd_write
, "1", 1);
360 PERROR("write (in ltt_relay_buffer_flush)");
361 ERR("this should never happen!");
363 //ust// atomic_set(<t_buf->wakeup_readers, 1);
367 * This function should not be called from NMI interrupt context
369 static notrace
void ltt_buf_unfull(struct ust_buffer
*buf
,
370 unsigned int subbuf_idx
,
373 //ust// struct ltt_channel_struct *ltt_channel =
374 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
375 //ust// struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
377 //ust// ltt_relay_wake_writers(ltt_buf);
380 int ust_buffers_do_get_subbuf(struct ust_buffer
*buf
, long *pconsumed_old
)
382 struct ust_channel
*channel
= buf
->chan
;
383 long consumed_old
, consumed_idx
, commit_count
, write_offset
;
384 consumed_old
= atomic_long_read(&buf
->consumed
);
385 consumed_idx
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
386 commit_count
= local_read(&buf
->commit_count
[consumed_idx
]);
388 * Make sure we read the commit count before reading the buffer
389 * data and the write offset. Correct consumed offset ordering
390 * wrt commit count is insured by the use of cmpxchg to update
391 * the consumed offset.
394 write_offset
= local_read(&buf
->offset
);
396 * Check that the subbuffer we are trying to consume has been
397 * already fully committed.
399 if (((commit_count
- buf
->chan
->subbuf_size
)
400 & channel
->commit_count_mask
)
401 - (BUFFER_TRUNC(consumed_old
, buf
->chan
)
402 >> channel
->n_subbufs_order
)
407 * Check that we are not about to read the same subbuffer in
408 * which the writer head is.
410 if ((SUBBUF_TRUNC(write_offset
, buf
->chan
)
411 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
416 *pconsumed_old
= consumed_old
;
420 int ust_buffers_do_put_subbuf(struct ust_buffer
*buf
, u32 uconsumed_old
)
422 long consumed_new
, consumed_old
;
424 consumed_old
= atomic_long_read(&buf
->consumed
);
425 consumed_old
= consumed_old
& (~0xFFFFFFFFL
);
426 consumed_old
= consumed_old
| uconsumed_old
;
427 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
429 //ust// spin_lock(<t_buf->full_lock);
430 if (atomic_long_cmpxchg(&buf
->consumed
, consumed_old
,
433 /* We have been pushed by the writer : the last
434 * buffer read _is_ corrupted! It can also
435 * happen if this is a buffer we never got. */
436 //ust// spin_unlock(<t_buf->full_lock);
439 /* tell the client that buffer is now unfull */
442 index
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
443 data
= BUFFER_OFFSET(consumed_old
, buf
->chan
);
444 ltt_buf_unfull(buf
, index
, data
);
445 //ust// spin_unlock(<t_buf->full_lock);
450 static void ltt_relay_print_subbuffer_errors(
451 struct ust_channel
*channel
,
454 struct ust_buffer
*ltt_buf
= channel
->buf
;
455 long cons_idx
, commit_count
, write_offset
;
457 cons_idx
= SUBBUF_INDEX(cons_off
, channel
);
458 commit_count
= local_read(<t_buf
->commit_count
[cons_idx
]);
460 * No need to order commit_count and write_offset reads because we
461 * execute after trace is stopped when there are no readers left.
463 write_offset
= local_read(<t_buf
->offset
);
464 WARN( "LTT : unread channel %s offset is %ld "
465 "and cons_off : %ld\n",
466 channel
->channel_name
, write_offset
, cons_off
);
467 /* Check each sub-buffer for non filled commit count */
468 if (((commit_count
- channel
->subbuf_size
) & channel
->commit_count_mask
)
469 - (BUFFER_TRUNC(cons_off
, channel
) >> channel
->n_subbufs_order
) != 0) {
470 ERR("LTT : %s : subbuffer %lu has non filled "
471 "commit count %lu.\n",
472 channel
->channel_name
, cons_idx
, commit_count
);
474 ERR("LTT : %s : commit count : %lu, subbuf size %zd\n",
475 channel
->channel_name
, commit_count
,
476 channel
->subbuf_size
);
479 static void ltt_relay_print_errors(struct ltt_trace_struct
*trace
,
480 struct ust_channel
*channel
)
482 struct ust_buffer
*ltt_buf
= channel
->buf
;
486 * Can be called in the error path of allocation when
487 * trans_channel_data is not yet set.
492 for (cons_off
= atomic_long_read(<t_buf
->consumed
);
493 (SUBBUF_TRUNC(local_read(<t_buf
->offset
),
496 cons_off
= SUBBUF_ALIGN(cons_off
, channel
))
497 ltt_relay_print_subbuffer_errors(channel
, cons_off
);
500 static void ltt_relay_print_buffer_errors(struct ust_channel
*channel
)
502 struct ltt_trace_struct
*trace
= channel
->trace
;
503 struct ust_buffer
*ltt_buf
= channel
->buf
;
505 if (local_read(<t_buf
->events_lost
))
507 "LTT : %s : %ld events lost "
509 channel
->channel_name
,
510 local_read(<t_buf
->events_lost
),
511 channel
->channel_name
);
512 if (local_read(<t_buf
->corrupted_subbuffers
))
514 "LTT : %s : %ld corrupted subbuffers "
516 channel
->channel_name
,
517 local_read(<t_buf
->corrupted_subbuffers
),
518 channel
->channel_name
);
520 ltt_relay_print_errors(trace
, channel
);
523 static void ltt_relay_release_channel(struct kref
*kref
)
525 struct ust_channel
*ltt_chan
= container_of(kref
,
526 struct ust_channel
, kref
);
533 //ust// static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
534 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
535 //ust// unsigned int cpu, unsigned int n_subbufs)
537 //ust// struct ltt_channel_buf_struct *ltt_buf =
538 //ust// percpu_ptr(ltt_chan->buf, cpu);
539 //ust// unsigned int j;
541 //ust// ltt_buf->commit_count =
542 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
543 //ust// GFP_KERNEL, cpu_to_node(cpu));
544 //ust// if (!ltt_buf->commit_count)
545 //ust// return -ENOMEM;
546 //ust// kref_get(&trace->kref);
547 //ust// kref_get(&trace->ltt_transport_kref);
548 //ust// kref_get(<t_chan->kref);
549 //ust// local_set(<t_buf->offset, ltt_subbuffer_header_size());
550 //ust// atomic_long_set(<t_buf->consumed, 0);
551 //ust// atomic_long_set(<t_buf->active_readers, 0);
552 //ust// for (j = 0; j < n_subbufs; j++)
553 //ust// local_set(<t_buf->commit_count[j], 0);
554 //ust// init_waitqueue_head(<t_buf->write_wait);
555 //ust// atomic_set(<t_buf->wakeup_readers, 0);
556 //ust// spin_lock_init(<t_buf->full_lock);
558 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
559 //ust// /* atomic_add made on local variable on data that belongs to
560 //ust// * various CPUs : ok because tracing not started (for this cpu). */
561 //ust// local_add(ltt_subbuffer_header_size(), <t_buf->commit_count[0]);
563 //ust// local_set(<t_buf->events_lost, 0);
564 //ust// local_set(<t_buf->corrupted_subbuffers, 0);
569 static int ust_buffers_init_buffer(struct ltt_trace_struct
*trace
,
570 struct ust_channel
*ltt_chan
, struct ust_buffer
*buf
,
571 unsigned int n_subbufs
)
578 zmalloc(sizeof(buf
->commit_count
) * n_subbufs
);
579 if (!buf
->commit_count
)
581 kref_get(&trace
->kref
);
582 kref_get(&trace
->ltt_transport_kref
);
583 kref_get(<t_chan
->kref
);
584 local_set(&buf
->offset
, ltt_subbuffer_header_size());
585 atomic_long_set(&buf
->consumed
, 0);
586 atomic_long_set(&buf
->active_readers
, 0);
587 for (j
= 0; j
< n_subbufs
; j
++)
588 local_set(&buf
->commit_count
[j
], 0);
589 //ust// init_waitqueue_head(&buf->write_wait);
590 //ust// atomic_set(&buf->wakeup_readers, 0);
591 //ust// spin_lock_init(&buf->full_lock);
593 ltt_buffer_begin_callback(buf
, trace
->start_tsc
, 0);
595 local_add(ltt_subbuffer_header_size(), &buf
->commit_count
[0]);
597 local_set(&buf
->events_lost
, 0);
598 local_set(&buf
->corrupted_subbuffers
, 0);
605 buf
->data_ready_fd_read
= fds
[0];
606 buf
->data_ready_fd_write
= fds
[1];
608 /* FIXME: do we actually need this? */
609 result
= fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
614 //ust// buf->commit_seq = malloc(sizeof(buf->commit_seq) * n_subbufs);
615 //ust// if(!ltt_buf->commit_seq) {
619 /* FIXME: decrementally destroy on error */
624 /* FIXME: use this function */
625 static void ust_buffers_destroy_buffer(struct ust_channel
*ltt_chan
)
627 struct ltt_trace_struct
*trace
= ltt_chan
->trace
;
628 struct ust_buffer
*ltt_buf
= ltt_chan
->buf
;
630 kref_put(<t_chan
->trace
->ltt_transport_kref
,
631 ltt_release_transport
);
632 ltt_relay_print_buffer_errors(ltt_chan
);
633 //ust// free(ltt_buf->commit_seq);
634 kfree(ltt_buf
->commit_count
);
635 ltt_buf
->commit_count
= NULL
;
636 kref_put(<t_chan
->kref
, ltt_relay_release_channel
);
637 kref_put(&trace
->kref
, ltt_release_trace
);
638 //ust// wake_up_interruptible(&trace->kref_wq);
641 static void ltt_chan_alloc_ltt_buf(struct ust_channel
*chan
)
647 /* FIXME: increase size if we have a seq_commit array that overflows the page */
648 size_t size
= PAGE_ALIGN(1);
650 result
= chan
->buf_shmid
= shmget(getpid(), size
, IPC_CREAT
| IPC_EXCL
| 0700);
651 if(chan
->buf_shmid
== -1) {
656 ptr
= shmat(chan
->buf_shmid
, NULL
, 0);
657 if(ptr
== (void *) -1) {
662 /* Already mark the shared memory for destruction. This will occur only
663 * when all users have detached.
665 result
= shmctl(chan
->buf_shmid
, IPC_RMID
, NULL
);
676 result
= shmctl(chan
->buf_shmid
, IPC_RMID
, NULL
);
687 static int ust_buffers_create_channel(const char *trace_name
, struct ltt_trace_struct
*trace
,
688 const char *channel_name
, struct ust_channel
*ltt_chan
,
689 unsigned int subbuf_size
, unsigned int n_subbufs
, int overwrite
)
694 kref_init(<t_chan
->kref
);
696 ltt_chan
->trace
= trace
;
697 ltt_chan
->buffer_begin
= ltt_buffer_begin_callback
;
698 ltt_chan
->buffer_end
= ltt_buffer_end_callback
;
699 ltt_chan
->overwrite
= overwrite
;
700 ltt_chan
->n_subbufs_order
= get_count_order(n_subbufs
);
701 ltt_chan
->commit_count_mask
= (~0UL >> ltt_chan
->n_subbufs_order
);
702 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
704 ltt_chan_alloc_ltt_buf(ltt_chan
);
706 //ust// ltt_chan->buf = malloc(sizeof(struct ltt_channel_buf_struct));
709 /* FIXME: handle error of this call */
710 result
= ust_buffers_channel_open(ltt_chan
, subbuf_size
, n_subbufs
);
712 printk(KERN_ERR
"LTT : Can't open channel for trace %s\n",
714 goto relay_open_error
;
721 //ust// percpu_free(ltt_chan->buf);
729 * LTTng channel flush function.
731 * Must be called when no tracing is active in the channel, because of
732 * accesses across CPUs.
734 static notrace
void ltt_relay_buffer_flush(struct ust_buffer
*buf
)
738 //ust// buf->finalized = 1;
739 ltt_force_switch(buf
, FORCE_FLUSH
);
741 result
= write(buf
->data_ready_fd_write
, "1", 1);
743 PERROR("write (in ltt_relay_buffer_flush)");
744 ERR("this should never happen!");
748 static void ltt_relay_async_wakeup_chan(struct ust_channel
*ltt_channel
)
750 //ust// unsigned int i;
751 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
753 //ust// for_each_possible_cpu(i) {
754 //ust// struct ltt_channel_buf_struct *ltt_buf =
755 //ust// percpu_ptr(ltt_channel->buf, i);
757 //ust// if (atomic_read(<t_buf->wakeup_readers) == 1) {
758 //ust// atomic_set(<t_buf->wakeup_readers, 0);
759 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
764 static void ltt_relay_finish_buffer(struct ust_channel
*channel
)
769 struct ust_buffer
*buf
= channel
->buf
;
770 ltt_relay_buffer_flush(buf
);
771 //ust// ltt_relay_wake_writers(ltt_buf);
772 /* closing the pipe tells the consumer the buffer is finished */
774 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
776 // PERROR("write (in ltt_relay_finish_buffer)");
777 // ERR("this should never happen!");
779 close(buf
->data_ready_fd_write
);
784 static void ltt_relay_finish_channel(struct ust_channel
*channel
)
786 //ust// unsigned int i;
788 //ust// for_each_possible_cpu(i)
789 ltt_relay_finish_buffer(channel
);
792 static void ltt_relay_remove_channel(struct ust_channel
*channel
)
794 ust_buffers_channel_close(channel
);
795 kref_put(&channel
->kref
, ltt_relay_release_channel
);
798 struct ltt_reserve_switch_offsets
{
799 long begin
, end
, old
;
800 long begin_switch
, end_switch_current
, end_switch_old
;
801 long commit_count
, reserve_commit_diff
;
802 size_t before_hdr_pad
, size
;
808 * !0 if execution must be aborted.
810 static inline int ltt_relay_try_reserve(
811 struct ust_channel
*channel
, struct ust_buffer
*buf
,
812 struct ltt_reserve_switch_offsets
*offsets
, size_t data_size
,
813 u64
*tsc
, unsigned int *rflags
, int largest_align
)
815 offsets
->begin
= local_read(&buf
->offset
);
816 offsets
->old
= offsets
->begin
;
817 offsets
->begin_switch
= 0;
818 offsets
->end_switch_current
= 0;
819 offsets
->end_switch_old
= 0;
821 *tsc
= trace_clock_read64();
822 if (last_tsc_overflow(buf
, *tsc
))
823 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
825 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) == 0) {
826 offsets
->begin_switch
= 1; /* For offsets->begin */
828 offsets
->size
= ust_get_header_size(channel
,
829 offsets
->begin
, data_size
,
830 &offsets
->before_hdr_pad
, *rflags
);
831 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
834 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
835 > buf
->chan
->subbuf_size
) {
836 offsets
->end_switch_old
= 1; /* For offsets->old */
837 offsets
->begin_switch
= 1; /* For offsets->begin */
840 if (offsets
->begin_switch
) {
843 if (offsets
->end_switch_old
)
844 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
,
846 offsets
->begin
= offsets
->begin
+ ltt_subbuffer_header_size();
847 /* Test new buffer integrity */
848 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
849 offsets
->reserve_commit_diff
=
850 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
851 >> channel
->n_subbufs_order
)
852 - (local_read(&buf
->commit_count
[subbuf_index
])
853 & channel
->commit_count_mask
);
854 if (offsets
->reserve_commit_diff
== 0) {
857 consumed
= atomic_long_read(&buf
->consumed
);
859 /* Next buffer not corrupted. */
860 if (!channel
->overwrite
&&
861 (SUBBUF_TRUNC(offsets
->begin
, buf
->chan
)
862 - SUBBUF_TRUNC(consumed
, buf
->chan
))
863 >= channel
->alloc_size
) {
865 long consumed_idx
= SUBBUF_INDEX(consumed
, buf
->chan
);
866 long commit_count
= local_read(&buf
->commit_count
[consumed_idx
]);
867 if(((commit_count
- buf
->chan
->subbuf_size
) & channel
->commit_count_mask
) - (BUFFER_TRUNC(consumed
, buf
->chan
) >> channel
->n_subbufs_order
) != 0) {
868 WARN("Event dropped. Caused by non-committed event.");
871 WARN("Event dropped. Caused by non-consumed buffer.");
874 * We do not overwrite non consumed buffers
875 * and we are full : event is lost.
877 local_inc(&buf
->events_lost
);
881 * next buffer not corrupted, we are either in
882 * overwrite mode or the buffer is not full.
883 * It's safe to write in this new subbuffer.
888 * Next subbuffer corrupted. Force pushing reader even
889 * in normal mode. It's safe to write in this new
893 offsets
->size
= ust_get_header_size(channel
,
894 offsets
->begin
, data_size
,
895 &offsets
->before_hdr_pad
, *rflags
);
896 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
899 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
900 > buf
->chan
->subbuf_size
) {
902 * Event too big for subbuffers, report error, don't
903 * complete the sub-buffer switch.
905 local_inc(&buf
->events_lost
);
909 * We just made a successful buffer switch and the event
910 * fits in the new subbuffer. Let's write.
915 * Event fits in the current buffer and we are not on a switch
916 * boundary. It's safe to write.
919 offsets
->end
= offsets
->begin
+ offsets
->size
;
921 if ((SUBBUF_OFFSET(offsets
->end
, buf
->chan
)) == 0) {
923 * The offset_end will fall at the very beginning of the next
926 offsets
->end_switch_current
= 1; /* For offsets->begin */
934 * !0 if execution must be aborted.
936 static inline int ltt_relay_try_switch(
937 enum force_switch_mode mode
,
938 struct ust_channel
*channel
,
939 struct ust_buffer
*buf
,
940 struct ltt_reserve_switch_offsets
*offsets
,
945 offsets
->begin
= local_read(&buf
->offset
);
946 offsets
->old
= offsets
->begin
;
947 offsets
->begin_switch
= 0;
948 offsets
->end_switch_old
= 0;
950 *tsc
= trace_clock_read64();
952 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) != 0) {
953 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
, buf
->chan
);
954 offsets
->end_switch_old
= 1;
956 /* we do not have to switch : buffer is empty */
959 if (mode
== FORCE_ACTIVE
)
960 offsets
->begin
+= ltt_subbuffer_header_size();
962 * Always begin_switch in FORCE_ACTIVE mode.
963 * Test new buffer integrity
965 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
966 offsets
->reserve_commit_diff
=
967 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
968 >> channel
->n_subbufs_order
)
969 - (local_read(&buf
->commit_count
[subbuf_index
])
970 & channel
->commit_count_mask
);
971 if (offsets
->reserve_commit_diff
== 0) {
972 /* Next buffer not corrupted. */
973 if (mode
== FORCE_ACTIVE
974 && !channel
->overwrite
975 && offsets
->begin
- atomic_long_read(&buf
->consumed
)
976 >= channel
->alloc_size
) {
978 * We do not overwrite non consumed buffers and we are
979 * full : ignore switch while tracing is active.
985 * Next subbuffer corrupted. Force pushing reader even in normal
989 offsets
->end
= offsets
->begin
;
993 static inline void ltt_reserve_push_reader(
994 struct ust_channel
*channel
,
995 struct ust_buffer
*buf
,
996 struct ltt_reserve_switch_offsets
*offsets
)
998 long consumed_old
, consumed_new
;
1001 consumed_old
= atomic_long_read(&buf
->consumed
);
1003 * If buffer is in overwrite mode, push the reader consumed
1004 * count if the write position has reached it and we are not
1005 * at the first iteration (don't push the reader farther than
1006 * the writer). This operation can be done concurrently by many
1007 * writers in the same buffer, the writer being at the farthest
1008 * write position sub-buffer index in the buffer being the one
1009 * which will win this loop.
1010 * If the buffer is not in overwrite mode, pushing the reader
1011 * only happens if a sub-buffer is corrupted.
1013 if ((SUBBUF_TRUNC(offsets
->end
-1, buf
->chan
)
1014 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
1015 >= channel
->alloc_size
)
1016 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
1018 consumed_new
= consumed_old
;
1021 } while (atomic_long_cmpxchg(&buf
->consumed
, consumed_old
,
1022 consumed_new
) != consumed_old
);
1024 if (consumed_old
!= consumed_new
) {
1026 * Reader pushed : we are the winner of the push, we can
1027 * therefore reequilibrate reserve and commit. Atomic increment
1028 * of the commit count permits other writers to play around
1029 * with this variable before us. We keep track of
1030 * corrupted_subbuffers even in overwrite mode :
1031 * we never want to write over a non completely committed
1032 * sub-buffer : possible causes : the buffer size is too low
1033 * compared to the unordered data input, or there is a writer
1034 * that died between the reserve and the commit.
1036 if (offsets
->reserve_commit_diff
) {
1038 * We have to alter the sub-buffer commit count.
1039 * We do not deliver the previous subbuffer, given it
1040 * was either corrupted or not consumed (overwrite
1043 local_add(offsets
->reserve_commit_diff
,
1045 SUBBUF_INDEX(offsets
->begin
,
1047 if (!channel
->overwrite
1048 || offsets
->reserve_commit_diff
1049 != channel
->subbuf_size
) {
1051 * The reserve commit diff was not subbuf_size :
1052 * it means the subbuffer was partly written to
1053 * and is therefore corrupted. If it is multiple
1054 * of subbuffer size and we are in flight
1055 * recorder mode, we are skipping over a whole
1058 local_inc(&buf
->corrupted_subbuffers
);
1066 * ltt_reserve_switch_old_subbuf: switch old subbuffer
1068 * Concurrency safe because we are the last and only thread to alter this
1069 * sub-buffer. As long as it is not delivered and read, no other thread can
1070 * alter the offset, alter the reserve_count or call the
1071 * client_buffer_end_callback on this sub-buffer.
1073 * The only remaining threads could be the ones with pending commits. They will
1074 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1075 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1076 * corrupted sub-buffers count and push the readers across these sub-buffers.
1078 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1079 * switches in, finding out it's corrupted. The result will be than the old
1080 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1081 * will be declared corrupted too because of the commit count adjustment.
1083 * Note : offset_old should never be 0 here.
1085 static inline void ltt_reserve_switch_old_subbuf(
1086 struct ust_channel
*channel
,
1087 struct ust_buffer
*buf
,
1088 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
1090 long oldidx
= SUBBUF_INDEX(offsets
->old
- 1, channel
);
1092 channel
->buffer_end(buf
, *tsc
, offsets
->old
, oldidx
);
1093 /* Must write buffer end before incrementing commit count */
1095 offsets
->commit_count
=
1096 local_add_return(channel
->subbuf_size
1097 - (SUBBUF_OFFSET(offsets
->old
- 1, channel
)
1099 &buf
->commit_count
[oldidx
]);
1100 if ((BUFFER_TRUNC(offsets
->old
- 1, channel
)
1101 >> channel
->n_subbufs_order
)
1102 - ((offsets
->commit_count
- channel
->subbuf_size
)
1103 & channel
->commit_count_mask
) == 0)
1104 ltt_deliver(buf
, oldidx
, offsets
->commit_count
);
1108 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
1110 * This code can be executed unordered : writers may already have written to the
1111 * sub-buffer before this code gets executed, caution. The commit makes sure
1112 * that this code is executed before the deliver of this sub-buffer.
1114 static /*inline*/ void ltt_reserve_switch_new_subbuf(
1115 struct ust_channel
*channel
,
1116 struct ust_buffer
*buf
,
1117 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
1119 long beginidx
= SUBBUF_INDEX(offsets
->begin
, channel
);
1121 channel
->buffer_begin(buf
, *tsc
, beginidx
);
1122 /* Must write buffer end before incrementing commit count */
1124 offsets
->commit_count
= local_add_return(ltt_subbuffer_header_size(),
1125 &buf
->commit_count
[beginidx
]);
1126 /* Check if the written buffer has to be delivered */
1127 if ((BUFFER_TRUNC(offsets
->begin
, channel
)
1128 >> channel
->n_subbufs_order
)
1129 - ((offsets
->commit_count
- channel
->subbuf_size
)
1130 & channel
->commit_count_mask
) == 0)
1131 ltt_deliver(buf
, beginidx
, offsets
->commit_count
);
1136 * ltt_reserve_end_switch_current: finish switching current subbuffer
1138 * Concurrency safe because we are the last and only thread to alter this
1139 * sub-buffer. As long as it is not delivered and read, no other thread can
1140 * alter the offset, alter the reserve_count or call the
1141 * client_buffer_end_callback on this sub-buffer.
1143 * The only remaining threads could be the ones with pending commits. They will
1144 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1145 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1146 * corrupted sub-buffers count and push the readers across these sub-buffers.
1148 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1149 * switches in, finding out it's corrupted. The result will be than the old
1150 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1151 * will be declared corrupted too because of the commit count adjustment.
1153 static inline void ltt_reserve_end_switch_current(
1154 struct ust_channel
*channel
,
1155 struct ust_buffer
*buf
,
1156 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
1158 long endidx
= SUBBUF_INDEX(offsets
->end
- 1, channel
);
1160 channel
->buffer_end(buf
, *tsc
, offsets
->end
, endidx
);
1161 /* Must write buffer begin before incrementing commit count */
1163 offsets
->commit_count
=
1164 local_add_return(channel
->subbuf_size
1165 - (SUBBUF_OFFSET(offsets
->end
- 1, channel
)
1167 &buf
->commit_count
[endidx
]);
1168 if ((BUFFER_TRUNC(offsets
->end
- 1, channel
)
1169 >> channel
->n_subbufs_order
)
1170 - ((offsets
->commit_count
- channel
->subbuf_size
)
1171 & channel
->commit_count_mask
) == 0)
1172 ltt_deliver(buf
, endidx
, offsets
->commit_count
);
1176 * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer.
1177 * @trace: the trace structure to log to.
1178 * @ltt_channel: channel structure
1179 * @transport_data: data structure specific to ltt relay
1180 * @data_size: size of the variable length data to log.
1181 * @slot_size: pointer to total size of the slot (out)
1182 * @buf_offset : pointer to reserved buffer offset (out)
1183 * @tsc: pointer to the tsc at the slot reservation (out)
1186 * Return : -ENOSPC if not enough space, else returns 0.
1187 * It will take care of sub-buffer switching.
1189 static notrace
int ltt_relay_reserve_slot(struct ltt_trace_struct
*trace
,
1190 struct ust_channel
*channel
, void **transport_data
,
1191 size_t data_size
, size_t *slot_size
, long *buf_offset
, u64
*tsc
,
1192 unsigned int *rflags
, int largest_align
)
1194 struct ust_buffer
*buf
= *transport_data
= channel
->buf
;
1195 struct ltt_reserve_switch_offsets offsets
;
1197 offsets
.reserve_commit_diff
= 0;
1201 * Perform retryable operations.
1203 if (ltt_nesting
> 4) {
1204 local_inc(&buf
->events_lost
);
1208 if (ltt_relay_try_reserve(channel
, buf
, &offsets
, data_size
, tsc
, rflags
,
1211 } while (local_cmpxchg(&buf
->offset
, offsets
.old
,
1212 offsets
.end
) != offsets
.old
);
1215 * Atomically update last_tsc. This update races against concurrent
1216 * atomic updates, but the race will always cause supplementary full TSC
1217 * events, never the opposite (missing a full TSC event when it would be
1220 save_last_tsc(buf
, *tsc
);
1223 * Push the reader if necessary
1225 ltt_reserve_push_reader(channel
, buf
, &offsets
);
1228 * Switch old subbuffer if needed.
1230 if (offsets
.end_switch_old
)
1231 ltt_reserve_switch_old_subbuf(channel
, buf
, &offsets
, tsc
);
1234 * Populate new subbuffer.
1236 if (offsets
.begin_switch
)
1237 ltt_reserve_switch_new_subbuf(channel
, buf
, &offsets
, tsc
);
1239 if (offsets
.end_switch_current
)
1240 ltt_reserve_end_switch_current(channel
, buf
, &offsets
, tsc
);
1242 *slot_size
= offsets
.size
;
1243 *buf_offset
= offsets
.begin
+ offsets
.before_hdr_pad
;
1248 * Force a sub-buffer switch for a per-cpu buffer. This operation is
1249 * completely reentrant : can be called while tracing is active with
1250 * absolutely no lock held.
1252 * Note, however, that as a local_cmpxchg is used for some atomic
1253 * operations, this function must be called from the CPU which owns the buffer
1254 * for a ACTIVE flush.
1256 static notrace
void ltt_force_switch(struct ust_buffer
*buf
,
1257 enum force_switch_mode mode
)
1259 struct ust_channel
*channel
= buf
->chan
;
1260 struct ltt_reserve_switch_offsets offsets
;
1263 offsets
.reserve_commit_diff
= 0;
1267 * Perform retryable operations.
1270 if (ltt_relay_try_switch(mode
, channel
, buf
, &offsets
, &tsc
))
1272 } while (local_cmpxchg(&buf
->offset
, offsets
.old
,
1273 offsets
.end
) != offsets
.old
);
1276 * Atomically update last_tsc. This update races against concurrent
1277 * atomic updates, but the race will always cause supplementary full TSC
1278 * events, never the opposite (missing a full TSC event when it would be
1281 save_last_tsc(buf
, tsc
);
1284 * Push the reader if necessary
1286 if (mode
== FORCE_ACTIVE
)
1287 ltt_reserve_push_reader(channel
, buf
, &offsets
);
1290 * Switch old subbuffer if needed.
1292 if (offsets
.end_switch_old
)
1293 ltt_reserve_switch_old_subbuf(channel
, buf
, &offsets
, &tsc
);
1296 * Populate new subbuffer.
1298 if (mode
== FORCE_ACTIVE
)
1299 ltt_reserve_switch_new_subbuf(channel
, buf
, &offsets
, &tsc
);
1302 static void ltt_relay_print_user_errors(struct ltt_trace_struct
*trace
,
1303 unsigned int chan_index
, size_t data_size
,
1304 struct user_dbg_data
*dbg
)
1306 struct ust_channel
*channel
;
1307 struct ust_buffer
*buf
;
1309 channel
= &trace
->channels
[chan_index
];
1312 printk(KERN_ERR
"Error in LTT usertrace : "
1313 "buffer full : event lost in blocking "
1314 "mode. Increase LTT_RESERVE_CRITICAL.\n");
1315 printk(KERN_ERR
"LTT nesting level is %u.\n", ltt_nesting
);
1316 printk(KERN_ERR
"LTT avail size %lu.\n",
1318 printk(KERN_ERR
"avai write : %lu, read : %lu\n",
1319 dbg
->write
, dbg
->read
);
1321 dbg
->write
= local_read(&buf
->offset
);
1322 dbg
->read
= atomic_long_read(&buf
->consumed
);
1324 printk(KERN_ERR
"LTT cur size %lu.\n",
1325 dbg
->write
+ LTT_RESERVE_CRITICAL
+ data_size
1326 - SUBBUF_TRUNC(dbg
->read
, channel
));
1327 printk(KERN_ERR
"cur write : %lu, read : %lu\n",
1328 dbg
->write
, dbg
->read
);
1331 static struct ltt_transport ust_relay_transport
= {
1334 .create_channel
= ust_buffers_create_channel
,
1335 .finish_channel
= ltt_relay_finish_channel
,
1336 .remove_channel
= ltt_relay_remove_channel
,
1337 .wakeup_channel
= ltt_relay_async_wakeup_chan
,
1338 // .commit_slot = ltt_relay_commit_slot,
1339 .reserve_slot
= ltt_relay_reserve_slot
,
1340 .user_errors
= ltt_relay_print_user_errors
,
1345 * for flight recording. must be called after relay_commit.
1346 * This function decrements de subbuffer's lost_size each time the commit count
1347 * reaches back the reserve offset (module subbuffer size). It is useful for
1350 static /* inline */ void ltt_write_commit_counter(struct ust_buffer
*buf
,
1351 struct ust_buffer
*ltt_buf
,
1352 long idx
, long buf_offset
, long commit_count
, size_t data_size
)
1355 long commit_seq_old
;
1357 offset
= buf_offset
+ data_size
;
1360 * SUBBUF_OFFSET includes commit_count_mask. We can simply
1361 * compare the offsets within the subbuffer without caring about
1362 * buffer full/empty mismatch because offset is never zero here
1363 * (subbuffer header and event headers have non-zero length).
1365 if (unlikely(SUBBUF_OFFSET(offset
- commit_count
, buf
->chan
)))
1368 commit_seq_old
= local_read(<t_buf
->commit_seq
[idx
]);
1369 while (commit_seq_old
< commit_count
)
1370 commit_seq_old
= local_cmpxchg(<t_buf
->commit_seq
[idx
],
1371 commit_seq_old
, commit_count
);
1375 * Atomic unordered slot commit. Increments the commit count in the
1376 * specified sub-buffer, and delivers it if necessary.
1380 * @ltt_channel : channel structure
1381 * @transport_data: transport-specific data
1382 * @buf_offset : offset following the event header.
1383 * @data_size : size of the event data.
1384 * @slot_size : size of the reserved slot.
1386 /* FIXME: make this function static inline in the .h! */
1387 /*static*/ /* inline */ notrace
void ltt_commit_slot(
1388 struct ust_channel
*channel
,
1389 void **transport_data
, long buf_offset
,
1390 size_t data_size
, size_t slot_size
)
1392 struct ust_buffer
*buf
= *transport_data
;
1393 long offset_end
= buf_offset
;
1394 long endidx
= SUBBUF_INDEX(offset_end
- 1, channel
);
1397 /* Must write slot data before incrementing commit count */
1399 commit_count
= local_add_return(slot_size
,
1400 &buf
->commit_count
[endidx
]);
1401 /* Check if all commits have been done */
1402 if ((BUFFER_TRUNC(offset_end
- 1, channel
)
1403 >> channel
->n_subbufs_order
)
1404 - ((commit_count
- channel
->subbuf_size
)
1405 & channel
->commit_count_mask
) == 0)
1406 ltt_deliver(buf
, endidx
, commit_count
);
1408 * Update lost_size for each commit. It's needed only for extracting
1409 * ltt buffers from vmcore, after crash.
1411 ltt_write_commit_counter(buf
, buf
, endidx
,
1412 buf_offset
, commit_count
, data_size
);
1416 static char initialized
= 0;
1418 void __attribute__((constructor
)) init_ustrelay_transport(void)
1421 ltt_transport_register(&ust_relay_transport
);
1426 static void __attribute__((destructor
)) ltt_relay_exit(void)
1428 ltt_transport_unregister(&ust_relay_transport
);