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 * Bernd Hufmann - Updated for added interfaces to ITmfEventProvider
17 *******************************************************************************/
19 package org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
;
22 import java
.nio
.ByteBuffer
;
23 import java
.util
.Collections
;
24 import java
.util
.List
;
25 import java
.util
.concurrent
.locks
.Lock
;
26 import java
.util
.concurrent
.locks
.ReentrantLock
;
28 import org
.eclipse
.core
.resources
.IProject
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.CoreException
;
31 import org
.eclipse
.core
.runtime
.IStatus
;
32 import org
.eclipse
.core
.runtime
.MultiStatus
;
33 import org
.eclipse
.core
.runtime
.Status
;
34 import org
.eclipse
.jdt
.annotation
.Nullable
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfExperimentContext
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfExperimentLocation
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfLocationArray
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSynchronizedSignal
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.SynchronizationAlgorithm
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.SynchronizationManager
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
62 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
63 * that are part of a tracing experiment.
66 * @author Francois Chouinard
68 public class TmfExperiment
extends TmfTrace
implements ITmfPersistentlyIndexable
{
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * The file name of the Synchronization
77 * @deprecated This file name shouldn't be used directly anymore. All
78 * synchronization files have been moved to a folder and you
79 * should use the {@link #getSynchronizationFolder(boolean)}
80 * method to return the path to this folder.
83 public static final String SYNCHRONIZATION_FILE_NAME
= "synchronization.bin"; //$NON-NLS-1$
86 * The name of the directory containing trace synchronization data. This
87 * directory typically will be preserved when traces are synchronized.
88 * Analysis involved in synchronization can put their supplementary files in
89 * there so they are not deleted when synchronized traces are copied.
91 private static final String SYNCHRONIZATION_DIRECTORY
= "sync_data"; //$NON-NLS-1$
94 * The default index page size
96 public static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 * The set of traces that constitute the experiment
105 private boolean fInitialized
= false;
108 * Lock for synchronization methods. These methods cannot be 'synchronized'
109 * since it makes it impossible to use an event request on the experiment
110 * during synchronization (the request thread would block)
112 private final Lock fSyncLock
= new ReentrantLock();
114 // ------------------------------------------------------------------------
116 // ------------------------------------------------------------------------
119 * Default constructor. Should not be used directly, but is needed for
122 * @deprecated Do not call this directly (but do not remove it either!)
125 public TmfExperiment() {
130 * Constructor of an experiment, taking the type, path, traces,
131 * indexPageSize and resource
136 * The experiment path
138 * The experiment set of traces
139 * @param indexPageSize
140 * The experiment index page size. You can use
141 * {@link TmfExperiment#DEFAULT_INDEX_PAGE_SIZE} for a default
144 * The resource associated to the experiment. You can use 'null'
145 * for no resources (tests, etc.)
147 public TmfExperiment(final Class
<?
extends ITmfEvent
> type
,
149 final ITmfTrace
[] traces
,
150 final int indexPageSize
,
151 final @Nullable IResource resource
) {
152 initExperiment(type
, path
, traces
, indexPageSize
, resource
);
156 protected ITmfTraceIndexer
createIndexer(int interval
) {
157 if (getCheckpointSize() > 0) {
158 return new TmfBTreeTraceIndexer(this, interval
);
160 return super.createIndexer(interval
);
164 * Clears the experiment
167 public synchronized void dispose() {
169 // Clean up the index if applicable
170 if (getIndexer() != null) {
171 getIndexer().dispose();
177 // ------------------------------------------------------------------------
178 // ITmfTrace - Initializers
179 // ------------------------------------------------------------------------
182 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) {
183 /* Do nothing for experiments */
187 * Initialization of an experiment, taking the type, path, traces,
188 * indexPageSize and resource
193 * the experiment path
195 * the experiment set of traces
196 * @param indexPageSize
197 * the experiment index page size
199 * the resource associated to the experiment
201 public void initExperiment(final Class
<?
extends ITmfEvent
> type
,
203 final ITmfTrace
[] traces
,
204 final int indexPageSize
,
205 final @Nullable IResource resource
) {
207 setCacheSize(indexPageSize
);
208 setStreamingInterval(0);
210 // traces have to be set before super.initialize()
211 if (traces
!= null) {
213 for (ITmfTrace trace
: traces
) {
221 super.initialize(resource
, path
, type
);
222 } catch (TmfTraceException e
) {
223 Activator
.logError("Error initializing experiment", e
); //$NON-NLS-1$
226 if (resource
!= null) {
227 this.synchronizeTraces();
232 public IStatus
validate(final IProject project
, final String path
) {
233 return Status
.OK_STATUS
;
236 // ------------------------------------------------------------------------
238 // ------------------------------------------------------------------------
241 * Get the traces contained in this experiment.
243 * @return The array of contained traces
245 public List
<ITmfTrace
> getTraces() {
246 return getChildren(ITmfTrace
.class);
250 * Returns the timestamp of the event at the requested index. If none,
254 * the event index (rank)
255 * @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 // ------------------------------------------------------------------------
269 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
271 // Make sure we have something to read from
272 if (getChildren().isEmpty()) {
276 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
277 && request
.getIndex() == 0) {
278 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
279 request
.setStartIndex((int) context
.getRank());
284 return seekEvent(request
.getIndex());
287 // ------------------------------------------------------------------------
288 // ITmfTrace trace positioning
289 // ------------------------------------------------------------------------
292 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
293 // Validate the location
294 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
295 return null; // Throw an exception?
298 int length
= getNbChildren();
300 // Initialize the location array if necessary
301 TmfLocationArray locationArray
= ((location
== null) ?
302 new TmfLocationArray(length
) :
303 ((TmfExperimentLocation
) location
).getLocationInfo());
305 ITmfLocation
[] locations
= locationArray
.getLocations();
306 long[] ranks
= locationArray
.getRanks();
308 // Create and populate the context's traces contexts
309 final TmfExperimentContext context
= new TmfExperimentContext(length
);
311 // Position the traces
313 for (int i
= 0; i
< length
; i
++) {
314 // Get the relevant trace attributes
315 final ITmfContext traceContext
= ((ITmfTrace
) getChild(i
)).seekEvent(locations
[i
]);
316 context
.setContext(i
, traceContext
);
317 traceContext
.setRank(ranks
[i
]);
318 // update location after seek
319 locations
[i
] = traceContext
.getLocation();
320 context
.setEvent(i
, ((ITmfTrace
) getChild(i
)).getNext(traceContext
));
325 context
.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations
, ranks
)));
326 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
327 context
.setRank(rank
);
332 // ------------------------------------------------------------------------
333 // ITmfTrace - SeekEvent operations (returning a trace context)
334 // ------------------------------------------------------------------------
337 public ITmfContext
seekEvent(final double ratio
) {
338 final ITmfContext context
= seekEvent(Math
.round(ratio
* getNbEvents()));
343 public double getLocationRatio(final ITmfLocation location
) {
344 if (location
instanceof TmfExperimentLocation
) {
346 TmfLocationArray locationArray
= ((TmfExperimentLocation
) location
).getLocationInfo();
347 for (int i
= 0; i
< locationArray
.size(); i
++) {
348 rank
+= locationArray
.getRank(i
);
350 return (double) rank
/ getNbEvents();
356 public ITmfLocation
getCurrentLocation() {
361 // ------------------------------------------------------------------------
362 // ITmfTrace trace positioning
363 // ------------------------------------------------------------------------
366 public synchronized ITmfEvent
parseEvent(final ITmfContext context
) {
367 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
368 final ITmfEvent event
= getNext(tmpContext
);
373 public synchronized ITmfEvent
getNext(ITmfContext context
) {
375 // Validate the context
376 if (!(context
instanceof TmfExperimentContext
)) {
377 return null; // Throw an exception?
380 int length
= getNbChildren();
382 // Make sure that we have something to read from
387 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
389 // If an event was consumed previously, first get the next one from that
391 final int lastTrace
= expContext
.getLastTrace();
392 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
393 final ITmfContext traceContext
= expContext
.getContext(lastTrace
);
394 expContext
.setEvent(lastTrace
, ((ITmfTrace
) getChild(lastTrace
)).getNext(traceContext
));
395 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
398 // Scan the candidate events and identify the "next" trace to read from
399 int trace
= TmfExperimentContext
.NO_TRACE
;
400 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
401 for (int i
= 0; i
< length
; i
++) {
402 final ITmfEvent event
= expContext
.getEvent(i
);
405 final ITmfTimestamp otherTS
= event
.getTimestamp();
406 if (otherTS
.compareTo(timestamp
) < 0) {
413 ITmfEvent event
= null;
414 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
415 event
= expContext
.getEvent(trace
);
417 updateAttributes(expContext
, event
.getTimestamp());
418 expContext
.increaseRank();
419 expContext
.setLastTrace(trace
);
420 final ITmfContext traceContext
= expContext
.getContext(trace
);
421 if (traceContext
== null) {
422 throw new IllegalStateException();
425 // Update the experiment location
426 TmfLocationArray locationArray
= new TmfLocationArray(
427 ((TmfExperimentLocation
) expContext
.getLocation()).getLocationInfo(),
428 trace
, traceContext
.getLocation(), traceContext
.getRank());
429 expContext
.setLocation(new TmfExperimentLocation(locationArray
));
437 public ITmfTimestamp
getInitialRangeOffset() {
439 List
<ITmfTrace
> children
= getChildren(ITmfTrace
.class);
441 if (children
.isEmpty()) {
442 return super.getInitialRangeOffset();
445 ITmfTimestamp initTs
= TmfTimestamp
.BIG_CRUNCH
;
446 for (ITmfTrace trace
: children
) {
447 ITmfTimestamp ts
= (trace
).getInitialRangeOffset();
448 if (ts
.compareTo(initTs
) < 0) {
456 * Get the path to the folder in the supplementary file where
457 * synchronization-related data can be kept so they are not deleted when the
458 * experiment is synchronized. Analysis involved in synchronization can put
459 * their supplementary files in there so they are preserved after
462 * If the directory does not exist, it will be created. A return value of
463 * <code>null</code> means either the trace resource does not exist or
464 * supplementary resources cannot be kept.
467 * If <code>true</code>, it returns the absolute path in the file
468 * system, including the supplementary file path. Otherwise, it
469 * returns only the directory name.
470 * @return The path to the folder where synchronization-related
471 * supplementary files can be kept or <code>null</code> if not
474 public String
getSynchronizationFolder(boolean absolute
) {
475 /* Set up the path to the synchronization file we'll use */
476 IResource resource
= this.getResource();
477 String syncDirectory
= null;
480 /* get the directory where the file will be stored. */
481 if (resource
!= null) {
482 String fullDirectory
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
483 /* Create the synchronization data directory if not present */
484 if (fullDirectory
!= null) {
485 fullDirectory
= fullDirectory
+ File
.separator
+ SYNCHRONIZATION_DIRECTORY
;
486 File syncDir
= new File(fullDirectory
);
490 syncDirectory
= fullDirectory
;
492 syncDirectory
= SYNCHRONIZATION_DIRECTORY
;
495 } catch (CoreException e
) {
499 return syncDirectory
;
503 * Synchronizes the traces of an experiment. By default it only tries to
504 * read a synchronization file if it exists
506 * @return The synchronization object
508 public SynchronizationAlgorithm
synchronizeTraces() {
509 return synchronizeTraces(false);
513 * Synchronizes the traces of an experiment.
516 * Whether to actually synchronize or just try opening a sync
518 * @return The synchronization object
520 public SynchronizationAlgorithm
synchronizeTraces(boolean doSync
) {
524 String syncDirectory
= getSynchronizationFolder(true);
526 final File syncFile
= (syncDirectory
!= null) ?
new File(syncDirectory
+ File
.separator
+ SYNCHRONIZATION_FILE_NAME
) : null;
528 final SynchronizationAlgorithm syncAlgo
= SynchronizationManager
.synchronizeTraces(syncFile
, Collections
.<ITmfTrace
> singleton(this), doSync
);
530 final TmfTraceSynchronizedSignal signal
= new TmfTraceSynchronizedSignal(this, syncAlgo
);
532 /* Broadcast in separate thread to prevent deadlock */
547 @SuppressWarnings("nls")
548 public synchronized String
toString() {
549 return "[TmfExperiment (" + getName() + ")]";
552 // ------------------------------------------------------------------------
554 // ------------------------------------------------------------------------
556 private synchronized void initializeStreamingMonitor() {
563 if (getStreamingInterval() == 0) {
564 final ITmfContext context
= seekEvent(0);
565 final ITmfEvent event
= getNext(context
);
570 final TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp(), TmfTimestamp
.BIG_CRUNCH
);
571 final TmfTraceRangeUpdatedSignal signal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
573 // Broadcast in separate thread to prevent deadlock
583 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
584 private ITmfTimestamp safeTimestamp
= null;
585 private ITmfTimestamp lastSafeTimestamp
= null;
586 private TmfTimeRange timeRange
= null;
590 while (!executorIsShutdown()) {
591 if (!getIndexer().isIndexing()) {
592 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
593 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
595 for (final ITmfTrace trace
: getChildren(ITmfTrace
.class)) {
596 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
597 startTimestamp
= trace
.getStartTime();
599 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
600 endTimestamp
= trace
.getEndTime();
603 if (safeTimestamp
!= null && (lastSafeTimestamp
== null || safeTimestamp
.compareTo(lastSafeTimestamp
) > 0)) {
604 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
605 lastSafeTimestamp
= safeTimestamp
;
609 safeTimestamp
= endTimestamp
;
610 if (timeRange
!= null) {
611 final TmfTraceRangeUpdatedSignal signal
=
612 new TmfTraceRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
617 Thread
.sleep(getStreamingInterval());
618 } catch (final InterruptedException e
) {
628 public long getStreamingInterval() {
630 for (final ITmfTrace trace
: getChildren(ITmfTrace
.class)) {
631 interval
= Math
.max(interval
, trace
.getStreamingInterval());
636 // ------------------------------------------------------------------------
638 // ------------------------------------------------------------------------
642 public void traceOpened(TmfTraceOpenedSignal signal
) {
643 if (signal
.getTrace() == this) {
644 initializeStreamingMonitor();
646 /* Initialize the analysis */
647 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
648 status
.add(executeAnalysis());
649 if (!status
.isOK()) {
650 Activator
.log(status
);
652 TmfTraceManager
.refreshSupplementaryFiles(this);
657 public synchronized int getCheckpointSize() {
658 int totalCheckpointSize
= 0;
660 List
<ITmfTrace
> children
= getChildren(ITmfTrace
.class);
661 for (ITmfTrace trace
: children
) {
662 if (!(trace
instanceof ITmfPersistentlyIndexable
)) {
666 ITmfPersistentlyIndexable persistableIndexTrace
= (ITmfPersistentlyIndexable
) trace
;
667 int currentTraceCheckpointSize
= persistableIndexTrace
.getCheckpointSize();
668 if (currentTraceCheckpointSize
<= 0) {
671 totalCheckpointSize
+= currentTraceCheckpointSize
;
672 // each entry in the TmfLocationArray has a rank in addition
674 totalCheckpointSize
+= 8;
676 } catch (UnsupportedOperationException e
) {
680 return totalCheckpointSize
;
684 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
685 List
<ITmfTrace
> children
= getChildren(ITmfTrace
.class);
686 int length
= children
.size();
687 ITmfLocation
[] locations
= new ITmfLocation
[length
];
688 long[] ranks
= new long[length
];
689 for (int i
= 0; i
< length
; ++i
) {
690 final ITmfTrace trace
= children
.get(i
);
691 locations
[i
] = ((ITmfPersistentlyIndexable
) trace
).restoreLocation(bufferIn
);
692 ranks
[i
] = bufferIn
.getLong();
694 TmfLocationArray arr
= new TmfLocationArray(locations
, ranks
);
695 TmfExperimentLocation l
= new TmfExperimentLocation(arr
);