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