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
28 #include <ust/kernelcompat.h>
29 #include <kcompat/kref.h>
34 #include "tracercore.h"
37 struct ltt_reserve_switch_offsets
{
39 long begin_switch
, end_switch_current
, end_switch_old
;
40 size_t before_hdr_pad
, size
;
44 static DEFINE_MUTEX(ust_buffers_channels_mutex
);
45 static LIST_HEAD(ust_buffers_channels
);
47 static int get_n_cpus(void)
50 static int n_cpus
= 0;
53 /* On Linux, when some processors are offline
54 * _SC_NPROCESSORS_CONF counts the offline
55 * processors, whereas _SC_NPROCESSORS_ONLN
56 * does not. If we used _SC_NPROCESSORS_ONLN,
57 * getcpu() could return a value greater than
58 * this sysconf, in which case the arrays
59 * indexed by processor would overflow.
61 result
= sysconf(_SC_NPROCESSORS_CONF
);
72 /* _ust_buffers_write()
74 * @buf: destination buffer
75 * @offset: offset in destination
77 * @len: length of source
78 * @cpy: already copied
81 void _ust_buffers_write(struct ust_buffer
*buf
, size_t offset
,
82 const void *src
, size_t len
, ssize_t cpy
)
89 WARN_ON(offset
>= buf
->buf_size
);
91 cpy
= min_t(size_t, len
, buf
->buf_size
- offset
);
92 ust_buffers_do_copy(buf
->buf_data
+ offset
, src
, cpy
);
93 } while (unlikely(len
!= cpy
));
96 static int ust_buffers_init_buffer(struct ust_trace
*trace
,
97 struct ust_channel
*ltt_chan
,
98 struct ust_buffer
*buf
,
99 unsigned int n_subbufs
);
101 static int ust_buffers_alloc_buf(struct ust_buffer
*buf
, size_t *size
)
106 *size
= PAGE_ALIGN(*size
);
108 result
= buf
->shmid
= shmget(getpid(), *size
, IPC_CREAT
| IPC_EXCL
| 0700);
109 if(result
== -1 && errno
== EINVAL
) {
110 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
113 else if(result
== -1) {
118 /* FIXME: should have matching call to shmdt */
119 ptr
= shmat(buf
->shmid
, NULL
, 0);
120 if(ptr
== (void *) -1) {
125 /* Already mark the shared memory for destruction. This will occur only
126 * when all users have detached.
128 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
135 buf
->buf_size
= *size
;
140 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
148 int ust_buffers_create_buf(struct ust_channel
*channel
, int cpu
)
151 struct ust_buffer
*buf
= channel
->buf
[cpu
];
154 result
= ust_buffers_alloc_buf(buf
, &channel
->alloc_size
);
159 kref_get(&channel
->kref
);
163 static void ust_buffers_destroy_channel(struct kref
*kref
)
165 struct ust_channel
*chan
= container_of(kref
, struct ust_channel
, kref
);
169 static void ust_buffers_destroy_buf(struct ust_buffer
*buf
)
171 struct ust_channel
*chan
= buf
->chan
;
174 result
= munmap(buf
->buf_data
, buf
->buf_size
);
179 //ust// chan->buf[buf->cpu] = NULL;
181 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
184 /* called from kref_put */
185 static void ust_buffers_remove_buf(struct kref
*kref
)
187 struct ust_buffer
*buf
= container_of(kref
, struct ust_buffer
, kref
);
188 ust_buffers_destroy_buf(buf
);
191 int ust_buffers_open_buf(struct ust_channel
*chan
, int cpu
)
195 result
= ust_buffers_create_buf(chan
, cpu
);
199 kref_init(&chan
->buf
[cpu
]->kref
);
201 result
= ust_buffers_init_buffer(chan
->trace
, chan
, chan
->buf
[cpu
], chan
->subbuf_cnt
);
207 /* FIXME: decrementally destroy on error? */
211 * ust_buffers_close_buf - close a channel buffer
214 static void ust_buffers_close_buf(struct ust_buffer
*buf
)
216 kref_put(&buf
->kref
, ust_buffers_remove_buf
);
219 int ust_buffers_channel_open(struct ust_channel
*chan
, size_t subbuf_size
, size_t subbuf_cnt
)
224 if(subbuf_size
== 0 || subbuf_cnt
== 0)
227 /* Check that the subbuffer size is larger than a page. */
228 WARN_ON_ONCE(subbuf_size
< PAGE_SIZE
);
231 * Make sure the number of subbuffers and subbuffer size are power of 2.
233 WARN_ON_ONCE(hweight32(subbuf_size
) != 1);
234 WARN_ON(hweight32(subbuf_cnt
) != 1);
236 chan
->version
= UST_CHANNEL_VERSION
;
237 chan
->subbuf_cnt
= subbuf_cnt
;
238 chan
->subbuf_size
= subbuf_size
;
239 chan
->subbuf_size_order
= get_count_order(subbuf_size
);
240 chan
->alloc_size
= subbuf_size
* subbuf_cnt
;
242 kref_init(&chan
->kref
);
244 mutex_lock(&ust_buffers_channels_mutex
);
245 for(i
=0; i
<chan
->n_cpus
; i
++) {
246 result
= ust_buffers_open_buf(chan
, i
);
250 list_add(&chan
->list
, &ust_buffers_channels
);
251 mutex_unlock(&ust_buffers_channels_mutex
);
255 /* Jump directly inside the loop to close the buffers that were already
258 ust_buffers_close_buf(chan
->buf
[i
]);
263 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
264 mutex_unlock(&ust_buffers_channels_mutex
);
268 void ust_buffers_channel_close(struct ust_channel
*chan
)
274 mutex_lock(&ust_buffers_channels_mutex
);
275 for(i
=0; i
<chan
->n_cpus
; i
++) {
276 /* FIXME: if we make it here, then all buffers were necessarily allocated. Moreover, we don't
277 * initialize to NULL so we cannot use this check. Should we? */
278 //ust// if (chan->buf[i])
279 ust_buffers_close_buf(chan
->buf
[i
]);
282 list_del(&chan
->list
);
283 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
284 mutex_unlock(&ust_buffers_channels_mutex
);
291 static void ust_buffers_destroy_buffer(struct ust_channel
*ltt_chan
, int cpu
);
293 static void ltt_force_switch(struct ust_buffer
*buf
,
294 enum force_switch_mode mode
);
299 static void ltt_buffer_begin(struct ust_buffer
*buf
,
300 u64 tsc
, unsigned int subbuf_idx
)
302 struct ust_channel
*channel
= buf
->chan
;
303 struct ltt_subbuffer_header
*header
=
304 (struct ltt_subbuffer_header
*)
305 ust_buffers_offset_address(buf
,
306 subbuf_idx
* buf
->chan
->subbuf_size
);
308 header
->cycle_count_begin
= tsc
;
309 header
->data_size
= 0xFFFFFFFF; /* for recognizing crashed buffers */
310 header
->sb_size
= 0xFFFFFFFF; /* for recognizing crashed buffers */
311 /* FIXME: add memory barrier? */
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(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 ust_buffers_offset_address(buf
,
325 subbuf_idx
* buf
->chan
->subbuf_size
);
326 u32 data_size
= SUBBUF_OFFSET(offset
- 1, buf
->chan
) + 1;
328 header
->data_size
= data_size
;
329 header
->sb_size
= PAGE_ALIGN(data_size
);
330 header
->cycle_count_end
= tsc
;
331 header
->events_lost
= uatomic_read(&buf
->events_lost
);
332 header
->subbuf_corrupt
= uatomic_read(&buf
->corrupted_subbuffers
);
333 if(unlikely(header
->events_lost
> 0)) {
334 DBG("Some events (%d) were lost in %s_%d", header
->events_lost
, buf
->chan
->channel_name
, buf
->cpu
);
339 * This function should not be called from NMI interrupt context
341 static notrace
void ltt_buf_unfull(struct ust_buffer
*buf
,
342 unsigned int subbuf_idx
,
348 * Promote compiler barrier to a smp_mb().
349 * For the specific LTTng case, this IPI call should be removed if the
350 * architecture does not reorder writes. This should eventually be provided by
351 * a separate architecture-specific infrastructure.
353 //ust// static void remote_mb(void *info)
358 int ust_buffers_get_subbuf(struct ust_buffer
*buf
, long *consumed
)
360 struct ust_channel
*channel
= buf
->chan
;
361 long consumed_old
, consumed_idx
, commit_count
, write_offset
;
364 consumed_old
= uatomic_read(&buf
->consumed
);
365 consumed_idx
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
366 commit_count
= uatomic_read(&buf
->commit_count
[consumed_idx
].cc_sb
);
368 * Make sure we read the commit count before reading the buffer
369 * data and the write offset. Correct consumed offset ordering
370 * wrt commit count is insured by the use of cmpxchg to update
371 * the consumed offset.
372 * smp_call_function_single can fail if the remote CPU is offline,
373 * this is OK because then there is no wmb to execute there.
374 * If our thread is executing on the same CPU as the on the buffers
375 * belongs to, we don't have to synchronize it at all. If we are
376 * migrated, the scheduler will take care of the memory barriers.
377 * Normally, smp_call_function_single() should ensure program order when
378 * executing the remote function, which implies that it surrounds the
379 * function execution with :
390 * However, smp_call_function_single() does not seem to clearly execute
391 * such barriers. It depends on spinlock semantic to provide the barrier
392 * before executing the IPI and, when busy-looping, csd_lock_wait only
393 * executes smp_mb() when it has to wait for the other CPU.
395 * I don't trust this code. Therefore, let's add the smp_mb() sequence
396 * required ourself, even if duplicated. It has no performance impact
399 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
400 * read and write vs write. They do not ensure core synchronization. We
401 * really have to ensure total order between the 3 barriers running on
404 //ust// #ifdef LTT_NO_IPI_BARRIER
406 * Local rmb to match the remote wmb to read the commit count before the
407 * buffer data and the write offset.
411 //ust// if (raw_smp_processor_id() != buf->cpu) {
412 //ust// smp_mb(); /* Total order with IPI handler smp_mb() */
413 //ust// smp_call_function_single(buf->cpu, remote_mb, NULL, 1);
414 //ust// smp_mb(); /* Total order with IPI handler smp_mb() */
418 write_offset
= uatomic_read(&buf
->offset
);
420 * Check that the subbuffer we are trying to consume has been
421 * already fully committed.
423 if (((commit_count
- buf
->chan
->subbuf_size
)
424 & channel
->commit_count_mask
)
425 - (BUFFER_TRUNC(consumed_old
, buf
->chan
)
426 >> channel
->n_subbufs_order
)
431 * Check that we are not about to read the same subbuffer in
432 * which the writer head is.
434 if ((SUBBUF_TRUNC(write_offset
, buf
->chan
)
435 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
440 /* FIXME: is this ok to disable the reading feature? */
441 //ust// retval = update_read_sb_index(buf, consumed_idx);
443 //ust// return retval;
445 *consumed
= consumed_old
;
450 int ust_buffers_put_subbuf(struct ust_buffer
*buf
, unsigned long uconsumed_old
)
452 long consumed_new
, consumed_old
;
454 consumed_old
= uatomic_read(&buf
->consumed
);
455 consumed_old
= consumed_old
& (~0xFFFFFFFFL
);
456 consumed_old
= consumed_old
| uconsumed_old
;
457 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
459 //ust// spin_lock(<t_buf->full_lock);
460 if (uatomic_cmpxchg(&buf
->consumed
, consumed_old
,
463 /* We have been pushed by the writer : the last
464 * buffer read _is_ corrupted! It can also
465 * happen if this is a buffer we never got. */
466 //ust// spin_unlock(<t_buf->full_lock);
469 /* tell the client that buffer is now unfull */
472 index
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
473 data
= BUFFER_OFFSET(consumed_old
, buf
->chan
);
474 ltt_buf_unfull(buf
, index
, data
);
475 //ust// spin_unlock(<t_buf->full_lock);
480 static void ltt_relay_print_subbuffer_errors(
481 struct ust_channel
*channel
,
482 long cons_off
, int cpu
)
484 struct ust_buffer
*ltt_buf
= channel
->buf
[cpu
];
485 long cons_idx
, commit_count
, commit_count_sb
, write_offset
;
487 cons_idx
= SUBBUF_INDEX(cons_off
, channel
);
488 commit_count
= uatomic_read(<t_buf
->commit_count
[cons_idx
].cc
);
489 commit_count_sb
= uatomic_read(<t_buf
->commit_count
[cons_idx
].cc_sb
);
492 * No need to order commit_count and write_offset reads because we
493 * execute after trace is stopped when there are no readers left.
495 write_offset
= uatomic_read(<t_buf
->offset
);
496 WARN( "LTT : unread channel %s offset is %ld "
497 "and cons_off : %ld (cpu %d)\n",
498 channel
->channel_name
, write_offset
, cons_off
, cpu
);
499 /* Check each sub-buffer for non filled commit count */
500 if (((commit_count
- channel
->subbuf_size
) & channel
->commit_count_mask
)
501 - (BUFFER_TRUNC(cons_off
, channel
) >> channel
->n_subbufs_order
) != 0) {
502 ERR("LTT : %s : subbuffer %lu has non filled "
503 "commit count [cc, cc_sb] [%lu,%lu].\n",
504 channel
->channel_name
, cons_idx
, commit_count
, commit_count_sb
);
506 ERR("LTT : %s : commit count : %lu, subbuf size %zd\n",
507 channel
->channel_name
, commit_count
,
508 channel
->subbuf_size
);
511 static void ltt_relay_print_errors(struct ust_trace
*trace
,
512 struct ust_channel
*channel
, int cpu
)
514 struct ust_buffer
*ltt_buf
= channel
->buf
[cpu
];
518 * Can be called in the error path of allocation when
519 * trans_channel_data is not yet set.
524 //ust// for (cons_off = 0; cons_off < rchan->alloc_size;
525 //ust// cons_off = SUBBUF_ALIGN(cons_off, rchan))
526 //ust// ust_buffers_print_written(ltt_chan, cons_off, cpu);
527 for (cons_off
= uatomic_read(<t_buf
->consumed
);
528 (SUBBUF_TRUNC(uatomic_read(<t_buf
->offset
),
531 cons_off
= SUBBUF_ALIGN(cons_off
, channel
))
532 ltt_relay_print_subbuffer_errors(channel
, cons_off
, cpu
);
535 static void ltt_relay_print_buffer_errors(struct ust_channel
*channel
, int cpu
)
537 struct ust_trace
*trace
= channel
->trace
;
538 struct ust_buffer
*ltt_buf
= channel
->buf
[cpu
];
540 if (uatomic_read(<t_buf
->events_lost
))
541 ERR("channel %s: %ld events lost (cpu %d)",
542 channel
->channel_name
,
543 uatomic_read(<t_buf
->events_lost
), cpu
);
544 if (uatomic_read(<t_buf
->corrupted_subbuffers
))
545 ERR("channel %s : %ld corrupted subbuffers (cpu %d)",
546 channel
->channel_name
,
547 uatomic_read(<t_buf
->corrupted_subbuffers
), cpu
);
549 ltt_relay_print_errors(trace
, channel
, cpu
);
552 static void ltt_relay_release_channel(struct kref
*kref
)
554 struct ust_channel
*ltt_chan
= container_of(kref
,
555 struct ust_channel
, kref
);
562 //ust// static int ltt_relay_create_buffer(struct ust_trace *trace,
563 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
564 //ust// unsigned int cpu, unsigned int n_subbufs)
566 //ust// struct ltt_channel_buf_struct *ltt_buf =
567 //ust// percpu_ptr(ltt_chan->buf, cpu);
568 //ust// unsigned int j;
570 //ust// ltt_buf->commit_count =
571 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
572 //ust// GFP_KERNEL, cpu_to_node(cpu));
573 //ust// if (!ltt_buf->commit_count)
574 //ust// return -ENOMEM;
575 //ust// kref_get(&trace->kref);
576 //ust// kref_get(&trace->ltt_transport_kref);
577 //ust// kref_get(<t_chan->kref);
578 //ust// uatomic_set(<t_buf->offset, ltt_subbuffer_header_size());
579 //ust// uatomic_set(<t_buf->consumed, 0);
580 //ust// uatomic_set(<t_buf->active_readers, 0);
581 //ust// for (j = 0; j < n_subbufs; j++)
582 //ust// uatomic_set(<t_buf->commit_count[j], 0);
583 //ust// init_waitqueue_head(<t_buf->write_wait);
584 //ust// uatomic_set(<t_buf->wakeup_readers, 0);
585 //ust// spin_lock_init(<t_buf->full_lock);
587 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
588 //ust// /* atomic_add made on local variable on data that belongs to
589 //ust// * various CPUs : ok because tracing not started (for this cpu). */
590 //ust// uatomic_add(<t_buf->commit_count[0], ltt_subbuffer_header_size());
592 //ust// uatomic_set(<t_buf->events_lost, 0);
593 //ust// uatomic_set(<t_buf->corrupted_subbuffers, 0);
598 static int ust_buffers_init_buffer(struct ust_trace
*trace
,
599 struct ust_channel
*ltt_chan
, struct ust_buffer
*buf
,
600 unsigned int n_subbufs
)
607 zmalloc(sizeof(*buf
->commit_count
) * n_subbufs
);
608 if (!buf
->commit_count
)
610 kref_get(&trace
->kref
);
611 kref_get(&trace
->ltt_transport_kref
);
612 kref_get(<t_chan
->kref
);
613 uatomic_set(&buf
->offset
, ltt_subbuffer_header_size());
614 uatomic_set(&buf
->consumed
, 0);
615 uatomic_set(&buf
->active_readers
, 0);
616 for (j
= 0; j
< n_subbufs
; j
++) {
617 uatomic_set(&buf
->commit_count
[j
].cc
, 0);
618 uatomic_set(&buf
->commit_count
[j
].cc_sb
, 0);
620 //ust// init_waitqueue_head(&buf->write_wait);
621 //ust// uatomic_set(&buf->wakeup_readers, 0);
622 //ust// spin_lock_init(&buf->full_lock);
624 ltt_buffer_begin(buf
, trace
->start_tsc
, 0);
626 uatomic_add(&buf
->commit_count
[0].cc
, ltt_subbuffer_header_size());
628 uatomic_set(&buf
->events_lost
, 0);
629 uatomic_set(&buf
->corrupted_subbuffers
, 0);
636 buf
->data_ready_fd_read
= fds
[0];
637 buf
->data_ready_fd_write
= fds
[1];
639 /* FIXME: do we actually need this? */
640 result
= fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
645 //ust// buf->commit_seq = malloc(sizeof(buf->commit_seq) * n_subbufs);
646 //ust// if(!ltt_buf->commit_seq) {
649 memset(buf
->commit_seq
, 0, sizeof(buf
->commit_seq
[0]) * n_subbufs
);
651 /* FIXME: decrementally destroy on error */
656 /* FIXME: use this function */
657 static void ust_buffers_destroy_buffer(struct ust_channel
*ltt_chan
, int cpu
)
659 struct ust_trace
*trace
= ltt_chan
->trace
;
660 struct ust_buffer
*ltt_buf
= ltt_chan
->buf
[cpu
];
662 kref_put(<t_chan
->trace
->ltt_transport_kref
,
663 ltt_release_transport
);
664 ltt_relay_print_buffer_errors(ltt_chan
, cpu
);
665 //ust// free(ltt_buf->commit_seq);
666 free(ltt_buf
->commit_count
);
667 ltt_buf
->commit_count
= NULL
;
668 kref_put(<t_chan
->kref
, ltt_relay_release_channel
);
669 kref_put(&trace
->kref
, ltt_release_trace
);
670 //ust// wake_up_interruptible(&trace->kref_wq);
673 static int ust_buffers_alloc_channel_buf_structs(struct ust_channel
*chan
)
680 size
= PAGE_ALIGN(1);
682 for(i
=0; i
<chan
->n_cpus
; i
++) {
684 result
= chan
->buf_struct_shmids
[i
] = shmget(getpid(), size
, IPC_CREAT
| IPC_EXCL
| 0700);
687 goto destroy_previous
;
690 /* FIXME: should have matching call to shmdt */
691 ptr
= shmat(chan
->buf_struct_shmids
[i
], NULL
, 0);
692 if(ptr
== (void *) -1) {
697 /* Already mark the shared memory for destruction. This will occur only
698 * when all users have detached.
700 result
= shmctl(chan
->buf_struct_shmids
[i
], IPC_RMID
, NULL
);
703 goto destroy_previous
;
711 /* Jumping inside this loop occurs from within the other loop above with i as
712 * counter, so it unallocates the structures for the cpu = current_i down to
716 result
= shmctl(chan
->buf_struct_shmids
[i
], IPC_RMID
, NULL
);
731 static int ust_buffers_create_channel(const char *trace_name
, struct ust_trace
*trace
,
732 const char *channel_name
, struct ust_channel
*ltt_chan
,
733 unsigned int subbuf_size
, unsigned int n_subbufs
, int overwrite
)
737 kref_init(<t_chan
->kref
);
739 ltt_chan
->trace
= trace
;
740 ltt_chan
->overwrite
= overwrite
;
741 ltt_chan
->n_subbufs_order
= get_count_order(n_subbufs
);
742 ltt_chan
->commit_count_mask
= (~0UL >> ltt_chan
->n_subbufs_order
);
743 ltt_chan
->n_cpus
= get_n_cpus();
744 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
745 ltt_chan
->buf
= (void *) malloc(ltt_chan
->n_cpus
* sizeof(void *));
746 if(ltt_chan
->buf
== NULL
) {
749 ltt_chan
->buf_struct_shmids
= (int *) malloc(ltt_chan
->n_cpus
* sizeof(int));
750 if(ltt_chan
->buf_struct_shmids
== NULL
)
753 result
= ust_buffers_alloc_channel_buf_structs(ltt_chan
);
755 goto free_buf_struct_shmids
;
758 result
= ust_buffers_channel_open(ltt_chan
, subbuf_size
, n_subbufs
);
760 ERR("Cannot open channel for trace %s", trace_name
);
761 goto unalloc_buf_structs
;
767 /* FIXME: put a call here to unalloc the buf structs! */
769 free_buf_struct_shmids
:
770 free(ltt_chan
->buf_struct_shmids
);
780 * LTTng channel flush function.
782 * Must be called when no tracing is active in the channel, because of
783 * accesses across CPUs.
785 static notrace
void ltt_relay_buffer_flush(struct ust_buffer
*buf
)
789 //ust// buf->finalized = 1;
790 ltt_force_switch(buf
, FORCE_FLUSH
);
792 result
= write(buf
->data_ready_fd_write
, "1", 1);
794 PERROR("write (in ltt_relay_buffer_flush)");
795 ERR("this should never happen!");
799 static void ltt_relay_async_wakeup_chan(struct ust_channel
*ltt_channel
)
801 //ust// unsigned int i;
802 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
804 //ust// for_each_possible_cpu(i) {
805 //ust// struct ltt_channel_buf_struct *ltt_buf =
806 //ust// percpu_ptr(ltt_channel->buf, i);
808 //ust// if (uatomic_read(<t_buf->wakeup_readers) == 1) {
809 //ust// uatomic_set(<t_buf->wakeup_readers, 0);
810 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
815 static void ltt_relay_finish_buffer(struct ust_channel
*channel
, unsigned int cpu
)
819 if (channel
->buf
[cpu
]) {
820 struct ust_buffer
*buf
= channel
->buf
[cpu
];
821 ltt_relay_buffer_flush(buf
);
822 //ust// ltt_relay_wake_writers(ltt_buf);
823 /* closing the pipe tells the consumer the buffer is finished */
825 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
827 // PERROR("write (in ltt_relay_finish_buffer)");
828 // ERR("this should never happen!");
830 close(buf
->data_ready_fd_write
);
835 static void ltt_relay_finish_channel(struct ust_channel
*channel
)
839 for(i
=0; i
<channel
->n_cpus
; i
++) {
840 ltt_relay_finish_buffer(channel
, i
);
844 static void ltt_relay_remove_channel(struct ust_channel
*channel
)
846 ust_buffers_channel_close(channel
);
847 kref_put(&channel
->kref
, ltt_relay_release_channel
);
851 * ltt_reserve_switch_old_subbuf: switch old subbuffer
853 * Concurrency safe because we are the last and only thread to alter this
854 * sub-buffer. As long as it is not delivered and read, no other thread can
855 * alter the offset, alter the reserve_count or call the
856 * client_buffer_end_callback on this sub-buffer.
858 * The only remaining threads could be the ones with pending commits. They will
859 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
860 * We detect corrupted subbuffers with commit and reserve counts. We keep a
861 * corrupted sub-buffers count and push the readers across these sub-buffers.
863 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
864 * switches in, finding out it's corrupted. The result will be than the old
865 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
866 * will be declared corrupted too because of the commit count adjustment.
868 * Note : offset_old should never be 0 here.
870 static void ltt_reserve_switch_old_subbuf(
871 struct ust_channel
*chan
, struct ust_buffer
*buf
,
872 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
874 long oldidx
= SUBBUF_INDEX(offsets
->old
- 1, chan
);
875 long commit_count
, padding_size
;
877 padding_size
= chan
->subbuf_size
878 - (SUBBUF_OFFSET(offsets
->old
- 1, chan
) + 1);
879 ltt_buffer_end(buf
, *tsc
, offsets
->old
, oldidx
);
882 * Must write slot data before incrementing commit count.
883 * This compiler barrier is upgraded into a smp_wmb() by the IPI
884 * sent by get_subbuf() when it does its smp_rmb().
887 uatomic_add(&buf
->commit_count
[oldidx
].cc
, padding_size
);
888 commit_count
= uatomic_read(&buf
->commit_count
[oldidx
].cc
);
889 ltt_check_deliver(chan
, buf
, offsets
->old
- 1, commit_count
, oldidx
);
890 ltt_write_commit_counter(chan
, buf
, oldidx
,
891 offsets
->old
, commit_count
, padding_size
);
895 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
897 * This code can be executed unordered : writers may already have written to the
898 * sub-buffer before this code gets executed, caution. The commit makes sure
899 * that this code is executed before the deliver of this sub-buffer.
901 static void ltt_reserve_switch_new_subbuf(
902 struct ust_channel
*chan
, struct ust_buffer
*buf
,
903 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
905 long beginidx
= SUBBUF_INDEX(offsets
->begin
, chan
);
908 ltt_buffer_begin(buf
, *tsc
, beginidx
);
911 * Must write slot data before incrementing commit count.
912 * This compiler barrier is upgraded into a smp_wmb() by the IPI
913 * sent by get_subbuf() when it does its smp_rmb().
916 uatomic_add(&buf
->commit_count
[beginidx
].cc
, ltt_subbuffer_header_size());
917 commit_count
= uatomic_read(&buf
->commit_count
[beginidx
].cc
);
918 /* Check if the written buffer has to be delivered */
919 ltt_check_deliver(chan
, buf
, offsets
->begin
, commit_count
, beginidx
);
920 ltt_write_commit_counter(chan
, buf
, beginidx
,
921 offsets
->begin
, commit_count
, ltt_subbuffer_header_size());
925 * ltt_reserve_end_switch_current: finish switching current subbuffer
927 * Concurrency safe because we are the last and only thread to alter this
928 * sub-buffer. As long as it is not delivered and read, no other thread can
929 * alter the offset, alter the reserve_count or call the
930 * client_buffer_end_callback on this sub-buffer.
932 * The only remaining threads could be the ones with pending commits. They will
933 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
934 * We detect corrupted subbuffers with commit and reserve counts. We keep a
935 * corrupted sub-buffers count and push the readers across these sub-buffers.
937 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
938 * switches in, finding out it's corrupted. The result will be than the old
939 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
940 * will be declared corrupted too because of the commit count adjustment.
942 static void ltt_reserve_end_switch_current(
943 struct ust_channel
*chan
,
944 struct ust_buffer
*buf
,
945 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
947 long endidx
= SUBBUF_INDEX(offsets
->end
- 1, chan
);
948 long commit_count
, padding_size
;
950 padding_size
= chan
->subbuf_size
951 - (SUBBUF_OFFSET(offsets
->end
- 1, chan
) + 1);
953 ltt_buffer_end(buf
, *tsc
, offsets
->end
, endidx
);
956 * Must write slot data before incrementing commit count.
957 * This compiler barrier is upgraded into a smp_wmb() by the IPI
958 * sent by get_subbuf() when it does its smp_rmb().
961 uatomic_add(&buf
->commit_count
[endidx
].cc
, padding_size
);
962 commit_count
= uatomic_read(&buf
->commit_count
[endidx
].cc
);
963 ltt_check_deliver(chan
, buf
,
964 offsets
->end
- 1, commit_count
, endidx
);
965 ltt_write_commit_counter(chan
, buf
, endidx
,
966 offsets
->end
, commit_count
, padding_size
);
972 * !0 if execution must be aborted.
974 static int ltt_relay_try_switch_slow(
975 enum force_switch_mode mode
,
976 struct ust_channel
*chan
,
977 struct ust_buffer
*buf
,
978 struct ltt_reserve_switch_offsets
*offsets
,
982 long reserve_commit_diff
;
984 offsets
->begin
= uatomic_read(&buf
->offset
);
985 offsets
->old
= offsets
->begin
;
986 offsets
->begin_switch
= 0;
987 offsets
->end_switch_old
= 0;
989 *tsc
= trace_clock_read64();
991 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) != 0) {
992 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
, buf
->chan
);
993 offsets
->end_switch_old
= 1;
995 /* we do not have to switch : buffer is empty */
998 if (mode
== FORCE_ACTIVE
)
999 offsets
->begin
+= ltt_subbuffer_header_size();
1001 * Always begin_switch in FORCE_ACTIVE mode.
1002 * Test new buffer integrity
1004 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1005 reserve_commit_diff
=
1006 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1007 >> chan
->n_subbufs_order
)
1008 - (uatomic_read(&buf
->commit_count
[subbuf_index
].cc_sb
)
1009 & chan
->commit_count_mask
);
1010 if (reserve_commit_diff
== 0) {
1011 /* Next buffer not corrupted. */
1012 if (mode
== FORCE_ACTIVE
1014 && offsets
->begin
- uatomic_read(&buf
->consumed
)
1015 >= chan
->alloc_size
) {
1017 * We do not overwrite non consumed buffers and we are
1018 * full : ignore switch while tracing is active.
1024 * Next subbuffer corrupted. Force pushing reader even in normal
1028 offsets
->end
= offsets
->begin
;
1033 * Force a sub-buffer switch for a per-cpu buffer. This operation is
1034 * completely reentrant : can be called while tracing is active with
1035 * absolutely no lock held.
1037 void ltt_force_switch_lockless_slow(struct ust_buffer
*buf
,
1038 enum force_switch_mode mode
)
1040 struct ust_channel
*chan
= buf
->chan
;
1041 struct ltt_reserve_switch_offsets offsets
;
1046 DBG("Switching (forced) %s_%d", chan
->channel_name
, buf
->cpu
);
1048 * Perform retryable operations.
1051 if (ltt_relay_try_switch_slow(mode
, chan
, buf
,
1054 } while (uatomic_cmpxchg(&buf
->offset
, offsets
.old
,
1055 offsets
.end
) != offsets
.old
);
1058 * Atomically update last_tsc. This update races against concurrent
1059 * atomic updates, but the race will always cause supplementary full TSC
1060 * events, never the opposite (missing a full TSC event when it would be
1063 save_last_tsc(buf
, tsc
);
1066 * Push the reader if necessary
1068 if (mode
== FORCE_ACTIVE
) {
1069 ltt_reserve_push_reader(chan
, buf
, offsets
.end
- 1);
1070 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan));
1074 * Switch old subbuffer if needed.
1076 if (offsets
.end_switch_old
) {
1077 //ust// ltt_clear_noref_flag(rchan, buf, SUBBUF_INDEX(offsets.old - 1, rchan));
1078 ltt_reserve_switch_old_subbuf(chan
, buf
, &offsets
, &tsc
);
1082 * Populate new subbuffer.
1084 if (mode
== FORCE_ACTIVE
)
1085 ltt_reserve_switch_new_subbuf(chan
, buf
, &offsets
, &tsc
);
1091 * !0 if execution must be aborted.
1093 static int ltt_relay_try_reserve_slow(struct ust_channel
*chan
, struct ust_buffer
*buf
,
1094 struct ltt_reserve_switch_offsets
*offsets
, size_t data_size
,
1095 u64
*tsc
, unsigned int *rflags
, int largest_align
)
1097 long reserve_commit_diff
;
1099 offsets
->begin
= uatomic_read(&buf
->offset
);
1100 offsets
->old
= offsets
->begin
;
1101 offsets
->begin_switch
= 0;
1102 offsets
->end_switch_current
= 0;
1103 offsets
->end_switch_old
= 0;
1105 *tsc
= trace_clock_read64();
1106 if (last_tsc_overflow(buf
, *tsc
))
1107 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
1109 if (unlikely(SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) == 0)) {
1110 offsets
->begin_switch
= 1; /* For offsets->begin */
1112 offsets
->size
= ust_get_header_size(chan
,
1113 offsets
->begin
, data_size
,
1114 &offsets
->before_hdr_pad
, *rflags
);
1115 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1118 if (unlikely((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) +
1119 offsets
->size
) > buf
->chan
->subbuf_size
)) {
1120 offsets
->end_switch_old
= 1; /* For offsets->old */
1121 offsets
->begin_switch
= 1; /* For offsets->begin */
1124 if (unlikely(offsets
->begin_switch
)) {
1128 * We are typically not filling the previous buffer completely.
1130 if (likely(offsets
->end_switch_old
))
1131 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
,
1133 offsets
->begin
= offsets
->begin
+ ltt_subbuffer_header_size();
1134 /* Test new buffer integrity */
1135 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1136 reserve_commit_diff
=
1137 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1138 >> chan
->n_subbufs_order
)
1139 - (uatomic_read(&buf
->commit_count
[subbuf_index
].cc_sb
)
1140 & chan
->commit_count_mask
);
1141 if (likely(reserve_commit_diff
== 0)) {
1142 /* Next buffer not corrupted. */
1143 if (unlikely(!chan
->overwrite
&&
1144 (SUBBUF_TRUNC(offsets
->begin
, buf
->chan
)
1145 - SUBBUF_TRUNC(uatomic_read(
1148 >= chan
->alloc_size
)) {
1150 * We do not overwrite non consumed buffers
1151 * and we are full : event is lost.
1153 uatomic_inc(&buf
->events_lost
);
1157 * next buffer not corrupted, we are either in
1158 * overwrite mode or the buffer is not full.
1159 * It's safe to write in this new subbuffer.
1164 * Next subbuffer corrupted. Drop event in normal and
1165 * overwrite mode. Caused by either a writer OOPS or
1166 * too many nested writes over a reserve/commit pair.
1168 uatomic_inc(&buf
->events_lost
);
1171 offsets
->size
= ust_get_header_size(chan
,
1172 offsets
->begin
, data_size
,
1173 &offsets
->before_hdr_pad
, *rflags
);
1174 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1177 if (unlikely((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
)
1178 + offsets
->size
) > buf
->chan
->subbuf_size
)) {
1180 * Event too big for subbuffers, report error, don't
1181 * complete the sub-buffer switch.
1183 uatomic_inc(&buf
->events_lost
);
1187 * We just made a successful buffer switch and the event
1188 * fits in the new subbuffer. Let's write.
1193 * Event fits in the current buffer and we are not on a switch
1194 * boundary. It's safe to write.
1197 offsets
->end
= offsets
->begin
+ offsets
->size
;
1199 if (unlikely((SUBBUF_OFFSET(offsets
->end
, buf
->chan
)) == 0)) {
1201 * The offset_end will fall at the very beginning of the next
1204 offsets
->end_switch_current
= 1; /* For offsets->begin */
1210 * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer.
1211 * @trace: the trace structure to log to.
1212 * @ltt_channel: channel structure
1213 * @transport_data: data structure specific to ltt relay
1214 * @data_size: size of the variable length data to log.
1215 * @slot_size: pointer to total size of the slot (out)
1216 * @buf_offset : pointer to reserved buffer offset (out)
1217 * @tsc: pointer to the tsc at the slot reservation (out)
1220 * Return : -ENOSPC if not enough space, else returns 0.
1221 * It will take care of sub-buffer switching.
1223 int ltt_reserve_slot_lockless_slow(struct ust_trace
*trace
,
1224 struct ust_channel
*chan
, void **transport_data
,
1225 size_t data_size
, size_t *slot_size
, long *buf_offset
, u64
*tsc
,
1226 unsigned int *rflags
, int largest_align
, int cpu
)
1228 struct ust_buffer
*buf
= chan
->buf
[cpu
];
1229 struct ltt_reserve_switch_offsets offsets
;
1234 if (unlikely(ltt_relay_try_reserve_slow(chan
, buf
, &offsets
,
1235 data_size
, tsc
, rflags
, largest_align
)))
1237 } while (unlikely(uatomic_cmpxchg(&buf
->offset
, offsets
.old
,
1238 offsets
.end
) != offsets
.old
));
1241 * Atomically update last_tsc. This update races against concurrent
1242 * atomic updates, but the race will always cause supplementary full TSC
1243 * events, never the opposite (missing a full TSC event when it would be
1246 save_last_tsc(buf
, *tsc
);
1249 * Push the reader if necessary
1251 ltt_reserve_push_reader(chan
, buf
, offsets
.end
- 1);
1254 * Clear noref flag for this subbuffer.
1256 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan));
1259 * Switch old subbuffer if needed.
1261 if (unlikely(offsets
.end_switch_old
)) {
1262 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.old - 1, chan));
1263 ltt_reserve_switch_old_subbuf(chan
, buf
, &offsets
, tsc
);
1264 DBG("Switching %s_%d", chan
->channel_name
, cpu
);
1268 * Populate new subbuffer.
1270 if (unlikely(offsets
.begin_switch
))
1271 ltt_reserve_switch_new_subbuf(chan
, buf
, &offsets
, tsc
);
1273 if (unlikely(offsets
.end_switch_current
))
1274 ltt_reserve_end_switch_current(chan
, buf
, &offsets
, tsc
);
1276 *slot_size
= offsets
.size
;
1277 *buf_offset
= offsets
.begin
+ offsets
.before_hdr_pad
;
1281 static struct ltt_transport ust_relay_transport
= {
1284 .create_channel
= ust_buffers_create_channel
,
1285 .finish_channel
= ltt_relay_finish_channel
,
1286 .remove_channel
= ltt_relay_remove_channel
,
1287 .wakeup_channel
= ltt_relay_async_wakeup_chan
,
1291 static char initialized
= 0;
1293 void __attribute__((constructor
)) init_ustrelay_transport(void)
1296 ltt_transport_register(&ust_relay_transport
);
1301 static void __attribute__((destructor
)) ust_buffers_exit(void)
1303 ltt_transport_unregister(&ust_relay_transport
);
1306 size_t ltt_write_event_header_slow(struct ust_trace
*trace
,
1307 struct ust_channel
*channel
,
1308 struct ust_buffer
*buf
, long buf_offset
,
1309 u16 eID
, u32 event_size
,
1310 u64 tsc
, unsigned int rflags
)
1312 struct ltt_event_header header
;
1316 case LTT_RFLAG_ID_SIZE_TSC
:
1317 header
.id_time
= 29 << LTT_TSC_BITS
;
1319 case LTT_RFLAG_ID_SIZE
:
1320 header
.id_time
= 30 << LTT_TSC_BITS
;
1323 header
.id_time
= 31 << LTT_TSC_BITS
;
1327 header
.id_time
|= (u32
)tsc
& LTT_TSC_MASK
;
1328 ust_buffers_write(buf
, buf_offset
, &header
, sizeof(header
));
1329 buf_offset
+= sizeof(header
);
1332 case LTT_RFLAG_ID_SIZE_TSC
:
1333 small_size
= (u16
)min_t(u32
, event_size
, LTT_MAX_SMALL_SIZE
);
1334 ust_buffers_write(buf
, buf_offset
,
1336 buf_offset
+= sizeof(u16
);
1337 ust_buffers_write(buf
, buf_offset
,
1338 &small_size
, sizeof(u16
));
1339 buf_offset
+= sizeof(u16
);
1340 if (small_size
== LTT_MAX_SMALL_SIZE
) {
1341 ust_buffers_write(buf
, buf_offset
,
1342 &event_size
, sizeof(u32
));
1343 buf_offset
+= sizeof(u32
);
1345 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
1346 ust_buffers_write(buf
, buf_offset
,
1348 buf_offset
+= sizeof(u64
);
1350 case LTT_RFLAG_ID_SIZE
:
1351 small_size
= (u16
)min_t(u32
, event_size
, LTT_MAX_SMALL_SIZE
);
1352 ust_buffers_write(buf
, buf_offset
,
1354 buf_offset
+= sizeof(u16
);
1355 ust_buffers_write(buf
, buf_offset
,
1356 &small_size
, sizeof(u16
));
1357 buf_offset
+= sizeof(u16
);
1358 if (small_size
== LTT_MAX_SMALL_SIZE
) {
1359 ust_buffers_write(buf
, buf_offset
,
1360 &event_size
, sizeof(u32
));
1361 buf_offset
+= sizeof(u32
);
1365 ust_buffers_write(buf
, buf_offset
,
1367 buf_offset
+= sizeof(u16
);