1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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
.FileNotFoundException
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.IOException
;
24 import java
.io
.ObjectInputStream
;
25 import java
.io
.ObjectOutputStream
;
26 import java
.util
.Collections
;
27 import java
.util
.LinkedHashMap
;
30 import org
.eclipse
.core
.resources
.IFolder
;
31 import org
.eclipse
.core
.resources
.IResource
;
32 import org
.eclipse
.core
.runtime
.CoreException
;
33 import org
.eclipse
.core
.runtime
.IStatus
;
34 import org
.eclipse
.core
.runtime
.MultiStatus
;
35 import org
.eclipse
.core
.runtime
.Path
;
36 import org
.eclipse
.core
.runtime
.Status
;
37 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
49 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
50 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
51 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.TmfStateStatistics
;
52 import org
.eclipse
.linuxtools
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
53 import org
.eclipse
.linuxtools
.tmf
.core
.synchronization
.TmfTimestampTransform
;
54 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
55 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
56 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
57 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
58 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
59 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
62 * Abstract implementation of ITmfTrace.
64 * Since the concept of 'location' is trace specific, the concrete classes have
65 * to provide the related methods, namely:
67 * <li> public ITmfLocation<?> getCurrentLocation()
68 * <li> public double getLocationRatio(ITmfLocation<?> location)
69 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
70 * <li> public ITmfContext seekEvent(double ratio)
71 * <li> public IStatus validate(IProject project, String path)
73 * A concrete trace must provide its corresponding parser. A common way to
74 * accomplish this is by making the concrete class extend TmfTrace and
75 * implement ITmfEventParser.
77 * The concrete class can either specify its own indexer or use the provided
78 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
79 * used as checkpoint interval.
82 * @author Francois Chouinard
85 * @see ITmfTraceIndexer
86 * @see ITmfEventParser
88 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
{
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
94 // The resource used for persistent properties for this trace
95 private IResource fResource
;
100 // The trace cache page size
101 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
103 // The number of events collected (so far)
104 private long fNbEvents
= 0;
106 // The time span of the event stream
107 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
108 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
110 // The trace streaming interval (0 = no streaming)
111 private long fStreamingInterval
= 0;
114 private ITmfTraceIndexer fIndexer
;
117 private ITmfEventParser fParser
;
119 // The trace's statistics
120 private ITmfStatistics fStatistics
;
123 * The collection of state systems that are registered with this trace. Each
124 * sub-class can decide to add its (one or many) state system to this map
125 * during their {@link #buildStateSystem()}.
129 protected final Map
<String
, ITmfStateSystem
> fStateSystems
=
130 new LinkedHashMap
<String
, ITmfStateSystem
>();
132 private ITmfTimestampTransform fTsTransform
;
134 private static final String SYNCHRONIZATION_FORMULA_FILE
= "sync_formula"; //$NON-NLS-1$
136 // ------------------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 * The default, parameterless, constructor
151 * The resource associated to the trace
153 * The type of events that will be read from this trace
155 * The path to the trace on the filesystem
157 * The trace cache size. Pass '-1' to use the default specified
158 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
160 * The trace streaming interval. You can use '0' for post-mortem
163 * The trace indexer. You can pass 'null' to use a default
164 * checkpoint indexer.
166 * The trace event parser. Use 'null' if (and only if) the trace
167 * object itself is also the ITmfEventParser to be used.
168 * @throws TmfTraceException
169 * If something failed during the opening
171 protected TmfTrace(final IResource resource
,
172 final Class
<?
extends ITmfEvent
> type
,
176 final ITmfTraceIndexer indexer
,
177 final ITmfEventParser parser
)
178 throws TmfTraceException
{
180 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
181 fStreamingInterval
= interval
;
184 initialize(resource
, path
, type
);
190 * @param trace the original trace
191 * @throws TmfTraceException Should not happen usually
193 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
196 throw new IllegalArgumentException();
198 fCacheSize
= trace
.getCacheSize();
199 fStreamingInterval
= trace
.getStreamingInterval();
200 fParser
= trace
.fParser
;
201 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
205 * Creates the indexer instance. Classes extending this class can override
206 * this to provide a different indexer implementation.
208 * @param interval the checkpoints interval
210 * @return the indexer
213 protected ITmfTraceIndexer
createIndexer(int interval
) {
214 return new TmfCheckpointIndexer(this, interval
);
217 // ------------------------------------------------------------------------
218 // ITmfTrace - Initializers
219 // ------------------------------------------------------------------------
222 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
223 initialize(resource
, path
, type
);
227 * Initialize the trace common attributes and the base component.
229 * @param resource the Eclipse resource (trace)
230 * @param path the trace path
231 * @param type the trace event type
233 * @throws TmfTraceException If something failed during the initialization
235 protected void initialize(final IResource resource
,
237 final Class
<?
extends ITmfEvent
> type
)
238 throws TmfTraceException
{
240 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
243 fResource
= resource
;
244 String traceName
= (resource
!= null) ? resource
.getName() : null;
245 // If no resource was provided, extract the display name the trace path
246 if (traceName
== null) {
247 traceName
= new Path(path
).lastSegment();
249 if (fParser
== null) {
250 if (this instanceof ITmfEventParser
) {
251 fParser
= (ITmfEventParser
) this;
253 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
256 super.init(traceName
, type
);
257 // register as VIP after super.init() because TmfComponent registers to signal manager there
258 TmfSignalManager
.registerVIP(this);
259 if (fIndexer
== null) {
260 fIndexer
= createIndexer(fCacheSize
);
265 * Indicates if the path points to an existing file/directory
267 * @param path the path to test
268 * @return true if the file/directory exists
270 protected boolean fileExists(final String path
) {
271 final File file
= new File(path
);
272 return file
.exists();
279 public void indexTrace(boolean waitForCompletion
) {
280 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
284 * The default implementation of TmfTrace uses a TmfStatistics back-end.
285 * Override this if you want to specify another type (or none at all).
287 * @return An IStatus indicating if the statistics could be built
288 * successfully or not.
291 protected IStatus
buildStatistics() {
293 * Initialize the statistics provider, but only if a Resource has been
294 * set (so we don't build it for experiments, for unit tests, etc.)
297 fStatistics
= (fResource
== null ?
null : new TmfStateStatistics(this) );
298 } catch (TmfTraceException e
) {
299 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage(), e
);
301 return Status
.OK_STATUS
;
305 * Build the state system(s) associated with this trace type.
307 * @return An IStatus indicating if the state system could be build
308 * successfully or not.
311 protected IStatus
buildStateSystem() {
313 * Nothing is done in the base implementation, please specify
314 * how/if to register a new state system in derived classes.
316 return Status
.OK_STATUS
;
323 public synchronized void dispose() {
324 /* Clean up the index if applicable */
325 if (getIndexer() != null) {
326 getIndexer().dispose();
329 /* Clean up the statistics */
330 if (fStatistics
!= null) {
331 fStatistics
.dispose();
334 /* Clean up the state systems */
335 for (ITmfStateSystem ss
: fStateSystems
.values()) {
342 // ------------------------------------------------------------------------
343 // ITmfTrace - Basic getters
344 // ------------------------------------------------------------------------
347 public Class
<ITmfEvent
> getEventType() {
348 return (Class
<ITmfEvent
>) super.getType();
352 public IResource
getResource() {
357 public String
getPath() {
362 public int getCacheSize() {
367 public long getStreamingInterval() {
368 return fStreamingInterval
;
372 * @return the trace indexer
375 protected ITmfTraceIndexer
getIndexer() {
380 * @return the trace parser
382 protected ITmfEventParser
getParser() {
390 public ITmfStatistics
getStatistics() {
398 public final Map
<String
, ITmfStateSystem
> getStateSystems() {
399 return Collections
.unmodifiableMap(fStateSystems
);
406 public final void registerStateSystem(String id
, ITmfStateSystem ss
) {
407 fStateSystems
.put(id
, ss
);
410 // ------------------------------------------------------------------------
411 // ITmfTrace - Trace characteristics getters
412 // ------------------------------------------------------------------------
415 public synchronized long getNbEvents() {
423 public TmfTimeRange
getTimeRange() {
424 return new TmfTimeRange(fStartTime
, fEndTime
);
431 public ITmfTimestamp
getStartTime() {
439 public ITmfTimestamp
getEndTime() {
447 public ITmfTimestamp
getInitialRangeOffset() {
448 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
449 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
456 public String
getHostId() {
457 return this.getName();
460 // ------------------------------------------------------------------------
461 // Convenience setters
462 // ------------------------------------------------------------------------
465 * Set the trace cache size. Must be done at initialization time.
467 * @param cacheSize The trace cache size
469 protected void setCacheSize(final int cacheSize
) {
470 fCacheSize
= cacheSize
;
474 * Set the trace known number of events. This can be quite dynamic
475 * during indexing or for live traces.
477 * @param nbEvents The number of events
479 protected synchronized void setNbEvents(final long nbEvents
) {
480 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
484 * Update the trace events time range
486 * @param range the new time range
489 protected void setTimeRange(final TmfTimeRange range
) {
490 fStartTime
= range
.getStartTime();
491 fEndTime
= range
.getEndTime();
495 * Update the trace chronologically first event timestamp
497 * @param startTime the new first event timestamp
500 protected void setStartTime(final ITmfTimestamp startTime
) {
501 fStartTime
= startTime
;
505 * Update the trace chronologically last event timestamp
507 * @param endTime the new last event timestamp
510 protected void setEndTime(final ITmfTimestamp endTime
) {
515 * Set the polling interval for live traces (default = 0 = no streaming).
517 * @param interval the new trace streaming interval
519 protected void setStreamingInterval(final long interval
) {
520 fStreamingInterval
= (interval
> 0) ? interval
: 0;
524 * Set the trace indexer. Must be done at initialization time.
526 * @param indexer the trace indexer
529 protected void setIndexer(final ITmfTraceIndexer indexer
) {
534 * Set the trace parser. Must be done at initialization time.
536 * @param parser the new trace parser
538 protected void setParser(final ITmfEventParser parser
) {
542 // ------------------------------------------------------------------------
543 // ITmfTrace - SeekEvent operations (returning a trace context)
544 // ------------------------------------------------------------------------
547 public synchronized ITmfContext
seekEvent(final long rank
) {
549 // A rank <= 0 indicates to seek the first event
551 ITmfContext context
= seekEvent((ITmfLocation
) null);
556 // Position the trace at the checkpoint
557 final ITmfContext context
= fIndexer
.seekIndex(rank
);
559 // And locate the requested event context
560 long pos
= context
.getRank();
562 ITmfEvent event
= getNext(context
);
563 while ((event
!= null) && (++pos
< rank
)) {
564 event
= getNext(context
);
574 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
576 // A null timestamp indicates to seek the first event
577 if (timestamp
== null) {
578 ITmfContext context
= seekEvent((ITmfLocation
) null);
583 // Position the trace at the checkpoint
584 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
586 // And locate the requested event context
587 ITmfLocation previousLocation
= context
.getLocation();
588 long previousRank
= context
.getRank();
589 ITmfEvent event
= getNext(context
);
590 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
591 previousLocation
= context
.getLocation();
592 previousRank
= context
.getRank();
593 event
= getNext(context
);
596 context
.setLocation(null);
597 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
600 context
= seekEvent(previousLocation
);
601 context
.setRank(previousRank
);
606 // ------------------------------------------------------------------------
607 // ITmfTrace - Read operations (returning an actual event)
608 // ------------------------------------------------------------------------
611 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
612 // parseEvent() does not update the context
613 final ITmfEvent event
= fParser
.parseEvent(context
);
615 updateAttributes(context
, event
.getTimestamp());
616 context
.setLocation(getCurrentLocation());
617 context
.increaseRank();
624 * Hook for special event processing by the concrete class
625 * (called by TmfTrace.getEvent())
627 * @param event the event
629 protected void processEvent(final ITmfEvent event
) {
634 * Update the trace attributes
636 * @param context the current trace context
637 * @param timestamp the corresponding timestamp
640 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
641 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
642 fStartTime
= timestamp
;
644 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
645 fEndTime
= timestamp
;
647 if (context
.hasValidRank()) {
648 long rank
= context
.getRank();
649 if (fNbEvents
<= rank
) {
650 fNbEvents
= rank
+ 1;
652 if (fIndexer
!= null) {
653 fIndexer
.updateIndex(context
, timestamp
);
658 // ------------------------------------------------------------------------
660 // ------------------------------------------------------------------------
666 public synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
667 if (executorIsShutdown()) {
670 if ((request
instanceof ITmfEventRequest
)
671 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
672 && (request
.getIndex() == 0))
674 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
675 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
679 return seekEvent(request
.getIndex());
682 // ------------------------------------------------------------------------
684 // ------------------------------------------------------------------------
687 * Handler for the Trace Opened signal
690 * The incoming signal
694 public void traceOpened(TmfTraceOpenedSignal signal
) {
695 boolean signalIsForUs
= false;
696 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
698 signalIsForUs
= true;
703 if (!signalIsForUs
) {
708 * The signal is either for this trace, or for an experiment containing
711 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
712 status
.add(buildStatistics());
713 status
.add(buildStateSystem());
714 if (!status
.isOK()) {
715 Activator
.log(status
);
718 /* Refresh the project, so it can pick up new files that got created. */
720 if (fResource
!= null) {
721 fResource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
723 } catch (CoreException e
) {
727 if (signal
.getTrace() == this) {
728 /* Additionally, the signal is directly for this trace. */
729 if (getNbEvents() == 0) {
733 /* For a streaming trace, the range updated signal should be sent
734 * by the subclass when a new safe time is determined.
736 if (getStreamingInterval() > 0) {
740 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
741 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
743 // Broadcast in separate thread to prevent deadlock
747 broadcast(rangeUpdatedsignal
);
755 * Signal handler for the TmfTraceRangeUpdatedSignal signal
757 * @param signal The incoming signal
761 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
762 if (signal
.getTrace() == this) {
763 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
768 * Signal handler for the TmfTraceUpdatedSignal signal
770 * @param signal The incoming signal
774 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
775 if (signal
.getSource() == getIndexer()) {
776 fNbEvents
= signal
.getNbEvents();
777 fStartTime
= signal
.getRange().getStartTime();
778 fEndTime
= signal
.getRange().getEndTime();
783 * Returns the file resource used to store synchronization formula. The file
786 * @return the synchronization file
788 private File
getSyncFormulaFile() {
790 if (fResource
instanceof IFolder
) {
792 String supplDirectory
;
794 supplDirectory
= fResource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
796 file
= new File(supplDirectory
+ File
.separator
+ SYNCHRONIZATION_FORMULA_FILE
);
798 } catch (CoreException e
) {
805 // ------------------------------------------------------------------------
806 // Timestamp transformation functions
807 // ------------------------------------------------------------------------
813 public ITmfTimestampTransform
getTimestampTransform() {
814 if (fTsTransform
== null) {
815 /* Check if a formula is stored somewhere in the resources */
816 File sync_file
= getSyncFormulaFile();
817 if (sync_file
!= null && sync_file
.exists()) {
820 FileInputStream fis
= new FileInputStream(sync_file
);
821 ObjectInputStream ois
= new ObjectInputStream(fis
);
822 fTsTransform
= (ITmfTimestampTransform
) ois
.readObject();
826 } catch (ClassNotFoundException e1
) {
827 fTsTransform
= TmfTimestampTransform
.IDENTITY
;
828 } catch (FileNotFoundException e1
) {
829 fTsTransform
= TmfTimestampTransform
.IDENTITY
;
830 } catch (IOException e1
) {
831 fTsTransform
= TmfTimestampTransform
.IDENTITY
;
834 fTsTransform
= TmfTimestampTransform
.IDENTITY
;
844 public void setTimestampTransform(final ITmfTimestampTransform tt
) {
847 /* Save the timestamp transform to a file */
848 File sync_file
= getSyncFormulaFile();
849 if (sync_file
!= null) {
850 if (sync_file
.exists()) {
853 FileOutputStream fos
;
854 ObjectOutputStream oos
;
856 /* Save the header of the file */
858 fos
= new FileOutputStream(sync_file
, false);
859 oos
= new ObjectOutputStream(fos
);
861 oos
.writeObject(fTsTransform
);
864 } catch (IOException e1
) {
865 Activator
.logError("Error writing timestamp transform for trace", e1
); //$NON-NLS-1$
874 public ITmfTimestamp
createTimestamp(long ts
) {
875 return new TmfTimestamp(getTimestampTransform().transform(ts
));
878 // ------------------------------------------------------------------------
880 // ------------------------------------------------------------------------
883 @SuppressWarnings("nls")
884 public synchronized String
toString() {
885 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
886 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
887 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";