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
.linuxtools
.tmf
.core
.trace
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.FileOutputStream
;
22 import java
.io
.IOException
;
23 import java
.io
.ObjectInputStream
;
24 import java
.io
.ObjectOutputStream
;
25 import java
.util
.Collections
;
26 import java
.util
.HashSet
;
27 import java
.util
.LinkedHashMap
;
29 import java
.util
.Map
.Entry
;
32 import org
.eclipse
.core
.resources
.IFolder
;
33 import org
.eclipse
.core
.resources
.IResource
;
34 import org
.eclipse
.core
.runtime
.CoreException
;
35 import org
.eclipse
.core
.runtime
.IStatus
;
36 import org
.eclipse
.core
.runtime
.MultiStatus
;
37 import org
.eclipse
.core
.runtime
.Path
;
38 import org
.eclipse
.core
.runtime
.Status
;
39 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModule
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAnalysisManager
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfAnalysisException
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
49 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
50 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
51 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
52 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
53 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
54 import org
.eclipse
.linuxtools
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
55 import org
.eclipse
.linuxtools
.tmf
.core
.synchronization
.TimestampTransformFactory
;
56 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
57 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
58 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
59 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
60 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
61 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
62 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
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)
76 * A concrete trace must provide its corresponding parser. A common way to
77 * accomplish this is by making the concrete class extend TmfTrace and
78 * implement ITmfEventParser.
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
, ITmfTraceCompleteness
{
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
97 // The resource used for persistent properties for this trace
98 private IResource fResource
;
101 private String fPath
;
103 // The trace cache page size
104 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
106 // The number of events collected (so far)
107 private volatile long fNbEvents
= 0;
109 // The time span of the event stream
110 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
111 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
113 // The trace streaming interval (0 = no streaming)
114 private long fStreamingInterval
= 0;
117 private ITmfTraceIndexer fIndexer
;
120 private ITmfEventParser fParser
;
122 private ITmfTimestampTransform fTsTransform
;
124 private final Map
<String
, IAnalysisModule
> fAnalysisModules
=
125 Collections
.synchronizedMap(new LinkedHashMap
<String
, IAnalysisModule
>());
127 private static final String SYNCHRONIZATION_FORMULA_FILE
= "sync_formula"; //$NON-NLS-1$
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
134 * The default, parameterless, constructor
138 fIndexer
= createIndexer(DEFAULT_BLOCK_SIZE
);
145 * The resource associated to the trace
147 * The type of events that will be read from this trace
149 * The path to the trace on the filesystem
151 * The trace cache size. Pass '-1' to use the default specified
152 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
154 * The trace streaming interval. You can use '0' for post-mortem
157 * The trace event parser. Use 'null' if (and only if) the trace
158 * object itself is also the ITmfEventParser to be used.
159 * @throws TmfTraceException
160 * If something failed during the opening
162 protected TmfTrace(final IResource resource
,
163 final Class
<?
extends ITmfEvent
> type
,
167 final ITmfEventParser parser
)
168 throws TmfTraceException
{
170 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
171 fStreamingInterval
= interval
;
173 initialize(resource
, path
, type
);
179 * @param trace the original trace
180 * @throws TmfTraceException Should not happen usually
182 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
185 throw new IllegalArgumentException();
187 fCacheSize
= trace
.getCacheSize();
188 fStreamingInterval
= trace
.getStreamingInterval();
189 fParser
= trace
.fParser
;
190 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
194 * Creates the indexer instance. Classes extending this class can override
195 * this to provide a different indexer implementation.
197 * @param interval the checkpoints interval
199 * @return the indexer
202 protected ITmfTraceIndexer
createIndexer(int interval
) {
203 return new TmfCheckpointIndexer(this, interval
);
206 // ------------------------------------------------------------------------
207 // ITmfTrace - Initializers
208 // ------------------------------------------------------------------------
211 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
, String name
) throws TmfTraceException
{
213 initTrace(resource
, path
, type
);
217 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
218 initialize(resource
, path
, type
);
222 * Initialize the trace common attributes and the base component.
224 * @param resource the Eclipse resource (trace)
225 * @param path the trace path
226 * @param type the trace event type
228 * @throws TmfTraceException If something failed during the initialization
230 protected void initialize(final IResource resource
,
232 final Class
<?
extends ITmfEvent
> type
)
233 throws TmfTraceException
{
235 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
238 fResource
= resource
;
239 String traceName
= getName();
240 if (traceName
== null || traceName
.isEmpty()) {
241 traceName
= (resource
!= null) ? resource
.getName() : new Path(path
).lastSegment();
243 if (fParser
== null) {
244 if (this instanceof ITmfEventParser
) {
245 fParser
= (ITmfEventParser
) this;
247 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
250 super.init(traceName
, type
);
251 // register as VIP after super.init() because TmfComponent registers to signal manager there
252 TmfSignalManager
.registerVIP(this);
253 if (fIndexer
!= null) {
256 fIndexer
= createIndexer(fCacheSize
);
260 * Indicates if the path points to an existing file/directory
262 * @param path the path to test
263 * @return true if the file/directory exists
265 protected boolean fileExists(final String path
) {
266 final File file
= new File(path
);
267 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
286 protected IStatus
executeAnalysis() {
287 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
288 Map
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules(this.getClass());
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()));
307 public IAnalysisModule
getAnalysisModule(String analysisId
) {
308 return fAnalysisModules
.get(analysisId
);
316 public Iterable
<IAnalysisModule
> getAnalysisModules() {
317 synchronized (fAnalysisModules
) {
318 Set
<IAnalysisModule
> modules
= new HashSet
<>(fAnalysisModules
.values());
327 public <T
extends IAnalysisModule
> T
getAnalysisModuleOfClass(Class
<T
> moduleClass
, String id
) {
328 Iterable
<T
> modules
= getAnalysisModulesOfClass(moduleClass
);
329 for (T module
: modules
) {
330 if (id
.equals(module
.getId())) {
341 public <T
> Iterable
<T
> getAnalysisModulesOfClass(Class
<T
> moduleClass
) {
342 Set
<T
> modules
= new HashSet
<>();
343 synchronized (fAnalysisModules
) {
344 for (Entry
<String
, IAnalysisModule
> entry
: fAnalysisModules
.entrySet()) {
345 if (moduleClass
.isAssignableFrom(entry
.getValue().getClass())) {
346 modules
.add(moduleClass
.cast(entry
.getValue()));
357 public synchronized void dispose() {
358 /* Clean up the index if applicable */
359 if (getIndexer() != null) {
360 getIndexer().dispose();
363 /* Clean up the analysis modules */
364 synchronized (fAnalysisModules
) {
365 for (IAnalysisModule module
: fAnalysisModules
.values()) {
368 fAnalysisModules
.clear();
374 // ------------------------------------------------------------------------
375 // ITmfTrace - Basic getters
376 // ------------------------------------------------------------------------
379 public Class
<?
extends ITmfEvent
> getEventType() {
380 return super.getType();
384 public IResource
getResource() {
389 public String
getPath() {
394 public int getCacheSize() {
399 public long getStreamingInterval() {
400 return fStreamingInterval
;
404 * @return the trace indexer
407 protected ITmfTraceIndexer
getIndexer() {
412 * @return the trace parser
414 protected ITmfEventParser
getParser() {
418 // ------------------------------------------------------------------------
419 // ITmfTrace - Trace characteristics getters
420 // ------------------------------------------------------------------------
423 public long getNbEvents() {
431 public TmfTimeRange
getTimeRange() {
432 return new TmfTimeRange(fStartTime
, fEndTime
);
439 public ITmfTimestamp
getStartTime() {
447 public ITmfTimestamp
getEndTime() {
455 public ITmfTimestamp
getInitialRangeOffset() {
456 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
457 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
464 public String
getHostId() {
465 return this.getName();
468 // ------------------------------------------------------------------------
469 // Convenience setters
470 // ------------------------------------------------------------------------
473 * Set the trace cache size. Must be done at initialization time.
475 * @param cacheSize The trace cache size
477 protected void setCacheSize(final int cacheSize
) {
478 fCacheSize
= cacheSize
;
482 * Set the trace known number of events. This can be quite dynamic
483 * during indexing or for live traces.
485 * @param nbEvents The number of events
487 protected synchronized void setNbEvents(final long nbEvents
) {
488 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
492 * Update the trace events time range
494 * @param range the new time range
497 protected void setTimeRange(final TmfTimeRange range
) {
498 fStartTime
= range
.getStartTime();
499 fEndTime
= range
.getEndTime();
503 * Update the trace chronologically first event timestamp
505 * @param startTime the new first event timestamp
508 protected void setStartTime(final ITmfTimestamp startTime
) {
509 fStartTime
= startTime
;
513 * Update the trace chronologically last event timestamp
515 * @param endTime the new last event timestamp
518 protected void setEndTime(final ITmfTimestamp endTime
) {
523 * Set the polling interval for live traces (default = 0 = no streaming).
525 * @param interval the new trace streaming interval
527 protected void setStreamingInterval(final long interval
) {
528 fStreamingInterval
= (interval
> 0) ? interval
: 0;
532 * Set the trace parser. Must be done at initialization time.
534 * @param parser the new trace parser
536 protected void setParser(final ITmfEventParser parser
) {
540 // ------------------------------------------------------------------------
541 // ITmfTrace - SeekEvent operations (returning a trace context)
542 // ------------------------------------------------------------------------
545 public synchronized ITmfContext
seekEvent(final long rank
) {
547 // A rank <= 0 indicates to seek the first event
549 ITmfContext context
= seekEvent((ITmfLocation
) null);
554 // Position the trace at the checkpoint
555 final ITmfContext context
= fIndexer
.seekIndex(rank
);
557 // And locate the requested event context
558 long pos
= context
.getRank();
560 ITmfEvent event
= getNext(context
);
561 while ((event
!= null) && (++pos
< rank
)) {
562 event
= getNext(context
);
572 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
574 // A null timestamp indicates to seek the first event
575 if (timestamp
== null) {
576 ITmfContext context
= seekEvent((ITmfLocation
) null);
581 // Position the trace at the checkpoint
582 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
584 // And locate the requested event context
585 ITmfLocation previousLocation
= context
.getLocation();
586 long previousRank
= context
.getRank();
587 ITmfEvent event
= getNext(context
);
588 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
589 previousLocation
= context
.getLocation();
590 previousRank
= context
.getRank();
591 event
= getNext(context
);
594 context
.setLocation(null);
595 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
598 context
= seekEvent(previousLocation
);
599 context
.setRank(previousRank
);
604 // ------------------------------------------------------------------------
605 // ITmfTrace - Read operations (returning an actual event)
606 // ------------------------------------------------------------------------
609 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
610 // parseEvent() does not update the context
611 final ITmfEvent event
= fParser
.parseEvent(context
);
613 updateAttributes(context
, event
.getTimestamp());
614 context
.setLocation(getCurrentLocation());
615 context
.increaseRank();
622 * Hook for special event processing by the concrete class
623 * (called by TmfTrace.getEvent())
625 * @param event the event
627 protected void processEvent(final ITmfEvent event
) {
632 * Update the trace attributes
634 * @param context the current trace context
635 * @param timestamp the corresponding timestamp
638 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
639 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
640 fStartTime
= timestamp
;
642 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
643 fEndTime
= timestamp
;
645 if (context
.hasValidRank()) {
646 long rank
= context
.getRank();
647 if (fNbEvents
<= rank
) {
648 fNbEvents
= rank
+ 1;
650 if (fIndexer
!= null) {
651 fIndexer
.updateIndex(context
, timestamp
);
656 // ------------------------------------------------------------------------
658 // ------------------------------------------------------------------------
664 public synchronized ITmfContext
armRequest(final ITmfEventRequest request
) {
665 if (executorIsShutdown()) {
668 if (!TmfTimestamp
.BIG_BANG
.equals(request
.getRange().getStartTime())
669 && (request
.getIndex() == 0)) {
670 final ITmfContext context
= seekEvent(request
.getRange().getStartTime());
671 request
.setStartIndex((int) context
.getRank());
675 return seekEvent(request
.getIndex());
678 // ------------------------------------------------------------------------
680 // ------------------------------------------------------------------------
683 * Handler for the Trace Opened signal
686 * The incoming signal
690 public void traceOpened(TmfTraceOpenedSignal signal
) {
691 boolean signalIsForUs
= false;
692 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
694 signalIsForUs
= true;
699 if (!signalIsForUs
) {
704 * The signal is either for this trace, or for an experiment containing
707 IStatus status
= executeAnalysis();
708 if (!status
.isOK()) {
709 Activator
.log(status
);
712 TmfTraceManager
.refreshSupplementaryFiles(this);
714 if (signal
.getTrace() == this) {
715 /* Additionally, the signal is directly for this trace. */
716 if (getNbEvents() == 0) {
720 /* For a streaming trace, the range updated signal should be sent
721 * by the subclass when a new safe time is determined.
723 if (getStreamingInterval() > 0) {
728 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
729 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
731 // Broadcast in separate thread to prevent deadlock
732 broadcastAsync(rangeUpdatedsignal
);
739 * Signal handler for the TmfTraceRangeUpdatedSignal signal
741 * @param signal The incoming signal
745 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
746 if (signal
.getTrace() == this) {
747 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
752 * Signal handler for the TmfTraceUpdatedSignal signal
754 * @param signal The incoming signal
758 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
759 if (signal
.getSource() == getIndexer()) {
760 fNbEvents
= signal
.getNbEvents();
761 fStartTime
= signal
.getRange().getStartTime();
762 fEndTime
= signal
.getRange().getEndTime();
767 * Returns the file resource used to store synchronization formula. The file
770 * @return the synchronization file
772 private File
getSyncFormulaFile() {
774 if (fResource
instanceof IFolder
) {
776 String supplDirectory
;
778 supplDirectory
= fResource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
780 file
= new File(supplDirectory
+ File
.separator
+ SYNCHRONIZATION_FORMULA_FILE
);
782 } catch (CoreException e
) {
789 // ------------------------------------------------------------------------
790 // Timestamp transformation functions
791 // ------------------------------------------------------------------------
797 public ITmfTimestampTransform
getTimestampTransform() {
798 if (fTsTransform
== null) {
799 /* Check if a formula is stored somewhere in the resources */
800 File sync_file
= getSyncFormulaFile();
801 if (sync_file
!= null && sync_file
.exists()) {
803 try (FileInputStream fis
= new FileInputStream(sync_file
);
804 ObjectInputStream ois
= new ObjectInputStream(fis
);) {
806 fTsTransform
= (ITmfTimestampTransform
) ois
.readObject();
808 } catch (ClassNotFoundException
| IOException e
) {
809 fTsTransform
= TimestampTransformFactory
.getDefaultTransform();
812 fTsTransform
= TimestampTransformFactory
.getDefaultTransform();
822 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
825 /* Save the timestamp transform to a file */
826 File sync_file
= getSyncFormulaFile();
827 if (sync_file
!= null) {
828 if (sync_file
.exists()) {
832 /* Save the header of the file */
833 try (FileOutputStream fos
= new FileOutputStream(sync_file
, false);
834 ObjectOutputStream oos
= new ObjectOutputStream(fos
);) {
836 oos
.writeObject(fTsTransform
);
837 } catch (IOException e1
) {
838 Activator
.logError("Error writing timestamp transform for trace", e1
); //$NON-NLS-1$
847 public ITmfTimestamp
createTimestamp(long ts
) {
848 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
851 // ------------------------------------------------------------------------
853 // ------------------------------------------------------------------------
856 @SuppressWarnings("nls")
857 public synchronized String
toString() {
858 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
859 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
860 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";
867 public boolean isComplete() {
869 * Be default, all traces are "complete" which means no more data will
879 public void setComplete(boolean isComplete
) {
881 * This should be overridden by trace classes that can support live
882 * reading (traces in an incomplete state)