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
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.core
.runtime
.jobs
.Job
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfEndSynchSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentDisposedSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentRangeUpdatedSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentUpdatedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
47 * <b><u>TmfExperiment</u></b>
49 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces that are part of a tracing experiment.
52 public class TmfExperiment
<T
extends TmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 // The currently selected experiment
59 protected static TmfExperiment
<?
> fCurrentExperiment
= null;
61 // The set of traces that constitute the experiment
62 protected ITmfTrace
<T
>[] fTraces
;
64 // The total number of events
65 protected long fNbEvents
;
67 // The experiment time range
68 protected TmfTimeRange fTimeRange
;
70 // The experiment reference timestamp (default: Zero)
71 protected TmfTimestamp fEpoch
;
73 // The experiment index
74 protected Vector
<TmfCheckpoint
> fCheckpoints
= new Vector
<TmfCheckpoint
>();
76 // The current experiment context
77 protected TmfExperimentContext fExperimentContext
;
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 public boolean validate(IProject project
, String path
) {
89 public void initTrace(String path
, Class
<T
> eventType
) throws FileNotFoundException
{
93 public void initTrace(String path
, Class
<T
> eventType
, boolean indexTrace
) throws FileNotFoundException
{
97 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
) throws FileNotFoundException
{
101 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) throws FileNotFoundException
{
105 public void initTrace(String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
, String name
) throws FileNotFoundException
{
113 * @param indexPageSize
115 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, TmfTimestamp epoch
, int indexPageSize
) {
116 this(type
, id
, traces
, TmfTimestamp
.Zero
, indexPageSize
, false);
119 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, TmfTimestamp epoch
, int indexPageSize
, boolean preIndexExperiment
) {
124 fIndexPageSize
= indexPageSize
;
125 fTimeRange
= TmfTimeRange
.Null
;
127 if (preIndexExperiment
) {
128 indexExperiment(true);
134 protected TmfExperiment(String id
, Class
<T
> type
) {
143 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
) {
144 this(type
, id
, traces
, TmfTimestamp
.Zero
, DEFAULT_INDEX_PAGE_SIZE
);
151 * @param indexPageSize
153 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, int indexPageSize
) {
154 this(type
, id
, traces
, TmfTimestamp
.Zero
, indexPageSize
);
162 @SuppressWarnings("unchecked")
163 public TmfExperiment(TmfExperiment
<T
> other
) {
164 super(other
.getName() + "(clone)", other
.fType
); //$NON-NLS-1$
166 fEpoch
= other
.fEpoch
;
167 fIndexPageSize
= other
.fIndexPageSize
;
169 fTraces
= new ITmfTrace
[other
.fTraces
.length
];
170 for (int trace
= 0; trace
< other
.fTraces
.length
; trace
++) {
171 fTraces
[trace
] = other
.fTraces
[trace
].copy();
174 fNbEvents
= other
.fNbEvents
;
175 fTimeRange
= other
.fTimeRange
;
179 public TmfExperiment
<T
> copy() {
180 TmfExperiment
<T
> experiment
= new TmfExperiment
<T
>(this);
181 TmfSignalManager
.deregister(experiment
);
186 * Clears the experiment
189 @SuppressWarnings("rawtypes")
190 public synchronized void dispose() {
192 TmfExperimentDisposedSignal
<T
> signal
= new TmfExperimentDisposedSignal
<T
>(this, this);
195 if (fTraces
!= null) {
196 for (ITmfTrace trace
: fTraces
) {
201 if (fCheckpoints
!= null) {
202 fCheckpoints
.clear();
207 // ------------------------------------------------------------------------
209 // ------------------------------------------------------------------------
212 public long getNbEvents() {
217 public int getCacheSize() {
218 return fIndexPageSize
;
222 public TmfTimeRange
getTimeRange() {
227 public TmfTimestamp
getStartTime() {
228 return fTimeRange
.getStartTime();
232 public TmfTimestamp
getEndTime() {
233 return fTimeRange
.getEndTime();
236 public Vector
<TmfCheckpoint
> getCheckpoints() {
240 // ------------------------------------------------------------------------
242 // ------------------------------------------------------------------------
244 public static void setCurrentExperiment(TmfExperiment
<?
> experiment
) {
245 fCurrentExperiment
= experiment
;
248 public static TmfExperiment
<?
> getCurrentExperiment() {
249 return fCurrentExperiment
;
252 public TmfTimestamp
getEpoch() {
256 public ITmfTrace
<T
>[] getTraces() {
261 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
268 public long getRank(TmfTimestamp timestamp
) {
269 TmfExperimentContext context
= seekEvent(timestamp
);
270 return context
.getRank();
274 * Returns the timestamp of the event at the requested index. If none, returns null.
279 public TmfTimestamp
getTimestamp(int index
) {
280 TmfExperimentContext context
= seekEvent(index
);
281 TmfEvent event
= getNextEvent(context
);
282 return (event
!= null) ? event
.getTimestamp() : null;
285 // ------------------------------------------------------------------------
287 // ------------------------------------------------------------------------
290 * Update the global time range
292 protected void updateTimeRange() {
293 TmfTimestamp startTime
= fTimeRange
!= TmfTimeRange
.Null ? fTimeRange
.getStartTime() : TmfTimestamp
.BigCrunch
;
294 TmfTimestamp endTime
= fTimeRange
!= TmfTimeRange
.Null ? fTimeRange
.getEndTime() : TmfTimestamp
.BigBang
;
296 for (ITmfTrace
<T
> trace
: fTraces
) {
297 TmfTimestamp traceStartTime
= trace
.getStartTime();
298 if (traceStartTime
.compareTo(startTime
, true) < 0)
299 startTime
= traceStartTime
;
300 TmfTimestamp traceEndTime
= trace
.getEndTime();
301 if (traceEndTime
.compareTo(endTime
, true) > 0)
302 endTime
= traceEndTime
;
304 fTimeRange
= new TmfTimeRange(startTime
, endTime
);
307 // ------------------------------------------------------------------------
309 // ------------------------------------------------------------------------
311 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
312 // Tracer.trace("Ctx: Arming request - start");
313 TmfTimestamp timestamp
= (request
instanceof ITmfEventRequest
<?
>) ?
((ITmfEventRequest
<T
>) request
).getRange().getStartTime()
316 if (TmfTimestamp
.BigBang
.equals(timestamp
) || request
.getIndex() > 0) {
317 timestamp
= null; // use request index
320 TmfExperimentContext context
= null;
321 if (timestamp
!= null) {
323 context
= seekEvent(timestamp
);
324 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
327 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
328 // We are already at the right context -> no need to seek
329 context
= fExperimentContext
;
331 context
= seekEvent(request
.getIndex());
334 // Tracer.trace("Ctx: Arming request - done");
338 @SuppressWarnings("unchecked")
340 public T
getNext(ITmfContext context
) {
341 if (context
instanceof TmfExperimentContext
) {
342 return (T
) getNextEvent((TmfExperimentContext
) context
);
347 // ------------------------------------------------------------------------
348 // ITmfTrace trace positioning
349 // ------------------------------------------------------------------------
351 // Returns a brand new context based on the location provided
352 // and initializes the event queues
354 public synchronized TmfExperimentContext
seekLocation(ITmfLocation
<?
> location
) {
355 // Validate the location
356 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
357 return null; // Throw an exception?
360 if (fTraces
== null) { // experiment has been disposed
364 // Instantiate the location
365 TmfExperimentLocation expLocation
= (location
== null) ?
new TmfExperimentLocation(new TmfLocationArray(
366 new ITmfLocation
<?
>[fTraces
.length
]), new long[fTraces
.length
]) : (TmfExperimentLocation
) location
.clone();
368 // Create and populate the context's traces contexts
369 TmfExperimentContext context
= new TmfExperimentContext(fTraces
, new TmfContext
[fTraces
.length
]);
370 // Tracer.trace("Ctx: SeekLocation - start");
373 for (int i
= 0; i
< fTraces
.length
; i
++) {
374 // Get the relevant trace attributes
375 ITmfLocation
<?
> traceLocation
= expLocation
.getLocation().locations
[i
];
376 long traceRank
= expLocation
.getRanks()[i
];
378 // Set the corresponding sub-context
379 context
.getContexts()[i
] = fTraces
[i
].seekLocation(traceLocation
);
380 context
.getContexts()[i
].setRank(traceRank
);
383 // Set the trace location and read the corresponding event
384 expLocation
.getLocation().locations
[i
] = context
.getContexts()[i
].getLocation().clone();
385 context
.getEvents()[i
] = fTraces
[i
].getNextEvent(context
.getContexts()[i
]);
388 // Tracer.trace("Ctx: SeekLocation - done");
391 context
.setLocation(expLocation
);
392 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
393 context
.setRank(rank
);
395 fExperimentContext
= context
;
403 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
406 public synchronized TmfExperimentContext
seekEvent(TmfTimestamp timestamp
) {
408 // Tracer.trace("Ctx: seekEvent(TS) - start");
410 if (timestamp
== null) {
411 timestamp
= TmfTimestamp
.BigBang
;
414 // First, find the right checkpoint
415 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
417 // In the very likely case that the checkpoint was not found, bsearch
418 // returns its negated would-be location (not an offset...). From that
419 // index, we can then position the stream and get the event.
421 index
= Math
.max(0, -(index
+ 2));
424 // Position the experiment at the checkpoint
425 ITmfLocation
<?
> location
;
426 synchronized (fCheckpoints
) {
427 if (fCheckpoints
.size() > 0) {
428 if (index
>= fCheckpoints
.size()) {
429 index
= fCheckpoints
.size() - 1;
431 location
= fCheckpoints
.elementAt(index
).getLocation();
437 TmfExperimentContext context
= seekLocation(location
);
438 context
.setRank((long) index
* fIndexPageSize
);
440 // And locate the event
441 TmfEvent event
= parseEvent(context
);
442 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
443 getNextEvent(context
);
444 event
= parseEvent(context
);
448 context
.setLocation(null);
449 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
458 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
461 public synchronized TmfExperimentContext
seekEvent(long rank
) {
463 // Tracer.trace("Ctx: seekEvent(rank) - start");
465 // Position the stream at the previous checkpoint
466 int index
= (int) rank
/ fIndexPageSize
;
467 ITmfLocation
<?
> location
;
468 synchronized (fCheckpoints
) {
469 if (fCheckpoints
.size() == 0) {
472 if (index
>= fCheckpoints
.size()) {
473 index
= fCheckpoints
.size() - 1;
475 location
= fCheckpoints
.elementAt(index
).getLocation();
479 TmfExperimentContext context
= seekLocation(location
);
480 context
.setRank((long) index
* fIndexPageSize
);
482 // And locate the event
483 TmfEvent event
= parseEvent(context
);
484 long pos
= context
.getRank();
485 while (event
!= null && pos
++ < rank
) {
486 getNextEvent(context
);
487 event
= parseEvent(context
);
491 context
.setLocation(null);
492 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
499 public TmfContext
seekLocation(double ratio
) {
500 TmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
505 public double getLocationRatio(ITmfLocation
<?
> location
) {
506 if (location
instanceof TmfExperimentLocation
) {
507 return (double) seekLocation(location
).getRank() / getNbEvents();
513 public ITmfLocation
<?
> getCurrentLocation() {
514 if (fExperimentContext
!= null) {
515 return fExperimentContext
.getLocation();
521 * Scan the next events from all traces and return the next one in chronological order.
527 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
529 // TmfContext context0 = context.getContexts()[0];
530 // TmfEvent event0 = context.getEvents()[0];
531 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
532 // long rank0 = context.getRank();
533 // int trace = context.getLastTrace();
535 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
537 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
538 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
539 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
540 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
541 // Tracer.trace(result.toString());
545 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
547 // Validate the context
548 if (!(context
instanceof TmfExperimentContext
)) {
549 return null; // Throw an exception?
552 if (!context
.equals(fExperimentContext
)) {
553 // Tracer.trace("Ctx: Restoring context");
554 fExperimentContext
= seekLocation(context
.getLocation());
557 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
559 // dumpContext(expContext, true);
561 // If an event was consumed previously, get the next one from that trace
562 int lastTrace
= expContext
.getLastTrace();
563 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
564 TmfContext traceContext
= expContext
.getContexts()[lastTrace
];
565 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
566 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
569 // Scan the candidate events and identify the "next" trace to read from
570 TmfEvent eventArray
[] = expContext
.getEvents();
571 if (eventArray
== null) {
574 int trace
= TmfExperimentContext
.NO_TRACE
;
575 TmfTimestamp timestamp
= TmfTimestamp
.BigCrunch
;
576 if (eventArray
.length
== 1) {
577 if (eventArray
[0] != null) {
578 timestamp
= eventArray
[0].getTimestamp();
582 for (int i
= 0; i
< eventArray
.length
; i
++) {
583 TmfEvent event
= eventArray
[i
];
584 if (event
!= null && event
.getTimestamp() != null) {
585 TmfTimestamp otherTS
= event
.getTimestamp();
586 if (otherTS
.compareTo(timestamp
, true) < 0) {
593 // Update the experiment context and set the "next" event
594 TmfEvent event
= null;
595 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
596 updateIndex(expContext
, timestamp
);
598 TmfContext traceContext
= expContext
.getContexts()[trace
];
599 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) expContext
.getLocation();
600 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
601 expLocation
.getLocation().locations
[trace
] = traceContext
.getLocation().clone();
603 // updateIndex(expContext, timestamp);
605 expLocation
.getRanks()[trace
] = traceContext
.getRank();
606 expContext
.setLastTrace(trace
);
607 expContext
.updateRank(1);
608 event
= expContext
.getEvents()[trace
];
609 fExperimentContext
= expContext
;
612 // if (event != null) {
613 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
614 // dumpContext(expContext, false);
615 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
621 public synchronized void updateIndex(ITmfContext context
, TmfTimestamp timestamp
) {
622 // Build the index as we go along
623 long rank
= context
.getRank();
624 if (context
.isValidRank() && (rank
% fIndexPageSize
) == 0) {
625 // Determine the table position
626 long position
= rank
/ fIndexPageSize
;
627 // Add new entry at proper location (if empty)
628 if (fCheckpoints
.size() == position
) {
629 ITmfLocation
<?
> location
= context
.getLocation().clone();
630 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
631 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
632 // + location.toString());
640 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
643 public TmfEvent
parseEvent(TmfContext context
) {
645 // Validate the context
646 if (!(context
instanceof TmfExperimentContext
)) {
647 return null; // Throw an exception?
650 if (!context
.equals(fExperimentContext
)) {
651 // Tracer.trace("Ctx: Restoring context");
652 seekLocation(context
.getLocation());
655 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
657 // If an event was consumed previously, get the next one from that trace
658 int lastTrace
= expContext
.getLastTrace();
659 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
660 TmfContext traceContext
= expContext
.getContexts()[lastTrace
];
661 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
662 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
663 fExperimentContext
= (TmfExperimentContext
) context
;
666 // Scan the candidate events and identify the "next" trace to read from
667 int trace
= TmfExperimentContext
.NO_TRACE
;
668 TmfTimestamp timestamp
= TmfTimestamp
.BigCrunch
;
669 for (int i
= 0; i
< expContext
.getTraces().length
; i
++) {
670 TmfEvent event
= expContext
.getEvents()[i
];
671 if (event
!= null && event
.getTimestamp() != null) {
672 TmfTimestamp otherTS
= event
.getTimestamp();
673 if (otherTS
.compareTo(timestamp
, true) < 0) {
680 TmfEvent event
= null;
681 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
682 event
= expContext
.getEvents()[trace
];
691 * @see java.lang.Object#toString()
694 @SuppressWarnings("nls")
695 public String
toString() {
696 return "[TmfExperiment (" + getName() + ")]";
699 // ------------------------------------------------------------------------
701 // ------------------------------------------------------------------------
703 private synchronized void initializeStreamingMonitor() {
704 if (getStreamingInterval() == 0) {
705 TmfContext context
= seekLocation(null);
706 TmfEvent event
= getNext(context
);
710 TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp(), TmfTimestamp
.BigCrunch
);
711 TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
716 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
717 TmfTimestamp safeTimestamp
= null;
718 TmfTimeRange timeRange
= null;
722 while (!fExecutor
.isShutdown()) {
723 if (!isIndexingBusy()) {
724 TmfTimestamp startTimestamp
= TmfTimestamp
.BigCrunch
;
725 TmfTimestamp endTimestamp
= TmfTimestamp
.BigBang
;
726 for (ITmfTrace
<T
> trace
: fTraces
) {
727 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
728 startTimestamp
= trace
.getStartTime();
730 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
731 endTimestamp
= trace
.getEndTime();
734 if (safeTimestamp
!= null && safeTimestamp
.compareTo(getTimeRange().getEndTime(), false) > 0) {
735 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
739 safeTimestamp
= endTimestamp
;
740 if (timeRange
!= null) {
741 TmfExperimentRangeUpdatedSignal signal
=
742 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
747 Thread
.sleep(getStreamingInterval());
748 } catch (InterruptedException e
) {
758 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
761 public long getStreamingInterval() {
763 for (ITmfTrace
<T
> trace
: fTraces
) {
764 interval
= Math
.max(interval
, trace
.getStreamingInterval());
770 * The experiment holds the globally ordered events of its set of traces. It is expected to provide access to each
771 * individual event by index i.e. it must be possible to request the Nth event of the experiment.
773 * The purpose of the index is to keep the information needed to rapidly restore the traces contexts at regular
774 * intervals (every INDEX_PAGE_SIZE event).
777 // The index page size
778 private static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
779 protected int fIndexPageSize
;
780 protected boolean fIndexing
= false;
781 protected TmfTimeRange fIndexingPendingRange
= TmfTimeRange
.Null
;
783 private Integer fEndSynchReference
;
785 // private static BufferedWriter fEventLog = null;
786 // private static BufferedWriter openLogFile(String filename) {
787 // BufferedWriter outfile = null;
789 // outfile = new BufferedWriter(new FileWriter(filename));
790 // } catch (IOException e) {
791 // e.printStackTrace();
796 protected boolean isIndexingBusy() {
797 synchronized (fCheckpoints
) {
802 protected void indexExperiment(boolean waitForCompletion
) {
803 indexExperiment(waitForCompletion
, 0, TmfTimeRange
.Eternity
);
806 @SuppressWarnings("unchecked")
807 protected void indexExperiment(boolean waitForCompletion
, final int index
, final TmfTimeRange timeRange
) {
809 synchronized (fCheckpoints
) {
816 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
818 protected IStatus
run(IProgressMonitor monitor
) {
819 while (!monitor
.isCanceled()) {
822 } catch (InterruptedException e
) {
823 return Status
.OK_STATUS
;
827 return Status
.OK_STATUS
;
832 // fEventLog = openLogFile("TraceEvent.log");
833 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
835 ITmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, timeRange
, index
, TmfDataRequest
.ALL_DATA
,
836 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) { // PATA FOREGROUND
838 // long indexingStart = System.nanoTime();
840 TmfTimestamp startTime
= (fTimeRange
== TmfTimeRange
.Null
) ?
null : fTimeRange
.getStartTime();
841 TmfTimestamp lastTime
= (fTimeRange
== TmfTimeRange
.Null
) ?
null : fTimeRange
.getEndTime();
842 long initialNbEvents
= fNbEvents
;
845 public void handleStarted() {
846 super.handleStarted();
850 public void handleData(TmfEvent event
) {
851 super.handleData(event
);
853 TmfTimestamp ts
= event
.getTimestamp();
854 if (startTime
== null)
855 startTime
= new TmfTimestamp(ts
);
856 lastTime
= new TmfTimestamp(ts
);
857 if ((getNbRead() % fIndexPageSize
) == 1 && getNbRead() != 1) {
864 public void handleSuccess() {
865 // long indexingEnd = System.nanoTime();
867 if (getRange() != TmfTimeRange
.Eternity
) {
868 lastTime
= getRange().getEndTime();
871 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
873 // long average = (indexingEnd - indexingStart) / fNbEvents;
874 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
875 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
876 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
877 // + (average % 1000) + " us/evt)");
878 super.handleSuccess();
882 public void handleCompleted() {
884 super.handleCompleted();
885 synchronized (fCheckpoints
) {
887 if (fIndexingPendingRange
!= TmfTimeRange
.Null
) {
888 indexExperiment(false, (int) fNbEvents
, fIndexingPendingRange
);
889 fIndexingPendingRange
= TmfTimeRange
.Null
;
894 private void updateExperiment() {
895 int nbRead
= getNbRead();
896 if (startTime
!= null) {
897 fTimeRange
= new TmfTimeRange(startTime
, new TmfTimestamp(lastTime
));
900 // updateTimeRange();
902 fNbEvents
= initialNbEvents
+ nbRead
;
908 sendRequest((ITmfDataRequest
<T
>) request
);
909 if (waitForCompletion
)
911 request
.waitForCompletion();
912 } catch (InterruptedException e
) {
917 protected void notifyListeners() {
918 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
919 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
922 // ------------------------------------------------------------------------
924 // ------------------------------------------------------------------------
927 public void experimentSelected(TmfExperimentSelectedSignal
<T
> signal
) {
928 TmfExperiment
<?
> experiment
= signal
.getExperiment();
929 if (experiment
== this) {
930 setCurrentExperiment(experiment
);
931 fEndSynchReference
= new Integer(signal
.getReference());
936 public void endSync(TmfEndSynchSignal signal
) {
937 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
938 fEndSynchReference
= null;
939 initializeStreamingMonitor();
945 public void experimentUpdated(TmfExperimentUpdatedSignal signal
) {
949 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal
) {
950 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
954 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
955 for (ITmfTrace
<T
> trace
: fTraces
) {
956 if (trace
== signal
.getTrace()) {
957 synchronized (fCheckpoints
) {
959 if (fIndexingPendingRange
== TmfTimeRange
.Null
) {
960 fIndexingPendingRange
= signal
.getRange();
962 TmfTimestamp startTime
= fIndexingPendingRange
.getStartTime();
963 TmfTimestamp endTime
= fIndexingPendingRange
.getEndTime();
964 if (signal
.getRange().getStartTime().compareTo(startTime
) < 0) {
965 startTime
= signal
.getRange().getStartTime();
967 if (signal
.getRange().getEndTime().compareTo(endTime
) > 0) {
968 endTime
= signal
.getRange().getEndTime();
970 fIndexingPendingRange
= new TmfTimeRange(startTime
, endTime
);
975 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
982 public String
getPath() {
983 // TODO Auto-generated method stub