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
;
20 import java
.util
.Collection
;
21 import java
.util
.Collections
;
22 import java
.util
.HashSet
;
23 import java
.util
.LinkedHashMap
;
27 import org
.eclipse
.core
.resources
.IResource
;
28 import org
.eclipse
.core
.runtime
.IStatus
;
29 import org
.eclipse
.core
.runtime
.MultiStatus
;
30 import org
.eclipse
.core
.runtime
.Path
;
31 import org
.eclipse
.core
.runtime
.Status
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.jdt
.annotation
.Nullable
;
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
.ITmfLostEvent
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
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
;
64 * Abstract implementation of ITmfTrace.
66 * Since the concept of 'location' is trace specific, the concrete classes have
67 * to provide the related methods, namely:
69 * <li> public ITmfLocation<?> getCurrentLocation()
70 * <li> public double getLocationRatio(ITmfLocation<?> location)
71 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
72 * <li> public ITmfContext seekEvent(double ratio)
73 * <li> public IStatus validate(IProject project, String path)
76 * When constructing an event, the concrete trace should use the trace's
77 * timestamp transform to create the timestamp, by either transforming the
78 * parsed time value directly or by using the method createTimestamp().
80 * The concrete class can either specify its own indexer or use the provided
81 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
82 * used as checkpoint interval.
85 * @author Francois Chouinard
88 * @see ITmfTraceIndexer
89 * @see ITmfEventParser
91 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
, ITmfEventParser
, ITmfTraceCompleteness
{
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * Basic aspects that should be valid for all trace types.
100 public static final @NonNull Collection
<@NonNull ITmfEventAspect
> BASE_ASPECTS
=
102 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
103 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
104 ITmfEventAspect
.BaseAspects
.CONTENTS
107 // ------------------------------------------------------------------------
108 // Instance attributes
109 // ------------------------------------------------------------------------
111 // The resource used for persistent properties for this trace
112 private IResource fResource
;
115 private @Nullable String fTraceTypeId
;
118 private String fPath
;
120 // The trace cache page size
121 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
123 // The number of events collected (so far)
124 private volatile long fNbEvents
= 0;
126 // The time span of the event stream
127 private @NonNull ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
128 private @NonNull ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
130 // The trace streaming interval (0 = no streaming)
131 private long fStreamingInterval
= 0;
134 private ITmfTraceIndexer fIndexer
;
136 private ITmfTimestampTransform fTsTransform
;
138 private final Map
<String
, IAnalysisModule
> fAnalysisModules
=
139 Collections
.synchronizedMap(new LinkedHashMap
<String
, IAnalysisModule
>());
141 // ------------------------------------------------------------------------
143 // ------------------------------------------------------------------------
146 * The default, parameterless, constructor
150 fIndexer
= new TmfCheckpointIndexer(this);
157 * The resource associated to the trace
159 * The type of events that will be read from this trace
161 * The path to the trace on the filesystem
163 * The trace cache size. Pass '-1' to use the default specified
164 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
166 * The trace streaming interval. You can use '0' for post-mortem
168 * @throws TmfTraceException
169 * If something failed during the opening
171 protected TmfTrace(final IResource resource
,
172 final Class
<?
extends ITmfEvent
> type
,
176 throws TmfTraceException
{
178 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
179 fStreamingInterval
= interval
;
180 initialize(resource
, path
, type
);
186 * @param trace the original trace
187 * @throws TmfTraceException Should not happen usually
189 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
192 throw new IllegalArgumentException();
194 fCacheSize
= trace
.getCacheSize();
195 fStreamingInterval
= trace
.getStreamingInterval();
196 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
200 * Creates the indexer instance. Classes extending this class can override
201 * this to provide a different indexer implementation.
203 * @param interval the checkpoints interval
205 * @return the indexer
207 protected ITmfTraceIndexer
createIndexer(int interval
) {
208 return new TmfCheckpointIndexer(this, interval
);
211 // ------------------------------------------------------------------------
212 // ITmfTrace - Initializers
213 // ------------------------------------------------------------------------
216 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
, String traceTypeId
) throws TmfTraceException
{
218 throw new IllegalArgumentException();
221 fTraceTypeId
= traceTypeId
;
222 initTrace(resource
, path
, type
);
226 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
227 initialize(resource
, path
, type
);
231 * Initialize the trace common attributes and the base component.
233 * @param resource the Eclipse resource (trace)
234 * @param path the trace path
235 * @param type the trace event type
237 * @throws TmfTraceException If something failed during the initialization
239 protected void initialize(final IResource resource
,
241 final Class
<?
extends ITmfEvent
> type
)
242 throws TmfTraceException
{
244 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
247 fResource
= resource
;
248 String traceName
= getName();
249 if (traceName
.isEmpty()) {
250 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
252 super.init(traceName
, type
);
253 // register as VIP after super.init() because TmfComponent registers to signal manager there
254 TmfSignalManager
.registerVIP(this);
255 if (fIndexer
!= null) {
258 fIndexer
= createIndexer(fCacheSize
);
262 * Indicates if the path points to an existing file/directory
264 * @param path the path to test
265 * @return true if the file/directory exists
267 protected boolean fileExists(final String path
) {
268 final File file
= new File(path
);
269 return file
.exists();
273 public void indexTrace(boolean waitForCompletion
) {
274 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
278 * Instantiate the applicable analysis modules and executes the analysis
279 * modules that are meant to be automatically executed
281 * @return An IStatus indicating whether the analysis could be run
282 * successfully or not
284 protected IStatus
executeAnalysis() {
285 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
287 /* First modules are initialized */
288 Map
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules(this.getClass());
289 for (IAnalysisModuleHelper helper
: modules
.values()) {
291 IAnalysisModule module
= helper
.newModule(this);
292 if (module
== null) {
295 fAnalysisModules
.put(module
.getId(), module
);
296 } catch (TmfAnalysisException e
) {
297 status
.add(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, e
.getMessage()));
301 /* Once all modules are initialized, automatic modules are executed */
302 for (IAnalysisModule module
: getAnalysisModules()) {
303 if (module
.isAutomatic()) {
304 status
.add(module
.schedule());
311 public IAnalysisModule
getAnalysisModule(String analysisId
) {
312 return fAnalysisModules
.get(analysisId
);
317 public Iterable
<IAnalysisModule
> getAnalysisModules() {
318 synchronized (fAnalysisModules
) {
319 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
325 public Iterable
<ITmfEventAspect
> getEventAspects() {
326 /* By default we provide only the base aspects valid for all trace types */
334 public synchronized void dispose() {
335 /* Clean up the index if applicable */
336 if (getIndexer() != null) {
337 getIndexer().dispose();
340 /* Clean up the analysis modules */
341 synchronized (fAnalysisModules
) {
342 for (IAnalysisModule module
: fAnalysisModules
.values()) {
345 fAnalysisModules
.clear();
351 // ------------------------------------------------------------------------
352 // ITmfTrace - Basic getters
353 // ------------------------------------------------------------------------
356 public IResource
getResource() {
361 public @Nullable String
getTraceTypeId() {
366 public String
getPath() {
371 public int getCacheSize() {
376 public long getStreamingInterval() {
377 return fStreamingInterval
;
381 * @return the trace indexer
383 protected ITmfTraceIndexer
getIndexer() {
387 // ------------------------------------------------------------------------
388 // ITmfTrace - Trace characteristics getters
389 // ------------------------------------------------------------------------
392 public long getNbEvents() {
397 public @NonNull TmfTimeRange
getTimeRange() {
398 return new TmfTimeRange(fStartTime
, fEndTime
);
402 public ITmfTimestamp
getStartTime() {
407 public ITmfTimestamp
getEndTime() {
412 public ITmfTimestamp
getInitialRangeOffset() {
413 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
414 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
418 public String
getHostId() {
419 return this.getName();
422 // ------------------------------------------------------------------------
423 // Convenience setters
424 // ------------------------------------------------------------------------
427 * Set the trace cache size. Must be done at initialization time.
429 * @param cacheSize The trace cache size
431 protected void setCacheSize(final int cacheSize
) {
432 fCacheSize
= cacheSize
;
436 * Set the trace known number of events. This can be quite dynamic
437 * during indexing or for live traces.
439 * @param nbEvents The number of events
441 protected synchronized void setNbEvents(final long nbEvents
) {
442 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
446 * Update the trace events time range
448 * @param range the new time range
450 protected void setTimeRange(final @NonNull TmfTimeRange range
) {
451 fStartTime
= range
.getStartTime();
452 fEndTime
= range
.getEndTime();
456 * Update the trace chronologically first event timestamp
458 * @param startTime the new first event timestamp
460 protected void setStartTime(final @NonNull ITmfTimestamp startTime
) {
461 fStartTime
= startTime
;
465 * Update the trace chronologically last event timestamp
467 * @param endTime the new last event timestamp
469 protected void setEndTime(final @NonNull ITmfTimestamp endTime
) {
474 * Set the polling interval for live traces (default = 0 = no streaming).
476 * @param interval the new trace streaming interval
478 protected void setStreamingInterval(final long interval
) {
479 fStreamingInterval
= (interval
> 0) ? interval
: 0;
482 // ------------------------------------------------------------------------
483 // ITmfTrace - SeekEvent operations (returning a trace context)
484 // ------------------------------------------------------------------------
487 public synchronized ITmfContext
seekEvent(final long rank
) {
489 // A rank <= 0 indicates to seek the first event
491 ITmfContext context
= seekEvent((ITmfLocation
) null);
496 // Position the trace at the checkpoint
497 final ITmfContext context
= fIndexer
.seekIndex(rank
);
499 // And locate the requested event context
500 long pos
= context
.getRank();
502 ITmfEvent event
= getNext(context
);
503 while ((event
!= null) && (++pos
< rank
)) {
504 event
= getNext(context
);
511 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
513 // A null timestamp indicates to seek the first event
514 if (timestamp
== null) {
515 ITmfContext context
= seekEvent((ITmfLocation
) null);
520 // Position the trace at the checkpoint
521 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
523 // And locate the requested event context
524 ITmfLocation previousLocation
= context
.getLocation();
525 long previousRank
= context
.getRank();
526 ITmfEvent event
= getNext(context
);
527 while (event
!= null && event
.getTimestamp().compareTo(timestamp
) < 0) {
528 previousLocation
= context
.getLocation();
529 previousRank
= context
.getRank();
530 event
= getNext(context
);
533 context
.setLocation(null);
534 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
537 context
= seekEvent(previousLocation
);
538 context
.setRank(previousRank
);
543 // ------------------------------------------------------------------------
544 // Read operations (returning an actual event)
545 // ------------------------------------------------------------------------
548 public abstract ITmfEvent
parseEvent(ITmfContext context
);
551 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
552 // parseEvent() does not update the context
553 final ITmfEvent event
= parseEvent(context
);
555 updateAttributes(context
, event
);
556 context
.setLocation(getCurrentLocation());
557 context
.increaseRank();
563 * Update the trace attributes
565 * @param context the current trace context
566 * @param timestamp the corresponding timestamp
567 * @deprecated Use {@link #updateAttributes(ITmfContext, ITmfEvent)}
570 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfTimestamp timestamp
) {
571 updateAttributes(context
, new TmfEvent(this, context
.getRank(), timestamp
, null, null));
575 * Update the trace attributes
577 * @param context the current trace context
578 * @param event the corresponding event
581 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfEvent event
) {
582 ITmfTimestamp timestamp
= event
.getTimestamp();
583 ITmfTimestamp endTime
= timestamp
;
584 if (event
instanceof ITmfLostEvent
) {
585 endTime
= ((ITmfLostEvent
) event
).getTimeRange().getEndTime();
587 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
588 fStartTime
= timestamp
;
590 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(endTime
) < 0)) {
593 if (context
.hasValidRank()) {
594 long rank
= context
.getRank();
595 if (fNbEvents
<= rank
) {
596 fNbEvents
= rank
+ 1;
598 if (fIndexer
!= null) {
599 fIndexer
.updateIndex(context
, timestamp
);
604 // ------------------------------------------------------------------------
606 // ------------------------------------------------------------------------
609 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
610 if (executorIsShutdown()) {
613 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
614 && (request
.getIndex() == 0)) {
615 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
616 request
.setStartIndex((int) context
.getRank());
620 return seekEvent(request
.getIndex());
623 // ------------------------------------------------------------------------
625 // ------------------------------------------------------------------------
628 * Handler for the Trace Opened signal
631 * The incoming signal
634 public void traceOpened(TmfTraceOpenedSignal signal
) {
635 boolean signalIsForUs
= false;
636 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
638 signalIsForUs
= true;
643 if (!signalIsForUs
) {
648 * The signal is either for this trace, or for an experiment containing
651 IStatus status
= executeAnalysis();
652 if (!status
.isOK()) {
653 Activator
.log(status
);
656 TmfTraceManager
.refreshSupplementaryFiles(this);
658 if (signal
.getTrace() == this) {
659 /* Additionally, the signal is directly for this trace. */
660 if (getNbEvents() == 0) {
664 /* For a streaming trace, the range updated signal should be sent
665 * by the subclass when a new safe time is determined.
667 if (getStreamingInterval() > 0) {
672 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
673 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
675 // Broadcast in separate thread to prevent deadlock
676 broadcastAsync(rangeUpdatedsignal
);
683 * Signal handler for the TmfTraceRangeUpdatedSignal signal
685 * @param signal The incoming signal
688 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
689 if (signal
.getTrace() == this) {
690 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
695 * Signal handler for the TmfTraceUpdatedSignal signal
697 * @param signal The incoming signal
700 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
701 if (signal
.getSource() == getIndexer()) {
702 fNbEvents
= signal
.getNbEvents();
703 fStartTime
= signal
.getRange().getStartTime();
704 fEndTime
= signal
.getRange().getEndTime();
708 // ------------------------------------------------------------------------
709 // Timestamp transformation functions
710 // ------------------------------------------------------------------------
713 public ITmfTimestampTransform
getTimestampTransform() {
714 if (fTsTransform
== null) {
715 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
721 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
723 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
727 public @NonNull ITmfTimestamp
createTimestamp(long ts
) {
728 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
731 // ------------------------------------------------------------------------
733 // ------------------------------------------------------------------------
736 @SuppressWarnings("nls")
737 public synchronized String
toString() {
738 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
739 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
740 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
744 public boolean isComplete() {
746 * Be default, all traces are "complete" which means no more data will
753 public void setComplete(boolean isComplete
) {
755 * This should be overridden by trace classes that can support live
756 * reading (traces in an incomplete state)