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
.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)
75 * When constructing an event, the concrete trace should use the trace's
76 * timestamp transform to create the timestamp, by either transforming the
77 * parsed time value directly or by using the method createTimestamp().
79 * The concrete class can either specify its own indexer or use the provided
80 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
81 * used as checkpoint interval.
84 * @author Francois Chouinard
87 * @see ITmfTraceIndexer
88 * @see ITmfEventParser
90 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
, ITmfEventParser
, ITmfTraceCompleteness
{
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 * Basic aspects that should be valid for all trace types.
99 public static final @NonNull Collection
<@NonNull ITmfEventAspect
<?
>> BASE_ASPECTS
=
101 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
102 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
103 ITmfEventAspect
.BaseAspects
.CONTENTS
106 // ------------------------------------------------------------------------
107 // Instance attributes
108 // ------------------------------------------------------------------------
110 // The resource used for persistent properties for this trace
111 private IResource fResource
;
114 private @Nullable String fTraceTypeId
;
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 @NonNull ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
127 private @NonNull ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
129 // The trace streaming interval (0 = no streaming)
130 private long fStreamingInterval
= 0;
133 private ITmfTraceIndexer fIndexer
;
135 private ITmfTimestampTransform fTsTransform
;
137 private final Map
<String
, IAnalysisModule
> fAnalysisModules
=
138 Collections
.synchronizedMap(new LinkedHashMap
<String
, IAnalysisModule
>());
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * The default, parameterless, constructor
149 fIndexer
= new TmfCheckpointIndexer(this);
156 * The resource associated to the trace
158 * The type of events that will be read from this trace
160 * The path to the trace on the filesystem
162 * The trace cache size. Pass '-1' to use the default specified
163 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
165 * The trace streaming interval. You can use '0' for post-mortem
167 * @throws TmfTraceException
168 * If something failed during the opening
170 protected TmfTrace(final IResource resource
,
171 final Class
<?
extends ITmfEvent
> type
,
175 throws TmfTraceException
{
177 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
178 fStreamingInterval
= interval
;
179 initialize(resource
, path
, type
);
185 * @param trace the original trace
186 * @throws TmfTraceException Should not happen usually
188 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
191 throw new IllegalArgumentException();
193 fCacheSize
= trace
.getCacheSize();
194 fStreamingInterval
= trace
.getStreamingInterval();
195 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
199 * Creates the indexer instance. Classes extending this class can override
200 * this to provide a different indexer implementation.
202 * @param interval the checkpoints interval
204 * @return the indexer
206 protected ITmfTraceIndexer
createIndexer(int interval
) {
207 return new TmfCheckpointIndexer(this, interval
);
210 // ------------------------------------------------------------------------
211 // ITmfTrace - Initializers
212 // ------------------------------------------------------------------------
215 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
, String traceTypeId
) throws TmfTraceException
{
217 throw new IllegalArgumentException();
220 fTraceTypeId
= traceTypeId
;
221 initTrace(resource
, path
, type
);
225 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
226 initialize(resource
, path
, type
);
230 * Initialize the trace common attributes and the base component.
232 * @param resource the Eclipse resource (trace)
233 * @param path the trace path
234 * @param type the trace event type
236 * @throws TmfTraceException If something failed during the initialization
238 protected void initialize(final IResource resource
,
240 final Class
<?
extends ITmfEvent
> type
)
241 throws TmfTraceException
{
243 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
246 fResource
= resource
;
247 String traceName
= getName();
248 if (traceName
.isEmpty()) {
249 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
251 super.init(traceName
, type
);
252 // register as VIP after super.init() because TmfComponent registers to signal manager there
253 TmfSignalManager
.registerVIP(this);
254 if (fIndexer
!= null) {
257 fIndexer
= createIndexer(fCacheSize
);
261 * Indicates if the path points to an existing file/directory
263 * @param path the path to test
264 * @return true if the file/directory exists
266 protected boolean fileExists(final String path
) {
267 final File file
= new File(path
);
268 return file
.exists();
272 public void indexTrace(boolean waitForCompletion
) {
273 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
277 * Instantiate the applicable analysis modules and executes the analysis
278 * modules that are meant to be automatically executed
280 * @return An IStatus indicating whether the analysis could be run
281 * successfully or not
283 protected IStatus
executeAnalysis() {
284 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
286 /* First modules are initialized */
287 Map
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules(this.getClass());
288 for (IAnalysisModuleHelper helper
: modules
.values()) {
290 IAnalysisModule module
= helper
.newModule(this);
291 if (module
== null) {
294 fAnalysisModules
.put(module
.getId(), module
);
295 } catch (TmfAnalysisException e
) {
296 status
.add(new Status(IStatus
.WARNING
, Activator
.PLUGIN_ID
, e
.getMessage()));
300 /* Once all modules are initialized, automatic modules are executed */
301 for (IAnalysisModule module
: getAnalysisModules()) {
302 if (module
.isAutomatic()) {
303 status
.add(module
.schedule());
310 public IAnalysisModule
getAnalysisModule(String analysisId
) {
311 return fAnalysisModules
.get(analysisId
);
316 public Iterable
<IAnalysisModule
> getAnalysisModules() {
317 synchronized (fAnalysisModules
) {
318 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
324 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {
325 /* By default we provide only the base aspects valid for all trace types */
333 public synchronized void dispose() {
334 /* Clean up the index if applicable */
335 if (getIndexer() != null) {
336 getIndexer().dispose();
339 /* Clean up the analysis modules */
340 synchronized (fAnalysisModules
) {
341 for (IAnalysisModule module
: fAnalysisModules
.values()) {
344 fAnalysisModules
.clear();
350 // ------------------------------------------------------------------------
351 // ITmfTrace - Basic getters
352 // ------------------------------------------------------------------------
355 public IResource
getResource() {
360 public @Nullable String
getTraceTypeId() {
365 public String
getPath() {
370 public int getCacheSize() {
375 public long getStreamingInterval() {
376 return fStreamingInterval
;
380 * @return the trace indexer
382 protected ITmfTraceIndexer
getIndexer() {
386 // ------------------------------------------------------------------------
387 // ITmfTrace - Trace characteristics getters
388 // ------------------------------------------------------------------------
391 public long getNbEvents() {
396 public @NonNull TmfTimeRange
getTimeRange() {
397 return new TmfTimeRange(fStartTime
, fEndTime
);
401 public ITmfTimestamp
getStartTime() {
406 public ITmfTimestamp
getEndTime() {
411 public ITmfTimestamp
getInitialRangeOffset() {
412 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
413 return TmfTimestamp
.fromNanos(DEFAULT_INITIAL_OFFSET_VALUE
);
417 public String
getHostId() {
418 return this.getName();
421 // ------------------------------------------------------------------------
422 // Convenience setters
423 // ------------------------------------------------------------------------
426 * Set the trace cache size. Must be done at initialization time.
428 * @param cacheSize The trace cache size
430 protected void setCacheSize(final int cacheSize
) {
431 fCacheSize
= cacheSize
;
435 * Set the trace known number of events. This can be quite dynamic
436 * during indexing or for live traces.
438 * @param nbEvents The number of events
440 protected synchronized void setNbEvents(final long nbEvents
) {
441 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
445 * Update the trace events time range
447 * @param range the new time range
449 protected void setTimeRange(final @NonNull TmfTimeRange range
) {
450 fStartTime
= range
.getStartTime();
451 fEndTime
= range
.getEndTime();
455 * Update the trace chronologically first event timestamp
457 * @param startTime the new first event timestamp
459 protected void setStartTime(final @NonNull ITmfTimestamp startTime
) {
460 fStartTime
= startTime
;
464 * Update the trace chronologically last event timestamp
466 * @param endTime the new last event timestamp
468 protected void setEndTime(final @NonNull ITmfTimestamp endTime
) {
473 * Set the polling interval for live traces (default = 0 = no streaming).
475 * @param interval the new trace streaming interval
477 protected void setStreamingInterval(final long interval
) {
478 fStreamingInterval
= (interval
> 0) ? interval
: 0;
481 // ------------------------------------------------------------------------
482 // ITmfTrace - SeekEvent operations (returning a trace context)
483 // ------------------------------------------------------------------------
486 public synchronized ITmfContext
seekEvent(final long rank
) {
488 // A rank <= 0 indicates to seek the first event
490 ITmfContext context
= seekEvent((ITmfLocation
) null);
495 // Position the trace at the checkpoint
496 final ITmfContext context
= fIndexer
.seekIndex(rank
);
498 // And locate the requested event context
499 long pos
= context
.getRank();
501 ITmfEvent event
= getNext(context
);
502 while ((event
!= null) && (++pos
< rank
)) {
503 event
= getNext(context
);
510 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
512 // A null timestamp indicates to seek the first event
513 if (timestamp
== null) {
514 ITmfContext context
= seekEvent((ITmfLocation
) null);
519 // Position the trace at the checkpoint
520 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
522 // And locate the requested event context
523 ITmfLocation previousLocation
= context
.getLocation();
524 long previousRank
= context
.getRank();
525 ITmfEvent event
= getNext(context
);
526 while (event
!= null && event
.getTimestamp().compareTo(timestamp
) < 0) {
527 previousLocation
= context
.getLocation();
528 previousRank
= context
.getRank();
529 event
= getNext(context
);
532 context
.setLocation(null);
533 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
536 context
= seekEvent(previousLocation
);
537 context
.setRank(previousRank
);
542 // ------------------------------------------------------------------------
543 // Read operations (returning an actual event)
544 // ------------------------------------------------------------------------
547 public abstract ITmfEvent
parseEvent(ITmfContext context
);
550 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
551 // parseEvent() does not update the context
552 final ITmfEvent event
= parseEvent(context
);
554 updateAttributes(context
, event
);
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
566 * @deprecated Use {@link #updateAttributes(ITmfContext, ITmfEvent)}
569 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfTimestamp timestamp
) {
570 updateAttributes(context
, new TmfEvent(this, context
.getRank(), timestamp
, null, null));
574 * Update the trace attributes
576 * @param context the current trace context
577 * @param event the corresponding event
580 protected synchronized void updateAttributes(final ITmfContext context
, final @NonNull ITmfEvent event
) {
581 ITmfTimestamp timestamp
= event
.getTimestamp();
582 ITmfTimestamp endTime
= timestamp
;
583 if (event
instanceof ITmfLostEvent
) {
584 endTime
= ((ITmfLostEvent
) event
).getTimeRange().getEndTime();
586 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
587 fStartTime
= timestamp
;
589 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(endTime
) < 0)) {
592 if (context
.hasValidRank()) {
593 long rank
= context
.getRank();
594 if (fNbEvents
<= rank
) {
595 fNbEvents
= rank
+ 1;
597 if (fIndexer
!= null) {
598 fIndexer
.updateIndex(context
, timestamp
);
603 // ------------------------------------------------------------------------
605 // ------------------------------------------------------------------------
608 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
609 if (executorIsShutdown()) {
612 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
613 && (request
.getIndex() == 0)) {
614 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
615 request
.setStartIndex((int) context
.getRank());
619 return seekEvent(request
.getIndex());
622 // ------------------------------------------------------------------------
624 // ------------------------------------------------------------------------
627 * Handler for the Trace Opened signal
630 * The incoming signal
633 public void traceOpened(TmfTraceOpenedSignal signal
) {
634 boolean signalIsForUs
= false;
635 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
637 signalIsForUs
= true;
642 if (!signalIsForUs
) {
647 * The signal is either for this trace, or for an experiment containing
650 IStatus status
= executeAnalysis();
651 if (!status
.isOK()) {
652 Activator
.log(status
);
655 TmfTraceManager
.refreshSupplementaryFiles(this);
657 if (signal
.getTrace() == this) {
658 /* Additionally, the signal is directly for this trace. */
659 if (getNbEvents() == 0) {
663 /* For a streaming trace, the range updated signal should be sent
664 * by the subclass when a new safe time is determined.
666 if (getStreamingInterval() > 0) {
671 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
672 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
674 // Broadcast in separate thread to prevent deadlock
675 broadcastAsync(rangeUpdatedsignal
);
682 * Signal handler for the TmfTraceRangeUpdatedSignal signal
684 * @param signal The incoming signal
687 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
688 if (signal
.getTrace() == this) {
689 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
694 * Signal handler for the TmfTraceUpdatedSignal signal
696 * @param signal The incoming signal
699 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
700 if (signal
.getSource() == getIndexer()) {
701 fNbEvents
= signal
.getNbEvents();
702 fStartTime
= signal
.getRange().getStartTime();
703 fEndTime
= signal
.getRange().getEndTime();
707 // ------------------------------------------------------------------------
708 // Timestamp transformation functions
709 // ------------------------------------------------------------------------
712 public ITmfTimestampTransform
getTimestampTransform() {
713 if (fTsTransform
== null) {
714 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
720 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
722 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
726 public @NonNull ITmfTimestamp
createTimestamp(long ts
) {
727 return TmfTimestamp
.fromNanos(getTimestampTransform().transform(ts
));
730 // ------------------------------------------------------------------------
732 // ------------------------------------------------------------------------
735 @SuppressWarnings("nls")
736 public synchronized String
toString() {
737 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
738 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
739 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
743 public boolean isComplete() {
745 * Be default, all traces are "complete" which means no more data will
752 public void setComplete(boolean isComplete
) {
754 * This should be overridden by trace classes that can support live
755 * reading (traces in an incomplete state)