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
;
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
<ITmfEventAspect
> BASE_ASPECTS
=
101 checkNotNull(ImmutableList
.of(
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 ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
128 private 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
= createIndexer(DEFAULT_BLOCK_SIZE
);
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 Class
<?
extends TmfTrace
> className
= checkNotNull(this.getClass());
288 Map
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules(className
);
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 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 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 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 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
.getTimestamp());
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
566 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
567 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
) > 0)) {
568 fStartTime
= timestamp
;
570 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
) < 0)) {
571 fEndTime
= timestamp
;
573 if (context
.hasValidRank()) {
574 long rank
= context
.getRank();
575 if (fNbEvents
<= rank
) {
576 fNbEvents
= rank
+ 1;
578 if (fIndexer
!= null) {
579 fIndexer
.updateIndex(context
, timestamp
);
584 // ------------------------------------------------------------------------
586 // ------------------------------------------------------------------------
589 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
590 if (executorIsShutdown()) {
593 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
594 && (request
.getIndex() == 0)) {
595 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
596 request
.setStartIndex((int) context
.getRank());
600 return seekEvent(request
.getIndex());
603 // ------------------------------------------------------------------------
605 // ------------------------------------------------------------------------
608 * Handler for the Trace Opened signal
611 * The incoming signal
614 public void traceOpened(TmfTraceOpenedSignal signal
) {
615 boolean signalIsForUs
= false;
616 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
618 signalIsForUs
= true;
623 if (!signalIsForUs
) {
628 * The signal is either for this trace, or for an experiment containing
631 IStatus status
= executeAnalysis();
632 if (!status
.isOK()) {
633 Activator
.log(status
);
636 TmfTraceManager
.refreshSupplementaryFiles(this);
638 if (signal
.getTrace() == this) {
639 /* Additionally, the signal is directly for this trace. */
640 if (getNbEvents() == 0) {
644 /* For a streaming trace, the range updated signal should be sent
645 * by the subclass when a new safe time is determined.
647 if (getStreamingInterval() > 0) {
652 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
653 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
655 // Broadcast in separate thread to prevent deadlock
656 broadcastAsync(rangeUpdatedsignal
);
663 * Signal handler for the TmfTraceRangeUpdatedSignal signal
665 * @param signal The incoming signal
668 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
669 if (signal
.getTrace() == this) {
670 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
675 * Signal handler for the TmfTraceUpdatedSignal signal
677 * @param signal The incoming signal
680 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
681 if (signal
.getSource() == getIndexer()) {
682 fNbEvents
= signal
.getNbEvents();
683 fStartTime
= signal
.getRange().getStartTime();
684 fEndTime
= signal
.getRange().getEndTime();
688 // ------------------------------------------------------------------------
689 // Timestamp transformation functions
690 // ------------------------------------------------------------------------
693 public ITmfTimestampTransform
getTimestampTransform() {
694 if (fTsTransform
== null) {
695 fTsTransform
= TimestampTransformFactory
.getTimestampTransform(getResource());
701 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
703 TimestampTransformFactory
.setTimestampTransform(getResource(), tt
);
707 public ITmfTimestamp
createTimestamp(long ts
) {
708 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
711 // ------------------------------------------------------------------------
713 // ------------------------------------------------------------------------
716 @SuppressWarnings("nls")
717 public synchronized String
toString() {
718 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
719 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
720 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
724 public boolean isComplete() {
726 * Be default, all traces are "complete" which means no more data will
733 public void setComplete(boolean isComplete
) {
735 * This should be overridden by trace classes that can support live
736 * reading (traces in an incomplete state)