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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.statistics; |
8192f2c6 AM |
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; | |
1d83ed07 | 21 | import org.eclipse.jdt.annotation.NonNull; |
8f0cd315 | 22 | import org.eclipse.jdt.annotation.Nullable; |
e894a508 | 23 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
2bdf0193 AM |
24 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; |
25 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; | |
26 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems; | |
27 | import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule; | |
28 | import 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 | */ |
35 | public 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 |
f479550c GB |
91 | public boolean setTrace(ITmfTrace trace) throws TmfAnalysisException { |
92 | if (!super.setTrace(trace)) { | |
93 | return false; | |
94 | } | |
8192f2c6 AM |
95 | |
96 | /* | |
97 | * Since these sub-analyzes are not built from an extension point, we | |
98 | * have to assign the trace ourselves. Very important to do so before | |
99 | * calling schedule()! | |
100 | */ | |
f479550c GB |
101 | if (!totalsModule.setTrace(trace)) { |
102 | return false; | |
103 | } | |
104 | if (!eventTypesModule.setTrace(trace)) { | |
105 | return false; | |
106 | } | |
107 | return true; | |
ba220ab1 AM |
108 | } |
109 | ||
110 | @Override | |
111 | protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException { | |
112 | ITmfTrace trace = getTrace(); | |
113 | if (trace == null) { | |
e1c415b3 BH |
114 | /* This analysis was cancelled in the meantime */ |
115 | fInitialized.countDown(); | |
116 | return false; | |
ba220ab1 | 117 | } |
8192f2c6 AM |
118 | |
119 | IStatus status1 = totalsModule.schedule(); | |
120 | IStatus status2 = eventTypesModule.schedule(); | |
ba220ab1 AM |
121 | if (!(status1.isOK() && status2.isOK())) { |
122 | cancelSubAnalyses(); | |
e1c415b3 | 123 | fInitialized.countDown(); |
8192f2c6 AM |
124 | return false; |
125 | } | |
126 | ||
8f0cd315 AM |
127 | /* Wait until the two modules are initialized */ |
128 | totalsModule.waitForInitialization(); | |
129 | eventTypesModule.waitForInitialization(); | |
8192f2c6 AM |
130 | |
131 | ITmfStateSystem totalsSS = totalsModule.getStateSystem(); | |
132 | ITmfStateSystem eventTypesSS = eventTypesModule.getStateSystem(); | |
133 | ||
134 | if (totalsSS == null || eventTypesSS == null) { | |
e1c415b3 BH |
135 | /* This analysis was cancelled in the meantime */ |
136 | fInitialized.countDown(); | |
137 | return false; | |
8192f2c6 AM |
138 | } |
139 | ||
d6b46913 | 140 | fStatistics = new TmfStateStatistics(totalsSS, eventTypesSS); |
8f0cd315 AM |
141 | |
142 | /* fStatistics is now set, consider this module initialized */ | |
143 | fInitialized.countDown(); | |
144 | ||
145 | /* | |
146 | * The rest of this "execute" will encompass the "execute" of the two | |
147 | * sub-analyzes. | |
148 | */ | |
ba220ab1 AM |
149 | if (!(totalsModule.waitForCompletion(monitor) && |
150 | eventTypesModule.waitForCompletion(monitor))) { | |
8f0cd315 AM |
151 | return false; |
152 | } | |
8192f2c6 AM |
153 | return true; |
154 | } | |
155 | ||
156 | @Override | |
157 | protected void canceling() { | |
158 | /* | |
159 | * FIXME The "right" way to cancel state system construction is not | |
160 | * available yet... | |
161 | */ | |
ba220ab1 | 162 | cancelSubAnalyses(); |
8192f2c6 AM |
163 | |
164 | ITmfStatistics stats = fStatistics; | |
165 | if (stats != null) { | |
166 | stats.dispose(); | |
167 | } | |
168 | } | |
169 | ||
ba220ab1 AM |
170 | private void cancelSubAnalyses() { |
171 | totalsModule.cancel(); | |
172 | eventTypesModule.cancel(); | |
173 | } | |
174 | ||
8192f2c6 AM |
175 | // ------------------------------------------------------------------------ |
176 | // ITmfStateSystemAnalysisModule | |
177 | // ------------------------------------------------------------------------ | |
178 | ||
179 | @Override | |
180 | public ITmfStateSystem getStateSystem(String id) { | |
181 | switch (id) { | |
182 | case TmfStatisticsTotalsModule.ID: | |
183 | return totalsModule.getStateSystem(); | |
184 | case TmfStatisticsEventTypesModule.ID: | |
185 | return eventTypesModule.getStateSystem(); | |
186 | default: | |
187 | return null; | |
188 | } | |
189 | } | |
190 | ||
8192f2c6 AM |
191 | @Override |
192 | public Iterable<ITmfStateSystem> getStateSystems() { | |
193 | List<ITmfStateSystem> list = new LinkedList<>(); | |
194 | list.add(totalsModule.getStateSystem()); | |
195 | list.add(eventTypesModule.getStateSystem()); | |
196 | return list; | |
197 | } | |
198 | } |