tmf : Make waitForInitialization() return a boolean
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / statistics / TmfStatisticsModule.java
CommitLineData
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 13package org.eclipse.tracecompass.tmf.core.statistics;
8192f2c6
AM
14
15import java.util.LinkedList;
16import java.util.List;
8f0cd315 17import java.util.concurrent.CountDownLatch;
8192f2c6
AM
18
19import org.eclipse.core.runtime.IProgressMonitor;
20import org.eclipse.core.runtime.IStatus;
1d83ed07 21import org.eclipse.jdt.annotation.NonNull;
8f0cd315 22import org.eclipse.jdt.annotation.Nullable;
e894a508 23import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
2bdf0193
AM
24import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
25import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
26import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
27import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
28import 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 */
35public 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 */
c81ffdf2 121 fInitializationSucceeded = false;
e1c415b3
BH
122 fInitialized.countDown();
123 return false;
ba220ab1 124 }
8192f2c6
AM
125
126 IStatus status1 = totalsModule.schedule();
127 IStatus status2 = eventTypesModule.schedule();
ba220ab1
AM
128 if (!(status1.isOK() && status2.isOK())) {
129 cancelSubAnalyses();
c81ffdf2 130 fInitializationSucceeded = false;
e1c415b3 131 fInitialized.countDown();
8192f2c6
AM
132 return false;
133 }
134
8f0cd315 135 /* Wait until the two modules are initialized */
c81ffdf2
JCK
136 if (!totalsModule.waitForInitialization() || !eventTypesModule.waitForInitialization()) {
137 fInitializationSucceeded = false;
138 fInitialized.countDown();
139 return false;
140 }
8192f2c6
AM
141
142 ITmfStateSystem totalsSS = totalsModule.getStateSystem();
143 ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem();
144
145 if (totalsSS == null || eventTypesSS == null) {
e1c415b3 146 /* This analysis was cancelled in the meantime */
c81ffdf2 147 fInitializationSucceeded = false;
e1c415b3 148 fInitialized.countDown();
c81ffdf2 149 throw new IllegalStateException("TmfStatisticsModule : Sub-modules initialization succeeded but there is a null state system."); //$NON-NLS-1$
8192f2c6
AM
150 }
151
d6b46913 152 fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS);
8f0cd315
AM
153
154 /* fStatistics is now set, consider this module initialized */
c81ffdf2 155 fInitializationSucceeded = true;
8f0cd315
AM
156 fInitialized.countDown();
157
158 /*
159 * The rest of this "execute" will encompass the "execute" of the two
160 * sub-analyzes.
161 */
ba220ab1
AM
162 if (!(totalsModule.waitForCompletion(monitor) &&
163 eventTypesModule.waitForCompletion(monitor))) {
8f0cd315
AM
164 return false;
165 }
8192f2c6
AM
166 return true;
167 }
168
169 @Override
170 protected void canceling() {
171 /*
172 * FIXME The "right" way to cancel state system construction is not
173 * available yet...
174 */
ba220ab1 175 cancelSubAnalyses();
8192f2c6
AM
176
177 ITmfStatistics stats = fStatistics;
178 if (stats != null) {
179 stats.dispose();
180 }
181 }
182
ba220ab1
AM
183 private void cancelSubAnalyses() {
184 totalsModule.cancel();
185 eventTypesModule.cancel();
186 }
187
8192f2c6
AM
188 // ------------------------------------------------------------------------
189 // ITmfStateSystemAnalysisModule
190 // ------------------------------------------------------------------------
191
192 @Override
193 public ITmfStateSystem getStateSystem(String id) {
194 switch (id) {
195 case TmfStatisticsTotalsModule.ID:
196 return totalsModule.getStateSystem();
197 case TmfStatisticsEventTypesModule.ID:
198 return eventTypesModule.getStateSystem();
199 default:
200 return null;
201 }
202 }
203
8192f2c6 204 @Override
11b771eb
PT
205 public @NonNull Iterable<@NonNull ITmfStateSystem> getStateSystems() {
206 List<@NonNull ITmfStateSystem> list = new LinkedList<>();
207 ITmfStateSystem totalsStateSystem = totalsModule.getStateSystem();
208 if (totalsStateSystem != null) {
209 list.add(totalsStateSystem);
210 }
211 ITmfStateSystem eventTypesStateSystem = eventTypesModule.getStateSystem();
212 if (eventTypesStateSystem != null) {
213 list.add(eventTypesStateSystem);
214 }
8192f2c6
AM
215 return list;
216 }
217}
This page took 0.080238 seconds and 5 git commands to generate.