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
.io
.FileNotFoundException
;
16 import java
.util
.Collections
;
17 import java
.util
.Vector
;
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
.ITmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
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 TmfEvent
> 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 resource
85 private IResource fResource
;
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 public boolean validate(IProject project
, String path
) {
97 public void initTrace(String path
, Class
<T
> eventType
) throws FileNotFoundException
{
101 public void initTrace(String path
, Class
<T
> eventType
, boolean indexTrace
) throws FileNotFoundException
{
105 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
) throws FileNotFoundException
{
109 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) throws FileNotFoundException
{
113 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
, String name
) throws FileNotFoundException
{
121 * @param indexPageSize
123 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
) {
124 this(type
, id
, traces
, TmfTimestamp
.Zero
, indexPageSize
, false);
127 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
, boolean preIndexExperiment
) {
132 fIndexPageSize
= indexPageSize
;
133 fTimeRange
= TmfTimeRange
.Null
;
135 if (preIndexExperiment
) {
136 indexExperiment(true);
142 protected TmfExperiment(String id
, Class
<T
> type
) {
151 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
) {
152 this(type
, id
, traces
, TmfTimestamp
.Zero
, DEFAULT_INDEX_PAGE_SIZE
);
159 * @param indexPageSize
161 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, int indexPageSize
) {
162 this(type
, id
, traces
, TmfTimestamp
.Zero
, indexPageSize
);
170 @SuppressWarnings("unchecked")
171 public TmfExperiment(TmfExperiment
<T
> other
) {
172 super(other
.getName() + "(clone)", other
.fType
); //$NON-NLS-1$
174 fEpoch
= other
.fEpoch
;
175 fIndexPageSize
= other
.fIndexPageSize
;
177 fTraces
= new ITmfTrace
[other
.fTraces
.length
];
178 for (int trace
= 0; trace
< other
.fTraces
.length
; trace
++) {
179 fTraces
[trace
] = other
.fTraces
[trace
].copy();
182 fNbEvents
= other
.fNbEvents
;
183 fTimeRange
= other
.fTimeRange
;
187 public TmfExperiment
<T
> copy() {
188 TmfExperiment
<T
> experiment
= new TmfExperiment
<T
>(this);
189 TmfSignalManager
.deregister(experiment
);
194 * Clears the experiment
197 @SuppressWarnings("rawtypes")
198 public synchronized void dispose() {
200 TmfExperimentDisposedSignal
<T
> signal
= new TmfExperimentDisposedSignal
<T
>(this, this);
203 if (fTraces
!= null) {
204 for (ITmfTrace trace
: fTraces
) {
209 if (fCheckpoints
!= null) {
210 fCheckpoints
.clear();
215 // ------------------------------------------------------------------------
217 // ------------------------------------------------------------------------
220 public long getNbEvents() {
225 public int getCacheSize() {
226 return fIndexPageSize
;
230 public TmfTimeRange
getTimeRange() {
235 public ITmfTimestamp
getStartTime() {
236 return fTimeRange
.getStartTime();
240 public ITmfTimestamp
getEndTime() {
241 return fTimeRange
.getEndTime();
244 public Vector
<TmfCheckpoint
> getCheckpoints() {
248 // ------------------------------------------------------------------------
250 // ------------------------------------------------------------------------
252 public static void setCurrentExperiment(TmfExperiment
<?
> experiment
) {
253 fCurrentExperiment
= experiment
;
256 public static TmfExperiment
<?
> getCurrentExperiment() {
257 return fCurrentExperiment
;
260 public ITmfTimestamp
getEpoch() {
264 public ITmfTrace
<T
>[] getTraces() {
269 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
276 public long getRank(ITmfTimestamp timestamp
) {
277 TmfExperimentContext context
= seekEvent(timestamp
);
278 return context
.getRank();
282 * Returns the timestamp of the event at the requested index. If none, returns null.
287 public ITmfTimestamp
getTimestamp(int index
) {
288 TmfExperimentContext context
= seekEvent(index
);
289 TmfEvent event
= getNextEvent(context
);
290 return (event
!= null) ? event
.getTimestamp() : null;
293 // ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
298 * Update the global time range
300 protected void updateTimeRange() {
301 ITmfTimestamp startTime
= fTimeRange
!= TmfTimeRange
.Null ? fTimeRange
.getStartTime() : TmfTimestamp
.BigCrunch
;
302 ITmfTimestamp endTime
= fTimeRange
!= TmfTimeRange
.Null ? fTimeRange
.getEndTime() : TmfTimestamp
.BigBang
;
304 for (ITmfTrace
<T
> trace
: fTraces
) {
305 ITmfTimestamp traceStartTime
= trace
.getStartTime();
306 if (traceStartTime
.compareTo(startTime
, true) < 0)
307 startTime
= traceStartTime
;
308 ITmfTimestamp traceEndTime
= trace
.getEndTime();
309 if (traceEndTime
.compareTo(endTime
, true) > 0)
310 endTime
= traceEndTime
;
312 fTimeRange
= new TmfTimeRange(startTime
, endTime
);
315 // ------------------------------------------------------------------------
317 // ------------------------------------------------------------------------
319 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
320 // Tracer.trace("Ctx: Arming request - start");
321 ITmfTimestamp timestamp
= (request
instanceof ITmfEventRequest
<?
>) ?
((ITmfEventRequest
<T
>) request
).getRange().getStartTime()
324 if (TmfTimestamp
.BigBang
.equals(timestamp
) || request
.getIndex() > 0) {
325 timestamp
= null; // use request index
328 TmfExperimentContext context
= null;
329 if (timestamp
!= null) {
331 context
= seekEvent(timestamp
);
332 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
335 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
336 // We are already at the right context -> no need to seek
337 context
= fExperimentContext
;
339 context
= seekEvent(request
.getIndex());
342 // Tracer.trace("Ctx: Arming request - done");
346 @SuppressWarnings("unchecked")
348 public T
getNext(ITmfContext context
) {
349 if (context
instanceof TmfExperimentContext
) {
350 return (T
) getNextEvent((TmfExperimentContext
) context
);
355 // ------------------------------------------------------------------------
356 // ITmfTrace trace positioning
357 // ------------------------------------------------------------------------
359 // Returns a brand new context based on the location provided
360 // and initializes the event queues
362 public synchronized TmfExperimentContext
seekLocation(ITmfLocation
<?
> location
) {
363 // Validate the location
364 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
365 return null; // Throw an exception?
368 if (fTraces
== null) { // experiment has been disposed
372 // Instantiate the location
373 TmfExperimentLocation expLocation
= (location
== null) ?
new TmfExperimentLocation(new TmfLocationArray(
374 new ITmfLocation
<?
>[fTraces
.length
]), new long[fTraces
.length
]) : (TmfExperimentLocation
) location
.clone();
376 // Create and populate the context's traces contexts
377 TmfExperimentContext context
= new TmfExperimentContext(fTraces
, new TmfContext
[fTraces
.length
]);
378 // Tracer.trace("Ctx: SeekLocation - start");
381 for (int i
= 0; i
< fTraces
.length
; i
++) {
382 // Get the relevant trace attributes
383 ITmfLocation
<?
> traceLocation
= expLocation
.getLocation().locations
[i
];
384 long traceRank
= expLocation
.getRanks()[i
];
386 // Set the corresponding sub-context
387 context
.getContexts()[i
] = fTraces
[i
].seekLocation(traceLocation
);
388 context
.getContexts()[i
].setRank(traceRank
);
391 // Set the trace location and read the corresponding event
392 expLocation
.getLocation().locations
[i
] = 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
.BigBang
;
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 TmfEvent 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 TmfEvent 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();
529 * Scan the next events from all traces and return the next one in chronological order.
535 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
537 // TmfContext context0 = context.getContexts()[0];
538 // TmfEvent event0 = context.getEvents()[0];
539 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
540 // long rank0 = context.getRank();
541 // int trace = context.getLastTrace();
543 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
545 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
546 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
547 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
548 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
549 // Tracer.trace(result.toString());
553 public synchronized TmfEvent
getNextEvent(TmfContext 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 TmfContext 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 TmfEvent eventArray
[] = expContext
.getEvents();
579 if (eventArray
== null) {
582 int trace
= TmfExperimentContext
.NO_TRACE
;
583 ITmfTimestamp timestamp
= TmfTimestamp
.BigCrunch
;
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 TmfEvent 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 TmfEvent event
= null;
603 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
604 updateIndex(expContext
, timestamp
);
606 TmfContext traceContext
= expContext
.getContexts()[trace
];
607 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) expContext
.getLocation();
608 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
609 expLocation
.getLocation().locations
[trace
] = 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 TmfEvent
parseEvent(TmfContext 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 TmfContext 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
.BigCrunch
;
677 for (int i
= 0; i
< expContext
.getTraces().length
; i
++) {
678 TmfEvent event
= expContext
.getEvents()[i
];
679 if (event
!= null && event
.getTimestamp() != null) {
680 ITmfTimestamp otherTS
= event
.getTimestamp();
681 if (otherTS
.compareTo(timestamp
, true) < 0) {
688 TmfEvent 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 TmfEvent event
= getNext(context
);
723 TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp(), TmfTimestamp
.BigCrunch
);
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
.BigCrunch
;
745 ITmfTimestamp endTimestamp
= TmfTimestamp
.BigBang
;
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
;
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
) {
822 protected void indexExperiment(boolean waitForCompletion
) {
823 indexExperiment(waitForCompletion
, 0, TmfTimeRange
.Eternity
);
826 @SuppressWarnings("unchecked")
827 protected void indexExperiment(boolean waitForCompletion
, final int index
, final TmfTimeRange timeRange
) {
829 synchronized (fCheckpoints
) {
836 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
838 protected IStatus
run(IProgressMonitor monitor
) {
839 while (!monitor
.isCanceled()) {
842 } catch (InterruptedException e
) {
843 return Status
.OK_STATUS
;
847 return Status
.OK_STATUS
;
852 // fEventLog = openLogFile("TraceEvent.log");
853 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
855 ITmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, timeRange
, index
, TmfDataRequest
.ALL_DATA
,
856 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) { // PATA FOREGROUND
858 // long indexingStart = System.nanoTime();
860 ITmfTimestamp startTime
= (fTimeRange
== TmfTimeRange
.Null
) ?
null : fTimeRange
.getStartTime();
861 ITmfTimestamp lastTime
= (fTimeRange
== TmfTimeRange
.Null
) ?
null : fTimeRange
.getEndTime();
862 long initialNbEvents
= fNbEvents
;
865 public void handleStarted() {
866 super.handleStarted();
870 public void handleData(TmfEvent event
) {
871 super.handleData(event
);
873 ITmfTimestamp ts
= event
.getTimestamp();
874 if (startTime
== null)
875 startTime
= ts
.clone();
876 lastTime
= ts
.clone();
877 if ((getNbRead() % fIndexPageSize
) == 1 && getNbRead() != 1) {
884 public void handleSuccess() {
885 // long indexingEnd = System.nanoTime();
887 if (getRange() != TmfTimeRange
.Eternity
) {
888 lastTime
= getRange().getEndTime();
891 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
893 // long average = (indexingEnd - indexingStart) / fNbEvents;
894 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
895 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
896 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
897 // + (average % 1000) + " us/evt)");
898 super.handleSuccess();
902 public void handleCompleted() {
904 super.handleCompleted();
905 synchronized (fCheckpoints
) {
907 if (fIndexingPendingRange
!= TmfTimeRange
.Null
) {
908 indexExperiment(false, (int) fNbEvents
, fIndexingPendingRange
);
909 fIndexingPendingRange
= TmfTimeRange
.Null
;
914 private void updateExperiment() {
915 int nbRead
= getNbRead();
916 if (startTime
!= null) {
917 fTimeRange
= new TmfTimeRange(startTime
, lastTime
.clone());
920 // updateTimeRange();
922 fNbEvents
= initialNbEvents
+ nbRead
;
928 sendRequest((ITmfDataRequest
<T
>) request
);
929 if (waitForCompletion
)
931 request
.waitForCompletion();
932 } catch (InterruptedException e
) {
937 protected void notifyListeners() {
938 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
939 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
942 // ------------------------------------------------------------------------
944 // ------------------------------------------------------------------------
947 public void experimentSelected(TmfExperimentSelectedSignal
<T
> signal
) {
948 TmfExperiment
<?
> experiment
= signal
.getExperiment();
949 if (experiment
== this) {
950 setCurrentExperiment(experiment
);
951 fEndSynchReference
= new Integer(signal
.getReference());
956 public void endSync(TmfEndSynchSignal signal
) {
957 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
958 fEndSynchReference
= null;
959 initializeStreamingMonitor();
965 public void experimentUpdated(TmfExperimentUpdatedSignal signal
) {
969 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal
) {
970 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
974 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
975 for (ITmfTrace
<T
> trace
: fTraces
) {
976 if (trace
== signal
.getTrace()) {
977 synchronized (fCheckpoints
) {
979 if (fIndexingPendingRange
== TmfTimeRange
.Null
) {
980 fIndexingPendingRange
= signal
.getRange();
982 ITmfTimestamp startTime
= fIndexingPendingRange
.getStartTime();
983 ITmfTimestamp endTime
= fIndexingPendingRange
.getEndTime();
984 if (signal
.getRange().getStartTime().compareTo(startTime
) < 0) {
985 startTime
= signal
.getRange().getStartTime();
987 if (signal
.getRange().getEndTime().compareTo(endTime
) > 0) {
988 endTime
= signal
.getRange().getEndTime();
990 fIndexingPendingRange
= new TmfTimeRange(startTime
, endTime
);
995 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
1002 public String
getPath() {
1003 // TODO Auto-generated method stub
1008 * Set the resource to be used for bookmarks on this experiment
1009 * @param resource the bookmarks resource
1011 public void setResource(IResource resource
) {
1012 fResource
= resource
;
1016 * Get the resource used for bookmarks on this experiment
1017 * @return the bookmarks resource or null if none is set
1019 public IResource
getResource() {