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
;
18 import org
.eclipse
.core
.resources
.IResource
;
19 import org
.eclipse
.core
.runtime
.IPath
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.ITmfStatistics
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statistics
.TmfStatistics
;
33 * Abstract implementation of ITmfTrace.
35 * Since the concept of 'location' is trace specific, the concrete classes have
36 * to provide the related methods, namely:
38 * <li> public ITmfLocation<?> getCurrentLocation()
39 * <li> public double getLocationRatio(ITmfLocation<?> location)
40 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
41 * <li> public ITmfContext seekEvent(double ratio)
42 * <li> public boolean validate(IProject project, String path)
44 * A concrete trace must provide its corresponding parser. A common way to
45 * accomplish this is by making the concrete class extend TmfTrace and
46 * implement ITmfEventParser.
48 * The concrete class can either specify its own indexer or use the provided
49 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
50 * used as checkpoint interval.
53 * @author Francois Chouinard
56 * @see ITmfTraceIndexer
57 * @see ITmfEventParser
59 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
{
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 // The resource used for persistent properties for this trace
66 private IResource fResource
;
71 // The trace cache page size
72 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
74 // The number of events collected (so far)
75 private long fNbEvents
= 0;
77 // The time span of the event stream
78 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_CRUNCH
;
79 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
81 // The trace streaming interval (0 = no streaming)
82 private long fStreamingInterval
= 0;
85 private ITmfTraceIndexer fIndexer
;
88 private ITmfEventParser fParser
;
90 // The trace's statistics
91 private ITmfStatistics fStatistics
;
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * The default, parameterless, constructor
105 * The standard constructor (non-live trace). Applicable when the trace
106 * implements its own parser and if at checkpoint-based index is OK.
108 * @param resource the resource associated to the trace
109 * @param type the trace event type
110 * @param path the trace path
111 * @param cacheSize the trace cache size
112 * @throws TmfTraceException
114 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
) throws TmfTraceException
{
115 this(resource
, type
, path
, cacheSize
, 0);
119 * The standard constructor (live trace). Applicable when the trace
120 * implements its own parser and if at checkpoint-based index is OK.
122 * @param resource the resource associated to the trace
123 * @param type the trace event type
124 * @param path the trace path
125 * @param cacheSize the trace cache size
126 * @param interval the trace streaming interval
127 * @throws TmfTraceException
129 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
130 this(resource
, type
, path
, cacheSize
, interval
, null);
134 * The 'non-default indexer' constructor. Allows to provide a trace
137 * @param resource the resource associated to the trace
138 * @param type the trace event type
139 * @param path the trace path
140 * @param cacheSize the trace cache size
141 * @param indexer the trace indexer
142 * @throws TmfTraceException
144 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
145 final long interval
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
146 this(resource
, type
, path
, cacheSize
, interval
, indexer
, null);
150 * The full constructor where trace specific indexer/parser are provided.
152 * @param resource the resource associated to the trace
153 * @param type the trace event type
154 * @param path the trace path
155 * @param cacheSize the trace cache size
156 * @param indexer the trace indexer
157 * @param parser the trace event parser
158 * @throws TmfTraceException
160 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
161 final long interval
, final ITmfTraceIndexer indexer
, final ITmfEventParser parser
) 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
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
);
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();
248 * @param waitForCompletion index synchronously (true) or not (false)
250 protected void indexTrace(boolean waitForCompletion
) {
251 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
255 * The default implementation of TmfTrace uses a TmfStatistics backend.
256 * Override this if you want to specify another type (or none at all).
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 TmfStatistics(this) );
272 public synchronized void dispose() {
273 // Clean up the index if applicable
274 if (getIndexer() != null) {
275 getIndexer().dispose();
280 // ------------------------------------------------------------------------
281 // ITmfTrace - Basic getters
282 // ------------------------------------------------------------------------
285 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
288 public Class
<ITmfEvent
> getEventType() {
289 return (Class
<ITmfEvent
>) super.getType();
293 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
296 public IResource
getResource() {
301 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
304 public String
getPath() {
309 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
312 public int getCacheSize() {
317 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
320 public long getStreamingInterval() {
321 return fStreamingInterval
;
325 * @return the trace indexer
327 protected ITmfTraceIndexer
getIndexer() {
332 * @return the trace parser
334 protected ITmfEventParser
getParser() {
342 public ITmfStatistics
getStatistics() {
350 public ITmfStateSystem
getStateSystem() {
352 * By default, no state system is used. Sub-classes can specify their
358 // ------------------------------------------------------------------------
359 // ITmfTrace - Trace characteristics getters
360 // ------------------------------------------------------------------------
363 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
366 public synchronized long getNbEvents() {
371 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
374 public TmfTimeRange
getTimeRange() {
375 return new TmfTimeRange(fStartTime
, fEndTime
);
379 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
382 public ITmfTimestamp
getStartTime() {
383 return fStartTime
.clone();
387 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
390 public ITmfTimestamp
getEndTime() {
391 return fEndTime
.clone();
394 // ------------------------------------------------------------------------
395 // Convenience setters/getters
396 // ------------------------------------------------------------------------
399 * Set the trace cache size. Must be done at initialization time.
401 * @param cacheSize The trace cache size
403 protected void setCacheSize(final int cacheSize
) {
404 fCacheSize
= cacheSize
;
408 * Set the trace known number of events. This can be quite dynamic
409 * during indexing or for live traces.
411 * @param nbEvents The number of events
413 protected synchronized void setNbEvents(final long nbEvents
) {
414 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
418 * Update the trace events time range
420 * @param range the new time range
422 protected void setTimeRange(final TmfTimeRange range
) {
423 fStartTime
= range
.getStartTime().clone();
424 fEndTime
= range
.getEndTime().clone();
428 * Update the trace chronologically first event timestamp
430 * @param startTime the new first event timestamp
432 protected void setStartTime(final ITmfTimestamp startTime
) {
433 fStartTime
= startTime
.clone();
437 * Update the trace chronologically last event timestamp
439 * @param endTime the new last event timestamp
441 protected void setEndTime(final ITmfTimestamp endTime
) {
442 fEndTime
= endTime
.clone();
446 * Set the polling interval for live traces (default = 0 = no streaming).
448 * @param interval the new trace streaming interval
450 protected void setStreamingInterval(final long interval
) {
451 fStreamingInterval
= (interval
> 0) ? interval
: 0;
455 * Set the trace indexer. Must be done at initialization time.
457 * @param indexer the trace indexer
459 protected void setIndexer(final ITmfTraceIndexer indexer
) {
464 * Set the trace parser. Must be done at initialization time.
466 * @param parser the new trace parser
468 protected void setParser(final ITmfEventParser parser
) {
472 // ------------------------------------------------------------------------
473 // ITmfTrace - SeekEvent operations (returning a trace context)
474 // ------------------------------------------------------------------------
477 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
480 public synchronized ITmfContext
seekEvent(final long rank
) {
482 // A rank <= 0 indicates to seek the first event
484 ITmfContext context
= seekEvent((ITmfLocation
) null);
489 // Position the trace at the checkpoint
490 final ITmfContext context
= fIndexer
.seekIndex(rank
);
492 // And locate the requested event context
493 long pos
= context
.getRank();
495 ITmfEvent event
= getNext(context
);
496 while ((event
!= null) && (++pos
< rank
)) {
497 event
= getNext(context
);
504 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
507 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
509 // A null timestamp indicates to seek the first event
510 if (timestamp
== null) {
511 ITmfContext context
= seekEvent((ITmfLocation
) null);
516 // Position the trace at the checkpoint
517 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
519 // And locate the requested event context
520 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
521 ITmfEvent event
= getNext(nextEventContext
);
522 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
523 context
= nextEventContext
.clone();
524 event
= getNext(nextEventContext
);
527 context
.setLocation(null);
528 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
533 // ------------------------------------------------------------------------
534 // ITmfTrace - Read operations (returning an actual event)
535 // ------------------------------------------------------------------------
538 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
541 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
542 // parseEvent() does not update the context
543 final ITmfEvent event
= fParser
.parseEvent(context
);
545 updateAttributes(context
, event
.getTimestamp());
546 context
.setLocation(getCurrentLocation());
547 context
.increaseRank();
554 * Hook for special event processing by the concrete class
555 * (called by TmfTrace.getEvent())
557 * @param event the event
559 protected void processEvent(final ITmfEvent event
) {
564 * Update the trace attributes
566 * @param context the current trace context
567 * @param timestamp the corresponding timestamp
569 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
570 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
571 fStartTime
= timestamp
.clone();
573 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
574 fEndTime
= timestamp
.clone();
576 if (context
.hasValidRank()) {
577 long rank
= context
.getRank();
578 if (fNbEvents
<= rank
) {
579 fNbEvents
= rank
+ 1;
581 if (fIndexer
!= null) {
582 fIndexer
.updateIndex(context
, timestamp
);
587 // ------------------------------------------------------------------------
589 // ------------------------------------------------------------------------
592 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
595 protected ITmfContext
armRequest(final ITmfDataRequest request
) {
596 if ((request
instanceof ITmfEventRequest
)
597 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
598 && (request
.getIndex() == 0))
600 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
601 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
605 return seekEvent(request
.getIndex());
608 // ------------------------------------------------------------------------
610 // ------------------------------------------------------------------------
613 * @see java.lang.Object#toString()
616 @SuppressWarnings("nls")
617 public synchronized String
toString() {
618 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
619 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
620 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";