Commit | Line | Data |
---|---|---|
8192f2c6 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
8192f2c6 AM |
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 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.statistics; |
8192f2c6 AM |
14 | |
15 | import java.util.LinkedList; | |
16 | import java.util.List; | |
8f0cd315 | 17 | import java.util.concurrent.CountDownLatch; |
8192f2c6 AM |
18 | |
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | import org.eclipse.core.runtime.IStatus; | |
1d83ed07 | 21 | import org.eclipse.jdt.annotation.NonNull; |
8f0cd315 | 22 | import org.eclipse.jdt.annotation.Nullable; |
e894a508 | 23 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
2bdf0193 AM |
24 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; |
25 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; | |
26 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems; | |
27 | import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule; | |
28 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
8192f2c6 AM |
29 | |
30 | /** | |
31 | * Analysis module to compute the statistics of a trace. | |
32 | * | |
33 | * @author Alexandre Montplaisir | |
8192f2c6 AM |
34 | */ |
35 | public class TmfStatisticsModule extends TmfAbstractAnalysisModule | |
36 | implements ITmfAnalysisModuleWithStateSystems { | |
37 | ||
38 | /** ID of this analysis module */ | |
1d83ed07 | 39 | public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.core.statistics.analysis"; //$NON-NLS-1$ |
8192f2c6 AM |
40 | |
41 | /** The trace's statistics */ | |
42 | private ITmfStatistics fStatistics = null; | |
c81ffdf2 | 43 | private boolean fInitializationSucceeded; |
8192f2c6 AM |
44 | |
45 | private final TmfStateSystemAnalysisModule totalsModule = new TmfStatisticsTotalsModule(); | |
46 | private final TmfStateSystemAnalysisModule eventTypesModule = new TmfStatisticsEventTypesModule(); | |
47 | ||
8f0cd315 AM |
48 | private final CountDownLatch fInitialized = new CountDownLatch(1); |
49 | ||
8192f2c6 AM |
50 | /** |
51 | * Constructor | |
52 | */ | |
53 | public TmfStatisticsModule() { | |
54 | super(); | |
8192f2c6 AM |
55 | } |
56 | ||
57 | /** | |
58 | * Get the statistics object built by this analysis | |
59 | * | |
60 | * @return The ITmfStatistics object | |
61 | */ | |
8f0cd315 | 62 | @Nullable |
8192f2c6 AM |
63 | public ITmfStatistics getStatistics() { |
64 | return fStatistics; | |
65 | } | |
66 | ||
8f0cd315 AM |
67 | /** |
68 | * Wait until the analyses/state systems underneath are ready to be queried. | |
c81ffdf2 | 69 | * @since 2.0 |
8f0cd315 | 70 | */ |
6537ccdd | 71 | @Override |
c81ffdf2 | 72 | public boolean waitForInitialization() { |
8f0cd315 AM |
73 | try { |
74 | fInitialized.await(); | |
c81ffdf2 JCK |
75 | } catch (InterruptedException e) { |
76 | return false; | |
77 | } | |
78 | return fInitializationSucceeded; | |
8f0cd315 AM |
79 | } |
80 | ||
8192f2c6 AM |
81 | // ------------------------------------------------------------------------ |
82 | // TmfAbstractAnalysisModule | |
83 | // ------------------------------------------------------------------------ | |
84 | ||
43355455 AM |
85 | @Override |
86 | public void dispose() { | |
87 | /* | |
88 | * The sub-analyses are not registered to the trace directly, so we need | |
89 | * to tell them when the trace is disposed. | |
90 | */ | |
91 | super.dispose(); | |
92 | totalsModule.dispose(); | |
93 | eventTypesModule.dispose(); | |
94 | } | |
95 | ||
8192f2c6 | 96 | @Override |
f479550c GB |
97 | public boolean setTrace(ITmfTrace trace) throws TmfAnalysisException { |
98 | if (!super.setTrace(trace)) { | |
99 | return false; | |
100 | } | |
8192f2c6 AM |
101 | |
102 | /* | |
103 | * Since these sub-analyzes are not built from an extension point, we | |
104 | * have to assign the trace ourselves. Very important to do so before | |
105 | * calling schedule()! | |
106 | */ | |
f479550c GB |
107 | if (!totalsModule.setTrace(trace)) { |
108 | return false; | |
109 | } | |
110 | if (!eventTypesModule.setTrace(trace)) { | |
111 | return false; | |
112 | } | |
113 | return true; | |
ba220ab1 AM |
114 | } |
115 | ||
116 | @Override | |
117 | protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException { | |
118 | ITmfTrace trace = getTrace(); | |
119 | if (trace == null) { | |
e1c415b3 | 120 | /* This analysis was cancelled in the meantime */ |
fa6e7869 | 121 | analysisReady(false); |
e1c415b3 | 122 | return false; |
ba220ab1 | 123 | } |
8192f2c6 AM |
124 | |
125 | IStatus status1 = totalsModule.schedule(); | |
126 | IStatus status2 = eventTypesModule.schedule(); | |
ba220ab1 AM |
127 | if (!(status1.isOK() && status2.isOK())) { |
128 | cancelSubAnalyses(); | |
fa6e7869 | 129 | analysisReady(false); |
8192f2c6 AM |
130 | return false; |
131 | } | |
132 | ||
8f0cd315 | 133 | /* Wait until the two modules are initialized */ |
c81ffdf2 | 134 | if (!totalsModule.waitForInitialization() || !eventTypesModule.waitForInitialization()) { |
fa6e7869 | 135 | analysisReady(false); |
c81ffdf2 JCK |
136 | return false; |
137 | } | |
8192f2c6 AM |
138 | |
139 | ITmfStateSystem totalsSS = totalsModule.getStateSystem(); | |
140 | ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem(); | |
141 | ||
142 | if (totalsSS == null || eventTypesSS == null) { | |
e1c415b3 | 143 | /* This analysis was cancelled in the meantime */ |
fa6e7869 | 144 | analysisReady(false); |
c81ffdf2 | 145 | throw new IllegalStateException("TmfStatisticsModule : Sub-modules initialization succeeded but there is a null state system."); //$NON-NLS-1$ |
8192f2c6 AM |
146 | } |
147 | ||
d6b46913 | 148 | fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS); |
8f0cd315 AM |
149 | |
150 | /* fStatistics is now set, consider this module initialized */ | |
fa6e7869 | 151 | analysisReady(true); |
8f0cd315 AM |
152 | |
153 | /* | |
154 | * The rest of this "execute" will encompass the "execute" of the two | |
155 | * sub-analyzes. | |
156 | */ | |
ba220ab1 AM |
157 | if (!(totalsModule.waitForCompletion(monitor) && |
158 | eventTypesModule.waitForCompletion(monitor))) { | |
8f0cd315 AM |
159 | return false; |
160 | } | |
8192f2c6 AM |
161 | return true; |
162 | } | |
163 | ||
fa6e7869 JCK |
164 | /** |
165 | * Make the module available and set whether the initialization went well or | |
166 | * not. If not, no state system is available and | |
167 | * {@link #waitForInitialization()} should return false. | |
168 | * | |
169 | * @param success | |
170 | * True if the initialization went well, false otherwise | |
171 | */ | |
172 | private void analysisReady(boolean succeeded) { | |
173 | fInitializationSucceeded = succeeded; | |
174 | fInitialized.countDown(); | |
175 | } | |
176 | ||
8192f2c6 AM |
177 | @Override |
178 | protected void canceling() { | |
179 | /* | |
180 | * FIXME The "right" way to cancel state system construction is not | |
181 | * available yet... | |
182 | */ | |
ba220ab1 | 183 | cancelSubAnalyses(); |
8192f2c6 AM |
184 | |
185 | ITmfStatistics stats = fStatistics; | |
186 | if (stats != null) { | |
187 | stats.dispose(); | |
188 | } | |
189 | } | |
190 | ||
ba220ab1 AM |
191 | private void cancelSubAnalyses() { |
192 | totalsModule.cancel(); | |
193 | eventTypesModule.cancel(); | |
194 | } | |
195 | ||
8192f2c6 AM |
196 | // ------------------------------------------------------------------------ |
197 | // ITmfStateSystemAnalysisModule | |
198 | // ------------------------------------------------------------------------ | |
199 | ||
200 | @Override | |
201 | public ITmfStateSystem getStateSystem(String id) { | |
202 | switch (id) { | |
203 | case TmfStatisticsTotalsModule.ID: | |
204 | return totalsModule.getStateSystem(); | |
205 | case TmfStatisticsEventTypesModule.ID: | |
206 | return eventTypesModule.getStateSystem(); | |
207 | default: | |
208 | return null; | |
209 | } | |
210 | } | |
211 | ||
8192f2c6 | 212 | @Override |
11b771eb PT |
213 | public @NonNull Iterable<@NonNull ITmfStateSystem> getStateSystems() { |
214 | List<@NonNull ITmfStateSystem> list = new LinkedList<>(); | |
215 | ITmfStateSystem totalsStateSystem = totalsModule.getStateSystem(); | |
216 | if (totalsStateSystem != null) { | |
217 | list.add(totalsStateSystem); | |
218 | } | |
219 | ITmfStateSystem eventTypesStateSystem = eventTypesModule.getStateSystem(); | |
220 | if (eventTypesStateSystem != null) { | |
221 | list.add(eventTypesStateSystem); | |
222 | } | |
8192f2c6 AM |
223 | return list; |
224 | } | |
225 | } |