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
.aspect
.ITmfEventAspect
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
61 import com
.google
.common
.collect
.ImmutableList
;
62 import com
.google
.common
.collect
.Multimap
;
65 * Abstract implementation of ITmfTrace.
67 * Since the concept of 'location' is trace specific, the concrete classes have
68 * to provide the related methods, namely:
70 * <li> public ITmfLocation<?> getCurrentLocation()
71 * <li> public double getLocationRatio(ITmfLocation<?> location)
72 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
73 * <li> public ITmfContext seekEvent(double ratio)
74 * <li> public IStatus validate(IProject project, String path)
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
, ITmfEventParser
, ITmfTraceCompleteness
{
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * Basic aspects that should be valid for all trace types.
101 public static final @NonNull Collection
<ITmfEventAspect
> BASE_ASPECTS
=
102 checkNotNull(ImmutableList
.of(
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 @Nullable String fTraceTypeId
;
119 private String fPath
;
121 // The trace cache page size
122 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
124 // The number of events collected (so far)
125 private volatile long fNbEvents
= 0;
127 // The time span of the event stream
128 private @NonNull ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
129 private @NonNull ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
131 // The trace streaming interval (0 = no streaming)
132 private long fStreamingInterval
= 0;
135 private ITmfTraceIndexer fIndexer
;
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
= new TmfCheckpointIndexer(this);
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
169 * @throws TmfTraceException
170 * If something failed during the opening
172 protected TmfTrace(final IResource resource
,
173 final Class
<?
extends ITmfEvent
> type
,
177 throws TmfTraceException
{
179 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
180 fStreamingInterval
= interval
;
181 initialize(resource
, path
, type
);
187 * @param trace the original trace
188 * @throws TmfTraceException Should not happen usually
190 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
193 throw new IllegalArgumentException();
195 fCacheSize
= trace
.getCacheSize();
196 fStreamingInterval
= trace
.getStreamingInterval();
197 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
201 * Creates the indexer instance. Classes extending this class can override
202 * this to provide a different indexer implementation.
204 * @param interval the checkpoints interval
206 * @return the indexer
208 protected ITmfTraceIndexer
createIndexer(int interval
) {
209 return new TmfCheckpointIndexer(this, interval
);
212 // ------------------------------------------------------------------------
213 // ITmfTrace - Initializers
214 // ------------------------------------------------------------------------
217 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
, String traceTypeId
) throws TmfTraceException
{
219 throw new IllegalArgumentException();
222 fTraceTypeId
= traceTypeId
;
223 initTrace(resource
, path
, type
);
227 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
228 initialize(resource
, path
, type
);
232 * Initialize the trace common attributes and the base component.
234 * @param resource the Eclipse resource (trace)
235 * @param path the trace path
236 * @param type the trace event type
238 * @throws TmfTraceException If something failed during the initialization
240 protected void initialize(final IResource resource
,
242 final Class
<?
extends ITmfEvent
> type
)
243 throws TmfTraceException
{
245 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
248 fResource
= resource
;
249 String traceName
= getName();
250 if (traceName
.isEmpty()) {
251 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
253 super.init(traceName
, type
);
254 // register as VIP after super.init() because TmfComponent registers to signal manager there
255 TmfSignalManager
.registerVIP(this);
256 if (fIndexer
!= null) {
259 fIndexer
= createIndexer(fCacheSize
);
263 * Indicates if the path points to an existing file/directory
265 * @param path the path to test
266 * @return true if the file/directory exists
268 protected boolean fileExists(final String path
) {
269 final File file
= new File(path
);
270 return file
.exists();
274 public void indexTrace(boolean waitForCompletion
) {
275 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
279 * Instantiate the applicable analysis modules and executes the analysis
280 * modules that are meant to be automatically executed
282 * @return An IStatus indicating whether the analysis could be run
283 * successfully or not
285 protected IStatus
executeAnalysis() {
286 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
288 Multimap
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules();
289 for (IAnalysisModuleHelper helper
: modules
.values()) {
291 IAnalysisModule module
= helper
.newModule(this);
292 if (module
== null) {
295 fAnalysisModules
.put(module
.getId(), module
);
296 if (module
.isAutomatic()) {
297 status
.add(module
.schedule());
299 } catch (TmfAnalysisException e
) {
300 status
.add(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, e
.getMessage()));
307 public IAnalysisModule
getAnalysisModule(String analysisId
) {
308 return fAnalysisModules
.get(analysisId
);
313 public Iterable
<IAnalysisModule
> getAnalysisModules() {
314 synchronized (fAnalysisModules
) {
315 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
321 public Iterable
<ITmfEventAspect
> getEventAspects() {
322 /* By default we provide only the base aspects valid for all trace types */
330 public synchronized void dispose() {
331 /* Clean up the index if applicable */
332 if (getIndexer() != null) {
333 getIndexer().dispose();
336 /* Clean up the analysis modules */
337 synchronized (fAnalysisModules
) {
338 for (IAnalysisModule module
: fAnalysisModules
.values()) {
341 fAnalysisModules
.clear();
347 // ------------------------------------------------------------------------
348 // ITmfTrace - Basic getters
349 // ------------------------------------------------------------------------
352 public Class
<?
extends ITmfEvent
> getEventType() {
353 return super.getType();
357 public IResource
getResource() {
362 public @Nullable String
getTraceTypeId() {
367 public String
getPath() {
372 public int getCacheSize() {
377 public long getStreamingInterval() {
378 return fStreamingInterval
;
382 * @return the trace indexer
384 protected ITmfTraceIndexer
getIndexer() {
388 // ------------------------------------------------------------------------
389 // ITmfTrace - Trace characteristics getters
390 // ------------------------------------------------------------------------
393 public long getNbEvents() {
398 public @NonNull TmfTimeRange
getTimeRange() {
399 return new TmfTimeRange(fStartTime
, fEndTime
);
403 public ITmfTimestamp
getStartTime() {
408 public ITmfTimestamp
getEndTime() {
413 public ITmfTimestamp
getInitialRangeOffset() {
414 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
415 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
419 public String
getHostId() {
420 return this.getName();
423 // ------------------------------------------------------------------------
424 // Convenience setters
425 // ------------------------------------------------------------------------
428 * Set the trace cache size. Must be done at initialization time.
430 * @param cacheSize The trace cache size
432 protected void setCacheSize(final int cacheSize
) {
433 fCacheSize
= cacheSize
;
437 * Set the trace known number of events. This can be quite dynamic
438 * during indexing or for live traces.
440 * @param nbEvents The number of events
442 protected synchronized void setNbEvents(final long nbEvents
) {
443 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
447 * Update the trace events time range
449 * @param range the new time range
451 protected void setTimeRange(final @NonNull TmfTimeRange range
) {
452 fStartTime
= range
.getStartTime();
453 fEndTime
= range
.getEndTime();
457 * Update the trace chronologically first event timestamp
459 * @param startTime the new first event timestamp
461 protected void setStartTime(final @NonNull ITmfTimestamp startTime
) {
462 fStartTime
= startTime
;
466 * Update the trace chronologically last event timestamp
468 * @param endTime the new last event timestamp
470 protected void setEndTime(final @NonNull ITmfTimestamp endTime
) {
475 * Set the polling interval for live traces (default = 0 = no streaming).
477 * @param interval the new trace streaming interval
479 protected void setStreamingInterval(final long interval
) {
480 fStreamingInterval
= (interval
> 0) ? interval
: 0;
483 // ------------------------------------------------------------------------
484 // ITmfTrace - SeekEvent operations (returning a trace context)
485 // ------------------------------------------------------------------------
488 public synchronized ITmfContext
seekEvent(final long rank
) {
490 // A rank <= 0 indicates to seek the first event
492 ITmfContext context
= seekEvent((ITmfLocation
) null);
497 // Position the trace at the checkpoint
498 final ITmfContext context
= fIndexer
.seekIndex(rank
);
500 // And locate the requested event context
501 long pos
= context
.getRank();
503 ITmfEvent event
= getNext(context
);
504 while ((event
!= null) && (++pos
< rank
)) {
505 event
= getNext(context
);
512 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
514 // A null timestamp indicates to seek the first event
515 if (timestamp
== null) {
516 ITmfContext context
= seekEvent((ITmfLocation
) null);
521 // Position the trace at the checkpoint
522 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
524 // And locate the requested event context
525 ITmfLocation previousLocation
= context
.getLocation();
526 long previousRank
= context
.getRank();
527 ITmfEvent event
= getNext(context
);
528 while (event
!= null && event
.getTimestamp().compareTo(timestamp
) < 0) {
529 previousLocation
= context
.getLocation();
530 previousRank
= context
.getRank();
531 event
= getNext(context
);
534 context
.setLocation(null);
535 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
538 context
= seekEvent(previousLocation
);
539 context
.setRank(previousRank
);
544 // ------------------------------------------------------------------------
545 // Read operations (returning an actual event)
546 // ------------------------------------------------------------------------
549 public abstract ITmfEvent
parseEvent(ITmfContext context
);
552 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
553 // parseEvent() does not update the context
554 final ITmfEvent event
= parseEvent(context
);
556 ITmfTimestamp timestamp
= event
.getTimestamp();
557 updateAttributes(context
, timestamp
);
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
570 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfTimestamp timestamp
) {
571 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
572 fStartTime
= timestamp
;
574 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
) < 0)) {
575 fEndTime
= timestamp
;
577 if (context
.hasValidRank()) {
578 long rank
= context
.getRank();
579 if (fNbEvents
<= rank
) {
580 fNbEvents
= rank
+ 1;
582 if (fIndexer
!= null) {
583 fIndexer
.updateIndex(context
, timestamp
);
588 // ------------------------------------------------------------------------
590 // ------------------------------------------------------------------------
593 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
594 if (executorIsShutdown()) {
597 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
598 && (request
.getIndex() == 0)) {
599 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
600 request
.setStartIndex((int) context
.getRank());
604 return seekEvent(request
.getIndex());
607 // ------------------------------------------------------------------------
609 // ------------------------------------------------------------------------
612 * Handler for the Trace Opened signal
615 * The incoming signal
618 public void traceOpened(TmfTraceOpenedSignal signal
) {
619 boolean signalIsForUs
= false;
620 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
622 signalIsForUs
= true;
627 if (!signalIsForUs
) {
632 * The signal is either for this trace, or for an experiment containing
635 IStatus status
= executeAnalysis();
636 if (!status
.isOK()) {
637 Activator
.log(status
);
640 TmfTraceManager
.refreshSupplementaryFiles(this);
642 if (signal
.getTrace() == this) {
643 /* Additionally, the signal is directly for this trace. */
644 if (getNbEvents() == 0) {
648 /* For a streaming trace, the range updated signal should be sent
649 * by the subclass when a new safe time is determined.
651 if (getStreamingInterval() > 0) {
656 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
657 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
659 // Broadcast in separate thread to prevent deadlock
660 broadcastAsync(rangeUpdatedsignal
);
667 * Signal handler for the TmfTraceRangeUpdatedSignal signal
669 * @param signal The incoming signal
672 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
673 if (signal
.getTrace() == this) {
674 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
679 * Signal handler for the TmfTraceUpdatedSignal signal
681 * @param signal The incoming signal
684 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
685 if (signal
.getSource() == getIndexer()) {
686 fNbEvents
= signal
.getNbEvents();
687 fStartTime
= signal
.getRange().getStartTime();
688 fEndTime
= signal
.getRange().getEndTime();
692 // ------------------------------------------------------------------------
693 // Timestamp transformation functions
694 // ------------------------------------------------------------------------
697 public ITmfTimestampTransform
getTimestampTransform() {
698 if (fTsTransform
== null) {
699 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
705 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
707 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
711 public @NonNull ITmfTimestamp
createTimestamp(long ts
) {
712 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
715 // ------------------------------------------------------------------------
717 // ------------------------------------------------------------------------
720 @SuppressWarnings("nls")
721 public synchronized String
toString() {
722 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
723 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
724 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
728 public boolean isComplete() {
730 * Be default, all traces are "complete" which means no more data will
737 public void setComplete(boolean isComplete
) {
739 * This should be overridden by trace classes that can support live
740 * reading (traces in an incomplete state)