TMF: Specify if an analysis applies differently to an experiment
[deliverable/tracecompass.git] / 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;
43
44 private final TmfStateSystemAnalysisModule totalsModule = new TmfStatisticsTotalsModule();
45 private final TmfStateSystemAnalysisModule eventTypesModule = new TmfStatisticsEventTypesModule();
46
8f0cd315
AM
47 private final CountDownLatch fInitialized = new CountDownLatch(1);
48
8192f2c6
AM
49 /**
50 * Constructor
51 */
52 public TmfStatisticsModule() {
53 super();
8192f2c6
AM
54 }
55
56 /**
57 * Get the statistics object built by this analysis
58 *
59 * @return The ITmfStatistics object
60 */
8f0cd315 61 @Nullable
8192f2c6
AM
62 public ITmfStatistics getStatistics() {
63 return fStatistics;
64 }
65
8f0cd315
AM
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
8192f2c6
AM
75 // ------------------------------------------------------------------------
76 // TmfAbstractAnalysisModule
77 // ------------------------------------------------------------------------
78
43355455
AM
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
8192f2c6 90 @Override
ba220ab1
AM
91 public void setTrace(ITmfTrace trace) throws TmfAnalysisException {
92 super.setTrace(trace);
8192f2c6
AM
93
94 /*
95 * Since these sub-analyzes are not built from an extension point, we
96 * have to assign the trace ourselves. Very important to do so before
97 * calling schedule()!
98 */
99 totalsModule.setTrace(trace);
100 eventTypesModule.setTrace(trace);
ba220ab1
AM
101 }
102
103 @Override
104 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
105 ITmfTrace trace = getTrace();
106 if (trace == null) {
e1c415b3
BH
107 /* This analysis was cancelled in the meantime */
108 fInitialized.countDown();
109 return false;
ba220ab1 110 }
8192f2c6
AM
111
112 IStatus status1 = totalsModule.schedule();
113 IStatus status2 = eventTypesModule.schedule();
ba220ab1
AM
114 if (!(status1.isOK() && status2.isOK())) {
115 cancelSubAnalyses();
e1c415b3 116 fInitialized.countDown();
8192f2c6
AM
117 return false;
118 }
119
8f0cd315
AM
120 /* Wait until the two modules are initialized */
121 totalsModule.waitForInitialization();
122 eventTypesModule.waitForInitialization();
8192f2c6
AM
123
124 ITmfStateSystem totalsSS = totalsModule.getStateSystem();
125 ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem();
126
127 if (totalsSS == null || eventTypesSS == null) {
e1c415b3
BH
128 /* This analysis was cancelled in the meantime */
129 fInitialized.countDown();
130 return false;
8192f2c6
AM
131 }
132
d6b46913 133 fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS);
8f0cd315
AM
134
135 /* fStatistics is now set, consider this module initialized */
136 fInitialized.countDown();
137
138 /*
139 * The rest of this "execute" will encompass the "execute" of the two
140 * sub-analyzes.
141 */
ba220ab1
AM
142 if (!(totalsModule.waitForCompletion(monitor) &&
143 eventTypesModule.waitForCompletion(monitor))) {
8f0cd315
AM
144 return false;
145 }
8192f2c6
AM
146 return true;
147 }
148
149 @Override
150 protected void canceling() {
151 /*
152 * FIXME The "right" way to cancel state system construction is not
153 * available yet...
154 */
ba220ab1 155 cancelSubAnalyses();
8192f2c6
AM
156
157 ITmfStatistics stats = fStatistics;
158 if (stats != null) {
159 stats.dispose();
160 }
161 }
162
ba220ab1
AM
163 private void cancelSubAnalyses() {
164 totalsModule.cancel();
165 eventTypesModule.cancel();
166 }
167
8192f2c6
AM
168 // ------------------------------------------------------------------------
169 // ITmfStateSystemAnalysisModule
170 // ------------------------------------------------------------------------
171
172 @Override
173 public ITmfStateSystem getStateSystem(String id) {
174 switch (id) {
175 case TmfStatisticsTotalsModule.ID:
176 return totalsModule.getStateSystem();
177 case TmfStatisticsEventTypesModule.ID:
178 return eventTypesModule.getStateSystem();
179 default:
180 return null;
181 }
182 }
183
8192f2c6
AM
184 @Override
185 public Iterable<ITmfStateSystem> getStateSystems() {
186 List<ITmfStateSystem> list = new LinkedList<>();
187 list.add(totalsModule.getStateSystem());
188 list.add(eventTypesModule.getStateSystem());
189 return list;
190 }
191}
This page took 0.059542 seconds and 5 git commands to generate.