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
.IResource
;
20 import org
.eclipse
.core
.runtime
.Path
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
31 * Abstract implementation of ITmfTrace.
33 * Since the concept of 'location' is trace specific, the concrete classes have
34 * to provide the related methods, namely:
36 * <li> public ITmfLocation<?> getCurrentLocation()
37 * <li> public double getLocationRatio(ITmfLocation<?> location)
38 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
39 * <li> public ITmfContext seekEvent(double ratio)
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.
51 * @author Francois Chouinard
54 * @see TmfEventProvider
56 * @see ITmfTraceIndexer
57 * @see ITmfEventParser
59 public abstract class TmfTrace
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * The default trace cache size
68 public static final int DEFAULT_TRACE_CACHE_SIZE
= 10000;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 // The resource used for persistent properties for this trace
75 private IResource fResource
;
83 protected int fCacheSize
= DEFAULT_TRACE_CACHE_SIZE
;
86 * The number of events collected so far
88 protected long fNbEvents
= 0;
90 // The time span of the event stream
91 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_CRUNCH
;
92 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
95 * The trace streaming interval (0 = no streaming)
97 protected long fStreamingInterval
= 0;
102 protected ITmfTraceIndexer
<ITmfTrace
<ITmfEvent
>> fIndexer
;
107 protected ITmfEventParser
<T
> fParser
;
109 // ------------------------------------------------------------------------
111 // ------------------------------------------------------------------------
114 * The default, parameterless, constructor
116 @SuppressWarnings({ "unchecked", "rawtypes" })
119 fIndexer
= new TmfCheckpointIndexer(this);
123 * The standard constructor (non-live trace). Applicable when the trace
124 * implements its own parser and if at checkpoint-based index is OK.
126 * @param resource the resource associated to the trace
127 * @param type the trace event type
128 * @param path the trace path
129 * @param cacheSize the trace cache size
130 * @throws TmfTraceException
132 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
) throws TmfTraceException
{
133 this(resource
, type
, path
, cacheSize
, 0, null);
137 * The standard constructor (live trace). Applicable when the trace
138 * implements its own parser and if at checkpoint-based index is OK.
140 * @param resource the resource associated to the trace
141 * @param type the trace event type
142 * @param path the trace path
143 * @param cacheSize the trace cache size
144 * @param interval the trace streaming interval
145 * @throws TmfTraceException
147 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
, final long interval
) throws TmfTraceException
{
148 this(resource
, type
, path
, cacheSize
, interval
, null);
152 * The 'non-default indexer' constructor. Allows to provide a trace
155 * @param resource the resource associated to the trace
156 * @param type the trace event type
157 * @param path the trace path
158 * @param cacheSize the trace cache size
159 * @param indexer the trace indexer
160 * @throws TmfTraceException
162 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
,
163 final long interval
, final ITmfTraceIndexer
<?
> indexer
) throws TmfTraceException
{
164 this(resource
, type
, path
, cacheSize
, interval
, null, null);
168 * The full constructor where trace specific indexer/parser are provided.
170 * @param resource the resource associated to the trace
171 * @param type the trace event type
172 * @param path the trace path
173 * @param cacheSize the trace cache size
174 * @param indexer the trace indexer
175 * @param parser the trace event parser
176 * @throws TmfTraceException
178 @SuppressWarnings({ "unchecked", "rawtypes" })
179 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
,
180 final long interval
, final ITmfTraceIndexer
<?
> indexer
, final ITmfEventParser
<T
> parser
) throws TmfTraceException
{
182 fCacheSize
= (cacheSize
> 0) ? cacheSize
: DEFAULT_TRACE_CACHE_SIZE
;
183 fStreamingInterval
= interval
;
184 fIndexer
= (indexer
!= null) ? indexer
: new TmfCheckpointIndexer(this, fCacheSize
);
186 initialize(resource
, path
, type
);
192 * @param trace the original trace
194 @SuppressWarnings({ "unchecked", "rawtypes" })
195 public TmfTrace(final TmfTrace
<T
> trace
) throws TmfTraceException
{
198 throw new IllegalArgumentException();
199 fCacheSize
= trace
.getCacheSize();
200 fStreamingInterval
= trace
.getStreamingInterval();
201 fIndexer
= new TmfCheckpointIndexer(this);
202 fParser
= trace
.fParser
;
203 initialize(trace
.getResource(), trace
.getPath(), trace
.getEventType());
206 // ------------------------------------------------------------------------
207 // ITmfTrace - Initializers
208 // ------------------------------------------------------------------------
211 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
214 public void initTrace(final IResource resource
, final String path
, final Class
<T
> type
) throws TmfTraceException
{
215 initialize(resource
, path
, type
);
216 fIndexer
.buildIndex(false);
220 * Initialize the trace common attributes and the base component.
222 * @param resource the Eclipse resource (trace)
223 * @param path the trace path
224 * @param type the trace event type
226 * @throws FileNotFoundException
228 @SuppressWarnings("unchecked")
229 protected void initialize(final IResource resource
, final String path
, final Class
<T
> type
) throws TmfTraceException
{
231 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
233 fResource
= resource
;
234 String traceName
= (resource
!= null) ? resource
.getName() : null;
235 // If no resource was provided, extract the display name the trace path
236 if (traceName
== null) {
237 final int sep
= path
.lastIndexOf(Path
.SEPARATOR
);
238 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
240 if (fParser
== null) {
241 if (this instanceof ITmfEventParser
) {
242 fParser
= (ITmfEventParser
<T
>) this;
244 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
247 super.init(traceName
, type
);
251 * Indicates if the path points to an existing file/directory
253 * @param path the path to test
254 * @return true if the file/directory exists
256 protected boolean fileExists(final String path
) {
257 final File file
= new File(path
);
258 return file
.exists();
261 // ------------------------------------------------------------------------
262 // ITmfTrace - Basic getters
263 // ------------------------------------------------------------------------
266 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
269 @SuppressWarnings("unchecked")
270 public Class
<T
> getEventType() {
271 return (Class
<T
>) super.getType();
275 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
278 public IResource
getResource() {
283 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
286 public String
getPath() {
291 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
294 public int getCacheSize() {
299 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
302 public long getStreamingInterval() {
303 return fStreamingInterval
;
306 // ------------------------------------------------------------------------
307 // ITmfTrace - Trace characteristics getters
308 // ------------------------------------------------------------------------
311 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
314 public synchronized long getNbEvents() {
319 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
322 public TmfTimeRange
getTimeRange() {
323 return new TmfTimeRange(fStartTime
, fEndTime
);
327 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
330 public ITmfTimestamp
getStartTime() {
331 return fStartTime
.clone();
335 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
338 public ITmfTimestamp
getEndTime() {
339 return fEndTime
.clone();
342 // ------------------------------------------------------------------------
343 // Convenience setters
344 // ------------------------------------------------------------------------
347 * Update the trace events time range
349 * @param range the new time range
351 protected void setTimeRange(final TmfTimeRange range
) {
352 fStartTime
= range
.getStartTime().clone();
353 fEndTime
= range
.getEndTime().clone();
357 * Update the trace chronologically first event timestamp
359 * @param startTime the new first event timestamp
361 protected void setStartTime(final ITmfTimestamp startTime
) {
362 fStartTime
= startTime
.clone();
366 * Update the trace chronologically last event timestamp
368 * @param endTime the new last event timestamp
370 protected void setEndTime(final ITmfTimestamp endTime
) {
371 fEndTime
= endTime
.clone();
375 * Update the trace streaming interval
377 * @param interval the new trace streaming interval
379 protected void setStreamingInterval(final long interval
) {
380 fStreamingInterval
= (interval
> 0) ? interval
: 0;
383 // ------------------------------------------------------------------------
384 // ITmfTrace - SeekEvent operations (returning a trace context)
385 // ------------------------------------------------------------------------
388 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
391 public synchronized ITmfContext
seekEvent(final long rank
) {
393 // A rank <= 0 indicates to seek the first event
395 ITmfContext context
= seekEvent((ITmfLocation
<?
>) null);
400 // Position the trace at the checkpoint
401 final ITmfContext context
= fIndexer
.seekIndex(rank
);
403 // And locate the requested event context
404 long pos
= context
.getRank();
406 ITmfEvent event
= readNextEvent(context
);
407 while (event
!= null && ++pos
< rank
) {
408 event
= readNextEvent(context
);
415 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
418 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
420 // A null timestamp indicates to seek the first event
421 if (timestamp
== null) {
422 ITmfContext context
= seekEvent((ITmfLocation
<?
>) null);
427 // Position the trace at the checkpoint
428 final ITmfContext context
= fIndexer
.seekIndex(timestamp
);
430 // And locate the requested event context
431 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
432 ITmfEvent event
= readNextEvent(nextEventContext
);
433 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
434 context
.setLocation(nextEventContext
.getLocation().clone());
435 context
.increaseRank();
436 event
= readNextEvent(nextEventContext
);
441 // ------------------------------------------------------------------------
442 // ITmfTrace - Read operations (returning an actual event)
443 // ------------------------------------------------------------------------
446 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
449 public synchronized ITmfEvent
readNextEvent(final ITmfContext context
) {
450 // parseEvent() does not update the context
451 final ITmfEvent event
= fParser
.parseEvent(context
);
453 updateAttributes(context
, event
.getTimestamp());
454 context
.setLocation(getCurrentLocation());
455 context
.increaseRank();
462 * Hook for special event processing by the concrete class
463 * (called by TmfTrace.getEvent())
465 * @param event the event
467 protected void processEvent(final ITmfEvent event
) {
472 * Update the trace attributes
474 * @param context the current trace context
478 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
479 if (fStartTime
.compareTo(timestamp
, false) > 0) {
480 fStartTime
= timestamp
;
482 if (fEndTime
.compareTo(timestamp
, false) < 0) {
483 fEndTime
= timestamp
;
485 if (context
.hasValidRank()) {
486 long rank
= context
.getRank();
487 if (fNbEvents
<= rank
) {
488 fNbEvents
= rank
+ 1;
490 fIndexer
.updateIndex(context
, timestamp
);
494 // ------------------------------------------------------------------------
496 // ------------------------------------------------------------------------
499 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
502 public ITmfContext
armRequest(final ITmfDataRequest
<T
> request
) {
503 if (request
instanceof ITmfEventRequest
<?
>
504 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime())
505 && request
.getIndex() == 0) {
506 final ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
507 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
511 return seekEvent(request
.getIndex());
515 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
518 @SuppressWarnings("unchecked")
519 public T
getNext(final ITmfContext context
) {
520 if (context
instanceof TmfContext
)
521 return (T
) readNextEvent(context
);
526 // ------------------------------------------------------------------------
528 // ------------------------------------------------------------------------
531 * @see java.lang.Object#toString()
534 @SuppressWarnings("nls")
535 public synchronized String
toString() {
536 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
537 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
538 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";