1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012, 2013 Ericsson
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 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.core
.trace
;
18 import java
.util
.Collection
;
19 import java
.util
.HashMap
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IPath
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.TmfStateStatistics
;
43 * Abstract implementation of ITmfTrace.
45 * Since the concept of 'location' is trace specific, the concrete classes have
46 * to provide the related methods, namely:
48 * <li> public ITmfLocation<?> getCurrentLocation()
49 * <li> public double getLocationRatio(ITmfLocation<?> location)
50 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
51 * <li> public ITmfContext seekEvent(double ratio)
52 * <li> public boolean validate(IProject project, String path)
54 * A concrete trace must provide its corresponding parser. A common way to
55 * accomplish this is by making the concrete class extend TmfTrace and
56 * implement ITmfEventParser.
58 * The concrete class can either specify its own indexer or use the provided
59 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
60 * used as checkpoint interval.
63 * @author Francois Chouinard
66 * @see ITmfTraceIndexer
67 * @see ITmfEventParser
69 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
{
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 // The resource used for persistent properties for this trace
76 private IResource fResource
;
81 // The trace cache page size
82 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
84 // The number of events collected (so far)
85 private long fNbEvents
= 0;
87 // The time span of the event stream
88 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
89 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
91 // The trace streaming interval (0 = no streaming)
92 private long fStreamingInterval
= 0;
95 private ITmfTraceIndexer fIndexer
;
98 private ITmfEventParser fParser
;
100 // The trace's statistics
101 private ITmfStatistics fStatistics
;
103 // The current selected time
104 private ITmfTimestamp fCurrentTime
= TmfTimestamp
.ZERO
;
106 // The current selected range
107 private TmfTimeRange fCurrentRange
= TmfTimeRange
.NULL_RANGE
;
110 * The collection of state systems that are registered with this trace. Each
111 * sub-class can decide to add its (one or many) state system to this map
112 * during their {@link #buildStateSystem()}.
116 protected final Map
<String
, ITmfStateSystem
> fStateSystems
=
117 new HashMap
<String
, ITmfStateSystem
>();
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
124 * The default, parameterless, constructor
131 * The standard constructor (non-live trace). Applicable when the trace
132 * implements its own parser and if at checkpoint-based index is OK.
134 * @param resource the resource associated to the trace
135 * @param type the trace event type
136 * @param path the trace path
137 * @param cacheSize the trace cache size
138 * @throws TmfTraceException If something failed during the opening
140 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
) throws TmfTraceException
{
141 this(resource
, type
, path
, cacheSize
, 0);
145 * The standard constructor (live trace). Applicable when the trace
146 * implements its own parser and if at checkpoint-based index is OK.
148 * @param resource the resource associated to the trace
149 * @param type the trace event type
150 * @param path the trace path
151 * @param cacheSize the trace cache size
152 * @param interval the trace streaming interval
153 * @throws TmfTraceException If something failed during the opening
155 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
156 this(resource
, type
, path
, cacheSize
, interval
, null);
160 * The 'non-default indexer' constructor. Allows to provide a trace
163 * @param resource the resource associated to the trace
164 * @param type the trace event type
165 * @param path the trace path
166 * @param cacheSize the trace cache size
167 * @param interval the trace streaming interval
168 * @param indexer the trace indexer
169 * @throws TmfTraceException If something failed during the opening
171 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
172 final long interval
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
173 this(resource
, type
, path
, cacheSize
, interval
, indexer
, null);
177 * The full constructor where trace specific indexer/parser are provided.
179 * @param resource the resource associated to the trace
180 * @param type the trace event type
181 * @param path the trace path
182 * @param cacheSize the trace cache size
183 * @param interval the trace streaming interval
184 * @param indexer the trace indexer
185 * @param parser the trace event parser
186 * @throws TmfTraceException If something failed during the opening
188 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
189 final long interval
, final ITmfTraceIndexer indexer
, final ITmfEventParser parser
) throws TmfTraceException
{
191 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
192 fStreamingInterval
= interval
;
193 fIndexer
= (indexer
!= null) ? indexer
: new TmfCheckpointIndexer(this, fCacheSize
);
195 initialize(resource
, path
, type
);
201 * @param trace the original trace
202 * @throws TmfTraceException Should not happen usually
204 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
207 throw new IllegalArgumentException();
209 fCacheSize
= trace
.getCacheSize();
210 fStreamingInterval
= trace
.getStreamingInterval();
211 fIndexer
= new TmfCheckpointIndexer(this);
212 fParser
= trace
.fParser
;
213 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
216 // ------------------------------------------------------------------------
217 // ITmfTrace - Initializers
218 // ------------------------------------------------------------------------
221 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
224 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
225 fIndexer
= new TmfCheckpointIndexer(this, fCacheSize
);
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
, final String path
, final Class
<?
extends ITmfEvent
> type
) 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 final int sep
= path
.lastIndexOf(IPath
.SEPARATOR
);
248 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
250 if (fParser
== null) {
251 if (this instanceof ITmfEventParser
) {
252 fParser
= (ITmfEventParser
) this;
254 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
257 super.init(traceName
, type
);
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();
274 * @param waitForCompletion index synchronously (true) or not (false)
276 protected void indexTrace(boolean waitForCompletion
) {
277 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
281 * The default implementation of TmfTrace uses a TmfStatistics back-end.
282 * Override this if you want to specify another type (or none at all).
284 * @throws TmfTraceException
285 * If there was a problem setting up the statistics
288 protected void buildStatistics() throws TmfTraceException
{
290 * Initialize the statistics provider, but only if a Resource has been
291 * set (so we don't build it for experiments, for unit tests, etc.)
293 fStatistics
= (fResource
== null ?
null : new TmfStateStatistics(this) );
297 * Build the state system(s) associated with this trace type.
299 * Suppressing the warning, because the 'throws' will usually happen in
302 * @throws TmfTraceException
303 * If there is a problem during the build
306 @SuppressWarnings("unused")
307 protected void buildStateSystem() throws TmfTraceException
{
309 * Nothing is done in the base implementation, please specify
310 * how/if to register a new state system in derived classes.
319 public synchronized void dispose() {
320 /* Clean up the index if applicable */
321 if (getIndexer() != null) {
322 getIndexer().dispose();
325 /* Clean up the statistics */
326 if (fStatistics
!= null) {
327 fStatistics
.dispose();
330 /* Clean up the state systems */
331 for (ITmfStateSystem ss
: fStateSystems
.values()) {
338 // ------------------------------------------------------------------------
339 // ITmfTrace - Basic getters
340 // ------------------------------------------------------------------------
343 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
346 public Class
<ITmfEvent
> getEventType() {
347 return (Class
<ITmfEvent
>) super.getType();
351 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
354 public IResource
getResource() {
359 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
362 public String
getPath() {
367 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
370 public int getCacheSize() {
375 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
378 public long getStreamingInterval() {
379 return fStreamingInterval
;
383 * @return the trace indexer
385 protected ITmfTraceIndexer
getIndexer() {
390 * @return the trace parser
392 protected ITmfEventParser
getParser() {
400 public ITmfStatistics
getStatistics() {
408 public final ITmfStateSystem
getStateSystem(String id
) {
409 return fStateSystems
.get(id
);
416 public final Collection
<String
> listStateSystems() {
417 return fStateSystems
.keySet();
420 // ------------------------------------------------------------------------
421 // ITmfTrace - Trace characteristics getters
422 // ------------------------------------------------------------------------
425 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
428 public synchronized long getNbEvents() {
433 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
436 public TmfTimeRange
getTimeRange() {
437 return new TmfTimeRange(fStartTime
, fEndTime
);
441 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
444 public ITmfTimestamp
getStartTime() {
449 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
452 public ITmfTimestamp
getEndTime() {
457 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentTime()
463 public ITmfTimestamp
getCurrentTime() {
468 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentRange()
474 public TmfTimeRange
getCurrentRange() {
475 return fCurrentRange
;
480 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getInitialRangeOffset()
486 public ITmfTimestamp
getInitialRangeOffset() {
487 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
488 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
491 // ------------------------------------------------------------------------
492 // Convenience setters
493 // ------------------------------------------------------------------------
496 * Set the trace cache size. Must be done at initialization time.
498 * @param cacheSize The trace cache size
500 protected void setCacheSize(final int cacheSize
) {
501 fCacheSize
= cacheSize
;
505 * Set the trace known number of events. This can be quite dynamic
506 * during indexing or for live traces.
508 * @param nbEvents The number of events
510 protected synchronized void setNbEvents(final long nbEvents
) {
511 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
515 * Update the trace events time range
517 * @param range the new time range
519 protected void setTimeRange(final TmfTimeRange range
) {
520 fStartTime
= range
.getStartTime();
521 fEndTime
= range
.getEndTime();
525 * Update the trace chronologically first event timestamp
527 * @param startTime the new first event timestamp
529 protected void setStartTime(final ITmfTimestamp startTime
) {
530 fStartTime
= startTime
;
534 * Update the trace chronologically last event timestamp
536 * @param endTime the new last event timestamp
538 protected void setEndTime(final ITmfTimestamp endTime
) {
543 * Set the polling interval for live traces (default = 0 = no streaming).
545 * @param interval the new trace streaming interval
547 protected void setStreamingInterval(final long interval
) {
548 fStreamingInterval
= (interval
> 0) ? interval
: 0;
552 * Set the trace indexer. Must be done at initialization time.
554 * @param indexer the trace indexer
556 protected void setIndexer(final ITmfTraceIndexer indexer
) {
561 * Set the trace parser. Must be done at initialization time.
563 * @param parser the new trace parser
565 protected void setParser(final ITmfEventParser parser
) {
569 // ------------------------------------------------------------------------
570 // ITmfTrace - SeekEvent operations (returning a trace context)
571 // ------------------------------------------------------------------------
574 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
577 public synchronized ITmfContext
seekEvent(final long rank
) {
579 // A rank <= 0 indicates to seek the first event
581 ITmfContext context
= seekEvent((ITmfLocation
) null);
586 // Position the trace at the checkpoint
587 final ITmfContext context
= fIndexer
.seekIndex(rank
);
589 // And locate the requested event context
590 long pos
= context
.getRank();
592 ITmfEvent event
= getNext(context
);
593 while ((event
!= null) && (++pos
< rank
)) {
594 event
= getNext(context
);
601 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
604 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
606 // A null timestamp indicates to seek the first event
607 if (timestamp
== null) {
608 ITmfContext context
= seekEvent((ITmfLocation
) null);
613 // Position the trace at the checkpoint
614 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
616 // And locate the requested event context
617 ITmfLocation previousLocation
= context
.getLocation();
618 long previousRank
= context
.getRank();
619 ITmfEvent event
= getNext(context
);
620 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
621 previousLocation
= context
.getLocation();
622 previousRank
= context
.getRank();
623 event
= getNext(context
);
626 context
.setLocation(null);
627 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
630 context
= seekEvent(previousLocation
);
631 context
.setRank(previousRank
);
636 // ------------------------------------------------------------------------
637 // ITmfTrace - Read operations (returning an actual event)
638 // ------------------------------------------------------------------------
641 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
644 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
645 // parseEvent() does not update the context
646 final ITmfEvent event
= fParser
.parseEvent(context
);
648 updateAttributes(context
, event
.getTimestamp());
649 context
.setLocation(getCurrentLocation());
650 context
.increaseRank();
657 * Hook for special event processing by the concrete class
658 * (called by TmfTrace.getEvent())
660 * @param event the event
662 protected void processEvent(final ITmfEvent event
) {
667 * Update the trace attributes
669 * @param context the current trace context
670 * @param timestamp the corresponding timestamp
672 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
673 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
674 fStartTime
= timestamp
;
676 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
677 fEndTime
= timestamp
;
679 if (fCurrentRange
== TmfTimeRange
.NULL_RANGE
) {
680 fCurrentTime
= timestamp
;
681 ITmfTimestamp initialOffset
= getInitialRangeOffset();
682 long endValue
= timestamp
.getValue() + initialOffset
.normalize(0, timestamp
.getScale()).getValue();
683 ITmfTimestamp endTimestamp
= new TmfTimestamp(endValue
, timestamp
.getScale());
684 fCurrentRange
= new TmfTimeRange(timestamp
, endTimestamp
);
686 if (context
.hasValidRank()) {
687 long rank
= context
.getRank();
688 if (fNbEvents
<= rank
) {
689 fNbEvents
= rank
+ 1;
691 if (fIndexer
!= null) {
692 fIndexer
.updateIndex(context
, timestamp
);
697 // ------------------------------------------------------------------------
699 // ------------------------------------------------------------------------
705 public synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
706 if (executorIsShutdown()) {
709 if ((request
instanceof ITmfEventRequest
)
710 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
711 && (request
.getIndex() == 0))
713 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
714 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
718 return seekEvent(request
.getIndex());
721 // ------------------------------------------------------------------------
723 // ------------------------------------------------------------------------
726 * Handler for the Trace Opened signal
729 * The incoming signal
733 public void traceOpened(TmfTraceOpenedSignal signal
) {
734 ITmfTrace trace
= signal
.getTrace();
735 if (signal
.getTrace() instanceof TmfExperiment
) {
736 TmfExperiment experiment
= (TmfExperiment
) signal
.getTrace();
737 for (ITmfTrace expTrace
: experiment
.getTraces()) {
738 if (expTrace
== this) {
745 /* the signal is for this trace or for an experiment containing this trace */
748 } catch (TmfTraceException e
) {
753 } catch (TmfTraceException e
) {
757 /* Refresh the project, so it can pick up new files that got created. */
759 if (fResource
!= null) {
760 fResource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
762 } catch (CoreException e
) {
766 if (signal
.getTrace() == this) {
767 /* the signal is for this trace or experiment */
768 if (getNbEvents() == 0) {
772 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
773 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
775 // Broadcast in separate thread to prevent deadlock
779 broadcast(rangeUpdatedsignal
);
787 * Signal handler for the TmfTraceRangeUpdatedSignal signal
789 * @param signal The incoming signal
793 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
794 if (signal
.getTrace() == this) {
795 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
800 * Signal handler for the TmfTimeSynchSignal signal
802 * @param signal The incoming signal
806 public void synchToTime(final TmfTimeSynchSignal signal
) {
807 if (signal
.getCurrentTime().compareTo(fStartTime
) >= 0 && signal
.getCurrentTime().compareTo(fEndTime
) <= 0) {
808 fCurrentTime
= signal
.getCurrentTime();
813 * Signal handler for the TmfRangeSynchSignal signal
815 * @param signal The incoming signal
819 public void synchToRange(final TmfRangeSynchSignal signal
) {
820 if (signal
.getCurrentTime().compareTo(fStartTime
) >= 0 && signal
.getCurrentTime().compareTo(fEndTime
) <= 0) {
821 fCurrentTime
= signal
.getCurrentTime();
823 if (signal
.getCurrentRange().getIntersection(getTimeRange()) != null) {
824 fCurrentRange
= signal
.getCurrentRange().getIntersection(getTimeRange());
828 // ------------------------------------------------------------------------
830 // ------------------------------------------------------------------------
833 * @see java.lang.Object#toString()
836 @SuppressWarnings("nls")
837 public synchronized String
toString() {
838 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
839 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
840 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";