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
;
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
= 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
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 fAnalysisModules
.put(module
.getId(), module
);
293 if (module
.isAutomatic()) {
294 status
.add(module
.schedule());
296 } catch (TmfAnalysisException e
) {
297 status
.add(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, e
.getMessage()));
304 public IAnalysisModule
getAnalysisModule(String analysisId
) {
305 return fAnalysisModules
.get(analysisId
);
310 public Iterable
<IAnalysisModule
> getAnalysisModules() {
311 synchronized (fAnalysisModules
) {
312 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
318 public Iterable
<ITmfEventAspect
> getEventAspects() {
319 /* By default we provide only the base aspects valid for all trace types */
327 public synchronized void dispose() {
328 /* Clean up the index if applicable */
329 if (getIndexer() != null) {
330 getIndexer().dispose();
333 /* Clean up the analysis modules */
334 synchronized (fAnalysisModules
) {
335 for (IAnalysisModule module
: fAnalysisModules
.values()) {
338 fAnalysisModules
.clear();
344 // ------------------------------------------------------------------------
345 // ITmfTrace - Basic getters
346 // ------------------------------------------------------------------------
349 public Class
<?
extends ITmfEvent
> getEventType() {
350 return super.getType();
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 ITmfTimestamp timestamp
= event
.getTimestamp();
554 updateAttributes(context
, timestamp
);
555 context
.setLocation(getCurrentLocation());
556 context
.increaseRank();
562 * Update the trace attributes
564 * @param context the current trace context
565 * @param timestamp the corresponding timestamp
567 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfTimestamp timestamp
) {
568 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
569 fStartTime
= timestamp
;
571 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
) < 0)) {
572 fEndTime
= timestamp
;
574 if (context
.hasValidRank()) {
575 long rank
= context
.getRank();
576 if (fNbEvents
<= rank
) {
577 fNbEvents
= rank
+ 1;
579 if (fIndexer
!= null) {
580 fIndexer
.updateIndex(context
, timestamp
);
585 // ------------------------------------------------------------------------
587 // ------------------------------------------------------------------------
590 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
591 if (executorIsShutdown()) {
594 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
595 && (request
.getIndex() == 0)) {
596 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
597 request
.setStartIndex((int) context
.getRank());
601 return seekEvent(request
.getIndex());
604 // ------------------------------------------------------------------------
606 // ------------------------------------------------------------------------
609 * Handler for the Trace Opened signal
612 * The incoming signal
615 public void traceOpened(TmfTraceOpenedSignal signal
) {
616 boolean signalIsForUs
= false;
617 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
619 signalIsForUs
= true;
624 if (!signalIsForUs
) {
629 * The signal is either for this trace, or for an experiment containing
632 IStatus status
= executeAnalysis();
633 if (!status
.isOK()) {
634 Activator
.log(status
);
637 TmfTraceManager
.refreshSupplementaryFiles(this);
639 if (signal
.getTrace() == this) {
640 /* Additionally, the signal is directly for this trace. */
641 if (getNbEvents() == 0) {
645 /* For a streaming trace, the range updated signal should be sent
646 * by the subclass when a new safe time is determined.
648 if (getStreamingInterval() > 0) {
653 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
654 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
656 // Broadcast in separate thread to prevent deadlock
657 broadcastAsync(rangeUpdatedsignal
);
664 * Signal handler for the TmfTraceRangeUpdatedSignal signal
666 * @param signal The incoming signal
669 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
670 if (signal
.getTrace() == this) {
671 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
676 * Signal handler for the TmfTraceUpdatedSignal signal
678 * @param signal The incoming signal
681 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
682 if (signal
.getSource() == getIndexer()) {
683 fNbEvents
= signal
.getNbEvents();
684 fStartTime
= signal
.getRange().getStartTime();
685 fEndTime
= signal
.getRange().getEndTime();
689 // ------------------------------------------------------------------------
690 // Timestamp transformation functions
691 // ------------------------------------------------------------------------
694 public ITmfTimestampTransform
getTimestampTransform() {
695 if (fTsTransform
== null) {
696 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
702 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
704 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
708 public @NonNull ITmfTimestamp
createTimestamp(long ts
) {
709 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
712 // ------------------------------------------------------------------------
714 // ------------------------------------------------------------------------
717 @SuppressWarnings("nls")
718 public synchronized String
toString() {
719 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
720 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
721 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
725 public boolean isComplete() {
727 * Be default, all traces are "complete" which means no more data will
734 public void setComplete(boolean isComplete
) {
736 * This should be overridden by trace classes that can support live
737 * reading (traces in an incomplete state)