Import lttng.kernel.core plugins from Scope
[deliverable/tracecompass.git] / lttng / org.lttng.scope.lttng.kernel.core.tests / src / org / lttng / scope / lttng / kernel / core / synchronization / UstKernelSyncTest.java
CommitLineData
451ba2f7
AM
1/*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
3 *
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 *******************************************************************************/
9
10package org.lttng.scope.lttng.kernel.core.synchronization;
11
12import static org.junit.Assert.assertEquals;
13import static org.junit.Assert.assertNotNull;
14
15import java.util.Objects;
16import java.util.concurrent.TimeUnit;
17import java.util.function.Predicate;
18
19import org.eclipse.jdt.annotation.NonNull;
20import org.eclipse.tracecompass.ctf.tmf.core.event.CtfTmfEvent;
21import org.eclipse.tracecompass.ctf.tmf.core.tests.shared.CtfTmfTestTraceUtils;
22import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
23import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
24import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
25import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
26import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
27import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
28import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
29import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
30import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Ignore;
34import org.junit.Rule;
35import org.junit.Test;
36import org.junit.rules.TestRule;
37import org.junit.rules.Timeout;
38import org.lttng.scope.lttng.kernel.core.analysis.os.KernelAnalysisModule;
39import org.lttng.scope.lttng.kernel.core.analysis.os.KernelThreadInformationProvider;
40import org.lttng.scope.lttng.kernel.core.tests.shared.LttngKernelTestTraceUtils;
41
42/**
43 * Test that synchronization between LTTng UST and kernel traces is done
44 * correctly.
45 *
46 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=484620
47 *
48 * @author Alexandre Montplaisir
49 */
50@Ignore("Collection-wide synchronization is unsupported at the moment")
51public class UstKernelSyncTest {
52
53 /** Time-out tests after 60 seconds */
54 @Rule public TestRule globalTimeout= new Timeout(60, TimeUnit.SECONDS);
55
56 private static final @NonNull CtfTestTrace KERNEL_TRACE = CtfTestTrace.CONTEXT_SWITCHES_KERNEL;
57 private static final @NonNull CtfTestTrace UST_TRACE = CtfTestTrace.CONTEXT_SWITCHES_UST;
58
59 private TmfExperiment fExperiment;
60 private ITmfTrace fUstTrace;
61 private KernelAnalysisModule fKernelModule;
62
63 /**
64 * Test setup
65 */
66 @Before
67 public void setup() {
68 ITmfTrace ustTrace = CtfTmfTestTraceUtils.getTrace(UST_TRACE);
69 ITmfTrace kernelTrace = LttngKernelTestTraceUtils.getTrace(KERNEL_TRACE);
70
71 TmfExperiment experiment = new TmfExperiment(CtfTmfEvent.class,
72 "test-exp",
73 new ITmfTrace[] { ustTrace, kernelTrace },
74 TmfExperiment.DEFAULT_INDEX_PAGE_SIZE,
75 null);
76
77 /* Simulate experiment being opened */
78 TmfSignalManager.dispatchSignal(new TmfTraceOpenedSignal(this, experiment, null));
79 TmfSignalManager.dispatchSignal(new TmfTraceSelectedSignal(this, experiment));
80
81 KernelAnalysisModule module = TmfTraceUtils.getAnalysisModuleOfClass(experiment,
82 KernelAnalysisModule.class, KernelAnalysisModule.ID);
83 assertNotNull(module);
84 module.waitForCompletion();
85
86 fExperiment = experiment;
87 fUstTrace = ustTrace;
88 fKernelModule = module;
89 }
90
91 /**
92 * Test teardown
93 */
94 @After
95 public void tearDown() {
96 if (fExperiment != null) {
97 fExperiment.dispose();
98 }
99 CtfTmfTestTraceUtils.dispose(UST_TRACE);
100 LttngKernelTestTraceUtils.dispose(KERNEL_TRACE);
101 }
102
103 /**
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.
106 *
107 * Reproduces the specific example that was pointed out in bug 484620.
108 */
109 @Test
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);
117
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");
122
123 if (addr == null || cs == null || ctxVtid == null) {
124 return false;
125 }
126
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);
131 };
132
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);
139
140 assertEquals(ustTrace, ustEvent.getTrace());
141
142 Integer tidFromKernel = KernelThreadInformationProvider.getThreadOnCpu(module,
143 ustEvent.getCPU(), ustEvent.getTimestamp().toNanos());
144
145 assertNotNull(tidFromKernel);
146 assertEquals(594, tidFromKernel.intValue());
147 }
148
149 /**
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
152 * same timestamp.
153 */
154 @Test
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);
162
163 ITmfContext context = ustTrace.seekEvent(0L);
164 CtfTmfEvent ustEvent = (CtfTmfEvent) ustTrace.getNext(context);
165 int count = 0;
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);
170
171 long ts = ustEvent.getTimestamp().toNanos();
172 long cpu = ustEvent.getCPU();
173 Integer kernelTid = KernelThreadInformationProvider.getThreadOnCpu(module, cpu, ts);
174 assertNotNull(kernelTid);
175
176 assertEquals("Wrong TID for trace event " + ustEvent.toString(), ustVtid.longValue(), kernelTid.longValue());
177
178 ustEvent = (CtfTmfEvent) ustTrace.getNext(context);
179 count++;
180 }
181
182 /* Make sure we've read all expected events */
183 assertEquals(UST_TRACE.getNbEvents(), count);
184 }
185}
This page took 0.03023 seconds and 5 git commands to generate.