1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.trace
;
17 import java
.util
.Collection
;
18 import java
.util
.HashMap
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.TmfStateStatistics
;
42 * Abstract implementation of ITmfTrace.
44 * Since the concept of 'location' is trace specific, the concrete classes have
45 * to provide the related methods, namely:
47 * <li> public ITmfLocation<?> getCurrentLocation()
48 * <li> public double getLocationRatio(ITmfLocation<?> location)
49 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
50 * <li> public ITmfContext seekEvent(double ratio)
51 * <li> public boolean validate(IProject project, String path)
53 * A concrete trace must provide its corresponding parser. A common way to
54 * accomplish this is by making the concrete class extend TmfTrace and
55 * implement ITmfEventParser.
57 * The concrete class can either specify its own indexer or use the provided
58 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
59 * used as checkpoint interval.
62 * @author Francois Chouinard
65 * @see ITmfTraceIndexer
66 * @see ITmfEventParser
68 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
{
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 // The resource used for persistent properties for this trace
75 private IResource fResource
;
80 // The trace cache page size
81 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
83 // The number of events collected (so far)
84 private long fNbEvents
= 0;
86 // The time span of the event stream
87 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
88 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
90 // The trace streaming interval (0 = no streaming)
91 private long fStreamingInterval
= 0;
94 private ITmfTraceIndexer fIndexer
;
97 private ITmfEventParser fParser
;
99 // The trace's statistics
100 private ITmfStatistics fStatistics
;
102 // The current selected time
103 private ITmfTimestamp fCurrentTime
= TmfTimestamp
.ZERO
;
105 // The current selected range
106 private TmfTimeRange fCurrentRange
= TmfTimeRange
.NULL_RANGE
;
109 * The collection of state systems that are registered with this trace. Each
110 * sub-class can decide to add its (one or many) state system to this map
111 * during their {@link #buildStateSystem()}.
115 protected final Map
<String
, ITmfStateSystem
> fStateSystems
=
116 new HashMap
<String
, ITmfStateSystem
>();
118 // ------------------------------------------------------------------------
120 // ------------------------------------------------------------------------
123 * The default, parameterless, constructor
130 * The standard constructor (non-live trace). Applicable when the trace
131 * implements its own parser and if at checkpoint-based index is OK.
133 * @param resource the resource associated to the trace
134 * @param type the trace event type
135 * @param path the trace path
136 * @param cacheSize the trace cache size
137 * @throws TmfTraceException If something failed during the opening
139 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
) throws TmfTraceException
{
140 this(resource
, type
, path
, cacheSize
, 0);
144 * The standard constructor (live trace). Applicable when the trace
145 * implements its own parser and if at checkpoint-based index is OK.
147 * @param resource the resource associated to the trace
148 * @param type the trace event type
149 * @param path the trace path
150 * @param cacheSize the trace cache size
151 * @param interval the trace streaming interval
152 * @throws TmfTraceException If something failed during the opening
154 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
155 this(resource
, type
, path
, cacheSize
, interval
, null);
159 * The 'non-default indexer' constructor. Allows to provide a trace
162 * @param resource the resource associated to the trace
163 * @param type the trace event type
164 * @param path the trace path
165 * @param cacheSize the trace cache size
166 * @param interval the trace streaming interval
167 * @param indexer the trace indexer
168 * @throws TmfTraceException If something failed during the opening
170 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
171 final long interval
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
172 this(resource
, type
, path
, cacheSize
, interval
, indexer
, null);
176 * The full constructor where trace specific indexer/parser are provided.
178 * @param resource the resource associated to the trace
179 * @param type the trace event type
180 * @param path the trace path
181 * @param cacheSize the trace cache size
182 * @param interval the trace streaming interval
183 * @param indexer the trace indexer
184 * @param parser the trace event parser
185 * @throws TmfTraceException If something failed during the opening
187 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
188 final long interval
, final ITmfTraceIndexer indexer
, final ITmfEventParser parser
) throws TmfTraceException
{
190 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
191 fStreamingInterval
= interval
;
192 fIndexer
= (indexer
!= null) ? indexer
: new TmfCheckpointIndexer(this, fCacheSize
);
194 initialize(resource
, path
, type
);
200 * @param trace the original trace
201 * @throws TmfTraceException Should not happen usually
203 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
206 throw new IllegalArgumentException();
208 fCacheSize
= trace
.getCacheSize();
209 fStreamingInterval
= trace
.getStreamingInterval();
210 fIndexer
= new TmfCheckpointIndexer(this);
211 fParser
= trace
.fParser
;
212 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
215 // ------------------------------------------------------------------------
216 // ITmfTrace - Initializers
217 // ------------------------------------------------------------------------
220 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
223 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
224 fIndexer
= new TmfCheckpointIndexer(this, fCacheSize
);
225 initialize(resource
, path
, type
);
229 * Initialize the trace common attributes and the base component.
231 * @param resource the Eclipse resource (trace)
232 * @param path the trace path
233 * @param type the trace event type
235 * @throws TmfTraceException If something failed during the initialization
237 protected void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
239 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
242 fResource
= resource
;
243 String traceName
= (resource
!= null) ? resource
.getName() : null;
244 // If no resource was provided, extract the display name the trace path
245 if (traceName
== null) {
246 final int sep
= path
.lastIndexOf(IPath
.SEPARATOR
);
247 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
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
);
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();
273 * @param waitForCompletion index synchronously (true) or not (false)
275 protected void indexTrace(boolean waitForCompletion
) {
276 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
280 * The default implementation of TmfTrace uses a TmfStatistics back-end.
281 * Override this if you want to specify another type (or none at all).
283 * @throws TmfTraceException
284 * If there was a problem setting up the statistics
287 protected void buildStatistics() throws TmfTraceException
{
289 * Initialize the statistics provider, but only if a Resource has been
290 * set (so we don't build it for experiments, for unit tests, etc.)
292 fStatistics
= (fResource
== null ?
null : new TmfStateStatistics(this) );
296 * Build the state system(s) associated with this trace type.
298 * Suppressing the warning, because the 'throws' will usually happen in
301 * @throws TmfTraceException
302 * If there is a problem during the build
305 @SuppressWarnings("unused")
306 protected void buildStateSystem() throws TmfTraceException
{
308 * Nothing is done in the base implementation, please specify
309 * how/if to register a new state system in derived classes.
318 public synchronized void dispose() {
319 /* Clean up the index if applicable */
320 if (getIndexer() != null) {
321 getIndexer().dispose();
324 /* Clean up the statistics */
325 if (fStatistics
!= null) {
326 fStatistics
.dispose();
329 /* Clean up the state systems */
330 for (ITmfStateSystem ss
: fStateSystems
.values()) {
337 // ------------------------------------------------------------------------
338 // ITmfTrace - Basic getters
339 // ------------------------------------------------------------------------
342 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
345 public Class
<ITmfEvent
> getEventType() {
346 return (Class
<ITmfEvent
>) super.getType();
350 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
353 public IResource
getResource() {
358 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
361 public String
getPath() {
366 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
369 public int getCacheSize() {
374 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
377 public long getStreamingInterval() {
378 return fStreamingInterval
;
382 * @return the trace indexer
384 protected ITmfTraceIndexer
getIndexer() {
389 * @return the trace parser
391 protected ITmfEventParser
getParser() {
399 public ITmfStatistics
getStatistics() {
407 public final ITmfStateSystem
getStateSystem(String id
) {
408 return fStateSystems
.get(id
);
415 public final Collection
<String
> listStateSystems() {
416 return fStateSystems
.keySet();
419 // ------------------------------------------------------------------------
420 // ITmfTrace - Trace characteristics getters
421 // ------------------------------------------------------------------------
424 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
427 public synchronized long getNbEvents() {
432 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
435 public TmfTimeRange
getTimeRange() {
436 return new TmfTimeRange(fStartTime
, fEndTime
);
440 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
443 public ITmfTimestamp
getStartTime() {
448 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
451 public ITmfTimestamp
getEndTime() {
456 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentTime()
462 public ITmfTimestamp
getCurrentTime() {
467 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentRange()
473 public TmfTimeRange
getCurrentRange() {
474 return fCurrentRange
;
479 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getInitialRangeOffset()
485 public ITmfTimestamp
getInitialRangeOffset() {
486 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
487 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
490 // ------------------------------------------------------------------------
491 // Convenience setters
492 // ------------------------------------------------------------------------
495 * Set the trace cache size. Must be done at initialization time.
497 * @param cacheSize The trace cache size
499 protected void setCacheSize(final int cacheSize
) {
500 fCacheSize
= cacheSize
;
504 * Set the trace known number of events. This can be quite dynamic
505 * during indexing or for live traces.
507 * @param nbEvents The number of events
509 protected synchronized void setNbEvents(final long nbEvents
) {
510 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
514 * Update the trace events time range
516 * @param range the new time range
518 protected void setTimeRange(final TmfTimeRange range
) {
519 fStartTime
= range
.getStartTime();
520 fEndTime
= range
.getEndTime();
524 * Update the trace chronologically first event timestamp
526 * @param startTime the new first event timestamp
528 protected void setStartTime(final ITmfTimestamp startTime
) {
529 fStartTime
= startTime
;
533 * Update the trace chronologically last event timestamp
535 * @param endTime the new last event timestamp
537 protected void setEndTime(final ITmfTimestamp endTime
) {
542 * Set the polling interval for live traces (default = 0 = no streaming).
544 * @param interval the new trace streaming interval
546 protected void setStreamingInterval(final long interval
) {
547 fStreamingInterval
= (interval
> 0) ? interval
: 0;
551 * Set the trace indexer. Must be done at initialization time.
553 * @param indexer the trace indexer
555 protected void setIndexer(final ITmfTraceIndexer indexer
) {
560 * Set the trace parser. Must be done at initialization time.
562 * @param parser the new trace parser
564 protected void setParser(final ITmfEventParser parser
) {
568 // ------------------------------------------------------------------------
569 // ITmfTrace - SeekEvent operations (returning a trace context)
570 // ------------------------------------------------------------------------
573 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
576 public synchronized ITmfContext
seekEvent(final long rank
) {
578 // A rank <= 0 indicates to seek the first event
580 ITmfContext context
= seekEvent((ITmfLocation
) null);
585 // Position the trace at the checkpoint
586 final ITmfContext context
= fIndexer
.seekIndex(rank
);
588 // And locate the requested event context
589 long pos
= context
.getRank();
591 ITmfEvent event
= getNext(context
);
592 while ((event
!= null) && (++pos
< rank
)) {
593 event
= getNext(context
);
600 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
603 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
605 // A null timestamp indicates to seek the first event
606 if (timestamp
== null) {
607 ITmfContext context
= seekEvent((ITmfLocation
) null);
612 // Position the trace at the checkpoint
613 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
615 // And locate the requested event context
616 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
617 ITmfEvent event
= getNext(nextEventContext
);
618 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
620 context
= nextEventContext
.clone();
621 event
= getNext(nextEventContext
);
623 nextEventContext
.dispose();
625 context
.setLocation(null);
626 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
631 // ------------------------------------------------------------------------
632 // ITmfTrace - Read operations (returning an actual event)
633 // ------------------------------------------------------------------------
636 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
639 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
640 // parseEvent() does not update the context
641 final ITmfEvent event
= fParser
.parseEvent(context
);
643 updateAttributes(context
, event
.getTimestamp());
644 context
.setLocation(getCurrentLocation());
645 context
.increaseRank();
652 * Hook for special event processing by the concrete class
653 * (called by TmfTrace.getEvent())
655 * @param event the event
657 protected void processEvent(final ITmfEvent event
) {
662 * Update the trace attributes
664 * @param context the current trace context
665 * @param timestamp the corresponding timestamp
667 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
668 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
669 fStartTime
= timestamp
;
671 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
672 fEndTime
= timestamp
;
674 if (fCurrentRange
== TmfTimeRange
.NULL_RANGE
) {
675 fCurrentTime
= timestamp
;
676 ITmfTimestamp initialOffset
= getInitialRangeOffset();
677 long endValue
= timestamp
.getValue() + initialOffset
.normalize(0, timestamp
.getScale()).getValue();
678 ITmfTimestamp endTimestamp
= new TmfTimestamp(endValue
, timestamp
.getScale());
679 fCurrentRange
= new TmfTimeRange(timestamp
, endTimestamp
);
681 if (context
.hasValidRank()) {
682 long rank
= context
.getRank();
683 if (fNbEvents
<= rank
) {
684 fNbEvents
= rank
+ 1;
686 if (fIndexer
!= null) {
687 fIndexer
.updateIndex(context
, timestamp
);
692 // ------------------------------------------------------------------------
694 // ------------------------------------------------------------------------
697 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
700 public synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
701 if (executorIsShutdown()) {
704 if ((request
instanceof ITmfEventRequest
)
705 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
706 && (request
.getIndex() == 0))
708 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
709 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
713 return seekEvent(request
.getIndex());
716 // ------------------------------------------------------------------------
718 // ------------------------------------------------------------------------
721 * Handler for the Trace Opened signal
724 * The incoming signal
728 public void traceOpened(TmfTraceOpenedSignal signal
) {
729 ITmfTrace trace
= signal
.getTrace();
730 if (signal
.getTrace() instanceof TmfExperiment
) {
731 TmfExperiment experiment
= (TmfExperiment
) signal
.getTrace();
732 for (ITmfTrace expTrace
: experiment
.getTraces()) {
733 if (expTrace
== this) {
740 /* the signal is for this trace or for an experiment containing this trace */
743 } catch (TmfTraceException e
) {
748 } catch (TmfTraceException e
) {
752 /* Refresh the project, so it can pick up new files that got created. */
754 if (fResource
!= null) {
755 fResource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
757 } catch (CoreException e
) {
761 if (signal
.getTrace() == this) {
762 /* the signal is for this trace or experiment */
763 if (getNbEvents() == 0) {
767 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
768 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
770 // Broadcast in separate thread to prevent deadlock
774 broadcast(rangeUpdatedsignal
);
782 * Signal handler for the TmfTraceRangeUpdatedSignal signal
784 * @param signal The incoming signal
788 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
789 if (signal
.getTrace() == this) {
790 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
795 * Signal handler for the TmfTimeSynchSignal signal
797 * @param signal The incoming signal
801 public void synchToTime(final TmfTimeSynchSignal signal
) {
802 if (signal
.getCurrentTime().compareTo(fStartTime
) >= 0 && signal
.getCurrentTime().compareTo(fEndTime
) <= 0) {
803 fCurrentTime
= signal
.getCurrentTime();
808 * Signal handler for the TmfRangeSynchSignal signal
810 * @param signal The incoming signal
814 public void synchToRange(final TmfRangeSynchSignal signal
) {
815 if (signal
.getCurrentTime().compareTo(fStartTime
) >= 0 && signal
.getCurrentTime().compareTo(fEndTime
) <= 0) {
816 fCurrentTime
= signal
.getCurrentTime();
818 if (signal
.getCurrentRange().getIntersection(getTimeRange()) != null) {
819 fCurrentRange
= signal
.getCurrentRange().getIntersection(getTimeRange());
823 // ------------------------------------------------------------------------
825 // ------------------------------------------------------------------------
828 * @see java.lang.Object#toString()
831 @SuppressWarnings("nls")
832 public synchronized String
toString() {
833 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
834 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
835 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";