tmf : add waitForInitialization() to ITmfAnalysisModuleWithStateSystem
[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
44 private final TmfStateSystemAnalysisModule totalsModule = new TmfStatisticsTotalsModule();
45 private final TmfStateSystemAnalysisModule eventTypesModule = new TmfStatisticsEventTypesModule();
46
47 private final CountDownLatch fInitialized = new CountDownLatch(1);
48
49 /**
50 * Constructor
51 */
52 public TmfStatisticsModule() {
53 super();
54 }
55
56 /**
57 * Get the statistics object built by this analysis
58 *
59 * @return The ITmfStatistics object
60 */
61 @Nullable
62 public ITmfStatistics getStatistics() {
63 return fStatistics;
64 }
65
66 /**
67 * Wait until the analyses/state systems underneath are ready to be queried.
68 */
69 @Override
70 public void waitForInitialization() {
71 try {
72 fInitialized.await();
73 } catch (InterruptedException e) {}
74 }
75
76 // ------------------------------------------------------------------------
77 // TmfAbstractAnalysisModule
78 // ------------------------------------------------------------------------
79
80 @Override
81 public void dispose() {
82 /*
83 * The sub-analyses are not registered to the trace directly, so we need
84 * to tell them when the trace is disposed.
85 */
86 super.dispose();
87 totalsModule.dispose();
88 eventTypesModule.dispose();
89 }
90
91 @Override
92 public boolean setTrace(ITmfTrace trace) throws TmfAnalysisException {
93 if (!super.setTrace(trace)) {
94 return false;
95 }
96
97 /*
98 * Since these sub-analyzes are not built from an extension point, we
99 * have to assign the trace ourselves. Very important to do so before
100 * calling schedule()!
101 */
102 if (!totalsModule.setTrace(trace)) {
103 return false;
104 }
105 if (!eventTypesModule.setTrace(trace)) {
106 return false;
107 }
108 return true;
109 }
110
111 @Override
112 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
113 ITmfTrace trace = getTrace();
114 if (trace == null) {
115 /* This analysis was cancelled in the meantime */
116 fInitialized.countDown();
117 return false;
118 }
119
120 IStatus status1 = totalsModule.schedule();
121 IStatus status2 = eventTypesModule.schedule();
122 if (!(status1.isOK() && status2.isOK())) {
123 cancelSubAnalyses();
124 fInitialized.countDown();
125 return false;
126 }
127
128 /* Wait until the two modules are initialized */
129 totalsModule.waitForInitialization();
130 eventTypesModule.waitForInitialization();
131
132 ITmfStateSystem totalsSS = totalsModule.getStateSystem();
133 ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem();
134
135 if (totalsSS == null || eventTypesSS == null) {
136 /* This analysis was cancelled in the meantime */
137 fInitialized.countDown();
138 return false;
139 }
140
141 fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS);
142
143 /* fStatistics is now set, consider this module initialized */
144 fInitialized.countDown();
145
146 /*
147 * The rest of this "execute" will encompass the "execute" of the two
148 * sub-analyzes.
149 */
150 if (!(totalsModule.waitForCompletion(monitor) &&
151 eventTypesModule.waitForCompletion(monitor))) {
152 return false;
153 }
154 return true;
155 }
156
157 @Override
158 protected void canceling() {
159 /*
160 * FIXME The "right" way to cancel state system construction is not
161 * available yet...
162 */
163 cancelSubAnalyses();
164
165 ITmfStatistics stats = fStatistics;
166 if (stats != null) {
167 stats.dispose();
168 }
169 }
170
171 private void cancelSubAnalyses() {
172 totalsModule.cancel();
173 eventTypesModule.cancel();
174 }
175
176 // ------------------------------------------------------------------------
177 // ITmfStateSystemAnalysisModule
178 // ------------------------------------------------------------------------
179
180 @Override
181 public ITmfStateSystem getStateSystem(String id) {
182 switch (id) {
183 case TmfStatisticsTotalsModule.ID:
184 return totalsModule.getStateSystem();
185 case TmfStatisticsEventTypesModule.ID:
186 return eventTypesModule.getStateSystem();
187 default:
188 return null;
189 }
190 }
191
192 @Override
193 public @NonNull Iterable<@NonNull ITmfStateSystem> getStateSystems() {
194 List<@NonNull ITmfStateSystem> list = new LinkedList<>();
195 ITmfStateSystem totalsStateSystem = totalsModule.getStateSystem();
196 if (totalsStateSystem != null) {
197 list.add(totalsStateSystem);
198 }
199 ITmfStateSystem eventTypesStateSystem = eventTypesModule.getStateSystem();
200 if (eventTypesStateSystem != null) {
201 list.add(eventTypesStateSystem);
202 }
203 return list;
204 }
205 }
This page took 0.038668 seconds and 6 git commands to generate.