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
.TmfSignalHandler
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.TmfStateStatistics
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
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 IStatus 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
;
103 * The collection of state systems that are registered with this trace. Each
104 * sub-class can decide to add its (one or many) state system to this map
105 * during their {@link #buildStateSystem()}.
109 protected final Map
<String
, ITmfStateSystem
> fStateSystems
=
110 new LinkedHashMap
<String
, ITmfStateSystem
>();
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
117 * The default, parameterless, constructor
127 * The resource associated to the trace
129 * The type of events that will be read from this trace
131 * The path to the trace on the filesystem
133 * The trace cache size. Pass '-1' to use the default specified
134 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
136 * The trace streaming interval. You can use '0' for post-mortem
139 * The trace indexer. You can pass 'null' to use a default
140 * checkpoint indexer.
142 * The trace event parser. Use 'null' if (and only if) the trace
143 * object itself is also the ITmfEventParser to be used.
144 * @throws TmfTraceException
145 * If something failed during the opening
147 protected TmfTrace(final IResource resource
,
148 final Class
<?
extends ITmfEvent
> type
,
152 final ITmfTraceIndexer indexer
,
153 final ITmfEventParser parser
)
154 throws TmfTraceException
{
156 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
157 fStreamingInterval
= interval
;
158 fIndexer
= (indexer
!= null) ? indexer
: new TmfCheckpointIndexer(this, fCacheSize
);
160 initialize(resource
, path
, type
);
166 * @param trace the original trace
167 * @throws TmfTraceException Should not happen usually
169 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
172 throw new IllegalArgumentException();
174 fCacheSize
= trace
.getCacheSize();
175 fStreamingInterval
= trace
.getStreamingInterval();
176 fIndexer
= new TmfCheckpointIndexer(this);
177 fParser
= trace
.fParser
;
178 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
181 // ------------------------------------------------------------------------
182 // ITmfTrace - Initializers
183 // ------------------------------------------------------------------------
186 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
187 fIndexer
= new TmfCheckpointIndexer(this, fCacheSize
);
188 initialize(resource
, path
, type
);
192 * Initialize the trace common attributes and the base component.
194 * @param resource the Eclipse resource (trace)
195 * @param path the trace path
196 * @param type the trace event type
198 * @throws TmfTraceException If something failed during the initialization
200 protected void initialize(final IResource resource
,
202 final Class
<?
extends ITmfEvent
> type
)
203 throws TmfTraceException
{
205 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
208 fResource
= resource
;
209 String traceName
= (resource
!= null) ? resource
.getName() : null;
210 // If no resource was provided, extract the display name the trace path
211 if (traceName
== null) {
212 final int sep
= path
.lastIndexOf(IPath
.SEPARATOR
);
213 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
215 if (fParser
== null) {
216 if (this instanceof ITmfEventParser
) {
217 fParser
= (ITmfEventParser
) this;
219 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
222 super.init(traceName
, type
);
223 // register as VIP after super.init() because TmfComponent registers to signal manager there
224 TmfSignalManager
.registerVIP(this);
228 * Indicates if the path points to an existing file/directory
230 * @param path the path to test
231 * @return true if the file/directory exists
233 protected boolean fileExists(final String path
) {
234 final File file
= new File(path
);
235 return file
.exists();
242 public void indexTrace(boolean waitForCompletion
) {
243 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
247 * The default implementation of TmfTrace uses a TmfStatistics back-end.
248 * Override this if you want to specify another type (or none at all).
250 * @throws TmfTraceException
251 * If there was a problem setting up the statistics
254 protected void buildStatistics() throws TmfTraceException
{
256 * Initialize the statistics provider, but only if a Resource has been
257 * set (so we don't build it for experiments, for unit tests, etc.)
259 fStatistics
= (fResource
== null ?
null : new TmfStateStatistics(this) );
263 * Build the state system(s) associated with this trace type.
265 * Suppressing the warning, because the 'throws' will usually happen in
268 * @throws TmfTraceException
269 * If there is a problem during the build
272 @SuppressWarnings("unused")
273 protected void buildStateSystem() throws TmfTraceException
{
275 * Nothing is done in the base implementation, please specify
276 * how/if to register a new state system in derived classes.
285 public synchronized void dispose() {
286 /* Clean up the index if applicable */
287 if (getIndexer() != null) {
288 getIndexer().dispose();
291 /* Clean up the statistics */
292 if (fStatistics
!= null) {
293 fStatistics
.dispose();
296 /* Clean up the state systems */
297 for (ITmfStateSystem ss
: fStateSystems
.values()) {
304 // ------------------------------------------------------------------------
305 // ITmfTrace - Basic getters
306 // ------------------------------------------------------------------------
309 public Class
<ITmfEvent
> getEventType() {
310 return (Class
<ITmfEvent
>) super.getType();
314 public IResource
getResource() {
319 public String
getPath() {
324 public int getCacheSize() {
329 public long getStreamingInterval() {
330 return fStreamingInterval
;
334 * @return the trace indexer
336 protected ITmfTraceIndexer
getIndexer() {
341 * @return the trace parser
343 protected ITmfEventParser
getParser() {
351 public ITmfStatistics
getStatistics() {
359 public final Map
<String
, ITmfStateSystem
> getStateSystems() {
360 return Collections
.unmodifiableMap(fStateSystems
);
367 public final void registerStateSystem(String id
, ITmfStateSystem ss
) {
368 fStateSystems
.put(id
, ss
);
371 // ------------------------------------------------------------------------
372 // ITmfTrace - Trace characteristics getters
373 // ------------------------------------------------------------------------
376 public synchronized long getNbEvents() {
384 public TmfTimeRange
getTimeRange() {
385 return new TmfTimeRange(fStartTime
, fEndTime
);
392 public ITmfTimestamp
getStartTime() {
400 public ITmfTimestamp
getEndTime() {
408 public ITmfTimestamp
getInitialRangeOffset() {
409 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
410 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
413 // ------------------------------------------------------------------------
414 // Convenience setters
415 // ------------------------------------------------------------------------
418 * Set the trace cache size. Must be done at initialization time.
420 * @param cacheSize The trace cache size
422 protected void setCacheSize(final int cacheSize
) {
423 fCacheSize
= cacheSize
;
427 * Set the trace known number of events. This can be quite dynamic
428 * during indexing or for live traces.
430 * @param nbEvents The number of events
432 protected synchronized void setNbEvents(final long nbEvents
) {
433 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
437 * Update the trace events time range
439 * @param range the new time range
442 protected void setTimeRange(final TmfTimeRange range
) {
443 fStartTime
= range
.getStartTime();
444 fEndTime
= range
.getEndTime();
448 * Update the trace chronologically first event timestamp
450 * @param startTime the new first event timestamp
453 protected void setStartTime(final ITmfTimestamp startTime
) {
454 fStartTime
= startTime
;
458 * Update the trace chronologically last event timestamp
460 * @param endTime the new last event timestamp
463 protected void setEndTime(final ITmfTimestamp endTime
) {
468 * Set the polling interval for live traces (default = 0 = no streaming).
470 * @param interval the new trace streaming interval
472 protected void setStreamingInterval(final long interval
) {
473 fStreamingInterval
= (interval
> 0) ? interval
: 0;
477 * Set the trace indexer. Must be done at initialization time.
479 * @param indexer the trace indexer
481 protected void setIndexer(final ITmfTraceIndexer indexer
) {
486 * Set the trace parser. Must be done at initialization time.
488 * @param parser the new trace parser
490 protected void setParser(final ITmfEventParser parser
) {
494 // ------------------------------------------------------------------------
495 // ITmfTrace - SeekEvent operations (returning a trace context)
496 // ------------------------------------------------------------------------
499 public synchronized ITmfContext
seekEvent(final long rank
) {
501 // A rank <= 0 indicates to seek the first event
503 ITmfContext context
= seekEvent((ITmfLocation
) null);
508 // Position the trace at the checkpoint
509 final ITmfContext context
= fIndexer
.seekIndex(rank
);
511 // And locate the requested event context
512 long pos
= context
.getRank();
514 ITmfEvent event
= getNext(context
);
515 while ((event
!= null) && (++pos
< rank
)) {
516 event
= getNext(context
);
526 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
528 // A null timestamp indicates to seek the first event
529 if (timestamp
== null) {
530 ITmfContext context
= seekEvent((ITmfLocation
) null);
535 // Position the trace at the checkpoint
536 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
538 // And locate the requested event context
539 ITmfLocation previousLocation
= context
.getLocation();
540 long previousRank
= context
.getRank();
541 ITmfEvent event
= getNext(context
);
542 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
543 previousLocation
= context
.getLocation();
544 previousRank
= context
.getRank();
545 event
= getNext(context
);
548 context
.setLocation(null);
549 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
552 context
= seekEvent(previousLocation
);
553 context
.setRank(previousRank
);
558 // ------------------------------------------------------------------------
559 // ITmfTrace - Read operations (returning an actual event)
560 // ------------------------------------------------------------------------
563 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
564 // parseEvent() does not update the context
565 final ITmfEvent event
= fParser
.parseEvent(context
);
567 updateAttributes(context
, event
.getTimestamp());
568 context
.setLocation(getCurrentLocation());
569 context
.increaseRank();
576 * Hook for special event processing by the concrete class
577 * (called by TmfTrace.getEvent())
579 * @param event the event
581 protected void processEvent(final ITmfEvent event
) {
586 * Update the trace attributes
588 * @param context the current trace context
589 * @param timestamp the corresponding timestamp
592 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
593 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
594 fStartTime
= timestamp
;
596 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
597 fEndTime
= timestamp
;
599 if (context
.hasValidRank()) {
600 long rank
= context
.getRank();
601 if (fNbEvents
<= rank
) {
602 fNbEvents
= rank
+ 1;
604 if (fIndexer
!= null) {
605 fIndexer
.updateIndex(context
, timestamp
);
610 // ------------------------------------------------------------------------
612 // ------------------------------------------------------------------------
618 public synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
619 if (executorIsShutdown()) {
622 if ((request
instanceof ITmfEventRequest
)
623 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
624 && (request
.getIndex() == 0))
626 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
627 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
631 return seekEvent(request
.getIndex());
634 // ------------------------------------------------------------------------
636 // ------------------------------------------------------------------------
639 * Handler for the Trace Opened signal
642 * The incoming signal
646 public void traceOpened(TmfTraceOpenedSignal signal
) {
647 boolean signalIsForUs
= false;
648 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
650 signalIsForUs
= true;
655 if (!signalIsForUs
) {
660 * The signal is either for this trace, or for an experiment containing
666 } catch (TmfTraceException e
) {
670 /* Refresh the project, so it can pick up new files that got created. */
672 if (fResource
!= null) {
673 fResource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
675 } catch (CoreException e
) {
679 if (signal
.getTrace() == this) {
680 /* Additionally, the signal is directly for this trace or experiment. */
681 if (getNbEvents() == 0) {
685 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
686 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
688 // Broadcast in separate thread to prevent deadlock
692 broadcast(rangeUpdatedsignal
);
700 * Signal handler for the TmfTraceRangeUpdatedSignal signal
702 * @param signal The incoming signal
706 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
707 if (signal
.getTrace() == this) {
708 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
712 // ------------------------------------------------------------------------
714 // ------------------------------------------------------------------------
717 @SuppressWarnings("nls")
718 public synchronized String
toString() {
719 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
720 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
721 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";