1 /*******************************************************************************
2 * Copyright (c) 2009, 2015 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 * Geneviève Bastien - Added timestamp transforms, its saving to file and
14 * timestamp creation functions
15 *******************************************************************************/
17 package org
.eclipse
.tracecompass
.tmf
.core
.trace
;
19 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
22 import java
.util
.Collection
;
23 import java
.util
.Collections
;
24 import java
.util
.HashSet
;
25 import java
.util
.LinkedHashMap
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.IStatus
;
31 import org
.eclipse
.core
.runtime
.MultiStatus
;
32 import org
.eclipse
.core
.runtime
.Path
;
33 import org
.eclipse
.core
.runtime
.Status
;
34 import org
.eclipse
.jdt
.annotation
.NonNull
;
35 import org
.eclipse
.jdt
.annotation
.Nullable
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfEventProvider
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfLostEvent
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
63 import com
.google
.common
.collect
.ImmutableList
;
66 * Abstract implementation of ITmfTrace.
68 * Since the concept of 'location' is trace specific, the concrete classes have
69 * to provide the related methods, namely:
71 * <li> public ITmfLocation<?> getCurrentLocation()
72 * <li> public double getLocationRatio(ITmfLocation<?> location)
73 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
74 * <li> public ITmfContext seekEvent(double ratio)
75 * <li> public IStatus validate(IProject project, String path)
78 * When constructing an event, the concrete trace should use the trace's
79 * timestamp transform to create the timestamp, by either transforming the
80 * parsed time value directly or by using the method createTimestamp().
82 * The concrete class can either specify its own indexer or use the provided
83 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
84 * used as checkpoint interval.
87 * @author Francois Chouinard
90 * @see ITmfTraceIndexer
91 * @see ITmfEventParser
93 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
, ITmfEventParser
, ITmfTraceCompleteness
{
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 * Basic aspects that should be valid for all trace types.
102 public static final @NonNull Collection
<@NonNull ITmfEventAspect
> BASE_ASPECTS
=
103 checkNotNull(ImmutableList
.of(
104 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
105 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
106 ITmfEventAspect
.BaseAspects
.CONTENTS
109 // ------------------------------------------------------------------------
110 // Instance attributes
111 // ------------------------------------------------------------------------
113 // The resource used for persistent properties for this trace
114 private IResource fResource
;
117 private @Nullable String fTraceTypeId
;
120 private String fPath
;
122 // The trace cache page size
123 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
125 // The number of events collected (so far)
126 private volatile long fNbEvents
= 0;
128 // The time span of the event stream
129 private @NonNull ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
130 private @NonNull ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
132 // The trace streaming interval (0 = no streaming)
133 private long fStreamingInterval
= 0;
136 private ITmfTraceIndexer fIndexer
;
138 private ITmfTimestampTransform fTsTransform
;
140 private final Map
<String
, IAnalysisModule
> fAnalysisModules
=
141 Collections
.synchronizedMap(new LinkedHashMap
<String
, IAnalysisModule
>());
143 // ------------------------------------------------------------------------
145 // ------------------------------------------------------------------------
148 * The default, parameterless, constructor
152 fIndexer
= new TmfCheckpointIndexer(this);
159 * The resource associated to the trace
161 * The type of events that will be read from this trace
163 * The path to the trace on the filesystem
165 * The trace cache size. Pass '-1' to use the default specified
166 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
168 * The trace streaming interval. You can use '0' for post-mortem
170 * @throws TmfTraceException
171 * If something failed during the opening
173 protected TmfTrace(final IResource resource
,
174 final Class
<?
extends ITmfEvent
> type
,
178 throws TmfTraceException
{
180 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
181 fStreamingInterval
= interval
;
182 initialize(resource
, path
, type
);
188 * @param trace the original trace
189 * @throws TmfTraceException Should not happen usually
191 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
194 throw new IllegalArgumentException();
196 fCacheSize
= trace
.getCacheSize();
197 fStreamingInterval
= trace
.getStreamingInterval();
198 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
202 * Creates the indexer instance. Classes extending this class can override
203 * this to provide a different indexer implementation.
205 * @param interval the checkpoints interval
207 * @return the indexer
209 protected ITmfTraceIndexer
createIndexer(int interval
) {
210 return new TmfCheckpointIndexer(this, interval
);
213 // ------------------------------------------------------------------------
214 // ITmfTrace - Initializers
215 // ------------------------------------------------------------------------
218 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
, String traceTypeId
) throws TmfTraceException
{
220 throw new IllegalArgumentException();
223 fTraceTypeId
= traceTypeId
;
224 initTrace(resource
, path
, type
);
228 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
229 initialize(resource
, path
, type
);
233 * Initialize the trace common attributes and the base component.
235 * @param resource the Eclipse resource (trace)
236 * @param path the trace path
237 * @param type the trace event type
239 * @throws TmfTraceException If something failed during the initialization
241 protected void initialize(final IResource resource
,
243 final Class
<?
extends ITmfEvent
> type
)
244 throws TmfTraceException
{
246 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
249 fResource
= resource
;
250 String traceName
= getName();
251 if (traceName
.isEmpty()) {
252 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
254 super.init(traceName
, type
);
255 // register as VIP after super.init() because TmfComponent registers to signal manager there
256 TmfSignalManager
.registerVIP(this);
257 if (fIndexer
!= null) {
260 fIndexer
= createIndexer(fCacheSize
);
264 * Indicates if the path points to an existing file/directory
266 * @param path the path to test
267 * @return true if the file/directory exists
269 protected boolean fileExists(final String path
) {
270 final File file
= new File(path
);
271 return file
.exists();
275 public void indexTrace(boolean waitForCompletion
) {
276 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
280 * Instantiate the applicable analysis modules and executes the analysis
281 * modules that are meant to be automatically executed
283 * @return An IStatus indicating whether the analysis could be run
284 * successfully or not
286 protected IStatus
executeAnalysis() {
287 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
289 /* First modules are initialized */
290 Map
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules(this.getClass());
291 for (IAnalysisModuleHelper helper
: modules
.values()) {
293 IAnalysisModule module
= helper
.newModule(this);
294 if (module
== null) {
297 fAnalysisModules
.put(module
.getId(), module
);
298 } catch (TmfAnalysisException e
) {
299 status
.add(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, e
.getMessage()));
303 /* Once all modules are initialized, automatic modules are executed */
304 for (IAnalysisModule module
: getAnalysisModules()) {
305 if (module
.isAutomatic()) {
306 status
.add(module
.schedule());
313 public IAnalysisModule
getAnalysisModule(String analysisId
) {
314 return fAnalysisModules
.get(analysisId
);
319 public Iterable
<IAnalysisModule
> getAnalysisModules() {
320 synchronized (fAnalysisModules
) {
321 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
327 public Iterable
<ITmfEventAspect
> getEventAspects() {
328 /* By default we provide only the base aspects valid for all trace types */
336 public synchronized void dispose() {
337 /* Clean up the index if applicable */
338 if (getIndexer() != null) {
339 getIndexer().dispose();
342 /* Clean up the analysis modules */
343 synchronized (fAnalysisModules
) {
344 for (IAnalysisModule module
: fAnalysisModules
.values()) {
347 fAnalysisModules
.clear();
353 // ------------------------------------------------------------------------
354 // ITmfTrace - Basic getters
355 // ------------------------------------------------------------------------
358 public IResource
getResource() {
363 public @Nullable String
getTraceTypeId() {
368 public String
getPath() {
373 public int getCacheSize() {
378 public long getStreamingInterval() {
379 return fStreamingInterval
;
383 * @return the trace indexer
385 protected ITmfTraceIndexer
getIndexer() {
389 // ------------------------------------------------------------------------
390 // ITmfTrace - Trace characteristics getters
391 // ------------------------------------------------------------------------
394 public long getNbEvents() {
399 public @NonNull TmfTimeRange
getTimeRange() {
400 return new TmfTimeRange(fStartTime
, fEndTime
);
404 public ITmfTimestamp
getStartTime() {
409 public ITmfTimestamp
getEndTime() {
414 public ITmfTimestamp
getInitialRangeOffset() {
415 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
416 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
420 public String
getHostId() {
421 return this.getName();
424 // ------------------------------------------------------------------------
425 // Convenience setters
426 // ------------------------------------------------------------------------
429 * Set the trace cache size. Must be done at initialization time.
431 * @param cacheSize The trace cache size
433 protected void setCacheSize(final int cacheSize
) {
434 fCacheSize
= cacheSize
;
438 * Set the trace known number of events. This can be quite dynamic
439 * during indexing or for live traces.
441 * @param nbEvents The number of events
443 protected synchronized void setNbEvents(final long nbEvents
) {
444 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
448 * Update the trace events time range
450 * @param range the new time range
452 protected void setTimeRange(final @NonNull TmfTimeRange range
) {
453 fStartTime
= range
.getStartTime();
454 fEndTime
= range
.getEndTime();
458 * Update the trace chronologically first event timestamp
460 * @param startTime the new first event timestamp
462 protected void setStartTime(final @NonNull ITmfTimestamp startTime
) {
463 fStartTime
= startTime
;
467 * Update the trace chronologically last event timestamp
469 * @param endTime the new last event timestamp
471 protected void setEndTime(final @NonNull ITmfTimestamp endTime
) {
476 * Set the polling interval for live traces (default = 0 = no streaming).
478 * @param interval the new trace streaming interval
480 protected void setStreamingInterval(final long interval
) {
481 fStreamingInterval
= (interval
> 0) ? interval
: 0;
484 // ------------------------------------------------------------------------
485 // ITmfTrace - SeekEvent operations (returning a trace context)
486 // ------------------------------------------------------------------------
489 public synchronized ITmfContext
seekEvent(final long rank
) {
491 // A rank <= 0 indicates to seek the first event
493 ITmfContext context
= seekEvent((ITmfLocation
) null);
498 // Position the trace at the checkpoint
499 final ITmfContext context
= fIndexer
.seekIndex(rank
);
501 // And locate the requested event context
502 long pos
= context
.getRank();
504 ITmfEvent event
= getNext(context
);
505 while ((event
!= null) && (++pos
< rank
)) {
506 event
= getNext(context
);
513 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
515 // A null timestamp indicates to seek the first event
516 if (timestamp
== null) {
517 ITmfContext context
= seekEvent((ITmfLocation
) null);
522 // Position the trace at the checkpoint
523 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
525 // And locate the requested event context
526 ITmfLocation previousLocation
= context
.getLocation();
527 long previousRank
= context
.getRank();
528 ITmfEvent event
= getNext(context
);
529 while (event
!= null && event
.getTimestamp().compareTo(timestamp
) < 0) {
530 previousLocation
= context
.getLocation();
531 previousRank
= context
.getRank();
532 event
= getNext(context
);
535 context
.setLocation(null);
536 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
539 context
= seekEvent(previousLocation
);
540 context
.setRank(previousRank
);
545 // ------------------------------------------------------------------------
546 // Read operations (returning an actual event)
547 // ------------------------------------------------------------------------
550 public abstract ITmfEvent
parseEvent(ITmfContext context
);
553 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
554 // parseEvent() does not update the context
555 final ITmfEvent event
= parseEvent(context
);
557 updateAttributes(context
, event
);
558 context
.setLocation(getCurrentLocation());
559 context
.increaseRank();
565 * Update the trace attributes
567 * @param context the current trace context
568 * @param timestamp the corresponding timestamp
569 * @deprecated Use {@link #updateAttributes(ITmfContext, ITmfEvent)}
572 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfTimestamp timestamp
) {
573 updateAttributes(context
, new TmfEvent(this, context
.getRank(), timestamp
, null, null));
577 * Update the trace attributes
579 * @param context the current trace context
580 * @param event the corresponding event
583 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfEvent event
) {
584 ITmfTimestamp timestamp
= event
.getTimestamp();
585 ITmfTimestamp endTime
= timestamp
;
586 if (event
instanceof ITmfLostEvent
) {
587 endTime
= ((ITmfLostEvent
) event
).getTimeRange().getEndTime();
589 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
590 fStartTime
= timestamp
;
592 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(endTime
) < 0)) {
595 if (context
.hasValidRank()) {
596 long rank
= context
.getRank();
597 if (fNbEvents
<= rank
) {
598 fNbEvents
= rank
+ 1;
600 if (fIndexer
!= null) {
601 fIndexer
.updateIndex(context
, timestamp
);
606 // ------------------------------------------------------------------------
608 // ------------------------------------------------------------------------
611 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
612 if (executorIsShutdown()) {
615 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
616 && (request
.getIndex() == 0)) {
617 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
618 request
.setStartIndex((int) context
.getRank());
622 return seekEvent(request
.getIndex());
625 // ------------------------------------------------------------------------
627 // ------------------------------------------------------------------------
630 * Handler for the Trace Opened signal
633 * The incoming signal
636 public void traceOpened(TmfTraceOpenedSignal signal
) {
637 boolean signalIsForUs
= false;
638 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
640 signalIsForUs
= true;
645 if (!signalIsForUs
) {
650 * The signal is either for this trace, or for an experiment containing
653 IStatus status
= executeAnalysis();
654 if (!status
.isOK()) {
655 Activator
.log(status
);
658 TmfTraceManager
.refreshSupplementaryFiles(this);
660 if (signal
.getTrace() == this) {
661 /* Additionally, the signal is directly for this trace. */
662 if (getNbEvents() == 0) {
666 /* For a streaming trace, the range updated signal should be sent
667 * by the subclass when a new safe time is determined.
669 if (getStreamingInterval() > 0) {
674 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
675 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
677 // Broadcast in separate thread to prevent deadlock
678 broadcastAsync(rangeUpdatedsignal
);
685 * Signal handler for the TmfTraceRangeUpdatedSignal signal
687 * @param signal The incoming signal
690 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
691 if (signal
.getTrace() == this) {
692 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
697 * Signal handler for the TmfTraceUpdatedSignal signal
699 * @param signal The incoming signal
702 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
703 if (signal
.getSource() == getIndexer()) {
704 fNbEvents
= signal
.getNbEvents();
705 fStartTime
= signal
.getRange().getStartTime();
706 fEndTime
= signal
.getRange().getEndTime();
710 // ------------------------------------------------------------------------
711 // Timestamp transformation functions
712 // ------------------------------------------------------------------------
715 public ITmfTimestampTransform
getTimestampTransform() {
716 if (fTsTransform
== null) {
717 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
723 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
725 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
729 public @NonNull ITmfTimestamp
createTimestamp(long ts
) {
730 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
733 // ------------------------------------------------------------------------
735 // ------------------------------------------------------------------------
738 @SuppressWarnings("nls")
739 public synchronized String
toString() {
740 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
741 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
742 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
746 public boolean isComplete() {
748 * Be default, all traces are "complete" which means no more data will
755 public void setComplete(boolean isComplete
) {
757 * This should be overridden by trace classes that can support live
758 * reading (traces in an incomplete state)