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-2011 - 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; version 2.1 of
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
24 * Note: this code does not support the ref/noref flag and reader-owned
25 * subbuffer scheme. Therefore, flight recorder mode uses a mechanism
26 * where the reader can read corrupted data (and detect this), thus
37 #include <ust/clock.h>
42 #include "tracercore.h"
43 #include <ust/usterr-signal-safe.h>
45 struct ltt_reserve_switch_offsets
{
47 long begin_switch
, end_switch_current
, end_switch_old
;
48 size_t before_hdr_pad
, size
;
52 static DEFINE_MUTEX(ust_buffers_channels_mutex
);
53 static CDS_LIST_HEAD(ust_buffers_channels
);
55 static void ltt_force_switch(struct ust_buffer
*buf
,
56 enum force_switch_mode mode
);
59 * _ust_buffers_strncpy_fixup - Fix an incomplete string in a ltt_relay buffer.
61 * @offset : offset within the buffer
62 * @len : length to write
63 * @copied: string actually copied
64 * @terminated: does string end with \0
66 * Fills string with "X" if incomplete.
68 void _ust_buffers_strncpy_fixup(struct ust_buffer
*buf
, size_t offset
,
69 size_t len
, size_t copied
, int terminated
)
71 size_t buf_offset
, cpy
;
75 * Deal with non-terminated string.
79 buf_offset
= BUFFER_OFFSET(offset
, buf
->chan
);
81 * Underlying layer should never ask for writes across
85 < buf
->chan
->subbuf_size
*buf
->chan
->subbuf_cnt
);
86 ust_buffers_do_memset(buf
->buf_data
+ buf_offset
, '\0', 1);
91 * Deal with incomplete string.
92 * Overwrite string's \0 with X too.
98 buf_offset
= BUFFER_OFFSET(offset
, buf
->chan
);
101 * Underlying layer should never ask for writes across subbuffers.
104 < buf
->chan
->subbuf_size
*buf
->chan
->subbuf_cnt
);
106 ust_buffers_do_memset(buf
->buf_data
+ buf_offset
,
110 * Overwrite last 'X' with '\0'.
113 buf_offset
= BUFFER_OFFSET(offset
, buf
->chan
);
115 * Underlying layer should never ask for writes across subbuffers.
118 < buf
->chan
->subbuf_size
*buf
->chan
->subbuf_cnt
);
119 ust_buffers_do_memset(buf
->buf_data
+ buf_offset
, '\0', 1);
122 static void ltt_buffer_begin(struct ust_buffer
*buf
,
123 u64 tsc
, unsigned int subbuf_idx
)
125 struct ust_channel
*channel
= buf
->chan
;
126 struct ltt_subbuffer_header
*header
=
127 (struct ltt_subbuffer_header
*)
128 ust_buffers_offset_address(buf
,
129 subbuf_idx
* buf
->chan
->subbuf_size
);
131 header
->cycle_count_begin
= tsc
;
132 header
->data_size
= 0xFFFFFFFF; /* for recognizing crashed buffers */
133 header
->sb_size
= 0xFFFFFFFF; /* for recognizing crashed buffers */
135 * No memory barrier needed to order data_data/sb_size vs commit count
136 * update, because commit count update contains a compiler barrier that
137 * ensures the order of the writes are OK from a program POV. It only
138 * matters for crash dump recovery which is not executed concurrently,
139 * so memory write order does not matter.
141 ltt_write_trace_header(channel
->trace
, header
);
144 static int map_buf_data(struct ust_buffer
*buf
, size_t *size
)
149 *size
= PAGE_ALIGN(*size
);
151 result
= buf
->shmid
= shmget(getpid(), *size
, IPC_CREAT
| IPC_EXCL
| 0700);
152 if (result
< 0 && errno
== EINVAL
) {
153 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
155 } else if (result
< 0) {
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 static int open_buf(struct ust_channel
*chan
, int cpu
)
193 struct ust_trace
*trace
= chan
->trace
;
194 struct ust_buffer
*buf
= chan
->buf
[cpu
];
195 unsigned int n_subbufs
= chan
->subbuf_cnt
;
198 result
= map_buf_data(buf
, &chan
->alloc_size
);
203 zmalloc(sizeof(*buf
->commit_count
) * n_subbufs
);
204 if (!buf
->commit_count
)
210 goto free_commit_count
;
212 buf
->data_ready_fd_read
= fds
[0];
213 buf
->data_ready_fd_write
= fds
[1];
218 uatomic_set(&buf
->offset
, ltt_subbuffer_header_size());
219 uatomic_set(&buf
->consumed
, 0);
220 uatomic_set(&buf
->active_readers
, 0);
221 for (j
= 0; j
< n_subbufs
; j
++) {
222 uatomic_set(&buf
->commit_count
[j
].cc
, 0);
223 uatomic_set(&buf
->commit_count
[j
].cc_sb
, 0);
226 ltt_buffer_begin(buf
, trace
->start_tsc
, 0);
228 uatomic_add(&buf
->commit_count
[0].cc
, ltt_subbuffer_header_size());
230 uatomic_set(&buf
->events_lost
, 0);
231 uatomic_set(&buf
->corrupted_subbuffers
, 0);
233 memset(buf
->commit_seq
, 0, sizeof(buf
->commit_seq
[0]) * n_subbufs
);
238 free(buf
->commit_count
);
241 if (shmdt(buf
->buf_data
) < 0) {
242 PERROR("shmdt failed");
248 static void close_buf(struct ust_buffer
*buf
)
252 result
= shmdt(buf
->buf_data
);
257 result
= close(buf
->data_ready_fd_read
);
262 result
= close(buf
->data_ready_fd_write
);
263 if (result
< 0 && errno
!= EBADF
) {
269 static int open_channel(struct ust_channel
*chan
, size_t subbuf_size
,
275 if(subbuf_size
== 0 || subbuf_cnt
== 0)
278 /* Check that the subbuffer size is larger than a page. */
279 WARN_ON_ONCE(subbuf_size
< PAGE_SIZE
);
282 * Make sure the number of subbuffers and subbuffer size are power of 2.
284 WARN_ON_ONCE(hweight32(subbuf_size
) != 1);
285 WARN_ON(hweight32(subbuf_cnt
) != 1);
287 chan
->version
= UST_CHANNEL_VERSION
;
288 chan
->subbuf_cnt
= subbuf_cnt
;
289 chan
->subbuf_size
= subbuf_size
;
290 chan
->subbuf_size_order
= get_count_order(subbuf_size
);
291 chan
->alloc_size
= subbuf_size
* subbuf_cnt
;
293 pthread_mutex_lock(&ust_buffers_channels_mutex
);
294 for (i
=0; i
< chan
->n_cpus
; i
++) {
295 result
= open_buf(chan
, i
);
299 cds_list_add(&chan
->list
, &ust_buffers_channels
);
300 pthread_mutex_unlock(&ust_buffers_channels_mutex
);
306 for(i
--; i
>= 0; i
--)
307 close_buf(chan
->buf
[i
]);
309 pthread_mutex_unlock(&ust_buffers_channels_mutex
);
313 static void close_channel(struct ust_channel
*chan
)
319 pthread_mutex_lock(&ust_buffers_channels_mutex
);
321 * checking for chan->buf[i] being NULL or not is useless in
322 * practice because we allocate buffers for all possible cpus.
323 * However, should we decide to change this and only allocate
324 * for online cpus, this check becomes useful.
326 for (i
=0; i
<chan
->n_cpus
; i
++) {
328 close_buf(chan
->buf
[i
]);
331 cds_list_del(&chan
->list
);
333 pthread_mutex_unlock(&ust_buffers_channels_mutex
);
337 * offset is assumed to never be 0 here : never deliver a completely empty
338 * subbuffer. The lost size is between 0 and subbuf_size-1.
340 static notrace
void ltt_buffer_end(struct ust_buffer
*buf
,
341 u64 tsc
, unsigned int offset
, unsigned int subbuf_idx
)
343 struct ltt_subbuffer_header
*header
=
344 (struct ltt_subbuffer_header
*)
345 ust_buffers_offset_address(buf
,
346 subbuf_idx
* buf
->chan
->subbuf_size
);
347 u32 data_size
= SUBBUF_OFFSET(offset
- 1, buf
->chan
) + 1;
349 header
->sb_size
= PAGE_ALIGN(data_size
);
350 header
->cycle_count_end
= tsc
;
351 header
->events_lost
= uatomic_read(&buf
->events_lost
);
352 header
->subbuf_corrupt
= uatomic_read(&buf
->corrupted_subbuffers
);
353 if(unlikely(header
->events_lost
> 0)) {
354 DBG("Some events (%d) were lost in %s_%d", header
->events_lost
, buf
->chan
->channel_name
, buf
->cpu
);
357 * Makes sure data_size write happens after write of the rest of the
358 * buffer end data, because data_size is used to identify a completely
359 * written subbuffer in a crash dump.
362 header
->data_size
= data_size
;
366 * This function should not be called from NMI interrupt context
368 static notrace
void ltt_buf_unfull(struct ust_buffer
*buf
,
369 unsigned int subbuf_idx
,
375 * Promote compiler cmm_barrier to a smp_mb().
376 * For the specific LTTng case, this IPI call should be removed if the
377 * architecture does not reorder writes. This should eventually be provided by
378 * a separate architecture-specific infrastructure.
380 //ust// static void remote_mb(void *info)
385 int ust_buffers_get_subbuf(struct ust_buffer
*buf
, long *consumed
)
387 struct ust_channel
*channel
= buf
->chan
;
388 long consumed_old
, consumed_idx
, commit_count
, write_offset
;
391 consumed_old
= uatomic_read(&buf
->consumed
);
392 consumed_idx
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
393 commit_count
= uatomic_read(&buf
->commit_count
[consumed_idx
].cc_sb
);
395 * Make sure we read the commit count before reading the buffer
396 * data and the write offset. Correct consumed offset ordering
397 * wrt commit count is insured by the use of cmpxchg to update
398 * the consumed offset.
402 * Local rmb to match the remote wmb to read the commit count before the
403 * buffer data and the write offset.
407 write_offset
= uatomic_read(&buf
->offset
);
409 * Check that the subbuffer we are trying to consume has been
410 * already fully committed.
412 if (((commit_count
- buf
->chan
->subbuf_size
)
413 & channel
->commit_count_mask
)
414 - (BUFFER_TRUNC(consumed_old
, buf
->chan
)
415 >> channel
->n_subbufs_order
)
420 * Check that we are not about to read the same subbuffer in
421 * which the writer head is.
423 if ((SUBBUF_TRUNC(write_offset
, buf
->chan
)
424 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
428 *consumed
= consumed_old
;
433 int ust_buffers_put_subbuf(struct ust_buffer
*buf
, unsigned long uconsumed_old
)
435 long consumed_new
, consumed_old
;
437 consumed_old
= uatomic_read(&buf
->consumed
);
438 consumed_old
= consumed_old
& (~0xFFFFFFFFL
);
439 consumed_old
= consumed_old
| uconsumed_old
;
440 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
442 if (uatomic_cmpxchg(&buf
->consumed
, consumed_old
,
445 /* We have been pushed by the writer : the last
446 * buffer read _is_ corrupted! It can also
447 * happen if this is a buffer we never got. */
450 /* tell the client that buffer is now unfull */
453 index
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
454 data
= BUFFER_OFFSET(consumed_old
, buf
->chan
);
455 ltt_buf_unfull(buf
, index
, data
);
460 static int map_buf_structs(struct ust_channel
*chan
)
467 size
= PAGE_ALIGN(1);
469 for(i
=0; i
<chan
->n_cpus
; i
++) {
471 result
= chan
->buf_struct_shmids
[i
] = shmget(getpid(), size
, IPC_CREAT
| IPC_EXCL
| 0700);
474 goto destroy_previous
;
477 ptr
= shmat(chan
->buf_struct_shmids
[i
], NULL
, 0);
478 if(ptr
== (void *) -1) {
483 /* Already mark the shared memory for destruction. This will occur only
484 * when all users have detached.
486 result
= shmctl(chan
->buf_struct_shmids
[i
], IPC_RMID
, NULL
);
489 goto destroy_previous
;
497 /* Jumping inside this loop occurs from within the other loop above with i as
498 * counter, so it unallocates the structures for the cpu = current_i down to
502 result
= shmctl(chan
->buf_struct_shmids
[i
], IPC_RMID
, NULL
);
514 static int unmap_buf_structs(struct ust_channel
*chan
)
518 for (i
=0; i
< chan
->n_cpus
; i
++) {
519 if (shmdt(chan
->buf
[i
]) < 0) {
529 static int create_channel(const char *trace_name
, struct ust_trace
*trace
,
530 const char *channel_name
, struct ust_channel
*chan
,
531 unsigned int subbuf_size
, unsigned int n_subbufs
, int overwrite
)
536 chan
->overwrite
= overwrite
;
537 chan
->n_subbufs_order
= get_count_order(n_subbufs
);
538 chan
->commit_count_mask
= (~0UL >> chan
->n_subbufs_order
);
539 chan
->n_cpus
= get_n_cpus();
541 /* These mappings should ideall be per-cpu, if somebody can do that
542 * from userspace, that would be cool!
544 chan
->buf
= (void *) zmalloc(chan
->n_cpus
* sizeof(void *));
545 if(chan
->buf
== NULL
) {
548 chan
->buf_struct_shmids
= (int *) zmalloc(chan
->n_cpus
* sizeof(int));
549 if(chan
->buf_struct_shmids
== NULL
)
552 result
= map_buf_structs(chan
);
554 goto free_buf_struct_shmids
;
557 result
= open_channel(chan
, subbuf_size
, n_subbufs
);
559 ERR("Cannot open channel for trace %s", trace_name
);
560 goto unmap_buf_structs
;
566 for (i
=0; i
< chan
->n_cpus
; i
++) {
567 if (shmdt(chan
->buf
[i
]) < 0) {
568 PERROR("shmdt bufstruct");
572 free_buf_struct_shmids
:
573 free(chan
->buf_struct_shmids
);
583 static void remove_channel(struct ust_channel
*chan
)
587 unmap_buf_structs(chan
);
589 free(chan
->buf_struct_shmids
);
594 static void ltt_relay_async_wakeup_chan(struct ust_channel
*ltt_channel
)
598 static void ltt_relay_finish_buffer(struct ust_channel
*channel
, unsigned int cpu
)
600 if (channel
->buf
[cpu
]) {
601 struct ust_buffer
*buf
= channel
->buf
[cpu
];
602 ltt_force_switch(buf
, FORCE_FLUSH
);
604 /* closing the pipe tells the consumer the buffer is finished */
605 close(buf
->data_ready_fd_write
);
610 static void finish_channel(struct ust_channel
*channel
)
614 for (i
=0; i
<channel
->n_cpus
; i
++) {
615 ltt_relay_finish_buffer(channel
, i
);
621 * ltt_reserve_switch_old_subbuf: switch old subbuffer
623 * Concurrency safe because we are the last and only thread to alter this
624 * sub-buffer. As long as it is not delivered and read, no other thread can
625 * alter the offset, alter the reserve_count or call the
626 * client_buffer_end_callback on this sub-buffer.
628 * The only remaining threads could be the ones with pending commits. They will
629 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
630 * We detect corrupted subbuffers with commit and reserve counts. We keep a
631 * corrupted sub-buffers count and push the readers across these sub-buffers.
633 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
634 * switches in, finding out it's corrupted. The result will be than the old
635 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
636 * will be declared corrupted too because of the commit count adjustment.
638 * Note : offset_old should never be 0 here.
640 static void ltt_reserve_switch_old_subbuf(
641 struct ust_channel
*chan
, struct ust_buffer
*buf
,
642 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
644 long oldidx
= SUBBUF_INDEX(offsets
->old
- 1, chan
);
645 long commit_count
, padding_size
;
647 padding_size
= chan
->subbuf_size
648 - (SUBBUF_OFFSET(offsets
->old
- 1, chan
) + 1);
649 ltt_buffer_end(buf
, *tsc
, offsets
->old
, oldidx
);
652 * Must write slot data before incrementing commit count.
653 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
654 * sent by get_subbuf() when it does its cmm_smp_rmb().
657 uatomic_add(&buf
->commit_count
[oldidx
].cc
, padding_size
);
658 commit_count
= uatomic_read(&buf
->commit_count
[oldidx
].cc
);
659 ltt_check_deliver(chan
, buf
, offsets
->old
- 1, commit_count
, oldidx
);
660 ltt_write_commit_counter(chan
, buf
, oldidx
,
661 offsets
->old
, commit_count
, padding_size
);
665 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
667 * This code can be executed unordered : writers may already have written to the
668 * sub-buffer before this code gets executed, caution. The commit makes sure
669 * that this code is executed before the deliver of this sub-buffer.
671 static void ltt_reserve_switch_new_subbuf(
672 struct ust_channel
*chan
, struct ust_buffer
*buf
,
673 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
675 long beginidx
= SUBBUF_INDEX(offsets
->begin
, chan
);
678 ltt_buffer_begin(buf
, *tsc
, beginidx
);
681 * Must write slot data before incrementing commit count.
682 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
683 * sent by get_subbuf() when it does its cmm_smp_rmb().
686 uatomic_add(&buf
->commit_count
[beginidx
].cc
, ltt_subbuffer_header_size());
687 commit_count
= uatomic_read(&buf
->commit_count
[beginidx
].cc
);
688 /* Check if the written buffer has to be delivered */
689 ltt_check_deliver(chan
, buf
, offsets
->begin
, commit_count
, beginidx
);
690 ltt_write_commit_counter(chan
, buf
, beginidx
,
691 offsets
->begin
, commit_count
, ltt_subbuffer_header_size());
695 * ltt_reserve_end_switch_current: finish switching current subbuffer
697 * Concurrency safe because we are the last and only thread to alter this
698 * sub-buffer. As long as it is not delivered and read, no other thread can
699 * alter the offset, alter the reserve_count or call the
700 * client_buffer_end_callback on this sub-buffer.
702 * The only remaining threads could be the ones with pending commits. They will
703 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
704 * We detect corrupted subbuffers with commit and reserve counts. We keep a
705 * corrupted sub-buffers count and push the readers across these sub-buffers.
707 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
708 * switches in, finding out it's corrupted. The result will be than the old
709 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
710 * will be declared corrupted too because of the commit count adjustment.
712 static void ltt_reserve_end_switch_current(
713 struct ust_channel
*chan
,
714 struct ust_buffer
*buf
,
715 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
717 long endidx
= SUBBUF_INDEX(offsets
->end
- 1, chan
);
718 long commit_count
, padding_size
;
720 padding_size
= chan
->subbuf_size
721 - (SUBBUF_OFFSET(offsets
->end
- 1, chan
) + 1);
723 ltt_buffer_end(buf
, *tsc
, offsets
->end
, endidx
);
726 * Must write slot data before incrementing commit count.
727 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
728 * sent by get_subbuf() when it does its cmm_smp_rmb().
731 uatomic_add(&buf
->commit_count
[endidx
].cc
, padding_size
);
732 commit_count
= uatomic_read(&buf
->commit_count
[endidx
].cc
);
733 ltt_check_deliver(chan
, buf
,
734 offsets
->end
- 1, commit_count
, endidx
);
735 ltt_write_commit_counter(chan
, buf
, endidx
,
736 offsets
->end
, commit_count
, padding_size
);
742 * !0 if execution must be aborted.
744 static int ltt_relay_try_switch_slow(
745 enum force_switch_mode mode
,
746 struct ust_channel
*chan
,
747 struct ust_buffer
*buf
,
748 struct ltt_reserve_switch_offsets
*offsets
,
752 long reserve_commit_diff
;
754 offsets
->begin
= uatomic_read(&buf
->offset
);
755 offsets
->old
= offsets
->begin
;
756 offsets
->begin_switch
= 0;
757 offsets
->end_switch_old
= 0;
759 *tsc
= trace_clock_read64();
761 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) != 0) {
762 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
, buf
->chan
);
763 offsets
->end_switch_old
= 1;
765 /* we do not have to switch : buffer is empty */
768 if (mode
== FORCE_ACTIVE
)
769 offsets
->begin
+= ltt_subbuffer_header_size();
771 * Always begin_switch in FORCE_ACTIVE mode.
772 * Test new buffer integrity
774 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
775 reserve_commit_diff
=
776 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
777 >> chan
->n_subbufs_order
)
778 - (uatomic_read(&buf
->commit_count
[subbuf_index
].cc_sb
)
779 & chan
->commit_count_mask
);
780 if (reserve_commit_diff
== 0) {
781 /* Next buffer not corrupted. */
782 if (mode
== FORCE_ACTIVE
784 && offsets
->begin
- uatomic_read(&buf
->consumed
)
785 >= chan
->alloc_size
) {
787 * We do not overwrite non consumed buffers and we are
788 * full : ignore switch while tracing is active.
794 * Next subbuffer corrupted. Force pushing reader even in normal
798 offsets
->end
= offsets
->begin
;
803 * Force a sub-buffer switch for a per-cpu buffer. This operation is
804 * completely reentrant : can be called while tracing is active with
805 * absolutely no lock held.
807 void ltt_force_switch_lockless_slow(struct ust_buffer
*buf
,
808 enum force_switch_mode mode
)
810 struct ust_channel
*chan
= buf
->chan
;
811 struct ltt_reserve_switch_offsets offsets
;
816 DBG("Switching (forced) %s_%d", chan
->channel_name
, buf
->cpu
);
818 * Perform retryable operations.
821 if (ltt_relay_try_switch_slow(mode
, chan
, buf
,
824 } while (uatomic_cmpxchg(&buf
->offset
, offsets
.old
,
825 offsets
.end
) != offsets
.old
);
828 * Atomically update last_tsc. This update races against concurrent
829 * atomic updates, but the race will always cause supplementary full TSC
830 * events, never the opposite (missing a full TSC event when it would be
833 save_last_tsc(buf
, tsc
);
836 * Push the reader if necessary
838 if (mode
== FORCE_ACTIVE
) {
839 ltt_reserve_push_reader(chan
, buf
, offsets
.end
- 1);
843 * Switch old subbuffer if needed.
845 if (offsets
.end_switch_old
) {
846 ltt_reserve_switch_old_subbuf(chan
, buf
, &offsets
, &tsc
);
850 * Populate new subbuffer.
852 if (mode
== FORCE_ACTIVE
)
853 ltt_reserve_switch_new_subbuf(chan
, buf
, &offsets
, &tsc
);
859 * !0 if execution must be aborted.
861 static int ltt_relay_try_reserve_slow(struct ust_channel
*chan
, struct ust_buffer
*buf
,
862 struct ltt_reserve_switch_offsets
*offsets
, size_t data_size
,
863 u64
*tsc
, unsigned int *rflags
, int largest_align
)
865 long reserve_commit_diff
;
867 offsets
->begin
= uatomic_read(&buf
->offset
);
868 offsets
->old
= offsets
->begin
;
869 offsets
->begin_switch
= 0;
870 offsets
->end_switch_current
= 0;
871 offsets
->end_switch_old
= 0;
873 *tsc
= trace_clock_read64();
874 if (last_tsc_overflow(buf
, *tsc
))
875 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
877 if (unlikely(SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) == 0)) {
878 offsets
->begin_switch
= 1; /* For offsets->begin */
880 offsets
->size
= ust_get_header_size(chan
,
881 offsets
->begin
, data_size
,
882 &offsets
->before_hdr_pad
, *rflags
);
883 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
886 if (unlikely((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) +
887 offsets
->size
) > buf
->chan
->subbuf_size
)) {
888 offsets
->end_switch_old
= 1; /* For offsets->old */
889 offsets
->begin_switch
= 1; /* For offsets->begin */
892 if (unlikely(offsets
->begin_switch
)) {
896 * We are typically not filling the previous buffer completely.
898 if (likely(offsets
->end_switch_old
))
899 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
,
901 offsets
->begin
= offsets
->begin
+ ltt_subbuffer_header_size();
902 /* Test new buffer integrity */
903 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
904 reserve_commit_diff
=
905 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
906 >> chan
->n_subbufs_order
)
907 - (uatomic_read(&buf
->commit_count
[subbuf_index
].cc_sb
)
908 & chan
->commit_count_mask
);
909 if (likely(reserve_commit_diff
== 0)) {
910 /* Next buffer not corrupted. */
911 if (unlikely(!chan
->overwrite
&&
912 (SUBBUF_TRUNC(offsets
->begin
, buf
->chan
)
913 - SUBBUF_TRUNC(uatomic_read(
916 >= chan
->alloc_size
)) {
918 * We do not overwrite non consumed buffers
919 * and we are full : event is lost.
921 uatomic_inc(&buf
->events_lost
);
925 * next buffer not corrupted, we are either in
926 * overwrite mode or the buffer is not full.
927 * It's safe to write in this new subbuffer.
932 * Next subbuffer corrupted. Drop event in normal and
933 * overwrite mode. Caused by either a writer OOPS or
934 * too many nested writes over a reserve/commit pair.
936 uatomic_inc(&buf
->events_lost
);
939 offsets
->size
= ust_get_header_size(chan
,
940 offsets
->begin
, data_size
,
941 &offsets
->before_hdr_pad
, *rflags
);
942 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
945 if (unlikely((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
)
946 + offsets
->size
) > buf
->chan
->subbuf_size
)) {
948 * Event too big for subbuffers, report error, don't
949 * complete the sub-buffer switch.
951 uatomic_inc(&buf
->events_lost
);
955 * We just made a successful buffer switch and the event
956 * fits in the new subbuffer. Let's write.
961 * Event fits in the current buffer and we are not on a switch
962 * boundary. It's safe to write.
965 offsets
->end
= offsets
->begin
+ offsets
->size
;
967 if (unlikely((SUBBUF_OFFSET(offsets
->end
, buf
->chan
)) == 0)) {
969 * The offset_end will fall at the very beginning of the next
972 offsets
->end_switch_current
= 1; /* For offsets->begin */
978 * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer.
979 * @trace: the trace structure to log to.
980 * @ltt_channel: channel structure
981 * @transport_data: data structure specific to ltt relay
982 * @data_size: size of the variable length data to log.
983 * @slot_size: pointer to total size of the slot (out)
984 * @buf_offset : pointer to reserved buffer offset (out)
985 * @tsc: pointer to the tsc at the slot reservation (out)
988 * Return : -ENOSPC if not enough space, else returns 0.
989 * It will take care of sub-buffer switching.
991 int ltt_reserve_slot_lockless_slow(struct ust_channel
*chan
,
992 struct ust_trace
*trace
, size_t data_size
,
993 int largest_align
, int cpu
,
994 struct ust_buffer
**ret_buf
,
995 size_t *slot_size
, long *buf_offset
,
996 u64
*tsc
, unsigned int *rflags
)
998 struct ust_buffer
*buf
= *ret_buf
= chan
->buf
[cpu
];
999 struct ltt_reserve_switch_offsets offsets
;
1004 if (unlikely(ltt_relay_try_reserve_slow(chan
, buf
, &offsets
,
1005 data_size
, tsc
, rflags
, largest_align
)))
1007 } while (unlikely(uatomic_cmpxchg(&buf
->offset
, offsets
.old
,
1008 offsets
.end
) != offsets
.old
));
1011 * Atomically update last_tsc. This update races against concurrent
1012 * atomic updates, but the race will always cause supplementary full TSC
1013 * events, never the opposite (missing a full TSC event when it would be
1016 save_last_tsc(buf
, *tsc
);
1019 * Push the reader if necessary
1021 ltt_reserve_push_reader(chan
, buf
, offsets
.end
- 1);
1024 * Switch old subbuffer if needed.
1026 if (unlikely(offsets
.end_switch_old
)) {
1027 ltt_reserve_switch_old_subbuf(chan
, buf
, &offsets
, tsc
);
1028 DBG("Switching %s_%d", chan
->channel_name
, cpu
);
1032 * Populate new subbuffer.
1034 if (unlikely(offsets
.begin_switch
))
1035 ltt_reserve_switch_new_subbuf(chan
, buf
, &offsets
, tsc
);
1037 if (unlikely(offsets
.end_switch_current
))
1038 ltt_reserve_end_switch_current(chan
, buf
, &offsets
, tsc
);
1040 *slot_size
= offsets
.size
;
1041 *buf_offset
= offsets
.begin
+ offsets
.before_hdr_pad
;
1045 static struct ltt_transport ust_relay_transport
= {
1048 .create_channel
= create_channel
,
1049 .finish_channel
= finish_channel
,
1050 .remove_channel
= remove_channel
,
1051 .wakeup_channel
= ltt_relay_async_wakeup_chan
,
1055 static char initialized
= 0;
1057 void __attribute__((constructor
)) init_ustrelay_transport(void)
1061 ltt_transport_register(&ust_relay_transport
);
1066 static void __attribute__((destructor
)) ust_buffers_exit(void)
1068 ltt_transport_unregister(&ust_relay_transport
);
1071 size_t ltt_write_event_header_slow(struct ust_channel
*channel
,
1072 struct ust_buffer
*buf
, long buf_offset
,
1073 u16 eID
, u32 event_size
,
1074 u64 tsc
, unsigned int rflags
)
1076 struct ltt_event_header header
;
1080 case LTT_RFLAG_ID_SIZE_TSC
:
1081 header
.id_time
= 29 << LTT_TSC_BITS
;
1083 case LTT_RFLAG_ID_SIZE
:
1084 header
.id_time
= 30 << LTT_TSC_BITS
;
1087 header
.id_time
= 31 << LTT_TSC_BITS
;
1095 header
.id_time
|= (u32
)tsc
& LTT_TSC_MASK
;
1096 ust_buffers_write(buf
, buf_offset
, &header
, sizeof(header
));
1097 buf_offset
+= sizeof(header
);
1100 case LTT_RFLAG_ID_SIZE_TSC
:
1101 small_size
= (u16
)min_t(u32
, event_size
, LTT_MAX_SMALL_SIZE
);
1102 ust_buffers_write(buf
, buf_offset
,
1104 buf_offset
+= sizeof(u16
);
1105 ust_buffers_write(buf
, buf_offset
,
1106 &small_size
, sizeof(u16
));
1107 buf_offset
+= sizeof(u16
);
1108 if (small_size
== LTT_MAX_SMALL_SIZE
) {
1109 ust_buffers_write(buf
, buf_offset
,
1110 &event_size
, sizeof(u32
));
1111 buf_offset
+= sizeof(u32
);
1113 buf_offset
+= ltt_align(buf_offset
, sizeof(u64
));
1114 ust_buffers_write(buf
, buf_offset
,
1116 buf_offset
+= sizeof(u64
);
1118 case LTT_RFLAG_ID_SIZE
:
1119 small_size
= (u16
)min_t(u32
, event_size
, LTT_MAX_SMALL_SIZE
);
1120 ust_buffers_write(buf
, buf_offset
,
1122 buf_offset
+= sizeof(u16
);
1123 ust_buffers_write(buf
, buf_offset
,
1124 &small_size
, sizeof(u16
));
1125 buf_offset
+= sizeof(u16
);
1126 if (small_size
== LTT_MAX_SMALL_SIZE
) {
1127 ust_buffers_write(buf
, buf_offset
,
1128 &event_size
, sizeof(u32
));
1129 buf_offset
+= sizeof(u32
);
1133 ust_buffers_write(buf
, buf_offset
,
1135 buf_offset
+= sizeof(u16
);