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
.experiment
;
15 import java
.util
.Collections
;
16 import java
.util
.Vector
;
18 import org
.eclipse
.core
.resources
.IFile
;
19 import org
.eclipse
.core
.resources
.IProject
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Status
;
24 import org
.eclipse
.core
.runtime
.jobs
.Job
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfEndSynchSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentDisposedSignal
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentRangeUpdatedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentUpdatedSignal
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
49 * <b><u>TmfExperiment</u></b>
51 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces that are part of a tracing experiment.
54 public class TmfExperiment
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 // The currently selected experiment
61 protected static TmfExperiment
<?
> fCurrentExperiment
= null;
63 // The set of traces that constitute the experiment
64 protected ITmfTrace
<T
>[] fTraces
;
66 // The total number of events
67 protected long fNbEvents
;
69 // The experiment time range
70 protected TmfTimeRange fTimeRange
;
72 // The experiment reference timestamp (default: Zero)
73 protected ITmfTimestamp fEpoch
;
75 // The experiment index
76 protected Vector
<TmfCheckpoint
> fCheckpoints
= new Vector
<TmfCheckpoint
>();
78 // The current experiment context
79 protected TmfExperimentContext fExperimentContext
;
81 // Flag to initialize only once
82 private boolean fInitialized
= false;
84 // The experiment bookmarks file
85 private IFile fBookmarksFile
;
87 // The properties resource
88 private IResource fResource
;
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 public boolean validate(IProject project
, String path
) {
100 public void initTrace(String name
, String path
, Class
<T
> eventType
) {
104 public void initTrace(String name
, String path
, Class
<T
> eventType
, boolean indexTrace
) {
106 initializeStreamingMonitor();
111 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
) {
115 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) {
117 initializeStreamingMonitor();
126 * @param indexPageSize
128 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
) {
129 this(type
, id
, traces
, TmfTimestamp
.ZERO
, indexPageSize
, false);
132 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
, boolean preIndexExperiment
) {
137 fIndexPageSize
= indexPageSize
;
138 fTimeRange
= TmfTimeRange
.NULL_RANGE
;
140 if (preIndexExperiment
) {
141 indexExperiment(true);
147 protected TmfExperiment(String id
, Class
<T
> type
) {
156 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
) {
157 this(type
, id
, traces
, TmfTimestamp
.ZERO
, DEFAULT_INDEX_PAGE_SIZE
);
164 * @param indexPageSize
166 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, int indexPageSize
) {
167 this(type
, id
, traces
, TmfTimestamp
.ZERO
, indexPageSize
);
175 @SuppressWarnings("unchecked")
176 public TmfExperiment(TmfExperiment
<T
> other
) {
177 super(other
.getName() + "(clone)", other
.fType
); //$NON-NLS-1$
179 fEpoch
= other
.fEpoch
;
180 fIndexPageSize
= other
.fIndexPageSize
;
182 fTraces
= new ITmfTrace
[other
.fTraces
.length
];
183 for (int trace
= 0; trace
< other
.fTraces
.length
; trace
++) {
184 fTraces
[trace
] = other
.fTraces
[trace
].copy();
187 fNbEvents
= other
.fNbEvents
;
188 fTimeRange
= other
.fTimeRange
;
192 public TmfExperiment
<T
> copy() {
193 TmfExperiment
<T
> experiment
= new TmfExperiment
<T
>(this);
194 TmfSignalManager
.deregister(experiment
);
199 * Clears the experiment
202 @SuppressWarnings("rawtypes")
203 public synchronized void dispose() {
205 TmfExperimentDisposedSignal
<T
> signal
= new TmfExperimentDisposedSignal
<T
>(this, this);
207 if (fCurrentExperiment
== this) {
208 fCurrentExperiment
= null;
211 if (fTraces
!= null) {
212 for (ITmfTrace trace
: fTraces
) {
217 if (fCheckpoints
!= null) {
218 fCheckpoints
.clear();
223 // ------------------------------------------------------------------------
225 // ------------------------------------------------------------------------
228 public long getNbEvents() {
233 public int getCacheSize() {
234 return fIndexPageSize
;
238 public TmfTimeRange
getTimeRange() {
243 public ITmfTimestamp
getStartTime() {
244 return fTimeRange
.getStartTime();
248 public ITmfTimestamp
getEndTime() {
249 return fTimeRange
.getEndTime();
252 public Vector
<TmfCheckpoint
> getCheckpoints() {
256 // ------------------------------------------------------------------------
258 // ------------------------------------------------------------------------
260 public static void setCurrentExperiment(TmfExperiment
<?
> experiment
) {
261 if (fCurrentExperiment
!= null && fCurrentExperiment
!= experiment
) {
262 fCurrentExperiment
.dispose();
264 fCurrentExperiment
= experiment
;
267 public static TmfExperiment
<?
> getCurrentExperiment() {
268 return fCurrentExperiment
;
271 public ITmfTimestamp
getEpoch() {
275 public ITmfTrace
<T
>[] getTraces() {
280 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
287 public long getRank(ITmfTimestamp timestamp
) {
288 TmfExperimentContext context
= seekEvent(timestamp
);
289 return context
.getRank();
293 * Returns the timestamp of the event at the requested index. If none, returns null.
298 public ITmfTimestamp
getTimestamp(int index
) {
299 TmfExperimentContext context
= seekEvent(index
);
300 ITmfEvent event
= getNextEvent(context
);
301 return (event
!= null) ? event
.getTimestamp() : null;
304 // ------------------------------------------------------------------------
306 // ------------------------------------------------------------------------
309 * Update the global time range
311 protected void updateTimeRange() {
312 ITmfTimestamp startTime
= fTimeRange
!= TmfTimeRange
.NULL_RANGE ? fTimeRange
.getStartTime() : TmfTimestamp
.BIG_CRUNCH
;
313 ITmfTimestamp endTime
= fTimeRange
!= TmfTimeRange
.NULL_RANGE ? fTimeRange
.getEndTime() : TmfTimestamp
.BIG_BANG
;
315 for (ITmfTrace
<T
> trace
: fTraces
) {
316 ITmfTimestamp traceStartTime
= trace
.getStartTime();
317 if (traceStartTime
.compareTo(startTime
, true) < 0)
318 startTime
= traceStartTime
;
319 ITmfTimestamp traceEndTime
= trace
.getEndTime();
320 if (traceEndTime
.compareTo(endTime
, true) > 0)
321 endTime
= traceEndTime
;
323 fTimeRange
= new TmfTimeRange(startTime
, endTime
);
326 // ------------------------------------------------------------------------
328 // ------------------------------------------------------------------------
330 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
331 // Tracer.trace("Ctx: Arming request - start");
332 ITmfTimestamp timestamp
= (request
instanceof ITmfEventRequest
<?
>) ?
((ITmfEventRequest
<T
>) request
).getRange().getStartTime()
335 if (TmfTimestamp
.BIG_BANG
.equals(timestamp
) || request
.getIndex() > 0) {
336 timestamp
= null; // use request index
339 TmfExperimentContext context
= null;
340 if (timestamp
!= null) {
342 context
= seekEvent(timestamp
);
343 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
346 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
347 // We are already at the right context -> no need to seek
348 context
= fExperimentContext
;
350 context
= seekEvent(request
.getIndex());
353 // Tracer.trace("Ctx: Arming request - done");
357 @SuppressWarnings("unchecked")
359 public T
getNext(ITmfContext context
) {
360 if (context
instanceof TmfExperimentContext
) {
361 return (T
) getNextEvent((TmfExperimentContext
) context
);
366 // ------------------------------------------------------------------------
367 // ITmfTrace trace positioning
368 // ------------------------------------------------------------------------
370 // Returns a brand new context based on the location provided
371 // and initializes the event queues
373 public synchronized TmfExperimentContext
seekLocation(ITmfLocation
<?
> location
) {
374 // Validate the location
375 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
376 return null; // Throw an exception?
379 if (fTraces
== null) { // experiment has been disposed
383 // Instantiate the location
384 TmfExperimentLocation expLocation
= (location
== null) ?
new TmfExperimentLocation(new TmfLocationArray(
385 new ITmfLocation
<?
>[fTraces
.length
]), new long[fTraces
.length
]) : (TmfExperimentLocation
) location
.clone();
387 // Create and populate the context's traces contexts
388 TmfExperimentContext context
= new TmfExperimentContext(fTraces
, new TmfContext
[fTraces
.length
]);
389 // Tracer.trace("Ctx: SeekLocation - start");
392 for (int i
= 0; i
< fTraces
.length
; i
++) {
393 // Get the relevant trace attributes
394 ITmfLocation
<?
> traceLocation
= expLocation
.getLocation().locations
[i
];
395 long traceRank
= expLocation
.getRanks()[i
];
397 // Set the corresponding sub-context
398 context
.getContexts()[i
] = fTraces
[i
].seekLocation(traceLocation
);
399 context
.getContexts()[i
].setRank(traceRank
);
402 // Set the trace location and read the corresponding event
403 /* The (TmfContext) cast should be safe since we created 'context'
404 * ourselves higher up. */
405 expLocation
.getLocation().locations
[i
] = ((TmfContext
) context
.getContexts()[i
]).getLocation().clone();
406 context
.getEvents()[i
] = fTraces
[i
].getNextEvent(context
.getContexts()[i
]);
409 // Tracer.trace("Ctx: SeekLocation - done");
412 context
.setLocation(expLocation
);
413 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
414 context
.setRank(rank
);
416 fExperimentContext
= context
;
424 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
427 public synchronized TmfExperimentContext
seekEvent(ITmfTimestamp timestamp
) {
429 // Tracer.trace("Ctx: seekEvent(TS) - start");
431 if (timestamp
== null) {
432 timestamp
= TmfTimestamp
.BIG_BANG
;
435 // First, find the right checkpoint
436 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
438 // In the very likely case that the checkpoint was not found, bsearch
439 // returns its negated would-be location (not an offset...). From that
440 // index, we can then position the stream and get the event.
442 index
= Math
.max(0, -(index
+ 2));
445 // Position the experiment at the checkpoint
446 ITmfLocation
<?
> location
;
447 synchronized (fCheckpoints
) {
448 if (fCheckpoints
.size() > 0) {
449 if (index
>= fCheckpoints
.size()) {
450 index
= fCheckpoints
.size() - 1;
452 location
= fCheckpoints
.elementAt(index
).getLocation();
458 TmfExperimentContext context
= seekLocation(location
);
459 context
.setRank((long) index
* fIndexPageSize
);
461 // And locate the event
462 ITmfEvent event
= parseEvent(context
);
463 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
464 getNextEvent(context
);
465 event
= parseEvent(context
);
469 context
.setLocation(null);
470 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
479 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
482 public synchronized TmfExperimentContext
seekEvent(long rank
) {
484 // Tracer.trace("Ctx: seekEvent(rank) - start");
486 // Position the stream at the previous checkpoint
487 int index
= (int) rank
/ fIndexPageSize
;
488 ITmfLocation
<?
> location
;
489 synchronized (fCheckpoints
) {
490 if (fCheckpoints
.size() == 0) {
493 if (index
>= fCheckpoints
.size()) {
494 index
= fCheckpoints
.size() - 1;
496 location
= fCheckpoints
.elementAt(index
).getLocation();
500 TmfExperimentContext context
= seekLocation(location
);
501 context
.setRank((long) index
* fIndexPageSize
);
503 // And locate the event
504 ITmfEvent event
= parseEvent(context
);
505 long pos
= context
.getRank();
506 while (event
!= null && pos
++ < rank
) {
507 getNextEvent(context
);
508 event
= parseEvent(context
);
512 context
.setLocation(null);
513 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
520 public TmfContext
seekLocation(double ratio
) {
521 TmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
526 public double getLocationRatio(ITmfLocation
<?
> location
) {
527 if (location
instanceof TmfExperimentLocation
) {
528 return (double) seekLocation(location
).getRank() / getNbEvents();
534 public ITmfLocation
<?
> getCurrentLocation() {
535 if (fExperimentContext
!= null) {
536 return fExperimentContext
.getLocation();
542 * Scan the next events from all traces and return the next one in chronological order.
548 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
550 // TmfContext context0 = context.getContexts()[0];
551 // TmfEvent event0 = context.getEvents()[0];
552 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
553 // long rank0 = context.getRank();
554 // int trace = context.getLastTrace();
556 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
558 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
559 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
560 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
561 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
562 // Tracer.trace(result.toString());
566 public synchronized ITmfEvent
getNextEvent(ITmfContext context
) {
568 // Validate the context
569 if (!(context
instanceof TmfExperimentContext
)) {
570 return null; // Throw an exception?
573 if (!context
.equals(fExperimentContext
)) {
574 // Tracer.trace("Ctx: Restoring context");
575 fExperimentContext
= seekLocation(context
.getLocation());
578 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
580 // dumpContext(expContext, true);
582 // If an event was consumed previously, get the next one from that trace
583 int lastTrace
= expContext
.getLastTrace();
584 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
585 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
586 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
587 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
590 // Scan the candidate events and identify the "next" trace to read from
591 ITmfEvent eventArray
[] = expContext
.getEvents();
592 if (eventArray
== null) {
595 int trace
= TmfExperimentContext
.NO_TRACE
;
596 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
597 if (eventArray
.length
== 1) {
598 if (eventArray
[0] != null) {
599 timestamp
= eventArray
[0].getTimestamp();
603 for (int i
= 0; i
< eventArray
.length
; i
++) {
604 ITmfEvent event
= eventArray
[i
];
605 if (event
!= null && event
.getTimestamp() != null) {
606 ITmfTimestamp otherTS
= event
.getTimestamp();
607 if (otherTS
.compareTo(timestamp
, true) < 0) {
614 // Update the experiment context and set the "next" event
615 ITmfEvent event
= null;
616 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
617 updateIndex(expContext
, timestamp
);
619 ITmfContext traceContext
= expContext
.getContexts()[trace
];
620 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) expContext
.getLocation();
621 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
622 expLocation
.getLocation().locations
[trace
] = (ITmfLocation
<?
extends Comparable
<?
>>) traceContext
.getLocation().clone();
624 // updateIndex(expContext, timestamp);
626 expLocation
.getRanks()[trace
] = traceContext
.getRank();
627 expContext
.setLastTrace(trace
);
628 expContext
.updateRank(1);
629 event
= expContext
.getEvents()[trace
];
630 fExperimentContext
= expContext
;
633 // if (event != null) {
634 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
635 // dumpContext(expContext, false);
636 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
642 public synchronized void updateIndex(ITmfContext context
, ITmfTimestamp timestamp
) {
643 // Build the index as we go along
644 long rank
= context
.getRank();
645 if (context
.isValidRank() && (rank
% fIndexPageSize
) == 0) {
646 // Determine the table position
647 long position
= rank
/ fIndexPageSize
;
648 // Add new entry at proper location (if empty)
649 if (fCheckpoints
.size() == position
) {
650 ITmfLocation
<?
> location
= context
.getLocation().clone();
651 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
652 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
653 // + location.toString());
661 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
664 public ITmfEvent
parseEvent(ITmfContext context
) {
666 // Validate the context
667 if (!(context
instanceof TmfExperimentContext
)) {
668 return null; // Throw an exception?
671 if (!context
.equals(fExperimentContext
)) {
672 // Tracer.trace("Ctx: Restoring context");
673 seekLocation(context
.getLocation());
676 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
678 // If an event was consumed previously, get the next one from that trace
679 int lastTrace
= expContext
.getLastTrace();
680 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
681 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
682 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
683 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
684 fExperimentContext
= (TmfExperimentContext
) context
;
687 // Scan the candidate events and identify the "next" trace to read from
688 int trace
= TmfExperimentContext
.NO_TRACE
;
689 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
690 for (int i
= 0; i
< expContext
.getTraces().length
; i
++) {
691 ITmfEvent event
= expContext
.getEvents()[i
];
692 if (event
!= null && event
.getTimestamp() != null) {
693 ITmfTimestamp otherTS
= event
.getTimestamp();
694 if (otherTS
.compareTo(timestamp
, true) < 0) {
701 ITmfEvent event
= null;
702 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
703 event
= expContext
.getEvents()[trace
];
712 * @see java.lang.Object#toString()
715 @SuppressWarnings("nls")
716 public String
toString() {
717 return "[TmfExperiment (" + getName() + ")]";
720 // ------------------------------------------------------------------------
722 // ------------------------------------------------------------------------
724 private synchronized void initializeStreamingMonitor() {
730 if (getStreamingInterval() == 0) {
731 TmfContext context
= seekLocation(null);
732 ITmfEvent event
= getNext(context
);
736 TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp().clone(), TmfTimestamp
.BIG_CRUNCH
);
737 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
739 // Broadcast in separate thread to prevent deadlock
749 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
750 ITmfTimestamp safeTimestamp
= null;
751 TmfTimeRange timeRange
= null;
755 while (!fExecutor
.isShutdown()) {
756 if (!isIndexingBusy()) {
757 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
758 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
759 for (ITmfTrace
<T
> trace
: fTraces
) {
760 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
761 startTimestamp
= trace
.getStartTime();
763 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
764 endTimestamp
= trace
.getEndTime();
767 if (safeTimestamp
!= null && safeTimestamp
.compareTo(getTimeRange().getEndTime(), false) > 0) {
768 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
772 safeTimestamp
= endTimestamp
;
773 if (timeRange
!= null) {
774 TmfExperimentRangeUpdatedSignal signal
=
775 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
780 Thread
.sleep(getStreamingInterval());
781 } catch (InterruptedException e
) {
791 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
794 public long getStreamingInterval() {
796 for (ITmfTrace
<T
> trace
: fTraces
) {
797 interval
= Math
.max(interval
, trace
.getStreamingInterval());
803 * The experiment holds the globally ordered events of its set of traces. It is expected to provide access to each
804 * individual event by index i.e. it must be possible to request the Nth event of the experiment.
806 * The purpose of the index is to keep the information needed to rapidly restore the traces contexts at regular
807 * intervals (every INDEX_PAGE_SIZE event).
810 // The index page size
811 private static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
812 protected int fIndexPageSize
;
813 protected boolean fIndexing
= false;
814 protected TmfTimeRange fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
816 private Integer fEndSynchReference
;
818 // private static BufferedWriter fEventLog = null;
819 // private static BufferedWriter openLogFile(String filename) {
820 // BufferedWriter outfile = null;
822 // outfile = new BufferedWriter(new FileWriter(filename));
823 // } catch (IOException e) {
824 // e.printStackTrace();
829 protected boolean isIndexingBusy() {
830 synchronized (fCheckpoints
) {
835 protected void indexExperiment(boolean waitForCompletion
) {
836 indexExperiment(waitForCompletion
, 0, TmfTimeRange
.ETERNITY
);
839 @SuppressWarnings("unchecked")
840 protected void indexExperiment(boolean waitForCompletion
, final int index
, final TmfTimeRange timeRange
) {
842 synchronized (fCheckpoints
) {
849 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
851 protected IStatus
run(IProgressMonitor monitor
) {
852 while (!monitor
.isCanceled()) {
855 } catch (InterruptedException e
) {
856 return Status
.OK_STATUS
;
860 return Status
.OK_STATUS
;
865 // fEventLog = openLogFile("TraceEvent.log");
866 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
868 ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, timeRange
, index
, TmfDataRequest
.ALL_DATA
,
869 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) { // PATA FOREGROUND
871 // long indexingStart = System.nanoTime();
873 ITmfTimestamp startTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getStartTime();
874 ITmfTimestamp lastTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getEndTime();
875 long initialNbEvents
= fNbEvents
;
878 public void handleStarted() {
879 super.handleStarted();
883 public void handleData(ITmfEvent event
) {
884 super.handleData(event
);
886 ITmfTimestamp ts
= event
.getTimestamp();
887 if (startTime
== null)
888 startTime
= ts
.clone();
889 lastTime
= ts
.clone();
890 if ((getNbRead() % fIndexPageSize
) == 1 && getNbRead() != 1) {
897 public void handleSuccess() {
898 // long indexingEnd = System.nanoTime();
900 // if the end time is a real value then it is the streaming safe time stamp
901 // set the last time to the safe time stamp to prevent unnecessary indexing requests
902 if (getRange().getEndTime() != TmfTimestamp
.BIG_CRUNCH
) {
903 lastTime
= getRange().getEndTime();
906 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
908 // long average = (indexingEnd - indexingStart) / fNbEvents;
909 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
910 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
911 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
912 // + (average % 1000) + " us/evt)");
913 super.handleSuccess();
917 public void handleCompleted() {
919 super.handleCompleted();
920 synchronized (fCheckpoints
) {
922 if (fIndexingPendingRange
!= TmfTimeRange
.NULL_RANGE
) {
923 indexExperiment(false, (int) fNbEvents
, fIndexingPendingRange
);
924 fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
929 private void updateExperiment() {
930 int nbRead
= getNbRead();
931 if (startTime
!= null) {
932 fTimeRange
= new TmfTimeRange(startTime
, lastTime
.clone());
935 // updateTimeRange();
937 fNbEvents
= initialNbEvents
+ nbRead
;
943 sendRequest((ITmfDataRequest
<T
>) request
);
944 if (waitForCompletion
)
946 request
.waitForCompletion();
947 } catch (InterruptedException e
) {
952 protected void notifyListeners() {
953 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
954 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
957 // ------------------------------------------------------------------------
959 // ------------------------------------------------------------------------
962 public void experimentSelected(TmfExperimentSelectedSignal
<T
> signal
) {
963 TmfExperiment
<?
> experiment
= signal
.getExperiment();
964 if (experiment
== this) {
965 setCurrentExperiment(experiment
);
966 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
971 public void endSync(TmfEndSynchSignal signal
) {
972 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
973 fEndSynchReference
= null;
974 initializeStreamingMonitor();
980 public void experimentUpdated(TmfExperimentUpdatedSignal signal
) {
984 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal
) {
985 if (signal
.getExperiment() == this) {
986 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
991 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
992 for (ITmfTrace
<T
> trace
: fTraces
) {
993 if (trace
== signal
.getTrace()) {
994 synchronized (fCheckpoints
) {
996 if (fIndexingPendingRange
== TmfTimeRange
.NULL_RANGE
) {
997 fIndexingPendingRange
= signal
.getRange();
999 ITmfTimestamp startTime
= fIndexingPendingRange
.getStartTime();
1000 ITmfTimestamp endTime
= fIndexingPendingRange
.getEndTime();
1001 if (signal
.getRange().getStartTime().compareTo(startTime
) < 0) {
1002 startTime
= signal
.getRange().getStartTime();
1004 if (signal
.getRange().getEndTime().compareTo(endTime
) > 0) {
1005 endTime
= signal
.getRange().getEndTime();
1007 fIndexingPendingRange
= new TmfTimeRange(startTime
, endTime
);
1012 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
1019 public String
getPath() {
1020 // TODO Auto-generated method stub
1025 * Set the file to be used for bookmarks on this experiment
1026 * @param file the bookmarks file
1028 public void setBookmarksFile(IFile file
) {
1029 fBookmarksFile
= file
;
1033 * Get the file used for bookmarks on this experiment
1034 * @return the bookmarks file or null if none is set
1036 public IFile
getBookmarksFile() {
1037 return fBookmarksFile
;
1041 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#setResource(org.eclipse.core.resources.IResource)
1044 public void setResource(IResource resource
) {
1045 fResource
= resource
;
1049 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
1052 public IResource
getResource() {