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
.TmfStateStatistics
;
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 TmfStateStatistics(this) );
272 public synchronized void dispose() {
273 /* Clean up the index if applicable */
274 if (getIndexer() != null) {
275 getIndexer().dispose();
278 /* Clean up the statistics */
279 if (fStatistics
!= null) {
280 fStatistics
.dispose();
285 // ------------------------------------------------------------------------
286 // ITmfTrace - Basic getters
287 // ------------------------------------------------------------------------
290 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
293 public Class
<ITmfEvent
> getEventType() {
294 return (Class
<ITmfEvent
>) super.getType();
298 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
301 public IResource
getResource() {
306 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
309 public String
getPath() {
314 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
317 public int getCacheSize() {
322 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
325 public long getStreamingInterval() {
326 return fStreamingInterval
;
330 * @return the trace indexer
332 protected ITmfTraceIndexer
getIndexer() {
337 * @return the trace parser
339 protected ITmfEventParser
getParser() {
347 public ITmfStatistics
getStatistics() {
355 public ITmfStateSystem
getStateSystem() {
357 * By default, no state system is used. Sub-classes can specify their
363 // ------------------------------------------------------------------------
364 // ITmfTrace - Trace characteristics getters
365 // ------------------------------------------------------------------------
368 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
371 public synchronized long getNbEvents() {
376 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
379 public TmfTimeRange
getTimeRange() {
380 return new TmfTimeRange(fStartTime
, fEndTime
);
384 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
387 public ITmfTimestamp
getStartTime() {
392 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
395 public ITmfTimestamp
getEndTime() {
399 // ------------------------------------------------------------------------
400 // Convenience setters/getters
401 // ------------------------------------------------------------------------
404 * Set the trace cache size. Must be done at initialization time.
406 * @param cacheSize The trace cache size
408 protected void setCacheSize(final int cacheSize
) {
409 fCacheSize
= cacheSize
;
413 * Set the trace known number of events. This can be quite dynamic
414 * during indexing or for live traces.
416 * @param nbEvents The number of events
418 protected synchronized void setNbEvents(final long nbEvents
) {
419 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
423 * Update the trace events time range
425 * @param range the new time range
427 protected void setTimeRange(final TmfTimeRange range
) {
428 fStartTime
= range
.getStartTime();
429 fEndTime
= range
.getEndTime();
433 * Update the trace chronologically first event timestamp
435 * @param startTime the new first event timestamp
437 protected void setStartTime(final ITmfTimestamp startTime
) {
438 fStartTime
= startTime
;
442 * Update the trace chronologically last event timestamp
444 * @param endTime the new last event timestamp
446 protected void setEndTime(final ITmfTimestamp endTime
) {
451 * Set the polling interval for live traces (default = 0 = no streaming).
453 * @param interval the new trace streaming interval
455 protected void setStreamingInterval(final long interval
) {
456 fStreamingInterval
= (interval
> 0) ? interval
: 0;
460 * Set the trace indexer. Must be done at initialization time.
462 * @param indexer the trace indexer
464 protected void setIndexer(final ITmfTraceIndexer indexer
) {
469 * Set the trace parser. Must be done at initialization time.
471 * @param parser the new trace parser
473 protected void setParser(final ITmfEventParser parser
) {
477 // ------------------------------------------------------------------------
478 // ITmfTrace - SeekEvent operations (returning a trace context)
479 // ------------------------------------------------------------------------
482 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
485 public synchronized ITmfContext
seekEvent(final long rank
) {
487 // A rank <= 0 indicates to seek the first event
489 ITmfContext context
= seekEvent((ITmfLocation
) null);
494 // Position the trace at the checkpoint
495 final ITmfContext context
= fIndexer
.seekIndex(rank
);
497 // And locate the requested event context
498 long pos
= context
.getRank();
500 ITmfEvent event
= getNext(context
);
501 while ((event
!= null) && (++pos
< rank
)) {
502 event
= getNext(context
);
509 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
512 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
514 // A null timestamp indicates to seek the first event
515 if (timestamp
== null) {
516 ITmfContext context
= seekEvent((ITmfLocation
) null);
521 // Position the trace at the checkpoint
522 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
524 // And locate the requested event context
525 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
526 ITmfEvent event
= getNext(nextEventContext
);
527 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
529 context
= nextEventContext
.clone();
530 event
= getNext(nextEventContext
);
532 nextEventContext
.dispose();
534 context
.setLocation(null);
535 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
540 // ------------------------------------------------------------------------
541 // ITmfTrace - Read operations (returning an actual event)
542 // ------------------------------------------------------------------------
545 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
548 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
549 // parseEvent() does not update the context
550 final ITmfEvent event
= fParser
.parseEvent(context
);
552 updateAttributes(context
, event
.getTimestamp());
553 context
.setLocation(getCurrentLocation());
554 context
.increaseRank();
561 * Hook for special event processing by the concrete class
562 * (called by TmfTrace.getEvent())
564 * @param event the event
566 protected void processEvent(final ITmfEvent event
) {
571 * Update the trace attributes
573 * @param context the current trace context
574 * @param timestamp the corresponding timestamp
576 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
577 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
578 fStartTime
= timestamp
;
580 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
581 fEndTime
= timestamp
;
583 if (context
.hasValidRank()) {
584 long rank
= context
.getRank();
585 if (fNbEvents
<= rank
) {
586 fNbEvents
= rank
+ 1;
588 if (fIndexer
!= null) {
589 fIndexer
.updateIndex(context
, timestamp
);
594 // ------------------------------------------------------------------------
596 // ------------------------------------------------------------------------
599 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
602 protected ITmfContext
armRequest(final ITmfDataRequest request
) {
603 if ((request
instanceof ITmfEventRequest
)
604 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
605 && (request
.getIndex() == 0))
607 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
608 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
612 return seekEvent(request
.getIndex());
615 // ------------------------------------------------------------------------
617 // ------------------------------------------------------------------------
620 * @see java.lang.Object#toString()
623 @SuppressWarnings("nls")
624 public synchronized String
toString() {
625 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
626 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
627 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";