1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson, É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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
12 * Patrick Tasse - Updated for removal of context clone
13 * Patrick Tasse - Updated for ranks in experiment location
14 * Geneviève Bastien - Added support of experiment synchronization
15 * Added the initExperiment method and default constructor
16 *******************************************************************************/
18 package org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
;
21 import java
.nio
.ByteBuffer
;
22 import java
.util
.Arrays
;
24 import org
.eclipse
.core
.resources
.IProject
;
25 import org
.eclipse
.core
.resources
.IResource
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.core
.runtime
.MultiStatus
;
29 import org
.eclipse
.core
.runtime
.Status
;
30 import org
.eclipse
.jdt
.annotation
.Nullable
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfExperimentContext
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfExperimentLocation
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfLocationArray
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSynchronizedSignal
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.SynchronizationAlgorithm
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.SynchronizationManager
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfEventParser
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
59 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
60 * that are part of a tracing experiment.
63 * @author Francois Chouinard
65 public class TmfExperiment
extends TmfTrace
implements ITmfEventParser
, ITmfPersistentlyIndexable
{
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * The file name of the Synchronization
75 * @deprecated This file name shouldn't be used directly anymore. All
76 * synchronization files have been moved to a folder and you
77 * should use the {@link #getSynchronizationFolder(boolean)}
78 * method to return the path to this folder.
81 public static final String SYNCHRONIZATION_FILE_NAME
= "synchronization.bin"; //$NON-NLS-1$
84 * The name of the directory containing trace synchronization data. This
85 * directory typically will be preserved when traces are synchronized.
86 * Analysis involved in synchronization can put their supplementary files in
87 * there so they are not deleted when synchronized traces are copied.
89 private static final String SYNCHRONIZATION_DIRECTORY
= "sync_data"; //$NON-NLS-1$
92 * The default index page size
94 public static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 * The set of traces that constitute the experiment
103 protected ITmfTrace
[] fTraces
;
106 * The set of traces that constitute the experiment
108 private boolean fInitialized
= false;
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
116 * Default constructor. Should not be used directly, but is needed for
119 * @deprecated Do not call this directly (but do not remove it either!)
122 public TmfExperiment() {
127 * Constructor of an experiment, taking the type, path, traces,
128 * indexPageSize and resource
133 * The experiment path
135 * The experiment set of traces
136 * @param indexPageSize
137 * The experiment index page size. You can use
138 * {@link TmfExperiment#DEFAULT_INDEX_PAGE_SIZE} for a default
141 * The resource associated to the experiment. You can use 'null'
142 * for no resources (tests, etc.)
144 public TmfExperiment(final Class
<?
extends ITmfEvent
> type
,
146 final ITmfTrace
[] traces
,
147 final int indexPageSize
,
148 final @Nullable IResource resource
) {
149 initExperiment(type
, path
, traces
, indexPageSize
, resource
);
153 protected ITmfTraceIndexer
createIndexer(int interval
) {
154 if (getCheckpointSize() > 0) {
155 return new TmfBTreeTraceIndexer(this, interval
);
157 return super.createIndexer(interval
);
161 * Clears the experiment
164 public synchronized void dispose() {
166 // Clean up the index if applicable
167 if (getIndexer() != null) {
168 getIndexer().dispose();
171 if (fTraces
!= null) {
172 for (final ITmfTrace trace
: fTraces
) {
180 // ------------------------------------------------------------------------
181 // ITmfTrace - Initializers
182 // ------------------------------------------------------------------------
185 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) {
186 /* Do nothing for experiments */
190 * Initialization of an experiment, taking the type, path, traces,
191 * indexPageSize and resource
196 * the experiment path
198 * the experiment set of traces
199 * @param indexPageSize
200 * the experiment index page size
202 * the resource associated to the experiment
205 public void initExperiment(final Class
<?
extends ITmfEvent
> type
,
207 final ITmfTrace
[] traces
,
208 final int indexPageSize
,
209 final @Nullable IResource resource
) {
211 setCacheSize(indexPageSize
);
212 setStreamingInterval(0);
214 // traces have to be set before super.initialize()
217 super.initialize(resource
, path
, type
);
218 } catch (TmfTraceException e
) {
219 Activator
.logError("Error initializing experiment", e
); //$NON-NLS-1$
222 if (resource
!= null) {
223 this.synchronizeTraces();
231 public IStatus
validate(final IProject project
, final String path
) {
232 return Status
.OK_STATUS
;
235 // ------------------------------------------------------------------------
237 // ------------------------------------------------------------------------
240 * Get the traces contained in this experiment.
242 * @return The array of contained traces
244 public ITmfTrace
[] getTraces() {
249 * Returns the timestamp of the event at the requested index. If none,
253 * the event index (rank)
254 * @return the corresponding event timestamp
257 public ITmfTimestamp
getTimestamp(final int index
) {
258 final ITmfContext context
= seekEvent(index
);
259 final ITmfEvent event
= getNext(context
);
261 return (event
!= null) ? event
.getTimestamp() : null;
264 // ------------------------------------------------------------------------
265 // Request management
266 // ------------------------------------------------------------------------
272 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
274 // Make sure we have something to read from
275 if (fTraces
== null) {
279 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
280 && request
.getIndex() == 0) {
281 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
282 request
.setStartIndex((int) context
.getRank());
287 return seekEvent(request
.getIndex());
290 // ------------------------------------------------------------------------
291 // ITmfTrace trace positioning
292 // ------------------------------------------------------------------------
298 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
299 // Validate the location
300 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
301 return null; // Throw an exception?
303 // Make sure we have something to read from
304 if (fTraces
== null) {
308 // Initialize the location array if necessary
309 TmfLocationArray locationArray
= ((location
== null) ?
310 new TmfLocationArray(fTraces
.length
) :
311 ((TmfExperimentLocation
) location
).getLocationInfo());
313 ITmfLocation
[] locations
= locationArray
.getLocations();
314 long[] ranks
= locationArray
.getRanks();
316 // Create and populate the context's traces contexts
317 final TmfExperimentContext context
= new TmfExperimentContext(fTraces
.length
);
319 // Position the traces
321 for (int i
= 0; i
< fTraces
.length
; i
++) {
322 // Get the relevant trace attributes
323 final ITmfContext traceContext
= fTraces
[i
].seekEvent(locations
[i
]);
324 context
.setContext(i
, traceContext
);
325 traceContext
.setRank(ranks
[i
]);
326 // update location after seek
327 locations
[i
] = traceContext
.getLocation();
328 context
.setEvent(i
, fTraces
[i
].getNext(traceContext
));
333 context
.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations
, ranks
)));
334 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
335 context
.setRank(rank
);
340 // ------------------------------------------------------------------------
341 // ITmfTrace - SeekEvent operations (returning a trace context)
342 // ------------------------------------------------------------------------
345 public ITmfContext
seekEvent(final double ratio
) {
346 final ITmfContext context
= seekEvent(Math
.round(ratio
* getNbEvents()));
354 public double getLocationRatio(final ITmfLocation location
) {
355 if (location
instanceof TmfExperimentLocation
) {
357 TmfLocationArray locationArray
= ((TmfExperimentLocation
) location
).getLocationInfo();
358 for (int i
= 0; i
< locationArray
.size(); i
++) {
359 rank
+= locationArray
.getRank(i
);
361 return (double) rank
/ getNbEvents();
370 public ITmfLocation
getCurrentLocation() {
375 // ------------------------------------------------------------------------
376 // ITmfTrace trace positioning
377 // ------------------------------------------------------------------------
380 public synchronized ITmfEvent
parseEvent(final ITmfContext context
) {
381 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
382 final ITmfEvent event
= getNext(tmpContext
);
387 public synchronized ITmfEvent
getNext(ITmfContext context
) {
389 // Validate the context
390 if (!(context
instanceof TmfExperimentContext
)) {
391 return null; // Throw an exception?
394 // Make sure that we have something to read from
395 if (fTraces
== null) {
399 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
401 // If an event was consumed previously, first get the next one from that
403 final int lastTrace
= expContext
.getLastTrace();
404 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
405 final ITmfContext traceContext
= expContext
.getContext(lastTrace
);
406 expContext
.setEvent(lastTrace
, fTraces
[lastTrace
].getNext(traceContext
));
407 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
410 // Scan the candidate events and identify the "next" trace to read from
411 int trace
= TmfExperimentContext
.NO_TRACE
;
412 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
413 for (int i
= 0; i
< fTraces
.length
; i
++) {
414 final ITmfEvent event
= expContext
.getEvent(i
);
415 if (event
!= null && event
.getTimestamp() != null) {
416 final ITmfTimestamp otherTS
= event
.getTimestamp();
417 if (otherTS
.compareTo(timestamp
) < 0) {
424 ITmfEvent event
= null;
425 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
426 event
= expContext
.getEvent(trace
);
428 updateAttributes(expContext
, event
.getTimestamp());
429 expContext
.increaseRank();
430 expContext
.setLastTrace(trace
);
431 final ITmfContext traceContext
= expContext
.getContext(trace
);
432 if (traceContext
== null) {
433 throw new IllegalStateException();
436 // Update the experiment location
437 TmfLocationArray locationArray
= new TmfLocationArray(
438 ((TmfExperimentLocation
) expContext
.getLocation()).getLocationInfo(),
439 trace
, traceContext
.getLocation(), traceContext
.getRank());
440 expContext
.setLocation(new TmfExperimentLocation(locationArray
));
453 public ITmfTimestamp
getInitialRangeOffset() {
454 if ((fTraces
== null) || (fTraces
.length
== 0)) {
455 return super.getInitialRangeOffset();
458 ITmfTimestamp initTs
= TmfTimestamp
.BIG_CRUNCH
;
459 for (int i
= 0; i
< fTraces
.length
; i
++) {
460 ITmfTimestamp ts
= fTraces
[i
].getInitialRangeOffset();
461 if (ts
.compareTo(initTs
) < 0) {
469 * Get the path to the folder in the supplementary file where
470 * synchronization-related data can be kept so they are not deleted when the
471 * experiment is synchronized. Analysis involved in synchronization can put
472 * their supplementary files in there so they are preserved after
475 * If the directory does not exist, it will be created. A return value of
476 * <code>null</code> means either the trace resource does not exist or
477 * supplementary resources cannot be kept.
480 * If <code>true</code>, it returns the absolute path in the file
481 * system, including the supplementary file path. Otherwise, it
482 * returns only the directory name.
483 * @return The path to the folder where synchronization-related
484 * supplementary files can be kept or <code>null</code> if not
488 public String
getSynchronizationFolder(boolean absolute
) {
489 /* Set up the path to the synchronization file we'll use */
490 IResource resource
= this.getResource();
491 String syncDirectory
= null;
494 /* get the directory where the file will be stored. */
495 if (resource
!= null) {
496 String fullDirectory
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
497 /* Create the synchronization data directory if not present */
498 if (fullDirectory
!= null) {
499 fullDirectory
= fullDirectory
+ File
.separator
+ SYNCHRONIZATION_DIRECTORY
;
500 File syncDir
= new File(fullDirectory
);
504 syncDirectory
= fullDirectory
;
506 syncDirectory
= SYNCHRONIZATION_DIRECTORY
;
509 } catch (CoreException e
) {
513 return syncDirectory
;
517 * Synchronizes the traces of an experiment. By default it only tries to
518 * read a synchronization file if it exists
520 * @return The synchronization object
523 public synchronized SynchronizationAlgorithm
synchronizeTraces() {
524 return synchronizeTraces(false);
528 * Synchronizes the traces of an experiment.
531 * Whether to actually synchronize or just try opening a sync
533 * @return The synchronization object
536 public synchronized SynchronizationAlgorithm
synchronizeTraces(boolean doSync
) {
538 String syncDirectory
= getSynchronizationFolder(true);
540 final File syncFile
= (syncDirectory
!= null) ?
new File(syncDirectory
+ File
.separator
+ SYNCHRONIZATION_FILE_NAME
) : null;
542 final SynchronizationAlgorithm syncAlgo
= SynchronizationManager
.synchronizeTraces(syncFile
, Arrays
.asList(fTraces
), doSync
);
544 final TmfTraceSynchronizedSignal signal
= new TmfTraceSynchronizedSignal(this, syncAlgo
);
546 /* Broadcast in separate thread to prevent deadlock */
558 @SuppressWarnings("nls")
559 public synchronized String
toString() {
560 return "[TmfExperiment (" + getName() + ")]";
563 // ------------------------------------------------------------------------
565 // ------------------------------------------------------------------------
567 private synchronized void initializeStreamingMonitor() {
574 if (getStreamingInterval() == 0) {
575 final ITmfContext context
= seekEvent(0);
576 final ITmfEvent event
= getNext(context
);
581 final TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp(), TmfTimestamp
.BIG_CRUNCH
);
582 final TmfTraceRangeUpdatedSignal signal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
584 // Broadcast in separate thread to prevent deadlock
594 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
595 private ITmfTimestamp safeTimestamp
= null;
596 private ITmfTimestamp lastSafeTimestamp
= null;
597 private TmfTimeRange timeRange
= null;
601 while (!executorIsShutdown()) {
602 if (!getIndexer().isIndexing()) {
603 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
604 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
605 for (final ITmfTrace trace
: fTraces
) {
606 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
607 startTimestamp
= trace
.getStartTime();
609 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
610 endTimestamp
= trace
.getEndTime();
613 if (safeTimestamp
!= null && (lastSafeTimestamp
== null || safeTimestamp
.compareTo(lastSafeTimestamp
) > 0)) {
614 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
615 lastSafeTimestamp
= safeTimestamp
;
619 safeTimestamp
= endTimestamp
;
620 if (timeRange
!= null) {
621 final TmfTraceRangeUpdatedSignal signal
=
622 new TmfTraceRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
627 Thread
.sleep(getStreamingInterval());
628 } catch (final InterruptedException e
) {
638 public long getStreamingInterval() {
640 for (final ITmfTrace trace
: fTraces
) {
641 interval
= Math
.max(interval
, trace
.getStreamingInterval());
646 // ------------------------------------------------------------------------
648 // ------------------------------------------------------------------------
652 public void traceOpened(TmfTraceOpenedSignal signal
) {
653 if (signal
.getTrace() == this) {
654 initializeStreamingMonitor();
656 /* Initialize the analysis */
657 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
658 status
.add(executeAnalysis());
659 if (!status
.isOK()) {
660 Activator
.log(status
);
662 TmfTraceManager
.refreshSupplementaryFiles(this);
670 public synchronized int getCheckpointSize() {
671 int totalCheckpointSize
= 0;
673 if (fTraces
!= null) {
674 for (final ITmfTrace trace
: fTraces
) {
675 if (!(trace
instanceof ITmfPersistentlyIndexable
)) {
679 ITmfPersistentlyIndexable persistableIndexTrace
= (ITmfPersistentlyIndexable
) trace
;
680 int currentTraceCheckpointSize
= persistableIndexTrace
.getCheckpointSize();
681 if (currentTraceCheckpointSize
<= 0) {
684 totalCheckpointSize
+= currentTraceCheckpointSize
;
685 // each entry in the TmfLocationArray has a rank in addition
687 totalCheckpointSize
+= 8;
690 } catch (UnsupportedOperationException e
) {
694 return totalCheckpointSize
;
701 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
702 ITmfLocation
[] locations
= new ITmfLocation
[fTraces
.length
];
703 long[] ranks
= new long[fTraces
.length
];
704 for (int i
= 0; i
< fTraces
.length
; ++i
) {
705 final ITmfTrace trace
= fTraces
[i
];
706 locations
[i
] = ((ITmfPersistentlyIndexable
) trace
).restoreLocation(bufferIn
);
707 ranks
[i
] = bufferIn
.getLong();
709 TmfLocationArray arr
= new TmfLocationArray(locations
, ranks
);
710 TmfExperimentLocation l
= new TmfExperimentLocation(arr
);