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());
565 @SuppressWarnings("unchecked")
567 public synchronized ITmfEvent
getNextEvent(ITmfContext context
) {
569 // Validate the context
570 if (!(context
instanceof TmfExperimentContext
)) {
571 return null; // Throw an exception?
574 if (!context
.equals(fExperimentContext
)) {
575 // Tracer.trace("Ctx: Restoring context");
576 fExperimentContext
= seekLocation(context
.getLocation());
579 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
581 // dumpContext(expContext, true);
583 // If an event was consumed previously, get the next one from that trace
584 int lastTrace
= expContext
.getLastTrace();
585 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
586 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
587 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
588 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
591 // Scan the candidate events and identify the "next" trace to read from
592 ITmfEvent eventArray
[] = expContext
.getEvents();
593 if (eventArray
== null) {
596 int trace
= TmfExperimentContext
.NO_TRACE
;
597 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
598 if (eventArray
.length
== 1) {
599 if (eventArray
[0] != null) {
600 timestamp
= eventArray
[0].getTimestamp();
604 for (int i
= 0; i
< eventArray
.length
; i
++) {
605 ITmfEvent event
= eventArray
[i
];
606 if (event
!= null && event
.getTimestamp() != null) {
607 ITmfTimestamp otherTS
= event
.getTimestamp();
608 if (otherTS
.compareTo(timestamp
, true) < 0) {
615 // Update the experiment context and set the "next" event
616 ITmfEvent event
= null;
617 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
618 updateIndex(expContext
, timestamp
);
620 ITmfContext traceContext
= expContext
.getContexts()[trace
];
621 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) expContext
.getLocation();
622 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
623 expLocation
.getLocation().locations
[trace
] = (ITmfLocation
<?
extends Comparable
<?
>>) traceContext
.getLocation().clone();
625 // updateIndex(expContext, timestamp);
627 expLocation
.getRanks()[trace
] = traceContext
.getRank();
628 expContext
.setLastTrace(trace
);
629 expContext
.updateRank(1);
630 event
= expContext
.getEvents()[trace
];
631 fExperimentContext
= expContext
;
634 // if (event != null) {
635 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
636 // dumpContext(expContext, false);
637 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
643 public synchronized void updateIndex(ITmfContext context
, ITmfTimestamp timestamp
) {
644 // Build the index as we go along
645 long rank
= context
.getRank();
646 if (context
.isValidRank() && (rank
% fIndexPageSize
) == 0) {
647 // Determine the table position
648 long position
= rank
/ fIndexPageSize
;
649 // Add new entry at proper location (if empty)
650 if (fCheckpoints
.size() == position
) {
651 ITmfLocation
<?
> location
= context
.getLocation().clone();
652 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
653 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
654 // + location.toString());
662 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
665 public ITmfEvent
parseEvent(ITmfContext context
) {
667 // Validate the context
668 if (!(context
instanceof TmfExperimentContext
)) {
669 return null; // Throw an exception?
672 if (!context
.equals(fExperimentContext
)) {
673 // Tracer.trace("Ctx: Restoring context");
674 seekLocation(context
.getLocation());
677 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
679 // If an event was consumed previously, get the next one from that trace
680 int lastTrace
= expContext
.getLastTrace();
681 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
682 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
683 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
684 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
685 fExperimentContext
= (TmfExperimentContext
) context
;
688 // Scan the candidate events and identify the "next" trace to read from
689 int trace
= TmfExperimentContext
.NO_TRACE
;
690 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
691 for (int i
= 0; i
< expContext
.getTraces().length
; i
++) {
692 ITmfEvent event
= expContext
.getEvents()[i
];
693 if (event
!= null && event
.getTimestamp() != null) {
694 ITmfTimestamp otherTS
= event
.getTimestamp();
695 if (otherTS
.compareTo(timestamp
, true) < 0) {
702 ITmfEvent event
= null;
703 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
704 event
= expContext
.getEvents()[trace
];
713 * @see java.lang.Object#toString()
716 @SuppressWarnings("nls")
717 public String
toString() {
718 return "[TmfExperiment (" + getName() + ")]";
721 // ------------------------------------------------------------------------
723 // ------------------------------------------------------------------------
725 private synchronized void initializeStreamingMonitor() {
731 if (getStreamingInterval() == 0) {
732 TmfContext context
= seekLocation(null);
733 ITmfEvent event
= getNext(context
);
737 TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp().clone(), TmfTimestamp
.BIG_CRUNCH
);
738 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
740 // Broadcast in separate thread to prevent deadlock
750 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
751 ITmfTimestamp safeTimestamp
= null;
752 TmfTimeRange timeRange
= null;
756 while (!fExecutor
.isShutdown()) {
757 if (!isIndexingBusy()) {
758 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
759 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
760 for (ITmfTrace
<T
> trace
: fTraces
) {
761 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
762 startTimestamp
= trace
.getStartTime();
764 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
765 endTimestamp
= trace
.getEndTime();
768 if (safeTimestamp
!= null && safeTimestamp
.compareTo(getTimeRange().getEndTime(), false) > 0) {
769 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
773 safeTimestamp
= endTimestamp
;
774 if (timeRange
!= null) {
775 TmfExperimentRangeUpdatedSignal signal
=
776 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
781 Thread
.sleep(getStreamingInterval());
782 } catch (InterruptedException e
) {
792 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
795 public long getStreamingInterval() {
797 for (ITmfTrace
<T
> trace
: fTraces
) {
798 interval
= Math
.max(interval
, trace
.getStreamingInterval());
804 * The experiment holds the globally ordered events of its set of traces. It is expected to provide access to each
805 * individual event by index i.e. it must be possible to request the Nth event of the experiment.
807 * The purpose of the index is to keep the information needed to rapidly restore the traces contexts at regular
808 * intervals (every INDEX_PAGE_SIZE event).
811 // The index page size
812 private static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
813 protected int fIndexPageSize
;
814 protected boolean fIndexing
= false;
815 protected TmfTimeRange fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
817 private Integer fEndSynchReference
;
819 // private static BufferedWriter fEventLog = null;
820 // private static BufferedWriter openLogFile(String filename) {
821 // BufferedWriter outfile = null;
823 // outfile = new BufferedWriter(new FileWriter(filename));
824 // } catch (IOException e) {
825 // e.printStackTrace();
830 protected boolean isIndexingBusy() {
831 synchronized (fCheckpoints
) {
836 protected void indexExperiment(boolean waitForCompletion
) {
837 indexExperiment(waitForCompletion
, 0, TmfTimeRange
.ETERNITY
);
840 @SuppressWarnings("unchecked")
841 protected void indexExperiment(boolean waitForCompletion
, final int index
, final TmfTimeRange timeRange
) {
843 synchronized (fCheckpoints
) {
850 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
852 protected IStatus
run(IProgressMonitor monitor
) {
853 while (!monitor
.isCanceled()) {
856 } catch (InterruptedException e
) {
857 return Status
.OK_STATUS
;
861 return Status
.OK_STATUS
;
866 // fEventLog = openLogFile("TraceEvent.log");
867 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
869 ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, timeRange
, index
, TmfDataRequest
.ALL_DATA
,
870 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) { // PATA FOREGROUND
872 // long indexingStart = System.nanoTime();
874 ITmfTimestamp startTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getStartTime();
875 ITmfTimestamp lastTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getEndTime();
876 long initialNbEvents
= fNbEvents
;
879 public void handleStarted() {
880 super.handleStarted();
884 public void handleData(ITmfEvent event
) {
885 super.handleData(event
);
887 ITmfTimestamp ts
= event
.getTimestamp();
888 if (startTime
== null)
889 startTime
= ts
.clone();
890 lastTime
= ts
.clone();
891 if ((getNbRead() % fIndexPageSize
) == 1 && getNbRead() != 1) {
898 public void handleSuccess() {
899 // long indexingEnd = System.nanoTime();
901 // if the end time is a real value then it is the streaming safe time stamp
902 // set the last time to the safe time stamp to prevent unnecessary indexing requests
903 if (getRange().getEndTime() != TmfTimestamp
.BIG_CRUNCH
) {
904 lastTime
= getRange().getEndTime();
907 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
909 // long average = (indexingEnd - indexingStart) / fNbEvents;
910 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
911 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
912 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
913 // + (average % 1000) + " us/evt)");
914 super.handleSuccess();
918 public void handleCompleted() {
920 super.handleCompleted();
921 synchronized (fCheckpoints
) {
923 if (fIndexingPendingRange
!= TmfTimeRange
.NULL_RANGE
) {
924 indexExperiment(false, (int) fNbEvents
, fIndexingPendingRange
);
925 fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
930 private void updateExperiment() {
931 int nbRead
= getNbRead();
932 if (startTime
!= null) {
933 fTimeRange
= new TmfTimeRange(startTime
, lastTime
.clone());
936 // updateTimeRange();
938 fNbEvents
= initialNbEvents
+ nbRead
;
944 sendRequest((ITmfDataRequest
<T
>) request
);
945 if (waitForCompletion
)
947 request
.waitForCompletion();
948 } catch (InterruptedException e
) {
953 protected void notifyListeners() {
954 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
955 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
958 // ------------------------------------------------------------------------
960 // ------------------------------------------------------------------------
963 public void experimentSelected(TmfExperimentSelectedSignal
<T
> signal
) {
964 TmfExperiment
<?
> experiment
= signal
.getExperiment();
965 if (experiment
== this) {
966 setCurrentExperiment(experiment
);
967 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
972 public void endSync(TmfEndSynchSignal signal
) {
973 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
974 fEndSynchReference
= null;
975 initializeStreamingMonitor();
981 public void experimentUpdated(TmfExperimentUpdatedSignal signal
) {
985 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal
) {
986 if (signal
.getExperiment() == this) {
987 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
992 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
993 for (ITmfTrace
<T
> trace
: fTraces
) {
994 if (trace
== signal
.getTrace()) {
995 synchronized (fCheckpoints
) {
997 if (fIndexingPendingRange
== TmfTimeRange
.NULL_RANGE
) {
998 fIndexingPendingRange
= signal
.getRange();
1000 ITmfTimestamp startTime
= fIndexingPendingRange
.getStartTime();
1001 ITmfTimestamp endTime
= fIndexingPendingRange
.getEndTime();
1002 if (signal
.getRange().getStartTime().compareTo(startTime
) < 0) {
1003 startTime
= signal
.getRange().getStartTime();
1005 if (signal
.getRange().getEndTime().compareTo(endTime
) > 0) {
1006 endTime
= signal
.getRange().getEndTime();
1008 fIndexingPendingRange
= new TmfTimeRange(startTime
, endTime
);
1013 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
1020 public String
getPath() {
1021 // TODO Auto-generated method stub
1026 * Set the file to be used for bookmarks on this experiment
1027 * @param file the bookmarks file
1029 public void setBookmarksFile(IFile file
) {
1030 fBookmarksFile
= file
;
1034 * Get the file used for bookmarks on this experiment
1035 * @return the bookmarks file or null if none is set
1037 public IFile
getBookmarksFile() {
1038 return fBookmarksFile
;
1042 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#setResource(org.eclipse.core.resources.IResource)
1045 public void setResource(IResource resource
) {
1046 fResource
= resource
;
1050 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
1053 public IResource
getResource() {