1 /*******************************************************************************
2 * Copyright (c) 2009, 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
.Collections
;
19 import java
.util
.LinkedHashMap
;
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
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.TmfStateStatistics
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
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 LinkedHashMap
<String
, ITmfStateSystem
>();
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
124 * The default, parameterless, constructor
134 * The resource associated to the trace
136 * The type of events that will be read from this trace
138 * The path to the trace on the filesystem
140 * The trace cache size. Pass '-1' to use the default specified
141 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
143 * The trace streaming interval. You can use '0' for post-mortem
146 * The trace indexer. You can pass 'null' to use a default
147 * checkpoint indexer.
149 * The trace event parser. Use 'null' if (and only if) the trace
150 * object itself is also the ITmfEventParser to be used.
151 * @throws TmfTraceException
152 * If something failed during the opening
154 protected TmfTrace(final IResource resource
,
155 final Class
<?
extends ITmfEvent
> type
,
159 final ITmfTraceIndexer indexer
,
160 final ITmfEventParser parser
)
161 throws TmfTraceException
{
163 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
164 fStreamingInterval
= interval
;
165 fIndexer
= (indexer
!= null) ? indexer
: new TmfCheckpointIndexer(this, fCacheSize
);
167 initialize(resource
, path
, type
);
173 * @param trace the original trace
174 * @throws TmfTraceException Should not happen usually
176 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
179 throw new IllegalArgumentException();
181 fCacheSize
= trace
.getCacheSize();
182 fStreamingInterval
= trace
.getStreamingInterval();
183 fIndexer
= new TmfCheckpointIndexer(this);
184 fParser
= trace
.fParser
;
185 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
188 // ------------------------------------------------------------------------
189 // ITmfTrace - Initializers
190 // ------------------------------------------------------------------------
193 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
196 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
197 fIndexer
= new TmfCheckpointIndexer(this, fCacheSize
);
198 initialize(resource
, path
, type
);
202 * Initialize the trace common attributes and the base component.
204 * @param resource the Eclipse resource (trace)
205 * @param path the trace path
206 * @param type the trace event type
208 * @throws TmfTraceException If something failed during the initialization
210 protected void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
212 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
215 fResource
= resource
;
216 String traceName
= (resource
!= null) ? resource
.getName() : null;
217 // If no resource was provided, extract the display name the trace path
218 if (traceName
== null) {
219 final int sep
= path
.lastIndexOf(IPath
.SEPARATOR
);
220 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
222 if (fParser
== null) {
223 if (this instanceof ITmfEventParser
) {
224 fParser
= (ITmfEventParser
) this;
226 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
229 super.init(traceName
, type
);
233 * Indicates if the path points to an existing file/directory
235 * @param path the path to test
236 * @return true if the file/directory exists
238 protected boolean fileExists(final String path
) {
239 final File file
= new File(path
);
240 return file
.exists();
246 * @param waitForCompletion index synchronously (true) or not (false)
248 protected void indexTrace(boolean waitForCompletion
) {
249 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
253 * The default implementation of TmfTrace uses a TmfStatistics back-end.
254 * Override this if you want to specify another type (or none at all).
256 * @throws TmfTraceException
257 * If there was a problem setting up the statistics
260 protected void buildStatistics() throws TmfTraceException
{
262 * Initialize the statistics provider, but only if a Resource has been
263 * set (so we don't build it for experiments, for unit tests, etc.)
265 fStatistics
= (fResource
== null ?
null : new TmfStateStatistics(this) );
269 * Build the state system(s) associated with this trace type.
271 * Suppressing the warning, because the 'throws' will usually happen in
274 * @throws TmfTraceException
275 * If there is a problem during the build
278 @SuppressWarnings("unused")
279 protected void buildStateSystem() throws TmfTraceException
{
281 * Nothing is done in the base implementation, please specify
282 * how/if to register a new state system in derived classes.
291 public synchronized void dispose() {
292 /* Clean up the index if applicable */
293 if (getIndexer() != null) {
294 getIndexer().dispose();
297 /* Clean up the statistics */
298 if (fStatistics
!= null) {
299 fStatistics
.dispose();
302 /* Clean up the state systems */
303 for (ITmfStateSystem ss
: fStateSystems
.values()) {
310 // ------------------------------------------------------------------------
311 // ITmfTrace - Basic getters
312 // ------------------------------------------------------------------------
318 public ITmfTrace
[] getTraces() {
319 return new ITmfTrace
[] { this };
323 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
326 public Class
<ITmfEvent
> getEventType() {
327 return (Class
<ITmfEvent
>) super.getType();
331 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
334 public IResource
getResource() {
339 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
342 public String
getPath() {
347 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
350 public int getCacheSize() {
355 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
358 public long getStreamingInterval() {
359 return fStreamingInterval
;
363 * @return the trace indexer
365 protected ITmfTraceIndexer
getIndexer() {
370 * @return the trace parser
372 protected ITmfEventParser
getParser() {
380 public ITmfStatistics
getStatistics() {
388 public final Map
<String
, ITmfStateSystem
> getStateSystems() {
389 return Collections
.unmodifiableMap(fStateSystems
);
396 public final void registerStateSystem(String id
, ITmfStateSystem ss
) {
397 fStateSystems
.put(id
, ss
);
400 // ------------------------------------------------------------------------
401 // ITmfTrace - Trace characteristics getters
402 // ------------------------------------------------------------------------
405 public synchronized long getNbEvents() {
413 public TmfTimeRange
getTimeRange() {
414 return new TmfTimeRange(fStartTime
, fEndTime
);
421 public ITmfTimestamp
getStartTime() {
429 public ITmfTimestamp
getEndTime() {
437 public ITmfTimestamp
getCurrentTime() {
445 public TmfTimeRange
getCurrentRange() {
446 return fCurrentRange
;
453 public ITmfTimestamp
getInitialRangeOffset() {
454 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
455 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
458 // ------------------------------------------------------------------------
459 // Convenience setters
460 // ------------------------------------------------------------------------
463 * Set the trace cache size. Must be done at initialization time.
465 * @param cacheSize The trace cache size
467 protected void setCacheSize(final int cacheSize
) {
468 fCacheSize
= cacheSize
;
472 * Set the trace known number of events. This can be quite dynamic
473 * during indexing or for live traces.
475 * @param nbEvents The number of events
477 protected synchronized void setNbEvents(final long nbEvents
) {
478 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
482 * Update the trace events time range
484 * @param range the new time range
487 protected void setTimeRange(final TmfTimeRange range
) {
488 fStartTime
= range
.getStartTime();
489 fEndTime
= range
.getEndTime();
493 * Update the trace chronologically first event timestamp
495 * @param startTime the new first event timestamp
498 protected void setStartTime(final ITmfTimestamp startTime
) {
499 fStartTime
= startTime
;
503 * Update the trace chronologically last event timestamp
505 * @param endTime the new last event timestamp
508 protected void setEndTime(final ITmfTimestamp endTime
) {
513 * Set the polling interval for live traces (default = 0 = no streaming).
515 * @param interval the new trace streaming interval
517 protected void setStreamingInterval(final long interval
) {
518 fStreamingInterval
= (interval
> 0) ? interval
: 0;
522 * Set the trace indexer. Must be done at initialization time.
524 * @param indexer the trace indexer
526 protected void setIndexer(final ITmfTraceIndexer indexer
) {
531 * Set the trace parser. Must be done at initialization time.
533 * @param parser the new trace parser
535 protected void setParser(final ITmfEventParser parser
) {
539 // ------------------------------------------------------------------------
540 // ITmfTrace - SeekEvent operations (returning a trace context)
541 // ------------------------------------------------------------------------
544 public synchronized ITmfContext
seekEvent(final long rank
) {
546 // A rank <= 0 indicates to seek the first event
548 ITmfContext context
= seekEvent((ITmfLocation
) null);
553 // Position the trace at the checkpoint
554 final ITmfContext context
= fIndexer
.seekIndex(rank
);
556 // And locate the requested event context
557 long pos
= context
.getRank();
559 ITmfEvent event
= getNext(context
);
560 while ((event
!= null) && (++pos
< rank
)) {
561 event
= getNext(context
);
571 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
573 // A null timestamp indicates to seek the first event
574 if (timestamp
== null) {
575 ITmfContext context
= seekEvent((ITmfLocation
) null);
580 // Position the trace at the checkpoint
581 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
583 // And locate the requested event context
584 ITmfLocation previousLocation
= context
.getLocation();
585 long previousRank
= context
.getRank();
586 ITmfEvent event
= getNext(context
);
587 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
588 previousLocation
= context
.getLocation();
589 previousRank
= context
.getRank();
590 event
= getNext(context
);
593 context
.setLocation(null);
594 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
597 context
= seekEvent(previousLocation
);
598 context
.setRank(previousRank
);
603 // ------------------------------------------------------------------------
604 // ITmfTrace - Read operations (returning an actual event)
605 // ------------------------------------------------------------------------
608 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
609 // parseEvent() does not update the context
610 final ITmfEvent event
= fParser
.parseEvent(context
);
612 updateAttributes(context
, event
.getTimestamp());
613 context
.setLocation(getCurrentLocation());
614 context
.increaseRank();
621 * Hook for special event processing by the concrete class
622 * (called by TmfTrace.getEvent())
624 * @param event the event
626 protected void processEvent(final ITmfEvent event
) {
631 * Update the trace attributes
633 * @param context the current trace context
634 * @param timestamp the corresponding timestamp
637 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
638 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
639 fStartTime
= timestamp
;
641 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
642 fEndTime
= timestamp
;
644 if (fCurrentRange
== TmfTimeRange
.NULL_RANGE
) {
645 fCurrentTime
= timestamp
;
646 ITmfTimestamp initialOffset
= getInitialRangeOffset();
647 long endValue
= timestamp
.getValue() + initialOffset
.normalize(0, timestamp
.getScale()).getValue();
648 ITmfTimestamp endTimestamp
= new TmfTimestamp(endValue
, timestamp
.getScale());
649 fCurrentRange
= new TmfTimeRange(timestamp
, endTimestamp
);
651 if (context
.hasValidRank()) {
652 long rank
= context
.getRank();
653 if (fNbEvents
<= rank
) {
654 fNbEvents
= rank
+ 1;
656 if (fIndexer
!= null) {
657 fIndexer
.updateIndex(context
, timestamp
);
662 // ------------------------------------------------------------------------
664 // ------------------------------------------------------------------------
670 public synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
671 if (executorIsShutdown()) {
674 if ((request
instanceof ITmfEventRequest
)
675 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
676 && (request
.getIndex() == 0))
678 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
679 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
683 return seekEvent(request
.getIndex());
686 // ------------------------------------------------------------------------
688 // ------------------------------------------------------------------------
691 * Handler for the Trace Opened signal
694 * The incoming signal
698 public void traceOpened(TmfTraceOpenedSignal signal
) {
699 ITmfTrace trace
= null;
700 for (ITmfTrace expTrace
: signal
.getTrace().getTraces()) {
701 if (expTrace
== this) {
708 /* This signal is not for us */
713 * The signal is for this trace, or for an experiment containing
719 } catch (TmfTraceException e
) {
723 /* Refresh the project, so it can pick up new files that got created. */
725 if (fResource
!= null) {
726 fResource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
728 } catch (CoreException e
) {
732 if (signal
.getTrace() == this) {
733 /* Additionally, the signal is directly for this trace or experiment. */
734 if (getNbEvents() == 0) {
738 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
739 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
741 // Broadcast in separate thread to prevent deadlock
745 broadcast(rangeUpdatedsignal
);
753 * Signal handler for the TmfTraceRangeUpdatedSignal signal
755 * @param signal The incoming signal
759 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
760 if (signal
.getTrace() == this) {
761 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
766 * Signal handler for the TmfTimeSynchSignal signal
768 * @param signal The incoming signal
772 public void synchToTime(final TmfTimeSynchSignal signal
) {
773 if (signal
.getCurrentTime().compareTo(fStartTime
) >= 0 && signal
.getCurrentTime().compareTo(fEndTime
) <= 0) {
774 fCurrentTime
= signal
.getCurrentTime();
779 * Signal handler for the TmfRangeSynchSignal signal
781 * @param signal The incoming signal
785 public void synchToRange(final TmfRangeSynchSignal signal
) {
786 if (signal
.getCurrentTime().compareTo(fStartTime
) >= 0 && signal
.getCurrentTime().compareTo(fEndTime
) <= 0) {
787 fCurrentTime
= signal
.getCurrentTime();
789 if (signal
.getCurrentRange().getIntersection(getTimeRange()) != null) {
790 fCurrentRange
= signal
.getCurrentRange().getIntersection(getTimeRange());
794 // ------------------------------------------------------------------------
796 // ------------------------------------------------------------------------
799 @SuppressWarnings("nls")
800 public synchronized String
toString() {
801 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
802 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
803 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";