tmf: Avoid hanging when waiting on a cancelled analysis
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / statistics / TmfStatisticsModule.java
CommitLineData
8192f2c6
AM
1/*******************************************************************************
2 * Copyright (c) 2013 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
13package org.eclipse.linuxtools.tmf.ui.views.statistics;
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;
8192f2c6
AM
22import org.eclipse.linuxtools.tmf.core.analysis.TmfAbstractAnalysisModule;
23import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
24import org.eclipse.linuxtools.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
25import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
26import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
27import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
28import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics;
29import org.eclipse.linuxtools.tmf.core.statistics.TmfStatisticsEventTypesModule;
30import org.eclipse.linuxtools.tmf.core.statistics.TmfStatisticsTotalsModule;
31import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
32import org.eclipse.linuxtools.tmf.ui.analysis.TmfAnalysisViewOutput;
33
34/**
35 * Analysis module to compute the statistics of a trace.
36 *
37 * @author Alexandre Montplaisir
38 * @since 3.0
39 */
40public class TmfStatisticsModule extends TmfAbstractAnalysisModule
41 implements ITmfAnalysisModuleWithStateSystems {
42
43 /** ID of this analysis module */
44 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.statistics.analysis"; //$NON-NLS-1$
45
46 /** The trace's statistics */
47 private ITmfStatistics fStatistics = null;
48
49 private final TmfStateSystemAnalysisModule totalsModule = new TmfStatisticsTotalsModule();
50 private final TmfStateSystemAnalysisModule eventTypesModule = new TmfStatisticsEventTypesModule();
51
8f0cd315
AM
52 private final CountDownLatch fInitialized = new CountDownLatch(1);
53
8192f2c6
AM
54 /**
55 * Constructor
56 */
57 public TmfStatisticsModule() {
58 super();
59 this.registerOutput(new TmfAnalysisViewOutput(TmfStatisticsView.ID));
60 }
61
62 /**
63 * Get the statistics object built by this analysis
64 *
65 * @return The ITmfStatistics object
66 */
8f0cd315 67 @Nullable
8192f2c6
AM
68 public ITmfStatistics getStatistics() {
69 return fStatistics;
70 }
71
8f0cd315
AM
72 /**
73 * Wait until the analyses/state systems underneath are ready to be queried.
74 */
75 public void waitForInitialization() {
76 try {
77 fInitialized.await();
78 } catch (InterruptedException e) {}
79 }
80
8192f2c6
AM
81 // ------------------------------------------------------------------------
82 // TmfAbstractAnalysisModule
83 // ------------------------------------------------------------------------
84
85 @Override
86 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
87 ITmfTrace trace = getTrace();
88 if (trace == null) {
89 /* This analysis's trace should not be null when this is called */
90 throw new IllegalStateException();
91 }
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 IStatus status1 = totalsModule.schedule();
102 IStatus status2 = eventTypesModule.schedule();
103 if (!status1.isOK() || !status2.isOK()) {
104 return false;
105 }
106
8f0cd315
AM
107 /* Wait until the two modules are initialized */
108 totalsModule.waitForInitialization();
109 eventTypesModule.waitForInitialization();
8192f2c6
AM
110
111 ITmfStateSystem totalsSS = totalsModule.getStateSystem();
112 ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem();
113
114 if (totalsSS == null || eventTypesSS == null) {
115 /* Better safe than sorry... */
116 throw new IllegalStateException();
117 }
118
d6b46913 119 fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS);
8f0cd315
AM
120
121 /* fStatistics is now set, consider this module initialized */
122 fInitialized.countDown();
123
124 /*
125 * The rest of this "execute" will encompass the "execute" of the two
126 * sub-analyzes.
127 */
128 if (!totalsModule.waitForCompletion(monitor) ||
129 !eventTypesModule.waitForCompletion(monitor)) {
130 return false;
131 }
8192f2c6
AM
132 return true;
133 }
134
135 @Override
136 protected void canceling() {
137 /*
138 * FIXME The "right" way to cancel state system construction is not
139 * available yet...
140 */
141 totalsModule.cancel();
142 eventTypesModule.cancel();
143
144 ITmfStatistics stats = fStatistics;
145 if (stats != null) {
146 stats.dispose();
147 }
148 }
149
150 // ------------------------------------------------------------------------
151 // ITmfStateSystemAnalysisModule
152 // ------------------------------------------------------------------------
153
154 @Override
155 public ITmfStateSystem getStateSystem(String id) {
156 switch (id) {
157 case TmfStatisticsTotalsModule.ID:
158 return totalsModule.getStateSystem();
159 case TmfStatisticsEventTypesModule.ID:
160 return eventTypesModule.getStateSystem();
161 default:
162 return null;
163 }
164 }
165
166 @Override
167 public String getStateSystemId(ITmfStateSystem ss) {
168 if (ss.equals(totalsModule.getStateSystem())) {
169 return TmfStatisticsTotalsModule.ID;
170 } else if (ss.equals(eventTypesModule.getStateSystem())){
171 return TmfStatisticsEventTypesModule.ID;
172 }
173 return null;
174 }
175
176 @Override
177 public Iterable<ITmfStateSystem> getStateSystems() {
178 List<ITmfStateSystem> list = new LinkedList<>();
179 list.add(totalsModule.getStateSystem());
180 list.add(eventTypesModule.getStateSystem());
181 return list;
182 }
183}
This page took 0.031993 seconds and 5 git commands to generate.