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
;
30 * Abstract implementation of ITmfTrace.
32 * Since the concept of 'location' is trace specific, the concrete classes have
33 * to provide the related methods, namely:
35 * <li> public ITmfLocation<?> getCurrentLocation()
36 * <li> public double getLocationRatio(ITmfLocation<?> location)
37 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
38 * <li> public ITmfContext seekEvent(double ratio)
39 * <li> public boolean validate(IProject project, String path)
41 * A concrete trace must provide its corresponding parser. A common way to
42 * accomplish this is by making the concrete class extend TmfTrace and
43 * implement ITmfEventParser.
45 * The concrete class can either specify its own indexer or use the provided
46 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
47 * used as checkpoint interval.
50 * @author Francois Chouinard
53 * @see ITmfTraceIndexer
54 * @see ITmfEventParser
56 public abstract class TmfTrace
extends TmfEventProvider
implements ITmfTrace
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 // The resource used for persistent properties for this trace
63 private IResource fResource
;
68 // The trace cache page size
69 private int fCacheSize
= ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
71 // The number of events collected (so far)
72 private long fNbEvents
= 0;
74 // The time span of the event stream
75 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_CRUNCH
;
76 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
78 // The trace streaming interval (0 = no streaming)
79 private long fStreamingInterval
= 0;
82 private ITmfTraceIndexer fIndexer
;
85 private ITmfEventParser fParser
;
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * The default, parameterless, constructor
99 * The standard constructor (non-live trace). Applicable when the trace
100 * implements its own parser and if at checkpoint-based index is OK.
102 * @param resource the resource associated to the trace
103 * @param type the trace event type
104 * @param path the trace path
105 * @param cacheSize the trace cache size
106 * @throws TmfTraceException
108 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
) throws TmfTraceException
{
109 this(resource
, type
, path
, cacheSize
, 0);
113 * The standard constructor (live trace). Applicable when the trace
114 * implements its own parser and if at checkpoint-based index is OK.
116 * @param resource the resource associated to the trace
117 * @param type the trace event type
118 * @param path the trace path
119 * @param cacheSize the trace cache size
120 * @param interval the trace streaming interval
121 * @throws TmfTraceException
123 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
124 this(resource
, type
, path
, cacheSize
, interval
, null);
128 * The 'non-default indexer' constructor. Allows to provide a trace
131 * @param resource the resource associated to the trace
132 * @param type the trace event type
133 * @param path the trace path
134 * @param cacheSize the trace cache size
135 * @param indexer the trace indexer
136 * @throws TmfTraceException
138 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
139 final long interval
, final ITmfTraceIndexer indexer
) throws TmfTraceException
{
140 this(resource
, type
, path
, cacheSize
, interval
, indexer
, null);
144 * The full constructor where trace specific indexer/parser are provided.
146 * @param resource the resource associated to the trace
147 * @param type the trace event type
148 * @param path the trace path
149 * @param cacheSize the trace cache size
150 * @param indexer the trace indexer
151 * @param parser the trace event parser
152 * @throws TmfTraceException
154 protected TmfTrace(final IResource resource
, final Class
<?
extends ITmfEvent
> type
, final String path
, final int cacheSize
,
155 final long interval
, final ITmfTraceIndexer indexer
, final ITmfEventParser parser
) throws TmfTraceException
{
157 fCacheSize
= (cacheSize
> 0) ? cacheSize
: ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
;
158 fStreamingInterval
= interval
;
159 fIndexer
= (indexer
!= null) ? indexer
: new TmfCheckpointIndexer(this, fCacheSize
);
161 initialize(resource
, path
, type
);
167 * @param trace the original trace
168 * @throws TmfTraceException Should not happen usually
170 public TmfTrace(final TmfTrace trace
) throws TmfTraceException
{
173 throw new IllegalArgumentException();
175 fCacheSize
= trace
.getCacheSize();
176 fStreamingInterval
= trace
.getStreamingInterval();
177 fIndexer
= new TmfCheckpointIndexer(this);
178 fParser
= trace
.fParser
;
179 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
182 // ------------------------------------------------------------------------
183 // ITmfTrace - Initializers
184 // ------------------------------------------------------------------------
187 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
190 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
191 fIndexer
= new TmfCheckpointIndexer(this, fCacheSize
);
192 initialize(resource
, path
, type
);
196 * Initialize the trace common attributes and the base component.
198 * @param resource the Eclipse resource (trace)
199 * @param path the trace path
200 * @param type the trace event type
202 * @throws TmfTraceException
204 protected void initialize(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
206 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
209 fResource
= resource
;
210 String traceName
= (resource
!= null) ? resource
.getName() : null;
211 // If no resource was provided, extract the display name the trace path
212 if (traceName
== null) {
213 final int sep
= path
.lastIndexOf(IPath
.SEPARATOR
);
214 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
216 if (fParser
== null) {
217 if (this instanceof ITmfEventParser
) {
218 fParser
= (ITmfEventParser
) this;
220 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
223 super.init(traceName
, type
);
227 * Indicates if the path points to an existing file/directory
229 * @param path the path to test
230 * @return true if the file/directory exists
232 protected boolean fileExists(final String path
) {
233 final File file
= new File(path
);
234 return file
.exists();
240 * @param waitForCompletion index synchronously (true) or not (false)
242 protected void indexTrace(boolean waitForCompletion
) {
243 getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, waitForCompletion
);
250 public synchronized void dispose() {
251 // Clean up the index if applicable
252 if (getIndexer() != null) {
253 getIndexer().dispose();
258 // ------------------------------------------------------------------------
259 // ITmfTrace - Basic getters
260 // ------------------------------------------------------------------------
263 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
266 public Class
<ITmfEvent
> getEventType() {
267 return (Class
<ITmfEvent
>) super.getType();
271 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
274 public IResource
getResource() {
279 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
282 public String
getPath() {
287 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
290 public int getCacheSize() {
295 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
298 public long getStreamingInterval() {
299 return fStreamingInterval
;
303 * @return the trace indexer
305 protected ITmfTraceIndexer
getIndexer() {
310 * @return the trace parser
312 protected ITmfEventParser
getParser() {
316 // ------------------------------------------------------------------------
317 // ITmfTrace - Trace characteristics getters
318 // ------------------------------------------------------------------------
321 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
324 public synchronized long getNbEvents() {
329 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
332 public TmfTimeRange
getTimeRange() {
333 return new TmfTimeRange(fStartTime
, fEndTime
);
337 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
340 public ITmfTimestamp
getStartTime() {
341 return fStartTime
.clone();
345 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
348 public ITmfTimestamp
getEndTime() {
349 return fEndTime
.clone();
352 // ------------------------------------------------------------------------
353 // Convenience setters/getters
354 // ------------------------------------------------------------------------
357 * Set the trace cache size. Must be done at initialization time.
359 * @param cacheSize The trace cache size
361 protected void setCacheSize(final int cacheSize
) {
362 fCacheSize
= cacheSize
;
366 * Set the trace known number of events. This can be quite dynamic
367 * during indexing or for live traces.
369 * @param nbEvents The number of events
371 protected synchronized void setNbEvents(final long nbEvents
) {
372 fNbEvents
= (nbEvents
> 0) ? nbEvents
: 0;
376 * Update the trace events time range
378 * @param range the new time range
380 protected void setTimeRange(final TmfTimeRange range
) {
381 fStartTime
= range
.getStartTime().clone();
382 fEndTime
= range
.getEndTime().clone();
386 * Update the trace chronologically first event timestamp
388 * @param startTime the new first event timestamp
390 protected void setStartTime(final ITmfTimestamp startTime
) {
391 fStartTime
= startTime
.clone();
395 * Update the trace chronologically last event timestamp
397 * @param endTime the new last event timestamp
399 protected void setEndTime(final ITmfTimestamp endTime
) {
400 fEndTime
= endTime
.clone();
404 * Set the polling interval for live traces (default = 0 = no streaming).
406 * @param interval the new trace streaming interval
408 protected void setStreamingInterval(final long interval
) {
409 fStreamingInterval
= (interval
> 0) ? interval
: 0;
413 * Set the trace indexer. Must be done at initialization time.
415 * @param indexer the trace indexer
417 protected void setIndexer(final ITmfTraceIndexer indexer
) {
422 * Set the trace parser. Must be done at initialization time.
424 * @param parser the new trace parser
426 protected void setParser(final ITmfEventParser parser
) {
430 // ------------------------------------------------------------------------
431 // ITmfTrace - SeekEvent operations (returning a trace context)
432 // ------------------------------------------------------------------------
435 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
438 public synchronized ITmfContext
seekEvent(final long rank
) {
440 // A rank <= 0 indicates to seek the first event
442 ITmfContext context
= seekEvent((ITmfLocation
) null);
447 // Position the trace at the checkpoint
448 final ITmfContext context
= fIndexer
.seekIndex(rank
);
450 // And locate the requested event context
451 long pos
= context
.getRank();
453 ITmfEvent event
= getNext(context
);
454 while ((event
!= null) && (++pos
< rank
)) {
455 event
= getNext(context
);
462 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
465 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
467 // A null timestamp indicates to seek the first event
468 if (timestamp
== null) {
469 ITmfContext context
= seekEvent((ITmfLocation
) null);
474 // Position the trace at the checkpoint
475 ITmfContext context
= fIndexer
.seekIndex(timestamp
);
477 // And locate the requested event context
478 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
479 ITmfEvent event
= getNext(nextEventContext
);
480 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
481 context
= nextEventContext
.clone();
482 event
= getNext(nextEventContext
);
485 context
.setLocation(null);
486 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
491 // ------------------------------------------------------------------------
492 // ITmfTrace - Read operations (returning an actual event)
493 // ------------------------------------------------------------------------
496 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
499 public synchronized ITmfEvent
getNext(final ITmfContext context
) {
500 // parseEvent() does not update the context
501 final ITmfEvent event
= fParser
.parseEvent(context
);
503 updateAttributes(context
, event
.getTimestamp());
504 context
.setLocation(getCurrentLocation());
505 context
.increaseRank();
512 * Hook for special event processing by the concrete class
513 * (called by TmfTrace.getEvent())
515 * @param event the event
517 protected void processEvent(final ITmfEvent event
) {
522 * Update the trace attributes
524 * @param context the current trace context
525 * @param timestamp the corresponding timestamp
527 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
528 if (fStartTime
.equals(TmfTimestamp
.BIG_BANG
) || (fStartTime
.compareTo(timestamp
, false) > 0)) {
529 fStartTime
= timestamp
.clone();
531 if (fEndTime
.equals(TmfTimestamp
.BIG_CRUNCH
) || (fEndTime
.compareTo(timestamp
, false) < 0)) {
532 fEndTime
= timestamp
.clone();
534 if (context
.hasValidRank()) {
535 long rank
= context
.getRank();
536 if (fNbEvents
<= rank
) {
537 fNbEvents
= rank
+ 1;
539 fIndexer
.updateIndex(context
, timestamp
);
543 // ------------------------------------------------------------------------
545 // ------------------------------------------------------------------------
548 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
551 protected ITmfContext
armRequest(final ITmfDataRequest request
) {
552 if ((request
instanceof ITmfEventRequest
)
553 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
554 && (request
.getIndex() == 0))
556 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
557 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
561 return seekEvent(request
.getIndex());
564 // ------------------------------------------------------------------------
566 // ------------------------------------------------------------------------
569 * @see java.lang.Object#toString()
572 @SuppressWarnings("nls")
573 public synchronized String
toString() {
574 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
575 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
576 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";