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
.ITmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
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 TmfEvent
> 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 path
, Class
<T
> eventType
) throws FileNotFoundException
{
101 initTmfTrace(path
, eventType
, DEFAULT_INDEX_PAGE_SIZE
, false, null);
105 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
) throws FileNotFoundException
{
106 initTmfTrace(path
, eventType
, cacheSize
, false, null);
110 public void initTrace(String path
, Class
<T
> eventType
, boolean indexTrace
) throws FileNotFoundException
{
111 initTmfTrace(path
, eventType
, DEFAULT_INDEX_PAGE_SIZE
, indexTrace
, null);
115 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) throws FileNotFoundException
{
116 initTmfTrace(path
, eventType
, cacheSize
, indexTrace
, null);
120 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
, String name
) throws FileNotFoundException
{
121 initTmfTrace(path
, eventType
, cacheSize
, indexTrace
, name
);
124 private void initTmfTrace(String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
, String name
) throws FileNotFoundException
{
129 if (fTraceName
== null) {
130 fTraceName
= ""; //$NON-NLS-1$
132 int sep
= path
.lastIndexOf(Path
.SEPARATOR
);
133 fTraceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
136 super.init(fTraceName
, eventType
);
137 fIndexPageSize
= (cacheSize
> 0) ? cacheSize
: DEFAULT_INDEX_PAGE_SIZE
;
143 public boolean validate(IProject project
, String path
) {
144 File file
= new File(path
);
145 return file
.exists();
150 * @throws FileNotFoundException
152 protected TmfTrace(String name
, Class
<T
> type
, String path
) throws FileNotFoundException
{
153 this(name
, type
, path
, DEFAULT_INDEX_PAGE_SIZE
, true);
159 * @throws FileNotFoundException
161 protected TmfTrace(String name
, Class
<T
> type
, String path
, int cacheSize
) throws FileNotFoundException
{
162 this(name
, type
, path
, cacheSize
, true);
168 * @throws FileNotFoundException
170 protected TmfTrace(String name
, Class
<T
> type
, String path
, boolean indexTrace
) throws FileNotFoundException
{
171 this(name
, type
, path
, DEFAULT_INDEX_PAGE_SIZE
, indexTrace
);
178 * @throws FileNotFoundException
180 protected TmfTrace(String name
, Class
<T
> type
, String path
, int cacheSize
, boolean indexTrace
) throws FileNotFoundException
{
183 initTrace(path
, type
, cacheSize
, indexTrace
);
186 @SuppressWarnings("unchecked")
188 public TmfTrace
<T
> clone() throws CloneNotSupportedException
{
189 TmfTrace
<T
> clone
= (TmfTrace
<T
>) super.clone();
190 clone
.fCheckpoints
= fCheckpoints
;
191 clone
.fStartTime
= fStartTime
.clone();
192 clone
.fEndTime
= fEndTime
.clone();
196 // ------------------------------------------------------------------------
198 // ------------------------------------------------------------------------
201 * @return the trace path
204 public String
getPath() {
209 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents()
212 public long getNbEvents() {
217 * @return the size of the cache
220 public int getCacheSize() {
221 return fIndexPageSize
;
225 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange()
228 public TmfTimeRange
getTimeRange() {
229 return new TmfTimeRange(fStartTime
, fEndTime
);
233 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime()
236 public ITmfTimestamp
getStartTime() {
241 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime()
244 public ITmfTimestamp
getEndTime() {
249 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
252 public long getStreamingInterval() {
256 @SuppressWarnings("unchecked")
257 public Vector
<TmfCheckpoint
> getCheckpoints() {
258 return (Vector
<TmfCheckpoint
>) fCheckpoints
.clone();
262 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
269 public long getRank(ITmfTimestamp timestamp
) {
270 TmfContext context
= seekEvent(timestamp
);
271 return context
.getRank();
274 // ------------------------------------------------------------------------
276 // ------------------------------------------------------------------------
278 protected void setTimeRange(TmfTimeRange range
) {
279 fStartTime
= range
.getStartTime();
280 fEndTime
= range
.getEndTime();
283 protected void setStartTime(ITmfTimestamp startTime
) {
284 fStartTime
= startTime
;
287 protected void setEndTime(ITmfTimestamp endTime
) {
291 // ------------------------------------------------------------------------
293 // ------------------------------------------------------------------------
296 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
297 if (request
instanceof ITmfEventRequest
<?
>
298 && !TmfTimestamp
.BigBang
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime()) && request
.getIndex() == 0) {
299 ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
300 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
304 return seekEvent(request
.getIndex());
308 * Return the next piece of data based on the context supplied. The context would typically be updated for the
314 @SuppressWarnings("unchecked")
316 public T
getNext(ITmfContext context
) {
317 if (context
instanceof TmfContext
) {
318 return (T
) getNextEvent((TmfContext
) context
);
323 // ------------------------------------------------------------------------
325 // ------------------------------------------------------------------------
328 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
331 public TmfContext
seekEvent(ITmfTimestamp timestamp
) {
333 if (timestamp
== null) {
334 timestamp
= TmfTimestamp
.BigBang
;
337 // First, find the right checkpoint
338 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
340 // In the very likely case that the checkpoint was not found, bsearch
341 // returns its negated would-be location (not an offset...). From that
342 // index, we can then position the stream and get the event.
344 index
= Math
.max(0, -(index
+ 2));
347 // Position the stream at the checkpoint
348 ITmfLocation
<?
> location
;
349 synchronized (fCheckpoints
) {
350 if (fCheckpoints
.size() > 0) {
351 if (index
>= fCheckpoints
.size()) {
352 index
= fCheckpoints
.size() - 1;
354 location
= fCheckpoints
.elementAt(index
).getLocation();
359 TmfContext context
= seekLocation(location
);
360 context
.setRank(index
* fIndexPageSize
);
362 // And locate the event
363 TmfContext nextEventContext
= context
.clone(); // Must use clone() to get the right subtype...
364 TmfEvent event
= getNextEvent(nextEventContext
);
365 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
366 context
.setLocation(nextEventContext
.getLocation().clone());
367 context
.updateRank(1);
368 event
= getNextEvent(nextEventContext
);
375 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int)
378 public TmfContext
seekEvent(long rank
) {
380 // Position the stream at the previous checkpoint
381 int index
= (int) rank
/ fIndexPageSize
;
382 ITmfLocation
<?
> location
;
383 synchronized (fCheckpoints
) {
384 if (fCheckpoints
.size() == 0) {
387 if (index
>= fCheckpoints
.size()) {
388 index
= fCheckpoints
.size() - 1;
390 location
= fCheckpoints
.elementAt(index
).getLocation();
394 TmfContext context
= seekLocation(location
);
395 long pos
= index
* fIndexPageSize
;
396 context
.setRank(pos
);
399 TmfEvent event
= getNextEvent(context
);
400 while (event
!= null && ++pos
< rank
) {
401 event
= getNextEvent(context
);
411 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.
412 * linuxtools.tmf.trace.ITmfTrace.TraceContext)
415 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
416 // parseEvent() does not update the context
417 TmfEvent event
= parseEvent(context
);
419 updateIndex(context
, context
.getRank(), event
.getTimestamp());
420 context
.setLocation(getCurrentLocation());
421 context
.updateRank(1);
427 protected synchronized void updateIndex(ITmfContext context
, long rank
, ITmfTimestamp timestamp
) {
428 if (fStartTime
.compareTo(timestamp
, false) > 0)
429 fStartTime
= timestamp
;
430 if (fEndTime
.compareTo(timestamp
, false) < 0)
431 fEndTime
= timestamp
;
432 if (context
.isValidRank()) {
433 if (fNbEvents
<= rank
)
434 fNbEvents
= rank
+ 1;
435 // Build the index as we go along
436 if ((rank
% fIndexPageSize
) == 0) {
437 // Determine the table position
438 long position
= rank
/ fIndexPageSize
;
439 // Add new entry at proper location (if empty)
440 if (fCheckpoints
.size() == position
) {
441 ITmfLocation
<?
> location
= context
.getLocation().clone();
442 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
443 // System.out.println(getName() + "[" + (fCheckpoints.size()
444 // - 1) + "] " + timestamp + ", " + location.toString());
451 * Hook for special processing by the concrete class (called by getNextEvent())
455 protected void processEvent(TmfEvent event
) {
456 // Do nothing by default
459 // ------------------------------------------------------------------------
461 // ------------------------------------------------------------------------
464 * @see java.lang.Object#toString()
467 @SuppressWarnings("nls")
468 public String
toString() {
469 return "[TmfTrace (" + getName() + ")]";
472 // ------------------------------------------------------------------------
474 // ------------------------------------------------------------------------
477 * The purpose of the index is to keep the information needed to rapidly
478 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
482 @SuppressWarnings({ "unchecked" })
483 protected void indexTrace(boolean waitForCompletion
) {
485 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
487 protected IStatus
run(IProgressMonitor monitor
) {
488 while (!monitor
.isCanceled()) {
491 } catch (InterruptedException e
) {
492 return Status
.OK_STATUS
;
496 return Status
.OK_STATUS
;
501 fCheckpoints
.clear();
502 ITmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, TmfTimeRange
.Eternity
, TmfDataRequest
.ALL_DATA
,
503 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) {
505 ITmfTimestamp startTime
= null;
506 ITmfTimestamp lastTime
= null;
509 public void handleData(TmfEvent event
) {
510 super.handleData(event
);
512 ITmfTimestamp ts
= event
.getTimestamp();
513 if (startTime
== null)
514 startTime
= ts
.clone();
515 lastTime
= ts
.clone();
517 if ((getNbRead() % fIndexPageSize
) == 0) {
524 public void handleSuccess() {
529 public void handleCompleted() {
531 super.handleCompleted();
534 private void updateTrace() {
535 int nbRead
= getNbRead();
537 fStartTime
= startTime
;
545 sendRequest((ITmfDataRequest
<T
>) request
);
546 if (waitForCompletion
)
548 request
.waitForCompletion();
549 } catch (InterruptedException e
) {
554 protected void notifyListeners() {
555 broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime
, fEndTime
)));
559 * Set the resource to be used for bookmarks on this trace
560 * @param resource the bookmarks resource
562 public void setResource(IResource resource
) {
563 fResource
= resource
;
567 * Get the resource used for bookmarks on this trace
568 * @return the bookmarks resource or null if none is set
570 public IResource
getResource() {