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 * Geneviève Bastien - Added timestamp transforms, its saving to file and
14 * timestamp creation functions
15 *******************************************************************************/
17 package org
.eclipse
.tracecompass
.tmf
.core
.trace
;
20 import java
.util
.Collection
;
21 import java
.util
.Collections
;
22 import java
.util
.HashSet
;
23 import java
.util
.LinkedHashMap
;
25 import java
.util
.Map
.Entry
;
28 import org
.eclipse
.core
.resources
.IResource
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.core
.runtime
.MultiStatus
;
31 import org
.eclipse
.core
.runtime
.Path
;
32 import org
.eclipse
.core
.runtime
.Status
;
33 import org
.eclipse
.jdt
.annotation
.NonNull
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfEventProvider
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
59 import com
.google
.common
.collect
.ImmutableList
;
62 * Abstract implementation of ITmfTrace.
64 * Since the concept of 'location' is trace specific, the concrete classes have
65 * to provide the related methods, namely:
67 * <li> public ITmfLocation<?> getCurrentLocation()
68 * <li> public double getLocationRatio(ITmfLocation<?> location)
69 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
70 * <li> public ITmfContext seekEvent(double ratio)
71 * <li> public IStatus validate(IProject project, String path)
73 * A concrete trace must provide its corresponding parser. A common way to
74 * accomplish this is by making the concrete class extend TmfTrace and
75 * implement ITmfEventParser.
77 * When constructing an event, the concrete trace should use the trace's
78 * timestamp transform to create the timestamp, by either transforming the
79 * parsed time value directly or by using the method createTimestamp().
81 * The concrete class can either specify its own indexer or use the provided
82 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
83 * used as checkpoint interval.
86 * @author Francois Chouinard
89 * @see ITmfTraceIndexer
90 * @see ITmfEventParser
92 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
, ITmfTraceCompleteness
{
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * Basic aspects that should be valid for all trace types.
101 public static final Collection
<ITmfEventAspect
> BASE_ASPECTS
=
103 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
104 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
105 ITmfEventAspect
.BaseAspects
.CONTENTS
108 // ------------------------------------------------------------------------
109 // Instance attributes
110 // ------------------------------------------------------------------------
112 // The resource used for persistent properties for this trace
113 private IResource fResource
;
116 private String fPath
;
118 // The trace cache page size
119 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
121 // The number of events collected (so far)
122 private volatile long fNbEvents
= 0;
124 // The time span of the event stream
125 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
126 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
128 // The trace streaming interval (0 = no streaming)
129 private long fStreamingInterval
= 0;
132 private ITmfTraceIndexer fIndexer
;
135 private ITmfEventParser fParser
;
137 private ITmfTimestampTransform fTsTransform
;
139 private final Map
<String
, IAnalysisModule
> fAnalysisModules
=
140 Collections
.synchronizedMap(new LinkedHashMap
<String
, IAnalysisModule
>());
142 // ------------------------------------------------------------------------
144 // ------------------------------------------------------------------------
147 * The default, parameterless, constructor
151 fIndexer
= createIndexer(DEFAULT_BLOCK_SIZE
);
158 * The resource associated to the trace
160 * The type of events that will be read from this trace
162 * The path to the trace on the filesystem
164 * The trace cache size. Pass '-1' to use the default specified
165 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
167 * The trace streaming interval. You can use '0' for post-mortem
170 * The trace event parser. Use 'null' if (and only if) the trace
171 * object itself is also the ITmfEventParser to be used.
172 * @throws TmfTraceException
173 * If something failed during the opening
175 protected TmfTrace(final IResource resource
,
176 final Class
<?
extends ITmfEvent
> type
,
180 final ITmfEventParser parser
)
181 throws TmfTraceException
{
183 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
184 fStreamingInterval
= interval
;
186 initialize(resource
, path
, type
);
192 * @param trace the original trace
193 * @throws TmfTraceException Should not happen usually
195 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
198 throw new IllegalArgumentException();
200 fCacheSize
= trace
.getCacheSize();
201 fStreamingInterval
= trace
.getStreamingInterval();
202 fParser
= trace
.fParser
;
203 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
207 * Creates the indexer instance. Classes extending this class can override
208 * this to provide a different indexer implementation.
210 * @param interval the checkpoints interval
212 * @return the indexer
215 protected ITmfTraceIndexer
createIndexer(int interval
) {
216 return new TmfCheckpointIndexer(this, interval
);
219 // ------------------------------------------------------------------------
220 // ITmfTrace - Initializers
221 // ------------------------------------------------------------------------
224 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
) throws TmfTraceException
{
226 throw new IllegalArgumentException();
229 initTrace(resource
, path
, type
);
233 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
234 initialize(resource
, path
, type
);
238 * Initialize the trace common attributes and the base component.
240 * @param resource the Eclipse resource (trace)
241 * @param path the trace path
242 * @param type the trace event type
244 * @throws TmfTraceException If something failed during the initialization
246 protected void initialize(final IResource resource
,
248 final Class
<?
extends ITmfEvent
> type
)
249 throws TmfTraceException
{
251 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
254 fResource
= resource
;
255 String traceName
= getName();
256 if (traceName
.isEmpty()) {
257 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
259 if (fParser
== null) {
260 if (this instanceof ITmfEventParser
) {
261 fParser
= (ITmfEventParser
) this;
263 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
266 super.init(traceName
, type
);
267 // register as VIP after super.init() because TmfComponent registers to signal manager there
268 TmfSignalManager
.registerVIP(this);
269 if (fIndexer
!= null) {
272 fIndexer
= createIndexer(fCacheSize
);
276 * Indicates if the path points to an existing file/directory
278 * @param path the path to test
279 * @return true if the file/directory exists
281 protected boolean fileExists(final String path
) {
282 final File file
= new File(path
);
283 return file
.exists();
290 public void indexTrace(boolean waitForCompletion
) {
291 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
295 * Instantiate the applicable analysis modules and executes the analysis
296 * modules that are meant to be automatically executed
298 * @return An IStatus indicating whether the analysis could be run
299 * successfully or not
302 protected IStatus
executeAnalysis() {
303 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
305 @SuppressWarnings("null")
306 @NonNull Class
<?
extends TmfTrace
> className
= this.getClass();
307 Map
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules(className
);
308 for (IAnalysisModuleHelper helper
: modules
.values()) {
310 IAnalysisModule module
= helper
.newModule(this);
311 fAnalysisModules
.put(module
.getId(), module
);
312 if (module
.isAutomatic()) {
313 status
.add(module
.schedule());
315 } catch (TmfAnalysisException e
) {
316 status
.add(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, e
.getMessage()));
326 public IAnalysisModule
getAnalysisModule(String analysisId
) {
327 return fAnalysisModules
.get(analysisId
);
335 public Iterable
<IAnalysisModule
> getAnalysisModules() {
336 synchronized (fAnalysisModules
) {
337 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
346 public <T
extends IAnalysisModule
> T
getAnalysisModuleOfClass(Class
<T
> moduleClass
, String id
) {
347 Iterable
<T
> modules
= getAnalysisModulesOfClass(moduleClass
);
348 for (T module
: modules
) {
349 if (id
.equals(module
.getId())) {
360 public <T
> Iterable
<T
> getAnalysisModulesOfClass(Class
<T
> moduleClass
) {
361 Set
<T
> modules
= new HashSet
<>();
362 synchronized (fAnalysisModules
) {
363 for (Entry
<String
, IAnalysisModule
> entry
: fAnalysisModules
.entrySet()) {
364 if (moduleClass
.isAssignableFrom(entry
.getValue().getClass())) {
365 modules
.add(moduleClass
.cast(entry
.getValue()));
373 public Iterable
<ITmfEventAspect
> getEventAspects() {
374 /* By default we provide only the base aspects valid for all trace types */
382 public synchronized void dispose() {
383 /* Clean up the index if applicable */
384 if (getIndexer() != null) {
385 getIndexer().dispose();
388 /* Clean up the analysis modules */
389 synchronized (fAnalysisModules
) {
390 for (IAnalysisModule module
: fAnalysisModules
.values()) {
393 fAnalysisModules
.clear();
399 // ------------------------------------------------------------------------
400 // ITmfTrace - Basic getters
401 // ------------------------------------------------------------------------
404 public Class
<?
extends ITmfEvent
> getEventType() {
405 return super.getType();
409 public IResource
getResource() {
414 public String
getPath() {
419 public int getCacheSize() {
424 public long getStreamingInterval() {
425 return fStreamingInterval
;
429 * @return the trace indexer
432 protected ITmfTraceIndexer
getIndexer() {
437 * @return the trace parser
439 protected ITmfEventParser
getParser() {
443 // ------------------------------------------------------------------------
444 // ITmfTrace - Trace characteristics getters
445 // ------------------------------------------------------------------------
448 public long getNbEvents() {
456 public TmfTimeRange
getTimeRange() {
457 return new TmfTimeRange(fStartTime
, fEndTime
);
464 public ITmfTimestamp
getStartTime() {
472 public ITmfTimestamp
getEndTime() {
480 public ITmfTimestamp
getInitialRangeOffset() {
481 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
482 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
489 public String
getHostId() {
490 return this.getName();
493 // ------------------------------------------------------------------------
494 // Convenience setters
495 // ------------------------------------------------------------------------
498 * Set the trace cache size. Must be done at initialization time.
500 * @param cacheSize The trace cache size
502 protected void setCacheSize(final int cacheSize
) {
503 fCacheSize
= cacheSize
;
507 * Set the trace known number of events. This can be quite dynamic
508 * during indexing or for live traces.
510 * @param nbEvents The number of events
512 protected synchronized void setNbEvents(final long nbEvents
) {
513 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
517 * Update the trace events time range
519 * @param range the new time range
522 protected void setTimeRange(final TmfTimeRange range
) {
523 fStartTime
= range
.getStartTime();
524 fEndTime
= range
.getEndTime();
528 * Update the trace chronologically first event timestamp
530 * @param startTime the new first event timestamp
533 protected void setStartTime(final ITmfTimestamp startTime
) {
534 fStartTime
= startTime
;
538 * Update the trace chronologically last event timestamp
540 * @param endTime the new last event timestamp
543 protected void setEndTime(final ITmfTimestamp endTime
) {
548 * Set the polling interval for live traces (default = 0 = no streaming).
550 * @param interval the new trace streaming interval
552 protected void setStreamingInterval(final long interval
) {
553 fStreamingInterval
= (interval
> 0) ? interval
: 0;
557 * Set the trace parser. Must be done at initialization time.
559 * @param parser the new trace parser
561 protected void setParser(final ITmfEventParser parser
) {
565 // ------------------------------------------------------------------------
566 // ITmfTrace - SeekEvent operations (returning a trace context)
567 // ------------------------------------------------------------------------
570 public synchronized ITmfContext
seekEvent(final long rank
) {
572 // A rank <= 0 indicates to seek the first event
574 ITmfContext context
= seekEvent((ITmfLocation
) null);
579 // Position the trace at the checkpoint
580 final ITmfContext context
= fIndexer
.seekIndex(rank
);
582 // And locate the requested event context
583 long pos
= context
.getRank();
585 ITmfEvent event
= getNext(context
);
586 while ((event
!= null) && (++pos
< rank
)) {
587 event
= getNext(context
);
597 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
599 // A null timestamp indicates to seek the first event
600 if (timestamp
== null) {
601 ITmfContext context
= seekEvent((ITmfLocation
) null);
606 // Position the trace at the checkpoint
607 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
609 // And locate the requested event context
610 ITmfLocation previousLocation
= context
.getLocation();
611 long previousRank
= context
.getRank();
612 ITmfEvent event
= getNext(context
);
613 while (event
!= null && event
.getTimestamp().compareTo(timestamp
) < 0) {
614 previousLocation
= context
.getLocation();
615 previousRank
= context
.getRank();
616 event
= getNext(context
);
619 context
.setLocation(null);
620 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
623 context
= seekEvent(previousLocation
);
624 context
.setRank(previousRank
);
629 // ------------------------------------------------------------------------
630 // ITmfTrace - Read operations (returning an actual event)
631 // ------------------------------------------------------------------------
634 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
635 // parseEvent() does not update the context
636 final ITmfEvent event
= fParser
.parseEvent(context
);
638 updateAttributes(context
, event
.getTimestamp());
639 context
.setLocation(getCurrentLocation());
640 context
.increaseRank();
647 * Hook for special event processing by the concrete class
648 * (called by TmfTrace.getEvent())
650 * @param event the event
652 protected void processEvent(final ITmfEvent event
) {
657 * Update the trace attributes
659 * @param context the current trace context
660 * @param timestamp the corresponding timestamp
663 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
664 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
665 fStartTime
= timestamp
;
667 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
) < 0)) {
668 fEndTime
= timestamp
;
670 if (context
.hasValidRank()) {
671 long rank
= context
.getRank();
672 if (fNbEvents
<= rank
) {
673 fNbEvents
= rank
+ 1;
675 if (fIndexer
!= null) {
676 fIndexer
.updateIndex(context
, timestamp
);
681 // ------------------------------------------------------------------------
683 // ------------------------------------------------------------------------
689 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
690 if (executorIsShutdown()) {
693 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
694 && (request
.getIndex() == 0)) {
695 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
696 request
.setStartIndex((int) context
.getRank());
700 return seekEvent(request
.getIndex());
703 // ------------------------------------------------------------------------
705 // ------------------------------------------------------------------------
708 * Handler for the Trace Opened signal
711 * The incoming signal
715 public void traceOpened(TmfTraceOpenedSignal signal
) {
716 boolean signalIsForUs
= false;
717 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
719 signalIsForUs
= true;
724 if (!signalIsForUs
) {
729 * The signal is either for this trace, or for an experiment containing
732 IStatus status
= executeAnalysis();
733 if (!status
.isOK()) {
734 Activator
.log(status
);
737 TmfTraceManager
.refreshSupplementaryFiles(this);
739 if (signal
.getTrace() == this) {
740 /* Additionally, the signal is directly for this trace. */
741 if (getNbEvents() == 0) {
745 /* For a streaming trace, the range updated signal should be sent
746 * by the subclass when a new safe time is determined.
748 if (getStreamingInterval() > 0) {
753 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
754 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
756 // Broadcast in separate thread to prevent deadlock
757 broadcastAsync(rangeUpdatedsignal
);
764 * Signal handler for the TmfTraceRangeUpdatedSignal signal
766 * @param signal The incoming signal
770 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
771 if (signal
.getTrace() == this) {
772 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
777 * Signal handler for the TmfTraceUpdatedSignal signal
779 * @param signal The incoming signal
783 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
784 if (signal
.getSource() == getIndexer()) {
785 fNbEvents
= signal
.getNbEvents();
786 fStartTime
= signal
.getRange().getStartTime();
787 fEndTime
= signal
.getRange().getEndTime();
791 // ------------------------------------------------------------------------
792 // Timestamp transformation functions
793 // ------------------------------------------------------------------------
799 public ITmfTimestampTransform
getTimestampTransform() {
800 if (fTsTransform
== null) {
801 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
810 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
812 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
819 public ITmfTimestamp
createTimestamp(long ts
) {
820 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
823 // ------------------------------------------------------------------------
825 // ------------------------------------------------------------------------
828 @SuppressWarnings("nls")
829 public synchronized String
toString() {
830 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
831 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
832 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
839 public boolean isComplete() {
841 * Be default, all traces are "complete" which means no more data will
851 public void setComplete(boolean isComplete
) {
853 * This should be overridden by trace classes that can support live
854 * reading (traces in an incomplete state)