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
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.views
.controlflow2
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
14 import static org
.junit
.Assert
.fail
;
16 import java
.util
.List
;
17 import java
.util
.stream
.Collectors
;
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
;
46 * Tests for {@link ControlFlowRenderProvider}.
48 * @author Alexandre Montplaisir
50 public class ControlFlowRenderProviderTest
{
52 // /** Timeout the tests after 2 minutes */
54 // public TestRule timeoutRule = new Timeout(2, TimeUnit.MINUTES);
56 private static final long NANOS_PER_SECOND
= 1000000000L;
58 private static final @NonNull CtfTestTrace TEST_TRACE
= CtfTestTrace
.KERNEL
;
60 private static ITmfTrace sfTrace
;
61 private static ITmfStateSystem sfSS
;
63 private ControlFlowRenderProvider provider
= new ControlFlowRenderProvider();
69 public void setupClass() {
70 TmfTrace trace
= KernelCtfTraceStub
.getTrace(TEST_TRACE
);
71 trace
.traceOpened(new TmfTraceOpenedSignal(ControlFlowRenderProviderTest
.class, trace
, null));
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();
78 ITmfStateSystem ss
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
89 public void teardownClass() {
90 if (sfTrace
!= null) {
91 /* Trace's dispose will dispose its state systems */
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.
101 public void test1s() {
103 final ITmfTrace trace
= sfTrace
;
104 final ITmfStateSystem ss
= sfSS
;
105 assertNotNull(trace
);
108 final long start
= trace
.getStartTime().toNanos();
109 final long end
= start
+ 1 * NANOS_PER_SECOND
;
111 TimeGraphModelRender modelRender
= provider
.getRender(trace
, start
, end
, 1);
113 /* Check that the list of attributes (tree render) are the same */
114 TimeGraphTreeRender treeRender
= modelRender
.getTreeRender();
115 List
<TimeGraphTreeElement
> treeElems
= treeRender
.getAllTreeElements();
117 List
<String
> tidsFromRender
= treeElems
.stream()
118 .map(e
-> (ControlFlowTreeElement
) e
)
119 .mapToInt(ControlFlowTreeElement
::getTid
)
120 .mapToObj(tid
-> String
.valueOf(tid
))
122 .collect(Collectors
.toList());
124 int threadsQuark
= ss
.getQuarkAbsolute(Attributes
.THREADS
);
125 List
<String
> tidsFromSS
= ss
.getSubAttributes(threadsQuark
, false).stream()
126 .map(quark
-> ss
.getAttributeName(quark
))
128 if (name
.startsWith(Attributes
.THREAD_0_PREFIX
)) {
134 .collect(Collectors
.toList());
136 assertEquals(tidsFromSS
, tidsFromRender
);
137 // TODO Also verify against known hard-coded list
140 /* Check that the state intervals are the same */
141 List
<String
> tidsInSS
= ss
.getQuarks(threadsQuark
, "*").stream()
142 .map(ss
::getAttributeName
)
144 .collect(Collectors
.toList());
146 for (String tid
: tidsInSS
) {
147 int threadQuark
= ss
.getQuarkRelative(threadsQuark
, tid
);
148 List
<ITmfStateInterval
> intervalsFromSS
=
149 StateSystemUtils
.queryHistoryRange(ss
, threadQuark
, start
, end
);
151 TimeGraphTreeElement elem
= treeElems
.stream()
152 .map(e
-> (ControlFlowTreeElement
) e
)
153 .filter(e
-> e
.getSourceQuark() == threadQuark
)
157 int index
= treeElems
.indexOf(elem
);
158 List
<TimeGraphStateInterval
> intervalsFromRender
= modelRender
.getStateIntervals().get(index
);
160 verifySameIntervals(intervalsFromSS
, intervalsFromRender
);
161 // TODO Also verify against known hard-coded list
164 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
165 fail(e
.getMessage());
169 private static void verifySameIntervals(List
<ITmfStateInterval
> ssIntervals
,
170 List
<TimeGraphStateInterval
> renderIntervals
) {
171 assertEquals(ssIntervals
.size(), renderIntervals
.size());
173 for (int i
= 0; i
< ssIntervals
.size(); i
++) {
174 ITmfStateInterval ssInterval
= ssIntervals
.get(i
);
175 TimeGraphStateInterval renderInterval
= renderIntervals
.get(i
);
177 assertEquals(ssInterval
.getStartTime(), renderInterval
.getStartEvent().getTimestamp());
178 assertEquals(ssInterval
.getEndTime(), renderInterval
.getEndEvent().getTimestamp());
180 int stateValue
= ssInterval
.getStateValue().unboxInt();
181 String stateName
= ControlFlowRenderProvider
.mapStateValueToStateName(stateValue
);
182 assertEquals(stateName
, renderInterval
.getStateName());