ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / 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
402ef15f 13package org.eclipse.linuxtools.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;
8f0cd315 21import org.eclipse.jdt.annotation.Nullable;
bcec0116 22import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
8192f2c6
AM
23import org.eclipse.linuxtools.tmf.core.analysis.TmfAbstractAnalysisModule;
24import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
25import org.eclipse.linuxtools.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
8192f2c6 26import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
8192f2c6 27import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
8192f2c6
AM
28
29/**
30 * Analysis module to compute the statistics of a trace.
31 *
32 * @author Alexandre Montplaisir
33 * @since 3.0
34 */
35public class TmfStatisticsModule extends TmfAbstractAnalysisModule
36 implements ITmfAnalysisModuleWithStateSystems {
37
38 /** ID of this analysis module */
402ef15f 39 public static final 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) {
107 /* This analysis's trace should not be null when this is called */
108 throw new IllegalStateException();
109 }
8192f2c6
AM
110
111 IStatus status1 = totalsModule.schedule();
112 IStatus status2 = eventTypesModule.schedule();
ba220ab1
AM
113 if (!(status1.isOK() && status2.isOK())) {
114 cancelSubAnalyses();
8192f2c6
AM
115 return false;
116 }
117
8f0cd315
AM
118 /* Wait until the two modules are initialized */
119 totalsModule.waitForInitialization();
120 eventTypesModule.waitForInitialization();
8192f2c6
AM
121
122 ITmfStateSystem totalsSS = totalsModule.getStateSystem();
123 ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem();
124
125 if (totalsSS == null || eventTypesSS == null) {
126 /* Better safe than sorry... */
127 throw new IllegalStateException();
128 }
129
d6b46913 130 fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS);
8f0cd315
AM
131
132 /* fStatistics is now set, consider this module initialized */
133 fInitialized.countDown();
134
135 /*
136 * The rest of this "execute" will encompass the "execute" of the two
137 * sub-analyzes.
138 */
ba220ab1
AM
139 if (!(totalsModule.waitForCompletion(monitor) &&
140 eventTypesModule.waitForCompletion(monitor))) {
8f0cd315
AM
141 return false;
142 }
8192f2c6
AM
143 return true;
144 }
145
146 @Override
147 protected void canceling() {
148 /*
149 * FIXME The "right" way to cancel state system construction is not
150 * available yet...
151 */
ba220ab1 152 cancelSubAnalyses();
8192f2c6
AM
153
154 ITmfStatistics stats = fStatistics;
155 if (stats != null) {
156 stats.dispose();
157 }
158 }
159
ba220ab1
AM
160 private void cancelSubAnalyses() {
161 totalsModule.cancel();
162 eventTypesModule.cancel();
163 }
164
8192f2c6
AM
165 // ------------------------------------------------------------------------
166 // ITmfStateSystemAnalysisModule
167 // ------------------------------------------------------------------------
168
169 @Override
170 public ITmfStateSystem getStateSystem(String id) {
171 switch (id) {
172 case TmfStatisticsTotalsModule.ID:
173 return totalsModule.getStateSystem();
174 case TmfStatisticsEventTypesModule.ID:
175 return eventTypesModule.getStateSystem();
176 default:
177 return null;
178 }
179 }
180
8192f2c6
AM
181 @Override
182 public Iterable<ITmfStateSystem> getStateSystems() {
183 List<ITmfStateSystem> list = new LinkedList<>();
184 list.add(totalsModule.getStateSystem());
185 list.add(eventTypesModule.getStateSystem());
186 return list;
187 }
188}
This page took 0.046539 seconds and 5 git commands to generate.