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
;
64 import com
.google
.common
.collect
.Multimap
;
67 * Abstract implementation of ITmfTrace.
69 * Since the concept of 'location' is trace specific, the concrete classes have
70 * to provide the related methods, namely:
72 * <li> public ITmfLocation<?> getCurrentLocation()
73 * <li> public double getLocationRatio(ITmfLocation<?> location)
74 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
75 * <li> public ITmfContext seekEvent(double ratio)
76 * <li> public IStatus validate(IProject project, String path)
79 * When constructing an event, the concrete trace should use the trace's
80 * timestamp transform to create the timestamp, by either transforming the
81 * parsed time value directly or by using the method createTimestamp().
83 * The concrete class can either specify its own indexer or use the provided
84 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
85 * used as checkpoint interval.
88 * @author Francois Chouinard
91 * @see ITmfTraceIndexer
92 * @see ITmfEventParser
94 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
, ITmfEventParser
, ITmfTraceCompleteness
{
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 * Basic aspects that should be valid for all trace types.
103 public static final @NonNull Collection
<ITmfEventAspect
> BASE_ASPECTS
=
104 checkNotNull(ImmutableList
.of(
105 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
106 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
107 ITmfEventAspect
.BaseAspects
.CONTENTS
110 // ------------------------------------------------------------------------
111 // Instance attributes
112 // ------------------------------------------------------------------------
114 // The resource used for persistent properties for this trace
115 private IResource fResource
;
118 private @Nullable String fTraceTypeId
;
121 private String fPath
;
123 // The trace cache page size
124 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
126 // The number of events collected (so far)
127 private volatile long fNbEvents
= 0;
129 // The time span of the event stream
130 private @NonNull ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
131 private @NonNull ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
133 // The trace streaming interval (0 = no streaming)
134 private long fStreamingInterval
= 0;
137 private ITmfTraceIndexer fIndexer
;
139 private ITmfTimestampTransform fTsTransform
;
141 private final Map
<String
, IAnalysisModule
> fAnalysisModules
=
142 Collections
.synchronizedMap(new LinkedHashMap
<String
, IAnalysisModule
>());
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 * The default, parameterless, constructor
153 fIndexer
= new TmfCheckpointIndexer(this);
160 * The resource associated to the trace
162 * The type of events that will be read from this trace
164 * The path to the trace on the filesystem
166 * The trace cache size. Pass '-1' to use the default specified
167 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
169 * The trace streaming interval. You can use '0' for post-mortem
171 * @throws TmfTraceException
172 * If something failed during the opening
174 protected TmfTrace(final IResource resource
,
175 final Class
<?
extends ITmfEvent
> type
,
179 throws TmfTraceException
{
181 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
182 fStreamingInterval
= interval
;
183 initialize(resource
, path
, type
);
189 * @param trace the original trace
190 * @throws TmfTraceException Should not happen usually
192 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
195 throw new IllegalArgumentException();
197 fCacheSize
= trace
.getCacheSize();
198 fStreamingInterval
= trace
.getStreamingInterval();
199 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
203 * Creates the indexer instance. Classes extending this class can override
204 * this to provide a different indexer implementation.
206 * @param interval the checkpoints interval
208 * @return the indexer
210 protected ITmfTraceIndexer
createIndexer(int interval
) {
211 return new TmfCheckpointIndexer(this, interval
);
214 // ------------------------------------------------------------------------
215 // ITmfTrace - Initializers
216 // ------------------------------------------------------------------------
219 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
, String traceTypeId
) throws TmfTraceException
{
221 throw new IllegalArgumentException();
224 fTraceTypeId
= traceTypeId
;
225 initTrace(resource
, path
, type
);
229 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
230 initialize(resource
, path
, type
);
234 * Initialize the trace common attributes and the base component.
236 * @param resource the Eclipse resource (trace)
237 * @param path the trace path
238 * @param type the trace event type
240 * @throws TmfTraceException If something failed during the initialization
242 protected void initialize(final IResource resource
,
244 final Class
<?
extends ITmfEvent
> type
)
245 throws TmfTraceException
{
247 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
250 fResource
= resource
;
251 String traceName
= getName();
252 if (traceName
.isEmpty()) {
253 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
255 super.init(traceName
, type
);
256 // register as VIP after super.init() because TmfComponent registers to signal manager there
257 TmfSignalManager
.registerVIP(this);
258 if (fIndexer
!= null) {
261 fIndexer
= createIndexer(fCacheSize
);
265 * Indicates if the path points to an existing file/directory
267 * @param path the path to test
268 * @return true if the file/directory exists
270 protected boolean fileExists(final String path
) {
271 final File file
= new File(path
);
272 return file
.exists();
276 public void indexTrace(boolean waitForCompletion
) {
277 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
281 * Instantiate the applicable analysis modules and executes the analysis
282 * modules that are meant to be automatically executed
284 * @return An IStatus indicating whether the analysis could be run
285 * successfully or not
287 protected IStatus
executeAnalysis() {
288 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
290 Multimap
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules();
291 for (IAnalysisModuleHelper helper
: modules
.values()) {
293 IAnalysisModule module
= helper
.newModule(this);
294 if (module
== null) {
297 fAnalysisModules
.put(module
.getId(), module
);
298 if (module
.isAutomatic()) {
299 status
.add(module
.schedule());
301 } catch (TmfAnalysisException e
) {
302 status
.add(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, e
.getMessage()));
309 public IAnalysisModule
getAnalysisModule(String analysisId
) {
310 return fAnalysisModules
.get(analysisId
);
315 public Iterable
<IAnalysisModule
> getAnalysisModules() {
316 synchronized (fAnalysisModules
) {
317 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
323 public Iterable
<ITmfEventAspect
> getEventAspects() {
324 /* By default we provide only the base aspects valid for all trace types */
332 public synchronized void dispose() {
333 /* Clean up the index if applicable */
334 if (getIndexer() != null) {
335 getIndexer().dispose();
338 /* Clean up the analysis modules */
339 synchronized (fAnalysisModules
) {
340 for (IAnalysisModule module
: fAnalysisModules
.values()) {
343 fAnalysisModules
.clear();
349 // ------------------------------------------------------------------------
350 // ITmfTrace - Basic getters
351 // ------------------------------------------------------------------------
354 public IResource
getResource() {
359 public @Nullable String
getTraceTypeId() {
364 public String
getPath() {
369 public int getCacheSize() {
374 public long getStreamingInterval() {
375 return fStreamingInterval
;
379 * @return the trace indexer
381 protected ITmfTraceIndexer
getIndexer() {
385 // ------------------------------------------------------------------------
386 // ITmfTrace - Trace characteristics getters
387 // ------------------------------------------------------------------------
390 public long getNbEvents() {
395 public @NonNull TmfTimeRange
getTimeRange() {
396 return new TmfTimeRange(fStartTime
, fEndTime
);
400 public ITmfTimestamp
getStartTime() {
405 public ITmfTimestamp
getEndTime() {
410 public ITmfTimestamp
getInitialRangeOffset() {
411 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
412 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
416 public String
getHostId() {
417 return this.getName();
420 // ------------------------------------------------------------------------
421 // Convenience setters
422 // ------------------------------------------------------------------------
425 * Set the trace cache size. Must be done at initialization time.
427 * @param cacheSize The trace cache size
429 protected void setCacheSize(final int cacheSize
) {
430 fCacheSize
= cacheSize
;
434 * Set the trace known number of events. This can be quite dynamic
435 * during indexing or for live traces.
437 * @param nbEvents The number of events
439 protected synchronized void setNbEvents(final long nbEvents
) {
440 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
444 * Update the trace events time range
446 * @param range the new time range
448 protected void setTimeRange(final @NonNull TmfTimeRange range
) {
449 fStartTime
= range
.getStartTime();
450 fEndTime
= range
.getEndTime();
454 * Update the trace chronologically first event timestamp
456 * @param startTime the new first event timestamp
458 protected void setStartTime(final @NonNull ITmfTimestamp startTime
) {
459 fStartTime
= startTime
;
463 * Update the trace chronologically last event timestamp
465 * @param endTime the new last event timestamp
467 protected void setEndTime(final @NonNull ITmfTimestamp endTime
) {
472 * Set the polling interval for live traces (default = 0 = no streaming).
474 * @param interval the new trace streaming interval
476 protected void setStreamingInterval(final long interval
) {
477 fStreamingInterval
= (interval
> 0) ? interval
: 0;
480 // ------------------------------------------------------------------------
481 // ITmfTrace - SeekEvent operations (returning a trace context)
482 // ------------------------------------------------------------------------
485 public synchronized ITmfContext
seekEvent(final long rank
) {
487 // A rank <= 0 indicates to seek the first event
489 ITmfContext context
= seekEvent((ITmfLocation
) null);
494 // Position the trace at the checkpoint
495 final ITmfContext context
= fIndexer
.seekIndex(rank
);
497 // And locate the requested event context
498 long pos
= context
.getRank();
500 ITmfEvent event
= getNext(context
);
501 while ((event
!= null) && (++pos
< rank
)) {
502 event
= getNext(context
);
509 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
511 // A null timestamp indicates to seek the first event
512 if (timestamp
== null) {
513 ITmfContext context
= seekEvent((ITmfLocation
) null);
518 // Position the trace at the checkpoint
519 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
521 // And locate the requested event context
522 ITmfLocation previousLocation
= context
.getLocation();
523 long previousRank
= context
.getRank();
524 ITmfEvent event
= getNext(context
);
525 while (event
!= null && event
.getTimestamp().compareTo(timestamp
) < 0) {
526 previousLocation
= context
.getLocation();
527 previousRank
= context
.getRank();
528 event
= getNext(context
);
531 context
.setLocation(null);
532 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
535 context
= seekEvent(previousLocation
);
536 context
.setRank(previousRank
);
541 // ------------------------------------------------------------------------
542 // Read operations (returning an actual event)
543 // ------------------------------------------------------------------------
546 public abstract ITmfEvent
parseEvent(ITmfContext context
);
549 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
550 // parseEvent() does not update the context
551 final ITmfEvent event
= parseEvent(context
);
553 updateAttributes(context
, event
);
554 context
.setLocation(getCurrentLocation());
555 context
.increaseRank();
561 * Update the trace attributes
563 * @param context the current trace context
564 * @param timestamp the corresponding timestamp
565 * @deprecated Use {@link #updateAttributes(ITmfContext, ITmfEvent)}
568 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfTimestamp timestamp
) {
569 updateAttributes(context
, new TmfEvent(this, context
.getRank(), timestamp
, null, null));
573 * Update the trace attributes
575 * @param context the current trace context
576 * @param event the corresponding event
579 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfEvent event
) {
580 ITmfTimestamp timestamp
= event
.getTimestamp();
581 ITmfTimestamp endTime
= timestamp
;
582 if (event
instanceof ITmfLostEvent
) {
583 endTime
= ((ITmfLostEvent
) event
).getTimeRange().getEndTime();
585 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
586 fStartTime
= timestamp
;
588 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(endTime
) < 0)) {
591 if (context
.hasValidRank()) {
592 long rank
= context
.getRank();
593 if (fNbEvents
<= rank
) {
594 fNbEvents
= rank
+ 1;
596 if (fIndexer
!= null) {
597 fIndexer
.updateIndex(context
, timestamp
);
602 // ------------------------------------------------------------------------
604 // ------------------------------------------------------------------------
607 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
608 if (executorIsShutdown()) {
611 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
612 && (request
.getIndex() == 0)) {
613 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
614 request
.setStartIndex((int) context
.getRank());
618 return seekEvent(request
.getIndex());
621 // ------------------------------------------------------------------------
623 // ------------------------------------------------------------------------
626 * Handler for the Trace Opened signal
629 * The incoming signal
632 public void traceOpened(TmfTraceOpenedSignal signal
) {
633 boolean signalIsForUs
= false;
634 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
636 signalIsForUs
= true;
641 if (!signalIsForUs
) {
646 * The signal is either for this trace, or for an experiment containing
649 IStatus status
= executeAnalysis();
650 if (!status
.isOK()) {
651 Activator
.log(status
);
654 TmfTraceManager
.refreshSupplementaryFiles(this);
656 if (signal
.getTrace() == this) {
657 /* Additionally, the signal is directly for this trace. */
658 if (getNbEvents() == 0) {
662 /* For a streaming trace, the range updated signal should be sent
663 * by the subclass when a new safe time is determined.
665 if (getStreamingInterval() > 0) {
670 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
671 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
673 // Broadcast in separate thread to prevent deadlock
674 broadcastAsync(rangeUpdatedsignal
);
681 * Signal handler for the TmfTraceRangeUpdatedSignal signal
683 * @param signal The incoming signal
686 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
687 if (signal
.getTrace() == this) {
688 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
693 * Signal handler for the TmfTraceUpdatedSignal signal
695 * @param signal The incoming signal
698 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
699 if (signal
.getSource() == getIndexer()) {
700 fNbEvents
= signal
.getNbEvents();
701 fStartTime
= signal
.getRange().getStartTime();
702 fEndTime
= signal
.getRange().getEndTime();
706 // ------------------------------------------------------------------------
707 // Timestamp transformation functions
708 // ------------------------------------------------------------------------
711 public ITmfTimestampTransform
getTimestampTransform() {
712 if (fTsTransform
== null) {
713 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
719 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
721 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
725 public @NonNull ITmfTimestamp
createTimestamp(long ts
) {
726 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
729 // ------------------------------------------------------------------------
731 // ------------------------------------------------------------------------
734 @SuppressWarnings("nls")
735 public synchronized String
toString() {
736 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
737 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
738 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
742 public boolean isComplete() {
744 * Be default, all traces are "complete" which means no more data will
751 public void setComplete(boolean isComplete
) {
753 * This should be overridden by trace classes that can support live
754 * reading (traces in an incomplete state)