tmf : Move initialization steps of modules into analysisReady() method
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / statistics / TmfStatisticsModule.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
13 package org.eclipse.tracecompass.tmf.core.statistics;
14
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.concurrent.CountDownLatch;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
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;
29
30 /**
31 * Analysis module to compute the statistics of a trace.
32 *
33 * @author Alexandre Montplaisir
34 */
35 public class TmfStatisticsModule extends TmfAbstractAnalysisModule
36 implements ITmfAnalysisModuleWithStateSystems {
37
38 /** ID of this analysis module */
39 public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.core.statistics.analysis"; //$NON-NLS-1$
40
41 /** The trace's statistics */
42 private ITmfStatistics fStatistics = null;
43 private boolean fInitializationSucceeded;
44
45 private final TmfStateSystemAnalysisModule totalsModule = new TmfStatisticsTotalsModule();
46 private final TmfStateSystemAnalysisModule eventTypesModule = new TmfStatisticsEventTypesModule();
47
48 private final CountDownLatch fInitialized = new CountDownLatch(1);
49
50 /**
51 * Constructor
52 */
53 public TmfStatisticsModule() {
54 super();
55 }
56
57 /**
58 * Get the statistics object built by this analysis
59 *
60 * @return The ITmfStatistics object
61 */
62 @Nullable
63 public ITmfStatistics getStatistics() {
64 return fStatistics;
65 }
66
67 /**
68 * Wait until the analyses/state systems underneath are ready to be queried.
69 * @since 2.0
70 */
71 @Override
72 public boolean waitForInitialization() {
73 try {
74 fInitialized.await();
75 } catch (InterruptedException e) {
76 return false;
77 }
78 return fInitializationSucceeded;
79 }
80
81 // ------------------------------------------------------------------------
82 // TmfAbstractAnalysisModule
83 // ------------------------------------------------------------------------
84
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
96 @Override
97 public boolean setTrace(ITmfTrace trace) throws TmfAnalysisException {
98 if (!super.setTrace(trace)) {
99 return false;
100 }
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 */
107 if (!totalsModule.setTrace(trace)) {
108 return false;
109 }
110 if (!eventTypesModule.setTrace(trace)) {
111 return false;
112 }
113 return true;
114 }
115
116 @Override
117 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
118 ITmfTrace trace = getTrace();
119 if (trace == null) {
120 /* This analysis was cancelled in the meantime */
121 analysisReady(false);
122 return false;
123 }
124
125 IStatus status1 = totalsModule.schedule();
126 IStatus status2 = eventTypesModule.schedule();
127 if (!(status1.isOK() && status2.isOK())) {
128 cancelSubAnalyses();
129 analysisReady(false);
130 return false;
131 }
132
133 /* Wait until the two modules are initialized */
134 if (!totalsModule.waitForInitialization() || !eventTypesModule.waitForInitialization()) {
135 analysisReady(false);
136 return false;
137 }
138
139 ITmfStateSystem totalsSS = totalsModule.getStateSystem();
140 ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem();
141
142 if (totalsSS == null || eventTypesSS == null) {
143 /* This analysis was cancelled in the meantime */
144 analysisReady(false);
145 throw new IllegalStateException("TmfStatisticsModule : Sub-modules initialization succeeded but there is a null state system."); //$NON-NLS-1$
146 }
147
148 fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS);
149
150 /* fStatistics is now set, consider this module initialized */
151 analysisReady(true);
152
153 /*
154 * The rest of this "execute" will encompass the "execute" of the two
155 * sub-analyzes.
156 */
157 if (!(totalsModule.waitForCompletion(monitor) &&
158 eventTypesModule.waitForCompletion(monitor))) {
159 return false;
160 }
161 return true;
162 }
163
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
177 @Override
178 protected void canceling() {
179 /*
180 * FIXME The "right" way to cancel state system construction is not
181 * available yet...
182 */
183 cancelSubAnalyses();
184
185 ITmfStatistics stats = fStatistics;
186 if (stats != null) {
187 stats.dispose();
188 }
189 }
190
191 private void cancelSubAnalyses() {
192 totalsModule.cancel();
193 eventTypesModule.cancel();
194 }
195
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
212 @Override
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 }
223 return list;
224 }
225 }
This page took 0.043519 seconds and 5 git commands to generate.