[WIP] CFV Refactor
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core.tests / src / org / eclipse / tracecompass / analysis / os / linux / core / tests / views / controlflow2 / ControlFlowRenderProviderTest.java
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
10 package org.eclipse.tracecompass.analysis.os.linux.core.tests.views.controlflow2;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.fail;
15
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
21 import org.eclipse.tracecompass.analysis.os.linux.core.tests.stubs.trace.KernelCtfTraceStub;
22 import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
23 import org.eclipse.tracecompass.internal.analysis.os.linux.core.views.controlflow2.ControlFlowRenderProvider;
24 import org.eclipse.tracecompass.internal.analysis.os.linux.core.views.controlflow2.ControlFlowTreeElement;
25 import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.TimeGraphModelRender;
26 import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.TimeGraphStateInterval;
27 import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.TimeGraphTreeElement;
28 import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.TimeGraphTreeRender;
29 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
30 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
31 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
33 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
34 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
35 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
36 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
37 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
38 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
39 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
40 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44
45 /**
46 * Tests for {@link ControlFlowRenderProvider}.
47 *
48 * @author Alexandre Montplaisir
49 */
50 public class ControlFlowRenderProviderTest {
51
52 // /** Timeout the tests after 2 minutes */
53 // @Rule
54 // public TestRule timeoutRule = new Timeout(2, TimeUnit.MINUTES);
55
56 private static final long NANOS_PER_SECOND = 1000000000L;
57
58 private static final @NonNull CtfTestTrace TEST_TRACE = CtfTestTrace.KERNEL;
59
60 private static ITmfTrace sfTrace;
61 private static ITmfStateSystem sfSS;
62
63 private ControlFlowRenderProvider provider = new ControlFlowRenderProvider();
64
65 /**
66 * Test class setup
67 */
68 @Before
69 public void setupClass() {
70 TmfTrace trace = KernelCtfTraceStub.getTrace(TEST_TRACE);
71 trace.traceOpened(new TmfTraceOpenedSignal(ControlFlowRenderProviderTest.class, trace, null));
72
73 IAnalysisModule analysis = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelAnalysisModule.class, KernelAnalysisModule.ID);
74 assertNotNull(analysis);
75 analysis.schedule(); // Should have run, just in case
76 analysis.waitForCompletion();
77
78 ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
79 assertNotNull(ss);
80
81 sfTrace = trace;
82 sfSS = ss;
83 }
84
85 /**
86 * Test class teardown
87 */
88 @After
89 public void teardownClass() {
90 if (sfTrace != null) {
91 /* Trace's dispose will dispose its state systems */
92 sfTrace.dispose();
93 }
94 }
95
96 /**
97 * Check that the info in a render for the first second of the trace matches
98 * the corresponding info found in the state system.
99 */
100 @Test
101 public void test1s() {
102 try {
103 final ITmfTrace trace = sfTrace;
104 final ITmfStateSystem ss = sfSS;
105 assertNotNull(trace);
106 assertNotNull(ss);
107
108 final long start = trace.getStartTime().toNanos();
109 final long end = start + 1 * NANOS_PER_SECOND;
110
111 TimeGraphModelRender modelRender = provider.getRender(trace, start, end, 1);
112
113 /* Check that the list of attributes (tree render) are the same */
114 TimeGraphTreeRender treeRender = modelRender.getTreeRender();
115 List<TimeGraphTreeElement> treeElems = treeRender.getAllTreeElements();
116
117 List<String> tidsFromRender = treeElems.stream()
118 .map(e -> (ControlFlowTreeElement) e)
119 .mapToInt(ControlFlowTreeElement::getTid)
120 .mapToObj(tid -> String.valueOf(tid))
121 .sorted()
122 .collect(Collectors.toList());
123
124 int threadsQuark = ss.getQuarkAbsolute(Attributes.THREADS);
125 List<String> tidsFromSS = ss.getSubAttributes(threadsQuark, false).stream()
126 .map(quark -> ss.getAttributeName(quark))
127 .map(name -> {
128 if (name.startsWith(Attributes.THREAD_0_PREFIX)) {
129 return "0";
130 }
131 return name;
132 })
133 .sorted()
134 .collect(Collectors.toList());
135
136 assertEquals(tidsFromSS, tidsFromRender);
137 // TODO Also verify against known hard-coded list
138
139
140 /* Check that the state intervals are the same */
141 List<String> tidsInSS = ss.getQuarks(threadsQuark, "*").stream()
142 .map(ss::getAttributeName)
143 .sorted()
144 .collect(Collectors.toList());
145
146 for (String tid : tidsInSS) {
147 int threadQuark = ss.getQuarkRelative(threadsQuark, tid);
148 List<ITmfStateInterval> intervalsFromSS =
149 StateSystemUtils.queryHistoryRange(ss, threadQuark, start, end);
150
151 TimeGraphTreeElement elem = treeElems.stream()
152 .map(e -> (ControlFlowTreeElement) e)
153 .filter(e -> e.getSourceQuark() == threadQuark)
154 .findFirst()
155 .get();
156
157 int index = treeElems.indexOf(elem);
158 List<TimeGraphStateInterval> intervalsFromRender = modelRender.getStateIntervals().get(index);
159
160 verifySameIntervals(intervalsFromSS, intervalsFromRender);
161 // TODO Also verify against known hard-coded list
162 }
163
164 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
165 fail(e.getMessage());
166 }
167 }
168
169 private static void verifySameIntervals(List<ITmfStateInterval> ssIntervals,
170 List<TimeGraphStateInterval> renderIntervals) {
171 assertEquals(ssIntervals.size(), renderIntervals.size());
172
173 for (int i = 0; i < ssIntervals.size(); i++) {
174 ITmfStateInterval ssInterval = ssIntervals.get(i);
175 TimeGraphStateInterval renderInterval = renderIntervals.get(i);
176
177 assertEquals(ssInterval.getStartTime(), renderInterval.getStartEvent().getTimestamp());
178 assertEquals(ssInterval.getEndTime(), renderInterval.getEndEvent().getTimestamp());
179
180 int stateValue = ssInterval.getStateValue().unboxInt();
181 String stateName = ControlFlowRenderProvider.mapStateValueToStateName(stateValue);
182 assertEquals(stateName, renderInterval.getStateName());
183 }
184 }
185 }
This page took 0.034718 seconds and 5 git commands to generate.