1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.statistics
;
15 import java
.util
.LinkedList
;
16 import java
.util
.List
;
17 import java
.util
.concurrent
.CountDownLatch
;
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
;
31 * Analysis module to compute the statistics of a trace.
33 * @author Alexandre Montplaisir
35 public class TmfStatisticsModule
extends TmfAbstractAnalysisModule
36 implements ITmfAnalysisModuleWithStateSystems
{
38 /** ID of this analysis module */
39 public static final @NonNull String ID
= "org.eclipse.linuxtools.tmf.core.statistics.analysis"; //$NON-NLS-1$
41 /** The trace's statistics */
42 private ITmfStatistics fStatistics
= null;
43 private boolean fInitializationSucceeded
;
45 private final TmfStateSystemAnalysisModule totalsModule
= new TmfStatisticsTotalsModule();
46 private final TmfStateSystemAnalysisModule eventTypesModule
= new TmfStatisticsEventTypesModule();
48 private final CountDownLatch fInitialized
= new CountDownLatch(1);
53 public TmfStatisticsModule() {
58 * Get the statistics object built by this analysis
60 * @return The ITmfStatistics object
63 public ITmfStatistics
getStatistics() {
68 * Wait until the analyses/state systems underneath are ready to be queried.
72 public boolean waitForInitialization() {
75 } catch (InterruptedException e
) {
78 return fInitializationSucceeded
;
81 // ------------------------------------------------------------------------
82 // TmfAbstractAnalysisModule
83 // ------------------------------------------------------------------------
86 public void dispose() {
88 * The sub-analyses are not registered to the trace directly, so we need
89 * to tell them when the trace is disposed.
92 totalsModule
.dispose();
93 eventTypesModule
.dispose();
97 public boolean setTrace(ITmfTrace trace
) throws TmfAnalysisException
{
98 if (!super.setTrace(trace
)) {
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()!
107 if (!totalsModule
.setTrace(trace
)) {
110 if (!eventTypesModule
.setTrace(trace
)) {
117 protected boolean executeAnalysis(IProgressMonitor monitor
) throws TmfAnalysisException
{
118 ITmfTrace trace
= getTrace();
120 /* This analysis was cancelled in the meantime */
121 analysisReady(false);
125 IStatus status1
= totalsModule
.schedule();
126 IStatus status2
= eventTypesModule
.schedule();
127 if (!(status1
.isOK() && status2
.isOK())) {
129 analysisReady(false);
133 /* Wait until the two modules are initialized */
134 if (!totalsModule
.waitForInitialization() || !eventTypesModule
.waitForInitialization()) {
135 analysisReady(false);
139 ITmfStateSystem totalsSS
= totalsModule
.getStateSystem();
140 ITmfStateSystem eventTypesSS
= eventTypesModule
.getStateSystem();
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$
148 fStatistics
= new TmfStateStatistics(totalsSS
, eventTypesSS
);
150 /* fStatistics is now set, consider this module initialized */
154 * The rest of this "execute" will encompass the "execute" of the two
157 if (!(totalsModule
.waitForCompletion(monitor
) &&
158 eventTypesModule
.waitForCompletion(monitor
))) {
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.
170 * True if the initialization went well, false otherwise
172 private void analysisReady(boolean succeeded
) {
173 fInitializationSucceeded
= succeeded
;
174 fInitialized
.countDown();
178 protected void canceling() {
180 * FIXME The "right" way to cancel state system construction is not
185 ITmfStatistics stats
= fStatistics
;
191 private void cancelSubAnalyses() {
192 totalsModule
.cancel();
193 eventTypesModule
.cancel();
196 // ------------------------------------------------------------------------
197 // ITmfStateSystemAnalysisModule
198 // ------------------------------------------------------------------------
201 public ITmfStateSystem
getStateSystem(String id
) {
203 case TmfStatisticsTotalsModule
.ID
:
204 return totalsModule
.getStateSystem();
205 case TmfStatisticsEventTypesModule
.ID
:
206 return eventTypesModule
.getStateSystem();
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
);
219 ITmfStateSystem eventTypesStateSystem
= eventTypesModule
.getStateSystem();
220 if (eventTypesStateSystem
!= null) {
221 list
.add(eventTypesStateSystem
);