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
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.MultiStatus
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
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
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
49 * Abstract implementation of ITmfTrace.
51 * Since the concept of 'location' is trace specific, the concrete classes have
52 * to provide the related methods, namely:
54 * <li> public ITmfLocation<?> getCurrentLocation()
55 * <li> public double getLocationRatio(ITmfLocation<?> location)
56 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
57 * <li> public ITmfContext seekEvent(double ratio)
58 * <li> public IStatus validate(IProject project, String path)
60 * A concrete trace must provide its corresponding parser. A common way to
61 * accomplish this is by making the concrete class extend TmfTrace and
62 * implement ITmfEventParser.
64 * The concrete class can either specify its own indexer or use the provided
65 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
66 * used as checkpoint interval.
69 * @author Francois Chouinard
72 * @see ITmfTraceIndexer
73 * @see ITmfEventParser
75 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
{
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 // The resource used for persistent properties for this trace
82 private IResource fResource
;
87 // The trace cache page size
88 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
90 // The number of events collected (so far)
91 private long fNbEvents
= 0;
93 // The time span of the event stream
94 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_BANG
;
95 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
97 // The trace streaming interval (0 = no streaming)
98 private long fStreamingInterval
= 0;
101 private ITmfTraceIndexer fIndexer
;
104 private ITmfEventParser fParser
;
106 // The trace's statistics
107 private ITmfStatistics fStatistics
;
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 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
194 fIndexer
= new TmfCheckpointIndexer(this, fCacheSize
);
195 initialize(resource
, path
, type
);
199 * Initialize the trace common attributes and the base component.
201 * @param resource the Eclipse resource (trace)
202 * @param path the trace path
203 * @param type the trace event type
205 * @throws TmfTraceException If something failed during the initialization
207 protected void initialize(final IResource resource
,
209 final Class
<?
extends ITmfEvent
> type
)
210 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
);
230 // register as VIP after super.init() because TmfComponent registers to signal manager there
231 TmfSignalManager
.registerVIP(this);
235 * Indicates if the path points to an existing file/directory
237 * @param path the path to test
238 * @return true if the file/directory exists
240 protected boolean fileExists(final String path
) {
241 final File file
= new File(path
);
242 return file
.exists();
249 public void indexTrace(boolean waitForCompletion
) {
250 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
254 * The default implementation of TmfTrace uses a TmfStatistics back-end.
255 * Override this if you want to specify another type (or none at all).
257 * @return An IStatus indicating if the statistics could be built
258 * successfully or not.
261 protected IStatus
buildStatistics() {
263 * Initialize the statistics provider, but only if a Resource has been
264 * set (so we don't build it for experiments, for unit tests, etc.)
267 fStatistics
= (fResource
== null ?
null : new TmfStateStatistics(this) );
268 } catch (TmfTraceException e
) {
269 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, e
.getMessage(), e
);
271 return Status
.OK_STATUS
;
275 * Build the state system(s) associated with this trace type.
277 * Suppressing the warning, because the 'throws' will usually happen in
280 * @return An IStatus indicating if the state system could be build
281 * successfully or not.
284 protected IStatus
buildStateSystem() {
286 * Nothing is done in the base implementation, please specify
287 * how/if to register a new state system in derived classes.
289 return Status
.OK_STATUS
;
296 public synchronized void dispose() {
297 /* Clean up the index if applicable */
298 if (getIndexer() != null) {
299 getIndexer().dispose();
302 /* Clean up the statistics */
303 if (fStatistics
!= null) {
304 fStatistics
.dispose();
307 /* Clean up the state systems */
308 for (ITmfStateSystem ss
: fStateSystems
.values()) {
315 // ------------------------------------------------------------------------
316 // ITmfTrace - Basic getters
317 // ------------------------------------------------------------------------
320 public Class
<ITmfEvent
> getEventType() {
321 return (Class
<ITmfEvent
>) super.getType();
325 public IResource
getResource() {
330 public String
getPath() {
335 public int getCacheSize() {
340 public long getStreamingInterval() {
341 return fStreamingInterval
;
345 * @return the trace indexer
348 protected ITmfTraceIndexer
getIndexer() {
353 * @return the trace parser
355 protected ITmfEventParser
getParser() {
363 public ITmfStatistics
getStatistics() {
371 public final Map
<String
, ITmfStateSystem
> getStateSystems() {
372 return Collections
.unmodifiableMap(fStateSystems
);
379 public final void registerStateSystem(String id
, ITmfStateSystem ss
) {
380 fStateSystems
.put(id
, ss
);
383 // ------------------------------------------------------------------------
384 // ITmfTrace - Trace characteristics getters
385 // ------------------------------------------------------------------------
388 public synchronized long getNbEvents() {
396 public TmfTimeRange
getTimeRange() {
397 return new TmfTimeRange(fStartTime
, fEndTime
);
404 public ITmfTimestamp
getStartTime() {
412 public ITmfTimestamp
getEndTime() {
420 public ITmfTimestamp
getInitialRangeOffset() {
421 final long DEFAULT_INITIAL_OFFSET_VALUE
= (1L * 100 * 1000 * 1000); // .1sec
422 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE
, ITmfTimestamp
.NANOSECOND_SCALE
);
429 public String
getHostId() {
430 return this.getName();
433 // ------------------------------------------------------------------------
434 // Convenience setters
435 // ------------------------------------------------------------------------
438 * Set the trace cache size. Must be done at initialization time.
440 * @param cacheSize The trace cache size
442 protected void setCacheSize(final int cacheSize
) {
443 fCacheSize
= cacheSize
;
447 * Set the trace known number of events. This can be quite dynamic
448 * during indexing or for live traces.
450 * @param nbEvents The number of events
452 protected synchronized void setNbEvents(final long nbEvents
) {
453 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
457 * Update the trace events time range
459 * @param range the new time range
462 protected void setTimeRange(final TmfTimeRange range
) {
463 fStartTime
= range
.getStartTime();
464 fEndTime
= range
.getEndTime();
468 * Update the trace chronologically first event timestamp
470 * @param startTime the new first event timestamp
473 protected void setStartTime(final ITmfTimestamp startTime
) {
474 fStartTime
= startTime
;
478 * Update the trace chronologically last event timestamp
480 * @param endTime the new last event timestamp
483 protected void setEndTime(final ITmfTimestamp endTime
) {
488 * Set the polling interval for live traces (default = 0 = no streaming).
490 * @param interval the new trace streaming interval
492 protected void setStreamingInterval(final long interval
) {
493 fStreamingInterval
= (interval
> 0) ? interval
: 0;
497 * Set the trace indexer. Must be done at initialization time.
499 * @param indexer the trace indexer
502 protected void setIndexer(final ITmfTraceIndexer indexer
) {
507 * Set the trace parser. Must be done at initialization time.
509 * @param parser the new trace parser
511 protected void setParser(final ITmfEventParser parser
) {
515 // ------------------------------------------------------------------------
516 // ITmfTrace - SeekEvent operations (returning a trace context)
517 // ------------------------------------------------------------------------
520 public synchronized ITmfContext
seekEvent(final long rank
) {
522 // A rank <= 0 indicates to seek the first event
524 ITmfContext context
= seekEvent((ITmfLocation
) null);
529 // Position the trace at the checkpoint
530 final ITmfContext context
= fIndexer
.seekIndex(rank
);
532 // And locate the requested event context
533 long pos
= context
.getRank();
535 ITmfEvent event
= getNext(context
);
536 while ((event
!= null) && (++pos
< rank
)) {
537 event
= getNext(context
);
547 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
549 // A null timestamp indicates to seek the first event
550 if (timestamp
== null) {
551 ITmfContext context
= seekEvent((ITmfLocation
) null);
556 // Position the trace at the checkpoint
557 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
559 // And locate the requested event context
560 ITmfLocation previousLocation
= context
.getLocation();
561 long previousRank
= context
.getRank();
562 ITmfEvent event
= getNext(context
);
563 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
564 previousLocation
= context
.getLocation();
565 previousRank
= context
.getRank();
566 event
= getNext(context
);
569 context
.setLocation(null);
570 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
573 context
= seekEvent(previousLocation
);
574 context
.setRank(previousRank
);
579 // ------------------------------------------------------------------------
580 // ITmfTrace - Read operations (returning an actual event)
581 // ------------------------------------------------------------------------
584 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
585 // parseEvent() does not update the context
586 final ITmfEvent event
= fParser
.parseEvent(context
);
588 updateAttributes(context
, event
.getTimestamp());
589 context
.setLocation(getCurrentLocation());
590 context
.increaseRank();
597 * Hook for special event processing by the concrete class
598 * (called by TmfTrace.getEvent())
600 * @param event the event
602 protected void processEvent(final ITmfEvent event
) {
607 * Update the trace attributes
609 * @param context the current trace context
610 * @param timestamp the corresponding timestamp
613 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
614 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
615 fStartTime
= timestamp
;
617 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
618 fEndTime
= timestamp
;
620 if (context
.hasValidRank()) {
621 long rank
= context
.getRank();
622 if (fNbEvents
<= rank
) {
623 fNbEvents
= rank
+ 1;
625 if (fIndexer
!= null) {
626 fIndexer
.updateIndex(context
, timestamp
);
631 // ------------------------------------------------------------------------
633 // ------------------------------------------------------------------------
639 public synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
640 if (executorIsShutdown()) {
643 if ((request
instanceof ITmfEventRequest
)
644 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
645 && (request
.getIndex() == 0))
647 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
648 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
652 return seekEvent(request
.getIndex());
655 // ------------------------------------------------------------------------
657 // ------------------------------------------------------------------------
660 * Handler for the Trace Opened signal
663 * The incoming signal
667 public void traceOpened(TmfTraceOpenedSignal signal
) {
668 boolean signalIsForUs
= false;
669 for (ITmfTrace trace
: TmfTraceManager
.getTraceSet(signal
.getTrace())) {
671 signalIsForUs
= true;
676 if (!signalIsForUs
) {
681 * The signal is either for this trace, or for an experiment containing
684 MultiStatus status
= new MultiStatus(Activator
.PLUGIN_ID
, IStatus
.OK
, null, null);
685 status
.add(buildStatistics());
686 status
.add(buildStateSystem());
687 if (!status
.isOK()) {
688 Activator
.log(status
);
691 /* Refresh the project, so it can pick up new files that got created. */
693 if (fResource
!= null) {
694 fResource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
696 } catch (CoreException e
) {
700 if (signal
.getTrace() == this) {
701 /* Additionally, the signal is directly for this trace. */
702 if (getNbEvents() == 0) {
706 /* For a streaming trace, the range updated signal should be sent
707 * by the subclass when a new safe time is determined.
709 if (getStreamingInterval() > 0) {
713 final TmfTimeRange timeRange
= new TmfTimeRange(getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
714 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
716 // Broadcast in separate thread to prevent deadlock
720 broadcast(rangeUpdatedsignal
);
728 * Signal handler for the TmfTraceRangeUpdatedSignal signal
730 * @param signal The incoming signal
734 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
735 if (signal
.getTrace() == this) {
736 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);
740 // ------------------------------------------------------------------------
742 // ------------------------------------------------------------------------
745 @SuppressWarnings("nls")
746 public synchronized String
toString() {
747 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
748 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
749 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";