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
;
17 import java
.io
.FileNotFoundException
;
19 import org
.eclipse
.core
.resources
.IProject
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.runtime
.Path
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
31 * <b><u>TmfTrace</u></b>
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)
43 * A concrete trace must provide its corresponding parser. A common way to
44 * accomplish this is by making the concrete class extend TmfTrace and
45 * implement ITmfEventParser.
47 * The concrete class can either specify its own indexer or use the provided
48 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
49 * used as checkpoint interval.
51 public abstract class TmfTrace
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * The default trace cache size
60 public static final int DEFAULT_TRACE_CACHE_SIZE
= 1000;
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 // The resource used for persistent properties for this trace
67 private IResource fResource
;
75 protected int fCacheSize
= DEFAULT_TRACE_CACHE_SIZE
;
78 * The number of events collected so far
80 protected long fNbEvents
= 0;
82 // The time span of the event stream
83 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_CRUNCH
;
84 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
87 * The trace streaming interval (0 = no streaming)
89 protected long fStreamingInterval
= 0;
94 protected ITmfTraceIndexer
<ITmfTrace
<ITmfEvent
>> fIndexer
;
99 protected ITmfEventParser
<ITmfEvent
> fParser
;
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * The default, parameterless, constructor
108 @SuppressWarnings({ "unchecked", "rawtypes" })
111 fIndexer
= new TmfCheckpointIndexer(this);
115 * The standard constructor (non-live trace). Applicable when the trace
116 * implements its own parser and if at checkpoint-based index is OK.
118 * @param resource the resource associated to the trace
119 * @param type the trace event type
120 * @param path the trace path
121 * @param cacheSize the trace cache size
122 * @throws FileNotFoundException
124 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
) throws FileNotFoundException
{
125 this(resource
, type
, path
, cacheSize
, 0, null);
129 * The standard constructor (live trace). Applicable when the trace
130 * implements its own parser and if at checkpoint-based index is OK.
132 * @param resource the resource associated to the trace
133 * @param type the trace event type
134 * @param path the trace path
135 * @param cacheSize the trace cache size
136 * @param interval the trace streaming interval
137 * @throws FileNotFoundException
139 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
, final long interval
) throws FileNotFoundException
{
140 this(resource
, type
, path
, cacheSize
, interval
, null);
144 * The 'non-default indexer' constructor. Allows to provide a trace
147 * @param resource the resource associated to the trace
148 * @param type the trace event type
149 * @param path the trace path
150 * @param cacheSize the trace cache size
151 * @param indexer the trace indexer
152 * @throws FileNotFoundException
154 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
,
155 final long interval
, final ITmfTraceIndexer
<?
> indexer
) throws FileNotFoundException
{
156 this(resource
, type
, path
, cacheSize
, interval
, null, null);
160 * The full constructor where trace specific indexer/parser are provided.
162 * @param resource the resource associated to the trace
163 * @param type the trace event type
164 * @param path the trace path
165 * @param cacheSize the trace cache size
166 * @param indexer the trace indexer
167 * @param parser the trace event parser
168 * @throws FileNotFoundException
170 @SuppressWarnings({ "unchecked", "rawtypes" })
171 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
,
172 final long interval
, final ITmfTraceIndexer
<?
> indexer
, final ITmfEventParser
<ITmfEvent
> parser
) throws FileNotFoundException
{
174 fCacheSize
= (cacheSize
> 0) ? cacheSize
: DEFAULT_TRACE_CACHE_SIZE
;
175 fStreamingInterval
= interval
;
176 fIndexer
= (indexer
!= null) ? indexer
: new TmfCheckpointIndexer(this, fCacheSize
);
178 initialize(resource
, path
, type
);
184 * @param trace the original trace
186 @SuppressWarnings({ "unchecked", "rawtypes" })
187 public TmfTrace(final TmfTrace
<T
> trace
) throws FileNotFoundException
{
190 throw new IllegalArgumentException();
191 fCacheSize
= trace
.getCacheSize();
192 fStreamingInterval
= trace
.getStreamingInterval();
193 fIndexer
= new TmfCheckpointIndexer(this);
194 fParser
= trace
.fParser
;
195 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
198 // ------------------------------------------------------------------------
199 // ITmfTrace - Initializers
200 // ------------------------------------------------------------------------
203 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
206 public void initTrace(final IResource resource
, final String path
, final Class
<T
> type
) throws FileNotFoundException
{
207 initialize(resource
, path
, type
);
208 fIndexer
.buildIndex(false);
212 * Initialize the trace common attributes and the base component.
214 * @param resource the Eclipse resource (trace)
215 * @param path the trace path
216 * @param type the trace event type
218 * @throws FileNotFoundException
220 protected void initialize(final IResource resource
, final String path
, final Class
<T
> type
) throws FileNotFoundException
{
222 throw new FileNotFoundException();
224 fResource
= resource
;
225 String traceName
= (resource
!= null) ? resource
.getName() : null;
226 // If no resource was provided, extract the display name the trace path
227 if (traceName
== null) {
228 final int sep
= path
.lastIndexOf(Path
.SEPARATOR
);
229 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
231 super.init(traceName
, type
);
235 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
237 * Default validation: make sure the trace file exists.
240 public boolean validate(final IProject project
, final String path
) {
241 final File file
= new File(path
);
242 return file
.exists();
245 // ------------------------------------------------------------------------
246 // ITmfTrace - Basic getters
247 // ------------------------------------------------------------------------
250 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
253 @SuppressWarnings("unchecked")
254 public Class
<T
> getEventType() {
255 return (Class
<T
>) super.getType();
259 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
262 public IResource
getResource() {
267 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
270 public String
getPath() {
275 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
278 public int getCacheSize() {
283 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
286 public long getStreamingInterval() {
287 return fStreamingInterval
;
290 // ------------------------------------------------------------------------
291 // ITmfTrace - Trace characteristics getters
292 // ------------------------------------------------------------------------
295 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
298 public long getNbEvents() {
303 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
306 public TmfTimeRange
getTimeRange() {
307 return new TmfTimeRange(fStartTime
, fEndTime
);
311 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
314 public ITmfTimestamp
getStartTime() {
315 return fStartTime
.clone();
319 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
322 public ITmfTimestamp
getEndTime() {
323 return fEndTime
.clone();
326 // ------------------------------------------------------------------------
327 // Convenience setters
328 // ------------------------------------------------------------------------
331 * Update the trace events time range
333 * @param range the new time range
335 protected void setTimeRange(final TmfTimeRange range
) {
336 fStartTime
= range
.getStartTime().clone();
337 fEndTime
= range
.getEndTime().clone();
341 * Update the trace chronologically first event timestamp
343 * @param startTime the new first event timestamp
345 protected void setStartTime(final ITmfTimestamp startTime
) {
346 fStartTime
= startTime
.clone();
350 * Update the trace chronologically last event timestamp
352 * @param endTime the new last event timestamp
354 protected void setEndTime(final ITmfTimestamp endTime
) {
355 fEndTime
= endTime
.clone();
359 * Update the trace streaming interval
361 * @param interval the new trace streaming interval
363 protected void setStreamingInterval(final long interval
) {
364 fStreamingInterval
= (interval
> 0) ? interval
: 0;
367 // ------------------------------------------------------------------------
368 // ITmfTrace - SeekEvent operations (returning a trace context)
369 // ------------------------------------------------------------------------
372 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
375 public synchronized ITmfContext
seekEvent(final long rank
) {
377 // A rank <= 0 indicates to seek the first event
379 return seekEvent((ITmfLocation
<?
>) null);
381 // Position the trace at the checkpoint
382 final ITmfContext context
= fIndexer
.seekIndex(rank
);
384 // And locate the requested event context
385 long pos
= context
.getRank();
387 ITmfEvent event
= readEvent(context
);
388 while (event
!= null && ++pos
< rank
) {
389 event
= readEvent(context
);
396 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
399 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
401 // A null timestamp indicates to seek the first event
402 if (timestamp
== null)
405 // Position the trace at the checkpoint
406 final ITmfContext context
= fIndexer
.seekIndex(timestamp
);
408 // And locate the requested event context
409 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
410 ITmfEvent event
= readEvent(nextEventContext
);
411 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
412 context
.setLocation(nextEventContext
.getLocation().clone());
413 context
.increaseRank();
414 event
= readEvent(nextEventContext
);
419 // ------------------------------------------------------------------------
420 // ITmfTrace - Read operations (returning an actual event)
421 // ------------------------------------------------------------------------
424 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
427 public synchronized ITmfEvent
readEvent(final ITmfContext context
) {
428 // parseEvent() does not update the context
429 final ITmfEvent event
= fParser
.parseEvent(context
);
431 updateAttributes(context
, event
.getTimestamp());
432 context
.setLocation(getCurrentLocation());
433 context
.increaseRank();
440 * Hook for special event processing by the concrete class
441 * (called by TmfTrace.getEvent())
443 * @param event the event
445 protected void processEvent(final ITmfEvent event
) {
450 * Update the trace attributes
452 * @param context the current trace context
456 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
457 if (fStartTime
.compareTo(timestamp
, false) > 0) {
458 fStartTime
= timestamp
;
460 if (fEndTime
.compareTo(timestamp
, false) < 0) {
461 fEndTime
= timestamp
;
463 if (context
.hasValidRank()) {
464 long rank
= context
.getRank();
465 if (fNbEvents
<= rank
) {
466 fNbEvents
= rank
+ 1;
468 fIndexer
.updateIndex(context
, timestamp
);
472 // ------------------------------------------------------------------------
474 // ------------------------------------------------------------------------
477 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
480 public ITmfContext
armRequest(final ITmfDataRequest
<T
> request
) {
481 if (request
instanceof ITmfEventRequest
<?
>
482 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime())
483 && request
.getIndex() == 0) {
484 final ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
485 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
489 return seekEvent(request
.getIndex());
493 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
496 @SuppressWarnings("unchecked")
497 public T
getNext(final ITmfContext context
) {
498 if (context
instanceof TmfContext
)
499 return (T
) readEvent(context
);
504 // ------------------------------------------------------------------------
506 // ------------------------------------------------------------------------
509 * @see java.lang.Object#toString()
512 @SuppressWarnings("nls")
513 public String
toString() {
514 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
515 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
516 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";