1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.trace
;
16 import java
.io
.FileNotFoundException
;
17 import java
.util
.Collections
;
18 import java
.util
.Vector
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.Path
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.core
.runtime
.jobs
.Job
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
39 * <b><u>TmfTrace</u></b>
41 * Abstract implementation of ITmfTrace. It should be sufficient to extend this class and provide implementation for
42 * <code>getCurrentLocation()</code> and <code>seekLocation()</code>, as well as a proper parser, to have a working
43 * concrete implementation.
45 * Note: The notion of event rank is still under heavy discussion. Although used by the Events View and probably useful
46 * in the general case, there is no easy way to implement it for LTTng (actually a strong case is being made that this
49 * That it is not supported by LTTng does by no mean indicate that it is not useful for (just about) every other tracing
50 * tool. Therefore, this class provides a minimal (and partial) implementation of rank. However, the current
51 * implementation should not be relied on in the general case.
53 * TODO: Add support for live streaming (notifications, incremental indexing, ...)
55 public abstract class TmfTrace
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
>, Cloneable
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 // The default number of events to cache
62 // TODO: Make the DEFAULT_CACHE_SIZE a preference
63 public static final int DEFAULT_INDEX_PAGE_SIZE
= 50000;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
73 private String fTraceName
;
75 // The cache page size AND checkpoints interval
76 protected int fIndexPageSize
= DEFAULT_INDEX_PAGE_SIZE
;
78 // The set of event stream checkpoints (for random access)
79 protected Vector
<TmfCheckpoint
> fCheckpoints
= new Vector
<TmfCheckpoint
>();
81 // The number of events collected
82 protected long fNbEvents
= 0;
84 // The time span of the event stream
85 private ITmfTimestamp fStartTime
= TmfTimestamp
.BigCrunch
;
86 private ITmfTimestamp fEndTime
= TmfTimestamp
.BigBang
;
89 private IResource fResource
;
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
100 public void initTrace(String name
, String path
, Class
<T
> eventType
) throws FileNotFoundException
{
101 initTmfTrace(name
, path
, eventType
, DEFAULT_INDEX_PAGE_SIZE
, false);
105 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
) throws FileNotFoundException
{
106 initTmfTrace(name
, path
, eventType
, cacheSize
, false);
110 public void initTrace(String name
, String path
, Class
<T
> eventType
, boolean indexTrace
) throws FileNotFoundException
{
111 initTmfTrace(name
, path
, eventType
, DEFAULT_INDEX_PAGE_SIZE
, indexTrace
);
115 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) throws FileNotFoundException
{
116 initTmfTrace(name
, path
, eventType
, cacheSize
, indexTrace
);
119 private void initTmfTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) throws FileNotFoundException
{
124 if (fTraceName
== null) {
125 fTraceName
= ""; //$NON-NLS-1$
127 int sep
= path
.lastIndexOf(Path
.SEPARATOR
);
128 fTraceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
131 super.init(fTraceName
, eventType
);
132 fIndexPageSize
= (cacheSize
> 0) ? cacheSize
: DEFAULT_INDEX_PAGE_SIZE
;
138 public boolean validate(IProject project
, String path
) {
139 File file
= new File(path
);
140 return file
.exists();
145 * @throws FileNotFoundException
147 protected TmfTrace(String name
, Class
<T
> type
, String path
) throws FileNotFoundException
{
148 this(name
, type
, path
, DEFAULT_INDEX_PAGE_SIZE
, true);
154 * @throws FileNotFoundException
156 protected TmfTrace(String name
, Class
<T
> type
, String path
, int cacheSize
) throws FileNotFoundException
{
157 this(name
, type
, path
, cacheSize
, true);
163 * @throws FileNotFoundException
165 protected TmfTrace(String name
, Class
<T
> type
, String path
, boolean indexTrace
) throws FileNotFoundException
{
166 this(name
, type
, path
, DEFAULT_INDEX_PAGE_SIZE
, indexTrace
);
173 * @throws FileNotFoundException
175 protected TmfTrace(String name
, Class
<T
> type
, String path
, int cacheSize
, boolean indexTrace
) throws FileNotFoundException
{
178 initTrace(name
, path
, type
, cacheSize
, indexTrace
);
181 @SuppressWarnings("unchecked")
183 public TmfTrace
<T
> clone() throws CloneNotSupportedException
{
184 TmfTrace
<T
> clone
= (TmfTrace
<T
>) super.clone();
185 clone
.fCheckpoints
= fCheckpoints
;
186 clone
.fStartTime
= fStartTime
.clone();
187 clone
.fEndTime
= fEndTime
.clone();
191 // ------------------------------------------------------------------------
193 // ------------------------------------------------------------------------
196 * @return the trace path
199 public String
getPath() {
204 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents()
207 public synchronized long getNbEvents() {
212 * @return the size of the cache
215 public int getCacheSize() {
216 return fIndexPageSize
;
220 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange()
223 public TmfTimeRange
getTimeRange() {
224 return new TmfTimeRange(fStartTime
, fEndTime
);
228 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime()
231 public ITmfTimestamp
getStartTime() {
236 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime()
239 public ITmfTimestamp
getEndTime() {
244 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
247 public long getStreamingInterval() {
251 @SuppressWarnings("unchecked")
252 public Vector
<TmfCheckpoint
> getCheckpoints() {
253 return (Vector
<TmfCheckpoint
>) fCheckpoints
.clone();
257 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
264 public long getRank(ITmfTimestamp timestamp
) {
265 TmfContext context
= seekEvent(timestamp
);
266 return context
.getRank();
269 // ------------------------------------------------------------------------
271 // ------------------------------------------------------------------------
273 protected void setTimeRange(TmfTimeRange range
) {
274 fStartTime
= range
.getStartTime();
275 fEndTime
= range
.getEndTime();
278 protected void setStartTime(ITmfTimestamp startTime
) {
279 fStartTime
= startTime
;
282 protected void setEndTime(ITmfTimestamp endTime
) {
286 // ------------------------------------------------------------------------
288 // ------------------------------------------------------------------------
291 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
292 if (request
instanceof ITmfEventRequest
<?
>
293 && !TmfTimestamp
.BigBang
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime()) && request
.getIndex() == 0) {
294 ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
295 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
299 return seekEvent(request
.getIndex());
303 * Return the next piece of data based on the context supplied. The context would typically be updated for the
309 @SuppressWarnings("unchecked")
311 public T
getNext(ITmfContext context
) {
312 if (context
instanceof TmfContext
) {
313 return (T
) getNextEvent((TmfContext
) context
);
318 // ------------------------------------------------------------------------
320 // ------------------------------------------------------------------------
323 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
326 public TmfContext
seekEvent(ITmfTimestamp ts
) {
328 ITmfTimestamp timestamp
= ts
;
329 if (timestamp
== null) {
330 timestamp
= TmfTimestamp
.BigBang
;
333 // First, find the right checkpoint
334 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
336 // In the very likely case that the checkpoint was not found, bsearch
337 // returns its negated would-be location (not an offset...). From that
338 // index, we can then position the stream and get the event.
340 index
= Math
.max(0, -(index
+ 2));
343 // Position the stream at the checkpoint
344 ITmfLocation
<?
> location
;
345 synchronized (fCheckpoints
) {
346 if (!fCheckpoints
.isEmpty()) {
347 if (index
>= fCheckpoints
.size()) {
348 index
= fCheckpoints
.size() - 1;
350 location
= fCheckpoints
.elementAt(index
).getLocation();
355 TmfContext context
= seekLocation(location
);
356 context
.setRank(index
* fIndexPageSize
);
358 // And locate the event
359 TmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
360 ITmfEvent event
= getNextEvent(nextEventContext
);
361 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
362 context
.setLocation(nextEventContext
.getLocation().clone());
363 context
.updateRank(1);
364 event
= getNextEvent(nextEventContext
);
371 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int)
374 public TmfContext
seekEvent(long rank
) {
376 // Position the stream at the previous checkpoint
377 int index
= (int) rank
/ fIndexPageSize
;
378 ITmfLocation
<?
> location
;
379 synchronized (fCheckpoints
) {
380 if (fCheckpoints
.isEmpty()) {
383 if (index
>= fCheckpoints
.size()) {
384 index
= fCheckpoints
.size() - 1;
386 location
= fCheckpoints
.elementAt(index
).getLocation();
390 TmfContext context
= seekLocation(location
);
391 long pos
= index
* fIndexPageSize
;
392 context
.setRank(pos
);
395 ITmfEvent event
= getNextEvent(context
);
396 while (event
!= null && ++pos
< rank
) {
397 event
= getNextEvent(context
);
407 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.
408 * linuxtools.tmf.trace.ITmfTrace.TraceContext)
411 public synchronized ITmfEvent
getNextEvent(TmfContext context
) {
412 // parseEvent() does not update the context
413 ITmfEvent event
= parseEvent(context
);
415 updateIndex(context
, context
.getRank(), event
.getTimestamp());
416 context
.setLocation(getCurrentLocation());
417 context
.updateRank(1);
423 protected synchronized void updateIndex(ITmfContext context
, long rank
, ITmfTimestamp timestamp
) {
424 if (fStartTime
.compareTo(timestamp
, false) > 0)
425 fStartTime
= timestamp
;
426 if (fEndTime
.compareTo(timestamp
, false) < 0)
427 fEndTime
= timestamp
;
428 if (context
.isValidRank()) {
429 if (fNbEvents
<= rank
)
430 fNbEvents
= rank
+ 1;
431 // Build the index as we go along
432 if ((rank
% fIndexPageSize
) == 0) {
433 // Determine the table position
434 long position
= rank
/ fIndexPageSize
;
435 // Add new entry at proper location (if empty)
436 if (fCheckpoints
.size() == position
) {
437 ITmfLocation
<?
> location
= context
.getLocation().clone();
438 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
439 // System.out.println(getName() + "[" + (fCheckpoints.size()
440 // - 1) + "] " + timestamp + ", " + location.toString());
447 * Hook for special processing by the concrete class (called by getNextEvent())
451 protected void processEvent(ITmfEvent event
) {
452 // Do nothing by default
455 // ------------------------------------------------------------------------
457 // ------------------------------------------------------------------------
460 * @see java.lang.Object#toString()
463 @SuppressWarnings("nls")
464 public String
toString() {
465 return "[TmfTrace (" + getName() + ")]";
468 // ------------------------------------------------------------------------
470 // ------------------------------------------------------------------------
473 * The purpose of the index is to keep the information needed to rapidly
474 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
478 @SuppressWarnings({ "unchecked" })
479 protected void indexTrace(boolean waitForCompletion
) {
481 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
483 protected IStatus
run(IProgressMonitor monitor
) {
484 while (!monitor
.isCanceled()) {
487 } catch (InterruptedException e
) {
488 return Status
.OK_STATUS
;
492 return Status
.OK_STATUS
;
497 fCheckpoints
.clear();
498 ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, TmfTimeRange
.Eternity
, TmfDataRequest
.ALL_DATA
,
499 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) {
501 ITmfTimestamp startTime
= null;
502 ITmfTimestamp lastTime
= null;
505 public void handleData(ITmfEvent event
) {
506 super.handleData(event
);
508 ITmfTimestamp ts
= event
.getTimestamp();
509 if (startTime
== null)
510 startTime
= ts
.clone();
511 lastTime
= ts
.clone();
513 if ((getNbRead() % fIndexPageSize
) == 0) {
520 public void handleSuccess() {
525 public void handleCompleted() {
527 super.handleCompleted();
530 private synchronized void updateTrace() {
531 int nbRead
= getNbRead();
533 fStartTime
= startTime
;
541 sendRequest((ITmfDataRequest
<T
>) request
);
542 if (waitForCompletion
)
544 request
.waitForCompletion();
545 } catch (InterruptedException e
) {
546 // e.printStackTrace();
550 protected void notifyListeners() {
551 broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime
, fEndTime
)));
555 * Set the resource to be used for bookmarks on this trace
556 * @param resource the bookmarks resource
558 public void setResource(IResource resource
) {
559 fResource
= resource
;
563 * Get the resource used for bookmarks on this trace
564 * @return the bookmarks resource or null if none is set
566 public IResource
getResource() {