Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.tmf.ui.views.statistics; | |
14 | ||
15 | import java.util.LinkedList; | |
16 | import java.util.List; | |
8f0cd315 | 17 | import java.util.concurrent.CountDownLatch; |
8192f2c6 AM |
18 | |
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | import org.eclipse.core.runtime.IStatus; | |
8f0cd315 | 21 | import org.eclipse.jdt.annotation.Nullable; |
8192f2c6 AM |
22 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAbstractAnalysisModule; |
23 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException; | |
24 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems; | |
25 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem; | |
26 | import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule; | |
27 | import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics; | |
28 | import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics; | |
29 | import org.eclipse.linuxtools.tmf.core.statistics.TmfStatisticsEventTypesModule; | |
30 | import org.eclipse.linuxtools.tmf.core.statistics.TmfStatisticsTotalsModule; | |
31 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
32 | import 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 | */ | |
40 | public 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 | ||
43355455 AM |
85 | @Override |
86 | public void dispose() { | |
87 | /* | |
88 | * The sub-analyses are not registered to the trace directly, so we need | |
89 | * to tell them when the trace is disposed. | |
90 | */ | |
91 | super.dispose(); | |
92 | totalsModule.dispose(); | |
93 | eventTypesModule.dispose(); | |
94 | } | |
95 | ||
8192f2c6 | 96 | @Override |
ba220ab1 AM |
97 | public void setTrace(ITmfTrace trace) throws TmfAnalysisException { |
98 | super.setTrace(trace); | |
8192f2c6 AM |
99 | |
100 | /* | |
101 | * Since these sub-analyzes are not built from an extension point, we | |
102 | * have to assign the trace ourselves. Very important to do so before | |
103 | * calling schedule()! | |
104 | */ | |
105 | totalsModule.setTrace(trace); | |
106 | eventTypesModule.setTrace(trace); | |
ba220ab1 AM |
107 | } |
108 | ||
109 | @Override | |
110 | protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException { | |
111 | ITmfTrace trace = getTrace(); | |
112 | if (trace == null) { | |
113 | /* This analysis's trace should not be null when this is called */ | |
114 | throw new IllegalStateException(); | |
115 | } | |
8192f2c6 AM |
116 | |
117 | IStatus status1 = totalsModule.schedule(); | |
118 | IStatus status2 = eventTypesModule.schedule(); | |
ba220ab1 AM |
119 | if (!(status1.isOK() && status2.isOK())) { |
120 | cancelSubAnalyses(); | |
8192f2c6 AM |
121 | return false; |
122 | } | |
123 | ||
8f0cd315 AM |
124 | /* Wait until the two modules are initialized */ |
125 | totalsModule.waitForInitialization(); | |
126 | eventTypesModule.waitForInitialization(); | |
8192f2c6 AM |
127 | |
128 | ITmfStateSystem totalsSS = totalsModule.getStateSystem(); | |
129 | ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem(); | |
130 | ||
131 | if (totalsSS == null || eventTypesSS == null) { | |
132 | /* Better safe than sorry... */ | |
133 | throw new IllegalStateException(); | |
134 | } | |
135 | ||
d6b46913 | 136 | fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS); |
8f0cd315 AM |
137 | |
138 | /* fStatistics is now set, consider this module initialized */ | |
139 | fInitialized.countDown(); | |
140 | ||
141 | /* | |
142 | * The rest of this "execute" will encompass the "execute" of the two | |
143 | * sub-analyzes. | |
144 | */ | |
ba220ab1 AM |
145 | if (!(totalsModule.waitForCompletion(monitor) && |
146 | eventTypesModule.waitForCompletion(monitor))) { | |
8f0cd315 AM |
147 | return false; |
148 | } | |
8192f2c6 AM |
149 | return true; |
150 | } | |
151 | ||
152 | @Override | |
153 | protected void canceling() { | |
154 | /* | |
155 | * FIXME The "right" way to cancel state system construction is not | |
156 | * available yet... | |
157 | */ | |
ba220ab1 | 158 | cancelSubAnalyses(); |
8192f2c6 AM |
159 | |
160 | ITmfStatistics stats = fStatistics; | |
161 | if (stats != null) { | |
162 | stats.dispose(); | |
163 | } | |
164 | } | |
165 | ||
ba220ab1 AM |
166 | private void cancelSubAnalyses() { |
167 | totalsModule.cancel(); | |
168 | eventTypesModule.cancel(); | |
169 | } | |
170 | ||
8192f2c6 AM |
171 | // ------------------------------------------------------------------------ |
172 | // ITmfStateSystemAnalysisModule | |
173 | // ------------------------------------------------------------------------ | |
174 | ||
175 | @Override | |
176 | public ITmfStateSystem getStateSystem(String id) { | |
177 | switch (id) { | |
178 | case TmfStatisticsTotalsModule.ID: | |
179 | return totalsModule.getStateSystem(); | |
180 | case TmfStatisticsEventTypesModule.ID: | |
181 | return eventTypesModule.getStateSystem(); | |
182 | default: | |
183 | return null; | |
184 | } | |
185 | } | |
186 | ||
8192f2c6 AM |
187 | @Override |
188 | public Iterable<ITmfStateSystem> getStateSystems() { | |
189 | List<ITmfStateSystem> list = new LinkedList<>(); | |
190 | list.add(totalsModule.getStateSystem()); | |
191 | list.add(eventTypesModule.getStateSystem()); | |
192 | return list; | |
193 | } | |
194 | } |