Commit | Line | Data |
---|---|---|
c068a752 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
c068a752 GB |
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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.analysis; |
c068a752 GB |
14 | |
15 | import java.util.ArrayList; | |
63c43609 | 16 | import java.util.Collections; |
c068a752 GB |
17 | import java.util.HashMap; |
18 | import java.util.List; | |
19 | import java.util.Map; | |
54eae41f | 20 | import java.util.Set; |
c068a752 GB |
21 | import java.util.concurrent.CountDownLatch; |
22 | import java.util.concurrent.TimeUnit; | |
23 | ||
24 | import org.eclipse.core.runtime.IProgressMonitor; | |
25 | import org.eclipse.core.runtime.IStatus; | |
ba27dd38 | 26 | import org.eclipse.core.runtime.NullProgressMonitor; |
c068a752 GB |
27 | import org.eclipse.core.runtime.Status; |
28 | import org.eclipse.core.runtime.jobs.Job; | |
dc58a1fb | 29 | import org.eclipse.jdt.annotation.NonNull; |
ba27dd38 GB |
30 | import org.eclipse.jdt.annotation.NonNullByDefault; |
31 | import org.eclipse.jdt.annotation.Nullable; | |
c068a752 | 32 | import org.eclipse.osgi.util.NLS; |
2bdf0193 AM |
33 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
34 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement.ValuePriorityLevel; | |
35 | import org.eclipse.tracecompass.tmf.core.component.TmfComponent; | |
36 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; | |
37 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; | |
38 | import org.eclipse.tracecompass.tmf.core.signal.TmfStartAnalysisSignal; | |
39 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; | |
40 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal; | |
41 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
42 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
c068a752 GB |
43 | |
44 | /** | |
45 | * Base class that analysis modules main class may extend. It provides default | |
46 | * behavior to some methods of the analysis module | |
47 | * | |
48 | * @author Geneviève Bastien | |
49 | * @since 3.0 | |
50 | */ | |
ba27dd38 | 51 | @NonNullByDefault |
c068a752 GB |
52 | public abstract class TmfAbstractAnalysisModule extends TmfComponent implements IAnalysisModule { |
53 | ||
ba27dd38 | 54 | private @Nullable String fId; |
c068a752 | 55 | private boolean fAutomatic = false, fStarted = false; |
ba27dd38 | 56 | private volatile @Nullable ITmfTrace fTrace; |
a4524c1b AM |
57 | private final Map<String, Object> fParameters = new HashMap<>(); |
58 | private final List<String> fParameterNames = new ArrayList<>(); | |
59 | private final List<IAnalysisOutput> fOutputs = new ArrayList<>(); | |
60 | private List<IAnalysisParameterProvider> fParameterProviders = new ArrayList<>(); | |
ba27dd38 | 61 | private @Nullable Job fJob = null; |
c068a752 GB |
62 | |
63 | private final Object syncObj = new Object(); | |
64 | ||
65 | /* Latch tracking if the analysis is completed or not */ | |
66 | private CountDownLatch fFinishedLatch = new CountDownLatch(0); | |
67 | ||
68 | private boolean fAnalysisCancelled = false; | |
69 | ||
70 | @Override | |
71 | public boolean isAutomatic() { | |
72 | return fAutomatic; | |
73 | } | |
74 | ||
75 | @Override | |
76 | public String getName() { | |
ba27dd38 | 77 | return super.getName(); |
c068a752 GB |
78 | } |
79 | ||
80 | @Override | |
81 | public void setName(String name) { | |
ba27dd38 | 82 | super.setName(name); |
c068a752 GB |
83 | } |
84 | ||
85 | @Override | |
86 | public void setId(String id) { | |
87 | fId = id; | |
88 | } | |
89 | ||
90 | @Override | |
91 | public String getId() { | |
dc58a1fb GB |
92 | String id = fId; |
93 | if (id == null) { | |
11835c0f AM |
94 | id = new String(this.getClass().getCanonicalName()); |
95 | fId = id; | |
dc58a1fb GB |
96 | } |
97 | return id; | |
c068a752 GB |
98 | } |
99 | ||
100 | @Override | |
101 | public void setAutomatic(boolean auto) { | |
102 | fAutomatic = auto; | |
103 | } | |
104 | ||
105 | @Override | |
106 | public void setTrace(ITmfTrace trace) throws TmfAnalysisException { | |
107 | if (fTrace != null) { | |
108 | throw new TmfAnalysisException(NLS.bind(Messages.TmfAbstractAnalysisModule_TraceSetMoreThanOnce, getName())); | |
109 | } | |
110 | ||
111 | /* Check that analysis can be executed */ | |
112 | if (!canExecute(trace)) { | |
113 | throw new TmfAnalysisException(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, getName())); | |
114 | } | |
26683871 | 115 | |
c068a752 GB |
116 | fTrace = trace; |
117 | /* Get the parameter providers for this trace */ | |
e1c415b3 | 118 | fParameterProviders = TmfAnalysisManager.getParameterProviders(this, trace); |
c068a752 GB |
119 | for (IAnalysisParameterProvider provider : fParameterProviders) { |
120 | provider.registerModule(this); | |
121 | } | |
122 | resetAnalysis(); | |
123 | fStarted = false; | |
124 | } | |
125 | ||
126 | /** | |
127 | * Gets the trace | |
128 | * | |
129 | * @return The trace | |
130 | */ | |
ba27dd38 | 131 | protected @Nullable ITmfTrace getTrace() { |
c068a752 GB |
132 | return fTrace; |
133 | } | |
134 | ||
135 | @Override | |
136 | public void addParameter(String name) { | |
137 | fParameterNames.add(name); | |
138 | } | |
139 | ||
140 | @Override | |
ba27dd38 | 141 | public synchronized void setParameter(String name, @Nullable Object value) { |
c068a752 GB |
142 | if (!fParameterNames.contains(name)) { |
143 | throw new RuntimeException(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, name, getName())); | |
144 | } | |
145 | Object oldValue = fParameters.get(name); | |
146 | fParameters.put(name, value); | |
147 | if ((value != null) && !(value.equals(oldValue))) { | |
148 | parameterChanged(name); | |
149 | } | |
150 | } | |
151 | ||
152 | @Override | |
153 | public synchronized void notifyParameterChanged(String name) { | |
154 | if (!fParameterNames.contains(name)) { | |
155 | throw new RuntimeException(NLS.bind(Messages.TmfAbstractAnalysisModule_InvalidParameter, name, getName())); | |
156 | } | |
157 | Object oldValue = fParameters.get(name); | |
158 | Object value = getParameter(name); | |
159 | if ((value != null) && !(value.equals(oldValue))) { | |
160 | parameterChanged(name); | |
161 | } | |
162 | } | |
163 | ||
164 | /** | |
165 | * Used to indicate that a parameter value has been changed | |
166 | * | |
167 | * @param name | |
168 | * The name of the modified parameter | |
169 | */ | |
170 | protected void parameterChanged(String name) { | |
171 | ||
172 | } | |
173 | ||
174 | @Override | |
ba27dd38 | 175 | public @Nullable Object getParameter(String name) { |
c068a752 GB |
176 | Object paramValue = fParameters.get(name); |
177 | /* The parameter is not set, maybe it can be provided by someone else */ | |
178 | if ((paramValue == null) && (fTrace != null)) { | |
179 | for (IAnalysisParameterProvider provider : fParameterProviders) { | |
180 | paramValue = provider.getParameter(name); | |
181 | if (paramValue != null) { | |
182 | break; | |
183 | } | |
184 | } | |
185 | } | |
186 | return paramValue; | |
187 | } | |
188 | ||
189 | @Override | |
ba27dd38 | 190 | public boolean canExecute(ITmfTrace trace) { |
26683871 GB |
191 | for (TmfAnalysisRequirement requirement : getAnalysisRequirements()) { |
192 | if (!requirement.isFulfilled(trace)) { | |
193 | return false; | |
194 | } | |
195 | } | |
c068a752 GB |
196 | return true; |
197 | } | |
198 | ||
199 | /** | |
200 | * Set the countdown latch back to 1 so the analysis can be executed again | |
201 | */ | |
202 | protected void resetAnalysis() { | |
0d3a54a3 | 203 | fFinishedLatch.countDown(); |
c068a752 GB |
204 | fFinishedLatch = new CountDownLatch(1); |
205 | } | |
206 | ||
207 | /** | |
208 | * Actually executes the analysis itself | |
209 | * | |
210 | * @param monitor | |
211 | * Progress monitor | |
212 | * @return Whether the analysis was completed successfully or not | |
213 | * @throws TmfAnalysisException | |
214 | * Method may throw an analysis exception | |
215 | */ | |
216 | protected abstract boolean executeAnalysis(final IProgressMonitor monitor) throws TmfAnalysisException; | |
217 | ||
218 | /** | |
219 | * Indicate the analysis has been canceled. It is abstract to force | |
220 | * implementing class to cleanup what they are running. This is called by | |
221 | * the job's canceling. It does not need to be called directly. | |
222 | */ | |
223 | protected abstract void canceling(); | |
224 | ||
225 | /** | |
226 | * To be called when the analysis is completed, whether normally or because | |
227 | * it was cancelled or for any other reason. | |
228 | * | |
229 | * It has to be called inside a synchronized block | |
230 | */ | |
231 | private void setAnalysisCompleted() { | |
232 | fStarted = false; | |
233 | fJob = null; | |
234 | fFinishedLatch.countDown(); | |
235 | } | |
236 | ||
237 | /** | |
238 | * Cancels the analysis if it is executing | |
239 | */ | |
240 | @Override | |
241 | public final void cancel() { | |
242 | synchronized (syncObj) { | |
243 | if (fJob != null) { | |
244 | if (fJob.cancel()) { | |
245 | fAnalysisCancelled = true; | |
246 | setAnalysisCompleted(); | |
247 | } | |
248 | } | |
249 | fStarted = false; | |
250 | } | |
251 | } | |
252 | ||
130e6f4e MAL |
253 | @Override |
254 | public void dispose() { | |
255 | super.dispose(); | |
256 | cancel(); | |
257 | } | |
258 | ||
ba27dd38 | 259 | private void execute(final ITmfTrace trace) { |
c068a752 GB |
260 | |
261 | /* | |
262 | * TODO: The analysis in a job should be done at the analysis manager | |
263 | * level instead of depending on this abstract class implementation, | |
264 | * otherwise another analysis implementation may block the main thread | |
265 | */ | |
266 | ||
267 | /* Do not execute if analysis has already run */ | |
268 | if (fFinishedLatch.getCount() == 0) { | |
269 | return; | |
270 | } | |
271 | ||
272 | /* Do not execute if analysis already running */ | |
273 | synchronized (syncObj) { | |
274 | if (fStarted) { | |
275 | return; | |
276 | } | |
277 | fStarted = true; | |
278 | } | |
279 | ||
280 | /* | |
281 | * Actual analysis will be run on a separate thread | |
282 | */ | |
ba27dd38 GB |
283 | @SuppressWarnings("null") |
284 | @NonNull String jobName = NLS.bind(Messages.TmfAbstractAnalysisModule_RunningAnalysis, getName()); | |
285 | fJob = new Job(jobName) { | |
c068a752 | 286 | @Override |
ba27dd38 GB |
287 | protected @Nullable IStatus run(final @Nullable IProgressMonitor monitor) { |
288 | IProgressMonitor mon = monitor; | |
289 | if (mon == null) { | |
290 | mon = new NullProgressMonitor(); | |
291 | } | |
c068a752 | 292 | try { |
ba27dd38 | 293 | mon.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$ |
c068a752 | 294 | broadcast(new TmfStartAnalysisSignal(TmfAbstractAnalysisModule.this, TmfAbstractAnalysisModule.this)); |
ba27dd38 | 295 | fAnalysisCancelled = !executeAnalysis(mon); |
c068a752 | 296 | } catch (TmfAnalysisException e) { |
e1c415b3 | 297 | Activator.logError("Error executing analysis with trace " + trace.getName(), e); //$NON-NLS-1$ |
c068a752 GB |
298 | } finally { |
299 | synchronized (syncObj) { | |
ba27dd38 | 300 | mon.done(); |
c068a752 GB |
301 | setAnalysisCompleted(); |
302 | } | |
88567ed2 | 303 | TmfTraceManager.refreshSupplementaryFiles(trace); |
c068a752 GB |
304 | } |
305 | if (!fAnalysisCancelled) { | |
306 | return Status.OK_STATUS; | |
307 | } | |
baa96b1d BH |
308 | // Reset analysis so that it can be executed again. |
309 | resetAnalysis(); | |
c068a752 GB |
310 | return Status.CANCEL_STATUS; |
311 | } | |
312 | ||
313 | @Override | |
314 | protected void canceling() { | |
315 | TmfAbstractAnalysisModule.this.canceling(); | |
316 | } | |
317 | ||
318 | }; | |
319 | fJob.schedule(); | |
320 | } | |
321 | ||
322 | @Override | |
323 | public IStatus schedule() { | |
e1c415b3 BH |
324 | synchronized (syncObj) { |
325 | final ITmfTrace trace = getTrace(); | |
326 | if (trace == null) { | |
327 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, String.format("No trace specified for analysis %s", getName())); //$NON-NLS-1$ | |
328 | } | |
329 | execute(trace); | |
c068a752 | 330 | } |
c068a752 | 331 | |
ba27dd38 GB |
332 | @SuppressWarnings("null") |
333 | @NonNull IStatus status = Status.OK_STATUS; | |
334 | return status; | |
c068a752 GB |
335 | } |
336 | ||
337 | @Override | |
d9810555 AM |
338 | public Iterable<IAnalysisOutput> getOutputs() { |
339 | return fOutputs; | |
c068a752 GB |
340 | } |
341 | ||
342 | @Override | |
343 | public void registerOutput(IAnalysisOutput output) { | |
344 | if (!fOutputs.contains(output)) { | |
345 | fOutputs.add(output); | |
346 | } | |
347 | } | |
348 | ||
7d6122fc AM |
349 | @Override |
350 | public boolean waitForCompletion() { | |
351 | try { | |
352 | fFinishedLatch.await(); | |
353 | } catch (InterruptedException e) { | |
354 | Activator.logError("Error while waiting for module completion", e); //$NON-NLS-1$ | |
355 | } | |
356 | return !fAnalysisCancelled; | |
357 | } | |
358 | ||
c068a752 GB |
359 | @Override |
360 | public boolean waitForCompletion(IProgressMonitor monitor) { | |
361 | try { | |
0d3a54a3 AM |
362 | while (!fFinishedLatch.await(500, TimeUnit.MILLISECONDS)) { |
363 | if (fAnalysisCancelled || monitor.isCanceled()) { | |
c068a752 GB |
364 | fAnalysisCancelled = true; |
365 | return false; | |
366 | } | |
367 | } | |
368 | } catch (InterruptedException e) { | |
369 | Activator.logError("Error while waiting for module completion", e); //$NON-NLS-1$ | |
370 | } | |
371 | return !fAnalysisCancelled; | |
372 | } | |
373 | ||
374 | /** | |
375 | * Signal handler for trace closing | |
376 | * | |
377 | * @param signal | |
378 | * Trace closed signal | |
379 | */ | |
380 | @TmfSignalHandler | |
381 | public void traceClosed(TmfTraceClosedSignal signal) { | |
382 | /* Is the closing trace the one that was requested? */ | |
e1c415b3 BH |
383 | synchronized (syncObj) { |
384 | if (signal.getTrace() == fTrace) { | |
385 | cancel(); | |
386 | fTrace = null; | |
387 | } | |
c068a752 GB |
388 | } |
389 | } | |
390 | ||
715154b1 GB |
391 | /** |
392 | * Signal handler for when the trace becomes active | |
393 | * | |
394 | * @param signal | |
395 | * Trace selected signal | |
396 | * @since 3.1 | |
397 | */ | |
398 | @TmfSignalHandler | |
399 | public void traceSelected(TmfTraceSelectedSignal signal) { | |
400 | /* | |
401 | * Since some parameter providers may handle many traces, we need to | |
402 | * register the current trace to it | |
403 | */ | |
404 | if (signal.getTrace() == fTrace) { | |
405 | for (IAnalysisParameterProvider provider : fParameterProviders) { | |
406 | provider.registerModule(this); | |
407 | } | |
408 | } | |
409 | } | |
410 | ||
c068a752 GB |
411 | /** |
412 | * Returns a full help text to display | |
413 | * | |
414 | * @return Full help text for the module | |
415 | */ | |
416 | protected String getFullHelpText() { | |
ba27dd38 GB |
417 | @SuppressWarnings("null") |
418 | @NonNull String text = NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisModule, getName()); | |
419 | return text; | |
c068a752 GB |
420 | } |
421 | ||
422 | /** | |
423 | * Gets a short help text, to display as header to other help text | |
424 | * | |
425 | * @param trace | |
426 | * The trace to show help for | |
427 | * | |
428 | * @return Short help text describing the module | |
429 | */ | |
430 | protected String getShortHelpText(ITmfTrace trace) { | |
ba27dd38 GB |
431 | @SuppressWarnings("null") |
432 | @NonNull String text = NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisForTrace, getName(), trace.getName()); | |
433 | return text; | |
c068a752 GB |
434 | } |
435 | ||
436 | /** | |
437 | * Gets the help text specific for a trace who does not have required | |
54eae41f GB |
438 | * characteristics for module to execute. The default implementation uses |
439 | * the analysis requirements. | |
c068a752 GB |
440 | * |
441 | * @param trace | |
442 | * The trace to apply the analysis to | |
443 | * @return Help text | |
444 | */ | |
ba27dd38 | 445 | protected String getTraceCannotExecuteHelpText(ITmfTrace trace) { |
54eae41f GB |
446 | StringBuilder builder = new StringBuilder(); |
447 | builder.append(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, getName())); | |
448 | for (TmfAnalysisRequirement requirement : getAnalysisRequirements()) { | |
449 | if (!requirement.isFulfilled(trace)) { | |
450 | builder.append("\n\n"); //$NON-NLS-1$ | |
451 | builder.append(NLS.bind(Messages.TmfAnalysis_RequirementNotFulfilled, requirement.getType())); | |
452 | builder.append("\n"); //$NON-NLS-1$ | |
453 | builder.append(NLS.bind(Messages.TmfAnalysis_RequirementMandatoryValues, requirement.getValues(ValuePriorityLevel.MANDATORY))); | |
454 | Set<String> information = requirement.getInformation(); | |
455 | if (!information.isEmpty()) { | |
456 | builder.append("\n"); //$NON-NLS-1$ | |
457 | builder.append(NLS.bind(Messages.TmfAnalysis_RequirementInformation, information)); | |
458 | } | |
459 | } | |
460 | } | |
ba27dd38 GB |
461 | @SuppressWarnings("null") |
462 | @NonNull String helpText = builder.toString(); | |
463 | return helpText; | |
c068a752 GB |
464 | } |
465 | ||
466 | @Override | |
467 | public String getHelpText() { | |
468 | return getFullHelpText(); | |
469 | } | |
470 | ||
471 | @Override | |
472 | public String getHelpText(ITmfTrace trace) { | |
c068a752 GB |
473 | String text = getShortHelpText(trace); |
474 | if (!canExecute(trace)) { | |
54eae41f | 475 | text = text + "\n\n" + getTraceCannotExecuteHelpText(trace); //$NON-NLS-1$ |
c068a752 GB |
476 | } |
477 | return text; | |
478 | } | |
479 | ||
63c43609 MR |
480 | @Override |
481 | public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() { | |
ba27dd38 GB |
482 | @SuppressWarnings("null") |
483 | @NonNull Iterable<TmfAnalysisRequirement> emptySet = Collections.EMPTY_SET; | |
484 | return emptySet; | |
63c43609 | 485 | } |
c068a752 | 486 | } |