1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 * Bernd Hufmann - Integrated history builder functionality
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.statesystem
;
17 import java
.io
.IOException
;
18 import java
.util
.Collections
;
20 import java
.util
.concurrent
.CountDownLatch
;
22 import org
.apache
.commons
.io
.FileUtils
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.jdt
.annotation
.Nullable
;
28 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.statesystem
.backends
.partial
.PartialHistoryBackend
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.statesystem
.backends
.partial
.PartialStateSystem
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemFactory
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.StateHistoryBackendFactory
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceCompleteness
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
51 * Abstract analysis module to generate a state system. It is a base class that
52 * can be used as a shortcut by analysis who just need to build a single state
53 * system with a state provider.
55 * Analysis implementing this class should only need to provide a state system
56 * and optionally a backend (default to NULL) and, if required, a filename
57 * (defaults to the analysis'ID)
59 * @author Geneviève Bastien
61 public abstract class TmfStateSystemAnalysisModule
extends TmfAbstractAnalysisModule
62 implements ITmfAnalysisModuleWithStateSystems
{
64 private static final String EXTENSION
= ".ht"; //$NON-NLS-1$
66 private final CountDownLatch fInitialized
= new CountDownLatch(1);
67 private final Object fRequestSyncObj
= new Object();
69 @Nullable private ITmfStateSystemBuilder fStateSystem
;
70 @Nullable private ITmfStateProvider fStateProvider
;
71 @Nullable private IStateHistoryBackend fHtBackend
;
72 @Nullable private ITmfEventRequest fRequest
;
73 @Nullable private TmfTimeRange fTimeRange
= null;
75 private int fNbRead
= 0;
76 private boolean fInitializationSucceeded
;
79 * State system backend types
81 * @author Geneviève Bastien
83 protected enum StateSystemBackendType
{
84 /** Full history in file */
86 /** In memory state system */
90 /** State system backed with partial history */
95 * Retrieve a state system belonging to trace, by passing the ID of the
96 * relevant analysis module.
98 * This will start the execution of the analysis module, and start the
99 * construction of the state system, if needed.
102 * The trace for which you want the state system
104 * The ID of the state system analysis module
105 * @return The state system, or null if there was no match or the module was
106 * not initialized correctly
108 public static @Nullable ITmfStateSystem
getStateSystem(ITmfTrace trace
, String moduleId
) {
109 TmfStateSystemAnalysisModule module
=
110 TmfTraceUtils
.getAnalysisModuleOfClass(trace
, TmfStateSystemAnalysisModule
.class, moduleId
);
111 if (module
!= null) {
112 ITmfStateSystem ss
= module
.getStateSystem();
116 IStatus status
= module
.schedule();
118 return module
.waitForInitialization() ? module
.getStateSystem() : null;
125 * Get the state provider for this analysis module
127 * @return the state provider
129 protected abstract ITmfStateProvider
createStateProvider();
132 * Get the state system backend type used by this module
134 * @return The {@link StateSystemBackendType}
136 protected StateSystemBackendType
getBackendType() {
137 /* Using full history by default, sub-classes can override */
138 return StateSystemBackendType
.FULL
;
142 * Get the supplementary file name where to save this state system. The
143 * default is the ID of the analysis followed by the extension.
145 * @return The supplementary file name
147 protected String
getSsFileName() {
148 return getId() + EXTENSION
;
152 * Get the state system generated by this analysis, or null if it is not yet
155 * @return The state system
158 public ITmfStateSystem
getStateSystem() {
166 public boolean waitForInitialization() {
168 fInitialized
.await();
169 } catch (InterruptedException e
) {
172 return fInitializationSucceeded
;
175 // ------------------------------------------------------------------------
176 // TmfAbstractAnalysisModule
177 // ------------------------------------------------------------------------
179 private @Nullable File
getSsFile() {
180 ITmfTrace trace
= getTrace();
184 String directory
= TmfTraceManager
.getSupplementaryFileDir(trace
);
185 File htFile
= new File(directory
+ getSsFileName());
190 protected boolean executeAnalysis(@Nullable final IProgressMonitor monitor
) {
191 IProgressMonitor mon
= (monitor
== null ?
new NullProgressMonitor() : monitor
);
192 final ITmfStateProvider provider
= createStateProvider();
196 /* FIXME: State systems should make use of the monitor, to be cancelled */
198 /* Get the state system according to backend */
199 StateSystemBackendType backend
= getBackendType();
202 ITmfTrace trace
= getTrace();
204 // Analysis was cancelled in the meantime
205 analysisReady(false);
210 File htFile
= getSsFile();
211 if (htFile
== null) {
214 createFullHistory(id
, provider
, htFile
);
218 File htFile
= getSsFile();
219 if (htFile
== null) {
222 createPartialHistory(id
, provider
, htFile
);
226 createInMemoryHistory(id
, provider
);
229 createNullHistory(id
, provider
);
234 } catch (TmfTraceException e
) {
235 analysisReady(false);
238 return !mon
.isCanceled();
242 * Make the module available and set whether the initialization succeeded or
243 * not. If not, no state system is available and
244 * {@link #waitForInitialization()} should return false.
247 * True if the initialization succeeded, false otherwise
249 private void analysisReady(boolean succeeded
) {
250 fInitializationSucceeded
= succeeded
;
251 fInitialized
.countDown();
255 protected void canceling() {
256 ITmfEventRequest req
= fRequest
;
257 if ((req
!= null) && (!req
.isCompleted())) {
263 public void dispose() {
265 if (fStateSystem
!= null) {
266 fStateSystem
.dispose();
270 // ------------------------------------------------------------------------
271 // History creation methods
272 // ------------------------------------------------------------------------
275 * Load the history file matching the target trace. If the file already
276 * exists, it will be opened directly. If not, it will be created from
279 private void createFullHistory(String id
, ITmfStateProvider provider
, File htFile
) throws TmfTraceException
{
281 /* If the target file already exists, do not rebuild it uselessly */
282 // TODO for now we assume it's complete. Might be a good idea to check
283 // at least if its range matches the trace's range.
285 if (htFile
.exists()) {
286 /* Load an existing history */
287 final int version
= provider
.getVersion();
289 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createHistoryTreeBackendExistingFile(
290 id
, htFile
, version
);
291 fHtBackend
= backend
;
292 fStateSystem
= StateSystemFactory
.newStateSystem(backend
, false);
295 } catch (IOException e
) {
297 * There was an error opening the existing file. Perhaps it was
298 * corrupted, perhaps it's an old version? We'll just
299 * fall-through and try to build a new one from scratch instead.
304 /* Size of the blocking queue to use when building a state history */
305 final int QUEUE_SIZE
= 10000;
308 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createHistoryTreeBackendNewFile(
309 id
, htFile
, provider
.getVersion(), provider
.getStartTime(), QUEUE_SIZE
);
310 fHtBackend
= backend
;
311 fStateSystem
= StateSystemFactory
.newStateSystem(backend
);
312 provider
.assignTargetStateSystem(fStateSystem
);
314 } catch (IOException e
) {
316 * If it fails here however, it means there was a problem writing to
317 * the disk, so throw a real exception this time.
319 throw new TmfTraceException(e
.toString(), e
);
324 * Create a new state system backed with a partial history. A partial
325 * history is similar to a "full" one (which you get with
326 * {@link #newFullHistory}), except that the file on disk is much smaller,
327 * but queries are a bit slower.
329 * Also note that single-queries are implemented using a full-query
330 * underneath, (which are much slower), so this might not be a good fit for
331 * a use case where you have to do lots of single queries.
333 private void createPartialHistory(String id
, ITmfStateProvider provider
, File htPartialFile
)
334 throws TmfTraceException
{
336 * The order of initializations is very tricky (but very important!)
337 * here. We need to follow this pattern:
338 * (1 is done before the call to this method)
340 * 1- Instantiate realStateProvider
341 * 2- Instantiate realBackend
342 * 3- Instantiate partialBackend, with prereqs:
343 * 3a- Instantiate partialProvider, via realProvider.getNew()
344 * 3b- Instantiate nullBackend (partialSS's backend)
345 * 3c- Instantiate partialSS
346 * 3d- partialProvider.assignSS(partialSS)
347 * 4- Instantiate realSS
348 * 5- partialSS.assignUpstream(realSS)
349 * 6- realProvider.assignSS(realSS)
350 * 7- Call HistoryBuilder(realProvider, realSS, partialBackend) to build the thing.
353 /* Size of the blocking queue to use when building a state history */
354 final int QUEUE_SIZE
= 10000;
356 final long granularity
= 50000;
359 IStateHistoryBackend realBackend
= null;
361 realBackend
= StateHistoryBackendFactory
.createHistoryTreeBackendNewFile(
362 id
, htPartialFile
, provider
.getVersion(), provider
.getStartTime(), QUEUE_SIZE
);
363 } catch (IOException e
) {
364 throw new TmfTraceException(e
.toString(), e
);
368 ITmfStateProvider partialProvider
= provider
.getNewInstance();
370 /* 3b-3c, constructor automatically uses a NullBackend */
371 PartialStateSystem pss
= new PartialStateSystem();
374 partialProvider
.assignTargetStateSystem(pss
);
377 IStateHistoryBackend partialBackend
= new PartialHistoryBackend(id
+ ".partial", partialProvider
, pss
, realBackend
, granularity
); //$NON-NLS-1$
380 @SuppressWarnings("restriction")
381 org
.eclipse
.tracecompass
.internal
.statesystem
.core
.StateSystem realSS
=
382 (org
.eclipse
.tracecompass
.internal
.statesystem
.core
.StateSystem
) StateSystemFactory
.newStateSystem(partialBackend
);
385 pss
.assignUpstream(realSS
);
388 provider
.assignTargetStateSystem(realSS
);
391 fHtBackend
= partialBackend
;
392 fStateSystem
= realSS
;
398 * Create a new state system using a null history back-end. This means that
399 * no history intervals will be saved anywhere, and as such only
400 * {@link ITmfStateSystem#queryOngoingState} will be available.
402 private void createNullHistory(String id
, ITmfStateProvider provider
) {
403 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createNullBackend(id
);
404 fHtBackend
= backend
;
405 fStateSystem
= StateSystemFactory
.newStateSystem(backend
);
406 provider
.assignTargetStateSystem(fStateSystem
);
411 * Create a new state system using in-memory interval storage. This should
412 * only be done for very small state system, and will be naturally limited
415 private void createInMemoryHistory(String id
, ITmfStateProvider provider
) {
416 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createInMemoryBackend(id
, provider
.getStartTime());
417 fHtBackend
= backend
;
418 fStateSystem
= StateSystemFactory
.newStateSystem(backend
);
419 provider
.assignTargetStateSystem(fStateSystem
);
423 private void disposeProvider(boolean deleteFiles
) {
424 ITmfStateProvider provider
= fStateProvider
;
425 if (provider
!= null) {
428 if (deleteFiles
&& (fHtBackend
!= null)) {
429 fHtBackend
.removeFiles();
433 private void build(ITmfStateProvider provider
) {
434 if ((fStateSystem
== null) || (fHtBackend
== null)) {
435 throw new IllegalArgumentException();
438 ITmfEventRequest request
= fRequest
;
439 if ((request
!= null) && (!request
.isCompleted())) {
443 fTimeRange
= TmfTimeRange
.ETERNITY
;
444 final ITmfTrace trace
= provider
.getTrace();
445 if (!isCompleteTrace(trace
)) {
446 fTimeRange
= trace
.getTimeRange();
449 fStateProvider
= provider
;
450 synchronized (fRequestSyncObj
) {
455 * The state system object is now created, we can consider this module
456 * "initialized" (components can retrieve it and start doing queries).
461 * Block the executeAnalysis() construction is complete (so that the
462 * progress monitor displays that it is running).
465 if (fRequest
!= null) {
466 fRequest
.waitForCompletion();
468 } catch (InterruptedException e
) {
473 private class StateSystemEventRequest
extends TmfEventRequest
{
474 private final ITmfStateProvider sci
;
475 private final ITmfTrace trace
;
477 public StateSystemEventRequest(ITmfStateProvider sp
, TmfTimeRange timeRange
, int index
) {
478 super(ITmfEvent
.class,
481 ITmfEventRequest
.ALL_DATA
,
482 ITmfEventRequest
.ExecutionType
.BACKGROUND
,
483 TmfStateSystemAnalysisModule
.this.getDependencyLevel());
485 trace
= sci
.getTrace();
490 public void handleData(final ITmfEvent event
) {
491 super.handleData(event
);
492 if (event
.getTrace() == trace
) {
493 sci
.processEvent(event
);
494 } else if (trace
instanceof TmfExperiment
) {
496 * If the request is for an experiment, check if the event is
497 * from one of the child trace
499 for (ITmfTrace childTrace
: ((TmfExperiment
) trace
).getTraces()) {
500 if (childTrace
== event
.getTrace()) {
501 sci
.processEvent(event
);
508 public void handleSuccess() {
509 super.handleSuccess();
510 if (isCompleteTrace(trace
)) {
511 disposeProvider(false);
513 fNbRead
+= getNbRead();
514 synchronized (fRequestSyncObj
) {
515 final TmfTimeRange timeRange
= fTimeRange
;
516 if (timeRange
!= null) {
517 if (getRange().getEndTime().getValue() < timeRange
.getEndTime().getValue()) {
526 public void handleCancel() {
527 super.handleCancel();
528 if (isCompleteTrace(trace
)) {
529 disposeProvider(true);
534 public void handleFailure() {
535 super.handleFailure();
536 disposeProvider(true);
540 // ------------------------------------------------------------------------
541 // ITmfAnalysisModuleWithStateSystems
542 // ------------------------------------------------------------------------
546 public ITmfStateSystem
getStateSystem(String id
) {
547 if (id
.equals(getId())) {
554 public @NonNull Iterable
<@NonNull ITmfStateSystem
> getStateSystems() {
555 ITmfStateSystemBuilder stateSystem
= fStateSystem
;
556 if (stateSystem
== null) {
557 return Collections
.EMPTY_SET
;
559 return Collections
.singleton(stateSystem
);
563 * Signal handler for the TmfTraceRangeUpdatedSignal signal
565 * @param signal The incoming signal
568 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
569 fTimeRange
= signal
.getRange();
570 ITmfStateProvider stateProvider
= fStateProvider
;
571 synchronized (fRequestSyncObj
) {
572 if (signal
.getTrace() == getTrace() && stateProvider
!= null && stateProvider
.getAssignedStateSystem() != null) {
573 ITmfEventRequest request
= fRequest
;
574 if ((request
== null) || request
.isCompleted()) {
581 private void startRequest() {
582 ITmfStateProvider stateProvider
= fStateProvider
;
583 TmfTimeRange timeRange
= fTimeRange
;
584 if (stateProvider
== null || timeRange
== null) {
587 ITmfEventRequest request
= new StateSystemEventRequest(stateProvider
, timeRange
, fNbRead
);
588 stateProvider
.getTrace().sendRequest(request
);
592 private static boolean isCompleteTrace(ITmfTrace trace
) {
593 return !(trace
instanceof ITmfTraceCompleteness
) || ((ITmfTraceCompleteness
) trace
).isComplete();
596 // ------------------------------------------------------------------------
597 // ITmfPropertiesProvider
598 // ------------------------------------------------------------------------
604 public @NonNull Map
<@NonNull String
, @NonNull String
> getProperties() {
605 Map
<@NonNull String
, @NonNull String
> properties
= super.getProperties();
607 StateSystemBackendType backend
= getBackendType();
608 properties
.put(NonNullUtils
.checkNotNull(Messages
.TmfStateSystemAnalysisModule_PropertiesBackend
), backend
.name());
612 File htFile
= getSsFile();
613 if (htFile
!= null) {
614 if (htFile
.exists()) {
615 properties
.put(NonNullUtils
.checkNotNull(Messages
.TmfStateSystemAnalysisModule_PropertiesFileSize
), FileUtils
.byteCountToDisplaySize(htFile
.length()));
617 properties
.put(NonNullUtils
.checkNotNull(Messages
.TmfStateSystemAnalysisModule_PropertiesFileSize
), NonNullUtils
.checkNotNull(Messages
.TmfStateSystemAnalysisModule_PropertiesAnalysisNotExecuted
));