tmf: Add XML analysis helper properties
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / statesystem / TmfStateSystemAnalysisModule.java
CommitLineData
8a6ff07f 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2013, 2015 École Polytechnique de Montréal
8a6ff07f
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
baa96b1d 11 * Bernd Hufmann - Integrated history builder functionality
8a6ff07f
GB
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.core.statesystem;
8a6ff07f 15
5db5a3a4
AM
16import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
8a6ff07f 18import java.io.File;
baa96b1d 19import java.io.IOException;
5237a931 20import java.util.Collections;
09ec275e 21import java.util.concurrent.CountDownLatch;
8a6ff07f
GB
22
23import org.eclipse.core.runtime.IProgressMonitor;
e1c415b3 24import org.eclipse.core.runtime.IStatus;
baa96b1d 25import org.eclipse.core.runtime.NullProgressMonitor;
baa96b1d 26import org.eclipse.jdt.annotation.Nullable;
2bdf0193
AM
27import org.eclipse.tracecompass.internal.tmf.core.statesystem.backends.partial.PartialHistoryBackend;
28import org.eclipse.tracecompass.internal.tmf.core.statesystem.backends.partial.PartialStateSystem;
e894a508
AM
29import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
30import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
31import org.eclipse.tracecompass.statesystem.core.StateSystemFactory;
32import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
0306a843 33import org.eclipse.tracecompass.statesystem.core.backend.StateHistoryBackendFactory;
2bdf0193
AM
34import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
35import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
36import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
37import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
38import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
de83d1ab
MAL
39import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
40import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
2bdf0193
AM
41import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
42import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
de83d1ab 43import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceCompleteness;
2bdf0193 44import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
b8585c7c 45import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
5c5fa260 46import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
8a6ff07f
GB
47
48/**
49 * Abstract analysis module to generate a state system. It is a base class that
50 * can be used as a shortcut by analysis who just need to build a single state
51 * system with a state provider.
52 *
53 * Analysis implementing this class should only need to provide a state system
54 * and optionally a backend (default to NULL) and, if required, a filename
55 * (defaults to the analysis'ID)
56 *
57 * @author Geneviève Bastien
8a6ff07f
GB
58 */
59public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisModule
5237a931 60 implements ITmfAnalysisModuleWithStateSystems {
8a6ff07f 61
8a6ff07f
GB
62 private static final String EXTENSION = ".ht"; //$NON-NLS-1$
63
09ec275e 64 private final CountDownLatch fInitialized = new CountDownLatch(1);
de83d1ab 65 private final Object fRequestSyncObj = new Object();
09ec275e 66
baa96b1d
BH
67 @Nullable private ITmfStateSystemBuilder fStateSystem;
68 @Nullable private ITmfStateProvider fStateProvider;
69 @Nullable private IStateHistoryBackend fHtBackend;
70 @Nullable private ITmfEventRequest fRequest;
de83d1ab
MAL
71 @Nullable private TmfTimeRange fTimeRange = null;
72
73 private int fNbRead = 0;
5237a931 74
8a6ff07f
GB
75 /**
76 * State system backend types
77 *
78 * @author Geneviève Bastien
79 */
80 protected enum StateSystemBackendType {
81 /** Full history in file */
82 FULL,
83 /** In memory state system */
84 INMEM,
85 /** Null history */
86 NULL,
87 /** State system backed with partial history */
88 PARTIAL
89 }
90
72221aa4
AM
91 /**
92 * Retrieve a state system belonging to trace, by passing the ID of the
93 * relevant analysis module.
94 *
95 * This will start the execution of the analysis module, and start the
96 * construction of the state system, if needed.
97 *
98 * @param trace
99 * The trace for which you want the state system
100 * @param moduleId
101 * The ID of the state system analysis module
102 * @return The state system, or null if there was no match
72221aa4
AM
103 */
104 public static @Nullable ITmfStateSystem getStateSystem(ITmfTrace trace, String moduleId) {
105 TmfStateSystemAnalysisModule module =
b8585c7c 106 TmfTraceUtils.getAnalysisModuleOfClass(trace, TmfStateSystemAnalysisModule.class, moduleId);
72221aa4 107 if (module != null) {
e8e09aa3
GB
108 ITmfStateSystem ss = module.getStateSystem();
109 if (ss != null) {
110 return ss;
111 }
e1c415b3
BH
112 IStatus status = module.schedule();
113 if (status.isOK()) {
114 module.waitForInitialization();
e1c415b3
BH
115 return module.getStateSystem();
116 }
72221aa4
AM
117 }
118 return null;
119 }
120
8a6ff07f
GB
121 /**
122 * Get the state provider for this analysis module
123 *
124 * @return the state provider
125 */
8a6ff07f
GB
126 protected abstract ITmfStateProvider createStateProvider();
127
128 /**
129 * Get the state system backend type used by this module
130 *
131 * @return The {@link StateSystemBackendType}
132 */
a3b864c0
AM
133 protected StateSystemBackendType getBackendType() {
134 /* Using full history by default, sub-classes can override */
135 return StateSystemBackendType.FULL;
136 }
8a6ff07f
GB
137
138 /**
139 * Get the supplementary file name where to save this state system. The
140 * default is the ID of the analysis followed by the extension.
141 *
142 * @return The supplementary file name
143 */
144 protected String getSsFileName() {
145 return getId() + EXTENSION;
146 }
147
148 /**
baa96b1d
BH
149 * Get the state system generated by this analysis, or null if it is not yet
150 * created.
8a6ff07f
GB
151 *
152 * @return The state system
153 */
baa96b1d 154 @Nullable
8a6ff07f
GB
155 public ITmfStateSystem getStateSystem() {
156 return fStateSystem;
157 }
158
09ec275e
AM
159 /**
160 * Block the calling thread until the analysis module has been initialized.
161 * After this method returns, {@link #getStateSystem()} should not return
162 * null anymore.
163 */
164 public void waitForInitialization() {
165 try {
166 fInitialized.await();
167 } catch (InterruptedException e) {}
168 }
169
baa96b1d
BH
170 // ------------------------------------------------------------------------
171 // TmfAbstractAnalysisModule
172 // ------------------------------------------------------------------------
8a6ff07f 173
baa96b1d
BH
174 @Override
175 protected boolean executeAnalysis(@Nullable final IProgressMonitor monitor) {
176 IProgressMonitor mon = (monitor == null ? new NullProgressMonitor() : monitor);
177 final ITmfStateProvider provider = createStateProvider();
8a6ff07f 178
84a9548a 179 String id = getId();
84a9548a 180
8a6ff07f
GB
181 /* FIXME: State systems should make use of the monitor, to be cancelled */
182 try {
183 /* Get the state system according to backend */
184 StateSystemBackendType backend = getBackendType();
185 String directory;
baa96b1d 186 File htFile;
e1c415b3
BH
187
188 ITmfTrace trace = getTrace();
189 if (trace == null) {
190 // Analysis was cancelled in the meantime
191 fInitialized.countDown();
192 return false;
193 }
8a6ff07f
GB
194 switch (backend) {
195 case FULL:
e1c415b3 196 directory = TmfTraceManager.getSupplementaryFileDir(trace);
baa96b1d 197 htFile = new File(directory + getSsFileName());
84a9548a 198 createFullHistory(id, provider, htFile);
8a6ff07f
GB
199 break;
200 case PARTIAL:
e1c415b3 201 directory = TmfTraceManager.getSupplementaryFileDir(trace);
baa96b1d 202 htFile = new File(directory + getSsFileName());
84a9548a 203 createPartialHistory(id, provider, htFile);
8a6ff07f
GB
204 break;
205 case INMEM:
84a9548a 206 createInMemoryHistory(id, provider);
8a6ff07f
GB
207 break;
208 case NULL:
84a9548a 209 createNullHistory(id, provider);
8a6ff07f
GB
210 break;
211 default:
212 break;
213 }
214 } catch (TmfTraceException e) {
e1c415b3 215 fInitialized.countDown();
8a6ff07f
GB
216 return false;
217 }
baa96b1d 218 return !mon.isCanceled();
8a6ff07f
GB
219 }
220
221 @Override
222 protected void canceling() {
baa96b1d
BH
223 ITmfEventRequest req = fRequest;
224 if ((req != null) && (!req.isCompleted())) {
225 req.cancel();
226 }
227 }
228
a1529f38
AM
229 @Override
230 public void dispose() {
130e6f4e 231 super.dispose();
a1529f38
AM
232 if (fStateSystem != null) {
233 fStateSystem.dispose();
234 }
a1529f38
AM
235 }
236
baa96b1d
BH
237 // ------------------------------------------------------------------------
238 // History creation methods
239 // ------------------------------------------------------------------------
240
241 /*
242 * Load the history file matching the target trace. If the file already
243 * exists, it will be opened directly. If not, it will be created from
244 * scratch.
245 */
84a9548a 246 private void createFullHistory(String id, ITmfStateProvider provider, File htFile) throws TmfTraceException {
baa96b1d
BH
247
248 /* If the target file already exists, do not rebuild it uselessly */
249 // TODO for now we assume it's complete. Might be a good idea to check
250 // at least if its range matches the trace's range.
251
252 if (htFile.exists()) {
253 /* Load an existing history */
254 final int version = provider.getVersion();
255 try {
0306a843
AM
256 IStateHistoryBackend backend = StateHistoryBackendFactory.createHistoryTreeBackendExistingFile(
257 id, htFile, version);
84a9548a 258 fHtBackend = backend;
b2f62cb5 259 fStateSystem = StateSystemFactory.newStateSystem(backend, false);
09ec275e 260 fInitialized.countDown();
baa96b1d
BH
261 return;
262 } catch (IOException e) {
263 /*
264 * There was an error opening the existing file. Perhaps it was
265 * corrupted, perhaps it's an old version? We'll just
266 * fall-through and try to build a new one from scratch instead.
267 */
268 }
269 }
270
271 /* Size of the blocking queue to use when building a state history */
4cdea8fc 272 final int QUEUE_SIZE = 10000;
baa96b1d
BH
273
274 try {
0306a843 275 IStateHistoryBackend backend = StateHistoryBackendFactory.createHistoryTreeBackendNewFile(
f6d24a71 276 id, htFile, provider.getVersion(), provider.getStartTime(), QUEUE_SIZE);
84a9548a 277 fHtBackend = backend;
b2f62cb5 278 fStateSystem = StateSystemFactory.newStateSystem(backend);
baa96b1d
BH
279 provider.assignTargetStateSystem(fStateSystem);
280 build(provider);
281 } catch (IOException e) {
282 /*
283 * If it fails here however, it means there was a problem writing to
284 * the disk, so throw a real exception this time.
285 */
286 throw new TmfTraceException(e.toString(), e);
287 }
288 }
289
290 /*
291 * Create a new state system backed with a partial history. A partial
292 * history is similar to a "full" one (which you get with
293 * {@link #newFullHistory}), except that the file on disk is much smaller,
294 * but queries are a bit slower.
295 *
296 * Also note that single-queries are implemented using a full-query
297 * underneath, (which are much slower), so this might not be a good fit for
298 * a use case where you have to do lots of single queries.
299 */
84a9548a 300 private void createPartialHistory(String id, ITmfStateProvider provider, File htPartialFile)
baa96b1d
BH
301 throws TmfTraceException {
302 /*
303 * The order of initializations is very tricky (but very important!)
304 * here. We need to follow this pattern:
305 * (1 is done before the call to this method)
306 *
307 * 1- Instantiate realStateProvider
308 * 2- Instantiate realBackend
309 * 3- Instantiate partialBackend, with prereqs:
310 * 3a- Instantiate partialProvider, via realProvider.getNew()
311 * 3b- Instantiate nullBackend (partialSS's backend)
312 * 3c- Instantiate partialSS
313 * 3d- partialProvider.assignSS(partialSS)
314 * 4- Instantiate realSS
315 * 5- partialSS.assignUpstream(realSS)
316 * 6- realProvider.assignSS(realSS)
317 * 7- Call HistoryBuilder(realProvider, realSS, partialBackend) to build the thing.
318 */
319
320 /* Size of the blocking queue to use when building a state history */
321 final int QUEUE_SIZE = 10000;
322
323 final long granularity = 50000;
324
325 /* 2 */
326 IStateHistoryBackend realBackend = null;
327 try {
0306a843 328 realBackend = StateHistoryBackendFactory.createHistoryTreeBackendNewFile(
f6d24a71 329 id, htPartialFile, provider.getVersion(), provider.getStartTime(), QUEUE_SIZE);
baa96b1d
BH
330 } catch (IOException e) {
331 throw new TmfTraceException(e.toString(), e);
332 }
333
334 /* 3a */
335 ITmfStateProvider partialProvider = provider.getNewInstance();
336
337 /* 3b-3c, constructor automatically uses a NullBackend */
338 PartialStateSystem pss = new PartialStateSystem();
339
340 /* 3d */
341 partialProvider.assignTargetStateSystem(pss);
342
343 /* 3 */
651cbda0 344 IStateHistoryBackend partialBackend = new PartialHistoryBackend(id + ".partial", partialProvider, pss, realBackend, granularity); //$NON-NLS-1$
baa96b1d
BH
345
346 /* 4 */
bcec0116 347 @SuppressWarnings("restriction")
e894a508 348 org.eclipse.tracecompass.internal.statesystem.core.StateSystem realSS =
b2f62cb5 349 (org.eclipse.tracecompass.internal.statesystem.core.StateSystem) StateSystemFactory.newStateSystem(partialBackend);
baa96b1d
BH
350
351 /* 5 */
352 pss.assignUpstream(realSS);
353
354 /* 6 */
355 provider.assignTargetStateSystem(realSS);
356
357 /* 7 */
358 fHtBackend = partialBackend;
359 fStateSystem = realSS;
360
361 build(provider);
362 }
363
364 /*
365 * Create a new state system using a null history back-end. This means that
366 * no history intervals will be saved anywhere, and as such only
367 * {@link ITmfStateSystem#queryOngoingState} will be available.
368 */
84a9548a 369 private void createNullHistory(String id, ITmfStateProvider provider) {
0306a843 370 IStateHistoryBackend backend = StateHistoryBackendFactory.createNullBackend(id);
84a9548a 371 fHtBackend = backend;
b2f62cb5 372 fStateSystem = StateSystemFactory.newStateSystem(backend);
baa96b1d
BH
373 provider.assignTargetStateSystem(fStateSystem);
374 build(provider);
375 }
376
377 /*
378 * Create a new state system using in-memory interval storage. This should
379 * only be done for very small state system, and will be naturally limited
380 * to 2^31 intervals.
381 */
84a9548a 382 private void createInMemoryHistory(String id, ITmfStateProvider provider) {
0306a843 383 IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(id, provider.getStartTime());
84a9548a 384 fHtBackend = backend;
b2f62cb5 385 fStateSystem = StateSystemFactory.newStateSystem(backend);
baa96b1d
BH
386 provider.assignTargetStateSystem(fStateSystem);
387 build(provider);
388 }
389
a1529f38 390 private void disposeProvider(boolean deleteFiles) {
baa96b1d
BH
391 ITmfStateProvider provider = fStateProvider;
392 if (provider != null) {
393 provider.dispose();
394 }
395 if (deleteFiles && (fHtBackend != null)) {
396 fHtBackend.removeFiles();
397 }
398 }
399
400 private void build(ITmfStateProvider provider) {
401 if ((fStateSystem == null) || (fHtBackend == null)) {
402 throw new IllegalArgumentException();
403 }
404
405 ITmfEventRequest request = fRequest;
406 if ((request != null) && (!request.isCompleted())) {
407 request.cancel();
408 }
409
de83d1ab
MAL
410 fTimeRange = TmfTimeRange.ETERNITY;
411 final ITmfTrace trace = provider.getTrace();
d0c7e4ba 412 if (!isCompleteTrace(trace)) {
6cfc180e 413 fTimeRange = trace.getTimeRange();
de83d1ab 414 }
baa96b1d 415
baa96b1d 416 fStateProvider = provider;
de83d1ab
MAL
417 synchronized (fRequestSyncObj) {
418 startRequest();
419 }
baa96b1d 420
09ec275e
AM
421 /*
422 * The state system object is now created, we can consider this module
423 * "initialized" (components can retrieve it and start doing queries).
424 */
425 fInitialized.countDown();
426
427 /*
428 * Block the executeAnalysis() construction is complete (so that the
429 * progress monitor displays that it is running).
430 */
baa96b1d 431 try {
de83d1ab
MAL
432 if (fRequest != null) {
433 fRequest.waitForCompletion();
434 }
baa96b1d
BH
435 } catch (InterruptedException e) {
436 e.printStackTrace();
437 }
438 }
439
440 private class StateSystemEventRequest extends TmfEventRequest {
441 private final ITmfStateProvider sci;
442 private final ITmfTrace trace;
443
de83d1ab 444 public StateSystemEventRequest(ITmfStateProvider sp, TmfTimeRange timeRange, int index) {
e2bcc8a5 445 super(ITmfEvent.class,
de83d1ab
MAL
446 timeRange,
447 index,
baa96b1d
BH
448 ITmfEventRequest.ALL_DATA,
449 ITmfEventRequest.ExecutionType.BACKGROUND);
450 this.sci = sp;
451
452 // sci.getTrace() will eventually return a @NonNull
5db5a3a4 453 trace = checkNotNull(sci.getTrace());
baa96b1d 454
baa96b1d
BH
455 }
456
457 @Override
41f3b36b 458 public void handleData(final ITmfEvent event) {
baa96b1d 459 super.handleData(event);
41f3b36b 460 if (event.getTrace() == trace) {
baa96b1d 461 sci.processEvent(event);
2d208fb7
GB
462 } else if (trace instanceof TmfExperiment) {
463 /*
464 * If the request is for an experiment, check if the event is
465 * from one of the child trace
466 */
467 for (ITmfTrace childTrace : ((TmfExperiment) trace).getTraces()) {
468 if (childTrace == event.getTrace()) {
469 sci.processEvent(event);
470 }
471 }
baa96b1d
BH
472 }
473 }
474
475 @Override
476 public void handleSuccess() {
477 super.handleSuccess();
de83d1ab
MAL
478 if (isCompleteTrace(trace)) {
479 disposeProvider(false);
480 } else {
481 fNbRead += getNbRead();
482 synchronized (fRequestSyncObj) {
483 final TmfTimeRange timeRange = fTimeRange;
484 if (timeRange != null) {
485 if (getRange().getEndTime().getValue() < timeRange.getEndTime().getValue()) {
486 startRequest();
487 }
488 }
489 }
490 }
baa96b1d
BH
491 }
492
493 @Override
494 public void handleCancel() {
495 super.handleCancel();
de83d1ab
MAL
496 if (isCompleteTrace(trace)) {
497 disposeProvider(true);
498 }
baa96b1d
BH
499 }
500
501 @Override
502 public void handleFailure() {
503 super.handleFailure();
a1529f38 504 disposeProvider(true);
baa96b1d 505 }
8a6ff07f
GB
506 }
507
5237a931
AM
508 // ------------------------------------------------------------------------
509 // ITmfAnalysisModuleWithStateSystems
510 // ------------------------------------------------------------------------
511
512 @Override
baa96b1d
BH
513 @Nullable
514 public ITmfStateSystem getStateSystem(String id) {
5237a931
AM
515 if (id.equals(getId())) {
516 return fStateSystem;
517 }
518 return null;
519 }
520
8a6ff07f 521 @Override
5237a931 522 public Iterable<ITmfStateSystem> getStateSystems() {
aa353506
AM
523 ITmfStateSystemBuilder stateSystem = fStateSystem;
524 if (stateSystem == null) {
525 return Collections.EMPTY_SET;
526 }
527 return Collections.singleton(stateSystem);
8a6ff07f 528 }
de83d1ab
MAL
529
530 /**
531 * Signal handler for the TmfTraceRangeUpdatedSignal signal
532 *
533 * @param signal The incoming signal
534 */
535 @TmfSignalHandler
536 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
537 fTimeRange = signal.getRange();
538 ITmfStateProvider stateProvider = fStateProvider;
539 synchronized (fRequestSyncObj) {
540 if (signal.getTrace() == getTrace() && stateProvider != null && stateProvider.getAssignedStateSystem() != null) {
541 ITmfEventRequest request = fRequest;
542 if ((request == null) || request.isCompleted()) {
543 startRequest();
544 }
545 }
546 }
547 }
548
549 private void startRequest() {
550 ITmfStateProvider stateProvider = fStateProvider;
551 TmfTimeRange timeRange = fTimeRange;
552 if (stateProvider == null || timeRange == null) {
553 return;
554 }
555 ITmfEventRequest request = new StateSystemEventRequest(stateProvider, timeRange, fNbRead);
556 stateProvider.getTrace().sendRequest(request);
557 fRequest = request;
558 }
559
560 private static boolean isCompleteTrace(ITmfTrace trace) {
561 return !(trace instanceof ITmfTraceCompleteness) || ((ITmfTraceCompleteness) trace).isComplete();
562 }
8a6ff07f 563}
This page took 0.108993 seconds and 5 git commands to generate.