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
.TmfTraceUpdatedSignal
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
48 * <b><u>TmfExperiment</u></b>
50 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces that are part of a tracing experiment.
53 public class TmfExperiment
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 // The currently selected experiment
60 protected static TmfExperiment
<?
> fCurrentExperiment
= null;
62 // The set of traces that constitute the experiment
63 protected ITmfTrace
<T
>[] fTraces
;
65 // The total number of events
66 protected long fNbEvents
;
68 // The experiment time range
69 protected TmfTimeRange fTimeRange
;
71 // The experiment reference timestamp (default: ZERO)
72 protected ITmfTimestamp fEpoch
;
74 // The experiment index
75 protected Vector
<TmfCheckpoint
> fCheckpoints
= new Vector
<TmfCheckpoint
>();
77 // The current experiment context
78 protected TmfExperimentContext fExperimentContext
;
80 // Flag to initialize only once
81 private boolean fInitialized
= false;
83 // The experiment bookmarks file
84 private IFile fBookmarksFile
;
86 // The properties resource
87 private IResource fResource
;
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 public TmfExperiment
<T
> clone() throws CloneNotSupportedException
{
95 throw new CloneNotSupportedException();
99 public boolean validate(IProject project
, String path
) {
104 public void initTrace(String name
, String path
, Class
<T
> eventType
) {
112 * @param indexPageSize
114 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
) {
115 this(type
, id
, traces
, TmfTimestamp
.ZERO
, indexPageSize
, false);
118 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
, boolean preIndexExperiment
) {
123 fIndexPageSize
= indexPageSize
;
124 fTimeRange
= TmfTimeRange
.NULL_RANGE
;
126 if (preIndexExperiment
) {
127 indexExperiment(true, 0, TmfTimeRange
.ETERNITY
);
132 protected TmfExperiment(String id
, Class
<T
> type
) {
141 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
) {
142 this(type
, id
, traces
, TmfTimestamp
.ZERO
, DEFAULT_INDEX_PAGE_SIZE
);
149 * @param indexPageSize
151 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, int indexPageSize
) {
152 this(type
, id
, traces
, TmfTimestamp
.ZERO
, indexPageSize
);
160 @SuppressWarnings("unchecked")
161 public TmfExperiment(TmfExperiment
<T
> other
) {
162 super(other
.getName() + "(clone)", other
.fType
); //$NON-NLS-1$
164 fEpoch
= other
.fEpoch
;
165 fIndexPageSize
= other
.fIndexPageSize
;
167 fTraces
= new ITmfTrace
[other
.fTraces
.length
];
168 for (int trace
= 0; trace
< other
.fTraces
.length
; trace
++) {
169 fTraces
[trace
] = other
.fTraces
[trace
].copy();
172 fNbEvents
= other
.fNbEvents
;
173 fTimeRange
= other
.fTimeRange
;
177 public TmfExperiment
<T
> copy() {
178 TmfExperiment
<T
> experiment
= new TmfExperiment
<T
>(this);
179 TmfSignalManager
.deregister(experiment
);
184 * Clears the experiment
187 @SuppressWarnings("rawtypes")
188 public synchronized void dispose() {
190 TmfExperimentDisposedSignal
<T
> signal
= new TmfExperimentDisposedSignal
<T
>(this, this);
192 if (fCurrentExperiment
== this) {
193 fCurrentExperiment
= null;
196 if (fTraces
!= null) {
197 for (ITmfTrace trace
: fTraces
) {
202 if (fCheckpoints
!= null) {
203 fCheckpoints
.clear();
208 // ------------------------------------------------------------------------
210 // ------------------------------------------------------------------------
213 public long getNbEvents() {
218 public int getIndexPageSize() {
219 return fIndexPageSize
;
223 public TmfTimeRange
getTimeRange() {
228 public ITmfTimestamp
getStartTime() {
229 return fTimeRange
.getStartTime();
233 public ITmfTimestamp
getEndTime() {
234 return fTimeRange
.getEndTime();
237 public Vector
<TmfCheckpoint
> getCheckpoints() {
241 // ------------------------------------------------------------------------
243 // ------------------------------------------------------------------------
245 public static void setCurrentExperiment(TmfExperiment
<?
> experiment
) {
246 if ((fCurrentExperiment
!= null) && (fCurrentExperiment
!= experiment
)) {
247 fCurrentExperiment
.dispose();
249 fCurrentExperiment
= experiment
;
252 public static TmfExperiment
<?
> getCurrentExperiment() {
253 return fCurrentExperiment
;
256 public ITmfTimestamp
getEpoch() {
260 public ITmfTrace
<T
>[] getTraces() {
265 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
268 * @param timestamp the event timestamp
269 * @return the corresponding event rank
272 public long getRank(ITmfTimestamp timestamp
) {
273 TmfExperimentContext context
= seekEvent(timestamp
);
274 return context
.getRank();
278 * Returns the timestamp of the event at the requested index. If none, returns null.
280 * @param index the event index (rank)
281 * @return the corresponding event timestamp
283 public ITmfTimestamp
getTimestamp(int index
) {
284 TmfExperimentContext context
= seekEvent(index
);
285 ITmfEvent event
= getNextEvent(context
);
286 return (event
!= null) ? event
.getTimestamp() : null;
289 // ------------------------------------------------------------------------
291 // ------------------------------------------------------------------------
294 * Update the global time range
296 protected void updateTimeRange() {
297 ITmfTimestamp startTime
= fTimeRange
!= TmfTimeRange
.NULL_RANGE ? fTimeRange
.getStartTime() : TmfTimestamp
.BIG_CRUNCH
;
298 ITmfTimestamp endTime
= fTimeRange
!= TmfTimeRange
.NULL_RANGE ? fTimeRange
.getEndTime() : TmfTimestamp
.BIG_BANG
;
300 for (ITmfTrace
<T
> trace
: fTraces
) {
301 ITmfTimestamp traceStartTime
= trace
.getStartTime();
302 if (traceStartTime
.compareTo(startTime
, true) < 0) {
303 startTime
= traceStartTime
;
305 ITmfTimestamp traceEndTime
= trace
.getEndTime();
306 if (traceEndTime
.compareTo(endTime
, true) > 0) {
307 endTime
= traceEndTime
;
310 fTimeRange
= new TmfTimeRange(startTime
, endTime
);
313 // ------------------------------------------------------------------------
315 // ------------------------------------------------------------------------
317 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
318 // Tracer.trace("Ctx: Arming request - start");
319 ITmfTimestamp timestamp
= (request
instanceof ITmfEventRequest
<?
>) ?
((ITmfEventRequest
<T
>) request
).getRange().getStartTime()
322 if (TmfTimestamp
.BIG_BANG
.equals(timestamp
) || (request
.getIndex() > 0)) {
323 timestamp
= null; // use request index
326 TmfExperimentContext context
= null;
327 if (timestamp
!= null) {
329 context
= seekEvent(timestamp
);
330 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
333 if ((fExperimentContext
!= null) && (fExperimentContext
.getRank() == request
.getIndex())) {
334 // We are already at the right context -> no need to seek
335 context
= fExperimentContext
;
337 context
= seekEvent(request
.getIndex());
340 // Tracer.trace("Ctx: Arming request - done");
344 @SuppressWarnings("unchecked")
346 public T
getNext(ITmfContext context
) {
347 if (context
instanceof TmfExperimentContext
) {
348 return (T
) getNextEvent(context
);
353 // ------------------------------------------------------------------------
354 // ITmfTrace trace positioning
355 // ------------------------------------------------------------------------
357 // Returns a brand new context based on the location provided
358 // and initializes the event queues
360 public synchronized TmfExperimentContext
seekLocation(ITmfLocation
<?
> location
) {
361 // Validate the location
362 if ((location
!= null) && !(location
instanceof TmfExperimentLocation
)) {
363 return null; // Throw an exception?
366 if (fTraces
== null) { // experiment has been disposed
370 // Instantiate the location
371 TmfExperimentLocation expLocation
= (location
== null) ?
new TmfExperimentLocation(new TmfLocationArray(
372 new ITmfLocation
<?
>[fTraces
.length
]), new long[fTraces
.length
]) : (TmfExperimentLocation
) location
.clone();
374 // Create and populate the context's traces contexts
375 TmfExperimentContext context
= new TmfExperimentContext(fTraces
, new ITmfContext
[fTraces
.length
]);
376 // Tracer.trace("Ctx: SeekLocation - start");
379 for (int i
= 0; i
< fTraces
.length
; i
++) {
380 // Get the relevant trace attributes
381 ITmfLocation
<?
> traceLocation
= expLocation
.getLocation().locations
[i
];
382 long traceRank
= expLocation
.getRanks()[i
];
384 // Set the corresponding sub-context
385 context
.getContexts()[i
] = fTraces
[i
].seekLocation(traceLocation
);
386 context
.getContexts()[i
].setRank(traceRank
);
389 // Set the trace location and read the corresponding event
390 /* The (TmfContext) cast should be safe since we created 'context'
391 * ourselves higher up. */
392 expLocation
.getLocation().locations
[i
] = (ITmfLocation
<?
extends Comparable
<?
>>) (context
.getContexts()[i
]).getLocation().clone();
393 context
.getEvents()[i
] = fTraces
[i
].getNextEvent(context
.getContexts()[i
]);
396 // Tracer.trace("Ctx: SeekLocation - done");
399 context
.setLocation(expLocation
);
400 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
401 context
.setRank(rank
);
403 fExperimentContext
= context
;
411 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
414 public synchronized TmfExperimentContext
seekEvent(ITmfTimestamp timestamp
) {
416 // Tracer.trace("Ctx: seekEvent(TS) - start");
418 if (timestamp
== null) {
419 timestamp
= TmfTimestamp
.BIG_BANG
;
422 // First, find the right checkpoint
423 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
425 // In the very likely case that the checkpoint was not found, bsearch
426 // returns its negated would-be location (not an offset...). From that
427 // index, we can then position the stream and get the event.
429 index
= Math
.max(0, -(index
+ 2));
432 // Position the experiment at the checkpoint
433 ITmfLocation
<?
> location
;
434 synchronized (fCheckpoints
) {
435 if (fCheckpoints
.size() > 0) {
436 if (index
>= fCheckpoints
.size()) {
437 index
= fCheckpoints
.size() - 1;
439 location
= fCheckpoints
.elementAt(index
).getLocation();
445 TmfExperimentContext context
= seekLocation(location
);
446 context
.setRank((long) index
* fIndexPageSize
);
448 // And locate the event
449 ITmfEvent event
= parseEvent(context
);
450 while ((event
!= null) && (event
.getTimestamp().compareTo(timestamp
, false) < 0)) {
451 getNextEvent(context
);
452 event
= parseEvent(context
);
456 context
.setLocation(null);
457 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
466 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
469 public synchronized TmfExperimentContext
seekEvent(long rank
) {
471 // Tracer.trace("Ctx: seekEvent(rank) - start");
473 // Position the stream at the previous checkpoint
474 int index
= (int) rank
/ fIndexPageSize
;
475 ITmfLocation
<?
> location
;
476 synchronized (fCheckpoints
) {
477 if (fCheckpoints
.size() == 0) {
480 if (index
>= fCheckpoints
.size()) {
481 index
= fCheckpoints
.size() - 1;
483 location
= fCheckpoints
.elementAt(index
).getLocation();
487 TmfExperimentContext context
= seekLocation(location
);
488 context
.setRank((long) index
* fIndexPageSize
);
490 // And locate the event
491 ITmfEvent event
= parseEvent(context
);
492 long pos
= context
.getRank();
493 while ((event
!= null) && (pos
++ < rank
)) {
494 getNextEvent(context
);
495 event
= parseEvent(context
);
499 context
.setLocation(null);
500 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
507 public TmfContext
seekLocation(double ratio
) {
508 TmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
513 public double getLocationRatio(ITmfLocation
<?
> location
) {
514 if (location
instanceof TmfExperimentLocation
) {
515 return (double) seekLocation(location
).getRank() / getNbEvents();
521 public ITmfLocation
<?
> getCurrentLocation() {
522 if (fExperimentContext
!= null) {
523 return fExperimentContext
.getLocation();
528 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
530 // TmfContext context0 = context.getContexts()[0];
531 // TmfEvent event0 = context.getEvents()[0];
532 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
533 // long rank0 = context.getRank();
534 // int trace = context.getLastTrace();
536 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
538 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
539 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
540 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
541 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
542 // Tracer.trace(result.toString());
546 * Scan the next events from all traces and return the next one in chronological order.
548 * @param context the trace context
549 * @return the next event
551 @SuppressWarnings("unchecked")
553 public synchronized ITmfEvent
getNextEvent(ITmfContext context
) {
555 // Validate the context
556 if (!(context
instanceof TmfExperimentContext
)) {
557 return null; // Throw an exception?
560 if (!context
.equals(fExperimentContext
)) {
561 // Tracer.trace("Ctx: Restoring context");
562 fExperimentContext
= seekLocation(context
.getLocation());
565 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
567 // dumpContext(expContext, true);
569 // If an event was consumed previously, get the next one from that trace
570 int lastTrace
= expContext
.getLastTrace();
571 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
572 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
573 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
574 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
577 // Scan the candidate events and identify the "next" trace to read from
578 ITmfEvent eventArray
[] = expContext
.getEvents();
579 if (eventArray
== null) {
582 int trace
= TmfExperimentContext
.NO_TRACE
;
583 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
584 if (eventArray
.length
== 1) {
585 if (eventArray
[0] != null) {
586 timestamp
= eventArray
[0].getTimestamp();
590 for (int i
= 0; i
< eventArray
.length
; i
++) {
591 ITmfEvent event
= eventArray
[i
];
592 if ((event
!= null) && (event
.getTimestamp() != null)) {
593 ITmfTimestamp otherTS
= event
.getTimestamp();
594 if (otherTS
.compareTo(timestamp
, true) < 0) {
601 // Update the experiment context and set the "next" event
602 ITmfEvent event
= null;
603 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
604 updateIndex(expContext
, timestamp
);
606 ITmfContext traceContext
= expContext
.getContexts()[trace
];
607 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) expContext
.getLocation();
608 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
609 expLocation
.getLocation().locations
[trace
] = (ITmfLocation
<?
extends Comparable
<?
>>) traceContext
.getLocation().clone();
611 // updateIndex(expContext, timestamp);
613 expLocation
.getRanks()[trace
] = traceContext
.getRank();
614 expContext
.setLastTrace(trace
);
615 expContext
.updateRank(1);
616 event
= expContext
.getEvents()[trace
];
617 fExperimentContext
= expContext
;
620 // if (event != null) {
621 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
622 // dumpContext(expContext, false);
623 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
629 public synchronized void updateIndex(ITmfContext context
, ITmfTimestamp timestamp
) {
630 // Build the index as we go along
631 long rank
= context
.getRank();
632 if (context
.isValidRank() && ((rank
% fIndexPageSize
) == 0)) {
633 // Determine the table position
634 long position
= rank
/ fIndexPageSize
;
635 // Add new entry at proper location (if empty)
636 if (fCheckpoints
.size() == position
) {
637 ITmfLocation
<?
> location
= context
.getLocation().clone();
638 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
639 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
640 // + location.toString());
648 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
651 public ITmfEvent
parseEvent(ITmfContext context
) {
653 // Validate the context
654 if (!(context
instanceof TmfExperimentContext
)) {
655 return null; // Throw an exception?
658 if (!context
.equals(fExperimentContext
)) {
659 // Tracer.trace("Ctx: Restoring context");
660 seekLocation(context
.getLocation());
663 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
665 // If an event was consumed previously, get the next one from that trace
666 int lastTrace
= expContext
.getLastTrace();
667 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
668 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
669 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
670 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
671 fExperimentContext
= (TmfExperimentContext
) context
;
674 // Scan the candidate events and identify the "next" trace to read from
675 int trace
= TmfExperimentContext
.NO_TRACE
;
676 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
677 for (int i
= 0; i
< expContext
.getTraces().length
; i
++) {
678 ITmfEvent event
= expContext
.getEvents()[i
];
679 if ((event
!= null) && (event
.getTimestamp() != null)) {
680 ITmfTimestamp otherTS
= event
.getTimestamp();
681 if (otherTS
.compareTo(timestamp
, true) < 0) {
688 ITmfEvent event
= null;
689 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
690 event
= expContext
.getEvents()[trace
];
699 * @see java.lang.Object#toString()
702 @SuppressWarnings("nls")
703 public String
toString() {
704 return "[TmfExperiment (" + getName() + ")]";
707 // ------------------------------------------------------------------------
709 // ------------------------------------------------------------------------
711 private synchronized void initializeStreamingMonitor() {
717 if (getStreamingInterval() == 0) {
718 TmfContext context
= seekLocation(null);
719 ITmfEvent event
= getNext(context
);
723 TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp().clone(), TmfTimestamp
.BIG_CRUNCH
);
724 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
726 // Broadcast in separate thread to prevent deadlock
736 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
737 ITmfTimestamp safeTimestamp
= null;
738 TmfTimeRange timeRange
= null;
742 while (!fExecutor
.isShutdown()) {
743 if (!isIndexingBusy()) {
744 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
745 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
746 for (ITmfTrace
<T
> trace
: fTraces
) {
747 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
748 startTimestamp
= trace
.getStartTime();
750 if ((trace
.getStreamingInterval() != 0) && (trace
.getEndTime().compareTo(endTimestamp
) > 0)) {
751 endTimestamp
= trace
.getEndTime();
754 if ((safeTimestamp
!= null) && (safeTimestamp
.compareTo(getTimeRange().getEndTime(), false) > 0)) {
755 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
759 safeTimestamp
= endTimestamp
;
760 if (timeRange
!= null) {
761 TmfExperimentRangeUpdatedSignal signal
=
762 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
767 Thread
.sleep(getStreamingInterval());
768 } catch (InterruptedException e
) {
778 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
781 public long getStreamingInterval() {
783 for (ITmfTrace
<T
> trace
: fTraces
) {
784 interval
= Math
.max(interval
, trace
.getStreamingInterval());
790 * The experiment holds the globally ordered events of its set of traces. It is expected to provide access to each
791 * individual event by index i.e. it must be possible to request the Nth event of the experiment.
793 * The purpose of the index is to keep the information needed to rapidly restore the traces contexts at regular
794 * intervals (every INDEX_PAGE_SIZE event).
797 // The index page size
798 private static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
799 protected int fIndexPageSize
;
800 protected boolean fIndexing
= false;
801 protected TmfTimeRange fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
803 private Integer fEndSynchReference
;
805 // private static BufferedWriter fEventLog = null;
806 // private static BufferedWriter openLogFile(String filename) {
807 // BufferedWriter outfile = null;
809 // outfile = new BufferedWriter(new FileWriter(filename));
810 // } catch (IOException e) {
811 // e.printStackTrace();
816 protected boolean isIndexingBusy() {
817 synchronized (fCheckpoints
) {
823 public void indexTrace(boolean waitForCompletion
) {
824 if (waitForCompletion
) {
825 initializeStreamingMonitor();
829 @SuppressWarnings("unchecked")
830 private void indexExperiment(boolean waitForCompletion
, final int index
, final TmfTimeRange timeRange
) {
832 synchronized (fCheckpoints
) {
839 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
841 protected IStatus
run(IProgressMonitor monitor
) {
842 while (!monitor
.isCanceled()) {
845 } catch (InterruptedException e
) {
846 return Status
.OK_STATUS
;
850 return Status
.OK_STATUS
;
855 // fEventLog = openLogFile("TraceEvent.log");
856 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
858 ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, timeRange
, index
, TmfDataRequest
.ALL_DATA
,
859 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) { // PATA FOREGROUND
861 // long indexingStart = System.nanoTime();
863 ITmfTimestamp startTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getStartTime();
864 ITmfTimestamp lastTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getEndTime();
865 long initialNbEvents
= fNbEvents
;
868 public void handleStarted() {
869 super.handleStarted();
873 public void handleData(ITmfEvent event
) {
874 super.handleData(event
);
876 ITmfTimestamp ts
= event
.getTimestamp();
877 if (startTime
== null) {
878 startTime
= ts
.clone();
880 lastTime
= ts
.clone();
881 if (((getNbRead() % fIndexPageSize
) == 1) && (getNbRead() != 1)) {
888 public void handleSuccess() {
889 // long indexingEnd = System.nanoTime();
891 // if the end time is a real value then it is the streaming safe time stamp
892 // set the last time to the safe time stamp to prevent unnecessary indexing requests
893 if (getRange().getEndTime() != TmfTimestamp
.BIG_CRUNCH
) {
894 lastTime
= getRange().getEndTime();
897 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
899 // long average = (indexingEnd - indexingStart) / fNbEvents;
900 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
901 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
902 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
903 // + (average % 1000) + " us/evt)");
904 super.handleSuccess();
908 public void handleCompleted() {
910 super.handleCompleted();
911 synchronized (fCheckpoints
) {
913 if (fIndexingPendingRange
!= TmfTimeRange
.NULL_RANGE
) {
914 indexExperiment(false, (int) fNbEvents
, fIndexingPendingRange
);
915 fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
920 private void updateExperiment() {
921 int nbRead
= getNbRead();
922 if (startTime
!= null) {
923 fTimeRange
= new TmfTimeRange(startTime
, lastTime
.clone());
926 // updateTimeRange();
928 fNbEvents
= initialNbEvents
+ nbRead
;
934 sendRequest((ITmfDataRequest
<T
>) request
);
935 if (waitForCompletion
) {
937 request
.waitForCompletion();
938 } catch (InterruptedException e
) {
944 protected void notifyListeners() {
945 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
946 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
949 // ------------------------------------------------------------------------
951 // ------------------------------------------------------------------------
954 public void experimentSelected(TmfExperimentSelectedSignal
<T
> signal
) {
955 TmfExperiment
<?
> experiment
= signal
.getExperiment();
956 if (experiment
== this) {
957 setCurrentExperiment(experiment
);
958 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
963 public void endSync(TmfEndSynchSignal signal
) {
964 if ((fEndSynchReference
!= null) && (fEndSynchReference
.intValue() == signal
.getReference())) {
965 fEndSynchReference
= null;
966 initializeStreamingMonitor();
972 public void experimentUpdated(TmfExperimentUpdatedSignal signal
) {
976 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal
) {
977 if (signal
.getExperiment() == this) {
978 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
983 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
984 for (ITmfTrace
<T
> trace
: fTraces
) {
985 if (trace
== signal
.getTrace()) {
986 synchronized (fCheckpoints
) {
988 if (fIndexingPendingRange
== TmfTimeRange
.NULL_RANGE
) {
989 fIndexingPendingRange
= signal
.getRange();
991 ITmfTimestamp startTime
= fIndexingPendingRange
.getStartTime();
992 ITmfTimestamp endTime
= fIndexingPendingRange
.getEndTime();
993 if (signal
.getRange().getStartTime().compareTo(startTime
) < 0) {
994 startTime
= signal
.getRange().getStartTime();
996 if (signal
.getRange().getEndTime().compareTo(endTime
) > 0) {
997 endTime
= signal
.getRange().getEndTime();
999 fIndexingPendingRange
= new TmfTimeRange(startTime
, endTime
);
1004 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
1011 public String
getPath() {
1012 // TODO Auto-generated method stub
1017 * Set the file to be used for bookmarks on this experiment
1018 * @param file the bookmarks file
1020 public void setBookmarksFile(IFile file
) {
1021 fBookmarksFile
= file
;
1025 * Get the file used for bookmarks on this experiment
1026 * @return the bookmarks file or null if none is set
1028 public IFile
getBookmarksFile() {
1029 return fBookmarksFile
;
1033 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#setResource(org.eclipse.core.resources.IResource)
1036 public void setResource(IResource resource
) {
1037 fResource
= resource
;
1041 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
1044 public IResource
getResource() {