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
30 #include <ust/clock.h>
35 #include "tracercore.h"
38 struct ltt_reserve_switch_offsets
{
40 long begin_switch
, end_switch_current
, end_switch_old
;
41 size_t before_hdr_pad
, size
;
45 static DEFINE_MUTEX(ust_buffers_channels_mutex
);
46 static CDS_LIST_HEAD(ust_buffers_channels
);
48 static int get_n_cpus(void)
51 static int n_cpus
= 0;
54 /* On Linux, when some processors are offline
55 * _SC_NPROCESSORS_CONF counts the offline
56 * processors, whereas _SC_NPROCESSORS_ONLN
57 * does not. If we used _SC_NPROCESSORS_ONLN,
58 * getcpu() could return a value greater than
59 * this sysconf, in which case the arrays
60 * indexed by processor would overflow.
62 result
= sysconf(_SC_NPROCESSORS_CONF
);
74 * _ust_buffers_strncpy_fixup - Fix an incomplete string in a ltt_relay buffer.
76 * @offset : offset within the buffer
77 * @len : length to write
78 * @copied: string actually copied
79 * @terminated: does string end with \0
81 * Fills string with "X" if incomplete.
83 void _ust_buffers_strncpy_fixup(struct ust_buffer
*buf
, size_t offset
,
84 size_t len
, size_t copied
, int terminated
)
86 size_t buf_offset
, cpy
;
90 * Deal with non-terminated string.
94 buf_offset
= BUFFER_OFFSET(offset
, buf
->chan
);
96 * Underlying layer should never ask for writes across
100 < buf
->chan
->subbuf_size
*buf
->chan
->subbuf_cnt
);
101 ust_buffers_do_memset(buf
->buf_data
+ buf_offset
, '\0', 1);
106 * Deal with incomplete string.
107 * Overwrite string's \0 with X too.
113 buf_offset
= BUFFER_OFFSET(offset
, buf
->chan
);
116 * Underlying layer should never ask for writes across subbuffers.
119 < buf
->chan
->subbuf_size
*buf
->chan
->subbuf_cnt
);
121 ust_buffers_do_memset(buf
->buf_data
+ buf_offset
,
125 * Overwrite last 'X' with '\0'.
128 buf_offset
= BUFFER_OFFSET(offset
, buf
->chan
);
130 * Underlying layer should never ask for writes across subbuffers.
133 < buf
->chan
->subbuf_size
*buf
->chan
->subbuf_cnt
);
134 ust_buffers_do_memset(buf
->buf_data
+ buf_offset
, '\0', 1);
137 static int ust_buffers_init_buffer(struct ust_trace
*trace
,
138 struct ust_channel
*ltt_chan
,
139 struct ust_buffer
*buf
,
140 unsigned int n_subbufs
);
142 static int ust_buffers_alloc_buf(struct ust_buffer
*buf
, size_t *size
)
147 *size
= PAGE_ALIGN(*size
);
149 result
= buf
->shmid
= shmget(getpid(), *size
, IPC_CREAT
| IPC_EXCL
| 0700);
150 if(result
== -1 && errno
== EINVAL
) {
151 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
154 else if(result
== -1) {
159 /* FIXME: should have matching call to shmdt */
160 ptr
= shmat(buf
->shmid
, NULL
, 0);
161 if(ptr
== (void *) -1) {
166 /* Already mark the shared memory for destruction. This will occur only
167 * when all users have detached.
169 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
176 buf
->buf_size
= *size
;
181 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
189 int ust_buffers_create_buf(struct ust_channel
*channel
, int cpu
)
192 struct ust_buffer
*buf
= channel
->buf
[cpu
];
195 result
= ust_buffers_alloc_buf(buf
, &channel
->alloc_size
);
200 kref_get(&channel
->kref
);
204 static void ust_buffers_destroy_channel(struct kref
*kref
)
206 struct ust_channel
*chan
= _ust_container_of(kref
, struct ust_channel
, kref
);
210 static void ust_buffers_destroy_buf(struct ust_buffer
*buf
)
212 struct ust_channel
*chan
= buf
->chan
;
215 result
= munmap(buf
->buf_data
, buf
->buf_size
);
220 //ust// chan->buf[buf->cpu] = NULL;
222 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
225 /* called from kref_put */
226 static void ust_buffers_remove_buf(struct kref
*kref
)
228 struct ust_buffer
*buf
= _ust_container_of(kref
, struct ust_buffer
, kref
);
229 ust_buffers_destroy_buf(buf
);
232 int ust_buffers_open_buf(struct ust_channel
*chan
, int cpu
)
236 result
= ust_buffers_create_buf(chan
, cpu
);
240 kref_init(&chan
->buf
[cpu
]->kref
);
242 result
= ust_buffers_init_buffer(chan
->trace
, chan
, chan
->buf
[cpu
], chan
->subbuf_cnt
);
248 /* FIXME: decrementally destroy on error? */
252 * ust_buffers_close_buf - close a channel buffer
255 static void ust_buffers_close_buf(struct ust_buffer
*buf
)
257 kref_put(&buf
->kref
, ust_buffers_remove_buf
);
260 int ust_buffers_channel_open(struct ust_channel
*chan
, size_t subbuf_size
, size_t subbuf_cnt
)
265 if(subbuf_size
== 0 || subbuf_cnt
== 0)
268 /* Check that the subbuffer size is larger than a page. */
269 WARN_ON_ONCE(subbuf_size
< PAGE_SIZE
);
272 * Make sure the number of subbuffers and subbuffer size are power of 2.
274 WARN_ON_ONCE(hweight32(subbuf_size
) != 1);
275 WARN_ON(hweight32(subbuf_cnt
) != 1);
277 chan
->version
= UST_CHANNEL_VERSION
;
278 chan
->subbuf_cnt
= subbuf_cnt
;
279 chan
->subbuf_size
= subbuf_size
;
280 chan
->subbuf_size_order
= get_count_order(subbuf_size
);
281 chan
->alloc_size
= subbuf_size
* subbuf_cnt
;
283 kref_init(&chan
->kref
);
285 pthread_mutex_lock(&ust_buffers_channels_mutex
);
286 for(i
=0; i
<chan
->n_cpus
; i
++) {
287 result
= ust_buffers_open_buf(chan
, i
);
291 cds_list_add(&chan
->list
, &ust_buffers_channels
);
292 pthread_mutex_unlock(&ust_buffers_channels_mutex
);
296 /* Jump directly inside the loop to close the buffers that were already
299 ust_buffers_close_buf(chan
->buf
[i
]);
304 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
305 pthread_mutex_unlock(&ust_buffers_channels_mutex
);
309 void ust_buffers_channel_close(struct ust_channel
*chan
)
315 pthread_mutex_lock(&ust_buffers_channels_mutex
);
316 for(i
=0; i
<chan
->n_cpus
; i
++) {
317 /* FIXME: if we make it here, then all buffers were necessarily allocated. Moreover, we don't
318 * initialize to NULL so we cannot use this check. Should we? */
319 //ust// if (chan->buf[i])
320 ust_buffers_close_buf(chan
->buf
[i
]);
323 cds_list_del(&chan
->list
);
324 kref_put(&chan
->kref
, ust_buffers_destroy_channel
);
325 pthread_mutex_unlock(&ust_buffers_channels_mutex
);
332 static void ust_buffers_destroy_buffer(struct ust_channel
*ltt_chan
, int cpu
);
334 static void ltt_force_switch(struct ust_buffer
*buf
,
335 enum force_switch_mode mode
);
340 static void ltt_buffer_begin(struct ust_buffer
*buf
,
341 u64 tsc
, unsigned int subbuf_idx
)
343 struct ust_channel
*channel
= buf
->chan
;
344 struct ltt_subbuffer_header
*header
=
345 (struct ltt_subbuffer_header
*)
346 ust_buffers_offset_address(buf
,
347 subbuf_idx
* buf
->chan
->subbuf_size
);
349 header
->cycle_count_begin
= tsc
;
350 header
->data_size
= 0xFFFFFFFF; /* for recognizing crashed buffers */
351 header
->sb_size
= 0xFFFFFFFF; /* for recognizing crashed buffers */
352 /* FIXME: add memory cmm_barrier? */
353 ltt_write_trace_header(channel
->trace
, header
);
357 * offset is assumed to never be 0 here : never deliver a completely empty
358 * subbuffer. The lost size is between 0 and subbuf_size-1.
360 static notrace
void ltt_buffer_end(struct ust_buffer
*buf
,
361 u64 tsc
, unsigned int offset
, unsigned int subbuf_idx
)
363 struct ltt_subbuffer_header
*header
=
364 (struct ltt_subbuffer_header
*)
365 ust_buffers_offset_address(buf
,
366 subbuf_idx
* buf
->chan
->subbuf_size
);
367 u32 data_size
= SUBBUF_OFFSET(offset
- 1, buf
->chan
) + 1;
369 header
->data_size
= data_size
;
370 header
->sb_size
= PAGE_ALIGN(data_size
);
371 header
->cycle_count_end
= tsc
;
372 header
->events_lost
= uatomic_read(&buf
->events_lost
);
373 header
->subbuf_corrupt
= uatomic_read(&buf
->corrupted_subbuffers
);
374 if(unlikely(header
->events_lost
> 0)) {
375 DBG("Some events (%d) were lost in %s_%d", header
->events_lost
, buf
->chan
->channel_name
, buf
->cpu
);
380 * This function should not be called from NMI interrupt context
382 static notrace
void ltt_buf_unfull(struct ust_buffer
*buf
,
383 unsigned int subbuf_idx
,
389 * Promote compiler cmm_barrier to a smp_mb().
390 * For the specific LTTng case, this IPI call should be removed if the
391 * architecture does not reorder writes. This should eventually be provided by
392 * a separate architecture-specific infrastructure.
394 //ust// static void remote_mb(void *info)
399 int ust_buffers_get_subbuf(struct ust_buffer
*buf
, long *consumed
)
401 struct ust_channel
*channel
= buf
->chan
;
402 long consumed_old
, consumed_idx
, commit_count
, write_offset
;
405 consumed_old
= uatomic_read(&buf
->consumed
);
406 consumed_idx
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
407 commit_count
= uatomic_read(&buf
->commit_count
[consumed_idx
].cc_sb
);
409 * Make sure we read the commit count before reading the buffer
410 * data and the write offset. Correct consumed offset ordering
411 * wrt commit count is insured by the use of cmpxchg to update
412 * the consumed offset.
413 * smp_call_function_single can fail if the remote CPU is offline,
414 * this is OK because then there is no wmb to execute there.
415 * If our thread is executing on the same CPU as the on the buffers
416 * belongs to, we don't have to synchronize it at all. If we are
417 * migrated, the scheduler will take care of the memory cmm_barriers.
418 * Normally, smp_call_function_single() should ensure program order when
419 * executing the remote function, which implies that it surrounds the
420 * function execution with :
431 * However, smp_call_function_single() does not seem to clearly execute
432 * such cmm_barriers. It depends on spinlock semantic to provide the cmm_barrier
433 * before executing the IPI and, when busy-looping, csd_lock_wait only
434 * executes smp_mb() when it has to wait for the other CPU.
436 * I don't trust this code. Therefore, let's add the smp_mb() sequence
437 * required ourself, even if duplicated. It has no performance impact
440 * smp_mb() is needed because cmm_smp_rmb() and cmm_smp_wmb() only order read vs
441 * read and write vs write. They do not ensure core synchronization. We
442 * really have to ensure total order between the 3 cmm_barriers running on
445 //ust// #ifdef LTT_NO_IPI_BARRIER
447 * Local rmb to match the remote wmb to read the commit count before the
448 * buffer data and the write offset.
452 //ust// if (raw_smp_processor_id() != buf->cpu) {
453 //ust// smp_mb(); /* Total order with IPI handler smp_mb() */
454 //ust// smp_call_function_single(buf->cpu, remote_mb, NULL, 1);
455 //ust// smp_mb(); /* Total order with IPI handler smp_mb() */
459 write_offset
= uatomic_read(&buf
->offset
);
461 * Check that the subbuffer we are trying to consume has been
462 * already fully committed.
464 if (((commit_count
- buf
->chan
->subbuf_size
)
465 & channel
->commit_count_mask
)
466 - (BUFFER_TRUNC(consumed_old
, buf
->chan
)
467 >> channel
->n_subbufs_order
)
472 * Check that we are not about to read the same subbuffer in
473 * which the writer head is.
475 if ((SUBBUF_TRUNC(write_offset
, buf
->chan
)
476 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
481 /* FIXME: is this ok to disable the reading feature? */
482 //ust// retval = update_read_sb_index(buf, consumed_idx);
484 //ust// return retval;
486 *consumed
= consumed_old
;
491 int ust_buffers_put_subbuf(struct ust_buffer
*buf
, unsigned long uconsumed_old
)
493 long consumed_new
, consumed_old
;
495 consumed_old
= uatomic_read(&buf
->consumed
);
496 consumed_old
= consumed_old
& (~0xFFFFFFFFL
);
497 consumed_old
= consumed_old
| uconsumed_old
;
498 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
500 //ust// spin_lock(<t_buf->full_lock);
501 if (uatomic_cmpxchg(&buf
->consumed
, consumed_old
,
504 /* We have been pushed by the writer : the last
505 * buffer read _is_ corrupted! It can also
506 * happen if this is a buffer we never got. */
507 //ust// spin_unlock(<t_buf->full_lock);
510 /* tell the client that buffer is now unfull */
513 index
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
514 data
= BUFFER_OFFSET(consumed_old
, buf
->chan
);
515 ltt_buf_unfull(buf
, index
, data
);
516 //ust// spin_unlock(<t_buf->full_lock);
521 static void ltt_relay_print_subbuffer_errors(
522 struct ust_channel
*channel
,
523 long cons_off
, int cpu
)
525 struct ust_buffer
*ltt_buf
= channel
->buf
[cpu
];
526 long cons_idx
, commit_count
, commit_count_sb
, write_offset
;
528 cons_idx
= SUBBUF_INDEX(cons_off
, channel
);
529 commit_count
= uatomic_read(<t_buf
->commit_count
[cons_idx
].cc
);
530 commit_count_sb
= uatomic_read(<t_buf
->commit_count
[cons_idx
].cc_sb
);
533 * No need to order commit_count and write_offset reads because we
534 * execute after trace is stopped when there are no readers left.
536 write_offset
= uatomic_read(<t_buf
->offset
);
537 WARN( "LTT : unread channel %s offset is %ld "
538 "and cons_off : %ld (cpu %d)\n",
539 channel
->channel_name
, write_offset
, cons_off
, cpu
);
540 /* Check each sub-buffer for non filled commit count */
541 if (((commit_count
- channel
->subbuf_size
) & channel
->commit_count_mask
)
542 - (BUFFER_TRUNC(cons_off
, channel
) >> channel
->n_subbufs_order
) != 0) {
543 ERR("LTT : %s : subbuffer %lu has non filled "
544 "commit count [cc, cc_sb] [%lu,%lu].\n",
545 channel
->channel_name
, cons_idx
, commit_count
, commit_count_sb
);
547 ERR("LTT : %s : commit count : %lu, subbuf size %zd\n",
548 channel
->channel_name
, commit_count
,
549 channel
->subbuf_size
);
552 static void ltt_relay_print_errors(struct ust_trace
*trace
,
553 struct ust_channel
*channel
, int cpu
)
555 struct ust_buffer
*ltt_buf
= channel
->buf
[cpu
];
559 * Can be called in the error path of allocation when
560 * trans_channel_data is not yet set.
565 //ust// for (cons_off = 0; cons_off < rchan->alloc_size;
566 //ust// cons_off = SUBBUF_ALIGN(cons_off, rchan))
567 //ust// ust_buffers_print_written(ltt_chan, cons_off, cpu);
568 for (cons_off
= uatomic_read(<t_buf
->consumed
);
569 (SUBBUF_TRUNC(uatomic_read(<t_buf
->offset
),
572 cons_off
= SUBBUF_ALIGN(cons_off
, channel
))
573 ltt_relay_print_subbuffer_errors(channel
, cons_off
, cpu
);
576 static void ltt_relay_print_buffer_errors(struct ust_channel
*channel
, int cpu
)
578 struct ust_trace
*trace
= channel
->trace
;
579 struct ust_buffer
*ltt_buf
= channel
->buf
[cpu
];
581 if (uatomic_read(<t_buf
->events_lost
))
582 ERR("channel %s: %ld events lost (cpu %d)",
583 channel
->channel_name
,
584 uatomic_read(<t_buf
->events_lost
), cpu
);
585 if (uatomic_read(<t_buf
->corrupted_subbuffers
))
586 ERR("channel %s : %ld corrupted subbuffers (cpu %d)",
587 channel
->channel_name
,
588 uatomic_read(<t_buf
->corrupted_subbuffers
), cpu
);
590 ltt_relay_print_errors(trace
, channel
, cpu
);
593 static void ltt_relay_release_channel(struct kref
*kref
)
595 struct ust_channel
*ltt_chan
= _ust_container_of(kref
,
596 struct ust_channel
, kref
);
603 //ust// static int ltt_relay_create_buffer(struct ust_trace *trace,
604 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
605 //ust// unsigned int cpu, unsigned int n_subbufs)
607 //ust// struct ltt_channel_buf_struct *ltt_buf =
608 //ust// percpu_ptr(ltt_chan->buf, cpu);
609 //ust// unsigned int j;
611 //ust// ltt_buf->commit_count =
612 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
613 //ust// GFP_KERNEL, cpu_to_node(cpu));
614 //ust// if (!ltt_buf->commit_count)
615 //ust// return -ENOMEM;
616 //ust// kref_get(&trace->kref);
617 //ust// kref_get(&trace->ltt_transport_kref);
618 //ust// kref_get(<t_chan->kref);
619 //ust// uatomic_set(<t_buf->offset, ltt_subbuffer_header_size());
620 //ust// uatomic_set(<t_buf->consumed, 0);
621 //ust// uatomic_set(<t_buf->active_readers, 0);
622 //ust// for (j = 0; j < n_subbufs; j++)
623 //ust// uatomic_set(<t_buf->commit_count[j], 0);
624 //ust// init_waitqueue_head(<t_buf->write_wait);
625 //ust// uatomic_set(<t_buf->wakeup_readers, 0);
626 //ust// spin_lock_init(<t_buf->full_lock);
628 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
629 //ust// /* atomic_add made on local variable on data that belongs to
630 //ust// * various CPUs : ok because tracing not started (for this cpu). */
631 //ust// uatomic_add(<t_buf->commit_count[0], ltt_subbuffer_header_size());
633 //ust// uatomic_set(<t_buf->events_lost, 0);
634 //ust// uatomic_set(<t_buf->corrupted_subbuffers, 0);
639 static int ust_buffers_init_buffer(struct ust_trace
*trace
,
640 struct ust_channel
*ltt_chan
, struct ust_buffer
*buf
,
641 unsigned int n_subbufs
)
648 zmalloc(sizeof(*buf
->commit_count
) * n_subbufs
);
649 if (!buf
->commit_count
)
651 kref_get(&trace
->kref
);
652 kref_get(&trace
->ltt_transport_kref
);
653 kref_get(<t_chan
->kref
);
654 uatomic_set(&buf
->offset
, ltt_subbuffer_header_size());
655 uatomic_set(&buf
->consumed
, 0);
656 uatomic_set(&buf
->active_readers
, 0);
657 for (j
= 0; j
< n_subbufs
; j
++) {
658 uatomic_set(&buf
->commit_count
[j
].cc
, 0);
659 uatomic_set(&buf
->commit_count
[j
].cc_sb
, 0);
661 //ust// init_waitqueue_head(&buf->write_wait);
662 //ust// uatomic_set(&buf->wakeup_readers, 0);
663 //ust// spin_lock_init(&buf->full_lock);
665 ltt_buffer_begin(buf
, trace
->start_tsc
, 0);
667 uatomic_add(&buf
->commit_count
[0].cc
, ltt_subbuffer_header_size());
669 uatomic_set(&buf
->events_lost
, 0);
670 uatomic_set(&buf
->corrupted_subbuffers
, 0);
677 buf
->data_ready_fd_read
= fds
[0];
678 buf
->data_ready_fd_write
= fds
[1];
680 //ust// buf->commit_seq = malloc(sizeof(buf->commit_seq) * n_subbufs);
681 //ust// if(!ltt_buf->commit_seq) {
684 memset(buf
->commit_seq
, 0, sizeof(buf
->commit_seq
[0]) * n_subbufs
);
686 /* FIXME: decrementally destroy on error */
691 /* FIXME: use this function */
692 static void ust_buffers_destroy_buffer(struct ust_channel
*ltt_chan
, int cpu
)
694 struct ust_trace
*trace
= ltt_chan
->trace
;
695 struct ust_buffer
*ltt_buf
= ltt_chan
->buf
[cpu
];
697 kref_put(<t_chan
->trace
->ltt_transport_kref
,
698 ltt_release_transport
);
699 ltt_relay_print_buffer_errors(ltt_chan
, cpu
);
700 //ust// free(ltt_buf->commit_seq);
701 free(ltt_buf
->commit_count
);
702 ltt_buf
->commit_count
= NULL
;
703 kref_put(<t_chan
->kref
, ltt_relay_release_channel
);
704 kref_put(&trace
->kref
, ltt_release_trace
);
705 //ust// wake_up_interruptible(&trace->kref_wq);
708 static int ust_buffers_alloc_channel_buf_structs(struct ust_channel
*chan
)
715 size
= PAGE_ALIGN(1);
717 for(i
=0; i
<chan
->n_cpus
; i
++) {
719 result
= chan
->buf_struct_shmids
[i
] = shmget(getpid(), size
, IPC_CREAT
| IPC_EXCL
| 0700);
722 goto destroy_previous
;
725 /* FIXME: should have matching call to shmdt */
726 ptr
= shmat(chan
->buf_struct_shmids
[i
], NULL
, 0);
727 if(ptr
== (void *) -1) {
732 /* Already mark the shared memory for destruction. This will occur only
733 * when all users have detached.
735 result
= shmctl(chan
->buf_struct_shmids
[i
], IPC_RMID
, NULL
);
738 goto destroy_previous
;
746 /* Jumping inside this loop occurs from within the other loop above with i as
747 * counter, so it unallocates the structures for the cpu = current_i down to
751 result
= shmctl(chan
->buf_struct_shmids
[i
], IPC_RMID
, NULL
);
766 static int ust_buffers_create_channel(const char *trace_name
, struct ust_trace
*trace
,
767 const char *channel_name
, struct ust_channel
*ltt_chan
,
768 unsigned int subbuf_size
, unsigned int n_subbufs
, int overwrite
)
772 kref_init(<t_chan
->kref
);
774 ltt_chan
->trace
= trace
;
775 ltt_chan
->overwrite
= overwrite
;
776 ltt_chan
->n_subbufs_order
= get_count_order(n_subbufs
);
777 ltt_chan
->commit_count_mask
= (~0UL >> ltt_chan
->n_subbufs_order
);
778 ltt_chan
->n_cpus
= get_n_cpus();
779 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
780 ltt_chan
->buf
= (void *) zmalloc(ltt_chan
->n_cpus
* sizeof(void *));
781 if(ltt_chan
->buf
== NULL
) {
784 ltt_chan
->buf_struct_shmids
= (int *) zmalloc(ltt_chan
->n_cpus
* sizeof(int));
785 if(ltt_chan
->buf_struct_shmids
== NULL
)
788 result
= ust_buffers_alloc_channel_buf_structs(ltt_chan
);
790 goto free_buf_struct_shmids
;
793 result
= ust_buffers_channel_open(ltt_chan
, subbuf_size
, n_subbufs
);
795 ERR("Cannot open channel for trace %s", trace_name
);
796 goto unalloc_buf_structs
;
802 /* FIXME: put a call here to unalloc the buf structs! */
804 free_buf_struct_shmids
:
805 free(ltt_chan
->buf_struct_shmids
);
814 static void ltt_relay_async_wakeup_chan(struct ust_channel
*ltt_channel
)
816 //ust// unsigned int i;
817 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
819 //ust// for_each_possible_cpu(i) {
820 //ust// struct ltt_channel_buf_struct *ltt_buf =
821 //ust// percpu_ptr(ltt_channel->buf, i);
823 //ust// if (uatomic_read(<t_buf->wakeup_readers) == 1) {
824 //ust// uatomic_set(<t_buf->wakeup_readers, 0);
825 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
830 static void ltt_relay_finish_buffer(struct ust_channel
*channel
, unsigned int cpu
)
834 if (channel
->buf
[cpu
]) {
835 struct ust_buffer
*buf
= channel
->buf
[cpu
];
836 ltt_force_switch(buf
, FORCE_FLUSH
);
837 //ust// ltt_relay_wake_writers(ltt_buf);
838 /* closing the pipe tells the consumer the buffer is finished */
840 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
842 // PERROR("write (in ltt_relay_finish_buffer)");
843 // ERR("this should never happen!");
845 close(buf
->data_ready_fd_write
);
850 static void ltt_relay_finish_channel(struct ust_channel
*channel
)
854 for(i
=0; i
<channel
->n_cpus
; i
++) {
855 ltt_relay_finish_buffer(channel
, i
);
859 static void ltt_relay_remove_channel(struct ust_channel
*channel
)
861 ust_buffers_channel_close(channel
);
862 kref_put(&channel
->kref
, ltt_relay_release_channel
);
866 * ltt_reserve_switch_old_subbuf: switch old subbuffer
868 * Concurrency safe because we are the last and only thread to alter this
869 * sub-buffer. As long as it is not delivered and read, no other thread can
870 * alter the offset, alter the reserve_count or call the
871 * client_buffer_end_callback on this sub-buffer.
873 * The only remaining threads could be the ones with pending commits. They will
874 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
875 * We detect corrupted subbuffers with commit and reserve counts. We keep a
876 * corrupted sub-buffers count and push the readers across these sub-buffers.
878 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
879 * switches in, finding out it's corrupted. The result will be than the old
880 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
881 * will be declared corrupted too because of the commit count adjustment.
883 * Note : offset_old should never be 0 here.
885 static void ltt_reserve_switch_old_subbuf(
886 struct ust_channel
*chan
, struct ust_buffer
*buf
,
887 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
889 long oldidx
= SUBBUF_INDEX(offsets
->old
- 1, chan
);
890 long commit_count
, padding_size
;
892 padding_size
= chan
->subbuf_size
893 - (SUBBUF_OFFSET(offsets
->old
- 1, chan
) + 1);
894 ltt_buffer_end(buf
, *tsc
, offsets
->old
, oldidx
);
897 * Must write slot data before incrementing commit count.
898 * This compiler cmm_barrier is upgraded into a cmm_smp_wmb() by the IPI
899 * sent by get_subbuf() when it does its cmm_smp_rmb().
902 uatomic_add(&buf
->commit_count
[oldidx
].cc
, padding_size
);
903 commit_count
= uatomic_read(&buf
->commit_count
[oldidx
].cc
);
904 ltt_check_deliver(chan
, buf
, offsets
->old
- 1, commit_count
, oldidx
);
905 ltt_write_commit_counter(chan
, buf
, oldidx
,
906 offsets
->old
, commit_count
, padding_size
);
910 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
912 * This code can be executed unordered : writers may already have written to the
913 * sub-buffer before this code gets executed, caution. The commit makes sure
914 * that this code is executed before the deliver of this sub-buffer.
916 static void ltt_reserve_switch_new_subbuf(
917 struct ust_channel
*chan
, struct ust_buffer
*buf
,
918 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
920 long beginidx
= SUBBUF_INDEX(offsets
->begin
, chan
);
923 ltt_buffer_begin(buf
, *tsc
, beginidx
);
926 * Must write slot data before incrementing commit count.
927 * This compiler cmm_barrier is upgraded into a cmm_smp_wmb() by the IPI
928 * sent by get_subbuf() when it does its cmm_smp_rmb().
931 uatomic_add(&buf
->commit_count
[beginidx
].cc
, ltt_subbuffer_header_size());
932 commit_count
= uatomic_read(&buf
->commit_count
[beginidx
].cc
);
933 /* Check if the written buffer has to be delivered */
934 ltt_check_deliver(chan
, buf
, offsets
->begin
, commit_count
, beginidx
);
935 ltt_write_commit_counter(chan
, buf
, beginidx
,
936 offsets
->begin
, commit_count
, ltt_subbuffer_header_size());
940 * ltt_reserve_end_switch_current: finish switching current subbuffer
942 * Concurrency safe because we are the last and only thread to alter this
943 * sub-buffer. As long as it is not delivered and read, no other thread can
944 * alter the offset, alter the reserve_count or call the
945 * client_buffer_end_callback on this sub-buffer.
947 * The only remaining threads could be the ones with pending commits. They will
948 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
949 * We detect corrupted subbuffers with commit and reserve counts. We keep a
950 * corrupted sub-buffers count and push the readers across these sub-buffers.
952 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
953 * switches in, finding out it's corrupted. The result will be than the old
954 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
955 * will be declared corrupted too because of the commit count adjustment.
957 static void ltt_reserve_end_switch_current(
958 struct ust_channel
*chan
,
959 struct ust_buffer
*buf
,
960 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
962 long endidx
= SUBBUF_INDEX(offsets
->end
- 1, chan
);
963 long commit_count
, padding_size
;
965 padding_size
= chan
->subbuf_size
966 - (SUBBUF_OFFSET(offsets
->end
- 1, chan
) + 1);
968 ltt_buffer_end(buf
, *tsc
, offsets
->end
, endidx
);
971 * Must write slot data before incrementing commit count.
972 * This compiler cmm_barrier is upgraded into a cmm_smp_wmb() by the IPI
973 * sent by get_subbuf() when it does its cmm_smp_rmb().
976 uatomic_add(&buf
->commit_count
[endidx
].cc
, padding_size
);
977 commit_count
= uatomic_read(&buf
->commit_count
[endidx
].cc
);
978 ltt_check_deliver(chan
, buf
,
979 offsets
->end
- 1, commit_count
, endidx
);
980 ltt_write_commit_counter(chan
, buf
, endidx
,
981 offsets
->end
, commit_count
, padding_size
);
987 * !0 if execution must be aborted.
989 static int ltt_relay_try_switch_slow(
990 enum force_switch_mode mode
,
991 struct ust_channel
*chan
,
992 struct ust_buffer
*buf
,
993 struct ltt_reserve_switch_offsets
*offsets
,
997 long reserve_commit_diff
;
999 offsets
->begin
= uatomic_read(&buf
->offset
);
1000 offsets
->old
= offsets
->begin
;
1001 offsets
->begin_switch
= 0;
1002 offsets
->end_switch_old
= 0;
1004 *tsc
= trace_clock_read64();
1006 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) != 0) {
1007 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
, buf
->chan
);
1008 offsets
->end_switch_old
= 1;
1010 /* we do not have to switch : buffer is empty */
1013 if (mode
== FORCE_ACTIVE
)
1014 offsets
->begin
+= ltt_subbuffer_header_size();
1016 * Always begin_switch in FORCE_ACTIVE mode.
1017 * Test new buffer integrity
1019 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1020 reserve_commit_diff
=
1021 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1022 >> chan
->n_subbufs_order
)
1023 - (uatomic_read(&buf
->commit_count
[subbuf_index
].cc_sb
)
1024 & chan
->commit_count_mask
);
1025 if (reserve_commit_diff
== 0) {
1026 /* Next buffer not corrupted. */
1027 if (mode
== FORCE_ACTIVE
1029 && offsets
->begin
- uatomic_read(&buf
->consumed
)
1030 >= chan
->alloc_size
) {
1032 * We do not overwrite non consumed buffers and we are
1033 * full : ignore switch while tracing is active.
1039 * Next subbuffer corrupted. Force pushing reader even in normal
1043 offsets
->end
= offsets
->begin
;
1048 * Force a sub-buffer switch for a per-cpu buffer. This operation is
1049 * completely reentrant : can be called while tracing is active with
1050 * absolutely no lock held.
1052 void ltt_force_switch_lockless_slow(struct ust_buffer
*buf
,
1053 enum force_switch_mode mode
)
1055 struct ust_channel
*chan
= buf
->chan
;
1056 struct ltt_reserve_switch_offsets offsets
;
1061 DBG("Switching (forced) %s_%d", chan
->channel_name
, buf
->cpu
);
1063 * Perform retryable operations.
1066 if (ltt_relay_try_switch_slow(mode
, chan
, buf
,
1069 } while (uatomic_cmpxchg(&buf
->offset
, offsets
.old
,
1070 offsets
.end
) != offsets
.old
);
1073 * Atomically update last_tsc. This update races against concurrent
1074 * atomic updates, but the race will always cause supplementary full TSC
1075 * events, never the opposite (missing a full TSC event when it would be
1078 save_last_tsc(buf
, tsc
);
1081 * Push the reader if necessary
1083 if (mode
== FORCE_ACTIVE
) {
1084 ltt_reserve_push_reader(chan
, buf
, offsets
.end
- 1);
1085 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan));
1089 * Switch old subbuffer if needed.
1091 if (offsets
.end_switch_old
) {
1092 //ust// ltt_clear_noref_flag(rchan, buf, SUBBUF_INDEX(offsets.old - 1, rchan));
1093 ltt_reserve_switch_old_subbuf(chan
, buf
, &offsets
, &tsc
);
1097 * Populate new subbuffer.
1099 if (mode
== FORCE_ACTIVE
)
1100 ltt_reserve_switch_new_subbuf(chan
, buf
, &offsets
, &tsc
);
1106 * !0 if execution must be aborted.
1108 static int ltt_relay_try_reserve_slow(struct ust_channel
*chan
, struct ust_buffer
*buf
,
1109 struct ltt_reserve_switch_offsets
*offsets
, size_t data_size
,
1110 u64
*tsc
, unsigned int *rflags
, int largest_align
)
1112 long reserve_commit_diff
;
1114 offsets
->begin
= uatomic_read(&buf
->offset
);
1115 offsets
->old
= offsets
->begin
;
1116 offsets
->begin_switch
= 0;
1117 offsets
->end_switch_current
= 0;
1118 offsets
->end_switch_old
= 0;
1120 *tsc
= trace_clock_read64();
1121 if (last_tsc_overflow(buf
, *tsc
))
1122 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
1124 if (unlikely(SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) == 0)) {
1125 offsets
->begin_switch
= 1; /* For offsets->begin */
1127 offsets
->size
= ust_get_header_size(chan
,
1128 offsets
->begin
, data_size
,
1129 &offsets
->before_hdr_pad
, *rflags
);
1130 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1133 if (unlikely((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) +
1134 offsets
->size
) > buf
->chan
->subbuf_size
)) {
1135 offsets
->end_switch_old
= 1; /* For offsets->old */
1136 offsets
->begin_switch
= 1; /* For offsets->begin */
1139 if (unlikely(offsets
->begin_switch
)) {
1143 * We are typically not filling the previous buffer completely.
1145 if (likely(offsets
->end_switch_old
))
1146 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
,
1148 offsets
->begin
= offsets
->begin
+ ltt_subbuffer_header_size();
1149 /* Test new buffer integrity */
1150 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1151 reserve_commit_diff
=
1152 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1153 >> chan
->n_subbufs_order
)
1154 - (uatomic_read(&buf
->commit_count
[subbuf_index
].cc_sb
)
1155 & chan
->commit_count_mask
);
1156 if (likely(reserve_commit_diff
== 0)) {
1157 /* Next buffer not corrupted. */
1158 if (unlikely(!chan
->overwrite
&&
1159 (SUBBUF_TRUNC(offsets
->begin
, buf
->chan
)
1160 - SUBBUF_TRUNC(uatomic_read(
1163 >= chan
->alloc_size
)) {
1165 * We do not overwrite non consumed buffers
1166 * and we are full : event is lost.
1168 uatomic_inc(&buf
->events_lost
);
1172 * next buffer not corrupted, we are either in
1173 * overwrite mode or the buffer is not full.
1174 * It's safe to write in this new subbuffer.
1179 * Next subbuffer corrupted. Drop event in normal and
1180 * overwrite mode. Caused by either a writer OOPS or
1181 * too many nested writes over a reserve/commit pair.
1183 uatomic_inc(&buf
->events_lost
);
1186 offsets
->size
= ust_get_header_size(chan
,
1187 offsets
->begin
, data_size
,
1188 &offsets
->before_hdr_pad
, *rflags
);
1189 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1192 if (unlikely((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
)
1193 + offsets
->size
) > buf
->chan
->subbuf_size
)) {
1195 * Event too big for subbuffers, report error, don't
1196 * complete the sub-buffer switch.
1198 uatomic_inc(&buf
->events_lost
);
1202 * We just made a successful buffer switch and the event
1203 * fits in the new subbuffer. Let's write.
1208 * Event fits in the current buffer and we are not on a switch
1209 * boundary. It's safe to write.
1212 offsets
->end
= offsets
->begin
+ offsets
->size
;
1214 if (unlikely((SUBBUF_OFFSET(offsets
->end
, buf
->chan
)) == 0)) {
1216 * The offset_end will fall at the very beginning of the next
1219 offsets
->end_switch_current
= 1; /* For offsets->begin */
1225 * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer.
1226 * @trace: the trace structure to log to.
1227 * @ltt_channel: channel structure
1228 * @transport_data: data structure specific to ltt relay
1229 * @data_size: size of the variable length data to log.
1230 * @slot_size: pointer to total size of the slot (out)
1231 * @buf_offset : pointer to reserved buffer offset (out)
1232 * @tsc: pointer to the tsc at the slot reservation (out)
1235 * Return : -ENOSPC if not enough space, else returns 0.
1236 * It will take care of sub-buffer switching.
1238 int ltt_reserve_slot_lockless_slow(struct ust_channel
*chan
,
1239 struct ust_trace
*trace
, size_t data_size
,
1240 int largest_align
, int cpu
,
1241 struct ust_buffer
**ret_buf
,
1242 size_t *slot_size
, long *buf_offset
,
1243 u64
*tsc
, unsigned int *rflags
)
1245 struct ust_buffer
*buf
= *ret_buf
= chan
->buf
[cpu
];
1246 struct ltt_reserve_switch_offsets offsets
;
1251 if (unlikely(ltt_relay_try_reserve_slow(chan
, buf
, &offsets
,
1252 data_size
, tsc
, rflags
, largest_align
)))
1254 } while (unlikely(uatomic_cmpxchg(&buf
->offset
, offsets
.old
,
1255 offsets
.end
) != offsets
.old
));
1258 * Atomically update last_tsc. This update races against concurrent
1259 * atomic updates, but the race will always cause supplementary full TSC
1260 * events, never the opposite (missing a full TSC event when it would be
1263 save_last_tsc(buf
, *tsc
);
1266 * Push the reader if necessary
1268 ltt_reserve_push_reader(chan
, buf
, offsets
.end
- 1);
1271 * Clear noref flag for this subbuffer.
1273 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan));
1276 * Switch old subbuffer if needed.
1278 if (unlikely(offsets
.end_switch_old
)) {
1279 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.old - 1, chan));
1280 ltt_reserve_switch_old_subbuf(chan
, buf
, &offsets
, tsc
);
1281 DBG("Switching %s_%d", chan
->channel_name
, cpu
);
1285 * Populate new subbuffer.
1287 if (unlikely(offsets
.begin_switch
))
1288 ltt_reserve_switch_new_subbuf(chan
, buf
, &offsets
, tsc
);
1290 if (unlikely(offsets
.end_switch_current
))
1291 ltt_reserve_end_switch_current(chan
, buf
, &offsets
, tsc
);
1293 *slot_size
= offsets
.size
;
1294 *buf_offset
= offsets
.begin
+ offsets
.before_hdr_pad
;
1298 static struct ltt_transport ust_relay_transport
= {
1301 .create_channel
= ust_buffers_create_channel
,
1302 .finish_channel
= ltt_relay_finish_channel
,
1303 .remove_channel
= ltt_relay_remove_channel
,
1304 .wakeup_channel
= ltt_relay_async_wakeup_chan
,
1308 static char initialized
= 0;
1310 void __attribute__((constructor
)) init_ustrelay_transport(void)
1313 ltt_transport_register(&ust_relay_transport
);
1318 static void __attribute__((destructor
)) ust_buffers_exit(void)
1320 ltt_transport_unregister(&ust_relay_transport
);
1323 size_t ltt_write_event_header_slow(struct ust_channel
*channel
,
1324 struct ust_buffer
*buf
, long buf_offset
,
1325 u16 eID
, u32 event_size
,
1326 u64 tsc
, unsigned int rflags
)
1328 struct ltt_event_header header
;
1332 case LTT_RFLAG_ID_SIZE_TSC
:
1333 header
.id_time
= 29 << LTT_TSC_BITS
;
1335 case LTT_RFLAG_ID_SIZE
:
1336 header
.id_time
= 30 << LTT_TSC_BITS
;
1339 header
.id_time
= 31 << LTT_TSC_BITS
;
1343 header
.id_time
|= (u32
)tsc
& LTT_TSC_MASK
;
1344 ust_buffers_write(buf
, buf_offset
, &header
, sizeof(header
));
1345 buf_offset
+= sizeof(header
);
1348 case LTT_RFLAG_ID_SIZE_TSC
:
1349 small_size
= (u16
)min_t(u32
, event_size
, LTT_MAX_SMALL_SIZE
);
1350 ust_buffers_write(buf
, buf_offset
,
1352 buf_offset
+= sizeof(u16
);
1353 ust_buffers_write(buf
, buf_offset
,
1354 &small_size
, sizeof(u16
));
1355 buf_offset
+= sizeof(u16
);
1356 if (small_size
== LTT_MAX_SMALL_SIZE
) {
1357 ust_buffers_write(buf
, buf_offset
,
1358 &event_size
, sizeof(u32
));
1359 buf_offset
+= sizeof(u32
);
1361 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
1362 ust_buffers_write(buf
, buf_offset
,
1364 buf_offset
+= sizeof(u64
);
1366 case LTT_RFLAG_ID_SIZE
:
1367 small_size
= (u16
)min_t(u32
, event_size
, LTT_MAX_SMALL_SIZE
);
1368 ust_buffers_write(buf
, buf_offset
,
1370 buf_offset
+= sizeof(u16
);
1371 ust_buffers_write(buf
, buf_offset
,
1372 &small_size
, sizeof(u16
));
1373 buf_offset
+= sizeof(u16
);
1374 if (small_size
== LTT_MAX_SMALL_SIZE
) {
1375 ust_buffers_write(buf
, buf_offset
,
1376 &event_size
, sizeof(u32
));
1377 buf_offset
+= sizeof(u32
);
1381 ust_buffers_write(buf
, buf_offset
,
1383 buf_offset
+= sizeof(u16
);