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
;
18 import java
.util
.Collections
;
19 import java
.util
.Vector
;
21 import org
.eclipse
.core
.resources
.IProject
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.runtime
.Path
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
33 * <b><u>TmfTrace</u></b>
35 * Abstract implementation of ITmfTrace.
39 public abstract class TmfTrace
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * The default trace cache size
48 public static final int DEFAULT_TRACE_CACHE_SIZE
= 1000;
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 // The resource used for persistent properties for this trace
55 private IResource fResource
;
61 * The cache page size AND trace checkpoints interval
63 protected int fCacheSize
= DEFAULT_TRACE_CACHE_SIZE
;
65 // The set of event stream checkpoints
66 protected Vector
<TmfCheckpoint
> fCheckpoints
= new Vector
<TmfCheckpoint
>();
68 // The number of events collected
69 protected long fNbEvents
= 0;
71 // The time span of the event stream
72 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_CRUNCH
;
73 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
76 * The trace streaming interval (0 = no streaming)
78 protected long fStreamingInterval
= 0;
83 protected ITmfTraceIndexer
<ITmfTrace
<ITmfEvent
>> fIndexer
;
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
90 * The default, parameterless, constructor
92 @SuppressWarnings({ "unchecked", "rawtypes" })
95 fIndexer
= new TmfTraceIndexer(this);
99 * The standard constructor (non-streaming trace)
101 * @param resource the resource associated to the trace
102 * @param type the trace event type
103 * @param path the trace path
104 * @param cacheSize the trace cache size
105 * @throws FileNotFoundException
107 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
) throws FileNotFoundException
{
108 this(resource
, type
, path
, cacheSize
, 0, null);
112 * The standard constructor (streaming trace)
114 * @param resource the resource associated to the trace
115 * @param type the trace event type
116 * @param path the trace path
117 * @param cacheSize the trace cache size
118 * @param interval the trace streaming interval
119 * @throws FileNotFoundException
121 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
, final long interval
) throws FileNotFoundException
{
122 this(resource
, type
, path
, cacheSize
, interval
, null);
126 * The full constructor
128 * @param resource the resource associated to the trace
129 * @param type the trace event type
130 * @param path the trace path
131 * @param cacheSize the trace cache size
132 * @param indexer the trace indexer
133 * @throws FileNotFoundException
135 @SuppressWarnings({ "unchecked", "rawtypes" })
136 protected TmfTrace(final IResource resource
, final Class
<T
> type
, final String path
, final int cacheSize
,
137 final long interval
, final ITmfTraceIndexer
<?
> indexer
) throws FileNotFoundException
{
139 fCacheSize
= (cacheSize
> 0) ? cacheSize
: DEFAULT_TRACE_CACHE_SIZE
;
140 fStreamingInterval
= interval
;
141 fIndexer
= (indexer
!= null) ? indexer
: new TmfTraceIndexer(this);
142 initialize(resource
, path
, type
);
148 * @param trace the original trace
150 @SuppressWarnings({ "unchecked", "rawtypes" })
151 public TmfTrace(final TmfTrace
<T
> trace
) throws FileNotFoundException
{
154 throw new IllegalArgumentException();
155 fCacheSize
= trace
.getCacheSize();
156 fStreamingInterval
= trace
.getStreamingInterval();
157 fIndexer
= new TmfTraceIndexer(this);
158 initialize(trace
.getResource(), trace
.getPath(), trace
.getType());
165 * @throws FileNotFoundException
167 protected void initialize(final IResource resource
, final String path
, final Class
<T
> type
) throws FileNotFoundException
{
168 fResource
= resource
;
170 String traceName
= (resource
!= null) ? resource
.getName() : null;
171 // If no display name was provided, extract it from the trace path
172 if (traceName
== null)
174 final int sep
= path
.lastIndexOf(Path
.SEPARATOR
);
175 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
178 traceName
= ""; //$NON-NLS-1$
180 super.init(traceName
, type
);
183 // ------------------------------------------------------------------------
184 // ITmfTrace - Initializers
185 // ------------------------------------------------------------------------
188 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
191 public void initTrace(final IResource resource
, final String path
, final Class
<T
> type
) throws FileNotFoundException
{
192 initialize(resource
, path
, type
);
193 fIndexer
.buildIndex(false);
197 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
199 * Default validation: make sure the trace file exists.
202 public boolean validate(final IProject project
, final String path
) {
203 final File file
= new File(path
);
204 return file
.exists();
207 // ------------------------------------------------------------------------
208 // ITmfTrace - Basic getters
209 // ------------------------------------------------------------------------
212 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getType()
215 @SuppressWarnings("unchecked")
216 public Class
<T
> getType() {
217 return (Class
<T
>) super.getType();
221 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
224 public IResource
getResource() {
229 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
232 public String
getPath() {
237 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
240 public int getCacheSize() {
245 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
248 public long getStreamingInterval() {
249 return fStreamingInterval
;
252 // ------------------------------------------------------------------------
253 // ITmfTrace - Trace characteristics getters
254 // ------------------------------------------------------------------------
257 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
260 public long getNbEvents() {
265 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
268 public TmfTimeRange
getTimeRange() {
269 return new TmfTimeRange(fStartTime
, fEndTime
);
273 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
276 public ITmfTimestamp
getStartTime() {
277 return fStartTime
.clone();
281 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
284 public ITmfTimestamp
getEndTime() {
285 return fEndTime
.clone();
288 // ------------------------------------------------------------------------
289 // Convenience setters
290 // ------------------------------------------------------------------------
293 * Update the trace events time range
295 * @param range the new time range
297 protected void setTimeRange(final TmfTimeRange range
) {
298 fStartTime
= range
.getStartTime().clone();
299 fEndTime
= range
.getEndTime().clone();
303 * Update the trace chronologically first event timestamp
305 * @param startTime the new first event timestamp
307 protected void setStartTime(final ITmfTimestamp startTime
) {
308 fStartTime
= startTime
.clone();
312 * Update the trace chronologically last event timestamp
314 * @param endTime the new last event timestamp
316 protected void setEndTime(final ITmfTimestamp endTime
) {
317 fEndTime
= endTime
.clone();
321 * Update the trace streaming interval
323 * @param interval the new trace streaming interval
325 protected void setStreamingInterval(final long interval
) {
326 fStreamingInterval
= interval
;
329 // ------------------------------------------------------------------------
330 // ITmfTrace - Seek operations (returning a reading context)
331 // ------------------------------------------------------------------------
334 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
337 public ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
339 // A null timestamp indicates to seek the first event
340 if (timestamp
== null)
341 return seekLocation(null);
343 // Find the checkpoint at or before the requested timestamp.
344 // In the very likely event that the timestamp is not at a checkpoint
345 // boundary, bsearch will return index = (- (insertion point + 1)).
346 // It is then trivial to compute the index of the previous checkpoint.
347 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
349 index
= Math
.max(0, -(index
+ 2));
352 // Position the trace at the checkpoint
353 final ITmfContext context
= seekCheckpoint(index
);
355 // And locate the requested event context
356 final ITmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
357 ITmfEvent event
= getNextEvent(nextEventContext
);
358 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
359 context
.setLocation(nextEventContext
.getLocation().clone());
360 context
.increaseRank();
361 event
= getNextEvent(nextEventContext
);
367 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
370 public ITmfContext
seekEvent(final long rank
) {
372 // A rank <= 0 indicates to seek the first event
374 return seekLocation(null);
376 // Find the checkpoint at or before the requested rank.
377 final int index
= (int) rank
/ fCacheSize
;
378 final ITmfContext context
= seekCheckpoint(index
);
380 // And locate the requested event context
381 long pos
= context
.getRank();
383 ITmfEvent event
= getNextEvent(context
);
384 while (event
!= null && ++pos
< rank
) {
385 event
= getNextEvent(context
);
393 * Position the trace at the given checkpoint
395 * @param index the checkpoint index
396 * @return the corresponding context
398 private ITmfContext
seekCheckpoint(int index
) {
399 ITmfLocation
<?
> location
;
400 synchronized (fCheckpoints
) {
401 if (!fCheckpoints
.isEmpty()) {
402 if (index
>= fCheckpoints
.size()) {
403 index
= fCheckpoints
.size() - 1;
405 location
= fCheckpoints
.elementAt(index
).getLocation();
410 final ITmfContext context
= seekLocation(location
);
411 context
.setRank(index
* fCacheSize
);
415 // ------------------------------------------------------------------------
416 // ITmfTrace - Read operations (returning an actual event)
417 // ------------------------------------------------------------------------
420 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
423 public synchronized ITmfEvent
getNextEvent(final ITmfContext context
) {
424 // parseEvent() does not update the context
425 final ITmfEvent event
= parseEvent(context
);
427 updateIndex(context
, context
.getRank(), event
.getTimestamp());
428 context
.setLocation(getCurrentLocation());
429 context
.increaseRank();
436 * Hook for special processing by the concrete class (called by
441 protected void processEvent(final ITmfEvent event
) {
442 // Do nothing by default
445 protected synchronized void updateIndex(final ITmfContext context
, final long rank
, final ITmfTimestamp timestamp
) {
446 if (fStartTime
.compareTo(timestamp
, false) > 0) {
447 fStartTime
= timestamp
;
449 if (fEndTime
.compareTo(timestamp
, false) < 0) {
450 fEndTime
= timestamp
;
452 if (context
.hasValidRank()) {
453 if (fNbEvents
<= rank
) {
454 fNbEvents
= rank
+ 1;
456 // Build the index as we go along
457 if ((rank
% fCacheSize
) == 0) {
458 // Determine the table position
459 final long position
= rank
/ fCacheSize
;
460 // Add new entry at proper location (if empty)
461 if (fCheckpoints
.size() == position
) {
462 final ITmfLocation
<?
> location
= context
.getLocation().clone();
463 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
464 // System.out.println(getName() + "[" + (fCheckpoints.size() + "] " + timestamp + ", " + location.toString());
470 // // ------------------------------------------------------------------------
471 // // ITmfTrace - indexing
472 // // ------------------------------------------------------------------------
475 // * The index is a list of contexts that point to events at regular interval
476 // * (rank-wise) in the trace. After it is built, the index can be used to
477 // * quickly access any event by rank or timestamp.
479 // * fIndexPageSize holds the event interval (default INDEX_PAGE_SIZE).
482 // @SuppressWarnings({ "unchecked" })
483 // protected void indexTrace(final boolean waitForCompletion) {
485 // // The monitoring job
486 // final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
489 // protected IStatus run(final IProgressMonitor monitor) {
490 // while (!monitor.isCanceled()) {
492 // Thread.sleep(100);
493 // } catch (final InterruptedException e) {
494 // return Status.OK_STATUS;
498 // return Status.OK_STATUS;
503 // // Clear the checkpoints
504 // fCheckpoints.clear();
506 // // Build a background request for all the trace data. The index is
507 // // updated as we go by getNextEvent().
508 // final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class,
509 // TmfTimeRange.ETERNITY,
510 // TmfDataRequest.ALL_DATA, fCacheSize, ITmfDataRequest.ExecutionType.BACKGROUND)
513 // ITmfTimestamp startTime = null;
514 // ITmfTimestamp lastTime = null;
517 // public void handleData(final ITmfEvent event) {
518 // super.handleData(event);
519 // if (event != null) {
520 // final ITmfTimestamp timestamp = event.getTimestamp();
521 // if (startTime == null) {
522 // startTime = timestamp.clone();
524 // lastTime = timestamp.clone();
526 // // Update the trace status at regular intervals
527 // if ((getNbRead() % fCacheSize) == 0) {
528 // updateTraceStatus();
534 // public void handleSuccess() {
535 // updateTraceStatus();
539 // public void handleCompleted() {
541 // super.handleCompleted();
544 // private synchronized void updateTraceStatus() {
545 // final int nbRead = getNbRead();
546 // if (nbRead != 0) {
547 // fStartTime = startTime;
548 // fEndTime = lastTime;
549 // fNbEvents = nbRead;
550 // notifyListeners();
555 // // Submit the request and wait for completion if required
556 // sendRequest((ITmfDataRequest<T>) request);
557 // if (waitForCompletion) {
559 // request.waitForCompletion();
560 // } catch (final InterruptedException e) {
565 // private void notifyListeners() {
566 // broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime)));
569 // ------------------------------------------------------------------------
571 // ------------------------------------------------------------------------
574 public ITmfContext
armRequest(final ITmfDataRequest
<T
> request
) {
575 if (request
instanceof ITmfEventRequest
<?
>
576 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime())
577 && request
.getIndex() == 0) {
578 final ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
579 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
583 return seekEvent(request
.getIndex());
587 * Return the next piece of data based on the context supplied. The context
588 * would typically be updated for the subsequent read.
591 * @return the event referred to by context
594 @SuppressWarnings("unchecked")
595 public T
getNext(final ITmfContext context
) {
596 if (context
instanceof TmfContext
)
597 return (T
) getNextEvent(context
);
602 // ------------------------------------------------------------------------
604 // ------------------------------------------------------------------------
607 @SuppressWarnings("nls")
608 public String
toString() {
609 return "TmfTrace [fPath=" + fPath
+ ", fCacheSize=" + fCacheSize
610 + ", fNbEvents=" + fNbEvents
+ ", fStartTime=" + fStartTime
611 + ", fEndTime=" + fEndTime
+ ", fStreamingInterval=" + fStreamingInterval
+ "]";