1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.lttng
.scope
.lttng
.kernel
.core
.synchronization
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
15 import java
.util
.Objects
;
16 import java
.util
.concurrent
.TimeUnit
;
17 import java
.util
.function
.Predicate
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.tracecompass
.ctf
.tmf
.core
.event
.CtfTmfEvent
;
21 import org
.eclipse
.tracecompass
.ctf
.tmf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
22 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
31 import org
.junit
.After
;
32 import org
.junit
.Before
;
33 import org
.junit
.Ignore
;
34 import org
.junit
.Rule
;
35 import org
.junit
.Test
;
36 import org
.junit
.rules
.TestRule
;
37 import org
.junit
.rules
.Timeout
;
38 import org
.lttng
.scope
.lttng
.kernel
.core
.analysis
.os
.KernelAnalysisModule
;
39 import org
.lttng
.scope
.lttng
.kernel
.core
.analysis
.os
.KernelThreadInformationProvider
;
40 import org
.lttng
.scope
.lttng
.kernel
.core
.tests
.shared
.LttngKernelTestTraceUtils
;
43 * Test that synchronization between LTTng UST and kernel traces is done
46 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=484620
48 * @author Alexandre Montplaisir
50 @Ignore("Collection-wide synchronization is unsupported at the moment")
51 public class UstKernelSyncTest
{
53 /** Time-out tests after 60 seconds */
54 @Rule public TestRule globalTimeout
= new Timeout(60, TimeUnit
.SECONDS
);
56 private static final @NonNull CtfTestTrace KERNEL_TRACE
= CtfTestTrace
.CONTEXT_SWITCHES_KERNEL
;
57 private static final @NonNull CtfTestTrace UST_TRACE
= CtfTestTrace
.CONTEXT_SWITCHES_UST
;
59 private TmfExperiment fExperiment
;
60 private ITmfTrace fUstTrace
;
61 private KernelAnalysisModule fKernelModule
;
68 ITmfTrace ustTrace
= CtfTmfTestTraceUtils
.getTrace(UST_TRACE
);
69 ITmfTrace kernelTrace
= LttngKernelTestTraceUtils
.getTrace(KERNEL_TRACE
);
71 TmfExperiment experiment
= new TmfExperiment(CtfTmfEvent
.class,
73 new ITmfTrace
[] { ustTrace
, kernelTrace
},
74 TmfExperiment
.DEFAULT_INDEX_PAGE_SIZE
,
77 /* Simulate experiment being opened */
78 TmfSignalManager
.dispatchSignal(new TmfTraceOpenedSignal(this, experiment
, null));
79 TmfSignalManager
.dispatchSignal(new TmfTraceSelectedSignal(this, experiment
));
81 KernelAnalysisModule module
= TmfTraceUtils
.getAnalysisModuleOfClass(experiment
,
82 KernelAnalysisModule
.class, KernelAnalysisModule
.ID
);
83 assertNotNull(module
);
84 module
.waitForCompletion();
86 fExperiment
= experiment
;
88 fKernelModule
= module
;
95 public void tearDown() {
96 if (fExperiment
!= null) {
97 fExperiment
.dispose();
99 CtfTmfTestTraceUtils
.dispose(UST_TRACE
);
100 LttngKernelTestTraceUtils
.dispose(KERNEL_TRACE
);
104 * Test that the TID given by the kernel analysis matches the one from the
105 * UST event's context for a given UST event that was known to fail.
107 * Reproduces the specific example that was pointed out in bug 484620.
110 public void testOneEvent() {
111 TmfExperiment experiment
= fExperiment
;
112 ITmfTrace ustTrace
= fUstTrace
;
113 KernelAnalysisModule module
= fKernelModule
;
114 assertNotNull(experiment
);
115 assertNotNull(ustTrace
);
116 assertNotNull(module
);
118 Predicate
<@NonNull ITmfEvent
> eventFinder
= event
-> {
119 Long addr
= event
.getContent().getFieldValue(Long
.class, "addr");
120 Long cs
= event
.getContent().getFieldValue(Long
.class, "call_site");
121 Long ctxVtid
= event
.getContent().getFieldValue(Long
.class, "context._vtid");
123 if (addr
== null || cs
== null || ctxVtid
== null) {
127 return Objects
.equals(event
.getType().getName(), "lttng_ust_cyg_profile:func_entry") &&
128 Objects
.equals(Long
.toHexString(addr
), "804af97") &&
129 Objects
.equals(Long
.toHexString(cs
), "804ab03") &&
130 Objects
.equals(ctxVtid
.longValue(), 594L);
133 /* The event we're looking for is the second event matching the predicate */
134 CtfTmfEvent ustEvent
= (CtfTmfEvent
) TmfTraceUtils
.getNextEventMatching(experiment
, 0, eventFinder
, null);
135 assertNotNull(ustEvent
);
136 long rank
= experiment
.seekEvent(ustEvent
.getTimestamp()).getRank() + 1;
137 ustEvent
= (CtfTmfEvent
) TmfTraceUtils
.getNextEventMatching(experiment
, rank
, eventFinder
, null);
138 assertNotNull(ustEvent
);
140 assertEquals(ustTrace
, ustEvent
.getTrace());
142 Integer tidFromKernel
= KernelThreadInformationProvider
.getThreadOnCpu(module
,
143 ustEvent
.getCPU(), ustEvent
.getTimestamp().toNanos());
145 assertNotNull(tidFromKernel
);
146 assertEquals(594, tidFromKernel
.intValue());
150 * Test going through the whole UST trace, making sure the VTID context of
151 * each event corresponds to the TID given by the kernel analysis at the
155 public void testWholeUstTrace() {
156 TmfExperiment experiment
= fExperiment
;
157 ITmfTrace ustTrace
= fUstTrace
;
158 KernelAnalysisModule module
= fKernelModule
;
159 assertNotNull(experiment
);
160 assertNotNull(ustTrace
);
161 assertNotNull(module
);
163 ITmfContext context
= ustTrace
.seekEvent(0L);
164 CtfTmfEvent ustEvent
= (CtfTmfEvent
) ustTrace
.getNext(context
);
166 while (ustEvent
!= null) {
167 Long ustVtid
= ustEvent
.getContent().getFieldValue(Long
.class, "context._vtid");
168 /* All events in the trace should have that context */
169 assertNotNull(ustVtid
);
171 long ts
= ustEvent
.getTimestamp().toNanos();
172 long cpu
= ustEvent
.getCPU();
173 Integer kernelTid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, cpu
, ts
);
174 assertNotNull(kernelTid
);
176 assertEquals("Wrong TID for trace event " + ustEvent
.toString(), ustVtid
.longValue(), kernelTid
.longValue());
178 ustEvent
= (CtfTmfEvent
) ustTrace
.getNext(context
);
182 /* Make sure we've read all expected events */
183 assertEquals(UST_TRACE
.getNbEvents(), count
);