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