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
.tracecompass
.internal
.tmf
.core
.Activator
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfEventProvider
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
60 import com
.google
.common
.collect
.ImmutableList
;
63 * Abstract implementation of ITmfTrace.
65 * Since the concept of 'location' is trace specific, the concrete classes have
66 * to provide the related methods, namely:
68 * <li> public ITmfLocation<?> getCurrentLocation()
69 * <li> public double getLocationRatio(ITmfLocation<?> location)
70 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
71 * <li> public ITmfContext seekEvent(double ratio)
72 * <li> public IStatus validate(IProject project, String path)
74 * A concrete trace must provide its corresponding parser. A common way to
75 * accomplish this is by making the concrete class extend TmfTrace and
76 * implement ITmfEventParser.
78 * When constructing an event, the concrete trace should use the trace's
79 * timestamp transform to create the timestamp, by either transforming the
80 * parsed time value directly or by using the method createTimestamp().
82 * The concrete class can either specify its own indexer or use the provided
83 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
84 * used as checkpoint interval.
87 * @author Francois Chouinard
90 * @see ITmfTraceIndexer
91 * @see ITmfEventParser
93 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
, ITmfTraceCompleteness
{
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 * Basic aspects that should be valid for all trace types.
102 public static final @NonNull Collection
<ITmfEventAspect
> BASE_ASPECTS
=
103 checkNotNull(ImmutableList
.of(
104 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
105 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
106 ITmfEventAspect
.BaseAspects
.CONTENTS
109 // ------------------------------------------------------------------------
110 // Instance attributes
111 // ------------------------------------------------------------------------
113 // The resource used for persistent properties for this trace
114 private IResource fResource
;
117 private String fPath
;
119 // The trace cache page size
120 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
122 // The number of events collected (so far)
123 private volatile long fNbEvents
= 0;
125 // The time span of the event stream
126 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
127 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
129 // The trace streaming interval (0 = no streaming)
130 private long fStreamingInterval
= 0;
133 private ITmfTraceIndexer fIndexer
;
136 private ITmfEventParser fParser
;
138 private ITmfTimestampTransform fTsTransform
;
140 private final Map
<String
, IAnalysisModule
> fAnalysisModules
=
141 Collections
.synchronizedMap(new LinkedHashMap
<String
, IAnalysisModule
>());
143 // ------------------------------------------------------------------------
145 // ------------------------------------------------------------------------
148 * The default, parameterless, constructor
152 fIndexer
= createIndexer(DEFAULT_BLOCK_SIZE
);
159 * The resource associated to the trace
161 * The type of events that will be read from this trace
163 * The path to the trace on the filesystem
165 * The trace cache size. Pass '-1' to use the default specified
166 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
168 * The trace streaming interval. You can use '0' for post-mortem
171 * The trace event parser. Use 'null' if (and only if) the trace
172 * object itself is also the ITmfEventParser to be used.
173 * @throws TmfTraceException
174 * If something failed during the opening
176 protected TmfTrace(final IResource resource
,
177 final Class
<?
extends ITmfEvent
> type
,
181 final ITmfEventParser parser
)
182 throws TmfTraceException
{
184 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
185 fStreamingInterval
= interval
;
187 initialize(resource
, path
, type
);
193 * @param trace the original trace
194 * @throws TmfTraceException Should not happen usually
196 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
199 throw new IllegalArgumentException();
201 fCacheSize
= trace
.getCacheSize();
202 fStreamingInterval
= trace
.getStreamingInterval();
203 fParser
= trace
.fParser
;
204 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
208 * Creates the indexer instance. Classes extending this class can override
209 * this to provide a different indexer implementation.
211 * @param interval the checkpoints interval
213 * @return the indexer
216 protected ITmfTraceIndexer
createIndexer(int interval
) {
217 return new TmfCheckpointIndexer(this, interval
);
220 // ------------------------------------------------------------------------
221 // ITmfTrace - Initializers
222 // ------------------------------------------------------------------------
225 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
) throws TmfTraceException
{
227 throw new IllegalArgumentException();
230 initTrace(resource
, path
, type
);
234 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
235 initialize(resource
, path
, type
);
239 * Initialize the trace common attributes and the base component.
241 * @param resource the Eclipse resource (trace)
242 * @param path the trace path
243 * @param type the trace event type
245 * @throws TmfTraceException If something failed during the initialization
247 protected void initialize(final IResource resource
,
249 final Class
<?
extends ITmfEvent
> type
)
250 throws TmfTraceException
{
252 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
255 fResource
= resource
;
256 String traceName
= getName();
257 if (traceName
.isEmpty()) {
258 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
260 if (fParser
== null) {
261 if (this instanceof ITmfEventParser
) {
262 fParser
= (ITmfEventParser
) this;
264 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
267 super.init(traceName
, type
);
268 // register as VIP after super.init() because TmfComponent registers to signal manager there
269 TmfSignalManager
.registerVIP(this);
270 if (fIndexer
!= null) {
273 fIndexer
= createIndexer(fCacheSize
);
277 * Indicates if the path points to an existing file/directory
279 * @param path the path to test
280 * @return true if the file/directory exists
282 protected boolean fileExists(final String path
) {
283 final File file
= new File(path
);
284 return file
.exists();
291 public void indexTrace(boolean waitForCompletion
) {
292 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
296 * Instantiate the applicable analysis modules and executes the analysis
297 * modules that are meant to be automatically executed
299 * @return An IStatus indicating whether the analysis could be run
300 * successfully or not
303 protected IStatus
executeAnalysis() {
304 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
306 Class
<?
extends TmfTrace
> className
= checkNotNull(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());
343 public Iterable
<ITmfEventAspect
> getEventAspects() {
344 /* By default we provide only the base aspects valid for all trace types */
352 public synchronized void dispose() {
353 /* Clean up the index if applicable */
354 if (getIndexer() != null) {
355 getIndexer().dispose();
358 /* Clean up the analysis modules */
359 synchronized (fAnalysisModules
) {
360 for (IAnalysisModule module
: fAnalysisModules
.values()) {
363 fAnalysisModules
.clear();
369 // ------------------------------------------------------------------------
370 // ITmfTrace - Basic getters
371 // ------------------------------------------------------------------------
374 public Class
<?
extends ITmfEvent
> getEventType() {
375 return super.getType();
379 public IResource
getResource() {
384 public String
getPath() {
389 public int getCacheSize() {
394 public long getStreamingInterval() {
395 return fStreamingInterval
;
399 * @return the trace indexer
402 protected ITmfTraceIndexer
getIndexer() {
407 * @return the trace parser
409 protected ITmfEventParser
getParser() {
413 // ------------------------------------------------------------------------
414 // ITmfTrace - Trace characteristics getters
415 // ------------------------------------------------------------------------
418 public long getNbEvents() {
426 public TmfTimeRange
getTimeRange() {
427 return new TmfTimeRange(fStartTime
, fEndTime
);
434 public ITmfTimestamp
getStartTime() {
442 public ITmfTimestamp
getEndTime() {
450 public ITmfTimestamp
getInitialRangeOffset() {
451 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
452 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
459 public String
getHostId() {
460 return this.getName();
463 // ------------------------------------------------------------------------
464 // Convenience setters
465 // ------------------------------------------------------------------------
468 * Set the trace cache size. Must be done at initialization time.
470 * @param cacheSize The trace cache size
472 protected void setCacheSize(final int cacheSize
) {
473 fCacheSize
= cacheSize
;
477 * Set the trace known number of events. This can be quite dynamic
478 * during indexing or for live traces.
480 * @param nbEvents The number of events
482 protected synchronized void setNbEvents(final long nbEvents
) {
483 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
487 * Update the trace events time range
489 * @param range the new time range
492 protected void setTimeRange(final TmfTimeRange range
) {
493 fStartTime
= range
.getStartTime();
494 fEndTime
= range
.getEndTime();
498 * Update the trace chronologically first event timestamp
500 * @param startTime the new first event timestamp
503 protected void setStartTime(final ITmfTimestamp startTime
) {
504 fStartTime
= startTime
;
508 * Update the trace chronologically last event timestamp
510 * @param endTime the new last event timestamp
513 protected void setEndTime(final ITmfTimestamp endTime
) {
518 * Set the polling interval for live traces (default = 0 = no streaming).
520 * @param interval the new trace streaming interval
522 protected void setStreamingInterval(final long interval
) {
523 fStreamingInterval
= (interval
> 0) ? interval
: 0;
527 * Set the trace parser. Must be done at initialization time.
529 * @param parser the new trace parser
531 protected void setParser(final ITmfEventParser parser
) {
535 // ------------------------------------------------------------------------
536 // ITmfTrace - SeekEvent operations (returning a trace context)
537 // ------------------------------------------------------------------------
540 public synchronized ITmfContext
seekEvent(final long rank
) {
542 // A rank <= 0 indicates to seek the first event
544 ITmfContext context
= seekEvent((ITmfLocation
) null);
549 // Position the trace at the checkpoint
550 final ITmfContext context
= fIndexer
.seekIndex(rank
);
552 // And locate the requested event context
553 long pos
= context
.getRank();
555 ITmfEvent event
= getNext(context
);
556 while ((event
!= null) && (++pos
< rank
)) {
557 event
= getNext(context
);
567 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
569 // A null timestamp indicates to seek the first event
570 if (timestamp
== null) {
571 ITmfContext context
= seekEvent((ITmfLocation
) null);
576 // Position the trace at the checkpoint
577 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
579 // And locate the requested event context
580 ITmfLocation previousLocation
= context
.getLocation();
581 long previousRank
= context
.getRank();
582 ITmfEvent event
= getNext(context
);
583 while (event
!= null && event
.getTimestamp().compareTo(timestamp
) < 0) {
584 previousLocation
= context
.getLocation();
585 previousRank
= context
.getRank();
586 event
= getNext(context
);
589 context
.setLocation(null);
590 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
593 context
= seekEvent(previousLocation
);
594 context
.setRank(previousRank
);
599 // ------------------------------------------------------------------------
600 // ITmfTrace - Read operations (returning an actual event)
601 // ------------------------------------------------------------------------
604 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
605 // parseEvent() does not update the context
606 final ITmfEvent event
= fParser
.parseEvent(context
);
608 updateAttributes(context
, event
.getTimestamp());
609 context
.setLocation(getCurrentLocation());
610 context
.increaseRank();
617 * Hook for special event processing by the concrete class
618 * (called by TmfTrace.getEvent())
620 * @param event the event
622 protected void processEvent(final ITmfEvent event
) {
627 * Update the trace attributes
629 * @param context the current trace context
630 * @param timestamp the corresponding timestamp
633 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
634 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
635 fStartTime
= timestamp
;
637 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
) < 0)) {
638 fEndTime
= timestamp
;
640 if (context
.hasValidRank()) {
641 long rank
= context
.getRank();
642 if (fNbEvents
<= rank
) {
643 fNbEvents
= rank
+ 1;
645 if (fIndexer
!= null) {
646 fIndexer
.updateIndex(context
, timestamp
);
651 // ------------------------------------------------------------------------
653 // ------------------------------------------------------------------------
659 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
660 if (executorIsShutdown()) {
663 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
664 && (request
.getIndex() == 0)) {
665 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
666 request
.setStartIndex((int) context
.getRank());
670 return seekEvent(request
.getIndex());
673 // ------------------------------------------------------------------------
675 // ------------------------------------------------------------------------
678 * Handler for the Trace Opened signal
681 * The incoming signal
685 public void traceOpened(TmfTraceOpenedSignal signal
) {
686 boolean signalIsForUs
= false;
687 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
689 signalIsForUs
= true;
694 if (!signalIsForUs
) {
699 * The signal is either for this trace, or for an experiment containing
702 IStatus status
= executeAnalysis();
703 if (!status
.isOK()) {
704 Activator
.log(status
);
707 TmfTraceManager
.refreshSupplementaryFiles(this);
709 if (signal
.getTrace() == this) {
710 /* Additionally, the signal is directly for this trace. */
711 if (getNbEvents() == 0) {
715 /* For a streaming trace, the range updated signal should be sent
716 * by the subclass when a new safe time is determined.
718 if (getStreamingInterval() > 0) {
723 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
724 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
726 // Broadcast in separate thread to prevent deadlock
727 broadcastAsync(rangeUpdatedsignal
);
734 * Signal handler for the TmfTraceRangeUpdatedSignal signal
736 * @param signal The incoming signal
740 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
741 if (signal
.getTrace() == this) {
742 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
747 * Signal handler for the TmfTraceUpdatedSignal signal
749 * @param signal The incoming signal
753 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
754 if (signal
.getSource() == getIndexer()) {
755 fNbEvents
= signal
.getNbEvents();
756 fStartTime
= signal
.getRange().getStartTime();
757 fEndTime
= signal
.getRange().getEndTime();
761 // ------------------------------------------------------------------------
762 // Timestamp transformation functions
763 // ------------------------------------------------------------------------
769 public ITmfTimestampTransform
getTimestampTransform() {
770 if (fTsTransform
== null) {
771 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
780 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
782 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
789 public ITmfTimestamp
createTimestamp(long ts
) {
790 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
793 // ------------------------------------------------------------------------
795 // ------------------------------------------------------------------------
798 @SuppressWarnings("nls")
799 public synchronized String
toString() {
800 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
801 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
802 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
809 public boolean isComplete() {
811 * Be default, all traces are "complete" which means no more data will
821 public void setComplete(boolean isComplete
) {
823 * This should be overridden by trace classes that can support live
824 * reading (traces in an incomplete state)