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.
37 public abstract class TmfTrace
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 * The default trace cache size
46 public static final int DEFAULT_TRACE_CACHE_SIZE
= 1000;
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 // The resource used for persistent properties for this trace
53 private IResource fResource
;
59 * The cache page size AND trace checkpoints interval
61 protected int fCacheSize
= DEFAULT_TRACE_CACHE_SIZE
;
63 // The set of event stream checkpoints
64 // protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
66 // The number of events collected
67 protected long fNbEvents
= 0;
69 // The time span of the event stream
70 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_CRUNCH
;
71 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
74 * The trace streaming interval (0 = no streaming)
76 protected long fStreamingInterval
= 0;
81 protected ITmfTraceIndexer
<ITmfTrace
<ITmfEvent
>> fIndexer
;
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 * The default, parameterless, constructor
90 @SuppressWarnings({ "unchecked", "rawtypes" })
93 fIndexer
= new TmfTraceIndexer(this);
97 * The standard constructor (non-live trace)
99 * @param resource the resource associated to the trace
100 * @param type the trace event type
101 * @param path the trace path
102 * @param cacheSize the trace cache size
103 * @throws FileNotFoundException
105 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
) throws FileNotFoundException
{
106 this(resource
, type
, path
, cacheSize
, 0, null);
110 * The standard constructor (live trace)
112 * @param resource the resource associated to the trace
113 * @param type the trace event type
114 * @param path the trace path
115 * @param cacheSize the trace cache size
116 * @param interval the trace streaming interval
117 * @throws FileNotFoundException
119 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
, final long interval
) throws FileNotFoundException
{
120 this(resource
, type
, path
, cacheSize
, interval
, null);
124 * The full constructor
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 * @param indexer the trace indexer
131 * @throws FileNotFoundException
133 @SuppressWarnings({ "unchecked", "rawtypes" })
134 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
,
135 final long interval
, final ITmfTraceIndexer
<?
> indexer
) throws FileNotFoundException
{
137 fCacheSize
= (cacheSize
> 0) ? cacheSize
: DEFAULT_TRACE_CACHE_SIZE
;
138 fStreamingInterval
= interval
;
139 fIndexer
= (indexer
!= null) ? indexer
: new TmfTraceIndexer(this, fCacheSize
);
140 initialize(resource
, path
, type
);
146 * @param trace the original trace
148 @SuppressWarnings({ "unchecked", "rawtypes" })
149 public TmfTrace(final TmfTrace
<T
> trace
) throws FileNotFoundException
{
152 throw new IllegalArgumentException();
153 fCacheSize
= trace
.getCacheSize();
154 fStreamingInterval
= trace
.getStreamingInterval();
155 fIndexer
= new TmfTraceIndexer(this);
156 initialize(trace
.getResource(), trace
.getPath(), trace
.getType());
160 * Initialize the trace common attributes and the base component.
162 * @param resource the Eclipse resource (trace)
163 * @param path the trace path
164 * @param type the trace event type
166 * @throws FileNotFoundException
168 protected void initialize(final IResource resource
, final String path
, final Class
<T
> type
) throws FileNotFoundException
{
170 throw new FileNotFoundException();
172 fResource
= resource
;
173 String traceName
= (resource
!= null) ? resource
.getName() : null;
174 // If no resource was provided, extract the display name the trace path
175 if (traceName
== null) {
176 final int sep
= path
.lastIndexOf(Path
.SEPARATOR
);
177 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
179 super.init(traceName
, type
);
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
<T
> type
) throws FileNotFoundException
{
191 initialize(resource
, path
, type
);
192 fIndexer
.buildIndex(false);
196 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
198 * Default validation: make sure the trace file exists.
201 public boolean validate(final IProject project
, final String path
) {
202 final File file
= new File(path
);
203 return file
.exists();
206 // ------------------------------------------------------------------------
207 // ITmfTrace - Basic getters
208 // ------------------------------------------------------------------------
211 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getType()
214 @SuppressWarnings("unchecked")
215 public Class
<T
> getType() {
216 return (Class
<T
>) super.getType();
220 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
223 public IResource
getResource() {
228 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
231 public String
getPath() {
236 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
239 public int getCacheSize() {
244 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
247 public long getStreamingInterval() {
248 return fStreamingInterval
;
251 // ------------------------------------------------------------------------
252 // ITmfTrace - Trace characteristics getters
253 // ------------------------------------------------------------------------
256 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
259 public long getNbEvents() {
264 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
267 public TmfTimeRange
getTimeRange() {
268 return new TmfTimeRange(fStartTime
, fEndTime
);
272 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
275 public ITmfTimestamp
getStartTime() {
276 return fStartTime
.clone();
280 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
283 public ITmfTimestamp
getEndTime() {
284 return fEndTime
.clone();
287 // ------------------------------------------------------------------------
288 // Convenience setters
289 // ------------------------------------------------------------------------
292 * Update the trace events time range
294 * @param range the new time range
296 protected void setTimeRange(final TmfTimeRange range
) {
297 fStartTime
= range
.getStartTime().clone();
298 fEndTime
= range
.getEndTime().clone();
302 * Update the trace chronologically first event timestamp
304 * @param startTime the new first event timestamp
306 protected void setStartTime(final ITmfTimestamp startTime
) {
307 fStartTime
= startTime
.clone();
311 * Update the trace chronologically last event timestamp
313 * @param endTime the new last event timestamp
315 protected void setEndTime(final ITmfTimestamp endTime
) {
316 fEndTime
= endTime
.clone();
320 * Update the trace streaming interval
322 * @param interval the new trace streaming interval
324 protected void setStreamingInterval(final long interval
) {
325 fStreamingInterval
= (interval
> 0) ? interval
: 0;
328 // ------------------------------------------------------------------------
329 // ITmfTrace - Seek operations (returning a reading context)
330 // ------------------------------------------------------------------------
333 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
336 public synchronized ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
338 // A null timestamp indicates to seek the first event
339 if (timestamp
== null)
340 return seekLocation(null);
342 // Position the trace at the checkpoint
343 final ITmfContext context
= fIndexer
.seekIndex(timestamp
);
345 // And locate the requested event context
346 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
347 ITmfEvent event
= getNextEvent(nextEventContext
);
348 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
349 context
.setLocation(nextEventContext
.getLocation().clone());
350 context
.increaseRank();
351 event
= getNextEvent(nextEventContext
);
357 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
360 public synchronized ITmfContext
seekEvent(final long rank
) {
362 // A rank <= 0 indicates to seek the first event
364 return seekLocation(null);
366 // Position the trace at the checkpoint
367 final ITmfContext context
= fIndexer
.seekIndex(rank
);
369 // And locate the requested event context
370 long pos
= context
.getRank();
372 ITmfEvent event
= getNextEvent(context
);
373 while (event
!= null && ++pos
< rank
) {
374 event
= getNextEvent(context
);
380 // ------------------------------------------------------------------------
381 // ITmfTrace - Read operations (returning an actual event)
382 // ------------------------------------------------------------------------
385 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
388 public synchronized ITmfEvent
parseEvent(final ITmfContext context
) {
389 final ITmfContext tmpContext
= context
.clone();
390 final ITmfEvent event
= getNextEvent(tmpContext
);
395 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
398 public synchronized ITmfEvent
getNextEvent(final ITmfContext context
) {
399 // parseEvent() does not update the context
400 final ITmfEvent event
= parseEvent(context
);
402 updateAttributes(context
, event
.getTimestamp());
403 context
.setLocation(getCurrentLocation());
404 context
.increaseRank();
411 * Hook for special event processing by the concrete class
412 * (called by TmfTrace.getNextEvent())
414 * @param event the event
416 protected void processEvent(final ITmfEvent event
) {
421 * Update the trace attributes
423 * @param context the current trace context
427 protected synchronized void updateAttributes(final ITmfContext context
, final ITmfTimestamp timestamp
) {
428 if (fStartTime
.compareTo(timestamp
, false) > 0) {
429 fStartTime
= timestamp
;
431 if (fEndTime
.compareTo(timestamp
, false) < 0) {
432 fEndTime
= timestamp
;
434 if (context
.hasValidRank()) {
435 long rank
= context
.getRank();
436 if (fNbEvents
<= rank
) {
437 fNbEvents
= rank
+ 1;
439 fIndexer
.updateIndex(context
, timestamp
);
443 // ------------------------------------------------------------------------
445 // ------------------------------------------------------------------------
448 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
451 public ITmfContext
armRequest(final ITmfDataRequest
<T
> request
) {
452 if (request
instanceof ITmfEventRequest
<?
>
453 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime())
454 && request
.getIndex() == 0) {
455 final ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
456 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
460 return seekEvent(request
.getIndex());
464 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
467 @SuppressWarnings("unchecked")
468 public T
getNext(final ITmfContext context
) {
469 if (context
instanceof TmfContext
)
470 return (T
) getNextEvent(context
);
475 // ------------------------------------------------------------------------
477 // ------------------------------------------------------------------------
480 * @see java.lang.Object#toString()
483 @SuppressWarnings("nls")
484 public String
toString() {
485 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
486 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
487 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";